]>
Commit | Line | Data |
---|---|---|
63924435 ET |
1 | #include "clar_libgit2.h" |
2 | #include "posix.h" | |
3 | #include "blob.h" | |
4 | #include "filter.h" | |
5 | #include "buf_text.h" | |
6 | #include "git2/sys/filter.h" | |
7 | #include "git2/sys/repository.h" | |
8 | #include "custom_helpers.h" | |
9 | ||
10 | static git_repository *g_repo = NULL; | |
11 | ||
bae467ae | 12 | static git_filter *create_wildcard_filter(void); |
63924435 ET |
13 | |
14 | #define DATA_LEN 32 | |
15 | ||
16 | static unsigned char input[] = { | |
17 | 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, | |
18 | 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, | |
19 | 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, | |
20 | 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, | |
21 | }; | |
22 | ||
23 | static unsigned char reversed[] = { | |
24 | 0x1f, 0x1e, 0x1d, 0x1c, 0x1b, 0x1a, 0x19, 0x18, | |
25 | 0x17, 0x16, 0x15, 0x14, 0x13, 0x12, 0x11, 0x10, | |
26 | 0x0f, 0x0e, 0x0d, 0x0c, 0x0b, 0x0a, 0x09, 0x08, | |
27 | 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00, | |
28 | }; | |
29 | ||
30 | static unsigned char flipped[] = { | |
31 | 0xff, 0xfe, 0xfd, 0xfc, 0xfb, 0xfa, 0xf9, 0xf8, | |
32 | 0xf7, 0xf6, 0xf5, 0xf4, 0xf3, 0xf2, 0xf1, 0xf0, | |
33 | 0xef, 0xee, 0xed, 0xec, 0xeb, 0xea, 0xe9, 0xe8, | |
34 | 0xe7, 0xe6, 0xe5, 0xe4, 0xe3, 0xe2, 0xe1, 0xe0, | |
35 | }; | |
36 | ||
37 | void test_filter_wildcard__initialize(void) | |
38 | { | |
39 | cl_git_pass(git_filter_register( | |
40 | "wildcard", create_wildcard_filter(), GIT_FILTER_DRIVER_PRIORITY)); | |
41 | ||
42 | g_repo = cl_git_sandbox_init("empty_standard_repo"); | |
43 | ||
44 | cl_git_rewritefile( | |
45 | "empty_standard_repo/.gitattributes", | |
46 | "* binary\n" | |
47 | "hero-flip-* filter=wcflip\n" | |
48 | "hero-reverse-* filter=wcreverse\n" | |
49 | "none-* filter=unregistered\n"); | |
50 | } | |
51 | ||
52 | void test_filter_wildcard__cleanup(void) | |
53 | { | |
54 | cl_git_pass(git_filter_unregister("wildcard")); | |
55 | ||
56 | cl_git_sandbox_cleanup(); | |
57 | g_repo = NULL; | |
58 | } | |
59 | ||
60 | static int wildcard_filter_check( | |
61 | git_filter *self, | |
62 | void **payload, | |
63 | const git_filter_source *src, | |
64 | const char **attr_values) | |
65 | { | |
bae467ae ET |
66 | GIT_UNUSED(self); |
67 | GIT_UNUSED(src); | |
68 | ||
63924435 ET |
69 | if (strcmp(attr_values[0], "wcflip") == 0 || |
70 | strcmp(attr_values[0], "wcreverse") == 0) { | |
71 | *payload = git__strdup(attr_values[0]); | |
ac3d33df | 72 | GIT_ERROR_CHECK_ALLOC(*payload); |
63924435 ET |
73 | return 0; |
74 | } | |
75 | ||
76 | return GIT_PASSTHROUGH; | |
77 | } | |
78 | ||
79 | static int wildcard_filter_apply( | |
80 | git_filter *self, | |
81 | void **payload, | |
82 | git_buf *to, | |
83 | const git_buf *from, | |
84 | const git_filter_source *source) | |
85 | { | |
86 | const char *filtername = *payload; | |
87 | ||
88 | if (filtername && strcmp(filtername, "wcflip") == 0) | |
89 | return bitflip_filter_apply(self, payload, to, from, source); | |
90 | else if (filtername && strcmp(filtername, "wcreverse") == 0) | |
91 | return reverse_filter_apply(self, payload, to, from, source); | |
92 | ||
93 | cl_fail("Unexpected attribute"); | |
94 | return GIT_PASSTHROUGH; | |
95 | } | |
96 | ||
bae467ae | 97 | static void wildcard_filter_cleanup(git_filter *self, void *payload) |
63924435 | 98 | { |
bae467ae | 99 | GIT_UNUSED(self); |
63924435 | 100 | git__free(payload); |
63924435 ET |
101 | } |
102 | ||
103 | static void wildcard_filter_free(git_filter *f) | |
104 | { | |
105 | git__free(f); | |
106 | } | |
107 | ||
9a99ca7b | 108 | static git_filter *create_wildcard_filter(void) |
63924435 ET |
109 | { |
110 | git_filter *filter = git__calloc(1, sizeof(git_filter)); | |
111 | cl_assert(filter); | |
112 | ||
113 | filter->version = GIT_FILTER_VERSION; | |
114 | filter->attributes = "filter=*"; | |
115 | filter->check = wildcard_filter_check; | |
116 | filter->apply = wildcard_filter_apply; | |
117 | filter->cleanup = wildcard_filter_cleanup; | |
118 | filter->shutdown = wildcard_filter_free; | |
119 | ||
120 | return filter; | |
121 | } | |
122 | ||
123 | void test_filter_wildcard__reverse(void) | |
124 | { | |
125 | git_filter_list *fl; | |
126 | git_buf in = GIT_BUF_INIT, out = GIT_BUF_INIT; | |
127 | ||
128 | cl_git_pass(git_filter_list_load( | |
129 | &fl, g_repo, NULL, "hero-reverse-foo", GIT_FILTER_TO_ODB, 0)); | |
130 | ||
bae467ae | 131 | cl_git_pass(git_buf_put(&in, (char *)input, DATA_LEN)); |
63924435 ET |
132 | cl_git_pass(git_filter_list_apply_to_data(&out, fl, &in)); |
133 | ||
134 | cl_assert_equal_i(DATA_LEN, out.size); | |
135 | ||
136 | cl_assert_equal_i( | |
137 | 0, memcmp(reversed, out.ptr, out.size)); | |
138 | ||
139 | git_filter_list_free(fl); | |
ac3d33df JK |
140 | git_buf_dispose(&out); |
141 | git_buf_dispose(&in); | |
63924435 ET |
142 | } |
143 | ||
144 | void test_filter_wildcard__flip(void) | |
145 | { | |
146 | git_filter_list *fl; | |
147 | git_buf in = GIT_BUF_INIT, out = GIT_BUF_INIT; | |
148 | ||
149 | cl_git_pass(git_filter_list_load( | |
150 | &fl, g_repo, NULL, "hero-flip-foo", GIT_FILTER_TO_ODB, 0)); | |
151 | ||
bae467ae | 152 | cl_git_pass(git_buf_put(&in, (char *)input, DATA_LEN)); |
63924435 ET |
153 | cl_git_pass(git_filter_list_apply_to_data(&out, fl, &in)); |
154 | ||
155 | cl_assert_equal_i(DATA_LEN, out.size); | |
156 | ||
157 | cl_assert_equal_i( | |
158 | 0, memcmp(flipped, out.ptr, out.size)); | |
159 | ||
160 | git_filter_list_free(fl); | |
ac3d33df JK |
161 | git_buf_dispose(&out); |
162 | git_buf_dispose(&in); | |
63924435 ET |
163 | } |
164 | ||
165 | void test_filter_wildcard__none(void) | |
166 | { | |
167 | git_filter_list *fl; | |
168 | git_buf in = GIT_BUF_INIT, out = GIT_BUF_INIT; | |
169 | ||
170 | cl_git_pass(git_filter_list_load( | |
171 | &fl, g_repo, NULL, "none-foo", GIT_FILTER_TO_ODB, 0)); | |
172 | ||
bae467ae | 173 | cl_git_pass(git_buf_put(&in, (char *)input, DATA_LEN)); |
63924435 ET |
174 | cl_git_pass(git_filter_list_apply_to_data(&out, fl, &in)); |
175 | ||
176 | cl_assert_equal_i(DATA_LEN, out.size); | |
177 | ||
178 | cl_assert_equal_i( | |
179 | 0, memcmp(input, out.ptr, out.size)); | |
180 | ||
181 | git_filter_list_free(fl); | |
ac3d33df JK |
182 | git_buf_dispose(&out); |
183 | git_buf_dispose(&in); | |
63924435 | 184 | } |