custom.c 7.47 KB
Newer Older
Russell Belfer committed
1 2 3 4 5 6
#include "clar_libgit2.h"
#include "posix.h"
#include "blob.h"
#include "filter.h"
#include "git2/sys/filter.h"
#include "git2/sys/repository.h"
7
#include "custom_helpers.h"
Russell Belfer committed
8

Russell Belfer committed
9 10 11 12 13
/* going TO_WORKDIR, filters are executed low to high
 * going TO_ODB, filters are executed high to low
 */
#define BITFLIP_FILTER_PRIORITY -1
#define REVERSE_FILTER_PRIORITY -2
Russell Belfer committed
14 15 16 17 18 19 20 21 22 23 24 25 26 27

#ifdef GIT_WIN32
# define NEWLINE "\r\n"
#else
# define NEWLINE "\n"
#endif

static char workdir_data[] =
	"some simple" NEWLINE
	"data" NEWLINE
	"that will be" NEWLINE
	"trivially" NEWLINE
	"scrambled." NEWLINE;

28 29
#define REVERSED_DATA_LEN 51

Russell Belfer committed
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
/* Represents the data above scrambled (bits flipped) after \r\n -> \n
 * conversion, then bytewise reversed
 */
static unsigned char bitflipped_and_reversed_data[] =
	{ 0xf5, 0xd1, 0x9b, 0x9a, 0x93, 0x9d, 0x92, 0x9e, 0x8d, 0x9c, 0x8c,
	  0xf5, 0x86, 0x93, 0x93, 0x9e, 0x96, 0x89, 0x96, 0x8d, 0x8b, 0xf5,
	  0x9a, 0x9d, 0xdf, 0x93, 0x93, 0x96, 0x88, 0xdf, 0x8b, 0x9e, 0x97,
	  0x8b, 0xf5, 0x9e, 0x8b, 0x9e, 0x9b, 0xf5, 0x9a, 0x93, 0x8f, 0x92,
	  0x96, 0x8c, 0xdf, 0x9a, 0x92, 0x90, 0x8c };

#define BITFLIPPED_AND_REVERSED_DATA_LEN 51

static git_repository *g_repo = NULL;

static void register_custom_filters(void);

void test_filter_custom__initialize(void)
{
	register_custom_filters();

	g_repo = cl_git_sandbox_init("empty_standard_repo");

	cl_git_mkfile(
		"empty_standard_repo/.gitattributes",
		"hero* bitflip reverse\n"
		"herofile text\n"
56
		"heroflip -reverse binary\n"
57
		"villain erroneous\n"
58
		"*.bin binary\n");
Russell Belfer committed
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
}

void test_filter_custom__cleanup(void)
{
	cl_git_sandbox_cleanup();
	g_repo = NULL;
}

static void register_custom_filters(void)
{
	static int filters_registered = 0;

	if (!filters_registered) {
		cl_git_pass(git_filter_register(
			"bitflip", create_bitflip_filter(), BITFLIP_FILTER_PRIORITY));

		cl_git_pass(git_filter_register(
Russell Belfer committed
76 77 78 79 80 81 82 83
			"reverse", create_reverse_filter("+reverse"),
			REVERSE_FILTER_PRIORITY));

		/* re-register reverse filter with standard filter=xyz priority */
		cl_git_pass(git_filter_register(
			"pre-reverse",
			create_reverse_filter("+prereverse"),
			GIT_FILTER_DRIVER_PRIORITY));
Russell Belfer committed
84

85 86 87 88 89
		cl_git_pass(git_filter_register(
			"erroneous",
			create_erroneous_filter("+erroneous"),
			GIT_FILTER_DRIVER_PRIORITY));

Russell Belfer committed
90 91 92 93 94 95 96
		filters_registered = 1;
	}
}

