Add unit tests for Commit parsing
[libgit2.git] / tests / t0401-parse.c
blob5b209c9654c83719b07d9fc1d2287ca361663fe1
1 #include "test_lib.h"
2 #include "test_helpers.h"
3 #include "commit.h"
4 #include <git/odb.h>
5 #include <git/commit.h>
7 static char *test_commits_broken[] = {
9 // empty commit
10 "",
12 // random garbage
13 "asd97sa9du902e9a0jdsuusad09as9du098709aweu8987sd\n",
15 // broken endlines 1
16 "tree f6c0dad3c7b3481caa9d73db21f91964894a945b\r\n\
17 parent 05452d6349abcd67aa396dfb28660d765d8b2a36\r\n\
18 author Vicent Marti <tanoku@gmail.com> 1273848544 +0200\r\n\
19 committer Vicent Marti <tanoku@gmail.com> 1273848544 +0200\r\n\
20 \r\n\
21 a test commit with broken endlines\r\n",
23 // broken endlines 2
24 "tree f6c0dad3c7b3481caa9d73db21f91964894a945b\
25 parent 05452d6349abcd67aa396dfb28660d765d8b2a36\
26 author Vicent Marti <tanoku@gmail.com> 1273848544 +0200\
27 committer Vicent Marti <tanoku@gmail.com> 1273848544 +0200\
29 another test commit with broken endlines",
31 // starting endlines
32 "\ntree f6c0dad3c7b3481caa9d73db21f91964894a945b\n\
33 parent 05452d6349abcd67aa396dfb28660d765d8b2a36\n\
34 author Vicent Marti <tanoku@gmail.com> 1273848544 +0200\n\
35 committer Vicent Marti <tanoku@gmail.com> 1273848544 +0200\n\
36 \n\
37 a test commit with a starting endline\n",
39 // corrupted commit 1
40 "tree f6c0dad3c7b3481caa9d73db21f91964894a945b\n\
41 parent 05452d6349abcd67aa396df",
43 // corrupted commit 2
44 "tree f6c0dad3c7b3481caa9d73db21f91964894a945b\n\
45 parent ",
47 // corrupted commit 3
48 "tree f6c0dad3c7b3481caa9d73db21f91964894a945b\n\
49 parent ",
51 // corrupted commit 4
52 "tree f6c0dad3c7b3481caa9d73db21f91964894a945b\n\
53 par",
55 // FIXME: duplicated parents?
56 // It this supposed to pass?
58 "tree f6c0dad3c7b3481caa9d73db21f91964894a945b\n\
59 parent 05452d6349abcd67aa396dfb28660d765d8b2a36\n\
60 parent 05452d6349abcd67aa396dfb28660d765d8b2a36\n\
61 author Vicent Marti <tanoku@gmail.com> 1273848544 +0200\n\
62 committer Vicent Marti <tanoku@gmail.com> 1273848544 +0200\n\
63 \n\
64 duplicated parent",
69 static char *test_commits_working[] = {
70 // simple commit with no message
71 "tree f6c0dad3c7b3481caa9d73db21f91964894a945b\n\
72 author Vicent Marti <tanoku@gmail.com> 1273848544 +0200\n\
73 committer Vicent Marti <tanoku@gmail.com> 1273848544 +0200\n\
74 \n",
76 // simple commit, no parent
77 "tree f6c0dad3c7b3481caa9d73db21f91964894a945b\n\
78 author Vicent Marti <tanoku@gmail.com> 1273848544 +0200\n\
79 committer Vicent Marti <tanoku@gmail.com> 1273848544 +0200\n\
80 \n\
81 a simple commit which works\n",
83 // simple commit, 1 parents
84 "tree f6c0dad3c7b3481caa9d73db21f91964894a945b\n\
85 parent 05452d6349abcd67aa396dfb28660d765d8b2a36\n\
86 author Vicent Marti <tanoku@gmail.com> 1273848544 +0200\n\
87 committer Vicent Marti <tanoku@gmail.com> 1273848544 +0200\n\
88 \n\
89 a simple commit which works\n",
92 BEGIN_TEST(parse_oid_test)
94 git_oid oid;
96 #define TEST_OID_PASS(string, header){ \
97 char *ptr = string;\
98 char *ptr_original = ptr;\
99 size_t len = strlen(ptr);\
100 must_pass(git_commit__parse_oid(&oid, &ptr, ptr + len, header));\
101 must_be_true(ptr == ptr_original + len);\
104 #define TEST_OID_FAIL(string, header){ \
105 char *ptr = string;\
106 size_t len = strlen(ptr);\
107 must_fail(git_commit__parse_oid(&oid, &ptr, ptr + len, header));\
110 TEST_OID_PASS("parent 05452d6349abcd67aa396dfb28660d765d8b2a36\n", "parent ");
111 TEST_OID_PASS("tree 05452d6349abcd67aa396dfb28660d765d8b2a36\n", "tree ");
112 TEST_OID_PASS("random_heading 05452d6349abcd67aa396dfb28660d765d8b2a36\n", "random_heading ");
113 TEST_OID_PASS("stuck_heading05452d6349abcd67aa396dfb28660d765d8b2a36\n", "stuck_heading");
114 TEST_OID_PASS("tree 5F4BEFFC0759261D015AA63A3A85613FF2F235DE\n", "tree ");
115 TEST_OID_PASS("tree 1A669B8AB81B5EB7D9DB69562D34952A38A9B504\n", "tree ");
116 TEST_OID_PASS("tree 5B20DCC6110FCC75D31C6CEDEBD7F43ECA65B503\n", "tree ");
117 TEST_OID_PASS("tree 173E7BF00EA5C33447E99E6C1255954A13026BE4\n", "tree ");
119 TEST_OID_FAIL("parent 05452d6349abcd67aa396dfb28660d765d8b2a36", "parent ");
120 TEST_OID_FAIL("05452d6349abcd67aa396dfb28660d765d8b2a36\n", "tree ");
121 TEST_OID_FAIL("parent05452d6349abcd67aa396dfb28660d765d8b2a6a\n", "parent ");
122 TEST_OID_FAIL("parent 05452d6349abcd67aa396dfb280d765d8b2a6\n", "parent ");
123 TEST_OID_FAIL("tree 05452d6349abcd67aa396dfb28660d765d8b2a36\n", "tree ");
124 TEST_OID_FAIL("parent 0545xd6349abcd67aa396dfb28660d765d8b2a36\n", "parent ");
125 TEST_OID_FAIL("parent 0545xd6349abcd67aa396dfb28660d765d8b2a36FF\n", "parent ");
126 TEST_OID_FAIL("", "tree ");
127 TEST_OID_FAIL("", "");
129 #undef TEST_OID_PASS
130 #undef TEST_OID_FAIL
132 END_TEST
134 BEGIN_TEST(parse_buffer_test)
135 const int broken_commit_count = sizeof(test_commits_broken) / sizeof(*test_commits_broken);
136 const int working_commit_count = sizeof(test_commits_working) / sizeof(*test_commits_working);
137 int i;
139 for (i = 0; i < broken_commit_count; ++i) {
140 git_commit commit;
141 commit.parsed = 0;
142 commit.pool = NULL;
144 must_fail(git_commit__parse_buffer(
145 &commit,
146 test_commits_broken[i],
147 strlen(test_commits_broken[i]))
151 for (i = 0; i < working_commit_count; ++i) {
152 git_commit commit;
153 commit.parsed = 0;
154 commit.pool = NULL;
156 must_pass(git_commit__parse_buffer(
157 &commit,
158 test_commits_working[i],
159 strlen(test_commits_working[i]))
163 END_TEST