parse.c 21.3 KB
Newer Older
1 2 3 4 5
#include "clar_libgit2.h"
#include <git2/types.h>
#include "commit.h"
#include "signature.h"

6
/* Fixture setup */
7 8 9
static git_repository *g_repo;
void test_commit_parse__initialize(void)
{
nulltoken committed
10
	g_repo = cl_git_sandbox_init("testrepo");
11 12 13
}
void test_commit_parse__cleanup(void)
{
nulltoken committed
14
	cl_git_sandbox_cleanup();
15 16 17
}


18
/* Header parsing */
19
typedef struct {
nulltoken committed
20 21
	const char *line;
	const char *header;
22 23 24
} parse_test_case;

static parse_test_case passing_header_cases[] = {
nulltoken committed
25 26 27 28 29 30 31 32 33
	{ "parent 05452d6349abcd67aa396dfb28660d765d8b2a36\n", "parent " },
	{ "tree 05452d6349abcd67aa396dfb28660d765d8b2a36\n", "tree " },
	{ "random_heading 05452d6349abcd67aa396dfb28660d765d8b2a36\n", "random_heading " },
	{ "stuck_heading05452d6349abcd67aa396dfb28660d765d8b2a36\n", "stuck_heading" },
	{ "tree 5F4BEFFC0759261D015AA63A3A85613FF2F235DE\n", "tree " },
	{ "tree 1A669B8AB81B5EB7D9DB69562D34952A38A9B504\n", "tree " },
	{ "tree 5B20DCC6110FCC75D31C6CEDEBD7F43ECA65B503\n", "tree " },
	{ "tree 173E7BF00EA5C33447E99E6C1255954A13026BE4\n", "tree " },
	{ NULL, NULL }
34 35 36
};

static parse_test_case failing_header_cases[] = {
nulltoken committed
37 38 39 40 41 42 43 44 45 46
	{ "parent 05452d6349abcd67aa396dfb28660d765d8b2a36", "parent " },
	{ "05452d6349abcd67aa396dfb28660d765d8b2a36\n", "tree " },
	{ "parent05452d6349abcd67aa396dfb28660d765d8b2a6a\n", "parent " },
	{ "parent 05452d6349abcd67aa396dfb280d765d8b2a6\n", "parent " },
	{ "tree  05452d6349abcd67aa396dfb28660d765d8b2a36\n", "tree " },
	{ "parent 0545xd6349abcd67aa396dfb28660d765d8b2a36\n", "parent " },
	{ "parent 0545xd6349abcd67aa396dfb28660d765d8b2a36FF\n", "parent " },
	{ "", "tree " },
	{ "", "" },
	{ NULL, NULL }
47 48 49 50
};

void test_commit_parse__header(void)
{
nulltoken committed
51
	git_oid oid;
52

nulltoken committed
53 54 55 56 57
	parse_test_case *testcase;
	for (testcase = passing_header_cases; testcase->line != NULL; testcase++)
	{
		const char *line = testcase->line;
		const char *line_end = line + strlen(line);
58

59
		cl_git_pass(git_object__parse_oid_header(&oid,
Edward Thomson committed
60
			&line, line_end, testcase->header, GIT_OID_SHA1));
nulltoken committed
61 62
		cl_assert(line == line_end);
	}
63

nulltoken committed
64 65 66 67
	for (testcase = failing_header_cases; testcase->line != NULL; testcase++)
	{
		const char *line = testcase->line;
		const char *line_end = line + strlen(line);
68

69
		cl_git_fail(git_object__parse_oid_header(&oid,
Edward Thomson committed
70
			&line, line_end, testcase->header, GIT_OID_SHA1));
nulltoken committed
71
	}
72 73 74
}


75
/* Signature parsing */
76
typedef struct {
nulltoken committed
77 78 79 80 81 82
	const char *string;
	const char *header;
	const char *name;
	const char *email;
	git_time_t time;
	int offset;
83 84 85 86 87 88 89 90 91 92 93
} passing_signature_test_case;

