Add unit tests for Commit parsing
[libgit2.git] / tests / t0101-oid.c
blobc867e4ff48cb00173ab50011a7cd593ab9d3905e
1 #include "test_lib.h"
2 #include <git/oid.h>
4 BEGIN_TEST(oid_szs)
5 git_oid out;
6 must_be_true(20 == GIT_OID_RAWSZ);
7 must_be_true(40 == GIT_OID_HEXSZ);
8 must_be_true(sizeof(out) == GIT_OID_RAWSZ);
9 must_be_true(sizeof(out.id) == GIT_OID_RAWSZ);
10 END_TEST
12 BEGIN_TEST(empty_string)
13 git_oid out;
14 must_fail(git_oid_mkstr(&out, ""));
15 END_TEST
17 BEGIN_TEST(invalid_string_moo)
18 git_oid out;
19 must_fail(git_oid_mkstr(&out, "moo"));
20 END_TEST
22 static int from_hex(unsigned int i)
24 if (i >= '0' && i <= '9')
25 return i - '0';
26 if (i >= 'a' && i <= 'f')
27 return 10 + (i - 'a');
28 if (i >= 'A' && i <= 'F')
29 return 10 + (i - 'A');
30 return -1;
33 BEGIN_TEST(invalid_string_all_chars)
34 git_oid out;
35 unsigned char exp[] = {
36 0x16, 0xa6, 0x77, 0x70, 0xb7,
37 0xd8, 0xd7, 0x23, 0x17, 0xc4,
38 0xb7, 0x75, 0x21, 0x3c, 0x23,
39 0xa8, 0xbd, 0x74, 0xf5, 0xe0,
41 char in[41] = "16a67770b7d8d72317c4b775213c23a8bd74f5e0";
42 unsigned int i;
44 for (i = 0; i < 256; i++) {
45 in[38] = (char)i;
47 if (from_hex(i) >= 0) {
48 exp[19] = (unsigned char)(from_hex(i) << 4);
49 if (git_oid_mkstr(&out, in))
50 test_die("line %d: must accept '%s'", __LINE__, in);
51 if (memcmp(out.id, exp, sizeof(out.id)))
52 test_die("line %d: bad parse of '%s', %x != %x",
53 __LINE__, in, exp[19], out.id[19]);
54 } else if (!git_oid_mkstr(&out, in))
55 test_die("line %d: must not accept '%s'", __LINE__, in);
57 END_TEST
59 BEGIN_TEST(invalid_string_16a67770b7d8d72317c4b775213c23a8bd74f5ez)
60 git_oid out;
61 must_fail(git_oid_mkstr(&out, "16a67770b7d8d72317c4b775213c23a8bd74f5ez"));
62 END_TEST
64 BEGIN_TEST(valid_string_16a67770b7d8d72317c4b775213c23a8bd74f5e0)
65 git_oid out;
66 unsigned char exp[] = {
67 0x16, 0xa6, 0x77, 0x70, 0xb7,
68 0xd8, 0xd7, 0x23, 0x17, 0xc4,
69 0xb7, 0x75, 0x21, 0x3c, 0x23,
70 0xa8, 0xbd, 0x74, 0xf5, 0xe0,
73 must_pass(git_oid_mkstr(&out, "16a67770b7d8d72317c4b775213c23a8bd74f5e0"));
74 must_pass(memcmp(out.id, exp, sizeof(out.id)));
76 must_pass(git_oid_mkstr(&out, "16A67770B7D8D72317C4b775213C23A8BD74F5E0"));
77 must_pass(memcmp(out.id, exp, sizeof(out.id)));
78 END_TEST
80 BEGIN_TEST(valid_raw)
81 git_oid out;
82 unsigned char exp[] = {
83 0x16, 0xa6, 0x77, 0x70, 0xb7,
84 0xd8, 0xd7, 0x23, 0x17, 0xc4,
85 0xb7, 0x75, 0x21, 0x3c, 0x23,
86 0xa8, 0xbd, 0x74, 0xf5, 0xe0,
89 git_oid_mkraw(&out, exp);
90 must_pass(memcmp(out.id, exp, sizeof(out.id)));
91 END_TEST
93 BEGIN_TEST(copy_oid)
94 git_oid a, b;
95 unsigned char exp[] = {
96 0x16, 0xa6, 0x77, 0x70, 0xb7,
97 0xd8, 0xd7, 0x23, 0x17, 0xc4,
98 0xb7, 0x75, 0x21, 0x3c, 0x23,
99 0xa8, 0xbd, 0x74, 0xf5, 0xe0,
102 memset(&b, 0, sizeof(b));
103 git_oid_mkraw(&a, exp);
104 git_oid_cpy(&b, &a);
105 must_pass(memcmp(a.id, exp, sizeof(a.id)));
106 END_TEST
108 BEGIN_TEST(cmp_oid_lt)
109 git_oid a, b;
110 unsigned char a_in[] = {
111 0x16, 0xa6, 0x77, 0x70, 0xb7,
112 0xd8, 0xd7, 0x23, 0x17, 0xc4,
113 0xb7, 0x75, 0x21, 0x3c, 0x23,
114 0xa8, 0xbd, 0x74, 0xf5, 0xe0,
116 unsigned char b_in[] = {
117 0x16, 0xa6, 0x77, 0x70, 0xb7,
118 0xd8, 0xd7, 0x23, 0x17, 0xc4,
119 0xb7, 0x75, 0x21, 0x3c, 0x23,
120 0xa8, 0xbd, 0x74, 0xf5, 0xf0,
123 git_oid_mkraw(&a, a_in);
124 git_oid_mkraw(&b, b_in);
125 must_be_true(git_oid_cmp(&a, &b) < 0);
126 END_TEST
128 BEGIN_TEST(cmp_oid_eq)
129 git_oid a, b;
130 unsigned char a_in[] = {
131 0x16, 0xa6, 0x77, 0x70, 0xb7,
132 0xd8, 0xd7, 0x23, 0x17, 0xc4,
133 0xb7, 0x75, 0x21, 0x3c, 0x23,
134 0xa8, 0xbd, 0x74, 0xf5, 0xe0,
137 git_oid_mkraw(&a, a_in);
138 git_oid_mkraw(&b, a_in);
139 must_be_true(git_oid_cmp(&a, &b) == 0);
140 END_TEST
142 BEGIN_TEST(cmp_oid_gt)
143 git_oid a, b;
144 unsigned char a_in[] = {
145 0x16, 0xa6, 0x77, 0x70, 0xb7,
146 0xd8, 0xd7, 0x23, 0x17, 0xc4,
147 0xb7, 0x75, 0x21, 0x3c, 0x23,
148 0xa8, 0xbd, 0x74, 0xf5, 0xe0,
150 unsigned char b_in[] = {
151 0x16, 0xa6, 0x77, 0x70, 0xb7,
152 0xd8, 0xd7, 0x23, 0x17, 0xc4,
153 0xb7, 0x75, 0x21, 0x3c, 0x23,
154 0xa8, 0xbd, 0x74, 0xf5, 0xd0,
157 git_oid_mkraw(&a, a_in);
158 git_oid_mkraw(&b, b_in);
159 must_be_true(git_oid_cmp(&a, &b) > 0);
160 END_TEST
162 BEGIN_TEST(cmp_oid_fmt)
163 const char *exp = "16a0123456789abcdef4b775213c23a8bd74f5e0";
164 git_oid in;
165 char out[GIT_OID_HEXSZ + 1];
167 must_pass(git_oid_mkstr(&in, exp));
169 /* Format doesn't touch the last byte */
170 out[GIT_OID_HEXSZ] = 'Z';
171 git_oid_fmt(out, &in);
172 must_be_true(out[GIT_OID_HEXSZ] == 'Z');
174 /* Format produced the right result */
175 out[GIT_OID_HEXSZ] = '\0';
176 must_pass(strcmp(exp, out));
177 END_TEST
179 BEGIN_TEST(cmp_oid_allocfmt)
180 const char *exp = "16a0123456789abcdef4b775213c23a8bd74f5e0";
181 git_oid in;
182 char *out;
184 must_pass(git_oid_mkstr(&in, exp));
186 out = git_oid_allocfmt(&in);
187 must_be_true(out);
188 must_pass(strcmp(exp, out));
189 free(out);
190 END_TEST
192 BEGIN_TEST(cmp_oid_pathfmt)
193 const char *exp1 = "16a0123456789abcdef4b775213c23a8bd74f5e0";
194 const char *exp2 = "16/a0123456789abcdef4b775213c23a8bd74f5e0";
195 git_oid in;
196 char out[GIT_OID_HEXSZ + 2];
198 must_pass(git_oid_mkstr(&in, exp1));
200 /* Format doesn't touch the last byte */
201 out[GIT_OID_HEXSZ + 1] = 'Z';
202 git_oid_pathfmt(out, &in);
203 must_be_true(out[GIT_OID_HEXSZ + 1] == 'Z');
205 /* Format produced the right result */
206 out[GIT_OID_HEXSZ + 1] = '\0';
207 must_pass(strcmp(exp2, out));
208 END_TEST
210 BEGIN_TEST(oid_to_string)
211 const char *exp = "16a0123456789abcdef4b775213c23a8bd74f5e0";
212 git_oid in;
213 char out[GIT_OID_HEXSZ + 1];
214 char *str;
215 int i;
217 must_pass(git_oid_mkstr(&in, exp));
219 /* NULL buffer pointer, returns static empty string */
220 str = git_oid_to_string(NULL, sizeof(out), &in);
221 must_be_true(str && *str == '\0' && str != out);
223 /* zero buffer size, returns static empty string */
224 str = git_oid_to_string(out, 0, &in);
225 must_be_true(str && *str == '\0' && str != out);
227 /* NULL oid pointer, returns static empty string */
228 str = git_oid_to_string(out, sizeof(out), NULL);
229 must_be_true(str && *str == '\0' && str != out);
231 /* n == 1, returns out as an empty string */
232 str = git_oid_to_string(out, 1, &in);
233 must_be_true(str && *str == '\0' && str == out);
235 for (i = 1; i < GIT_OID_HEXSZ; i++) {
236 out[i+1] = 'Z';
237 str = git_oid_to_string(out, i+1, &in);
238 /* returns out containing c-string */
239 must_be_true(str && str == out);
240 /* must be '\0' terminated */
241 must_be_true(*(str+i) == '\0');
242 /* must not touch bytes past end of string */
243 must_be_true(*(str+(i+1)) == 'Z');
244 /* i == n-1 charaters of string */
245 must_pass(strncmp(exp, out, i));
248 /* returns out as hex formatted c-string */
249 str = git_oid_to_string(out, sizeof(out), &in);
250 must_be_true(str && str == out && *(str+GIT_OID_HEXSZ) == '\0');
251 must_pass(strcmp(exp, out));
252 END_TEST