t01-rawobj.c 17.2 KB
Newer Older
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
/*
 * This file is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License, version 2,
 * as published by the Free Software Foundation.
 *
 * In addition to the permissions in the GNU General Public License,
 * the authors give you unlimited permission to link the compiled
 * version of this file into combinations with other programs,
 * and to distribute those combinations without any restriction
 * coming from the use of this file.  (The General Public License
 * restrictions do apply in other respects; for example, they cover
 * modification of the file, and distribution when not linked into
 * a combined executable.)
 *
 * This file is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; see the file COPYING.  If not, write to
 * the Free Software Foundation, 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */
#include "test_lib.h"

Vicent Marti committed
27
#include "odb.h"
28 29
#include "hash.h"

Vicent Marti committed
30 31 32 33 34 35 36
#include "t01-data.h"

static int hash_object(git_oid *oid, git_rawobj *obj)
{
	return git_odb_hash(oid, obj->data, obj->len, obj->type);
}

37
BEGIN_TEST(oid0, "validate size of oid objects")
38 39 40 41 42 43 44
	git_oid out;
	must_be_true(20 == GIT_OID_RAWSZ);
	must_be_true(40 == GIT_OID_HEXSZ);
	must_be_true(sizeof(out) == GIT_OID_RAWSZ);
	must_be_true(sizeof(out.id) == GIT_OID_RAWSZ);
END_TEST

45
BEGIN_TEST(oid1, "fail when parsing an empty string as oid")
46
	git_oid out;
Vicent Marti committed
47
	must_fail(git_oid_fromstr(&out, ""));
48 49
END_TEST

50
BEGIN_TEST(oid2, "fail when parsing an invalid string as oid")
51
	git_oid out;
Vicent Marti committed
52
	must_fail(git_oid_fromstr(&out, "moo"));
53 54
END_TEST

55
BEGIN_TEST(oid3, "find all invalid characters when parsing an oid")
56 57 58 59 60 61 62 63 64 65 66 67 68
	git_oid out;
	unsigned char exp[] = {
		0x16, 0xa6, 0x77, 0x70, 0xb7,
		0xd8, 0xd7, 0x23, 0x17, 0xc4,
		0xb7, 0x75, 0x21, 0x3c, 0x23,
		0xa8, 0xbd, 0x74, 0xf5, 0xe0,
	};
	char in[41] = "16a67770b7d8d72317c4b775213c23a8bd74f5e0";
	unsigned int i;

	for (i = 0; i < 256; i++) {
		in[38] = (char)i;

nulltoken committed
69 70
		if (git__fromhex(i) >= 0) {
			exp[19] = (unsigned char)(git__fromhex(i) << 4);
Vicent Marti committed
71
			must_pass(git_oid_fromstr(&out, in));
72 73
			must_be_true(memcmp(out.id, exp, sizeof(out.id)) == 0);
		} else {
Vicent Marti committed
74
			must_fail(git_oid_fromstr(&out, in));
75 76 77 78
		}
	}
END_TEST

79
BEGIN_TEST(oid4, "fail when parsing an invalid oid string")
80
	git_oid out;
Vicent Marti committed
81
	must_fail(git_oid_fromstr(&out, "16a67770b7d8d72317c4b775213c23a8bd74f5ez"));
82 83
END_TEST

84
BEGIN_TEST(oid5, "succeed when parsing a valid oid string")
85 86 87 88 89 90 91 92
	git_oid out;
	unsigned char exp[] = {
		0x16, 0xa6, 0x77, 0x70, 0xb7,
		0xd8, 0xd7, 0x23, 0x17, 0xc4,
		0xb7, 0x75, 0x21, 0x3c, 0x23,
		0xa8, 0xbd, 0x74, 0xf5, 0xe0,
	};

Vicent Marti committed
93
	must_pass(git_oid_fromstr(&out, "16a67770b7d8d72317c4b775213c23a8bd74f5e0"));
94 95
	must_pass(memcmp(out.id, exp, sizeof(out.id)));