passing_signature_test_case passing_signature_cases[] = {
	{"author Vicent Marti <tanoku@gmail.com> 12345 \n", "author ", "Vicent Marti", "tanoku@gmail.com", 12345, 0},
	{"author Vicent Marti <> 12345 \n", "author ", "Vicent Marti", "", 12345, 0},
	{"author Vicent Marti <tanoku@gmail.com> 231301 +1020\n", "author ", "Vicent Marti", "tanoku@gmail.com", 231301, 620},
	{"author Vicent Marti with an outrageously long name which will probably overflow the buffer <tanoku@gmail.com> 12345 \n", "author ", "Vicent Marti with an outrageously long name which will probably overflow the buffer", "tanoku@gmail.com", 12345, 0},
	{"author Vicent Marti <tanokuwithaveryveryverylongemailwhichwillprobablyvoverflowtheemailbuffer@gmail.com> 12345 \n", "author ", "Vicent Marti", "tanokuwithaveryveryverylongemailwhichwillprobablyvoverflowtheemailbuffer@gmail.com", 12345, 0},
	{"committer Vicent Marti <tanoku@gmail.com> 123456 +0000 \n", "committer ", "Vicent Marti", "tanoku@gmail.com", 123456, 0},
	{"committer Vicent Marti <tanoku@gmail.com> 123456 +0100 \n", "committer ", "Vicent Marti", "tanoku@gmail.com", 123456, 60},
	{"committer Vicent Marti <tanoku@gmail.com> 123456 -0100 \n", "committer ", "Vicent Marti", "tanoku@gmail.com", 123456, -60},
94
	/* Parse a signature without an author field */
95
	{"committer <tanoku@gmail.com> 123456 -0100 \n", "committer ", "", "tanoku@gmail.com", 123456, -60},
96
	/* Parse a signature without an author field */
97
	{"committer  <tanoku@gmail.com> 123456 -0100 \n", "committer ", "", "tanoku@gmail.com", 123456, -60},
98
	/* Parse a signature with an empty author field */
99
	{"committer   <tanoku@gmail.com> 123456 -0100 \n", "committer ", "", "tanoku@gmail.com", 123456, -60},
100
	/* Parse a signature with an empty email field */
101
	{"committer Vicent Marti <> 123456 -0100 \n", "committer ", "Vicent Marti", "", 123456, -60},
102
	/* Parse a signature with an empty email field */
103
	{"committer Vicent Marti < > 123456 -0100 \n", "committer ", "Vicent Marti", "", 123456, -60},
104
	/* Parse a signature with empty name and email */
105
	{"committer <> 123456 -0100 \n", "committer ", "", "", 123456, -60},
106
	/* Parse a signature with empty name and email */
107
	{"committer  <> 123456 -0100 \n", "committer ", "", "", 123456, -60},
108
	/* Parse a signature with empty name and email */
109
	{"committer  < > 123456 -0100 \n", "committer ", "", "", 123456, -60},
110
	/* Parse an obviously invalid signature */
111
	{"committer foo<@bar> 123456 -0100 \n", "committer ", "foo", "@bar", 123456, -60},
112
	/* Parse an obviously invalid signature */
113
	{"committer    foo<@bar> 123456 -0100 \n", "committer ", "foo", "@bar", 123456, -60},
114
	/* Parse an obviously invalid signature */
115
	{"committer <>\n", "committer ", "", "", 0, 0},
116 117
	{"committer Vicent Marti <tanoku@gmail.com> 123456 -1500 \n", "committer ", "Vicent Marti", "tanoku@gmail.com", 123456, 0},
	{"committer Vicent Marti <tanoku@gmail.com> 123456 +0163 \n", "committer ", "Vicent Marti", "tanoku@gmail.com", 123456, 0},
118
	{"author Vicent Marti <tanoku@gmail.com>\n", "author ", "Vicent Marti", "tanoku@gmail.com", 0, 0},
119 120 121 122 123 124 125 126
	/* a variety of dates */
	{"author Vicent Marti <tanoku@gmail.com> 0 \n", "author ", "Vicent Marti", "tanoku@gmail.com", 0, 0},
	{"author Vicent Marti <tanoku@gmail.com> 1234567890 \n", "author ", "Vicent Marti", "tanoku@gmail.com", 1234567890, 0},
	{"author Vicent Marti <tanoku@gmail.com> 2147483647 \n", "author ", "Vicent Marti", "tanoku@gmail.com", 0x7fffffff, 0},
	{"author Vicent Marti <tanoku@gmail.com> 4294967295 \n", "author ", "Vicent Marti", "tanoku@gmail.com", 0xffffffff, 0},
	{"author Vicent Marti <tanoku@gmail.com> 4294967296 \n", "author ", "Vicent Marti", "tanoku@gmail.com", 4294967296, 0},
	{"author Vicent Marti <tanoku@gmail.com> 8589934592 \n", "author ", "Vicent Marti", "tanoku@gmail.com", 8589934592, 0},

nulltoken committed
127
	{NULL,NULL,NULL,NULL,0,0}
128 129 130
};

