• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright (c) 2021, Google Inc.
2  *
3  * Permission to use, copy, modify, and/or distribute this software for any
4  * purpose with or without fee is hereby granted, provided that the above
5  * copyright notice and this permission notice appear in all copies.
6  *
7  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
10  * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
14 
15 #include <string>
16 #include <vector>
17 
18 #include <openssl/bio.h>
19 #include <openssl/conf.h>
20 
21 #include <gtest/gtest.h>
22 
23 #include "internal.h"
24 
25 
TEST(ConfTest,Parse)26 TEST(ConfTest, Parse) {
27   // Check that basic parsing works. (We strongly recommend that people don't
28   // use the [N]CONF functions.)
29 
30   static const char kConf[] = R"(
31 # Comment
32 
33 key=value
34 
35 [section_name]
36 key=value2
37 )";
38 
39   bssl::UniquePtr<BIO> bio(BIO_new_mem_buf(kConf, sizeof(kConf) - 1));
40   ASSERT_TRUE(bio);
41   bssl::UniquePtr<CONF> conf(NCONF_new(nullptr));
42   ASSERT_TRUE(conf);
43   ASSERT_TRUE(NCONF_load_bio(conf.get(), bio.get(), nullptr));
44   EXPECT_TRUE(NCONF_get_section(conf.get(), "section_name"));
45   EXPECT_FALSE(NCONF_get_section(conf.get(), "other_section"));
46   EXPECT_STREQ(NCONF_get_string(conf.get(), nullptr, "key"), "value");
47   EXPECT_STREQ(NCONF_get_string(conf.get(), "section_name", "key"), "value2");
48   EXPECT_STREQ(NCONF_get_string(conf.get(), "other_section", "key"), nullptr);
49 }
50 
TEST(ConfTest,ParseList)51 TEST(ConfTest, ParseList) {
52   const struct {
53     const char *list;
54     char sep;
55     bool remove_whitespace;
56     std::vector<std::string> expected;
57   } kTests[] = {
58       {"", ',', /*remove_whitespace=*/0, {""}},
59       {"", ',', /*remove_whitespace=*/1, {""}},
60 
61       {" ", ',', /*remove_whitespace=*/0, {" "}},
62       {" ", ',', /*remove_whitespace=*/1, {""}},
63 
64       {"hello world", ',', /*remove_whitespace=*/0, {"hello world"}},
65       {"hello world", ',', /*remove_whitespace=*/1, {"hello world"}},
66 
67       {" hello world ", ',', /*remove_whitespace=*/0, {" hello world "}},
68       {" hello world ", ',', /*remove_whitespace=*/1, {"hello world"}},
69 
70       {"hello,world", ',', /*remove_whitespace=*/0, {"hello", "world"}},
71       {"hello,world", ',', /*remove_whitespace=*/1, {"hello", "world"}},
72 
73       {"hello,,world", ',', /*remove_whitespace=*/0, {"hello", "", "world"}},
74       {"hello,,world", ',', /*remove_whitespace=*/1, {"hello", "", "world"}},
75 
76       {"\tab cd , , ef gh ",
77        ',',
78        /*remove_whitespace=*/0,
79        {"\tab cd ", " ", " ef gh "}},
80       {"\tab cd , , ef gh ",
81        ',',
82        /*remove_whitespace=*/1,
83        {"ab cd", "", "ef gh"}},
84   };
85   for (const auto& t : kTests) {
86     SCOPED_TRACE(t.list);
87     SCOPED_TRACE(t.sep);
88     SCOPED_TRACE(t.remove_whitespace);
89 
90     std::vector<std::string> result;
91     auto append_to_vector = [](const char *elem, size_t len, void *arg) -> int {
92       auto *vec = static_cast<std::vector<std::string> *>(arg);
93       vec->push_back(std::string(elem, len));
94       return 1;
95     };
96     ASSERT_TRUE(CONF_parse_list(t.list, t.sep, t.remove_whitespace,
97                                 append_to_vector, &result));
98     EXPECT_EQ(result, t.expected);
99   }
100 }
101