Hash :
9a99ca7b
Author :
Date :
2015-07-10T09:25:45
1 2 3 4 5 6 7 8 9 10 11 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 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
#include "clar_libgit2.h"
#include "posix.h"
#include "blob.h"
#include "filter.h"
#include "buf_text.h"
#include "git2/sys/filter.h"
#include "git2/sys/repository.h"
#include "custom_helpers.h"
static git_repository *g_repo = NULL;
static git_filter *create_wildcard_filter(void);
#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)
{
GIT_UNUSED(self);
GIT_UNUSED(src);
if (strcmp(attr_values[0], "wcflip") == 0 ||
strcmp(attr_values[0], "wcreverse") == 0) {
*payload = git__strdup(attr_values[0]);
GITERR_CHECK_ALLOC(*payload);
return 0;
}
return GIT_PASSTHROUGH;
}
static int wildcard_filter_apply(
git_filter *self,
void **payload,
git_buf *to,
const git_buf *from,
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;
}
static void wildcard_filter_cleanup(git_filter *self, void *payload)
{
GIT_UNUSED(self);
git__free(payload);
}
static void wildcard_filter_free(git_filter *f)
{
git__free(f);
}
static git_filter *create_wildcard_filter(void)
{
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;
filter->apply = wildcard_filter_apply;
filter->cleanup = wildcard_filter_cleanup;
filter->shutdown = wildcard_filter_free;
return filter;
}
void test_filter_wildcard__reverse(void)
{
git_filter_list *fl;
git_buf in = GIT_BUF_INIT, out = GIT_BUF_INIT;
cl_git_pass(git_filter_list_load(
&fl, g_repo, NULL, "hero-reverse-foo", GIT_FILTER_TO_ODB, 0));
cl_git_pass(git_buf_put(&in, (char *)input, DATA_LEN));
cl_git_pass(git_filter_list_apply_to_data(&out, fl, &in));
cl_assert_equal_i(DATA_LEN, out.size);
cl_assert_equal_i(
0, memcmp(reversed, out.ptr, out.size));
git_filter_list_free(fl);
git_buf_free(&out);
git_buf_free(&in);
}
void test_filter_wildcard__flip(void)
{
git_filter_list *fl;
git_buf in = GIT_BUF_INIT, out = GIT_BUF_INIT;
cl_git_pass(git_filter_list_load(
&fl, g_repo, NULL, "hero-flip-foo", GIT_FILTER_TO_ODB, 0));
cl_git_pass(git_buf_put(&in, (char *)input, DATA_LEN));
cl_git_pass(git_filter_list_apply_to_data(&out, fl, &in));
cl_assert_equal_i(DATA_LEN, out.size);
cl_assert_equal_i(
0, memcmp(flipped, out.ptr, out.size));
git_filter_list_free(fl);
git_buf_free(&out);
git_buf_free(&in);
}
void test_filter_wildcard__none(void)
{
git_filter_list *fl;
git_buf in = GIT_BUF_INIT, out = GIT_BUF_INIT;
cl_git_pass(git_filter_list_load(
&fl, g_repo, NULL, "none-foo", GIT_FILTER_TO_ODB, 0));
cl_git_pass(git_buf_put(&in, (char *)input, DATA_LEN));
cl_git_pass(git_filter_list_apply_to_data(&out, fl, &in));
cl_assert_equal_i(DATA_LEN, out.size);
cl_assert_equal_i(
0, memcmp(input, out.ptr, out.size));
git_filter_list_free(fl);
git_buf_free(&out);
git_buf_free(&in);
}