]> git.proxmox.com Git - libgit2.git/blobdiff - tests/config/read.c
New upstream version 1.1.0+dfsg.1
[libgit2.git] / tests / config / read.c
index ccc479bc1c778d07ed5e76a78fe2ff27a4bd64c0..10990875080f6ff95658a5071e612f477384be06 100644 (file)
@@ -779,6 +779,93 @@ void test_config_read__bom(void)
        git_buf_dispose(&buf);
 }
 
+void test_config_read__arbitrary_whitespace_before_subsection(void)
+{
+       git_buf buf = GIT_BUF_INIT;
+       git_config *cfg;
+
+       cl_set_cleanup(&clean_test_config, NULL);
+       cl_git_mkfile("./testconfig", "[some \t \"subsection\"]\n var = value\n");
+       cl_git_pass(git_config_open_ondisk(&cfg, "./testconfig"));
+       cl_git_pass(git_config_get_string_buf(&buf, cfg, "some.subsection.var"));
+       cl_assert_equal_s(buf.ptr, "value");
+
+       git_config_free(cfg);
+       git_buf_dispose(&buf);
+}
+
+void test_config_read__no_whitespace_after_subsection(void)
+{
+       git_config *cfg;
+
+       cl_set_cleanup(&clean_test_config, NULL);
+       cl_git_mkfile("./testconfig", "[some \"subsection\" ]\n var = value\n");
+       cl_git_fail(git_config_open_ondisk(&cfg, "./testconfig"));
+
+       git_config_free(cfg);
+}
+
+void test_config_read__invalid_space_section(void)
+{
+       git_config *cfg;
+
+       cl_set_cleanup(&clean_test_config, NULL);
+       cl_git_mkfile("./testconfig", "\xEF\xBB\xBF[some section]\n var = value\n");
+       cl_git_fail(git_config_open_ondisk(&cfg, "./testconfig"));
+
+       git_config_free(cfg);
+}
+
+void test_config_read__invalid_quoted_first_section(void)
+{
+       git_config *cfg;
+
+       cl_set_cleanup(&clean_test_config, NULL);
+       cl_git_mkfile("./testconfig", "\xEF\xBB\xBF[\"some\"]\n var = value\n");
+       cl_git_fail(git_config_open_ondisk(&cfg, "./testconfig"));
+
+       git_config_free(cfg);
+}
+
+void test_config_read__invalid_unquoted_subsection(void)
+{
+       git_config *cfg;
+
+       cl_set_cleanup(&clean_test_config, NULL);
+       cl_git_mkfile("./testconfig", "\xEF\xBB\xBF[some sub section]\n var = value\n");
+       cl_git_fail(git_config_open_ondisk(&cfg, "./testconfig"));
+
+       git_config_free(cfg);
+}
+
+void test_config_read__invalid_quoted_third_section(void)
+{
+       git_config *cfg;
+
+       cl_set_cleanup(&clean_test_config, NULL);
+       cl_git_mkfile("./testconfig", "\xEF\xBB\xBF[some sub \"section\"]\n var = value\n");
+       cl_git_fail(git_config_open_ondisk(&cfg, "./testconfig"));
+
+       git_config_free(cfg);
+}
+
+void test_config_read__unreadable_file_ignored(void)
+{
+       git_buf buf = GIT_BUF_INIT;
+       git_config *cfg;
+       int ret;
+
+       cl_set_cleanup(&clean_test_config, NULL);
+       cl_git_mkfile("./testconfig", "[some] var = value\n[some \"OtheR\"] var = value");
+       cl_git_pass(p_chmod("./testconfig", 0));
+
+       ret = git_config_open_ondisk(&cfg, "./test/config");
+       cl_assert(ret == 0 || ret == GIT_ENOTFOUND);
+
+       git_config_free(cfg);
+       git_buf_dispose(&buf);
+}
+
 void test_config_read__single_line(void)
 {
        git_buf buf = GIT_BUF_INIT;
@@ -841,3 +928,79 @@ void test_config_read__nosection(void)
        git_buf_dispose(&buf);
        git_config_free(cfg);
 }
