]> git.proxmox.com Git - libgit2.git/blob - tests/commit/parse.c
New upstream version 1.4.3+dfsg.1
[libgit2.git] / tests / commit / parse.c
1 #include "clar_libgit2.h"
2 #include <git2/types.h>
3 #include "commit.h"
4 #include "signature.h"
5
6 /* Fixture setup */
7 static git_repository *g_repo;
8 void test_commit_parse__initialize(void)
9 {
10 g_repo = cl_git_sandbox_init("testrepo");
11 }
12 void test_commit_parse__cleanup(void)
13 {
14 cl_git_sandbox_cleanup();
15 }
16
17
18 /* Header parsing */
19 typedef struct {
20 const char *line;
21 const char *header;
22 } parse_test_case;
23
24 static parse_test_case passing_header_cases[] = {
25 { "parent 05452d6349abcd67aa396dfb28660d765d8b2a36\n", "parent " },
26 { "tree 05452d6349abcd67aa396dfb28660d765d8b2a36\n", "tree " },
27 { "random_heading 05452d6349abcd67aa396dfb28660d765d8b2a36\n", "random_heading " },
28 { "stuck_heading05452d6349abcd67aa396dfb28660d765d8b2a36\n", "stuck_heading" },
29 { "tree 5F4BEFFC0759261D015AA63A3A85613FF2F235DE\n", "tree " },
30 { "tree 1A669B8AB81B5EB7D9DB69562D34952A38A9B504\n", "tree " },
31 { "tree 5B20DCC6110FCC75D31C6CEDEBD7F43ECA65B503\n", "tree " },
32 { "tree 173E7BF00EA5C33447E99E6C1255954A13026BE4\n", "tree " },
33 { NULL, NULL }
34 };
35
36 static parse_test_case failing_header_cases[] = {
37 { "parent 05452d6349abcd67aa396dfb28660d765d8b2a36", "parent " },
38 { "05452d6349abcd67aa396dfb28660d765d8b2a36\n", "tree " },
39 { "parent05452d6349abcd67aa396dfb28660d765d8b2a6a\n", "parent " },
40 { "parent 05452d6349abcd67aa396dfb280d765d8b2a6\n", "parent " },
41 { "tree 05452d6349abcd67aa396dfb28660d765d8b2a36\n", "tree " },
42 { "parent 0545xd6349abcd67aa396dfb28660d765d8b2a36\n", "parent " },
43 { "parent 0545xd6349abcd67aa396dfb28660d765d8b2a36FF\n", "parent " },
44 { "", "tree " },
45 { "", "" },
46 { NULL, NULL }
47 };
48
49 void test_commit_parse__header(void)
50 {
51 git_oid oid;
52
53 parse_test_case *testcase;
54 for (testcase = passing_header_cases; testcase->line != NULL; testcase++)
55 {
56 const char *line = testcase->line;
57 const char *line_end = line + strlen(line);
58
59 cl_git_pass(git_oid__parse(&oid, &line, line_end, testcase->header));
60 cl_assert(line == line_end);
61 }
62
63 for (testcase = failing_header_cases; testcase->line != NULL; testcase++)
64 {
65 const char *line = testcase->line;
66 const char *line_end = line + strlen(line);
67
68 cl_git_fail(git_oid__parse(&oid, &line, line_end, testcase->header));
69 }
70 }
71
72
73 /* Signature parsing */
74 typedef struct {
75 const char *string;
76 const char *header;
77 const char *name;
78 const char *email;
79 git_time_t time;
80 int offset;
81 } passing_signature_test_case;
82
83 passing_signature_test_case passing_signature_cases[] = {
84 {"author Vicent Marti <tanoku@gmail.com> 12345 \n", "author ", "Vicent Marti", "tanoku@gmail.com", 12345, 0},
85 {"author Vicent Marti <> 12345 \n", "author ", "Vicent Marti", "", 12345, 0},
86 {"author Vicent Marti <tanoku@gmail.com> 231301 +1020\n", "author ", "Vicent Marti", "tanoku@gmail.com", 231301, 620},
87 {"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},
88 {"author Vicent Marti <tanokuwithaveryveryverylongemailwhichwillprobablyvoverflowtheemailbuffer@gmail.com> 12345 \n", "author ", "Vicent Marti", "tanokuwithaveryveryverylongemailwhichwillprobablyvoverflowtheemailbuffer@gmail.com", 12345, 0},
89 {"committer Vicent Marti <tanoku@gmail.com> 123456 +0000 \n", "committer ", "Vicent Marti", "tanoku@gmail.com", 123456, 0},
90 {"committer Vicent Marti <tanoku@gmail.com> 123456 +0100 \n", "committer ", "Vicent Marti", "tanoku@gmail.com", 123456, 60},
91 {"committer Vicent Marti <tanoku@gmail.com> 123456 -0100 \n", "committer ", "Vicent Marti", "tanoku@gmail.com", 123456, -60},
92 /* Parse a signature without an author field */
93 {"committer <tanoku@gmail.com> 123456 -0100 \n", "committer ", "", "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 with an empty author field */
97 {"committer <tanoku@gmail.com> 123456 -0100 \n", "committer ", "", "tanoku@gmail.com", 123456, -60},
98 /* Parse a signature with an empty email field */
99 {"committer Vicent Marti <> 123456 -0100 \n", "committer ", "Vicent Marti", "", 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 empty name and email */
103 {"committer <> 123456 -0100 \n", "committer ", "", "", 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 an obviously invalid signature */
109 {"committer foo<@bar> 123456 -0100 \n", "committer ", "foo", "@bar", 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 <>\n", "committer ", "", "", 0, 0},
114 {"committer Vicent Marti <tanoku@gmail.com> 123456 -1500 \n", "committer ", "Vicent Marti", "tanoku@gmail.com", 123456, 0},
115 {"committer Vicent Marti <tanoku@gmail.com> 123456 +0163 \n", "committer ", "Vicent Marti", "tanoku@gmail.com", 123456, 0},
116 {"author Vicent Marti <tanoku@gmail.com>\n", "author ", "Vicent Marti", "tanoku@gmail.com", 0, 0},
117 /* a variety of dates */
118 {"author Vicent Marti <tanoku@gmail.com> 0 \n", "author ", "Vicent Marti", "tanoku@gmail.com", 0, 0},
119 {"author Vicent Marti <tanoku@gmail.com> 1234567890 \n", "author ", "Vicent Marti", "tanoku@gmail.com", 1234567890, 0},
120 {"author Vicent Marti <tanoku@gmail.com> 2147483647 \n", "author ", "Vicent Marti", "tanoku@gmail.com", 0x7fffffff, 0},
121 {"author Vicent Marti <tanoku@gmail.com> 4294967295 \n", "author ", "Vicent Marti", "tanoku@gmail.com", 0xffffffff, 0},
122 {"author Vicent Marti <tanoku@gmail.com> 4294967296 \n", "author ", "Vicent Marti", "tanoku@gmail.com", 4294967296, 0},
123 {"author Vicent Marti <tanoku@gmail.com> 8589934592 \n", "author ", "Vicent Marti", "tanoku@gmail.com", 8589934592, 0},
124
125 {NULL,NULL,NULL,NULL,0,0}
126 };
127
128 typedef struct {
129 const char *string;
130 const char *header;
131 } failing_signature_test_case;
132
133 failing_signature_test_case failing_signature_cases[] = {
134 {"committer Vicent Marti tanoku@gmail.com> 123456 -0100 \n", "committer "},
135 {"author Vicent Marti <tanoku@gmail.com> 12345 \n", "author "},
136 {"author Vicent Marti <tanoku@gmail.com> 12345 \n", "committer "},
137 {"author Vicent Marti 12345 \n", "author "},
138 {"author Vicent Marti <broken@email 12345 \n", "author "},
139 {"committer Vicent Marti ><\n", "committer "},
140 {"author ", "author "},
141 {NULL, NULL,}
142 };
143
144 void test_commit_parse__signature(void)
145 {
146 passing_signature_test_case *passcase;
147 failing_signature_test_case *failcase;
148
149 for (passcase = passing_signature_cases; passcase->string != NULL; passcase++)
150 {
151 const char *str = passcase->string;
152 size_t len = strlen(passcase->string);
153 struct git_signature person = {0};
154
155 cl_git_pass(git_signature__parse(&person, &str, str + len, passcase->header, '\n'));
156 cl_assert_equal_s(passcase->name, person.name);
157 cl_assert_equal_s(passcase->email, person.email);
158 cl_assert_equal_i((int)passcase->time, (int)person.when.time);
159 cl_assert_equal_i(passcase->offset, person.when.offset);
160 git__free(person.name); git__free(person.email);
161 }
162
163 for (failcase = failing_signature_cases; failcase->string != NULL; failcase++)
164 {
165 const char *str = failcase->string;
166 size_t len = strlen(failcase->string);
167 git_signature person = {0};
168 cl_git_fail(git_signature__parse(&person, &str, str + len, failcase->header, '\n'));
169 git__free(person.name); git__free(person.email);
170 }
171 }
172
173
174
175 static char *failing_commit_cases[] = {
176 /* empty commit */
177 "",
178 /* random garbage */
179 "asd97sa9du902e9a0jdsuusad09as9du098709aweu8987sd\n",
180 /* broken endlines 1 */
181 "tree f6c0dad3c7b3481caa9d73db21f91964894a945b\r\n\
182 parent 05452d6349abcd67aa396dfb28660d765d8b2a36\r\n\
183 author Vicent Marti <tanoku@gmail.com> 1273848544 +0200\r\n\
184 committer Vicent Marti <tanoku@gmail.com> 1273848544 +0200\r\n\
185 \r\n\
186 a test commit with broken endlines\r\n",
187 /* broken endlines 2 */
188 "tree f6c0dad3c7b3481caa9d73db21f91964894a945b\
189 parent 05452d6349abcd67aa396dfb28660d765d8b2a36\
190 author Vicent Marti <tanoku@gmail.com> 1273848544 +0200\
191 committer Vicent Marti <tanoku@gmail.com> 1273848544 +0200\
192 \
193 another test commit with broken endlines",
194 /* starting endlines */
195 "\ntree f6c0dad3c7b3481caa9d73db21f91964894a945b\n\
196 parent 05452d6349abcd67aa396dfb28660d765d8b2a36\n\
197 author Vicent Marti <tanoku@gmail.com> 1273848544 +0200\n\
198 committer Vicent Marti <tanoku@gmail.com> 1273848544 +0200\n\
199 \n\
200 a test commit with a starting endline\n",
201 /* corrupted commit 1 */
202 "tree f6c0dad3c7b3481caa9d73db21f91964894a945b\n\
203 parent 05452d6349abcd67aa396df",
204 /* corrupted commit 2 */
205 "tree f6c0dad3c7b3481caa9d73db21f91964894a945b\n\
206 parent ",
207 /* corrupted commit 3 */
208 "tree f6c0dad3c7b3481caa9d73db21f91964894a945b\n\
209 parent ",
210 /* corrupted commit 4 */
211 "tree f6c0dad3c7b3481caa9d73db21f91964894a945b\n\
212 par",
213 };
214
215
216 static char *passing_commit_cases[] = {
217 /* simple commit with no message */
218 "tree 1810dff58d8a660512d4832e740f692884338ccd\n\
219 author Vicent Marti <tanoku@gmail.com> 1273848544 +0200\n\
220 committer Vicent Marti <tanoku@gmail.com> 1273848544 +0200\n\
221 \n",
222 /* simple commit, no parent */
223 "tree 1810dff58d8a660512d4832e740f692884338ccd\n\
224 author Vicent Marti <tanoku@gmail.com> 1273848544 +0200\n\
225 committer Vicent Marti <tanoku@gmail.com> 1273848544 +0200\n\
226 \n\
227 a simple commit which works\n",
228 /* simple commit, no parent, no newline in message */
229 "tree 1810dff58d8a660512d4832e740f692884338ccd\n\
230 author Vicent Marti <tanoku@gmail.com> 1273848544 +0200\n\
231 committer Vicent Marti <tanoku@gmail.com> 1273848544 +0200\n\
232 \n\
233 a simple commit which works",
234 /* simple commit, 1 parent */
235 "tree 1810dff58d8a660512d4832e740f692884338ccd\n\
236 parent e90810b8df3e80c413d903f631643c716887138d\n\
237 author Vicent Marti <tanoku@gmail.com> 1273848544 +0200\n\
238 committer Vicent Marti <tanoku@gmail.com> 1273848544 +0200\n\
239 \n\
240 a simple commit which works\n",
241 /* simple commit with GPG signature */
242 "tree 6b79e22d69bf46e289df0345a14ca059dfc9bdf6\n\
243 parent 34734e478d6cf50c27c9d69026d93974d052c454\n\
244 author Ben Burkert <ben@benburkert.com> 1358451456 -0800\n\
245 committer Ben Burkert <ben@benburkert.com> 1358451456 -0800\n\
246 gpgsig -----BEGIN PGP SIGNATURE-----\n\
247 Version: GnuPG v1.4.12 (Darwin)\n\
248 \n\
249 iQIcBAABAgAGBQJQ+FMIAAoJEH+LfPdZDSs1e3EQAJMjhqjWF+WkGLHju7pTw2al\n\
250 o6IoMAhv0Z/LHlWhzBd9e7JeCnanRt12bAU7yvYp9+Z+z+dbwqLwDoFp8LVuigl8\n\
251 JGLcnwiUW3rSvhjdCp9irdb4+bhKUnKUzSdsR2CK4/hC0N2i/HOvMYX+BRsvqweq\n\
252 AsAkA6dAWh+gAfedrBUkCTGhlNYoetjdakWqlGL1TiKAefEZrtA1TpPkGn92vbLq\n\
253 SphFRUY9hVn1ZBWrT3hEpvAIcZag3rTOiRVT1X1flj8B2vGCEr3RrcwOIZikpdaW\n\
254 who/X3xh/DGbI2RbuxmmJpxxP/8dsVchRJJzBwG+yhwU/iN3MlV2c5D69tls/Dok\n\
255 6VbyU4lm/ae0y3yR83D9dUlkycOnmmlBAHKIZ9qUts9X7mWJf0+yy2QxJVpjaTGG\n\
256 cmnQKKPeNIhGJk2ENnnnzjEve7L7YJQF6itbx5VCOcsGh3Ocb3YR7DMdWjt7f8pu\n\
257 c6j+q1rP7EpE2afUN/geSlp5i3x8aXZPDj67jImbVCE/Q1X9voCtyzGJH7MXR0N9\n\
258 ZpRF8yzveRfMH8bwAJjSOGAFF5XkcR/RNY95o+J+QcgBLdX48h+ZdNmUf6jqlu3J\n\
259 7KmTXXQcOVpN6dD3CmRFsbjq+x6RHwa8u1iGn+oIkX908r97ckfB/kHKH7ZdXIJc\n\
260 cpxtDQQMGYFpXK/71stq\n\
261 =ozeK\n\
262 -----END PGP SIGNATURE-----\n\
263 \n\
264 a simple commit which works\n",
265 /* some tools create two author entries */
266 "tree 1810dff58d8a660512d4832e740f692884338ccd\n\
267 author Vicent Marti <tanoku@gmail.com> 1273848544 +0200\n\
268 author Helpful Coworker <helpful@coworker> 1273848544 +0200\n\
269 committer Vicent Marti <tanoku@gmail.com> 1273848544 +0200\n\
270 \n\
271 a simple commit which works",
272 };
273
274 static int parse_commit(git_commit **out, const char *buffer)
275 {
276 git_commit *commit;
277 git_odb_object fake_odb_object;
278 int error;
279
280 commit = (git_commit*)git__malloc(sizeof(git_commit));
281 memset(commit, 0x0, sizeof(git_commit));
282 commit->object.repo = g_repo;
283
284 memset(&fake_odb_object, 0x0, sizeof(git_odb_object));
285 fake_odb_object.buffer = (char *)buffer;
286 fake_odb_object.cached.size = strlen(fake_odb_object.buffer);
287
288 error = git_commit__parse(commit, &fake_odb_object);
289
290 *out = commit;
291 return error;
292 }
293
294 void test_commit_parse__entire_commit(void)
295 {
296 const int failing_commit_count = ARRAY_SIZE(failing_commit_cases);
297 const int passing_commit_count = ARRAY_SIZE(passing_commit_cases);
298 int i;
299 git_commit *commit;
300
301 for (i = 0; i < failing_commit_count; ++i) {
302 cl_git_fail(parse_commit(&commit, failing_commit_cases[i]));
303 git_commit__free(commit);
304 }
305
306 for (i = 0; i < passing_commit_count; ++i) {
307 cl_git_pass(parse_commit(&commit, passing_commit_cases[i]));
308
309 if (!i)
310 cl_assert_equal_s("", git_commit_message(commit));
311 else
312 cl_assert(git__prefixcmp(
313 git_commit_message(commit), "a simple commit which works") == 0);
314
315 git_commit__free(commit);
316 }
317 }
318
319
320 /* query the details on a parsed commit */
321 void test_commit_parse__details0(void) {
322 static const char *commit_ids[] = {
323 "a4a7dce85cf63874e984719f4fdd239f5145052f", /* 0 */
324 "9fd738e8f7967c078dceed8190330fc8648ee56a", /* 1 */
325 "4a202b346bb0fb0db7eff3cffeb3c70babbd2045", /* 2 */
326 "c47800c7266a2be04c571c04d5a6614691ea99bd", /* 3 */
327 "8496071c1b46c854b31185ea97743be6a8774479", /* 4 */
328 "5b5b025afb0b4c913b4c338a42934a3863bf3644", /* 5 */
329 "a65fedf39aefe402d3bb6e24df4d4f5fe4547750", /* 6 */
330 };
331 const size_t commit_count = sizeof(commit_ids) / sizeof(const char *);
332 unsigned int i;
333
334 for (i = 0; i < commit_count; ++i) {
335 git_oid id;
336 git_commit *commit;
337
338 const git_signature *author, *committer;
339 const char *message;
340 git_time_t commit_time;
341 unsigned int parents, p;
342 git_commit *parent = NULL, *old_parent = NULL;
343
344 git_oid_fromstr(&id, commit_ids[i]);
345
346 cl_git_pass(git_commit_lookup(&commit, g_repo, &id));
347
348 message = git_commit_message(commit);
349 author = git_commit_author(commit);
350 committer = git_commit_committer(commit);
351 commit_time = git_commit_time(commit);
352 parents = git_commit_parentcount(commit);
353
354 cl_assert_equal_s("Scott Chacon", author->name);
355 cl_assert_equal_s("schacon@gmail.com", author->email);
356 cl_assert_equal_s("Scott Chacon", committer->name);
357 cl_assert_equal_s("schacon@gmail.com", committer->email);
358 cl_assert(message != NULL);
359 cl_assert(commit_time > 0);
360 cl_assert(parents <= 2);
361 for (p = 0;p < parents;p++) {
362 if (old_parent != NULL)
363 git_commit_free(old_parent);
364
365 old_parent = parent;
366 cl_git_pass(git_commit_parent(&parent, commit, p));
367 cl_assert(parent != NULL);
368 cl_assert(git_commit_author(parent) != NULL); /* is it really a commit? */
369 }
370 git_commit_free(old_parent);
371 git_commit_free(parent);
372
373 cl_git_fail(git_commit_parent(&parent, commit, parents));
374 git_commit_free(commit);
375 }
376 }
377
378 void test_commit_parse__leading_lf(void)
379 {
380 git_commit *commit;
381 const char *buffer =
382 "tree 1810dff58d8a660512d4832e740f692884338ccd\n\
383 parent e90810b8df3e80c413d903f631643c716887138d\n\
384 author Vicent Marti <tanoku@gmail.com> 1273848544 +0200\n\
385 committer Vicent Marti <tanoku@gmail.com> 1273848544 +0200\n\
386 \n\
387 \n\
388 \n\
389 This commit has a few LF at the start of the commit message";
390 const char *message =
391 "This commit has a few LF at the start of the commit message";
392 const char *raw_message =
393 "\n\
394 \n\
395 This commit has a few LF at the start of the commit message";
396 cl_git_pass(parse_commit(&commit, buffer));
397 cl_assert_equal_s(message, git_commit_message(commit));
398 cl_assert_equal_s(raw_message, git_commit_message_raw(commit));
399 git_commit__free(commit);
400 }
401
402 void test_commit_parse__only_lf(void)
403 {
404 git_commit *commit;
405 const char *buffer =
406 "tree 1810dff58d8a660512d4832e740f692884338ccd\n\
407 parent e90810b8df3e80c413d903f631643c716887138d\n\
408 author Vicent Marti <tanoku@gmail.com> 1273848544 +0200\n\
409 committer Vicent Marti <tanoku@gmail.com> 1273848544 +0200\n\
410 \n\
411 \n\
412 \n";
413 const char *message = "";
414 const char *raw_message = "\n\n";
415
416 cl_git_pass(parse_commit(&commit, buffer));
417 cl_assert_equal_s(message, git_commit_message(commit));
418 cl_assert_equal_s(raw_message, git_commit_message_raw(commit));
419 git_commit__free(commit);
420 }
421
422 void test_commit_parse__arbitrary_field(void)
423 {
424 git_commit *commit;
425 git_buf buf = GIT_BUF_INIT;
426 const char *gpgsig = "-----BEGIN PGP SIGNATURE-----\n\
427 Version: GnuPG v1.4.12 (Darwin)\n\
428 \n\
429 iQIcBAABAgAGBQJQ+FMIAAoJEH+LfPdZDSs1e3EQAJMjhqjWF+WkGLHju7pTw2al\n\
430 o6IoMAhv0Z/LHlWhzBd9e7JeCnanRt12bAU7yvYp9+Z+z+dbwqLwDoFp8LVuigl8\n\
431 JGLcnwiUW3rSvhjdCp9irdb4+bhKUnKUzSdsR2CK4/hC0N2i/HOvMYX+BRsvqweq\n\
432 AsAkA6dAWh+gAfedrBUkCTGhlNYoetjdakWqlGL1TiKAefEZrtA1TpPkGn92vbLq\n\
433 SphFRUY9hVn1ZBWrT3hEpvAIcZag3rTOiRVT1X1flj8B2vGCEr3RrcwOIZikpdaW\n\
434 who/X3xh/DGbI2RbuxmmJpxxP/8dsVchRJJzBwG+yhwU/iN3MlV2c5D69tls/Dok\n\
435 6VbyU4lm/ae0y3yR83D9dUlkycOnmmlBAHKIZ9qUts9X7mWJf0+yy2QxJVpjaTGG\n\
436 cmnQKKPeNIhGJk2ENnnnzjEve7L7YJQF6itbx5VCOcsGh3Ocb3YR7DMdWjt7f8pu\n\
437 c6j+q1rP7EpE2afUN/geSlp5i3x8aXZPDj67jImbVCE/Q1X9voCtyzGJH7MXR0N9\n\
438 ZpRF8yzveRfMH8bwAJjSOGAFF5XkcR/RNY95o+J+QcgBLdX48h+ZdNmUf6jqlu3J\n\
439 7KmTXXQcOVpN6dD3CmRFsbjq+x6RHwa8u1iGn+oIkX908r97ckfB/kHKH7ZdXIJc\n\
440 cpxtDQQMGYFpXK/71stq\n\
441 =ozeK\n\
442 -----END PGP SIGNATURE-----";
443
444 cl_git_pass(parse_commit(&commit, passing_commit_cases[4]));
445
446 cl_git_pass(git_commit_header_field(&buf, commit, "tree"));
447 cl_assert_equal_s("6b79e22d69bf46e289df0345a14ca059dfc9bdf6", buf.ptr);
448 git_buf_dispose(&buf);
449
450 cl_git_pass(git_commit_header_field(&buf, commit, "parent"));
451 cl_assert_equal_s("34734e478d6cf50c27c9d69026d93974d052c454", buf.ptr);
452 git_buf_dispose(&buf);
453
454 cl_git_pass(git_commit_header_field(&buf, commit, "gpgsig"));
455 cl_assert_equal_s(gpgsig, buf.ptr);
456 git_buf_dispose(&buf);
457
458 cl_git_fail_with(GIT_ENOTFOUND, git_commit_header_field(&buf, commit, "awesomeness"));
459 cl_git_fail_with(GIT_ENOTFOUND, git_commit_header_field(&buf, commit, "par"));
460
461 git_commit__free(commit);
462 cl_git_pass(parse_commit(&commit, passing_commit_cases[0]));
463
464 cl_git_pass(git_commit_header_field(&buf, commit, "committer"));
465 cl_assert_equal_s("Vicent Marti <tanoku@gmail.com> 1273848544 +0200", buf.ptr);
466
467 git_buf_dispose(&buf);
468 git_commit__free(commit);
469 }
470
471 void test_commit_parse__extract_signature(void)
472 {
473 git_odb *odb;
474 git_oid commit_id;
475 git_buf signature = GIT_BUF_INIT, signed_data = GIT_BUF_INIT;
476 const char *gpgsig = "-----BEGIN PGP SIGNATURE-----\n\
477 Version: GnuPG v1.4.12 (Darwin)\n\
478 \n\
479 iQIcBAABAgAGBQJQ+FMIAAoJEH+LfPdZDSs1e3EQAJMjhqjWF+WkGLHju7pTw2al\n\
480 o6IoMAhv0Z/LHlWhzBd9e7JeCnanRt12bAU7yvYp9+Z+z+dbwqLwDoFp8LVuigl8\n\
481 JGLcnwiUW3rSvhjdCp9irdb4+bhKUnKUzSdsR2CK4/hC0N2i/HOvMYX+BRsvqweq\n\
482 AsAkA6dAWh+gAfedrBUkCTGhlNYoetjdakWqlGL1TiKAefEZrtA1TpPkGn92vbLq\n\
483 SphFRUY9hVn1ZBWrT3hEpvAIcZag3rTOiRVT1X1flj8B2vGCEr3RrcwOIZikpdaW\n\
484 who/X3xh/DGbI2RbuxmmJpxxP/8dsVchRJJzBwG+yhwU/iN3MlV2c5D69tls/Dok\n\
485 6VbyU4lm/ae0y3yR83D9dUlkycOnmmlBAHKIZ9qUts9X7mWJf0+yy2QxJVpjaTGG\n\
486 cmnQKKPeNIhGJk2ENnnnzjEve7L7YJQF6itbx5VCOcsGh3Ocb3YR7DMdWjt7f8pu\n\
487 c6j+q1rP7EpE2afUN/geSlp5i3x8aXZPDj67jImbVCE/Q1X9voCtyzGJH7MXR0N9\n\
488 ZpRF8yzveRfMH8bwAJjSOGAFF5XkcR/RNY95o+J+QcgBLdX48h+ZdNmUf6jqlu3J\n\
489 7KmTXXQcOVpN6dD3CmRFsbjq+x6RHwa8u1iGn+oIkX908r97ckfB/kHKH7ZdXIJc\n\
490 cpxtDQQMGYFpXK/71stq\n\
491 =ozeK\n\
492 -----END PGP SIGNATURE-----";
493
494 const char *data = "tree 6b79e22d69bf46e289df0345a14ca059dfc9bdf6\n\
495 parent 34734e478d6cf50c27c9d69026d93974d052c454\n\
496 author Ben Burkert <ben@benburkert.com> 1358451456 -0800\n\
497 committer Ben Burkert <ben@benburkert.com> 1358451456 -0800\n\
498 \n\
499 a simple commit which works\n";
500
501 const char *oneline_signature = "tree 51832e6397b30309c8bcad9c55fa6ae67778f378\n\
502 parent a1b6decaaac768b5e01e1b5dbf5b2cc081bed1eb\n\
503 author Some User <someuser@gmail.com> 1454537944 -0700\n\
504 committer Some User <someuser@gmail.com> 1454537944 -0700\n\
505 gpgsig bad\n\
506 \n\
507 corrupt signature\n";
508
509 const char *oneline_data = "tree 51832e6397b30309c8bcad9c55fa6ae67778f378\n\
510 parent a1b6decaaac768b5e01e1b5dbf5b2cc081bed1eb\n\
511 author Some User <someuser@gmail.com> 1454537944 -0700\n\
512 committer Some User <someuser@gmail.com> 1454537944 -0700\n\
513 \n\
514 corrupt signature\n";
515
516 cl_git_pass(git_repository_odb__weakptr(&odb, g_repo));
517 cl_git_pass(git_odb_write(&commit_id, odb, passing_commit_cases[4], strlen(passing_commit_cases[4]), GIT_OBJECT_COMMIT));
518
519 cl_git_pass(git_commit_extract_signature(&signature, &signed_data, g_repo, &commit_id, NULL));
520 cl_assert_equal_s(gpgsig, signature.ptr);
521 cl_assert_equal_s(data, signed_data.ptr);
522
523 git_buf_dispose(&signature);
524 git_buf_dispose(&signed_data);
525
526 cl_git_pass(git_commit_extract_signature(&signature, &signed_data, g_repo, &commit_id, "gpgsig"));
527 cl_assert_equal_s(gpgsig, signature.ptr);
528 cl_assert_equal_s(data, signed_data.ptr);
529
530 git_buf_dispose(&signature);
531 git_buf_dispose(&signed_data);
532
533 /* Try to parse a tree */
534 cl_git_pass(git_oid_fromstr(&commit_id, "45dd856fdd4d89b884c340ba0e047752d9b085d6"));
535 cl_git_fail_with(GIT_ENOTFOUND, git_commit_extract_signature(&signature, &signed_data, g_repo, &commit_id, NULL));
536 cl_assert_equal_i(GIT_ERROR_INVALID, git_error_last()->klass);
537
538 /* Try to parse an unsigned commit */
539 cl_git_pass(git_odb_write(&commit_id, odb, passing_commit_cases[1], strlen(passing_commit_cases[1]), GIT_OBJECT_COMMIT));
540 cl_git_fail_with(GIT_ENOTFOUND, git_commit_extract_signature(&signature, &signed_data, g_repo, &commit_id, NULL));
541 cl_assert_equal_i(GIT_ERROR_OBJECT, git_error_last()->klass);
542
543 /* Parse the commit with a single-line signature */
544 cl_git_pass(git_odb_write(&commit_id, odb, oneline_signature, strlen(oneline_signature), GIT_OBJECT_COMMIT));
545 cl_git_pass(git_commit_extract_signature(&signature, &signed_data, g_repo, &commit_id, NULL));
546 cl_assert_equal_s("bad", signature.ptr);
547 cl_assert_equal_s(oneline_data, signed_data.ptr);
548
549 git_buf_dispose(&signature);
550 git_buf_dispose(&signed_data);
551 }