Blame tests/filter/wildcard.c

Packit Service 20376f
#include "clar_libgit2.h"
Packit Service 20376f
#include "posix.h"
Packit Service 20376f
#include "blob.h"
Packit Service 20376f
#include "filter.h"
Packit Service 20376f
#include "buf_text.h"
Packit Service 20376f
#include "git2/sys/filter.h"
Packit Service 20376f
#include "git2/sys/repository.h"
Packit Service 20376f
#include "custom_helpers.h"
Packit Service 20376f
Packit Service 20376f
static git_repository *g_repo = NULL;
Packit Service 20376f
Packit Service 20376f
static git_filter *create_wildcard_filter(void);
Packit Service 20376f
Packit Service 20376f
#define DATA_LEN 32
Packit Service 20376f
Packit Service 20376f
static unsigned char input[] = {
Packit Service 20376f
	0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
Packit Service 20376f
	0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
Packit Service 20376f
	0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
Packit Service 20376f
	0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
Packit Service 20376f
};
Packit Service 20376f
Packit Service 20376f
static unsigned char reversed[] = {
Packit Service 20376f
	0x1f, 0x1e, 0x1d, 0x1c, 0x1b, 0x1a, 0x19, 0x18,
Packit Service 20376f
	0x17, 0x16, 0x15, 0x14, 0x13, 0x12, 0x11, 0x10,
Packit Service 20376f
	0x0f, 0x0e, 0x0d, 0x0c, 0x0b, 0x0a, 0x09, 0x08,
Packit Service 20376f
	0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00,
Packit Service 20376f
};
Packit Service 20376f
Packit Service 20376f
static unsigned char flipped[] = {
Packit Service 20376f
	0xff, 0xfe, 0xfd, 0xfc, 0xfb, 0xfa, 0xf9, 0xf8,
Packit Service 20376f
	0xf7, 0xf6, 0xf5, 0xf4, 0xf3, 0xf2, 0xf1, 0xf0,
Packit Service 20376f
	0xef, 0xee, 0xed, 0xec, 0xeb, 0xea, 0xe9, 0xe8,
Packit Service 20376f
	0xe7, 0xe6, 0xe5, 0xe4, 0xe3, 0xe2, 0xe1, 0xe0,
Packit Service 20376f
};
Packit Service 20376f
Packit Service 20376f
void test_filter_wildcard__initialize(void)
Packit Service 20376f
{
Packit Service 20376f
	cl_git_pass(git_filter_register(
Packit Service 20376f
		"wildcard", create_wildcard_filter(), GIT_FILTER_DRIVER_PRIORITY));
Packit Service 20376f
Packit Service 20376f
	g_repo = cl_git_sandbox_init("empty_standard_repo");
Packit Service 20376f
Packit Service 20376f
	cl_git_rewritefile(
Packit Service 20376f
		"empty_standard_repo/.gitattributes",
Packit Service 20376f
		"* binary\n"
Packit Service 20376f
		"hero-flip-* filter=wcflip\n"
Packit Service 20376f
		"hero-reverse-* filter=wcreverse\n"
Packit Service 20376f
		"none-* filter=unregistered\n");
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void test_filter_wildcard__cleanup(void)
Packit Service 20376f
{
Packit Service 20376f
	cl_git_pass(git_filter_unregister("wildcard"));
Packit Service 20376f
Packit Service 20376f
	cl_git_sandbox_cleanup();
Packit Service 20376f
	g_repo = NULL;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int wildcard_filter_check(
Packit Service 20376f
	git_filter  *self,
Packit Service 20376f
	void **payload,
Packit Service 20376f
	const git_filter_source *src,
Packit Service 20376f
	const char **attr_values)
Packit Service 20376f
{
Packit Service 20376f
	GIT_UNUSED(self);
Packit Service 20376f
	GIT_UNUSED(src);
Packit Service 20376f
Packit Service 20376f
	if (strcmp(attr_values[0], "wcflip") == 0 ||
Packit Service 20376f
		strcmp(attr_values[0], "wcreverse") == 0) {
Packit Service 20376f
		*payload = git__strdup(attr_values[0]);
Packit Service 20376f
		GITERR_CHECK_ALLOC(*payload);
Packit Service 20376f
		return 0;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return GIT_PASSTHROUGH;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int wildcard_filter_apply(
Packit Service 20376f
	git_filter     *self,
Packit Service 20376f
	void          **payload,
Packit Service 20376f
	git_buf        *to,
Packit Service 20376f
	const git_buf  *from,
Packit Service 20376f
	const git_filter_source *source)
Packit Service 20376f
{
Packit Service 20376f
	const char *filtername = *payload;
Packit Service 20376f
Packit Service 20376f
	if (filtername && strcmp(filtername, "wcflip") == 0)
Packit Service 20376f
		return bitflip_filter_apply(self, payload, to, from, source);
Packit Service 20376f
	else if (filtername && strcmp(filtername, "wcreverse") == 0)
Packit Service 20376f
		return reverse_filter_apply(self, payload, to, from, source);
Packit Service 20376f
Packit Service 20376f
	cl_fail("Unexpected attribute");
Packit Service 20376f
	return GIT_PASSTHROUGH;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static void wildcard_filter_cleanup(git_filter *self, void *payload)
Packit Service 20376f
{
Packit Service 20376f
	GIT_UNUSED(self);
Packit Service 20376f
	git__free(payload);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static void wildcard_filter_free(git_filter *f)
Packit Service 20376f
{
Packit Service 20376f
	git__free(f);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static git_filter *create_wildcard_filter(void)
Packit Service 20376f
{
Packit Service 20376f
	git_filter *filter = git__calloc(1, sizeof(git_filter));
Packit Service 20376f
	cl_assert(filter);
Packit Service 20376f
Packit Service 20376f
	filter->version = GIT_FILTER_VERSION;
Packit Service 20376f
	filter->attributes = "filter=*";
Packit Service 20376f
	filter->check = wildcard_filter_check;
Packit Service 20376f
	filter->apply = wildcard_filter_apply;
Packit Service 20376f
	filter->cleanup = wildcard_filter_cleanup;
Packit Service 20376f
	filter->shutdown = wildcard_filter_free;
Packit Service 20376f
Packit Service 20376f
	return filter;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void test_filter_wildcard__reverse(void)
Packit Service 20376f
{
Packit Service 20376f
	git_filter_list *fl;
Packit Service 20376f
	git_buf in = GIT_BUF_INIT, out = GIT_BUF_INIT;
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_filter_list_load(
Packit Service 20376f
		&fl, g_repo, NULL, "hero-reverse-foo", GIT_FILTER_TO_ODB, 0));
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_buf_put(&in, (char *)input, DATA_LEN));
Packit Service 20376f
	cl_git_pass(git_filter_list_apply_to_data(&out, fl, &in);;
Packit Service 20376f
Packit Service 20376f
	cl_assert_equal_i(DATA_LEN, out.size);
Packit Service 20376f
Packit Service 20376f
	cl_assert_equal_i(
Packit Service 20376f
		0, memcmp(reversed, out.ptr, out.size));
Packit Service 20376f
Packit Service 20376f
	git_filter_list_free(fl);
Packit Service 20376f
	git_buf_free(&out;;
Packit Service 20376f
	git_buf_free(&in);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void test_filter_wildcard__flip(void)
Packit Service 20376f
{
Packit Service 20376f
	git_filter_list *fl;
Packit Service 20376f
	git_buf in = GIT_BUF_INIT, out = GIT_BUF_INIT;
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_filter_list_load(
Packit Service 20376f
		&fl, g_repo, NULL, "hero-flip-foo", GIT_FILTER_TO_ODB, 0));
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_buf_put(&in, (char *)input, DATA_LEN));
Packit Service 20376f
	cl_git_pass(git_filter_list_apply_to_data(&out, fl, &in);;
Packit Service 20376f
Packit Service 20376f
	cl_assert_equal_i(DATA_LEN, out.size);
Packit Service 20376f
Packit Service 20376f
	cl_assert_equal_i(
Packit Service 20376f
		0, memcmp(flipped, out.ptr, out.size));
Packit Service 20376f
Packit Service 20376f
	git_filter_list_free(fl);
Packit Service 20376f
	git_buf_free(&out;;
Packit Service 20376f
	git_buf_free(&in);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void test_filter_wildcard__none(void)
Packit Service 20376f
{
Packit Service 20376f
	git_filter_list *fl;
Packit Service 20376f
	git_buf in = GIT_BUF_INIT, out = GIT_BUF_INIT;
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_filter_list_load(
Packit Service 20376f
		&fl, g_repo, NULL, "none-foo", GIT_FILTER_TO_ODB, 0));
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_buf_put(&in, (char *)input, DATA_LEN));
Packit Service 20376f
	cl_git_pass(git_filter_list_apply_to_data(&out, fl, &in);;
Packit Service 20376f
Packit Service 20376f
	cl_assert_equal_i(DATA_LEN, out.size);
Packit Service 20376f
Packit Service 20376f
	cl_assert_equal_i(
Packit Service 20376f
		0, memcmp(input, out.ptr, out.size));
Packit Service 20376f
Packit Service 20376f
	git_filter_list_free(fl);
Packit Service 20376f
	git_buf_free(&out;;
Packit Service 20376f
	git_buf_free(&in);
Packit Service 20376f
}