typedef struct {
nulltoken committed
131 132
	const char *string;
	const char *header;
133 134 135 136 137 138 139 140 141 142
} failing_signature_test_case;

failing_signature_test_case failing_signature_cases[] = {
	{"committer Vicent Marti tanoku@gmail.com> 123456 -0100 \n", "committer "},
	{"author Vicent Marti <tanoku@gmail.com> 12345 \n", "author  "},
	{"author Vicent Marti <tanoku@gmail.com> 12345 \n", "committer "},
	{"author Vicent Marti 12345 \n", "author "},
	{"author Vicent Marti <broken@email 12345 \n", "author "},
	{"committer Vicent Marti ><\n", "committer "},
	{"author ", "author "},
143
	{NULL, NULL,}
144 145 146 147
};

void test_commit_parse__signature(void)
{
nulltoken committed
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
	passing_signature_test_case *passcase;
	failing_signature_test_case *failcase;

	for (passcase = passing_signature_cases; passcase->string != NULL; passcase++)
	{
		const char *str = passcase->string;
		size_t len = strlen(passcase->string);
		struct git_signature person = {0};

		cl_git_pass(git_signature__parse(&person, &str, str + len, passcase->header, '\n'));
		cl_assert_equal_s(passcase->name, person.name);
		cl_assert_equal_s(passcase->email, person.email);
		cl_assert_equal_i((int)passcase->time, (int)person.when.time);
		cl_assert_equal_i(passcase->offset, person.when.offset);
		git__free(person.name); git__free(person.email);
	}

	for (failcase = failing_signature_cases; failcase->string != NULL; failcase++)
	{
		const char *str = failcase->string;
		size_t len = strlen(failcase->string);
		git_signature person = {0};
		cl_git_fail(git_signature__parse(&person, &str, str + len, failcase->header, '\n'));
		git__free(person.name); git__free(person.email);
	}
173 174 175 176 177
}



static char *failing_commit_cases[] = {
178
/* empty commit */
179
"",
180
/* random garbage */
181
"asd97sa9du902e9a0jdsuusad09as9du098709aweu8987sd\n",
182
/* broken endlines 1 */
183 184 185 186 187 188
"tree f6c0dad3c7b3481caa9d73db21f91964894a945b\r\n\
parent 05452d6349abcd67aa396dfb28660d765d8b2a36\r\n\
author Vicent Marti <tanoku@gmail.com> 1273848544 +0200\r\n\
committer Vicent Marti <tanoku@gmail.com> 1273848544 +0200\r\n\
\r\n\
a test commit with broken endlines\r\n",
189
/* broken endlines 2 */
190 191 192 193 194 195
"tree f6c0dad3c7b3481caa9d73db21f91964894a945b\
parent 05452d6349abcd67aa396dfb28660d765d8b2a36\
author Vicent Marti <tanoku@gmail.com> 1273848544 +0200\
committer Vicent Marti <tanoku@gmail.com> 1273848544 +0200\
\
another test commit with broken endlines",
196
/* starting endlines */
197 198 199 200 201 202
"\ntree f6c0dad3c7b3481caa9d73db21f91964894a945b\n\
parent 05452d6349abcd67aa396dfb28660d765d8b2a36\n\
author Vicent Marti <tanoku@gmail.com> 1273848544 +0200\n\
committer Vicent Marti <tanoku@gmail.com> 1273848544 +0200\n\
\n\
a test commit with a starting endline\n",
203
/* corrupted commit 1 */
204 205
"tree f6c0dad3c7b3481caa9d73db21f91964894a945b\n\
parent 05452d6349abcd67aa396df",
206
/* corrupted commit 2 */
207 208
"tree f6c0dad3c7b3481caa9d73db21f91964894a945b\n\
parent ",
209
/* corrupted commit 3 */
210 211
"tree f6c0dad3c7b3481caa9d73db21f91964894a945b\n\
parent ",
212
/* corrupted commit 4 */
213 214 215 216 217 218
"tree f6c0dad3c7b3481caa9d73db21f91964894a945b\n\
par",
};


