]> git.proxmox.com Git - libgit2.git/blame - tests/filter/custom.c
Merge pull request #4183 from pks-t/pks/coverity
[libgit2.git] / tests / filter / custom.c
CommitLineData
b47349b8
RB
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"
63924435 8#include "custom_helpers.h"
b47349b8 9
eefc32d5
RB
10/* going TO_WORKDIR, filters are executed low to high
11 * going TO_ODB, filters are executed high to low
12 */
13#define BITFLIP_FILTER_PRIORITY -1
14#define REVERSE_FILTER_PRIORITY -2
b47349b8 15
b47349b8
RB
16#ifdef GIT_WIN32
17# define NEWLINE "\r\n"
18#else
19# define NEWLINE "\n"
20#endif
21
22static char workdir_data[] =
23 "some simple" NEWLINE
24 "data" NEWLINE
25 "that will be" NEWLINE
26 "trivially" NEWLINE
27 "scrambled." NEWLINE;
28
63924435
ET
29#define REVERSED_DATA_LEN 51
30
b47349b8
RB
31/* Represents the data above scrambled (bits flipped) after \r\n -> \n
32 * conversion, then bytewise reversed
33 */
34static unsigned char bitflipped_and_reversed_data[] =
35 { 0xf5, 0xd1, 0x9b, 0x9a, 0x93, 0x9d, 0x92, 0x9e, 0x8d, 0x9c, 0x8c,
36 0xf5, 0x86, 0x93, 0x93, 0x9e, 0x96, 0x89, 0x96, 0x8d, 0x8b, 0xf5,
37 0x9a, 0x9d, 0xdf, 0x93, 0x93, 0x96, 0x88, 0xdf, 0x8b, 0x9e, 0x97,
38 0x8b, 0xf5, 0x9e, 0x8b, 0x9e, 0x9b, 0xf5, 0x9a, 0x93, 0x8f, 0x92,
39 0x96, 0x8c, 0xdf, 0x9a, 0x92, 0x90, 0x8c };
40
41#define BITFLIPPED_AND_REVERSED_DATA_LEN 51
42
43static git_repository *g_repo = NULL;
44
45static void register_custom_filters(void);
46
47void test_filter_custom__initialize(void)
48{
49 register_custom_filters();
50
51 g_repo = cl_git_sandbox_init("empty_standard_repo");
52
53 cl_git_mkfile(
54 "empty_standard_repo/.gitattributes",
55 "hero* bitflip reverse\n"
56 "herofile text\n"
ad7417d7
RB
57 "heroflip -reverse binary\n"
58 "*.bin binary\n");
b47349b8
RB
59}
60
61void test_filter_custom__cleanup(void)
62{
63 cl_git_sandbox_cleanup();
64 g_repo = NULL;
65}
66
b47349b8
RB
67static void register_custom_filters(void)
68{
69 static int filters_registered = 0;
70
71 if (!filters_registered) {
72 cl_git_pass(git_filter_register(
73 "bitflip", create_bitflip_filter(), BITFLIP_FILTER_PRIORITY));
74
75 cl_git_pass(git_filter_register(
eefc32d5
RB
76 "reverse", create_reverse_filter("+reverse"),
77 REVERSE_FILTER_PRIORITY));
78
79 /* re-register reverse filter with standard filter=xyz priority */
80 cl_git_pass(git_filter_register(
81 "pre-reverse",
82 create_reverse_filter("+prereverse"),
83 GIT_FILTER_DRIVER_PRIORITY));
b47349b8
RB
84
85 filters_registered = 1;
86 }
87}
88
b47349b8
RB
89void test_filter_custom__to_odb(void)
90{
91 git_filter_list *fl;
92 git_buf out = { 0 };
93 git_buf in = GIT_BUF_INIT_CONST(workdir_data, strlen(workdir_data));
94
95 cl_git_pass(git_filter_list_load(
5269008c 96 &fl, g_repo, NULL, "herofile", GIT_FILTER_TO_ODB, 0));
b47349b8
RB
97
98 cl_git_pass(git_filter_list_apply_to_data(&out, fl, &in));
99
100 cl_assert_equal_i(BITFLIPPED_AND_REVERSED_DATA_LEN, out.size);
101
102 cl_assert_equal_i(
103 0, memcmp(bitflipped_and_reversed_data, out.ptr, out.size));
104
105 git_filter_list_free(fl);
106 git_buf_free(&out);
107}
108
109void test_filter_custom__to_workdir(void)
110{
111 git_filter_list *fl;
112 git_buf out = { 0 };
113 git_buf in = GIT_BUF_INIT_CONST(
114 bitflipped_and_reversed_data, BITFLIPPED_AND_REVERSED_DATA_LEN);
115
116 cl_git_pass(git_filter_list_load(
5269008c 117 &fl, g_repo, NULL, "herofile", GIT_FILTER_TO_WORKTREE, 0));
b47349b8
RB
118
119 cl_git_pass(git_filter_list_apply_to_data(&out, fl, &in));
120
121 cl_assert_equal_i(strlen(workdir_data), out.size);
122
123 cl_assert_equal_i(
124 0, memcmp(workdir_data, out.ptr, out.size));
125
126 git_filter_list_free(fl);
127 git_buf_free(&out);
128}
129
130void test_filter_custom__can_register_a_custom_filter_in_the_repository(void)
131{
132 git_filter_list *fl;
133
134 cl_git_pass(git_filter_list_load(
5269008c 135 &fl, g_repo, NULL, "herofile", GIT_FILTER_TO_WORKTREE, 0));
b47349b8
RB
136 /* expect: bitflip, reverse, crlf */
137 cl_assert_equal_sz(3, git_filter_list_length(fl));
138 git_filter_list_free(fl);
139
140 cl_git_pass(git_filter_list_load(
5269008c 141 &fl, g_repo, NULL, "herocorp", GIT_FILTER_TO_WORKTREE, 0));
ad7417d7
RB
142 /* expect: bitflip, reverse - possibly crlf depending on global config */
143 {
144 size_t flen = git_filter_list_length(fl);
145 cl_assert(flen == 2 || flen == 3);
146 }
147 git_filter_list_free(fl);
148
149 cl_git_pass(git_filter_list_load(
5269008c 150 &fl, g_repo, NULL, "hero.bin", GIT_FILTER_TO_WORKTREE, 0));
b47349b8
RB
151 /* expect: bitflip, reverse */
152 cl_assert_equal_sz(2, git_filter_list_length(fl));
153 git_filter_list_free(fl);
154
155 cl_git_pass(git_filter_list_load(
5269008c 156 &fl, g_repo, NULL, "heroflip", GIT_FILTER_TO_WORKTREE, 0));
b47349b8
RB
157 /* expect: bitflip (because of -reverse) */
158 cl_assert_equal_sz(1, git_filter_list_length(fl));
159 git_filter_list_free(fl);
160
161 cl_git_pass(git_filter_list_load(
5269008c
RB
162 &fl, g_repo, NULL, "doesntapplytome.bin",
163 GIT_FILTER_TO_WORKTREE, 0));
b47349b8
RB
164 /* expect: none */
165 cl_assert_equal_sz(0, git_filter_list_length(fl));
166 git_filter_list_free(fl);
167}
eab3746b
RB
168
169void test_filter_custom__order_dependency(void)
170{
171 git_index *index;
172 git_blob *blob;
173 git_buf buf = { 0 };
174
175 /* so if ident and reverse are used together, an interesting thing
176 * happens - a reversed "$Id$" string is no longer going to trigger
177 * ident correctly. When checking out, the filters should be applied
178 * in order CLRF, then ident, then reverse, so ident expansion should
179 * work correctly. On check in, the content should be reversed, then
180 * ident, then CRLF filtered. Let's make sure that works...
181 */
182
183 cl_git_mkfile(
184 "empty_standard_repo/.gitattributes",
eefc32d5 185 "hero.*.rev-ident text ident prereverse eol=lf\n");
eab3746b
RB
186
187 cl_git_mkfile(
188 "empty_standard_repo/hero.1.rev-ident",
189 "This is a test\n$Id$\nHave fun!\n");
190
191 cl_git_mkfile(
192 "empty_standard_repo/hero.2.rev-ident",
193 "Another test\n$dI$\nCrazy!\n");
194
195 cl_git_pass(git_repository_index(&index, g_repo));
196 cl_git_pass(git_index_add_bypath(index, "hero.1.rev-ident"));
197 cl_git_pass(git_index_add_bypath(index, "hero.2.rev-ident"));
198 cl_repo_commit_from_index(NULL, g_repo, NULL, 0, "Filter chains\n");
199 git_index_free(index);
200
201 cl_git_pass(git_blob_lookup(&blob, g_repo,
d541170c 202 & git_index_get_bypath(index, "hero.1.rev-ident", 0)->id));
eab3746b
RB
203 cl_assert_equal_s(
204 "\n!nuf evaH\n$dI$\ntset a si sihT", git_blob_rawcontent(blob));
205 cl_git_pass(git_blob_filtered_content(&buf, blob, "hero.1.rev-ident", 0));
206 /* no expansion because id was reversed at checkin and now at ident
207 * time, reverse is not applied yet */
208 cl_assert_equal_s(
209 "This is a test\n$Id$\nHave fun!\n", buf.ptr);
210 git_blob_free(blob);
211
212 cl_git_pass(git_blob_lookup(&blob, g_repo,
d541170c 213 & git_index_get_bypath(index, "hero.2.rev-ident", 0)->id));
eab3746b
RB
214 cl_assert_equal_s(
215 "\n!yzarC\n$Id$\ntset rehtonA", git_blob_rawcontent(blob));
216 cl_git_pass(git_blob_filtered_content(&buf, blob, "hero.2.rev-ident", 0));
217 /* expansion because reverse was applied at checkin and at ident time,
218 * reverse is not applied yet */
219 cl_assert_equal_s(
1ecbcd8e 220 "Another test\n$ 59001fe193103b1016b27027c0c827d036fd0ac8 :dI$\nCrazy!\n", buf.ptr);
eab3746b
RB
221 cl_assert_equal_i(0, git_oid_strcmp(
222 git_blob_id(blob), "8ca0df630d728c0c72072b6101b301391ef10095"));
223 git_blob_free(blob);
224
225 git_buf_free(&buf);
226}
eefc32d5
RB
227
228void test_filter_custom__filter_registry_failure_cases(void)
229{
230 git_filter fake = { GIT_FILTER_VERSION, 0 };
231
232 cl_assert_equal_i(GIT_EEXISTS, git_filter_register("bitflip", &fake, 0));
233
234 cl_git_fail(git_filter_unregister(GIT_FILTER_CRLF));
235 cl_git_fail(git_filter_unregister(GIT_FILTER_IDENT));
236 cl_assert_equal_i(GIT_ENOTFOUND, git_filter_unregister("not-a-filter"));
237}