From cc295a26b2228b3c741f0fc0915890e184ca925d Mon Sep 17 00:00:00 2001 From: "Rafael G. Martins" Date: Mon, 23 Jul 2018 00:25:48 +0200 Subject: common: config-parser: fix empty values support --- src/common/config-parser.c | 11 ++ tests/common/check_config_parser.c | 318 +++++++++++++++++++++++++++++++++++++ 2 files changed, 329 insertions(+) diff --git a/src/common/config-parser.c b/src/common/config-parser.c index 235209e..2a1546d 100644 --- a/src/common/config-parser.c +++ b/src/common/config-parser.c @@ -174,6 +174,13 @@ bc_config_parse(const char *src, size_t src_len, const char *list_sections[], if (c == '=') { key = bc_strndup(src + start, current - start); state = CONFIG_SECTION_VALUE_START; + if (is_last) { + bc_trie_insert(section->data, bc_str_strip(key), + bc_strdup("")); + free(key); + key = NULL; + break; + } if (value == NULL) value = bc_string_new(); break; @@ -197,6 +204,10 @@ bc_config_parse(const char *src, size_t src_len, const char *list_sections[], state = CONFIG_SECTION_VALUE_QUOTE; break; } + if (c == '\r' || c == '\n' || is_last) { + state = CONFIG_SECTION_VALUE; + continue; + } bc_string_append_c(value, c); state = CONFIG_SECTION_VALUE; break; diff --git a/tests/common/check_config_parser.c b/tests/common/check_config_parser.c index b1e42a6..5114709 100644 --- a/tests/common/check_config_parser.c +++ b/tests/common/check_config_parser.c @@ -600,6 +600,323 @@ test_config_quoted_values(void **state) } +static void +test_config_empty_values(void **state) +{ + const char *a = + "[foo]\n" + "asd ="; + bc_error_t *err = NULL; + bc_config_t *c = bc_config_parse(a, strlen(a), NULL, &err); + assert_null(err); + assert_non_null(c); + assert_non_null(c->root); + assert_int_equal(bc_trie_size(c->root), 1); + char **s = bc_config_list_sections(c); + assert_non_null(s); + assert_int_equal(bc_strv_length(s), 1); + assert_string_equal(s[0], "foo"); + assert_null(s[1]); + bc_strv_free(s); + assert_string_equal(bc_config_get(c, "foo", "asd"), ""); + char **k = bc_config_list_keys(c, "foo"); + assert_non_null(k); + assert_int_equal(bc_strv_length(k), 1); + assert_string_equal(k[0], "asd"); + assert_null(k[1]); + bc_strv_free(k); + bc_config_free(c); + + a = + "[foo]\n" + "asd = \n"; + err = NULL; + c = bc_config_parse(a, strlen(a), NULL, &err); + assert_null(err); + assert_non_null(c); + assert_non_null(c->root); + assert_int_equal(bc_trie_size(c->root), 1); + s = bc_config_list_sections(c); + assert_non_null(s); + assert_int_equal(bc_strv_length(s), 1); + assert_string_equal(s[0], "foo"); + assert_null(s[1]); + bc_strv_free(s); + assert_string_equal(bc_config_get(c, "foo", "asd"), ""); + k = bc_config_list_keys(c, "foo"); + assert_non_null(k); + assert_int_equal(bc_strv_length(k), 1); + assert_string_equal(k[0], "asd"); + assert_null(k[1]); + bc_strv_free(k); + bc_config_free(c); + + a = + "[foo]\n" + "asd = foo\n" + "qwe ="; + err = NULL; + c = bc_config_parse(a, strlen(a), NULL, &err); + assert_null(err); + assert_non_null(c); + assert_non_null(c->root); + assert_int_equal(bc_trie_size(c->root), 1); + s = bc_config_list_sections(c); + assert_non_null(s); + assert_int_equal(bc_strv_length(s), 1); + assert_string_equal(s[0], "foo"); + assert_null(s[1]); + bc_strv_free(s); + assert_string_equal(bc_config_get(c, "foo", "asd"), "foo"); + assert_string_equal(bc_config_get(c, "foo", "qwe"), ""); + k = bc_config_list_keys(c, "foo"); + assert_non_null(k); + assert_int_equal(bc_strv_length(k), 2); + assert_string_equal(k[0], "asd"); + assert_string_equal(k[1], "qwe"); + assert_null(k[2]); + bc_strv_free(k); + bc_config_free(c); + + a = + "[foo]\n" + "asd = foo\n" + "qwe = \n"; + err = NULL; + c = bc_config_parse(a, strlen(a), NULL, &err); + assert_null(err); + assert_non_null(c); + assert_non_null(c->root); + assert_int_equal(bc_trie_size(c->root), 1); + s = bc_config_list_sections(c); + assert_non_null(s); + assert_int_equal(bc_strv_length(s), 1); + assert_string_equal(s[0], "foo"); + assert_null(s[1]); + bc_strv_free(s); + assert_string_equal(bc_config_get(c, "foo", "asd"), "foo"); + assert_string_equal(bc_config_get(c, "foo", "qwe"), ""); + k = bc_config_list_keys(c, "foo"); + assert_non_null(k); + assert_int_equal(bc_strv_length(k), 2); + assert_string_equal(k[0], "asd"); + assert_string_equal(k[1], "qwe"); + assert_null(k[2]); + bc_strv_free(k); + bc_config_free(c); + + a = + "[foo]\n" + "asd =\n" + "qwe = foo"; + err = NULL; + c = bc_config_parse(a, strlen(a), NULL, &err); + assert_null(err); + assert_non_null(c); + assert_non_null(c->root); + assert_int_equal(bc_trie_size(c->root), 1); + s = bc_config_list_sections(c); + assert_non_null(s); + assert_int_equal(bc_strv_length(s), 1); + assert_string_equal(s[0], "foo"); + assert_null(s[1]); + bc_strv_free(s); + assert_string_equal(bc_config_get(c, "foo", "asd"), ""); + assert_string_equal(bc_config_get(c, "foo", "qwe"), "foo"); + k = bc_config_list_keys(c, "foo"); + assert_non_null(k); + assert_int_equal(bc_strv_length(k), 2); + assert_string_equal(k[0], "asd"); + assert_string_equal(k[1], "qwe"); + assert_null(k[2]); + bc_strv_free(k); + bc_config_free(c); + + a = + "[foo]\n" + "asd = \n" + "qwe = foo\n"; + err = NULL; + c = bc_config_parse(a, strlen(a), NULL, &err); + assert_null(err); + assert_non_null(c); + assert_non_null(c->root); + assert_int_equal(bc_trie_size(c->root), 1); + s = bc_config_list_sections(c); + assert_non_null(s); + assert_int_equal(bc_strv_length(s), 1); + assert_string_equal(s[0], "foo"); + assert_null(s[1]); + bc_strv_free(s); + assert_string_equal(bc_config_get(c, "foo", "asd"), ""); + assert_string_equal(bc_config_get(c, "foo", "qwe"), "foo"); + k = bc_config_list_keys(c, "foo"); + assert_non_null(k); + assert_int_equal(bc_strv_length(k), 2); + assert_string_equal(k[0], "asd"); + assert_string_equal(k[1], "qwe"); + assert_null(k[2]); + bc_strv_free(k); + bc_config_free(c); + + a = + "[foo]\r\n" + "asd ="; + err = NULL; + c = bc_config_parse(a, strlen(a), NULL, &err); + assert_null(err); + assert_non_null(c); + assert_non_null(c->root); + assert_int_equal(bc_trie_size(c->root), 1); + s = bc_config_list_sections(c); + assert_non_null(s); + assert_int_equal(bc_strv_length(s), 1); + assert_string_equal(s[0], "foo"); + assert_null(s[1]); + bc_strv_free(s); + assert_string_equal(bc_config_get(c, "foo", "asd"), ""); + k = bc_config_list_keys(c, "foo"); + assert_non_null(k); + assert_int_equal(bc_strv_length(k), 1); + assert_string_equal(k[0], "asd"); + assert_null(k[1]); + bc_strv_free(k); + bc_config_free(c); + + a = + "[foo]\r\n" + "asd = \r\n"; + err = NULL; + c = bc_config_parse(a, strlen(a), NULL, &err); + assert_null(err); + assert_non_null(c); + assert_non_null(c->root); + assert_int_equal(bc_trie_size(c->root), 1); + s = bc_config_list_sections(c); + assert_non_null(s); + assert_int_equal(bc_strv_length(s), 1); + assert_string_equal(s[0], "foo"); + assert_null(s[1]); + bc_strv_free(s); + assert_string_equal(bc_config_get(c, "foo", "asd"), ""); + k = bc_config_list_keys(c, "foo"); + assert_non_null(k); + assert_int_equal(bc_strv_length(k), 1); + assert_string_equal(k[0], "asd"); + assert_null(k[1]); + bc_strv_free(k); + bc_config_free(c); + + a = + "[foo]\r\n" + "asd = foo\r\n" + "qwe ="; + err = NULL; + c = bc_config_parse(a, strlen(a), NULL, &err); + assert_null(err); + assert_non_null(c); + assert_non_null(c->root); + assert_int_equal(bc_trie_size(c->root), 1); + s = bc_config_list_sections(c); + assert_non_null(s); + assert_int_equal(bc_strv_length(s), 1); + assert_string_equal(s[0], "foo"); + assert_null(s[1]); + bc_strv_free(s); + assert_string_equal(bc_config_get(c, "foo", "asd"), "foo"); + assert_string_equal(bc_config_get(c, "foo", "qwe"), ""); + k = bc_config_list_keys(c, "foo"); + assert_non_null(k); + assert_int_equal(bc_strv_length(k), 2); + assert_string_equal(k[0], "asd"); + assert_string_equal(k[1], "qwe"); + assert_null(k[2]); + bc_strv_free(k); + bc_config_free(c); + + a = + "[foo]\r\n" + "asd = foo\r\n" + "qwe = \r\n"; + err = NULL; + c = bc_config_parse(a, strlen(a), NULL, &err); + assert_null(err); + assert_non_null(c); + assert_non_null(c->root); + assert_int_equal(bc_trie_size(c->root), 1); + s = bc_config_list_sections(c); + assert_non_null(s); + assert_int_equal(bc_strv_length(s), 1); + assert_string_equal(s[0], "foo"); + assert_null(s[1]); + bc_strv_free(s); + assert_string_equal(bc_config_get(c, "foo", "asd"), "foo"); + assert_string_equal(bc_config_get(c, "foo", "qwe"), ""); + k = bc_config_list_keys(c, "foo"); + assert_non_null(k); + assert_int_equal(bc_strv_length(k), 2); + assert_string_equal(k[0], "asd"); + assert_string_equal(k[1], "qwe"); + assert_null(k[2]); + bc_strv_free(k); + bc_config_free(c); + + a = + "[foo]\r\n" + "asd =\r\n" + "qwe = foo"; + err = NULL; + c = bc_config_parse(a, strlen(a), NULL, &err); + assert_null(err); + assert_non_null(c); + assert_non_null(c->root); + assert_int_equal(bc_trie_size(c->root), 1); + s = bc_config_list_sections(c); + assert_non_null(s); + assert_int_equal(bc_strv_length(s), 1); + assert_string_equal(s[0], "foo"); + assert_null(s[1]); + bc_strv_free(s); + assert_string_equal(bc_config_get(c, "foo", "asd"), ""); + assert_string_equal(bc_config_get(c, "foo", "qwe"), "foo"); + k = bc_config_list_keys(c, "foo"); + assert_non_null(k); + assert_int_equal(bc_strv_length(k), 2); + assert_string_equal(k[0], "asd"); + assert_string_equal(k[1], "qwe"); + assert_null(k[2]); + bc_strv_free(k); + bc_config_free(c); + + a = + "[foo]\r\n" + "asd = \r\n" + "qwe = foo\r\n"; + err = NULL; + c = bc_config_parse(a, strlen(a), NULL, &err); + assert_null(err); + assert_non_null(c); + assert_non_null(c->root); + assert_int_equal(bc_trie_size(c->root), 1); + s = bc_config_list_sections(c); + assert_non_null(s); + assert_int_equal(bc_strv_length(s), 1); + assert_string_equal(s[0], "foo"); + assert_null(s[1]); + bc_strv_free(s); + assert_string_equal(bc_config_get(c, "foo", "asd"), ""); + assert_string_equal(bc_config_get(c, "foo", "qwe"), "foo"); + k = bc_config_list_keys(c, "foo"); + assert_non_null(k); + assert_int_equal(bc_strv_length(k), 2); + assert_string_equal(k[0], "asd"); + assert_string_equal(k[1], "qwe"); + assert_null(k[2]); + bc_strv_free(k); + bc_config_free(c); +} + + static void test_config_key_prefix(void **state) { @@ -710,6 +1027,7 @@ main(void) unit_test(test_config_section_multiple_sections), unit_test(test_config_section_list), unit_test(test_config_quoted_values), + unit_test(test_config_empty_values), unit_test(test_config_key_prefix), unit_test(test_config_error_start), unit_test(test_config_error_section_with_newline), -- cgit v1.2.3-18-g5258