static char *passing_commit_cases[] = {
219
/* simple commit with no message */
220 221 222 223
"tree 1810dff58d8a660512d4832e740f692884338ccd\n\
author Vicent Marti <tanoku@gmail.com> 1273848544 +0200\n\
committer Vicent Marti <tanoku@gmail.com> 1273848544 +0200\n\
\n",
224
/* simple commit, no parent */
225 226 227 228 229
"tree 1810dff58d8a660512d4832e740f692884338ccd\n\
author Vicent Marti <tanoku@gmail.com> 1273848544 +0200\n\
committer Vicent Marti <tanoku@gmail.com> 1273848544 +0200\n\
\n\
a simple commit which works\n",
230
/* simple commit, no parent, no newline in message */
231 232 233 234 235
"tree 1810dff58d8a660512d4832e740f692884338ccd\n\
author Vicent Marti <tanoku@gmail.com> 1273848544 +0200\n\
committer Vicent Marti <tanoku@gmail.com> 1273848544 +0200\n\
\n\
a simple commit which works",
236
/* simple commit, 1 parent */
237 238 239 240 241 242
"tree 1810dff58d8a660512d4832e740f692884338ccd\n\
parent e90810b8df3e80c413d903f631643c716887138d\n\
author Vicent Marti <tanoku@gmail.com> 1273848544 +0200\n\
committer Vicent Marti <tanoku@gmail.com> 1273848544 +0200\n\
\n\
a simple commit which works\n",
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
/* simple commit with GPG signature */
"tree 6b79e22d69bf46e289df0345a14ca059dfc9bdf6\n\
parent 34734e478d6cf50c27c9d69026d93974d052c454\n\
author Ben Burkert <ben@benburkert.com> 1358451456 -0800\n\
committer Ben Burkert <ben@benburkert.com> 1358451456 -0800\n\
gpgsig -----BEGIN PGP SIGNATURE-----\n\
 Version: GnuPG v1.4.12 (Darwin)\n\
 \n\
 iQIcBAABAgAGBQJQ+FMIAAoJEH+LfPdZDSs1e3EQAJMjhqjWF+WkGLHju7pTw2al\n\
 o6IoMAhv0Z/LHlWhzBd9e7JeCnanRt12bAU7yvYp9+Z+z+dbwqLwDoFp8LVuigl8\n\
 JGLcnwiUW3rSvhjdCp9irdb4+bhKUnKUzSdsR2CK4/hC0N2i/HOvMYX+BRsvqweq\n\
 AsAkA6dAWh+gAfedrBUkCTGhlNYoetjdakWqlGL1TiKAefEZrtA1TpPkGn92vbLq\n\
 SphFRUY9hVn1ZBWrT3hEpvAIcZag3rTOiRVT1X1flj8B2vGCEr3RrcwOIZikpdaW\n\
 who/X3xh/DGbI2RbuxmmJpxxP/8dsVchRJJzBwG+yhwU/iN3MlV2c5D69tls/Dok\n\
 6VbyU4lm/ae0y3yR83D9dUlkycOnmmlBAHKIZ9qUts9X7mWJf0+yy2QxJVpjaTGG\n\
 cmnQKKPeNIhGJk2ENnnnzjEve7L7YJQF6itbx5VCOcsGh3Ocb3YR7DMdWjt7f8pu\n\
 c6j+q1rP7EpE2afUN/geSlp5i3x8aXZPDj67jImbVCE/Q1X9voCtyzGJH7MXR0N9\n\
 ZpRF8yzveRfMH8bwAJjSOGAFF5XkcR/RNY95o+J+QcgBLdX48h+ZdNmUf6jqlu3J\n\
 7KmTXXQcOVpN6dD3CmRFsbjq+x6RHwa8u1iGn+oIkX908r97ckfB/kHKH7ZdXIJc\n\
 cpxtDQQMGYFpXK/71stq\n\
 =ozeK\n\
 -----END PGP SIGNATURE-----\n\
\n\
a simple commit which works\n",
267 268 269 270 271 272 273
/* some tools create two author entries */
"tree 1810dff58d8a660512d4832e740f692884338ccd\n\
author Vicent Marti <tanoku@gmail.com> 1273848544 +0200\n\
author Helpful Coworker <helpful@coworker> 1273848544 +0200\n\
committer Vicent Marti <tanoku@gmail.com> 1273848544 +0200\n\
\n\
a simple commit which works",
274 275
};