void test_filter_custom__to_odb(void)
{
	git_filter_list *fl;
97 98 99
	git_buf out = GIT_BUF_INIT;
	const char *in;
	size_t in_len;
Russell Belfer committed
100 101

	cl_git_pass(git_filter_list_load(
102
		&fl, g_repo, NULL, "herofile", GIT_FILTER_TO_ODB, 0));
Russell Belfer committed
103

104 105 106 107
	in = workdir_data;
	in_len = strlen(workdir_data);

	cl_git_pass(git_filter_list_apply_to_buffer(&out, fl, in, in_len));
Russell Belfer committed
108 109 110 111 112 113 114

	cl_assert_equal_i(BITFLIPPED_AND_REVERSED_DATA_LEN, out.size);

	cl_assert_equal_i(
		0, memcmp(bitflipped_and_reversed_data, out.ptr, out.size));

	git_filter_list_free(fl);
115
	git_buf_dispose(&out);
Russell Belfer committed
116 117 118 119 120
}

void test_filter_custom__to_workdir(void)
{
	git_filter_list *fl;
121 122 123
	git_buf out = GIT_BUF_INIT;
	const char *in;
	size_t in_len;
Russell Belfer committed
124 125

	cl_git_pass(git_filter_list_load(
126
		&fl, g_repo, NULL, "herofile", GIT_FILTER_TO_WORKTREE, 0));
Russell Belfer committed
127

128 129 130 131
	in = (char *)bitflipped_and_reversed_data;
	in_len = BITFLIPPED_AND_REVERSED_DATA_LEN;

	cl_git_pass(git_filter_list_apply_to_buffer(&out, fl, in, in_len));
Russell Belfer committed
132 133 134 135 136 137 138

	cl_assert_equal_i(strlen(workdir_data), out.size);

	cl_assert_equal_i(
		0, memcmp(workdir_data, out.ptr, out.size));

	git_filter_list_free(fl);
139
	git_buf_dispose(&out);
Russell Belfer committed
140 141 142 143 144 145 146
}

void test_filter_custom__can_register_a_custom_filter_in_the_repository(void)
{
	git_filter_list *fl;

	cl_git_pass(git_filter_list_load(
147
		&fl, g_repo, NULL, "herofile", GIT_FILTER_TO_WORKTREE, 0));
Russell Belfer committed
148 149 150 151 152
	/* expect: bitflip, reverse, crlf */
	cl_assert_equal_sz(3, git_filter_list_length(fl));
	git_filter_list_free(fl);

	cl_git_pass(git_filter_list_load(
153
		&fl, g_repo, NULL, "herocorp", GIT_FILTER_TO_WORKTREE, 0));
154 155 156 157 158 159 160 161
	/* expect: bitflip, reverse - possibly crlf depending on global config */
	{
		size_t flen = git_filter_list_length(fl);
		cl_assert(flen == 2 || flen == 3);
	}
	git_filter_list_free(fl);

	cl_git_pass(git_filter_list_load(
162
		&fl, g_repo, NULL, "hero.bin", GIT_FILTER_TO_WORKTREE, 0));
Russell Belfer committed
163 164 165 166 167
	/* expect: bitflip, reverse */
	cl_assert_equal_sz(2, git_filter_list_length(fl));
	git_filter_list_free(fl);

	cl_git_pass(git_filter_list_load(
168
		&fl, g_repo, NULL, "heroflip", GIT_FILTER_TO_WORKTREE, 0));
Russell Belfer committed
169 170 171 172 173
	/* expect: bitflip (because of -reverse) */
	cl_assert_equal_sz(1, git_filter_list_length(fl));
	git_filter_list_free(fl);

	cl_git_pass(git_filter_list_load(
174 175
		&fl, g_repo, NULL, "doesntapplytome.bin",
		GIT_FILTER_TO_WORKTREE, 0));
Russell Belfer committed
176 177 178 179
	/* expect: none */
	cl_assert_equal_sz(0, git_filter_list_length(fl));
	git_filter_list_free(fl);
}
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196

