alg1485_unittest.cc (4071B)
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 <stdint.h> 8 9 #include "gtest/gtest.h" 10 11 #include "nss.h" 12 #include "nss_scoped_ptrs.h" 13 #include "prprf.h" 14 15 namespace nss_test { 16 17 typedef struct AVATestValuesStr { 18 std::string avaString; 19 bool expectedResult; 20 } AVATestValues; 21 22 typedef struct AVACompareValuesStr { 23 std::string avaString1; 24 std::string avaString2; 25 SECComparison expectedResult; 26 } AVACompareValues; 27 28 class Alg1485Test : public ::testing::Test {}; 29 30 class Alg1485ParseTest : public Alg1485Test, 31 public ::testing::WithParamInterface<AVATestValues> {}; 32 33 class Alg1485CompareTest 34 : public Alg1485Test, 35 public ::testing::WithParamInterface<AVACompareValues> {}; 36 37 static const AVATestValues kAVATestStrings[] = { 38 {"CN=Marshall T. Rose, O=Dover Beach Consulting, L=Santa Clara, " 39 "ST=California, C=US", 40 true}, 41 {"C=HU,L=Budapest,O=Organization,CN=Example - Qualified Citizen " 42 "CA,2.5.4.97=VATHU-10", 43 true}, 44 {"C=HU,L=Budapest,O=Example,CN=Example - Qualified Citizen " 45 "CA,OID.2.5.4.97=VATHU-10", 46 true}, 47 {"CN=Somebody,L=Set,O=Up,C=US,1=The,2=Bomb", true}, 48 {"OID.2.5.4.6=😑", true}, 49 {"2.5.4.6=😑", true}, 50 {"OID.moocow=😑", false}, // OIDs must be numeric 51 {"3.2=bad", false}, // OIDs cannot be overly large; 3 is too big 52 {"256.257=bad", false}, // Still too big 53 {"YO=LO", false}, // Unknown Tag, 'YO' 54 {"CN=Tester,ZZ=Top", false}, // Unknown tag, 'ZZ' 55 // These tests are disabled pending Bug 1363416 56 // { "01.02.03=Nope", false }, // Numbers not in minimal form 57 // { "000001.0000000001=👌", false }, 58 // { "CN=Somebody,L=Set,O=Up,C=US,01=The,02=Bomb", false }, 59 }; 60 61 static const AVACompareValues kAVACompareStrings[] = { 62 {"CN=Max, O=Mozilla, ST=Berlin", "CN=Max, O=Mozilla, ST=Berlin, C=DE", 63 SECLessThan}, 64 {"CN=Max, O=Mozilla, ST=Berlin, C=DE", "CN=Max, O=Mozilla, ST=Berlin", 65 SECGreaterThan}, 66 {"CN=Max, O=Mozilla, ST=Berlin, C=DE", "CN=Max, O=Mozilla, ST=Berlin, C=DE", 67 SECEqual}, 68 {"CN=Max1, O=Mozilla, ST=Berlin, C=DE", 69 "CN=Max2, O=Mozilla, ST=Berlin, C=DE", SECLessThan}, 70 {"CN=Max, O=Mozilla, ST=Berlin, C=DE", "CN=Max, O=Mozilla, ST=Berlin, C=US", 71 SECLessThan}, 72 }; 73 74 TEST_P(Alg1485ParseTest, TryParsingAVAStrings) { 75 const AVATestValues& param(GetParam()); 76 77 ScopedCERTName certName(CERT_AsciiToName(param.avaString.c_str())); 78 ASSERT_EQ(certName != nullptr, param.expectedResult); 79 } 80 81 TEST_P(Alg1485CompareTest, CompareAVAStrings) { 82 const AVACompareValues& param(GetParam()); 83 ScopedCERTName a(CERT_AsciiToName(param.avaString1.c_str())); 84 ScopedCERTName b(CERT_AsciiToName(param.avaString2.c_str())); 85 ASSERT_TRUE(a && b); 86 EXPECT_EQ(param.expectedResult, CERT_CompareName(a.get(), b.get())); 87 } 88 89 INSTANTIATE_TEST_SUITE_P(ParseAVAStrings, Alg1485ParseTest, 90 ::testing::ValuesIn(kAVATestStrings)); 91 INSTANTIATE_TEST_SUITE_P(CompareAVAStrings, Alg1485CompareTest, 92 ::testing::ValuesIn(kAVACompareStrings)); 93 94 TEST_F(Alg1485Test, ShortOIDTest) { 95 // This is not a valid OID (too short). CERT_GetOidString should return 0. 96 unsigned char data[] = {0x05}; 97 const SECItem oid = {siBuffer, data, sizeof(data)}; 98 char* result = CERT_GetOidString(&oid); 99 EXPECT_EQ(result, nullptr); 100 } 101 102 TEST_F(Alg1485Test, BrokenOIDTest) { 103 // This is not a valid OID (first bit of last byte is not set). 104 // CERT_GetOidString should return 0. 105 unsigned char data[] = {0x81, 0x82, 0x83, 0x84}; 106 const SECItem oid = {siBuffer, data, sizeof(data)}; 107 char* result = CERT_GetOidString(&oid); 108 EXPECT_EQ(15U, strlen(result)); 109 EXPECT_EQ(0, strncmp("OID.UNSUPPORTED", result, 15)); 110 PR_smprintf_free(result); 111 } 112 } // namespace nss_test