276 277 278 279 280 281 282 283 284 285 286 287 288 289
static int parse_commit(git_commit **out, const char *buffer)
{
	git_commit *commit;
	git_odb_object fake_odb_object;
	int error;

	commit = (git_commit*)git__malloc(sizeof(git_commit));
	memset(commit, 0x0, sizeof(git_commit));
	commit->object.repo = g_repo;

	memset(&fake_odb_object, 0x0, sizeof(git_odb_object));
	fake_odb_object.buffer = (char *)buffer;
	fake_odb_object.cached.size = strlen(fake_odb_object.buffer);

290
	error = git_commit__parse(commit, &fake_odb_object, GIT_OID_SHA1);
291 292 293 294 295

	*out = commit;
	return error;
}

296 297
void test_commit_parse__entire_commit(void)
{
298 299
	const int failing_commit_count = ARRAY_SIZE(failing_commit_cases);
	const int passing_commit_count = ARRAY_SIZE(passing_commit_cases);
300
	int i;
301
	git_commit *commit;
302

303
	for (i = 0; i < failing_commit_count; ++i) {
304
		cl_git_fail(parse_commit(&commit, failing_commit_cases[i]));
305 306 307
		git_commit__free(commit);
	}

308
	for (i = 0; i < passing_commit_count; ++i) {
309
		cl_git_pass(parse_commit(&commit, passing_commit_cases[i]));
310

311
		if (!i)
312
			cl_assert_equal_s("", git_commit_message(commit));
313 314 315
		else
			cl_assert(git__prefixcmp(
				git_commit_message(commit), "a simple commit which works") == 0);
316 317 318 319 320 321

		git_commit__free(commit);
	}
}


322
/* query the details on a parsed commit */
323
void test_commit_parse__details0(void) {
nulltoken committed
324 325 326 327 328 329 330 331 332
	static const char *commit_ids[] = {
		"a4a7dce85cf63874e984719f4fdd239f5145052f", /* 0 */
		"9fd738e8f7967c078dceed8190330fc8648ee56a", /* 1 */
		"4a202b346bb0fb0db7eff3cffeb3c70babbd2045", /* 2 */
		"c47800c7266a2be04c571c04d5a6614691ea99bd", /* 3 */
		"8496071c1b46c854b31185ea97743be6a8774479", /* 4 */
		"5b5b025afb0b4c913b4c338a42934a3863bf3644", /* 5 */
		"a65fedf39aefe402d3bb6e24df4d4f5fe4547750", /* 6 */
	};
333
	const size_t commit_count = sizeof(commit_ids) / sizeof(const char *);
nulltoken committed
334
	unsigned int i;
335 336 337 338 339 340 341 342 343 344 345

	for (i = 0; i < commit_count; ++i) {
		git_oid id;
		git_commit *commit;

		const git_signature *author, *committer;
		const char *message;
		git_time_t commit_time;
		unsigned int parents, p;
		git_commit *parent = NULL, *old_parent = NULL;

346
		git_oid__fromstr(&id, commit_ids[i], GIT_OID_SHA1);
347 348 349 350 351 352 353 354 355

		cl_git_pass(git_commit_lookup(&commit, g_repo, &id));

		message = git_commit_message(commit);
		author = git_commit_author(commit);
		committer = git_commit_committer(commit);
		commit_time = git_commit_time(commit);
		parents = git_commit_parentcount(commit);

356 357 358 359
		cl_assert_equal_s("Scott Chacon", author->name);
		cl_assert_equal_s("schacon@gmail.com", author->email);
		cl_assert_equal_s("Scott Chacon", committer->name);
		cl_assert_equal_s("schacon@gmail.com", committer->email);
360 361 362 363 364 365 366 367 368 369
		cl_assert(message != NULL);
		cl_assert(commit_time > 0);
		cl_assert(parents <= 2);
		for (p = 0;p < parents;p++) {
			if (old_parent != NULL)
				git_commit_free(old_parent);

			old_parent = parent;
			cl_git_pass(git_commit_parent(&parent, commit, p));
			cl_assert(parent != NULL);
370
			cl_assert(git_commit_author(parent) != NULL); /* is it really a commit? */
371 372 373 374 375 376 377 378 379
		}
		git_commit_free(old_parent);
		git_commit_free(parent);

		cl_git_fail(git_commit_parent(&parent, commit, parents));
		git_commit_free(commit);
	}
}