void test_filter_custom__order_dependency(void)
{
	git_index *index;
	git_blob *blob;
	git_buf buf = { 0 };

	/* so if ident and reverse are used together, an interesting thing
	 * happens - a reversed "$Id$" string is no longer going to trigger
	 * ident correctly.  When checking out, the filters should be applied
	 * in order CLRF, then ident, then reverse, so ident expansion should
	 * work correctly.  On check in, the content should be reversed, then
	 * ident, then CRLF filtered.  Let's make sure that works...
	 */

	cl_git_mkfile(
		"empty_standard_repo/.gitattributes",
Russell Belfer committed
197
		"hero.*.rev-ident text ident prereverse eol=lf\n");
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213

	cl_git_mkfile(
		"empty_standard_repo/hero.1.rev-ident",
		"This is a test\n$Id$\nHave fun!\n");

	cl_git_mkfile(
		"empty_standard_repo/hero.2.rev-ident",
		"Another test\n$dI$\nCrazy!\n");

	cl_git_pass(git_repository_index(&index, g_repo));
	cl_git_pass(git_index_add_bypath(index, "hero.1.rev-ident"));
	cl_git_pass(git_index_add_bypath(index, "hero.2.rev-ident"));
	cl_repo_commit_from_index(NULL, g_repo, NULL, 0, "Filter chains\n");
	git_index_free(index);

	cl_git_pass(git_blob_lookup(&blob, g_repo,
214
		& git_index_get_bypath(index, "hero.1.rev-ident", 0)->id));
215 216
	cl_assert_equal_s(
		"\n!nuf evaH\n$dI$\ntset a si sihT", git_blob_rawcontent(blob));
217
	cl_git_pass(git_blob_filter(&buf, blob, "hero.1.rev-ident", NULL));
218 219 220 221 222 223 224
	/* no expansion because id was reversed at checkin and now at ident
	 * time, reverse is not applied yet */
	cl_assert_equal_s(
		"This is a test\n$Id$\nHave fun!\n", buf.ptr);
	git_blob_free(blob);

	cl_git_pass(git_blob_lookup(&blob, g_repo,
225
		& git_index_get_bypath(index, "hero.2.rev-ident", 0)->id));
226 227
	cl_assert_equal_s(
		"\n!yzarC\n$Id$\ntset rehtonA", git_blob_rawcontent(blob));
228
	cl_git_pass(git_blob_filter(&buf, blob, "hero.2.rev-ident", NULL));
229 230 231
	/* expansion because reverse was applied at checkin and at ident time,
	 * reverse is not applied yet */
	cl_assert_equal_s(
232
		"Another test\n$ 59001fe193103b1016b27027c0c827d036fd0ac8 :dI$\nCrazy!\n", buf.ptr);
233 234 235 236
	cl_assert_equal_i(0, git_oid_strcmp(
		git_blob_id(blob), "8ca0df630d728c0c72072b6101b301391ef10095"));
	git_blob_free(blob);

237
	git_buf_dispose(&buf);
238
}
Russell Belfer committed
239 240 241 242 243 244 245 246 247 248 249

void test_filter_custom__filter_registry_failure_cases(void)
{
	git_filter fake = { GIT_FILTER_VERSION, 0 };

	cl_assert_equal_i(GIT_EEXISTS, git_filter_register("bitflip", &fake, 0));

	cl_git_fail(git_filter_unregister(GIT_FILTER_CRLF));
	cl_git_fail(git_filter_unregister(GIT_FILTER_IDENT));
	cl_assert_equal_i(GIT_ENOTFOUND, git_filter_unregister("not-a-filter"));
}
250 251 252 253 254

void test_filter_custom__erroneous_filter_fails(void)
{
	git_filter_list *filters;
	git_buf out = GIT_BUF_INIT;
255 256
	const char *in;
	size_t in_len;
257 258 259 260

	cl_git_pass(git_filter_list_load(
		&filters, g_repo, NULL, "villain", GIT_FILTER_TO_WORKTREE, 0));

261 262 263 264
	in = workdir_data;
	in_len = strlen(workdir_data);

	cl_git_fail(git_filter_list_apply_to_buffer(&out, filters, in, in_len));
265 266

	git_filter_list_free(filters);
267
	git_buf_dispose(&out);
268
}