Vicent Marti committed
96
	must_pass(git_oid_fromstr(&out, "16A67770B7D8D72317C4b775213C23A8BD74F5E0"));
97 98 99
	must_pass(memcmp(out.id, exp, sizeof(out.id)));
END_TEST

100
BEGIN_TEST(oid6, "build a valid oid from raw bytes")
101 102 103 104 105 106 107 108
	git_oid out;
	unsigned char exp[] = {
		0x16, 0xa6, 0x77, 0x70, 0xb7,
		0xd8, 0xd7, 0x23, 0x17, 0xc4,
		0xb7, 0x75, 0x21, 0x3c, 0x23,
		0xa8, 0xbd, 0x74, 0xf5, 0xe0,
	};

Vicent Marti committed
109
	git_oid_fromraw(&out, exp);
110 111 112
	must_pass(memcmp(out.id, exp, sizeof(out.id)));
END_TEST

113
BEGIN_TEST(oid7, "properly copy an oid to another")
114 115 116 117 118 119 120 121 122
	git_oid a, b;
	unsigned char exp[] = {
		0x16, 0xa6, 0x77, 0x70, 0xb7,
		0xd8, 0xd7, 0x23, 0x17, 0xc4,
		0xb7, 0x75, 0x21, 0x3c, 0x23,
		0xa8, 0xbd, 0x74, 0xf5, 0xe0,
	};

	memset(&b, 0, sizeof(b));
Vicent Marti committed
123
	git_oid_fromraw(&a, exp);
124 125 126 127
	git_oid_cpy(&b, &a);
	must_pass(memcmp(a.id, exp, sizeof(a.id)));
END_TEST

128
BEGIN_TEST(oid8, "compare two oids (lesser than)")
129 130 131 132 133 134 135 136 137 138 139 140 141 142
	git_oid a, b;
	unsigned char a_in[] = {
		0x16, 0xa6, 0x77, 0x70, 0xb7,
		0xd8, 0xd7, 0x23, 0x17, 0xc4,
		0xb7, 0x75, 0x21, 0x3c, 0x23,
		0xa8, 0xbd, 0x74, 0xf5, 0xe0,
	};
	unsigned char b_in[] = {
		0x16, 0xa6, 0x77, 0x70, 0xb7,
		0xd8, 0xd7, 0x23, 0x17, 0xc4,
		0xb7, 0x75, 0x21, 0x3c, 0x23,
		0xa8, 0xbd, 0x74, 0xf5, 0xf0,
	};

Vicent Marti committed
143 144
	git_oid_fromraw(&a, a_in);
	git_oid_fromraw(&b, b_in);
145 146 147
	must_be_true(git_oid_cmp(&a, &b) < 0);
END_TEST

148
BEGIN_TEST(oid9, "compare two oids (equal)")
149 150 151 152 153 154 155 156
	git_oid a, b;
	unsigned char a_in[] = {
		0x16, 0xa6, 0x77, 0x70, 0xb7,
		0xd8, 0xd7, 0x23, 0x17, 0xc4,
		0xb7, 0x75, 0x21, 0x3c, 0x23,
		0xa8, 0xbd, 0x74, 0xf5, 0xe0,
	};

Vicent Marti committed
157 158
	git_oid_fromraw(&a, a_in);
	git_oid_fromraw(&b, a_in);
159 160 161
	must_be_true(git_oid_cmp(&a, &b) == 0);
END_TEST

162
BEGIN_TEST(oid10, "compare two oids (greater than)")
163 164 165 166 167 168 169 170 171 172 173 174 175 176
	git_oid a, b;
	unsigned char a_in[] = {
		0x16, 0xa6, 0x77, 0x70, 0xb7,
		0xd8, 0xd7, 0x23, 0x17, 0xc4,
		0xb7, 0x75, 0x21, 0x3c, 0x23,
		0xa8, 0xbd, 0x74, 0xf5, 0xe0,
	};
	unsigned char b_in[] = {
		0x16, 0xa6, 0x77, 0x70, 0xb7,
		0xd8, 0xd7, 0x23, 0x17, 0xc4,
		0xb7, 0x75, 0x21, 0x3c, 0x23,
		0xa8, 0xbd, 0x74, 0xf5, 0xd0,
	};