380 381 382 383 384 385 386 387 388 389 390 391 392
void test_commit_parse__leading_lf(void)
{
	git_commit *commit;
	const char *buffer =
"tree 1810dff58d8a660512d4832e740f692884338ccd\n\
parent e90810b8df3e80c413d903f631643c716887138d\n\
author Vicent Marti <tanoku@gmail.com> 1273848544 +0200\n\
committer Vicent Marti <tanoku@gmail.com> 1273848544 +0200\n\
\n\
\n\
\n\
This commit has a few LF at the start of the commit message";
	const char *message =
393
"This commit has a few LF at the start of the commit message";
394 395 396 397
	const char *raw_message =
"\n\
\n\
This commit has a few LF at the start of the commit message";
398 399
	cl_git_pass(parse_commit(&commit, buffer));
	cl_assert_equal_s(message, git_commit_message(commit));
400
	cl_assert_equal_s(raw_message, git_commit_message_raw(commit));
401 402 403 404 405 406 407 408 409 410 411
	git_commit__free(commit);
}

void test_commit_parse__only_lf(void)
{
	git_commit *commit;
	const char *buffer =
"tree 1810dff58d8a660512d4832e740f692884338ccd\n\
parent e90810b8df3e80c413d903f631643c716887138d\n\
author Vicent Marti <tanoku@gmail.com> 1273848544 +0200\n\
committer Vicent Marti <tanoku@gmail.com> 1273848544 +0200\n\
412
\n\
413 414 415
\n\
\n";
	const char *message = "";
416
	const char *raw_message = "\n\n";
417

418
	cl_git_pass(parse_commit(&commit, buffer));
419
	cl_assert_equal_s(message, git_commit_message(commit));
420
	cl_assert_equal_s(raw_message, git_commit_message_raw(commit));
421 422
	git_commit__free(commit);
}
423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447

