]> git.proxmox.com Git - libgit2.git/blame - tests/filter/wildcard.c
New upstream version 1.4.3+dfsg.1
[libgit2.git] / tests / filter / wildcard.c
CommitLineData
63924435
ET
1#include "clar_libgit2.h"
2#include "posix.h"
3#include "blob.h"
4#include "filter.h"
63924435
ET
5#include "git2/sys/filter.h"
6#include "git2/sys/repository.h"
7#include "custom_helpers.h"
8
9static git_repository *g_repo = NULL;
10
bae467ae 11static git_filter *create_wildcard_filter(void);
63924435
ET
12
13#define DATA_LEN 32
14
15static 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
22static 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
29static 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
36void 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
51void 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
59static int wildcard_filter_check(
60 git_filter *self,
61 void **payload,
62 const git_filter_source *src,
63 const char **attr_values)
64{
bae467ae
ET
65 GIT_UNUSED(self);
66 GIT_UNUSED(src);
67
63924435
ET
68 if (strcmp(attr_values[0], "wcflip") == 0 ||
69 strcmp(attr_values[0], "wcreverse") == 0) {
70 *payload = git__strdup(attr_values[0]);
ac3d33df 71 GIT_ERROR_CHECK_ALLOC(*payload);
63924435
ET
72 return 0;
73 }
74
75 return GIT_PASSTHROUGH;
76}
77
78static int wildcard_filter_apply(
79 git_filter *self,
80 void **payload,
e579e0f7
MB
81 git_str *to,
82 const git_str *from,
63924435
ET
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
c25aa7cd
PP
96static 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
bae467ae 107static void wildcard_filter_cleanup(git_filter *self, void *payload)
63924435 108{
bae467ae 109 GIT_UNUSED(self);
63924435 110 git__free(payload);
63924435
ET
111}
112
113static void wildcard_filter_free(git_filter *f)
114{
115 git__free(f);
116}
117
9a99ca7b 118static git_filter *create_wildcard_filter(void)
63924435
ET
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;
c25aa7cd 126 filter->stream = wildcard_filter_stream;
63924435
ET
127 filter->cleanup = wildcard_filter_cleanup;
128 filter->shutdown = wildcard_filter_free;
129
130 return filter;
131}
132
133void test_filter_wildcard__reverse(void)
134{
135 git_filter_list *fl;
c25aa7cd 136 git_buf out = GIT_BUF_INIT;
63924435
ET
137
138 cl_git_pass(git_filter_list_load(
139 &fl, g_repo, NULL, "hero-reverse-foo", GIT_FILTER_TO_ODB, 0));
140
c25aa7cd 141 cl_git_pass(git_filter_list_apply_to_buffer(&out, fl, (char *)input, DATA_LEN));
63924435
ET
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);
ac3d33df 149 git_buf_dispose(&out);
63924435
ET
150}
151
152void test_filter_wildcard__flip(void)
153{
154 git_filter_list *fl;
c25aa7cd 155 git_buf out = GIT_BUF_INIT;
63924435
ET
156
157 cl_git_pass(git_filter_list_load(
158 &fl, g_repo, NULL, "hero-flip-foo", GIT_FILTER_TO_ODB, 0));
159
c25aa7cd 160 cl_git_pass(git_filter_list_apply_to_buffer(&out, fl, (char *)input, DATA_LEN));
63924435
ET
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);
ac3d33df 168 git_buf_dispose(&out);
63924435
ET
169}
170
171void test_filter_wildcard__none(void)
172{
173 git_filter_list *fl;
c25aa7cd 174 git_buf out = GIT_BUF_INIT;
63924435
ET
175
176 cl_git_pass(git_filter_list_load(
177 &fl, g_repo, NULL, "none-foo", GIT_FILTER_TO_ODB, 0));
178
c25aa7cd 179 cl_git_pass(git_filter_list_apply_to_buffer(&out, fl, (char *)input, DATA_LEN));
63924435
ET
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);
ac3d33df 187 git_buf_dispose(&out);
63924435 188}