Debian Codegraph @

debian/rust-vendor/aws-lc-sys/aws-lc/crypto/conf/conf_test.cc

raw
1 // Copyright (c) 2021, Google Inc.
2 // SPDX-License-Identifier: ISC
3
4 #include <algorithm>
5 #include <string>
6 #include <vector>
7 #include <map>
8
9 #include <openssl/bio.h>
10 #include <openssl/conf.h>
11 #include "../test/test_util.h"
12
13 #include <gtest/gtest.h>
14
15 #include "internal.h"
16
17
18 // A |CONF| is an unordered list of sections, where each section contains an
19 // ordered list of (name, value) pairs.
20 using ConfModel =
21 std::map<std::string, std::vector<std::pair<std::string, std::string>>>;
22
23 static void ExpectConfEquals(const CONF *conf, const ConfModel &model) {
24 // There is always a default section, even if empty. This is an easy mistake
25 // to make in test data, so test for it.
26 EXPECT_NE(model.find("default"), model.end())
27 << "Model does not have a default section";
28
29 size_t total_values = 0;
30 for (const auto &pair : model) {
31 const std::string &section = pair.first;
32 SCOPED_TRACE(section);
33
34 const STACK_OF(CONF_VALUE) *values =
35 NCONF_get_section(conf, section.c_str());
36 ASSERT_TRUE(values);
37 total_values += pair.second.size();
38
39 EXPECT_EQ(sk_CONF_VALUE_num(values), pair.second.size());
40
41 // If the lengths do not match, still compare up to the smaller of the two,
42 // to aid debugging.
43 size_t min_len = std::min(sk_CONF_VALUE_num(values), pair.second.size());
44 for (size_t i = 0; i < min_len; i++) {
45 SCOPED_TRACE(i);
46 const std::string &name = pair.second[i].first;
47 const std::string &value = pair.second[i].second;
48
49 const CONF_VALUE *v = sk_CONF_VALUE_value(values, i);
50 ASSERT_NE(v, nullptr);
51 EXPECT_EQ(v->section, section);
52 EXPECT_EQ(v->name, name);
53 EXPECT_EQ(v->value, value);
54
55 const char *str = NCONF_get_string(conf, section.c_str(), name.c_str());
56 ASSERT_NE(str, nullptr);
57 EXPECT_EQ(str, value);
58
59 if (section == "default") {
60 // nullptr is interpreted as the default section.
61 str = NCONF_get_string(conf, nullptr, name.c_str());
62 ASSERT_NE(str, nullptr);
63 EXPECT_EQ(str, value);
64 }
65 }
66 }
67
68 // Unrecognized sections must return nullptr.
69 EXPECT_EQ(NCONF_get_section(conf, "must_not_appear_in_tests"), nullptr);
70 EXPECT_EQ(NCONF_get_string(conf, "must_not_appear_in_tests",
71 "must_not_appear_in_tests"),
72 nullptr);
73 if (!model.empty()) {
74 // Valid section, invalid name.
75 EXPECT_EQ(NCONF_get_string(conf, model.begin()->first.c_str(),
76 "must_not_appear_in_tests"),
77 nullptr);
78 if (!model.begin()->second.empty()) {
79 // Invalid section, matching valid name in default returns value from
80 // default.
81 auto key = model.begin()->second.front().first.c_str();
82 auto default_value = NCONF_get_string(conf, "default", key);
83 auto retrieved = NCONF_get_string(conf, "must_not_appear_in_tests", key);
84 if (default_value) {
85 EXPECT_EQ(Bytes(retrieved), Bytes(default_value));
86 } else {
87 EXPECT_EQ(retrieved, nullptr);
88 }
89 }
90 }
91
92 // There should not be any other values in |conf|. |conf| currently stores
93 // both sections and values in the same map.
94 EXPECT_EQ(lh_CONF_VALUE_num_items(conf->data), total_values + model.size());
95 }
96
97 TEST(ConfTest, Parse) {
98 const struct {
99 std::string in;
100 ConfModel model;
101 } kTests[] = {
102 // Test basic parsing.
103 {
104 R"(# Comment
105
106 key=value
107
108 [section_name]
109 key=value2
110 )",
111 {
112 {"default", {{"key", "value"}}},
113 {"section_name", {{"key", "value2"}}},
114 },
115 },
116
117 // If a section is listed multiple times, keys add to the existing one.
118 {
119 R"(key1 = value1
120
121 [section1]
122 key2 = value2
123
124 [section2]
125 key3 = value3
126
127 [default]
128 key4 = value4
129
130 [section1]
131 key5 = value5
132 )",
133 {
134 {"default", {{"key1", "value1"}, {"key4", "value4"}}},
135 {"section1", {{"key2", "value2"}, {"key5", "value5"}}},
136 {"section2", {{"key3", "value3"}}},
137 },
138 },
139
140 // Although the CONF parser internally uses a buffer size of 512 bytes to
141 // read one line, it detects truncation and is able to parse long lines.
142 {
143 std::string(1000, 'a') + " = " + std::string(1000, 'b') + "\n",
144 {
145 {"default", {{std::string(1000, 'a'), std::string(1000, 'b')}}},
146 },
147 },
148
149 // Trailing backslashes are line continations.
150 {
151 "key=\\\nvalue\nkey2=foo\\\nbar=baz",
152 {
153 {"default", {{"key", "value"}, {"key2", "foobar=baz"}}},
154 },
155 },
156
157 // To be a line continuation, it must be at the end of the line.
158 {
159 "key=\\\nvalue\nkey2=foo\\ \nbar=baz",
160 {
161 {"default", {{"key", "value"}, {"key2", "foo"}, {"bar", "baz"}}},
162 },
163 },
164
165 // A line continuation without any following line is ignored.
166 {
167 "key=value\\",
168 {
169 {"default", {{"key", "value"}}},
170 },
171 },
172
173 // Values may have embedded whitespace, but leading and trailing
174 // whitespace is dropped.
175 {
176 "key = \t foo \t\t\tbar \t ",
177 {
178 {"default", {{"key", "foo \t\t\tbar"}}},
179 },
180 },
181
182 // Empty sections still end up in the file.
183 {
184 "[section1]\n[section2]\n[section3]\n",
185 {
186 {"default", {}},
187 {"section1", {}},
188 {"section2", {}},
189 {"section3", {}},
190 },
191 },
192
193 // Section names can contain spaces and punctuation.
194 {
195 "[This! Is. A? Section;]\nkey = value",
196 {
197 {"default", {}},
198 {"This! Is. A? Section;", {{"key", "value"}}},
199 },
200 },
201
202 // Trailing data after a section line is ignored.
203 {
204 "[section] key = value\nkey2 = value2\n",
205 {
206 {"default", {}},
207 {"section", {{"key2", "value2"}}},
208 },
209 },
210
211 // Comments may appear within a line. Escapes and quotes, however,
212 // suppress the comment character.
213 {
214 R"(
215 key1 = # comment
216 key2 = "# not a comment"
217 key3 = '# not a comment'
218 key4 = `# not a comment`
219 key5 = \# not a comment
220 )",
221 {
222 {"default",
223 {
224 {"key1", ""},
225 {"key2", "# not a comment"},
226 {"key3", "# not a comment"},
227 {"key4", "# not a comment"},
228 {"key5", "# not a comment"},
229 }},
230 },
231 },
232
233 // Quotes may appear in the middle of a string. Inside quotes, escape
234 // sequences like \n are not evaluated. \X always evaluates to X.
235 {
236 R"(
237 key1 = mix "of" 'different' `quotes`
238 key2 = "`'"
239 key3 = "\r\n\b\t\""
240 key4 = '\r\n\b\t\''
241 key5 = `\r\n\b\t\``
242 )",
243 {
244 {"default",
245 {
246 {"key1", "mix of different quotes"},
247 {"key2", "`'"},
248 {"key3", "rnbt\""},
249 {"key4", "rnbt'"},
250 {"key5", "rnbt`"},
251 }},
252 },
253 },
254
255 // Outside quotes, escape sequences like \n are evaluated. Unknown escapes
256 // turn into the character.
257 {
258 R"(
259 key = \r\n\b\t\"\'\`\z
260 )",
261 {
262 {"default",
263 {
264 {"key", "\r\n\b\t\"'`z"},
265 }},
266 },
267 },
268
269 // Escapes (but not quoting) work inside section names.
270 {
271 "[section\\ name]\nkey = value\n",
272 {
273 {"default", {}},
274 {"section name", {{"key", "value"}}},
275 },
276 },
277
278 // Escapes (but not quoting) are skipped over in key names, but they are
279 // left unevaluated. This is probably a bug.
280 {
281 "key\\ name = value\n",
282 {
283 {"default", {{"key\\ name", "value"}}},
284 },
285 },
286
287 // Keys can specify sections explicitly with ::.
288 {
289 R"(
290 [section1]
291 default::key1 = value1
292 section1::key2 = value2
293 section2::key3 = value3
294 section1::key4 = value4
295 section2::key5 = value5
296 default::key6 = value6
297 key7 = value7 # section1
298 )",
299 {
300 {"default", {{"key1", "value1"}, {"key6", "value6"}}},
301 {"section1",
302 {{"key2", "value2"}, {"key4", "value4"}, {"key7", "value7"}}},
303 {"section2", {{"key3", "value3"}, {"key5", "value5"}}},
304 },
305 },
306
307 // Punctuation is allowed in key names.
308 {
309 "key.1 = value\n",
310 {
311 {"default", {{"key.1", "value"}}},
312 },
313 },
314
315 // Variable references have been readded.
316 {
317 R"(
318 key1 = value1
319 key2 = $key1
320 )",
321 {
322 {
323 "default",
324 {{"key1", "value1"}, {"key2", "value1"}},
325 },
326 },
327 },
328
329 // Variable expansion with curly braces ${foo}
330 {
331 R"(
332 key1 = value1
333 key2 = ${key1}
334 )",
335 {
336 {
337 "default",
338 {{"key1", "value1"}, {"key2", "value1"}},
339 },
340 },
341 },
342
343 // Variable expansion with parentheses $(foo)
344 {
345 R"(
346 key1 = value1
347 key2 = $(key1)
348 )",
349 {
350 {
351 "default",
352 {{"key1", "value1"}, {"key2", "value1"}},
353 },
354 },
355 },
356
357 // Variable expansion with mixed content (prefix and suffix)
358 {
359 R"(
360 key1 = middle
361 key2 = prefix_${key1}_suffix
362 )",
363 {
364 {
365 "default",
366 {{"key1", "middle"}, {"key2", "prefix_middle_suffix"}},
367 },
368 },
369 },
370
371 // Multiple variable expansions in one value
372 {
373 R"(
374 key1 = hello
375 key2 = world
376 key3 = $key1 $key2
377 )",
378 {
379 {
380 "default",
381 {{"key1", "hello"}, {"key2", "world"}, {"key3", "hello world"}},
382 },
383 },
384 },
385
386 // Chained variable expansion
387 {
388 R"(
389 key1 = base
390 key2 = $key1
391 key3 = $key2
392 )",
393 {
394 {
395 "default",
396 {{"key1", "base"}, {"key2", "base"}, {"key3", "base"}},
397 },
398 },
399 },
400
401 // Cross-section variable reference with ${section::key}
402 {
403 R"(
404 [section1]
405 key1 = from_section1
406
407 [section2]
408 key2 = ${section1::key1}
409 )",
410 {
411 {"default", {}},
412 {"section1", {{"key1", "from_section1"}}},
413 {"section2", {{"key2", "from_section1"}}},
414 },
415 },
416
417 // Cross-section variable reference with $section::key (no braces)
418 {
419 R"(
420 [section1]
421 key1 = from_section1
422
423 [section2]
424 key2 = $section1::key1
425 )",
426 {
427 {"default", {}},
428 {"section1", {{"key1", "from_section1"}}},
429 {"section2", {{"key2", "from_section1"}}},
430 },
431 },
432
433 // Cross-section reference to default section
434 {
435 R"(
436 key1 = from_default
437
438 [section1]
439 key2 = ${default::key1}
440 )",
441 {
442 {"default", {{"key1", "from_default"}}},
443 {"section1", {{"key2", "from_default"}}},
444 },
445 },
446
447 // Variable name with underscore
448 {
449 R"(
450 key_with_underscore = value1
451 key2 = $key_with_underscore
452 )",
453 {
454 {
455 "default",
456 {{"key_with_underscore", "value1"}, {"key2", "value1"}},
457 },
458 },
459 },
460
461 // Variable name with numbers
462 {
463 R"(
464 key123 = value1
465 key2 = $key123
466 )",
467 {
468 {
469 "default",
470 {{"key123", "value1"}, {"key2", "value1"}},
471 },
472 },
473 },
474
475 // Variable at start, middle, and end
476 {
477 R"(
478 v = X
479 start = $v is here
480 middle = here $v is
481 end = here is $v
482 )",
483 {
484 {
485 "default",
486 {{"v", "X"},
487 {"start", "X is here"},
488 {"middle", "here X is"},
489 {"end", "here is X"}},
490 },
491 },
492 },
493
494 // Variable referencing same section implicitly
495 {
496 R"(
497 [mysection]
498 base = myvalue
499 derived = $base
500 )",
501 {
502 {"default", {}},
503 {"mysection", {{"base", "myvalue"}, {"derived", "myvalue"}}},
504 },
505 },
506
507 // Variable reference with parentheses and cross-section
508 {
509 R"(
510 [section1]
511 key1 = paren_value
512
513 [section2]
514 key2 = $(section1::key1)
515 )",
516 {
517 {"default", {}},
518 {"section1", {{"key1", "paren_value"}}},
519 {"section2", {{"key2", "paren_value"}}},
520 },
521 },
522
523 // Empty variable expansion (variable with empty value)
524 {
525 R"(
526 empty =
527 key2 = prefix${empty}suffix
528 )",
529 {
530 {
531 "default",
532 {{"empty", ""}, {"key2", "prefixsuffix"}},
533 },
534 },
535 },
536 };
537 for (const auto &t : kTests) {
538 SCOPED_TRACE(t.in);
539 bssl::UniquePtr<BIO> bio(BIO_new_mem_buf(t.in.data(), t.in.size()));
540 ASSERT_TRUE(bio);
541 bssl::UniquePtr<CONF> conf(NCONF_new(nullptr));
542 ASSERT_TRUE(conf);
543 ASSERT_TRUE(NCONF_load_bio(conf.get(), bio.get(), nullptr));
544
545 ExpectConfEquals(conf.get(), t.model);
546 }
547
548 const char *kInvalidTests[] = {
549 // Missing equals sign.
550 "key",
551 // Unterminated section heading.
552 "[section",
553 // Section names can only contain alphanumeric characters, punctuation,
554 // and escapes. Quotes are not punctuation.
555 "[\"section\"]",
556 // Keys can only contain alphanumeric characters, punctuaion, and escapes.
557 "key name = value",
558 "\"key\" = value",
559 // Variable expansion: undefined variable.
560 "key = $undefined_var",
561 // Variable expansion: missing closing brace.
562 "key = ${foo",
563 // Variable expansion: missing closing parenthesis.
564 "key = $(foo",
565 // Variable expansion: undefined cross-section variable.
566 "key = ${nonexistent_section::key}",
567 // Unterminated quotes (CONF_R_NO_CLOSE_QUOTE).
568 "key = \"unterminated",
569 "key = 'unterminated",
570 "key = `unterminated",
571 };
572 for (const auto &t : kInvalidTests) {
573 SCOPED_TRACE(t);
574 bssl::UniquePtr<BIO> bio(BIO_new_mem_buf(t, strlen(t)));
575 ASSERT_TRUE(bio);
576 bssl::UniquePtr<CONF> conf(NCONF_new(nullptr));
577 ASSERT_TRUE(conf);
578 EXPECT_FALSE(NCONF_load_bio(conf.get(), bio.get(), nullptr));
579 }
580 }
581
582 TEST(ConfTest, ParseList) {
583 const struct {
584 const char *list;
585 char sep;
586 bool remove_whitespace;
587 std::vector<std::string> expected;
588 } kTests[] = {
589 {"", ',', /*remove_whitespace=*/0, {""}},
590 {"", ',', /*remove_whitespace=*/1, {""}},
591
592 {" ", ',', /*remove_whitespace=*/0, {" "}},
593 {" ", ',', /*remove_whitespace=*/1, {""}},
594
595 {"hello world", ',', /*remove_whitespace=*/0, {"hello world"}},
596 {"hello world", ',', /*remove_whitespace=*/1, {"hello world"}},
597
598 {" hello world ", ',', /*remove_whitespace=*/0, {" hello world "}},
599 {" hello world ", ',', /*remove_whitespace=*/1, {"hello world"}},
600
601 {"hello,world", ',', /*remove_whitespace=*/0, {"hello", "world"}},
602 {"hello,world", ',', /*remove_whitespace=*/1, {"hello", "world"}},
603
604 {"hello,,world", ',', /*remove_whitespace=*/0, {"hello", "", "world"}},
605 {"hello,,world", ',', /*remove_whitespace=*/1, {"hello", "", "world"}},
606
607 {"\tab cd , , ef gh ",
608 ',',
609 /*remove_whitespace=*/0,
610 {"\tab cd ", " ", " ef gh "}},
611 {"\tab cd , , ef gh ",
612 ',',
613 /*remove_whitespace=*/1,
614 {"ab cd", "", "ef gh"}},
615 };
616 for (const auto& t : kTests) {
617 SCOPED_TRACE(t.list);
618 SCOPED_TRACE(t.sep);
619 SCOPED_TRACE(t.remove_whitespace);
620
621 std::vector<std::string> result;
622 auto append_to_vector = [](const char *elem, size_t len, void *arg) -> int {
623 auto *vec = static_cast<std::vector<std::string> *>(arg);
624 vec->push_back(std::string(elem, len));
625 return 1;
626 };
627 ASSERT_TRUE(CONF_parse_list(t.list, t.sep, t.remove_whitespace,
628 append_to_vector, &result));
629 EXPECT_EQ(result, t.expected);
630 }
631 }
632
633 TEST(ConfTest, NoopString) {
634 bssl::UniquePtr<char> string(CONF_get1_default_config_file());
635 EXPECT_STREQ("No support for Config files in AWS-LC.", string.get());
636 }