void test_commit_parse__arbitrary_field(void)
{
	git_commit *commit;
	git_buf buf = GIT_BUF_INIT;
	const char *gpgsig = "-----BEGIN PGP SIGNATURE-----\n\
Version: GnuPG v1.4.12 (Darwin)\n\
\n\
iQIcBAABAgAGBQJQ+FMIAAoJEH+LfPdZDSs1e3EQAJMjhqjWF+WkGLHju7pTw2al\n\
o6IoMAhv0Z/LHlWhzBd9e7JeCnanRt12bAU7yvYp9+Z+z+dbwqLwDoFp8LVuigl8\n\
JGLcnwiUW3rSvhjdCp9irdb4+bhKUnKUzSdsR2CK4/hC0N2i/HOvMYX+BRsvqweq\n\
AsAkA6dAWh+gAfedrBUkCTGhlNYoetjdakWqlGL1TiKAefEZrtA1TpPkGn92vbLq\n\
SphFRUY9hVn1ZBWrT3hEpvAIcZag3rTOiRVT1X1flj8B2vGCEr3RrcwOIZikpdaW\n\
who/X3xh/DGbI2RbuxmmJpxxP/8dsVchRJJzBwG+yhwU/iN3MlV2c5D69tls/Dok\n\
6VbyU4lm/ae0y3yR83D9dUlkycOnmmlBAHKIZ9qUts9X7mWJf0+yy2QxJVpjaTGG\n\
cmnQKKPeNIhGJk2ENnnnzjEve7L7YJQF6itbx5VCOcsGh3Ocb3YR7DMdWjt7f8pu\n\
c6j+q1rP7EpE2afUN/geSlp5i3x8aXZPDj67jImbVCE/Q1X9voCtyzGJH7MXR0N9\n\
ZpRF8yzveRfMH8bwAJjSOGAFF5XkcR/RNY95o+J+QcgBLdX48h+ZdNmUf6jqlu3J\n\
7KmTXXQcOVpN6dD3CmRFsbjq+x6RHwa8u1iGn+oIkX908r97ckfB/kHKH7ZdXIJc\n\
cpxtDQQMGYFpXK/71stq\n\
=ozeK\n\
-----END PGP SIGNATURE-----";

	cl_git_pass(parse_commit(&commit, passing_commit_cases[4]));

448 449
	cl_git_pass(git_commit_header_field(&buf, commit, "tree"));
	cl_assert_equal_s("6b79e22d69bf46e289df0345a14ca059dfc9bdf6", buf.ptr);
450
	git_buf_dispose(&buf);
451

452 453
	cl_git_pass(git_commit_header_field(&buf, commit, "parent"));
	cl_assert_equal_s("34734e478d6cf50c27c9d69026d93974d052c454", buf.ptr);
454
	git_buf_dispose(&buf);
455 456 457

	cl_git_pass(git_commit_header_field(&buf, commit, "gpgsig"));
	cl_assert_equal_s(gpgsig, buf.ptr);
458
	git_buf_dispose(&buf);
459 460 461 462

	cl_git_fail_with(GIT_ENOTFOUND, git_commit_header_field(&buf, commit, "awesomeness"));
	cl_git_fail_with(GIT_ENOTFOUND, git_commit_header_field(&buf, commit, "par"));

463 464 465 466 467 468
	git_commit__free(commit);
	cl_git_pass(parse_commit(&commit, passing_commit_cases[0]));

	cl_git_pass(git_commit_header_field(&buf, commit, "committer"));
	cl_assert_equal_s("Vicent Marti <tanoku@gmail.com> 1273848544 +0200", buf.ptr);

469
	git_buf_dispose(&buf);
470 471
	git_commit__free(commit);
}
472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502

