wildcard.c 4.42 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
#include "clar_libgit2.h"
#include "posix.h"
#include "blob.h"
#include "filter.h"
#include "git2/sys/filter.h"
#include "git2/sys/repository.h"
#include "custom_helpers.h"

static git_repository *g_repo = NULL;

11
static git_filter *create_wildcard_filter(void);
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 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 56 57 58 59 60 61 62 63 64

#define DATA_LEN 32

static unsigned char input[] = {
	0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
	0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
	0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
	0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
};

static unsigned char reversed[] = {
	0x1f, 0x1e, 0x1d, 0x1c, 0x1b, 0x1a, 0x19, 0x18,
	0x17, 0x16, 0x15, 0x14, 0x13, 0x12, 0x11, 0x10,
	0x0f, 0x0e, 0x0d, 0x0c, 0x0b, 0x0a, 0x09, 0x08,
	0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00,
};

static unsigned char flipped[] = {
	0xff, 0xfe, 0xfd, 0xfc, 0xfb, 0xfa, 0xf9, 0xf8,
	0xf7, 0xf6, 0xf5, 0xf4, 0xf3, 0xf2, 0xf1, 0xf0,
	0xef, 0xee, 0xed, 0xec, 0xeb, 0xea, 0xe9, 0xe8,
	0xe7, 0xe6, 0xe5, 0xe4, 0xe3, 0xe2, 0xe1, 0xe0,
};

void test_filter_wildcard__initialize(void)
{
	cl_git_pass(git_filter_register(
		"wildcard", create_wildcard_filter(), GIT_FILTER_DRIVER_PRIORITY));

	g_repo = cl_git_sandbox_init("empty_standard_repo");

	cl_git_rewritefile(
		"empty_standard_repo/.gitattributes",
		"* binary\n"
		"hero-flip-* filter=wcflip\n"
		"hero-reverse-* filter=wcreverse\n"
		"none-* filter=unregistered\n");
}

void test_filter_wildcard__cleanup(void)
{
	cl_git_pass(git_filter_unregister("wildcard"));

	cl_git_sandbox_cleanup();
	g_repo = NULL;
}

static int wildcard_filter_check(
	git_filter  *self,
	void **payload,
	const git_filter_source *src,
	const char **attr_values)
{
65 66 67
	GIT_UNUSED(self);
	GIT_UNUSED(src);

68 69 70
	if (strcmp(attr_values[0], "wcflip") == 0 ||
		strcmp(attr_values[0], "wcreverse") == 0) {
		*payload = git__strdup(attr_values[0]);
71
		GIT_ERROR_CHECK_ALLOC(*payload);
72 73 74 75 76 77 78 79 80
		return 0;
	}

	return GIT_PASSTHROUGH;
}

static int wildcard_filter_apply(
	git_filter     *self,
	void          **payload,
81 82
	git_str        *to,
	const git_str  *from,
83 84 85 86 87 88 89 90 91 92 93 94 95
	const git_filter_source *source)
{
	const char *filtername = *payload;

	if (filtername && strcmp(filtername, "wcflip") == 0)
		return bitflip_filter_apply(self, payload, to, from, source);
	else if (filtername && strcmp(filtername, "wcreverse") == 0)
		return reverse_filter_apply(self, payload, to, from, source);

	cl_fail("Unexpected attribute");
	return GIT_PASSTHROUGH;
}

96 97 98 99 100 101 102 103 104 105 106
static int wildcard_filter_stream(
	git_writestream **out,
	git_filter *self,
	void **payload,
	const git_filter_source *src,
	git_writestream *next)
{
	return git_filter_buffered_stream_new(out,
		self, wildcard_filter_apply, NULL, payload, src, next);
}

107
static void wildcard_filter_cleanup(git_filter *self, void *payload)
108
{
109
	GIT_UNUSED(self);
110 111 112 113 114 115 116 117
	git__free(payload);
}

static void wildcard_filter_free(git_filter *f)
{
	git__free(f);
}

118
static git_filter *create_wildcard_filter(void)
119 120 121 122 123 124 125
{
	git_filter *filter = git__calloc(1, sizeof(git_filter));
	cl_assert(filter);

	filter->version = GIT_FILTER_VERSION;
	filter->attributes = "filter=*";
	filter->check = wildcard_filter_check;
126
	filter->stream = wildcard_filter_stream;
127 128 129 130 131 132 133 134 135
	filter->cleanup = wildcard_filter_cleanup;
	filter->shutdown = wildcard_filter_free;

	return filter;
}

void test_filter_wildcard__reverse(void)
{
	git_filter_list *fl;
136
	git_buf out = GIT_BUF_INIT;
137 138 139 140

	cl_git_pass(git_filter_list_load(
		&fl, g_repo, NULL, "hero-reverse-foo", GIT_FILTER_TO_ODB, 0));

141
	cl_git_pass(git_filter_list_apply_to_buffer(&out, fl, (char *)input, DATA_LEN));
142 143 144 145 146 147 148

	cl_assert_equal_i(DATA_LEN, out.size);

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

	git_filter_list_free(fl);
149
	git_buf_dispose(&out);
150 151 152 153 154
}

void test_filter_wildcard__flip(void)
{
	git_filter_list *fl;
155
	git_buf out = GIT_BUF_INIT;
156 157 158 159

	cl_git_pass(git_filter_list_load(
		&fl, g_repo, NULL, "hero-flip-foo", GIT_FILTER_TO_ODB, 0));

160
	cl_git_pass(git_filter_list_apply_to_buffer(&out, fl, (char *)input, DATA_LEN));
161 162 163 164 165 166 167

	cl_assert_equal_i(DATA_LEN, out.size);

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

	git_filter_list_free(fl);
168
	git_buf_dispose(&out);
169 170 171 172 173
}

void test_filter_wildcard__none(void)
{
	git_filter_list *fl;
174
	git_buf out = GIT_BUF_INIT;
175 176 177 178

	cl_git_pass(git_filter_list_load(
		&fl, g_repo, NULL, "none-foo", GIT_FILTER_TO_ODB, 0));

179
	cl_git_pass(git_filter_list_apply_to_buffer(&out, fl, (char *)input, DATA_LEN));
180 181 182 183 184 185 186

	cl_assert_equal_i(DATA_LEN, out.size);

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

	git_filter_list_free(fl);
187
	git_buf_dispose(&out);
188
}