|
| 1 | +/* |
| 2 | + * Copyright (C) the libgit2 contributors. All rights reserved. |
| 3 | + * |
| 4 | + * This file is part of libgit2, distributed under the GNU GPL v2 with |
| 5 | + * a Linking Exception. For full terms see the included COPYING file. |
| 6 | + */ |
| 7 | + |
| 8 | +#include "clar_libgit2.h" |
| 9 | +#include "attr.h" |
| 10 | + |
| 11 | +static git_repository *g_repo = NULL; |
| 12 | + |
| 13 | +void test_attr_macro__cleanup(void) |
| 14 | +{ |
| 15 | + cl_git_sandbox_cleanup(); |
| 16 | + g_repo = NULL; |
| 17 | +} |
| 18 | + |
| 19 | +void test_attr_macro__macros(void) |
| 20 | +{ |
| 21 | + const char *names[5] = { "rootattr", "binary", "diff", "crlf", "frotz" }; |
| 22 | + const char *names2[5] = { "mymacro", "positive", "negative", "rootattr", "another" }; |
| 23 | + const char *names3[3] = { "macro2", "multi2", "multi3" }; |
| 24 | + const char *values[5]; |
| 25 | + |
| 26 | + g_repo = cl_git_sandbox_init("attr"); |
| 27 | + |
| 28 | + cl_git_pass(git_attr_get_many(values, g_repo, 0, "binfile", 5, names)); |
| 29 | + |
| 30 | + cl_assert(GIT_ATTR_IS_TRUE(values[0])); |
| 31 | + cl_assert(GIT_ATTR_IS_TRUE(values[1])); |
| 32 | + cl_assert(GIT_ATTR_IS_FALSE(values[2])); |
| 33 | + cl_assert(GIT_ATTR_IS_FALSE(values[3])); |
| 34 | + cl_assert(GIT_ATTR_IS_UNSPECIFIED(values[4])); |
| 35 | + |
| 36 | + cl_git_pass(git_attr_get_many(values, g_repo, 0, "macro_test", 5, names2)); |
| 37 | + |
| 38 | + cl_assert(GIT_ATTR_IS_TRUE(values[0])); |
| 39 | + cl_assert(GIT_ATTR_IS_TRUE(values[1])); |
| 40 | + cl_assert(GIT_ATTR_IS_FALSE(values[2])); |
| 41 | + cl_assert(GIT_ATTR_IS_UNSPECIFIED(values[3])); |
| 42 | + cl_assert_equal_s("77", values[4]); |
| 43 | + |
| 44 | + cl_git_pass(git_attr_get_many(values, g_repo, 0, "macro_test", 3, names3)); |
| 45 | + |
| 46 | + cl_assert(GIT_ATTR_IS_TRUE(values[0])); |
| 47 | + cl_assert(GIT_ATTR_IS_FALSE(values[1])); |
| 48 | + cl_assert_equal_s("answer", values[2]); |
| 49 | +} |
| 50 | + |
| 51 | +void test_attr_macro__bad_macros(void) |
| 52 | +{ |
| 53 | + const char *names[6] = { "rootattr", "positive", "negative", |
| 54 | + "firstmacro", "secondmacro", "thirdmacro" }; |
| 55 | + const char *values[6]; |
| 56 | + |
| 57 | + g_repo = cl_git_sandbox_init("attr"); |
| 58 | + |
| 59 | + cl_git_pass(git_attr_get_many(values, g_repo, 0, "macro_bad", 6, names)); |
| 60 | + |
| 61 | + /* these three just confirm that the "mymacro" rule ran */ |
| 62 | + cl_assert(GIT_ATTR_IS_UNSPECIFIED(values[0])); |
| 63 | + cl_assert(GIT_ATTR_IS_TRUE(values[1])); |
| 64 | + cl_assert(GIT_ATTR_IS_FALSE(values[2])); |
| 65 | + |
| 66 | + /* file contains: |
| 67 | + * # let's try some malicious macro defs |
| 68 | + * [attr]firstmacro -thirdmacro -secondmacro |
| 69 | + * [attr]secondmacro firstmacro -firstmacro |
| 70 | + * [attr]thirdmacro secondmacro=hahaha -firstmacro |
| 71 | + * macro_bad firstmacro secondmacro thirdmacro |
| 72 | + * |
| 73 | + * firstmacro assignment list ends up with: |
| 74 | + * -thirdmacro -secondmacro |
| 75 | + * secondmacro assignment list expands "firstmacro" and ends up with: |
| 76 | + * -thirdmacro -secondmacro -firstmacro |
| 77 | + * thirdmacro assignment don't expand so list ends up with: |
| 78 | + * secondmacro="hahaha" |
| 79 | + * |
| 80 | + * macro_bad assignment list ends up with: |
| 81 | + * -thirdmacro -secondmacro firstmacro && |
| 82 | + * -thirdmacro -secondmacro -firstmacro secondmacro && |
| 83 | + * secondmacro="hahaha" thirdmacro |
| 84 | + * |
| 85 | + * so summary results should be: |
| 86 | + * -firstmacro secondmacro="hahaha" thirdmacro |
| 87 | + */ |
| 88 | + cl_assert(GIT_ATTR_IS_FALSE(values[3])); |
| 89 | + cl_assert_equal_s("hahaha", values[4]); |
| 90 | + cl_assert(GIT_ATTR_IS_TRUE(values[5])); |
| 91 | +} |
0 commit comments