+
+enum {
+       MAP_TRUE = 0,
+       MAP_FALSE = 1,
+       MAP_ALWAYS = 2
+};
+
+static git_configmap _test_map1[] = {
+       {GIT_CONFIGMAP_STRING, "always", MAP_ALWAYS},
+       {GIT_CONFIGMAP_FALSE, NULL, MAP_FALSE},
+       {GIT_CONFIGMAP_TRUE, NULL, MAP_TRUE},
+};
+
+static git_configmap _test_map2[] = {
+       {GIT_CONFIGMAP_INT32, NULL, 0},
+};
+
+void test_config_read__get_mapped(void)
+{
+       git_config *cfg;
+       int val;
+       int known_good;
+
+       cl_set_cleanup(&clean_test_config, NULL);
+       cl_git_mkfile("./testconfig", "[header]\n"
+                                                                 "  key1 = 1\n"
+                                                                 "  key2 = true\n"
+                                                                 "  key3\n"
+                                                                 "  key4 = always\n"
+                                                                 "  key5 = false\n"
+                                                                 "  key6 = 0\n"
+                                                                 "  key7 = never\n"
+                                                                 "  key8 = On\n"
+                                                                 "  key9 = off\n");
+       cl_git_pass(git_config_open_ondisk(&cfg, "./testconfig"));
+
+       // check parsing bool and string
+       cl_git_pass(git_config_get_mapped(&val, cfg, "header.key1", _test_map1, ARRAY_SIZE(_test_map1)));
+       cl_assert_equal_i(val, MAP_TRUE);
+       cl_git_pass(git_config_get_mapped(&val, cfg, "header.key2", _test_map1, ARRAY_SIZE(_test_map1)));
+       cl_assert_equal_i(val, MAP_TRUE);
+       cl_git_pass(git_config_get_mapped(&val, cfg, "header.key3", _test_map1, ARRAY_SIZE(_test_map1)));
+       cl_assert_equal_i(val, MAP_TRUE);
+       cl_git_pass(git_config_get_mapped(&val, cfg, "header.key8", _test_map1, ARRAY_SIZE(_test_map1)));
+       cl_assert_equal_i(val, MAP_TRUE);
+
+       cl_git_pass(git_config_get_mapped(&val, cfg, "header.key4", _test_map1, ARRAY_SIZE(_test_map1)));
+       cl_assert_equal_i(val, MAP_ALWAYS);
+
+       cl_git_pass(git_config_get_mapped(&val, cfg, "header.key5", _test_map1, ARRAY_SIZE(_test_map1)));
+       cl_assert_equal_i(val, MAP_FALSE);
+       cl_git_pass(git_config_get_mapped(&val, cfg, "header.key6", _test_map1, ARRAY_SIZE(_test_map1)));
+       cl_assert_equal_i(val, MAP_FALSE);
+       cl_git_pass(git_config_get_mapped(&val, cfg, "header.key9", _test_map1, ARRAY_SIZE(_test_map1)));
+       cl_assert_equal_i(val, MAP_FALSE);
+
+       cl_git_fail(git_config_get_mapped(&val, cfg, "header.key7", _test_map1, ARRAY_SIZE(_test_map1)));
+
+       // check parsing int values
+       cl_git_pass(git_config_get_mapped(&val, cfg, "header.key1", _test_map2, ARRAY_SIZE(_test_map2)));
+       cl_git_pass(git_config_get_int32(&known_good, cfg, "header.key1"));
+       cl_assert_equal_i(val, known_good);
+       cl_git_pass(git_config_get_mapped(&val, cfg, "header.key6", _test_map2, ARRAY_SIZE(_test_map2)));
+       cl_git_pass(git_config_get_int32(&known_good, cfg, "header.key6"));
+       cl_assert_equal_i(val, known_good);
+
+       cl_git_fail(git_config_get_mapped(&val, cfg, "header.key2", _test_map2, ARRAY_SIZE(_test_map2)));
+       cl_git_fail(git_config_get_mapped(&val, cfg, "header.key3", _test_map2, ARRAY_SIZE(_test_map2)));
+       cl_git_fail(git_config_get_mapped(&val, cfg, "header.key4", _test_map2, ARRAY_SIZE(_test_map2)));
+       cl_git_fail(git_config_get_mapped(&val, cfg, "header.key5", _test_map2, ARRAY_SIZE(_test_map2)));
+       cl_git_fail(git_config_get_mapped(&val, cfg, "header.key7", _test_map2, ARRAY_SIZE(_test_map2)));
+       cl_git_fail(git_config_get_mapped(&val, cfg, "header.key8", _test_map2, ARRAY_SIZE(_test_map2)));
+       cl_git_fail(git_config_get_mapped(&val, cfg, "header.key9", _test_map2, ARRAY_SIZE(_test_map2)));
+
+       git_config_free(cfg);
+}