]> git.proxmox.com Git - libgit2.git/blob - tests/t0401-parse.c
Redesigned the walking/object lookup interface
[libgit2.git] / tests / t0401-parse.c
1 #include "test_lib.h"
2 #include "test_helpers.h"
3 #include "commit.h"
4 #include <git/odb.h>
5 #include <git/commit.h>
6 #include <git/revwalk.h>
7
8 static char *test_commits_broken[] = {
9
10 /* empty commit */
11 "",
12
13 /* random garbage */
14 "asd97sa9du902e9a0jdsuusad09as9du098709aweu8987sd\n",
15
16 /* broken endlines 1 */
17 "tree f6c0dad3c7b3481caa9d73db21f91964894a945b\r\n\
18 parent 05452d6349abcd67aa396dfb28660d765d8b2a36\r\n\
19 author Vicent Marti <tanoku@gmail.com> 1273848544 +0200\r\n\
20 committer Vicent Marti <tanoku@gmail.com> 1273848544 +0200\r\n\
21 \r\n\
22 a test commit with broken endlines\r\n",
23
24 /* broken endlines 2 */
25 "tree f6c0dad3c7b3481caa9d73db21f91964894a945b\
26 parent 05452d6349abcd67aa396dfb28660d765d8b2a36\
27 author Vicent Marti <tanoku@gmail.com> 1273848544 +0200\
28 committer Vicent Marti <tanoku@gmail.com> 1273848544 +0200\
29 \
30 another test commit with broken endlines",
31
32 /* starting endlines */
33 "\ntree f6c0dad3c7b3481caa9d73db21f91964894a945b\n\
34 parent 05452d6349abcd67aa396dfb28660d765d8b2a36\n\
35 author Vicent Marti <tanoku@gmail.com> 1273848544 +0200\n\
36 committer Vicent Marti <tanoku@gmail.com> 1273848544 +0200\n\
37 \n\
38 a test commit with a starting endline\n",
39
40 /* corrupted commit 1 */
41 "tree f6c0dad3c7b3481caa9d73db21f91964894a945b\n\
42 parent 05452d6349abcd67aa396df",
43
44 /* corrupted commit 2 */
45 "tree f6c0dad3c7b3481caa9d73db21f91964894a945b\n\
46 parent ",
47
48 /* corrupted commit 3 */
49 "tree f6c0dad3c7b3481caa9d73db21f91964894a945b\n\
50 parent ",
51
52 /* corrupted commit 4 */
53 "tree f6c0dad3c7b3481caa9d73db21f91964894a945b\n\
54 par",
55
56 };
57
58
59 static char *test_commits_working[] = {
60 /* simple commit with no message */
61 "tree f6c0dad3c7b3481caa9d73db21f91964894a945b\n\
62 author Vicent Marti <tanoku@gmail.com> 1273848544 +0200\n\
63 committer Vicent Marti <tanoku@gmail.com> 1273848544 +0200\n\
64 \n",
65
66 /* simple commit, no parent */
67 "tree f6c0dad3c7b3481caa9d73db21f91964894a945b\n\
68 author Vicent Marti <tanoku@gmail.com> 1273848544 +0200\n\
69 committer Vicent Marti <tanoku@gmail.com> 1273848544 +0200\n\
70 \n\
71 a simple commit which works\n",
72
73 /* simple commit, 1 parents */
74 "tree f6c0dad3c7b3481caa9d73db21f91964894a945b\n\
75 parent a4a7dce85cf63874e984719f4fdd239f5145052f\n\
76 author Vicent Marti <tanoku@gmail.com> 1273848544 +0200\n\
77 committer Vicent Marti <tanoku@gmail.com> 1273848544 +0200\n\
78 \n\
79 a simple commit which works\n",
80 };
81
82 BEGIN_TEST(parse_oid_test)
83
84 git_oid oid;
85
86 #define TEST_OID_PASS(string, header) { \
87 char *ptr = string;\
88 char *ptr_original = ptr;\
89 size_t len = strlen(ptr);\
90 must_pass(git__parse_oid(&oid, &ptr, ptr + len, header));\
91 must_be_true(ptr == ptr_original + len);\
92 }
93
94 #define TEST_OID_FAIL(string, header) { \
95 char *ptr = string;\
96 size_t len = strlen(ptr);\
97 must_fail(git__parse_oid(&oid, &ptr, ptr + len, header));\
98 }
99
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("", "");
118
119 #undef TEST_OID_PASS
120 #undef TEST_OID_FAIL
121
122 END_TEST
123
124 BEGIN_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);\
129 git_person person; \
130 must_pass(git__parse_person(&person, &ptr, ptr + len, _header));\
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);\
139 git_person person; \
140 must_fail(git__parse_person(&person, &ptr, ptr + len, _header));\
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
213 END_TEST
214
215 BEGIN_TEST(parse_buffer_test)
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 =
220 GIT_COMMIT_AUTHOR |
221 GIT_COMMIT_COMMITTER |
222 GIT_COMMIT_TIME |
223 GIT_COMMIT_MESSAGE |
224 GIT_COMMIT_MESSAGE_SHORT; /* parse everything */
225
226 int i;
227 git_repository *repo = git_repository_alloc(NULL);
228
229 must_be_true(repo != NULL);
230
231 for (i = 0; i < broken_commit_count; ++i) {
232 git_commit *commit;
233 commit = git__malloc(sizeof(git_commit));
234 memset(commit, 0x0, sizeof(git_commit));
235 commit->object.repo = repo;
236
237 must_fail(git_commit__parse_buffer(
238 commit,
239 test_commits_broken[i],
240 strlen(test_commits_broken[i]),
241 default_flags)
242 );
243
244 git_commit__free(commit);
245 }
246
247 for (i = 0; i < working_commit_count; ++i) {
248 git_commit *commit;
249 commit = git__malloc(sizeof(git_commit));
250 memset(commit, 0x0, sizeof(git_commit));
251 commit->object.repo = repo;
252
253 must_pass(git_commit__parse_buffer(
254 commit,
255 test_commits_working[i],
256 strlen(test_commits_working[i]),
257 default_flags)
258 );
259
260 git_commit__free(commit);
261 }
262
263 git_repository_free(repo);
264
265 END_TEST