]>
git.proxmox.com Git - libgit2.git/blob - tests/libgit2/object/commit/parse.c
1 #include "clar_libgit2.h"
6 static void assert_commit_parses(const char *data
, size_t datalen
,
7 const char *expected_treeid
,
8 const char *expected_author
,
9 const char *expected_committer
,
10 const char *expected_encoding
,
11 const char *expected_message
,
12 size_t expected_parents
)
16 datalen
= strlen(data
);
17 cl_git_pass(git_object__from_raw((git_object
**) &commit
, data
, datalen
, GIT_OBJECT_COMMIT
));
19 if (expected_author
) {
20 git_signature
*author
;
21 cl_git_pass(git_signature_from_buffer(&author
, expected_author
));
22 cl_assert(git_signature__equal(author
, commit
->author
));
23 cl_assert_equal_s(author
->name
, commit
->author
->name
);
24 cl_assert_equal_s(author
->email
, commit
->author
->email
);
25 cl_assert_equal_i(author
->when
.time
, commit
->author
->when
.time
);
26 cl_assert_equal_i(author
->when
.offset
, commit
->author
->when
.offset
);
27 cl_assert_equal_i(author
->when
.sign
, commit
->author
->when
.sign
);
28 git_signature_free(author
);
31 if (expected_committer
) {
32 git_signature
*committer
;
33 cl_git_pass(git_signature_from_buffer(&committer
, expected_committer
));
34 cl_assert_equal_s(committer
->name
, commit
->committer
->name
);
35 cl_assert_equal_s(committer
->email
, commit
->committer
->email
);
36 cl_assert_equal_i(committer
->when
.time
, commit
->committer
->when
.time
);
37 cl_assert_equal_i(committer
->when
.offset
, commit
->committer
->when
.offset
);
38 cl_assert_equal_i(committer
->when
.sign
, commit
->committer
->when
.sign
);
39 git_signature_free(committer
);
42 if (expected_encoding
)
43 cl_assert_equal_s(commit
->message_encoding
, expected_encoding
);
45 cl_assert_equal_p(commit
->message_encoding
, NULL
);
48 cl_assert_equal_s(commit
->raw_message
, expected_message
);
50 cl_assert_equal_p(commit
->message_encoding
, NULL
);
52 if (expected_treeid
) {
54 cl_git_pass(git_oid_fromstr(&tree_oid
, expected_treeid
));
55 cl_assert_equal_oid(&tree_oid
, &commit
->tree_id
);
58 cl_assert_equal_i(commit
->parent_ids
.size
, expected_parents
);
60 git_object__free(&commit
->object
);
63 static void assert_commit_fails(const char *data
, size_t datalen
)
67 datalen
= strlen(data
);
68 cl_git_fail(git_object__from_raw(&object
, data
, datalen
, GIT_OBJECT_COMMIT
));
71 void test_object_commit_parse__parsing_commit_succeeds(void)
74 "tree 3e7ac388cadacccdf1c6c5f3445895b71d9cb0f8\n"
75 "author Author <author@example.com>\n"
76 "committer Committer <committer@example.com>\n"
80 assert_commit_parses(commit
, 0,
81 "3e7ac388cadacccdf1c6c5f3445895b71d9cb0f8",
82 "Author <author@example.com>",
83 "Committer <committer@example.com>",
88 void test_object_commit_parse__parsing_commit_without_encoding_succeeds(void)
91 "tree 3e7ac388cadacccdf1c6c5f3445895b71d9cb0f8\n"
92 "author Author <author@example.com>\n"
93 "committer Committer <committer@example.com>\n"
96 assert_commit_parses(commit
, 0,
97 "3e7ac388cadacccdf1c6c5f3445895b71d9cb0f8",
98 "Author <author@example.com>",
99 "Committer <committer@example.com>",
104 void test_object_commit_parse__parsing_commit_with_multiple_authors_succeeds(void)
107 "tree 3e7ac388cadacccdf1c6c5f3445895b71d9cb0f8\n"
108 "author Author1 <author@example.com>\n"
109 "author Author2 <author@example.com>\n"
110 "author Author3 <author@example.com>\n"
111 "author Author4 <author@example.com>\n"
112 "committer Committer <committer@example.com>\n"
115 assert_commit_parses(commit
, 0,
116 "3e7ac388cadacccdf1c6c5f3445895b71d9cb0f8",
117 "Author1 <author@example.com>",
118 "Committer <committer@example.com>",
123 void test_object_commit_parse__parsing_commit_with_multiple_committers_succeeds(void)
126 "tree 3e7ac388cadacccdf1c6c5f3445895b71d9cb0f8\n"
127 "author Author <author@example.com>\n"
128 "committer Committer1 <committer@example.com>\n"
129 "committer Committer2 <committer@example.com>\n"
130 "committer Committer3 <committer@example.com>\n"
131 "committer Committer4 <committer@example.com>\n"
134 assert_commit_parses(commit
, 0,
135 "3e7ac388cadacccdf1c6c5f3445895b71d9cb0f8",
136 "Author <author@example.com>",
137 "Committer1 <committer@example.com>",
142 void test_object_commit_parse__parsing_commit_without_message_succeeds(void)
145 "tree 3e7ac388cadacccdf1c6c5f3445895b71d9cb0f8\n"
146 "author Author <author@example.com>\n"
147 "committer Committer <committer@example.com>\n";
148 assert_commit_parses(commit
, 0,
149 "3e7ac388cadacccdf1c6c5f3445895b71d9cb0f8",
150 "Author <author@example.com>",
151 "Committer <committer@example.com>",
156 void test_object_commit_parse__parsing_commit_with_unknown_fields_succeeds(void)
159 "tree 3e7ac388cadacccdf1c6c5f3445895b71d9cb0f8\n"
160 "author Author <author@example.com>\n"
161 "committer Committer <committer@example.com>\n"
166 assert_commit_parses(commit
, 0,
167 "3e7ac388cadacccdf1c6c5f3445895b71d9cb0f8",
168 "Author <author@example.com>",
169 "Committer <committer@example.com>",
174 void test_object_commit_parse__parsing_commit_with_invalid_tree_fails(void)
177 "tree 3e7ac388cadacccdf1xxx5f3445895b71d9cb0f8\n"
178 "author Author <author@example.com>\n"
179 "committer Committer <committer@example.com>\n"
182 assert_commit_fails(commit
, 0);
185 void test_object_commit_parse__parsing_commit_without_tree_fails(void)
188 "author Author <author@example.com>\n"
189 "committer Committer <committer@example.com>\n"
192 assert_commit_fails(commit
, 0);
195 void test_object_commit_parse__parsing_commit_without_author_fails(void)
198 "tree 3e7ac388cadacccdf1c6c5f3445895b71d9cb0f8\n"
199 "committer Committer <committer@example.com>\n"
202 assert_commit_fails(commit
, 0);
205 void test_object_commit_parse__parsing_commit_without_committer_fails(void)
208 "tree 3e7ac388cadacccdf1c6c5f3445895b71d9cb0f8\n"
209 "author Author <author@example.com>\n"
212 assert_commit_fails(commit
, 0);
215 void test_object_commit_parse__parsing_encoding_will_not_cause_oob_read(void)
218 "tree 3e7ac388cadacccdf1c6c5f3445895b71d9cb0f8\n"
223 * As we ignore unknown fields, the cut-off encoding field will be
226 assert_commit_parses(commit
, strlen(commit
) - strlen("ncoding foo\n"),
227 "3e7ac388cadacccdf1c6c5f3445895b71d9cb0f8",