Write a test for CONF_parse_list. Change-Id: Ied447b1e852b3b9b2bdc9617fa65a0cc1f425f7f Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/54470 Reviewed-by: Bob Beck <bbe@google.com> Commit-Queue: David Benjamin <davidben@google.com>
diff --git a/crypto/conf/conf_test.cc b/crypto/conf/conf_test.cc index 65938e1..fe03c5f 100644 --- a/crypto/conf/conf_test.cc +++ b/crypto/conf/conf_test.cc
@@ -12,11 +12,16 @@ * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ +#include <string> +#include <vector> + #include <openssl/bio.h> #include <openssl/conf.h> #include <gtest/gtest.h> +#include "internal.h" + TEST(ConfTest, Parse) { // Check that basic parsing works. (We strongly recommend that people don't @@ -42,3 +47,54 @@ EXPECT_STREQ(NCONF_get_string(conf.get(), "section_name", "key"), "value2"); EXPECT_STREQ(NCONF_get_string(conf.get(), "other_section", "key"), nullptr); } + +TEST(ConfTest, ParseList) { + const struct { + const char *list; + char sep; + bool remove_whitespace; + std::vector<std::string> expected; + } kTests[] = { + {"", ',', /*remove_whitespace=*/0, {""}}, + {"", ',', /*remove_whitespace=*/1, {""}}, + + {" ", ',', /*remove_whitespace=*/0, {" "}}, + {" ", ',', /*remove_whitespace=*/1, {""}}, + + {"hello world", ',', /*remove_whitespace=*/0, {"hello world"}}, + {"hello world", ',', /*remove_whitespace=*/1, {"hello world"}}, + + {" hello world ", ',', /*remove_whitespace=*/0, {" hello world "}}, + {" hello world ", ',', /*remove_whitespace=*/1, {"hello world"}}, + + {"hello,world", ',', /*remove_whitespace=*/0, {"hello", "world"}}, + {"hello,world", ',', /*remove_whitespace=*/1, {"hello", "world"}}, + + {"hello,,world", ',', /*remove_whitespace=*/0, {"hello", "", "world"}}, + {"hello,,world", ',', /*remove_whitespace=*/1, {"hello", "", "world"}}, + + {"\tab cd , , ef gh ", + ',', + /*remove_whitespace=*/0, + {"\tab cd ", " ", " ef gh "}}, + {"\tab cd , , ef gh ", + ',', + /*remove_whitespace=*/1, + {"ab cd", "", "ef gh"}}, + }; + for (const auto& t : kTests) { + SCOPED_TRACE(t.list); + SCOPED_TRACE(t.sep); + SCOPED_TRACE(t.remove_whitespace); + + std::vector<std::string> result; + auto append_to_vector = [](const char *elem, size_t len, void *arg) -> int { + auto *vec = static_cast<std::vector<std::string> *>(arg); + vec->push_back(std::string(elem, len)); + return 1; + }; + ASSERT_TRUE(CONF_parse_list(t.list, t.sep, t.remove_whitespace, + append_to_vector, &result)); + EXPECT_EQ(result, t.expected); + } +}
diff --git a/crypto/conf/internal.h b/crypto/conf/internal.h index 7fe4226..6075548 100644 --- a/crypto/conf/internal.h +++ b/crypto/conf/internal.h
@@ -31,9 +31,9 @@ // example. If |list_cb| returns <= 0, then the iteration is halted and that // value is returned immediately. Otherwise it returns one. Note that |list_cb| // may be called on an empty member. -int CONF_parse_list(const char *list, char sep, int remove_whitespace, - int (*list_cb)(const char *elem, size_t len, void *usr), - void *arg); +OPENSSL_EXPORT int CONF_parse_list( + const char *list, char sep, int remove_whitespace, + int (*list_cb)(const char *elem, size_t len, void *usr), void *arg); #if defined(__cplusplus)