]> git.proxmox.com Git - libgit2.git/blame - tests/filter/wildcard.c
xdiff: cleanup some warnings
[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"
5#include "buf_text.h"
6#include "git2/sys/filter.h"
7#include "git2/sys/repository.h"
8#include "custom_helpers.h"
9
10static git_repository *g_repo = NULL;
11
12static git_filter *create_wildcard_filter();
13
14#define DATA_LEN 32
15
16static 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
23static 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
30static 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
37void 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
52void 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
60static int wildcard_filter_check(
61 git_filter *self,
62 void **payload,
63 const git_filter_source *src,
64 const char **attr_values)
65{
66 if (strcmp(attr_values[0], "wcflip") == 0 ||
67 strcmp(attr_values[0], "wcreverse") == 0) {
68 *payload = git__strdup(attr_values[0]);
69 GITERR_CHECK_ALLOC(*payload);
70 return 0;
71 }
72
73 return GIT_PASSTHROUGH;
74}
75
76static int wildcard_filter_apply(
77 git_filter *self,
78 void **payload,
79 git_buf *to,
80 const git_buf *from,
81 const git_filter_source *source)
82{
83 const char *filtername = *payload;
84
85 if (filtername && strcmp(filtername, "wcflip") == 0)
86 return bitflip_filter_apply(self, payload, to, from, source);
87 else if (filtername && strcmp(filtername, "wcreverse") == 0)
88 return reverse_filter_apply(self, payload, to, from, source);
89
90 cl_fail("Unexpected attribute");
91 return GIT_PASSTHROUGH;
92}
93
94static int wildcard_filter_cleanup(git_filter *self, void *payload)
95{
96 git__free(payload);
97 return 0;
98}
99
100static void wildcard_filter_free(git_filter *f)
101{
102 git__free(f);
103}
104
105static git_filter *create_wildcard_filter()
106{
107 git_filter *filter = git__calloc(1, sizeof(git_filter));
108 cl_assert(filter);
109
110 filter->version = GIT_FILTER_VERSION;
111 filter->attributes = "filter=*";
112 filter->check = wildcard_filter_check;
113 filter->apply = wildcard_filter_apply;
114 filter->cleanup = wildcard_filter_cleanup;
115 filter->shutdown = wildcard_filter_free;
116
117 return filter;
118}
119
120void test_filter_wildcard__reverse(void)
121{
122 git_filter_list *fl;
123 git_buf in = GIT_BUF_INIT, out = GIT_BUF_INIT;
124
125 cl_git_pass(git_filter_list_load(
126 &fl, g_repo, NULL, "hero-reverse-foo", GIT_FILTER_TO_ODB, 0));
127
128 cl_git_pass(git_buf_put(&in, input, DATA_LEN));
129 cl_git_pass(git_filter_list_apply_to_data(&out, fl, &in));
130
131 cl_assert_equal_i(DATA_LEN, out.size);
132
133 cl_assert_equal_i(
134 0, memcmp(reversed, out.ptr, out.size));
135
136 git_filter_list_free(fl);
137 git_buf_free(&out);
138 git_buf_free(&in);
139}
140
141void test_filter_wildcard__flip(void)
142{
143 git_filter_list *fl;
144 git_buf in = GIT_BUF_INIT, out = GIT_BUF_INIT;
145
146 cl_git_pass(git_filter_list_load(
147 &fl, g_repo, NULL, "hero-flip-foo", GIT_FILTER_TO_ODB, 0));
148
149 cl_git_pass(git_buf_put(&in, input, DATA_LEN));
150 cl_git_pass(git_filter_list_apply_to_data(&out, fl, &in));
151
152 cl_assert_equal_i(DATA_LEN, out.size);
153
154 cl_assert_equal_i(
155 0, memcmp(flipped, out.ptr, out.size));
156
157 git_filter_list_free(fl);
158 git_buf_free(&out);
159 git_buf_free(&in);
160}
161
162void test_filter_wildcard__none(void)
163{
164 git_filter_list *fl;
165 git_buf in = GIT_BUF_INIT, out = GIT_BUF_INIT;
166
167 cl_git_pass(git_filter_list_load(
168 &fl, g_repo, NULL, "none-foo", GIT_FILTER_TO_ODB, 0));
169
170 cl_git_pass(git_buf_put(&in, input, DATA_LEN));
171 cl_git_pass(git_filter_list_apply_to_data(&out, fl, &in));
172
173 cl_assert_equal_i(DATA_LEN, out.size);
174
175 cl_assert_equal_i(
176 0, memcmp(input, out.ptr, out.size));
177
178 git_filter_list_free(fl);
179 git_buf_free(&out);
180 git_buf_free(&in);
181}