Debian Codegraph @

qt6/tests/check_distinguished_name_parser.cpp

raw
1 //========================================================================
2 //
3 // check_distinguished_name_parser.h
4 //
5 // This file is licensed under the GPLv2 or later
6 //
7 // Copyright 2023, 2025 g10 Code GmbH, Author: Sune Stolborg Vuorela <[email protected]>
8 // Copyright 2025 Ingo Klöcker <[email protected]>
9 //========================================================================
10 #include "DistinguishedNameParser.h"
11
12 #include <QtTest/QTest>
13 #include <iostream>
14
15 class TestDistinguishedNameParser : public QObject
16 {
17 Q_OBJECT
18 public:
19 explicit TestDistinguishedNameParser(QObject *parent = nullptr) : QObject(parent) { }
20 private Q_SLOTS:
21 // The big set of input/output. Several of the helper functions can be tested independently
22 void testParser();
23 void testParser_data();
24
25 void testRemoveLeadingSpaces();
26 void testRemoveLeadingSpaces_data();
27
28 void testRemoveTrailingSpaces();
29 void testRemoveTrailingSpaces_data();
30
31 void testParseHexString();
32 void testParseHexString_data();
33 };
34
35 void TestDistinguishedNameParser::testParser()
36 {
37 QFETCH(std::string, inputData);
38 QFETCH(DN::Result, expectedResult);
39
40 auto result = DN::parseString(inputData);
41 QCOMPARE(result, expectedResult);
42 }
43
44 void TestDistinguishedNameParser::testParser_data()
45 {
46 QTest::addColumn<std::string>("inputData");
47 QTest::addColumn<DN::Result>("expectedResult");
48
49 QTest::newRow("empty") << std::string {} << DN::Result {};
50 QTest::newRow("CN=Simple") << std::string { "CN=Simple" } << DN::Result { { "CN", "Simple" } };
51 QTest::newRow("CN=Name with spaces") << std::string { "CN=Name with spaces" } << DN::Result { { "CN", "Name with spaces" } };
52 QTest::newRow("CN=Simple,O=Silly") << std::string { "CN=Simple,O=Silly" } << DN::Result { { "CN", "Simple" }, { "O", "Silly" } };
53 QTest::newRow("CN=Steve Kille,O=Isode Limited,C=GB") << std::string { "CN=Steve Kille,O=Isode Limited,C=GB" } << DN::Result { { "CN", "Steve Kille" }, { "O", "Isode Limited" }, { "C", "GB" } };
54 QTest::newRow("[email protected], O=MyCompany, L=San Diego,ST=California, C=US")
55 << std::string { "[email protected], O=MyCompany, L=San Diego,ST=California, C=US" } << DN::Result { { "CN", "[email protected]" }, { "O", "MyCompany" }, { "L", "San Diego" }, { "ST", "California" }, { "C", "US" } };
56 QTest::newRow("Multi valued") << std::string { "OU=Sales+CN=J. Smith,O=Widget Inc.,C=US" }
57 << DN::Result { { "OU", "Sales" }, { "CN", "J. Smith" }, { "O", "Widget Inc." }, { "C", "US" } }; // This is technically wrong, but probably good enough for now
58 QTest::newRow("Escaping comma") << std::string { "CN=L. Eagle,O=Sue\\, Grabbit and Runn,C=GB" } << DN::Result { { "CN", "L. Eagle" }, { "O", "Sue, Grabbit and Runn" }, { "C", "GB" } };
59 QTest::newRow("Escaped trailing space") << std::string { "CN=Trailing space\\ " } << DN::Result { { "CN", "Trailing space " } };
60 QTest::newRow("Escaped quote") << std::string { "CN=Quotation \\\" Mark" } << DN::Result { { "CN", "Quotation \" Mark" } };
61
62 QTest::newRow("CN=Simple with escaping") << std::string { R"(CN=S\69mpl\65\7A)" } << DN::Result { { "CN", "Simplez" } };
63 QTest::newRow(R"(SN=Lu\C4\8Di\C4\87)") << std::string { R"(SN=Lu\C4\8Di\C4\87)" } << DN::Result { { "SN", "Lučić" } };
64 QTest::newRow("CN=\"Quoted name\"") << std::string { "CN=\"Quoted name\"" } << DN::Result { { "CN", "Quoted name" } };
65 QTest::newRow("CN=\" Leading and trailing spacees \"") << std::string { "CN=\" Leading and trailing spaces \"" } << DN::Result { { "CN", " Leading and trailing spaces " } };
66 QTest::newRow("Comma in quotes") << std::string { "CN=\"Comma, inside\"" } << DN::Result { { "CN", "Comma, inside" } };
67 QTest::newRow("forbidden chars in quotes") << std::string { R"(CN="Forbidden !@#$%&*()<>[]{},.?/\| chars")" } << DN::Result { { "CN", "Forbidden !@#$%&*()<>[]{},.?/\\| chars" } };
68 QTest::newRow("Quoted quotation") << std::string { R"(CN="Quotation \" Mark")" } << DN::Result { { "CN", "Quotation \" Mark" } };
69 QTest::newRow("Quoted quotation") << std::string { R"(CN="Quotation \" Mark\" Multiples")" } << DN::Result { { "CN", "Quotation \" Mark\" Multiples" } };
70
71 QTest::newRow("frompdf1") << std::string { "2.5.4.97=#5553742D49644E722E20444520313233343735323233,CN=TeleSec PKS eIDAS QES CA 5,O=Deutsche Telekom AG,C=DE" }
72 << DN::Result { { "2.5.4.97", "USt-IdNr. DE 123475223" }, { "CN", "TeleSec PKS eIDAS QES CA 5" }, { "O", "Deutsche Telekom AG" }, { "C", "DE" } };
73 QTest::newRow("frompdf1a") << std::string { "2.5.4.97=#5553742d49644E722e20444520313233343735323233,CN=TeleSec PKS eIDAS QES CA 5,O=Deutsche Telekom AG,C=DE" }
74 << DN::Result { { "2.5.4.97", "USt-IdNr. DE 123475223" }, { "CN", "TeleSec PKS eIDAS QES CA 5" }, { "O", "Deutsche Telekom AG" }, { "C", "DE" } };
75 QTest::newRow("frompdf2") << std::string { "2.5.4.5=#34,CN=Koch\\, Werner,2.5.4.42=#5765726E6572,2.5.4.4=#4B6F6368,C=DE" }
76 << DN::Result { { "SerialNumber", "4" }, { "CN", "Koch, Werner" }, { "GN", "Werner" }, { "SN", "Koch" }, { "C", "DE" } };
77 QTest::newRow("frompdf2a") << std::string { "2.5.4.5=#34,CN=Koch\\, Werner,oid.2.5.4.42=#5765726E6572,OID.2.5.4.4=#4B6F6368,C=DE" }
78 << DN::Result { { "SerialNumber", "4" }, { "CN", "Koch, Werner" }, { "GN", "Werner" }, { "SN", "Koch" }, { "C", "DE" } };
79 QTest::newRow("ends with hex string") << std::string { "2.5.4.5=#34" } << DN::Result { { "SerialNumber", "4" } };
80
81 // weird spacing
82 QTest::newRow("CN =Simple") << std::string { "CN =Simple" } << DN::Result { { "CN", "Simple" } };
83 QTest::newRow("CN= Simple") << std::string { "CN= Simple" } << DN::Result { { "CN", "Simple" } };
84 QTest::newRow("CN=Simple ") << std::string { "CN=Simple " } << DN::Result { { "CN", "Simple" } };
85 QTest::newRow("CN=Simple,") << std::string { "CN=Simple," } << DN::Result { { "CN", "Simple" } };
86 QTest::newRow("CN=Simple, O=Silly") << std::string { "CN=Simple, O=Silly" } << DN::Result { { "CN", "Simple" }, { "O", "Silly" } };
87
88 // various malformed
89 QTest::newRow("CN=Simple\\") << std::string { "CN=Simple\\" } << DN::Result {};
90 QTest::newRow("CN=") << std::string { "CN=" } << DN::Result {};
91 QTest::newRow("CN=Simple\\X") << std::string { "CN=Simple\\X" } << DN::Result {};
92 QTest::newRow("CN=Simple, O") << std::string { "CN=Simple, O" } << DN::Result {};
93 QTest::newRow("CN=Sim\"ple") << std::string { "CN=Sim\"ple, O" } << DN::Result {};
94 QTest::newRow("CN=Simple\\a") << std::string { "CN=Simple\\a" } << DN::Result {};
95 QTest::newRow("=Simple") << std::string { "=Simple" } << DN::Result {};
96 QTest::newRow("CN=\"Simple") << std::string { "CN=\"Simple" } << DN::Result {};
97 QTest::newRow("CN=\"Simple") << std::string { "CN=\"Simple\\" } << DN::Result {};
98 QTest::newRow("unquoted quotation in quotation") << std::string { R"("CN="Quotation " Mark")" } << DN::Result {};
99 }
100
101 void TestDistinguishedNameParser::testRemoveLeadingSpaces()
102 {
103 QFETCH(std::string, input);
104 QFETCH(std::string, expectedOutput);
105
106 auto result = DN::detail::removeLeadingSpaces(input);
107 QCOMPARE(result, expectedOutput);
108 }
109 void TestDistinguishedNameParser::testRemoveLeadingSpaces_data()
110 {
111 QTest::addColumn<std::string>("input");
112 QTest::addColumn<std::string>("expectedOutput");
113
114 QTest::newRow("Empty") << std::string {} << std::string {};
115 QTest::newRow("No leading spaces") << std::string { "horse" } << std::string { "horse" };
116 QTest::newRow("Some spaces") << std::string { " horse" } << std::string { "horse" };
117 QTest::newRow("Some leading and trailing") << std::string { " horse " } << std::string { "horse " };
118 }
119
120 void TestDistinguishedNameParser::testRemoveTrailingSpaces()
121 {
122 QFETCH(std::string, input);
123 QFETCH(std::string, expectedOutput);
124
125 auto result = DN::detail::removeTrailingSpaces(input);
126 QCOMPARE(result, expectedOutput);
127 }
128 void TestDistinguishedNameParser::testRemoveTrailingSpaces_data()
129 {
130 QTest::addColumn<std::string>("input");
131 QTest::addColumn<std::string>("expectedOutput");
132
133 QTest::newRow("Empty") << std::string {} << std::string {};
134 QTest::newRow("No leading spaces") << std::string { "horse" } << std::string { "horse" };
135 QTest::newRow("Some spaces") << std::string { "horse " } << std::string { "horse" };
136 QTest::newRow("Some leading and trailing") << std::string { " horse " } << std::string { " horse" };
137 }
138
139 void TestDistinguishedNameParser::testParseHexString()
140 {
141 QFETCH(std::string, input);
142 QFETCH(std::optional<std::string>, expectedOutput);
143
144 auto result = DN::detail::parseHexString(input);
145 QCOMPARE(result, expectedOutput);
146 }
147
148 void TestDistinguishedNameParser::testParseHexString_data()
149 {
150 QTest::addColumn<std::string>("input");
151 QTest::addColumn<std::optional<std::string>>("expectedOutput");
152
153 QTest::newRow("4") << std::string { "34" } << std::optional<std::string>("4");
154 QTest::newRow("Koch") << std::string { "4B6F6368" } << std::optional<std::string>("Koch");
155 QTest::newRow("USt-IdNr. DE 123475223") << std::string { "5553742D49644E722E20444520313233343735323233" } << std::optional<std::string>("USt-IdNr. DE 123475223");
156
157 // various baddies
158 QTest::newRow("empty") << std::string {} << std::optional<std::string> {};
159 QTest::newRow("FFF") << std::string { "FFF" } << std::optional<std::string> {};
160 QTest::newRow("F") << std::string { "F" } << std::optional<std::string> {};
161 QTest::newRow("XX") << std::string { "XX" } << std::optional<std::string> {};
162 }
163
164 QTEST_GUILESS_MAIN(TestDistinguishedNameParser);
165 #include "check_distinguished_name_parser.moc"