Vicent Marti committed
177 178
	git_oid_fromraw(&a, a_in);
	git_oid_fromraw(&b, b_in);
179 180 181
	must_be_true(git_oid_cmp(&a, &b) > 0);
END_TEST

182
BEGIN_TEST(oid11, "compare formated oids")
183 184 185 186
	const char *exp = "16a0123456789abcdef4b775213c23a8bd74f5e0";
	git_oid in;
	char out[GIT_OID_HEXSZ + 1];

Vicent Marti committed
187
	must_pass(git_oid_fromstr(&in, exp));
188 189 190 191 192 193 194 195

	/* Format doesn't touch the last byte */
	out[GIT_OID_HEXSZ] = 'Z';
	git_oid_fmt(out, &in);
	must_be_true(out[GIT_OID_HEXSZ] == 'Z');

	/* Format produced the right result */
	out[GIT_OID_HEXSZ] = '\0';
196
	must_be_true(strcmp(exp, out) == 0);
197 198
END_TEST

199
BEGIN_TEST(oid12, "compare oids (allocate + format)")
200 201 202 203
	const char *exp = "16a0123456789abcdef4b775213c23a8bd74f5e0";
	git_oid in;
	char *out;

Vicent Marti committed
204
	must_pass(git_oid_fromstr(&in, exp));
205 206 207

	out = git_oid_allocfmt(&in);
	must_be_true(out);
208
	must_be_true(strcmp(exp, out) == 0);
209
	git__free(out);
210 211
END_TEST

212
BEGIN_TEST(oid13, "compare oids (path format)")
213 214 215 216 217
	const char *exp1 = "16a0123456789abcdef4b775213c23a8bd74f5e0";
	const char *exp2 = "16/a0123456789abcdef4b775213c23a8bd74f5e0";
	git_oid in;
	char out[GIT_OID_HEXSZ + 2];

Vicent Marti committed
218
	must_pass(git_oid_fromstr(&in, exp1));
219 220 221 222 223 224 225 226

	/* Format doesn't touch the last byte */
	out[GIT_OID_HEXSZ + 1] = 'Z';
	git_oid_pathfmt(out, &in);
	must_be_true(out[GIT_OID_HEXSZ + 1] == 'Z');

	/* Format produced the right result */
	out[GIT_OID_HEXSZ + 1] = '\0';
227
	must_be_true(strcmp(exp2, out) == 0);
228 229
END_TEST

230
BEGIN_TEST(oid14, "convert raw oid to string")
231 232 233 234 235 236
	const char *exp = "16a0123456789abcdef4b775213c23a8bd74f5e0";
	git_oid in;
	char out[GIT_OID_HEXSZ + 1];
	char *str;
	int i;

Vicent Marti committed
237
	must_pass(git_oid_fromstr(&in, exp));
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270

	/* NULL buffer pointer, returns static empty string */
	str = git_oid_to_string(NULL, sizeof(out), &in);
	must_be_true(str && *str == '\0' && str != out);

	/* zero buffer size, returns static empty string */
	str = git_oid_to_string(out, 0, &in);
	must_be_true(str && *str == '\0' && str != out);

	/* NULL oid pointer, returns static empty string */
	str = git_oid_to_string(out, sizeof(out), NULL);
	must_be_true(str && *str == '\0' && str != out);

	/* n == 1, returns out as an empty string */
	str = git_oid_to_string(out, 1, &in);
	must_be_true(str && *str == '\0' && str == out);

	for (i = 1; i < GIT_OID_HEXSZ; i++) {
		out[i+1] = 'Z';
		str = git_oid_to_string(out, i+1, &in);
		/* returns out containing c-string */
		must_be_true(str && str == out);
		/* must be '\0' terminated */
		must_be_true(*(str+i) == '\0');
		/* must not touch bytes past end of string */
		must_be_true(*(str+(i+1)) == 'Z');
		/* i == n-1 charaters of string */
		must_pass(strncmp(exp, out, i));
	}

	/* returns out as hex formatted c-string */
	str = git_oid_to_string(out, sizeof(out), &in);
	must_be_true(str && str == out && *(str+GIT_OID_HEXSZ) == '\0');
