simple.c 6.94 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#include "clar_libgit2.h"
#include "repository.h"
#include "backend_helpers.h"

#define EMPTY_HASH "e69de29bb2d1d6434b8b29ae775ad8c2e48c5391"

static git_repository *_repo;
static git_odb *_odb;
static git_odb_object *_obj;
static git_oid _oid;

static void setup_backend(const fake_object *objs)
{
	git_odb_backend *backend;

16
	cl_git_pass(build_fake_backend(&backend, objs, false));
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

	cl_git_pass(git_repository_odb__weakptr(&_odb, _repo));
	cl_git_pass(git_odb_add_backend(_odb, backend, 10));
}

static void assert_object_contains(git_odb_object *obj, const char *expected)
{
	const char *actual = (const char *) git_odb_object_data(obj);

	cl_assert_equal_s(actual, expected);
}

void test_odb_backend_simple__initialize(void)
{
	_repo = cl_git_sandbox_init("testrepo.git");
	_odb = NULL;
	_obj = NULL;
}

void test_odb_backend_simple__cleanup(void)
{
	git_odb_object_free(_obj);
	cl_git_sandbox_cleanup();
	cl_git_pass(git_libgit2_opts(GIT_OPT_ENABLE_STRICT_HASH_VERIFICATION, 1));
}

void test_odb_backend_simple__read_of_object_succeeds(void)
{
	const fake_object objs[] = {
		{ "f6ea0495187600e7b2288c8ac19c5886383a4632", "foobar" },
		{ NULL, NULL }
	};

	setup_backend(objs);

52
	cl_git_pass(git_oid__fromstr(&_oid, objs[0].oid, GIT_OID_SHA1));
53 54 55 56 57 58 59 60 61 62 63 64 65 66
	cl_git_pass(git_odb_read(&_obj, _odb, &_oid));

	assert_object_contains(_obj, objs[0].content);
}

void test_odb_backend_simple__read_of_nonexisting_object_fails(void)
{
	const fake_object objs[] = {
		{ "f6ea0495187600e7b2288c8ac19c5886383a4632", "foobar" },
		{ NULL, NULL }
	};

	setup_backend(objs);

67
	cl_git_pass(git_oid__fromstr(&_oid, "f6ea0495187600e7b2288c8ac19c5886383a4633", GIT_OID_SHA1));
68 69 70 71 72 73 74 75 76 77 78 79
	cl_git_fail_with(GIT_ENOTFOUND, git_odb_read(&_obj, _odb, &_oid));
}

void test_odb_backend_simple__read_with_hash_mismatch_fails(void)
{
	const fake_object objs[] = {
		{ "1234567890123456789012345678901234567890", "nonmatching content" },
		{ NULL, NULL }
	};

	setup_backend(objs);

80
	cl_git_pass(git_oid__fromstr(&_oid, objs[0].oid, GIT_OID_SHA1));
81 82 83 84 85 86 87 88 89 90 91
	cl_git_fail_with(GIT_EMISMATCH, git_odb_read(&_obj, _odb, &_oid));
}

void test_odb_backend_simple__read_with_hash_mismatch_succeeds_without_verification(void)
{
	const fake_object objs[] = {
		{ "1234567890123456789012345678901234567890", "nonmatching content" },
		{ NULL, NULL }
	};

	setup_backend(objs);
92
	cl_git_pass(git_oid__fromstr(&_oid, objs[0].oid, GIT_OID_SHA1));
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108

	cl_git_pass(git_libgit2_opts(GIT_OPT_ENABLE_STRICT_HASH_VERIFICATION, 0));
	cl_git_pass(git_odb_read(&_obj, _odb, &_oid));

	assert_object_contains(_obj, objs[0].content);
}

void test_odb_backend_simple__read_prefix_succeeds(void)
{
	const fake_object objs[] = {
		{ "f6ea0495187600e7b2288c8ac19c5886383a4632", "foobar" },
		{ NULL, NULL }
	};

	setup_backend(objs);

109
	cl_git_pass(git_oid__fromstr(&_oid, "f6ea0495187600e7b2288c8ac19c5886383a4632", GIT_OID_SHA1));
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
	cl_git_pass(git_odb_read(&_obj, _odb, &_oid));

	assert_object_contains(_obj, objs[0].content);
}

void test_odb_backend_simple__read_prefix_of_nonexisting_object_fails(void)
{
	const fake_object objs[] = {
		{ "f6ea0495187600e7b2288c8ac19c5886383a4632", "foobar" },
		{ NULL, NULL }
	};
	char *hash = "f6ea0495187600e8";

	setup_backend(objs);

125
	cl_git_pass(git_oid__fromstrn(&_oid, hash, strlen(hash), GIT_OID_SHA1));
126 127 128 129 130 131 132 133 134 135 136 137 138
	cl_git_fail_with(GIT_ENOTFOUND, git_odb_read(&_obj, _odb, &_oid));
}

void test_odb_backend_simple__read_with_ambiguous_prefix_fails(void)
{
	const fake_object objs[] = {
		{ "1234567890111111111111111111111111111111", "first content" },
		{ "1234567890222222222222222222222222222222", "second content" },
		{ NULL, NULL }
	};

	setup_backend(objs);

139
	cl_git_pass(git_oid__fromstr(&_oid, objs[0].oid, GIT_OID_SHA1));
140 141 142 143 144 145 146 147 148 149 150 151 152
	cl_git_fail_with(GIT_EAMBIGUOUS, git_odb_read_prefix(&_obj, _odb, &_oid, 7));
}

