1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4  * License, v. 2.0. If a copy of the MPL was not distributed with this file,
5  * You can obtain one at http://mozilla.org/MPL/2.0/. */
6 
7 #include <string>
8 
9 #include "gtest/gtest.h"
10 
11 #include "cert.h"
12 #include "certt.h"
13 #include "secitem.h"
14 
15 namespace nss_test {
16 
17 class CERT_FormatNameUnitTest : public ::testing::Test {};
18 
TEST_F(CERT_FormatNameUnitTest,Overflow)19 TEST_F(CERT_FormatNameUnitTest, Overflow) {
20   // Construct a CERTName consisting of a single RDN with 20 organizational unit
21   // AVAs and 20 domain component AVAs. The actual contents don't matter, just
22   // the types.
23 
24   uint8_t oidValueBytes[] = {0x0c, 0x02, 0x58, 0x58};  // utf8String "XX"
25   SECItem oidValue = {siBuffer, oidValueBytes, sizeof(oidValueBytes)};
26   uint8_t oidTypeOUBytes[] = {0x55, 0x04, 0x0b};  // organizationalUnit
27   SECItem oidTypeOU = {siBuffer, oidTypeOUBytes, sizeof(oidTypeOUBytes)};
28   CERTAVA ouAVA = {oidTypeOU, oidValue};
29   uint8_t oidTypeDCBytes[] = {0x09, 0x92, 0x26, 0x89, 0x93,
30                               0xf2, 0x2c, 0x64, 0x1,  0x19};  // domainComponent
31   SECItem oidTypeDC = {siBuffer, oidTypeDCBytes, sizeof(oidTypeDCBytes)};
32   CERTAVA dcAVA = {oidTypeDC, oidValue};
33 
34   const int kNumEachAVA = 20;
35   CERTAVA* avas[(2 * kNumEachAVA) + 1];
36   for (int i = 0; i < kNumEachAVA; i++) {
37     avas[2 * i] = &ouAVA;
38     avas[(2 * i) + 1] = &dcAVA;
39   }
40   avas[2 * kNumEachAVA] = nullptr;
41 
42   CERTRDN rdn = {avas};
43   CERTRDN* rdns[2];
44   rdns[0] = &rdn;
45   rdns[1] = nullptr;
46 
47   std::string expectedResult =
48       "XX<br>XX<br>XX<br>XX<br>XX<br>XX<br>XX<br>XX<br>XX<br>XX<br>XX<br>XX<br>"
49       "XX<br>XX<br>XX<br>XX<br>XX<br>XX<br>XX<br>XX<br>XX<br>XX<br>XX<br>XX<br>"
50       "XX<br>XX<br>XX<br>XX<br>XX<br>XX<br>XX<br>XX<br>XX<br>XX<br>XX<br>XX<br>"
51       "XX<br>XX<br>XX<br>XX<br>";
52 
53   CERTName name = {nullptr, rdns};
54   char* result = CERT_FormatName(&name);
55   EXPECT_EQ(expectedResult, result);
56   PORT_Free(result);
57 }
58 
59 }  // namespace nss_test
60