271
	must_be_true(strcmp(exp, out) == 0);
272 273
END_TEST

274
BEGIN_TEST(oid15, "convert raw oid to string (big)")
275 276 277 278 279
	const char *exp = "16a0123456789abcdef4b775213c23a8bd74f5e0";
	git_oid in;
	char big[GIT_OID_HEXSZ + 1 + 3]; /* note + 4 => big buffer */
	char *str;

Vicent Marti committed
280
	must_pass(git_oid_fromstr(&in, exp));
281 282 283 284 285 286 287 288 289 290

	/* place some tail material */
	big[GIT_OID_HEXSZ+0] = 'W'; /* should be '\0' afterwards */
	big[GIT_OID_HEXSZ+1] = 'X'; /* should remain untouched   */
	big[GIT_OID_HEXSZ+2] = 'Y'; /* ditto */
	big[GIT_OID_HEXSZ+3] = 'Z'; /* ditto */

	/* returns big as hex formatted c-string */
	str = git_oid_to_string(big, sizeof(big), &in);
	must_be_true(str && str == big && *(str+GIT_OID_HEXSZ) == '\0');
291
	must_be_true(strcmp(exp, big) == 0);
292 293 294 295 296 297 298

	/* check tail material is untouched */
	must_be_true(str && str == big && *(str+GIT_OID_HEXSZ+1) == 'X');
	must_be_true(str && str == big && *(str+GIT_OID_HEXSZ+2) == 'Y');
	must_be_true(str && str == big && *(str+GIT_OID_HEXSZ+3) == 'Z');
END_TEST

299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381

BEGIN_TEST(oid16, "make sure the OID shortener doesn't choke on duplicate sha1s")

	git_oid_shorten *os;
	int min_len;

	os = git_oid_shorten_new(0);
	must_be_true(os != NULL);

	git_oid_shorten_add(os, "22596363b3de40b06f981fb85d82312e8c0ed511");
	git_oid_shorten_add(os, "ce08fe4884650f067bd5703b6a59a8b3b3c99a09");
	git_oid_shorten_add(os, "16a0123456789abcdef4b775213c23a8bd74f5e0");
	min_len = git_oid_shorten_add(os, "ce08fe4884650f067bd5703b6a59a8b3b3c99a09");

	must_be_true(min_len == GIT_OID_HEXSZ + 1);

	git_oid_shorten_free(os);
END_TEST

BEGIN_TEST(oid17, "stress test for the git_oid_shorten object")

#define MAX_OIDS 1000

	git_oid_shorten *os;
	char *oids[MAX_OIDS];
	char number_buffer[16];
	git_oid oid;
	size_t i, j;

	int min_len = 0, found_collision;

	os = git_oid_shorten_new(0);
	must_be_true(os != NULL);

	/*
	 * Insert in the shortener 1000 unique SHA1 ids
	 */
	for (i = 0; i < MAX_OIDS; ++i) {
		char *oid_text;

		sprintf(number_buffer, "%u", (unsigned int)i);
		git_hash_buf(&oid, number_buffer, strlen(number_buffer));

		oid_text = git__malloc(GIT_OID_HEXSZ + 1);
		git_oid_fmt(oid_text, &oid);
		oid_text[GIT_OID_HEXSZ] = 0;

		min_len = git_oid_shorten_add(os, oid_text);
		must_be_true(min_len >= 0);

		oids[i] = oid_text;
	}

	/*
	 * Compare the first `min_char - 1` characters of each
	 * SHA1 OID. If the minimizer worked, we should find at
	 * least one collision
	 */
	found_collision = 0;
	for (i = 0; i < MAX_OIDS; ++i) {
		for (j = 0; j < MAX_OIDS; ++j) {
			if (i != j && memcmp(oids[i], oids[j], min_len - 1) == 0)
				found_collision = 1;
		}
	}
	must_be_true(found_collision == 1);

	/*
	 * Compare the first `min_char` characters of each
	 * SHA1 OID. If the minimizer worked, every single preffix
	 * should be unique.
	 */
	found_collision = 0;
	for (i = 0; i < MAX_OIDS; ++i) {
		for (j = 0; j < MAX_OIDS; ++j) {
			if (i != j && memcmp(oids[i], oids[j], min_len) == 0)
				found_collision = 1;
		}
	}
	must_be_true(found_collision == 0);

	/* cleanup */
	for (i = 0; i < MAX_OIDS; ++i)