void test_odb_backend_simple__read_with_highly_ambiguous_prefix(void)
{
	const fake_object objs[] = {
		{ "1234567890111111111111111111111111111111", "first content" },
		{ "1234567890111111111111111111111111111112", "second content" },
		{ NULL, NULL }
	};

	setup_backend(objs);

153
	cl_git_pass(git_oid__fromstr(&_oid, objs[0].oid, GIT_OID_SHA1));
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
	cl_git_pass(git_libgit2_opts(GIT_OPT_ENABLE_STRICT_HASH_VERIFICATION, 0));
	cl_git_fail_with(GIT_EAMBIGUOUS, git_odb_read_prefix(&_obj, _odb, &_oid, 39));
	cl_git_pass(git_odb_read_prefix(&_obj, _odb, &_oid, 40));
	assert_object_contains(_obj, objs[0].content);
}

void test_odb_backend_simple__exists_succeeds(void)
{
	const fake_object objs[] = {
		{ "f6ea0495187600e7b2288c8ac19c5886383a4632", "foobar" },
		{ NULL, NULL }
	};

	setup_backend(objs);

169
	cl_git_pass(git_oid__fromstr(&_oid, objs[0].oid, GIT_OID_SHA1));
170 171 172 173 174 175 176 177 178 179 180 181
	cl_assert(git_odb_exists(_odb, &_oid));
}

void test_odb_backend_simple__exists_fails_for_nonexisting_object(void)
{
	const fake_object objs[] = {
		{ "f6ea0495187600e7b2288c8ac19c5886383a4632", "foobar" },
		{ NULL, NULL }
	};

	setup_backend(objs);

182
	cl_git_pass(git_oid__fromstr(&_oid, "f6ea0495187600e7b2288c8ac19c5886383a4633", GIT_OID_SHA1));
183 184 185 186 187 188 189 190 191 192 193 194 195 196
	cl_assert(git_odb_exists(_odb, &_oid) == 0);
}

void test_odb_backend_simple__exists_prefix_succeeds(void)
{
	const fake_object objs[] = {
		{ "1234567890111111111111111111111111111111", "first content" },
		{ "1234567890222222222222222222222222222222", "second content" },
		{ NULL, NULL }
	};
	git_oid found;

	setup_backend(objs);

197
	cl_git_pass(git_oid__fromstr(&_oid, objs[0].oid, GIT_OID_SHA1));
198 199 200 201 202 203 204 205 206 207 208 209 210 211
	cl_git_pass(git_odb_exists_prefix(&found, _odb, &_oid, 12));
	cl_assert(git_oid_equal(&found, &_oid));
}

void test_odb_backend_simple__exists_with_ambiguous_prefix_fails(void)
{
	const fake_object objs[] = {
		{ "1234567890111111111111111111111111111111", "first content" },
		{ "1234567890222222222222222222222222222222", "second content" },
		{ NULL, NULL }
	};

	setup_backend(objs);

212
	cl_git_pass(git_oid__fromstr(&_oid, objs[0].oid, GIT_OID_SHA1));
213 214 215 216 217 218 219 220 221 222 223 224 225 226
	cl_git_fail_with(GIT_EAMBIGUOUS, git_odb_exists_prefix(NULL, _odb, &_oid, 7));
}

void test_odb_backend_simple__exists_with_highly_ambiguous_prefix(void)
{
	const fake_object objs[] = {
		{ "1234567890111111111111111111111111111111", "first content" },
		{ "1234567890111111111111111111111111111112", "second content" },
		{ NULL, NULL }
	};
	git_oid found;

	setup_backend(objs);

227
	cl_git_pass(git_oid__fromstr(&_oid, objs[0].oid, GIT_OID_SHA1));
228 229 230 231 232
	cl_git_pass(git_libgit2_opts(GIT_OPT_ENABLE_STRICT_HASH_VERIFICATION, 0));
	cl_git_fail_with(GIT_EAMBIGUOUS, git_odb_exists_prefix(&found, _odb, &_oid, 39));
	cl_git_pass(git_odb_exists_prefix(&found, _odb, &_oid, 40));
	cl_assert(git_oid_equal(&found, &_oid));
}
233 234 235 236 237 238 239

void test_odb_backend_simple__null_oid_is_ignored(void)
{
	const fake_object objs[] = {
		{ "0000000000000000000000000000000000000000", "null oid content" },
		{ NULL, NULL }
	};
240
	git_oid null_oid = GIT_OID_SHA1_ZERO;
241 242 243 244 245 246 247 248
	git_odb_object *obj;

	setup_backend(objs);

	cl_git_pass(git_libgit2_opts(GIT_OPT_ENABLE_STRICT_HASH_VERIFICATION, 0));
	cl_assert(!git_odb_exists(_odb, &null_oid));

	cl_git_fail_with(GIT_ENOTFOUND, git_odb_read(&obj, _odb, &null_oid));
249
	cl_assert(git_error_last() && strstr(git_error_last()->message, "null OID"));
250
}