]>
Commit | Line | Data |
---|---|---|
e0799b6c | 1 | #include "clar_libgit2.h" |
eae0bfdc | 2 | #include "signature.h" |
e0799b6c BS |
3 | |
4 | static int try_build_signature(const char *name, const char *email, git_time_t time, int offset) | |
5 | { | |
6 | git_signature *sign; | |
e172cf08 | 7 | int error = 0; |
e0799b6c | 8 | |
e172cf08 | 9 | if ((error = git_signature_new(&sign, name, email, time, offset)) < 0) |
e0799b6c BS |
10 | return error; |
11 | ||
12 | git_signature_free((git_signature *)sign); | |
13 | ||
14 | return error; | |
15 | } | |
16 | ||
8aedf1d5 | 17 | static void assert_name_and_email( |
18 | const char *expected_name, | |
19 | const char *expected_email, | |
20 | const char *name, | |
21 | const char *email) | |
22 | { | |
23 | git_signature *sign; | |
24 | ||
25 | cl_git_pass(git_signature_new(&sign, name, email, 1234567890, 60)); | |
26 | cl_assert_equal_s(expected_name, sign->name); | |
27 | cl_assert_equal_s(expected_email, sign->email); | |
28 | ||
29 | git_signature_free(sign); | |
30 | } | |
e0799b6c | 31 | |
8aedf1d5 | 32 | void test_commit_signature__leading_and_trailing_spaces_are_trimmed(void) |
e0799b6c | 33 | { |
8aedf1d5 | 34 | assert_name_and_email("nulltoken", "emeric.fermas@gmail.com", " nulltoken ", " emeric.fermas@gmail.com "); |
24ec6999 CMN |
35 | assert_name_and_email("nulltoken", "emeric.fermas@gmail.com", " nulltoken ", " emeric.fermas@gmail.com \n"); |
36 | assert_name_and_email("nulltoken", "emeric.fermas@gmail.com", " \t nulltoken \n", " \n emeric.fermas@gmail.com \n"); | |
e0799b6c BS |
37 | } |
38 | ||
307c4a2b VM |
39 | void test_commit_signature__leading_and_trailing_crud_is_trimmed(void) |
40 | { | |
41 | assert_name_and_email("nulltoken", "emeric.fermas@gmail.com", "\"nulltoken\"", "\"emeric.fermas@gmail.com\""); | |
42 | assert_name_and_email("nulltoken w", "emeric.fermas@gmail.com", "nulltoken w.", "emeric.fermas@gmail.com"); | |
43 | assert_name_and_email("nulltoken \xe2\x98\xba", "emeric.fermas@gmail.com", "nulltoken \xe2\x98\xba", "emeric.fermas@gmail.com"); | |
44 | } | |
45 | ||
ac3d33df JK |
46 | void test_commit_signature__timezone_does_not_read_oob(void) |
47 | { | |
48 | const char *header = "A <a@example.com> 1461698487 +1234", *header_end; | |
49 | git_signature *sig; | |
50 | ||
51 | /* Let the buffer end midway between the timezone offeset's "+12" and "34" */ | |
52 | header_end = header + strlen(header) - 2; | |
53 | ||
54 | sig = git__calloc(1, sizeof(git_signature)); | |
55 | cl_assert(sig); | |
56 | ||
57 | cl_git_pass(git_signature__parse(sig, &header, header_end, NULL, '\0')); | |
58 | cl_assert_equal_s(sig->name, "A"); | |
59 | cl_assert_equal_s(sig->email, "a@example.com"); | |
60 | cl_assert_equal_i(sig->when.time, 1461698487); | |
61 | cl_assert_equal_i(sig->when.offset, 12); | |
62 | ||
63 | git_signature_free(sig); | |
64 | } | |
65 | ||
8aedf1d5 | 66 | void test_commit_signature__angle_brackets_in_names_are_not_supported(void) |
67 | { | |
68 | cl_git_fail(try_build_signature("<Phil Haack", "phil@haack", 1234567890, 60)); | |
69 | cl_git_fail(try_build_signature("Phil>Haack", "phil@haack", 1234567890, 60)); | |
70 | cl_git_fail(try_build_signature("<Phil Haack>", "phil@haack", 1234567890, 60)); | |
71 | } | |
72 | ||
73 | void test_commit_signature__angle_brackets_in_email_are_not_supported(void) | |
74 | { | |
75 | cl_git_fail(try_build_signature("Phil Haack", ">phil@haack", 1234567890, 60)); | |
76 | cl_git_fail(try_build_signature("Phil Haack", "phil@>haack", 1234567890, 60)); | |
77 | cl_git_fail(try_build_signature("Phil Haack", "<phil@haack>", 1234567890, 60)); | |
78 | } | |
e0799b6c BS |
79 | |
80 | void test_commit_signature__create_empties(void) | |
81 | { | |
ac3d33df | 82 | /* can not create a signature with empty name or email */ |
e0799b6c BS |
83 | cl_git_pass(try_build_signature("nulltoken", "emeric.fermas@gmail.com", 1234567890, 60)); |
84 | ||
85 | cl_git_fail(try_build_signature("", "emeric.fermas@gmail.com", 1234567890, 60)); | |
86 | cl_git_fail(try_build_signature(" ", "emeric.fermas@gmail.com", 1234567890, 60)); | |
76e3c43f CMN |
87 | cl_git_fail(try_build_signature("nulltoken", "", 1234567890, 60)); |
88 | cl_git_fail(try_build_signature("nulltoken", " ", 1234567890, 60)); | |
e0799b6c BS |
89 | } |
90 | ||
91 | void test_commit_signature__create_one_char(void) | |
92 | { | |
ac3d33df | 93 | /* creating a one character signature */ |
8aedf1d5 | 94 | assert_name_and_email("x", "foo@bar.baz", "x", "foo@bar.baz"); |
e0799b6c BS |
95 | } |
96 | ||
97 | void test_commit_signature__create_two_char(void) | |
98 | { | |
ac3d33df | 99 | /* creating a two character signature */ |
8aedf1d5 | 100 | assert_name_and_email("xx", "foo@bar.baz", "xx", "foo@bar.baz"); |
e0799b6c BS |
101 | } |
102 | ||
103 | void test_commit_signature__create_zero_char(void) | |
104 | { | |
ac3d33df | 105 | /* creating a zero character signature */ |
e0799b6c BS |
106 | git_signature *sign; |
107 | cl_git_fail(git_signature_new(&sign, "", "x@y.z", 1234567890, 60)); | |
108 | cl_assert(sign == NULL); | |
109 | } | |
d383c39b ET |
110 | |
111 | void test_commit_signature__from_buf(void) | |
112 | { | |
113 | git_signature *sign; | |
114 | ||
115 | cl_git_pass(git_signature_from_buffer(&sign, "Test User <test@test.tt> 1461698487 +0200")); | |
116 | cl_assert_equal_s("Test User", sign->name); | |
117 | cl_assert_equal_s("test@test.tt", sign->email); | |
118 | cl_assert_equal_i(1461698487, sign->when.time); | |
119 | cl_assert_equal_i(120, sign->when.offset); | |
120 | git_signature_free(sign); | |
121 | } | |
122 | ||
eae0bfdc PP |
123 | void test_commit_signature__from_buf_with_neg_zero_offset(void) |
124 | { | |
125 | git_signature *sign; | |
126 | ||
127 | cl_git_pass(git_signature_from_buffer(&sign, "Test User <test@test.tt> 1461698487 -0000")); | |
128 | cl_assert_equal_s("Test User", sign->name); | |
129 | cl_assert_equal_s("test@test.tt", sign->email); | |
130 | cl_assert_equal_i(1461698487, sign->when.time); | |
131 | cl_assert_equal_i(0, sign->when.offset); | |
132 | cl_assert_equal_i('-', sign->when.sign); | |
133 | git_signature_free(sign); | |
134 | } | |
135 | ||
136 | void test_commit_signature__pos_and_neg_zero_offsets_dont_match(void) | |
137 | { | |
138 | git_signature *with_neg_zero; | |
139 | git_signature *with_pos_zero; | |
140 | ||
141 | cl_git_pass(git_signature_from_buffer(&with_neg_zero, "Test User <test@test.tt> 1461698487 -0000")); | |
142 | cl_git_pass(git_signature_from_buffer(&with_pos_zero, "Test User <test@test.tt> 1461698487 +0000")); | |
143 | ||
144 | cl_assert(!git_signature__equal(with_neg_zero, with_pos_zero)); | |
145 | ||
146 | git_signature_free((git_signature *)with_neg_zero); | |
147 | git_signature_free((git_signature *)with_pos_zero); | |
148 | } |