382
		git__free(oids[i]);
383 384 385 386 387 388

	git_oid_shorten_free(os);

#undef MAX_OIDS
END_TEST

389 390 391 392 393 394
static char *hello_id = "22596363b3de40b06f981fb85d82312e8c0ed511";
static char *hello_text = "hello world\n";

static char *bye_id = "ce08fe4884650f067bd5703b6a59a8b3b3c99a09";
static char *bye_text = "bye world\n";

395
BEGIN_TEST(hash0, "normal hash by blocks")
396 397 398 399 400 401 402 403
    git_hash_ctx *ctx;
    git_oid id1, id2;

    must_be_true((ctx = git_hash_new_ctx()) != NULL);

	/* should already be init'd */
    git_hash_update(ctx, hello_text, strlen(hello_text));
    git_hash_final(&id2, ctx);
Vicent Marti committed
404
    must_pass(git_oid_fromstr(&id1, hello_id));
405 406 407 408 409 410
    must_be_true(git_oid_cmp(&id1, &id2) == 0);

	/* reinit should permit reuse */
    git_hash_init(ctx);
    git_hash_update(ctx, bye_text, strlen(bye_text));
    git_hash_final(&id2, ctx);
Vicent Marti committed
411
    must_pass(git_oid_fromstr(&id1, bye_id));
412 413 414 415 416
    must_be_true(git_oid_cmp(&id1, &id2) == 0);

    git_hash_free_ctx(ctx);
END_TEST

417
BEGIN_TEST(hash1, "hash whole buffer in a single call")
418 419
    git_oid id1, id2;

Vicent Marti committed
420
    must_pass(git_oid_fromstr(&id1, hello_id));
421 422 423 424 425 426

    git_hash_buf(&id2, hello_text, strlen(hello_text));

    must_be_true(git_oid_cmp(&id1, &id2) == 0);
END_TEST

427
BEGIN_TEST(hash2, "hash a vector")
428 429 430
    git_oid id1, id2;
    git_buf_vec vec[2];

Vicent Marti committed
431
    must_pass(git_oid_fromstr(&id1, hello_id));
432 433 434 435 436 437 438 439 440 441 442

    vec[0].data = hello_text;
    vec[0].len  = 4;
    vec[1].data = hello_text+4;
    vec[1].len  = strlen(hello_text)-4;

    git_hash_vec(&id2, vec, 2);

    must_be_true(git_oid_cmp(&id1, &id2) == 0);
END_TEST

443
BEGIN_TEST(objtype0, "convert type to string")
444 445 446 447 448 449 450 451 452 453 454 455 456 457 458
	must_be_true(!strcmp(git_object_type2string(GIT_OBJ_BAD), ""));
	must_be_true(!strcmp(git_object_type2string(GIT_OBJ__EXT1), ""));
	must_be_true(!strcmp(git_object_type2string(GIT_OBJ_COMMIT), "commit"));
	must_be_true(!strcmp(git_object_type2string(GIT_OBJ_TREE), "tree"));
	must_be_true(!strcmp(git_object_type2string(GIT_OBJ_BLOB), "blob"));
	must_be_true(!strcmp(git_object_type2string(GIT_OBJ_TAG), "tag"));
	must_be_true(!strcmp(git_object_type2string(GIT_OBJ__EXT2), ""));
	must_be_true(!strcmp(git_object_type2string(GIT_OBJ_OFS_DELTA), "OFS_DELTA"));
	must_be_true(!strcmp(git_object_type2string(GIT_OBJ_REF_DELTA), "REF_DELTA"));

	must_be_true(!strcmp(git_object_type2string(-2), ""));
	must_be_true(!strcmp(git_object_type2string(8), ""));
	must_be_true(!strcmp(git_object_type2string(1234), ""));
