]> git.proxmox.com Git - libgit2.git/blame - tests/t0401-parse.c
Refactor parsing methods
[libgit2.git] / tests / t0401-parse.c
CommitLineData
42281e00
VM
1#include "test_lib.h"
2#include "test_helpers.h"
3#include "commit.h"
4#include <git/odb.h>
5#include <git/commit.h>
d047b47a 6#include <git/revwalk.h>
42281e00
VM
7
8static char *test_commits_broken[] = {
9
52f2390b 10/* empty commit */
42281e00
VM
11"",
12
52f2390b 13/* random garbage */
42281e00
VM
14"asd97sa9du902e9a0jdsuusad09as9du098709aweu8987sd\n",
15
52f2390b 16/* broken endlines 1 */
42281e00
VM
17"tree f6c0dad3c7b3481caa9d73db21f91964894a945b\r\n\
18parent 05452d6349abcd67aa396dfb28660d765d8b2a36\r\n\
19author Vicent Marti <tanoku@gmail.com> 1273848544 +0200\r\n\
20committer Vicent Marti <tanoku@gmail.com> 1273848544 +0200\r\n\
21\r\n\
22a test commit with broken endlines\r\n",
23
52f2390b 24/* broken endlines 2 */
42281e00
VM
25"tree f6c0dad3c7b3481caa9d73db21f91964894a945b\
26parent 05452d6349abcd67aa396dfb28660d765d8b2a36\
27author Vicent Marti <tanoku@gmail.com> 1273848544 +0200\
28committer Vicent Marti <tanoku@gmail.com> 1273848544 +0200\
29\
30another test commit with broken endlines",
31
52f2390b 32/* starting endlines */
42281e00
VM
33"\ntree f6c0dad3c7b3481caa9d73db21f91964894a945b\n\
34parent 05452d6349abcd67aa396dfb28660d765d8b2a36\n\
35author Vicent Marti <tanoku@gmail.com> 1273848544 +0200\n\
36committer Vicent Marti <tanoku@gmail.com> 1273848544 +0200\n\
37\n\
38a test commit with a starting endline\n",
39
52f2390b 40/* corrupted commit 1 */
42281e00
VM
41"tree f6c0dad3c7b3481caa9d73db21f91964894a945b\n\
42parent 05452d6349abcd67aa396df",
43
52f2390b 44/* corrupted commit 2 */
42281e00
VM
45"tree f6c0dad3c7b3481caa9d73db21f91964894a945b\n\
46parent ",
47
52f2390b 48/* corrupted commit 3 */
42281e00
VM
49"tree f6c0dad3c7b3481caa9d73db21f91964894a945b\n\
50parent ",
51
52f2390b 52/* corrupted commit 4 */
42281e00
VM
53"tree f6c0dad3c7b3481caa9d73db21f91964894a945b\n\
54par",
55
42281e00
VM
56};
57
58
59static char *test_commits_working[] = {
52f2390b 60/* simple commit with no message */
42281e00
VM
61"tree f6c0dad3c7b3481caa9d73db21f91964894a945b\n\
62author Vicent Marti <tanoku@gmail.com> 1273848544 +0200\n\
63committer Vicent Marti <tanoku@gmail.com> 1273848544 +0200\n\
64\n",
65
52f2390b 66/* simple commit, no parent */
42281e00
VM
67"tree f6c0dad3c7b3481caa9d73db21f91964894a945b\n\
68author Vicent Marti <tanoku@gmail.com> 1273848544 +0200\n\
69committer Vicent Marti <tanoku@gmail.com> 1273848544 +0200\n\
70\n\
71a simple commit which works\n",
72
52f2390b 73/* simple commit, 1 parents */
42281e00
VM
74"tree f6c0dad3c7b3481caa9d73db21f91964894a945b\n\
75parent 05452d6349abcd67aa396dfb28660d765d8b2a36\n\
76author Vicent Marti <tanoku@gmail.com> 1273848544 +0200\n\
77committer Vicent Marti <tanoku@gmail.com> 1273848544 +0200\n\
78\n\
79a simple commit which works\n",
80};
81
82BEGIN_TEST(parse_oid_test)
83
52f2390b 84 git_oid oid;
42281e00 85
b2bc567f 86#define TEST_OID_PASS(string, header) { \
52f2390b
VM
87 char *ptr = string;\
88 char *ptr_original = ptr;\
89 size_t len = strlen(ptr);\
364788e1 90 must_pass(git__parse_oid(&oid, &ptr, ptr + len, header));\
52f2390b 91 must_be_true(ptr == ptr_original + len);\
42281e00
VM
92}
93
b2bc567f 94#define TEST_OID_FAIL(string, header) { \
52f2390b
VM
95 char *ptr = string;\
96 size_t len = strlen(ptr);\
364788e1 97 must_fail(git__parse_oid(&oid, &ptr, ptr + len, header));\
42281e00
VM
98}
99
52f2390b
VM
100 TEST_OID_PASS("parent 05452d6349abcd67aa396dfb28660d765d8b2a36\n", "parent ");
101 TEST_OID_PASS("tree 05452d6349abcd67aa396dfb28660d765d8b2a36\n", "tree ");
102 TEST_OID_PASS("random_heading 05452d6349abcd67aa396dfb28660d765d8b2a36\n", "random_heading ");
103 TEST_OID_PASS("stuck_heading05452d6349abcd67aa396dfb28660d765d8b2a36\n", "stuck_heading");
104 TEST_OID_PASS("tree 5F4BEFFC0759261D015AA63A3A85613FF2F235DE\n", "tree ");
105 TEST_OID_PASS("tree 1A669B8AB81B5EB7D9DB69562D34952A38A9B504\n", "tree ");
106 TEST_OID_PASS("tree 5B20DCC6110FCC75D31C6CEDEBD7F43ECA65B503\n", "tree ");
107 TEST_OID_PASS("tree 173E7BF00EA5C33447E99E6C1255954A13026BE4\n", "tree ");
108
109 TEST_OID_FAIL("parent 05452d6349abcd67aa396dfb28660d765d8b2a36", "parent ");
110 TEST_OID_FAIL("05452d6349abcd67aa396dfb28660d765d8b2a36\n", "tree ");
111 TEST_OID_FAIL("parent05452d6349abcd67aa396dfb28660d765d8b2a6a\n", "parent ");
112 TEST_OID_FAIL("parent 05452d6349abcd67aa396dfb280d765d8b2a6\n", "parent ");
113 TEST_OID_FAIL("tree 05452d6349abcd67aa396dfb28660d765d8b2a36\n", "tree ");
114 TEST_OID_FAIL("parent 0545xd6349abcd67aa396dfb28660d765d8b2a36\n", "parent ");
115 TEST_OID_FAIL("parent 0545xd6349abcd67aa396dfb28660d765d8b2a36FF\n", "parent ");
116 TEST_OID_FAIL("", "tree ");
117 TEST_OID_FAIL("", "");
42281e00
VM
118
119#undef TEST_OID_PASS
120#undef TEST_OID_FAIL
121
122END_TEST
123
52f2390b
VM
124BEGIN_TEST(parse_person_test)
125
126#define TEST_PERSON_PASS(_string, _header, _name, _email, _time) { \
127 char *ptr = _string; \
128 size_t len = strlen(_string);\
364788e1
VM
129 git_person person; \
130 must_pass(git__parse_person(&person, &ptr, ptr + len, _header));\
52f2390b
VM
131 must_be_true(strncmp(_name, person.name, 63) == 0);\
132 must_be_true(strncmp(_email, person.email, 63) == 0);\
133 must_be_true(_time == person.time);\
134}
135
136#define TEST_PERSON_FAIL(_string, _header) { \
137 char *ptr = _string; \
138 size_t len = strlen(_string);\
364788e1
VM
139 git_person person; \
140 must_fail(git__parse_person(&person, &ptr, ptr + len, _header));\
52f2390b
VM
141}
142
143 TEST_PERSON_PASS(
144 "author Vicent Marti <tanoku@gmail.com> 12345 \n",
145 "author ",
146 "Vicent Marti",
147 "tanoku@gmail.com",
148 12345);
149
150 TEST_PERSON_PASS(
151 "author Vicent Marti <> 12345 \n",
152 "author ",
153 "Vicent Marti",
154 "",
155 12345);
156
157 TEST_PERSON_PASS(
158 "author Vicent Marti <tanoku@gmail.com> 231301 +2020\n",
159 "author ",
160 "Vicent Marti",
161 "tanoku@gmail.com",
162 231301);
163
164 TEST_PERSON_PASS(
165 "author Vicent Marti with an outrageously long name \
166 which will probably overflow the buffer <tanoku@gmail.com> 12345 \n",
167 "author ",
168 "Vicent Marti with an outrageously long name \
169 which will probably overflow the buffer",
170 "tanoku@gmail.com",
171 12345);
172
173 TEST_PERSON_PASS(
174 "author Vicent Marti <tanokuwithaveryveryverylongemail\
175 whichwillprobablyvoverflowtheemailbuffer@gmail.com> 12345 \n",
176 "author ",
177 "Vicent Marti",
178 "tanokuwithaveryveryverylongemail\
179 whichwillprobablyvoverflowtheemailbuffer@gmail.com",
180 12345);
181
182 TEST_PERSON_FAIL(
183 "author Vicent Marti <tanoku@gmail.com> 12345 \n",
184 "author ");
185
186 TEST_PERSON_FAIL(
187 "author Vicent Marti <tanoku@gmail.com> 12345 \n",
188 "committer ");
189
190 TEST_PERSON_FAIL(
191 "author Vicent Marti 12345 \n",
192 "author ");
193
194 TEST_PERSON_FAIL(
195 "author Vicent Marti <broken@email 12345 \n",
196 "author ");
197
198 TEST_PERSON_FAIL(
199 "author Vicent Marti <tanoku@gmail.com> notime \n",
200 "author ");
201
202 TEST_PERSON_FAIL(
203 "author Vicent Marti <tanoku@gmail.com>\n",
204 "author ");
205
206 TEST_PERSON_FAIL(
207 "author ",
208 "author ");
209
210#undef TEST_PERSON_PASS
211#undef TESt_PERSON_FAIL
212
213END_TEST
214
42281e00 215BEGIN_TEST(parse_buffer_test)
52f2390b
VM
216 const int broken_commit_count = sizeof(test_commits_broken) / sizeof(*test_commits_broken);
217 const int working_commit_count = sizeof(test_commits_working) / sizeof(*test_commits_working);
218
219 const unsigned int default_flags = 0xFF; /* parse everything */
220
221 int i;
42281e00 222
52f2390b 223 git_revpool *pool = gitrp_alloc(NULL);
d047b47a 224
52f2390b
VM
225 for (i = 0; i < broken_commit_count; ++i) {
226 git_commit *commit;
227 commit = git__malloc(sizeof(git_commit));
228 memset(commit, 0x0, sizeof(git_commit));
229 commit->object.pool = pool;
42281e00 230
52f2390b
VM
231 must_fail(git_commit__parse_buffer(
232 commit,
233 test_commits_broken[i],
234 strlen(test_commits_broken[i]),
235 default_flags)
236 );
088a731f 237
52f2390b
VM
238 git_commit__free(commit);
239 }
42281e00 240
52f2390b
VM
241 for (i = 0; i < working_commit_count; ++i) {
242 git_commit *commit;
243 commit = git__malloc(sizeof(git_commit));
244 memset(commit, 0x0, sizeof(git_commit));
245 commit->object.pool = pool;
42281e00 246
52f2390b
VM
247 must_pass(git_commit__parse_buffer(
248 commit,
249 test_commits_working[i],
250 strlen(test_commits_working[i]),
251 default_flags)
252 );
088a731f 253
52f2390b
VM
254 git_commit__free(commit);
255 }
42281e00 256
52f2390b 257 gitrp_free(pool);
d047b47a 258
42281e00 259END_TEST