void test_commit_parse__extract_signature(void)
{
	git_odb *odb;
	git_oid commit_id;
	git_buf signature = GIT_BUF_INIT, signed_data = GIT_BUF_INIT;
	const char *gpgsig = "-----BEGIN PGP SIGNATURE-----\n\
Version: GnuPG v1.4.12 (Darwin)\n\
\n\
iQIcBAABAgAGBQJQ+FMIAAoJEH+LfPdZDSs1e3EQAJMjhqjWF+WkGLHju7pTw2al\n\
o6IoMAhv0Z/LHlWhzBd9e7JeCnanRt12bAU7yvYp9+Z+z+dbwqLwDoFp8LVuigl8\n\
JGLcnwiUW3rSvhjdCp9irdb4+bhKUnKUzSdsR2CK4/hC0N2i/HOvMYX+BRsvqweq\n\
AsAkA6dAWh+gAfedrBUkCTGhlNYoetjdakWqlGL1TiKAefEZrtA1TpPkGn92vbLq\n\
SphFRUY9hVn1ZBWrT3hEpvAIcZag3rTOiRVT1X1flj8B2vGCEr3RrcwOIZikpdaW\n\
who/X3xh/DGbI2RbuxmmJpxxP/8dsVchRJJzBwG+yhwU/iN3MlV2c5D69tls/Dok\n\
6VbyU4lm/ae0y3yR83D9dUlkycOnmmlBAHKIZ9qUts9X7mWJf0+yy2QxJVpjaTGG\n\
cmnQKKPeNIhGJk2ENnnnzjEve7L7YJQF6itbx5VCOcsGh3Ocb3YR7DMdWjt7f8pu\n\
c6j+q1rP7EpE2afUN/geSlp5i3x8aXZPDj67jImbVCE/Q1X9voCtyzGJH7MXR0N9\n\
ZpRF8yzveRfMH8bwAJjSOGAFF5XkcR/RNY95o+J+QcgBLdX48h+ZdNmUf6jqlu3J\n\
7KmTXXQcOVpN6dD3CmRFsbjq+x6RHwa8u1iGn+oIkX908r97ckfB/kHKH7ZdXIJc\n\
cpxtDQQMGYFpXK/71stq\n\
=ozeK\n\
-----END PGP SIGNATURE-----";

	const char *data =  "tree 6b79e22d69bf46e289df0345a14ca059dfc9bdf6\n\
parent 34734e478d6cf50c27c9d69026d93974d052c454\n\
author Ben Burkert <ben@benburkert.com> 1358451456 -0800\n\
committer Ben Burkert <ben@benburkert.com> 1358451456 -0800\n\
\n\
a simple commit which works\n";

503 504 505 506 507 508 509 510 511 512 513 514 515 516 517
	const char *oneline_signature = "tree 51832e6397b30309c8bcad9c55fa6ae67778f378\n\
parent a1b6decaaac768b5e01e1b5dbf5b2cc081bed1eb\n\
author Some User <someuser@gmail.com> 1454537944 -0700\n\
committer Some User <someuser@gmail.com> 1454537944 -0700\n\
gpgsig bad\n\
\n\
corrupt signature\n";

	const char *oneline_data = "tree 51832e6397b30309c8bcad9c55fa6ae67778f378\n\
parent a1b6decaaac768b5e01e1b5dbf5b2cc081bed1eb\n\
author Some User <someuser@gmail.com> 1454537944 -0700\n\
committer Some User <someuser@gmail.com> 1454537944 -0700\n\
\n\
corrupt signature\n";

518
	cl_git_pass(git_repository_odb__weakptr(&odb, g_repo));
519
	cl_git_pass(git_odb_write(&commit_id, odb, passing_commit_cases[4], strlen(passing_commit_cases[4]), GIT_OBJECT_COMMIT));
520 521 522 523 524

	cl_git_pass(git_commit_extract_signature(&signature, &signed_data, g_repo, &commit_id, NULL));
	cl_assert_equal_s(gpgsig, signature.ptr);
	cl_assert_equal_s(data, signed_data.ptr);

525 526
	git_buf_dispose(&signature);
	git_buf_dispose(&signed_data);
527 528 529 530 531

	cl_git_pass(git_commit_extract_signature(&signature, &signed_data, g_repo, &commit_id, "gpgsig"));
	cl_assert_equal_s(gpgsig, signature.ptr);
	cl_assert_equal_s(data, signed_data.ptr);

532 533 534
	git_buf_dispose(&signature);
	git_buf_dispose(&signed_data);

535
	/* Try to parse a tree */
536
	cl_git_pass(git_oid__fromstr(&commit_id, "45dd856fdd4d89b884c340ba0e047752d9b085d6", GIT_OID_SHA1));
537
	cl_git_fail_with(GIT_ENOTFOUND, git_commit_extract_signature(&signature, &signed_data, g_repo, &commit_id, NULL));
538
	cl_assert_equal_i(GIT_ERROR_INVALID, git_error_last()->klass);
539 540

	/* Try to parse an unsigned commit */
541
	cl_git_pass(git_odb_write(&commit_id, odb, passing_commit_cases[1], strlen(passing_commit_cases[1]), GIT_OBJECT_COMMIT));
542
	cl_git_fail_with(GIT_ENOTFOUND, git_commit_extract_signature(&signature, &signed_data, g_repo, &commit_id, NULL));
543
	cl_assert_equal_i(GIT_ERROR_OBJECT, git_error_last()->klass);
544

545
	/* Parse the commit with a single-line signature */
546
	cl_git_pass(git_odb_write(&commit_id, odb, oneline_signature, strlen(oneline_signature), GIT_OBJECT_COMMIT));
547 548 549 550
	cl_git_pass(git_commit_extract_signature(&signature, &signed_data, g_repo, &commit_id, NULL));
	cl_assert_equal_s("bad", signature.ptr);
	cl_assert_equal_s(oneline_data, signed_data.ptr);

551 552
	git_buf_dispose(&signature);
	git_buf_dispose(&signed_data);
553
}