END_TEST

459
BEGIN_TEST(objtype1, "convert string to type")
460 461 462 463 464 465 466 467 468 469 470 471 472
	must_be_true(git_object_string2type(NULL) == GIT_OBJ_BAD);
	must_be_true(git_object_string2type("") == GIT_OBJ_BAD);
	must_be_true(git_object_string2type("commit") == GIT_OBJ_COMMIT);
	must_be_true(git_object_string2type("tree") == GIT_OBJ_TREE);
	must_be_true(git_object_string2type("blob") == GIT_OBJ_BLOB);
	must_be_true(git_object_string2type("tag") == GIT_OBJ_TAG);
	must_be_true(git_object_string2type("OFS_DELTA") == GIT_OBJ_OFS_DELTA);
	must_be_true(git_object_string2type("REF_DELTA") == GIT_OBJ_REF_DELTA);

	must_be_true(git_object_string2type("CoMmIt") == GIT_OBJ_BAD);
	must_be_true(git_object_string2type("hohoho") == GIT_OBJ_BAD);
END_TEST

473
BEGIN_TEST(objtype2, "check if an object type is loose")
474 475 476 477 478 479 480 481 482 483 484 485 486 487 488
	must_be_true(git_object_typeisloose(GIT_OBJ_BAD) == 0);
	must_be_true(git_object_typeisloose(GIT_OBJ__EXT1) == 0);
	must_be_true(git_object_typeisloose(GIT_OBJ_COMMIT) == 1);
	must_be_true(git_object_typeisloose(GIT_OBJ_TREE) == 1);
	must_be_true(git_object_typeisloose(GIT_OBJ_BLOB) == 1);
	must_be_true(git_object_typeisloose(GIT_OBJ_TAG) == 1);
	must_be_true(git_object_typeisloose(GIT_OBJ__EXT2) == 0);
	must_be_true(git_object_typeisloose(GIT_OBJ_OFS_DELTA) == 0);
	must_be_true(git_object_typeisloose(GIT_OBJ_REF_DELTA) == 0);

	must_be_true(git_object_typeisloose(-2) == 0);
	must_be_true(git_object_typeisloose(8) == 0);
	must_be_true(git_object_typeisloose(1234) == 0);
END_TEST

489
BEGIN_TEST(objhash0, "hash junk data")
490 491
    git_oid id, id_zero;

Vicent Marti committed
492
    must_pass(git_oid_fromstr(&id_zero, zero_id));
493 494 495

    /* invalid types: */
    junk_obj.data = some_data;
Vicent Marti committed
496
    must_fail(hash_object(&id, &junk_obj));
497 498

    junk_obj.type = GIT_OBJ__EXT1;
Vicent Marti committed
499
    must_fail(hash_object(&id, &junk_obj));
500 501

    junk_obj.type = GIT_OBJ__EXT2;
Vicent Marti committed
502
    must_fail(hash_object(&id, &junk_obj));
503 504

    junk_obj.type = GIT_OBJ_OFS_DELTA;
Vicent Marti committed
505
    must_fail(hash_object(&id, &junk_obj));
506 507

    junk_obj.type = GIT_OBJ_REF_DELTA;
Vicent Marti committed
508
    must_fail(hash_object(&id, &junk_obj));
509 510 511 512

    /* data can be NULL only if len is zero: */
    junk_obj.type = GIT_OBJ_BLOB;
    junk_obj.data = NULL;
Vicent Marti committed
513
    must_pass(hash_object(&id, &junk_obj));
514 515 516
    must_be_true(git_oid_cmp(&id, &id_zero) == 0);

    junk_obj.len = 1;
Vicent Marti committed
517
    must_fail(hash_object(&id, &junk_obj));
518 519
END_TEST

