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