Debian Codegraph @

qt5/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 Q_DECLARE_METATYPE(DN::Result);
36 Q_DECLARE_METATYPE(std::string);
37 Q_DECLARE_METATYPE(std::optional<std::string>);
38
39 void TestDistinguishedNameParser::testParser()
40 {
41 QFETCH(std::string, inputData);
42 QFETCH(DN::Result, expectedResult);
43
44 auto result = DN::parseString(inputData);
45 QCOMPARE(result, expectedResult);
46 }
47
48 void TestDistinguishedNameParser::testParser_data()
49 {
50 QTest::addColumn<std::string>("inputData");
51 QTest::addColumn<DN::Result>("expectedResult");
52
53 QTest::newRow("empty") << std::string {} << DN::Result {};
54 QTest::newRow("CN=Simple") << std::string { "CN=Simple" } << DN::Result { { "CN", "Simple" } };
55 QTest::newRow("CN=Name with spaces") << std::string { "CN=Name with spaces" } << DN::Result { { "CN", "Name with spaces" } };
56 QTest::newRow("CN=Simple,O=Silly") << std::string { "CN=Simple,O=Silly" } << DN::Result { { "CN", "Simple" }, { "O", "Silly" } };
57 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" } };
58 QTest::newRow("[email protected], O=MyCompany, L=San Diego,ST=California, C=US")
59 << 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" } };
60 QTest::newRow("Multi valued") << std::string { "OU=Sales+CN=J. Smith,O=Widget Inc.,C=US" }
61 << DN::Result { { "OU", "Sales" }, { "CN", "J. Smith" }, { "O", "Widget Inc." }, { "C", "US" } }; // This is technically wrong, but probably good enough for now
62 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" } };
63 QTest::newRow("Escaped trailing space") << std::string { "CN=Trailing space\\ " } << DN::Result { { "CN", "Trailing space " } };
64 QTest::newRow("Escaped quote") << std::string { "CN=Quotation \\\" Mark" } << DN::Result { { "CN", "Quotation \" Mark" } };
65
66 QTest::newRow("CN=Simple with escaping") << std::string { R"(CN=S\69mpl\65\7A)" } << DN::Result { { "CN", "Simplez" } };
67 QTest::newRow(R"(SN=Lu\C4\8Di\C4\87)") << std::string { R"(SN=Lu\C4\8Di\C4\87)" } << DN::Result { { "SN", "Lučić" } };
68 QTest::newRow("CN=\"Quoted name\"") << std::string { "CN=\"Quoted name\"" } << DN::Result { { "CN", "Quoted name" } };
69 QTest::newRow("CN=\" Leading and trailing spacees \"") << std::string { "CN=\" Leading and trailing spaces \"" } << DN::Result { { "CN", " Leading and trailing spaces " } };
70 QTest::newRow("Comma in quotes") << std::string { "CN=\"Comma, inside\"" } << DN::Result { { "CN", "Comma, inside" } };
71 QTest::newRow("forbidden chars in quotes") << std::string { R"(CN="Forbidden !@#$%&*()<>[]{},.?/\| chars")" } << DN::Result { { "CN", "Forbidden !@#$%&*()<>[]{},.?/\\| chars" } };
72 QTest::newRow("Quoted quotation") << std::string { R"(CN="Quotation \" Mark")" } << DN::Result { { "CN", "Quotation \" Mark" } };
73 QTest::newRow("Quoted quotation") << std::string { R"(CN="Quotation \" Mark\" Multiples")" } << DN::Result { { "CN", "Quotation \" Mark\" Multiples" } };
74
75 QTest::newRow("frompdf1") << std::string { "2.5.4.97=#5553742D49644E722E20444520313233343735323233,CN=TeleSec PKS eIDAS QES CA 5,O=Deutsche Telekom AG,C=DE" }
76 << DN::Result { { "2.5.4.97", "USt-IdNr. DE 123475223" }, { "CN", "TeleSec PKS eIDAS QES CA 5" }, { "O", "Deutsche Telekom AG" }, { "C", "DE" } };
77 QTest::newRow("frompdf1a") << std::string { "2.5.4.97=#5553742D49644E722e20444520313233343735323233,CN=TeleSec PKS eIDAS QES CA 5,O=Deutsche Telekom AG,C=DE" }
78 << DN::Result { { "2.5.4.97", "USt-IdNr. DE 123475223" }, { "CN", "TeleSec PKS eIDAS QES CA 5" }, { "O", "Deutsche Telekom AG" }, { "C", "DE" } };
79 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" }
80 << DN::Result { { "SerialNumber", "4" }, { "CN", "Koch, Werner" }, { "GN", "Werner" }, { "SN", "Koch" }, { "C", "DE" } };
81 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" }
82 << DN::Result { { "SerialNumber", "4" }, { "CN", "Koch, Werner" }, { "GN", "Werner" }, { "SN", "Koch" }, { "C", "DE" } };
83 QTest::newRow("ends with hex string") << std::string { "2.5.4.5=#34" } << DN::Result { { "SerialNumber", "4" } };
84
85 // weird spacing
86 QTest::newRow("CN =Simple") << std::string { "CN =Simple" } << DN::Result { { "CN", "Simple" } };
87 QTest::newRow("CN= Simple") << std::string { "CN= Simple" } << DN::Result { { "CN", "Simple" } };
88 QTest::newRow("CN=Simple ") << std::string { "CN=Simple " } << DN::Result { { "CN", "Simple" } };
89 QTest::newRow("CN=Simple,") << std::string { "CN=Simple," } << DN::Result { { "CN", "Simple" } };
90 QTest::newRow("CN=Simple, O=Silly") << std::string { "CN=Simple, O=Silly" } << DN::Result { { "CN", "Simple" }, { "O", "Silly" } };
91
92 // various malformed
93 QTest::newRow("CN=Simple\\") << std::string { "CN=Simple\\" } << DN::Result {};
94 QTest::newRow("CN=") << std::string { "CN=" } << DN::Result {};
95 QTest::newRow("CN=Simple\\X") << std::string { "CN=Simple\\X" } << DN::Result {};
96 QTest::newRow("CN=Simple, O") << std::string { "CN=Simple, O" } << DN::Result {};
97 QTest::newRow("CN=Sim\"ple") << std::string { "CN=Sim\"ple, O" } << DN::Result {};
98 QTest::newRow("CN=Simple\\a") << std::string { "CN=Simple\\a" } << DN::Result {};
99 QTest::newRow("=Simple") << std::string { "=Simple" } << DN::Result {};
100 QTest::newRow("CN=\"Simple") << std::string { "CN=\"Simple" } << DN::Result {};
101 QTest::newRow("CN=\"Simple") << std::string { "CN=\"Simple\\" } << DN::Result {};
102 QTest::newRow("unquoted quotation in quotation") << std::string { R"("CN="Quotation " Mark")" } << DN::Result {};
103 }
104
105 void TestDistinguishedNameParser::testRemoveLeadingSpaces()
106 {
107 QFETCH(std::string, input);
108 QFETCH(std::string, expectedOutput);
109
110 auto result = DN::detail::removeLeadingSpaces(input);
111 QCOMPARE(result, expectedOutput);
112 }
113 void TestDistinguishedNameParser::testRemoveLeadingSpaces_data()
114 {
115 QTest::addColumn<std::string>("input");
116 QTest::addColumn<std::string>("expectedOutput");
117
118 QTest::newRow("Empty") << std::string {} << std::string {};
119 QTest::newRow("No leading spaces") << std::string { "horse" } << std::string { "horse" };
120 QTest::newRow("Some spaces") << std::string { " horse" } << std::string { "horse" };
121 QTest::newRow("Some leading and trailing") << std::string { " horse " } << std::string { "horse " };
122 }
123
124 void TestDistinguishedNameParser::testRemoveTrailingSpaces()
125 {
126 QFETCH(std::string, input);
127 QFETCH(std::string, expectedOutput);
128
129 auto result = DN::detail::removeTrailingSpaces(input);
130 QCOMPARE(result, expectedOutput);
131 }
132 void TestDistinguishedNameParser::testRemoveTrailingSpaces_data()
133 {
134 QTest::addColumn<std::string>("input");
135 QTest::addColumn<std::string>("expectedOutput");
136
137 QTest::newRow("Empty") << std::string {} << std::string {};
138 QTest::newRow("No leading spaces") << std::string { "horse" } << std::string { "horse" };
139 QTest::newRow("Some spaces") << std::string { "horse " } << std::string { "horse" };
140 QTest::newRow("Some leading and trailing") << std::string { " horse " } << std::string { " horse" };
141 }
142
143 void TestDistinguishedNameParser::testParseHexString()
144 {
145 QFETCH(std::string, input);
146 QFETCH(std::optional<std::string>, expectedOutput);
147
148 auto result = DN::detail::parseHexString(input);
149 QCOMPARE(result, expectedOutput);
150 }
151
152 void TestDistinguishedNameParser::testParseHexString_data()
153 {
154 QTest::addColumn<std::string>("input");
155 QTest::addColumn<std::optional<std::string>>("expectedOutput");
156
157 QTest::newRow("4") << std::string { "34" } << std::optional<std::string>("4");
158 QTest::newRow("Koch") << std::string { "4B6F6368" } << std::optional<std::string>("Koch");
159 QTest::newRow("USt-IdNr. DE 123475223") << std::string { "5553742D49644E722E20444520313233343735323233" } << std::optional<std::string>("USt-IdNr. DE 123475223");
160
161 // various baddies
162 QTest::newRow("empty") << std::string {} << std::optional<std::string> {};
163 QTest::newRow("FFF") << std::string { "FFF" } << std::optional<std::string> {};
164 QTest::newRow("F") << std::string { "F" } << std::optional<std::string> {};
165 QTest::newRow("XX") << std::string { "XX" } << std::optional<std::string> {};
166 }
167
168 QTEST_GUILESS_MAIN(TestDistinguishedNameParser);
169 #include "check_distinguished_name_parser.moc"