520
BEGIN_TEST(objhash1, "hash a commit object")
521 522
    git_oid id1, id2;

Vicent Marti committed
523
    must_pass(git_oid_fromstr(&id1, commit_id));
524

Vicent Marti committed
525
    must_pass(hash_object(&id2, &commit_obj));
526 527 528 529

    must_be_true(git_oid_cmp(&id1, &id2) == 0);
END_TEST

530
BEGIN_TEST(objhash2, "hash a tree object")
531 532
    git_oid id1, id2;

Vicent Marti committed
533
    must_pass(git_oid_fromstr(&id1, tree_id));
534

Vicent Marti committed
535
    must_pass(hash_object(&id2, &tree_obj));
536 537 538 539

    must_be_true(git_oid_cmp(&id1, &id2) == 0);
END_TEST

540
BEGIN_TEST(objhash3, "hash a tag object")
541 542
    git_oid id1, id2;

Vicent Marti committed
543
    must_pass(git_oid_fromstr(&id1, tag_id));
544

Vicent Marti committed
545
    must_pass(hash_object(&id2, &tag_obj));
546 547 548 549

    must_be_true(git_oid_cmp(&id1, &id2) == 0);
END_TEST

550
BEGIN_TEST(objhash4, "hash a zero-length object")
551 552
    git_oid id1, id2;

Vicent Marti committed
553
    must_pass(git_oid_fromstr(&id1, zero_id));
554

Vicent Marti committed
555
    must_pass(hash_object(&id2, &zero_obj));
556 557 558 559

    must_be_true(git_oid_cmp(&id1, &id2) == 0);
END_TEST

560
BEGIN_TEST(objhash5, "hash an one-byte long object")
561 562
    git_oid id1, id2;

Vicent Marti committed
563
    must_pass(git_oid_fromstr(&id1, one_id));
564

Vicent Marti committed
565
    must_pass(hash_object(&id2, &one_obj));
566 567 568 569

    must_be_true(git_oid_cmp(&id1, &id2) == 0);
END_TEST

570
BEGIN_TEST(objhash6, "hash a two-byte long object")
571 572
    git_oid id1, id2;

Vicent Marti committed
573
    must_pass(git_oid_fromstr(&id1, two_id));
574

Vicent Marti committed
575
    must_pass(hash_object(&id2, &two_obj));
576 577 578 579

    must_be_true(git_oid_cmp(&id1, &id2) == 0);
END_TEST

580
BEGIN_TEST(objhash7, "hash an object several bytes long")
581 582
    git_oid id1, id2;

Vicent Marti committed
583
    must_pass(git_oid_fromstr(&id1, some_id));
584

Vicent Marti committed
585
    must_pass(hash_object(&id2, &some_obj));
586 587 588 589

    must_be_true(git_oid_cmp(&id1, &id2) == 0);
END_TEST

590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606
BEGIN_SUITE(rawobjects)
	ADD_TEST(oid0);
	ADD_TEST(oid1);
	ADD_TEST(oid2);
	ADD_TEST(oid3);
	ADD_TEST(oid4);
	ADD_TEST(oid5);
	ADD_TEST(oid6);
	ADD_TEST(oid7);
	ADD_TEST(oid8);
	ADD_TEST(oid9);
	ADD_TEST(oid10);
	ADD_TEST(oid11);
	ADD_TEST(oid12);
	ADD_TEST(oid13);
	ADD_TEST(oid14);
	ADD_TEST(oid15);
607 608
	ADD_TEST(oid16);
	ADD_TEST(oid17);
609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626

	ADD_TEST(hash0);
	ADD_TEST(hash1);
	ADD_TEST(hash2);

	ADD_TEST(objtype0);
	ADD_TEST(objtype1);
	ADD_TEST(objtype2);

	ADD_TEST(objhash0);
	ADD_TEST(objhash1);
	ADD_TEST(objhash2);
	ADD_TEST(objhash3);
	ADD_TEST(objhash4);
	ADD_TEST(objhash5);
	ADD_TEST(objhash6);
	ADD_TEST(objhash7);
END_SUITE
627