]>
Commit | Line | Data |
---|---|---|
e3c47510 | 1 | #include "clar_libgit2.h" |
1a628100 | 2 | |
ad5611d8 | 3 | void test_errors__public_api(void) |
1a628100 RB |
4 | { |
5 | char *str_in_error; | |
6 | ||
ac3d33df JK |
7 | git_error_clear(); |
8 | cl_assert(git_error_last() == NULL); | |
1a628100 | 9 | |
ac3d33df | 10 | git_error_set_oom(); |
1a628100 | 11 | |
ac3d33df JK |
12 | cl_assert(git_error_last() != NULL); |
13 | cl_assert(git_error_last()->klass == GIT_ERROR_NOMEMORY); | |
14 | str_in_error = strstr(git_error_last()->message, "memory"); | |
1a628100 RB |
15 | cl_assert(str_in_error != NULL); |
16 | ||
ac3d33df | 17 | git_error_clear(); |
1a628100 | 18 | |
ac3d33df | 19 | git_error_set_str(GIT_ERROR_REPOSITORY, "This is a test"); |
1a628100 | 20 | |
ac3d33df JK |
21 | cl_assert(git_error_last() != NULL); |
22 | str_in_error = strstr(git_error_last()->message, "This is a test"); | |
1a628100 RB |
23 | cl_assert(str_in_error != NULL); |
24 | ||
ac3d33df JK |
25 | git_error_clear(); |
26 | cl_assert(git_error_last() == NULL); | |
1a628100 RB |
27 | } |
28 | ||
e3c47510 RB |
29 | #include "common.h" |
30 | #include "util.h" | |
31 | #include "posix.h" | |
32 | ||
ad5611d8 | 33 | void test_errors__new_school(void) |
e3c47510 RB |
34 | { |
35 | char *str_in_error; | |
36 | ||
ac3d33df JK |
37 | git_error_clear(); |
38 | cl_assert(git_error_last() == NULL); | |
e3c47510 | 39 | |
ac3d33df | 40 | git_error_set_oom(); /* internal fn */ |
e3c47510 | 41 | |
ac3d33df JK |
42 | cl_assert(git_error_last() != NULL); |
43 | cl_assert(git_error_last()->klass == GIT_ERROR_NOMEMORY); | |
44 | str_in_error = strstr(git_error_last()->message, "memory"); | |
e3c47510 RB |
45 | cl_assert(str_in_error != NULL); |
46 | ||
ac3d33df | 47 | git_error_clear(); |
e3c47510 | 48 | |
ac3d33df | 49 | git_error_set(GIT_ERROR_REPOSITORY, "This is a test"); /* internal fn */ |
e3c47510 | 50 | |
ac3d33df JK |
51 | cl_assert(git_error_last() != NULL); |
52 | str_in_error = strstr(git_error_last()->message, "This is a test"); | |
e3c47510 RB |
53 | cl_assert(str_in_error != NULL); |
54 | ||
ac3d33df JK |
55 | git_error_clear(); |
56 | cl_assert(git_error_last() == NULL); | |
e3c47510 | 57 | |
1a6e8f8a | 58 | do { |
e3c47510 | 59 | struct stat st; |
1a6e8f8a | 60 | memset(&st, 0, sizeof(st)); |
6654dbe3 | 61 | cl_assert(p_lstat("this_file_does_not_exist", &st) < 0); |
17bd6de3 | 62 | GIT_UNUSED(st); |
1a6e8f8a | 63 | } while (false); |
ac3d33df | 64 | git_error_set(GIT_ERROR_OS, "stat failed"); /* internal fn */ |
e3c47510 | 65 | |
ac3d33df JK |
66 | cl_assert(git_error_last() != NULL); |
67 | str_in_error = strstr(git_error_last()->message, "stat failed"); | |
e3c47510 RB |
68 | cl_assert(str_in_error != NULL); |
69 | cl_assert(git__prefixcmp(str_in_error, "stat failed: ") == 0); | |
70 | cl_assert(strlen(str_in_error) > strlen("stat failed: ")); | |
71 | ||
72 | #ifdef GIT_WIN32 | |
ac3d33df | 73 | git_error_clear(); |
e3c47510 RB |
74 | |
75 | /* The MSDN docs use this to generate a sample error */ | |
76 | cl_assert(GetProcessId(NULL) == 0); | |
ac3d33df | 77 | git_error_set(GIT_ERROR_OS, "GetProcessId failed"); /* internal fn */ |
e3c47510 | 78 | |
ac3d33df JK |
79 | cl_assert(git_error_last() != NULL); |
80 | str_in_error = strstr(git_error_last()->message, "GetProcessId failed"); | |
e3c47510 RB |
81 | cl_assert(str_in_error != NULL); |
82 | cl_assert(git__prefixcmp(str_in_error, "GetProcessId failed: ") == 0); | |
83 | cl_assert(strlen(str_in_error) > strlen("GetProcessId failed: ")); | |
84 | #endif | |
85 | ||
ac3d33df | 86 | git_error_clear(); |
e3c47510 | 87 | } |
f2f2d97f | 88 | |
ad5611d8 | 89 | void test_errors__restore(void) |
f2f2d97f ET |
90 | { |
91 | git_error_state err_state = {0}; | |
92 | ||
ac3d33df JK |
93 | git_error_clear(); |
94 | cl_assert(git_error_last() == NULL); | |
f2f2d97f | 95 | |
ac3d33df | 96 | cl_assert_equal_i(0, git_error_state_capture(&err_state, 0)); |
f2f2d97f ET |
97 | |
98 | memset(&err_state, 0x0, sizeof(git_error_state)); | |
99 | ||
ac3d33df JK |
100 | git_error_set(42, "Foo: %s", "bar"); |
101 | cl_assert_equal_i(-1, git_error_state_capture(&err_state, -1)); | |
f2f2d97f | 102 | |
ac3d33df | 103 | cl_assert(git_error_last() == NULL); |
f2f2d97f | 104 | |
ac3d33df | 105 | git_error_set(99, "Bar: %s", "foo"); |
f2f2d97f | 106 | |
ac3d33df | 107 | git_error_state_restore(&err_state); |
f2f2d97f | 108 | |
ac3d33df JK |
109 | cl_assert_equal_i(42, git_error_last()->klass); |
110 | cl_assert_equal_s("Foo: bar", git_error_last()->message); | |
f2f2d97f | 111 | } |
392702ee | 112 | |
ad5611d8 | 113 | void test_errors__free_state(void) |
ef4857c2 ET |
114 | { |
115 | git_error_state err_state = {0}; | |
116 | ||
ac3d33df | 117 | git_error_clear(); |
ef4857c2 | 118 | |
ac3d33df JK |
119 | git_error_set(42, "Foo: %s", "bar"); |
120 | cl_assert_equal_i(-1, git_error_state_capture(&err_state, -1)); | |
ef4857c2 | 121 | |
ac3d33df | 122 | git_error_set(99, "Bar: %s", "foo"); |
ef4857c2 | 123 | |
ac3d33df | 124 | git_error_state_free(&err_state); |
ef4857c2 | 125 | |
ac3d33df JK |
126 | cl_assert_equal_i(99, git_error_last()->klass); |
127 | cl_assert_equal_s("Bar: foo", git_error_last()->message); | |
ef4857c2 | 128 | |
ac3d33df | 129 | git_error_state_restore(&err_state); |
ef4857c2 | 130 | |
ac3d33df | 131 | cl_assert(git_error_last() == NULL); |
ef4857c2 ET |
132 | } |
133 | ||
ad5611d8 | 134 | void test_errors__restore_oom(void) |
5ef4b860 MP |
135 | { |
136 | git_error_state err_state = {0}; | |
988ea594 | 137 | const git_error *oom_error = NULL; |
5ef4b860 | 138 | |
ac3d33df | 139 | git_error_clear(); |
5ef4b860 | 140 | |
ac3d33df JK |
141 | git_error_set_oom(); /* internal fn */ |
142 | oom_error = git_error_last(); | |
988ea594 | 143 | cl_assert(oom_error); |
5ef4b860 | 144 | |
ac3d33df | 145 | cl_assert_equal_i(-1, git_error_state_capture(&err_state, -1)); |
5ef4b860 | 146 | |
ac3d33df JK |
147 | cl_assert(git_error_last() == NULL); |
148 | cl_assert_equal_i(GIT_ERROR_NOMEMORY, err_state.error_msg.klass); | |
ef4857c2 | 149 | cl_assert_equal_s("Out of memory", err_state.error_msg.message); |
5ef4b860 | 150 | |
ac3d33df | 151 | git_error_state_restore(&err_state); |
5ef4b860 | 152 | |
ac3d33df JK |
153 | cl_assert(git_error_last()->klass == GIT_ERROR_NOMEMORY); |
154 | cl_assert_(git_error_last() == oom_error, "static oom error not restored"); | |
5ef4b860 | 155 | |
ac3d33df | 156 | git_error_clear(); |
5ef4b860 MP |
157 | } |
158 | ||
392702ee ET |
159 | static int test_arraysize_multiply(size_t nelem, size_t size) |
160 | { | |
f1453c59 | 161 | size_t out; |
ac3d33df | 162 | GIT_ERROR_CHECK_ALLOC_MULTIPLY(&out, nelem, size); |
392702ee ET |
163 | return 0; |
164 | } | |
165 | ||
ad5611d8 | 166 | void test_errors__integer_overflow_alloc_multiply(void) |
392702ee ET |
167 | { |
168 | cl_git_pass(test_arraysize_multiply(10, 10)); | |
169 | cl_git_pass(test_arraysize_multiply(1000, 1000)); | |
170 | cl_git_pass(test_arraysize_multiply(SIZE_MAX/sizeof(void *), sizeof(void *))); | |
171 | cl_git_pass(test_arraysize_multiply(0, 10)); | |
172 | cl_git_pass(test_arraysize_multiply(10, 0)); | |
173 | ||
174 | cl_git_fail(test_arraysize_multiply(SIZE_MAX-1, sizeof(void *))); | |
175 | cl_git_fail(test_arraysize_multiply((SIZE_MAX/sizeof(void *))+1, sizeof(void *))); | |
176 | ||
ac3d33df JK |
177 | cl_assert_equal_i(GIT_ERROR_NOMEMORY, git_error_last()->klass); |
178 | cl_assert_equal_s("Out of memory", git_error_last()->message); | |
392702ee ET |
179 | } |
180 | ||
181 | static int test_arraysize_add(size_t one, size_t two) | |
182 | { | |
f1453c59 | 183 | size_t out; |
ac3d33df | 184 | GIT_ERROR_CHECK_ALLOC_ADD(&out, one, two); |
392702ee ET |
185 | return 0; |
186 | } | |
187 | ||
ad5611d8 | 188 | void test_errors__integer_overflow_alloc_add(void) |
392702ee ET |
189 | { |
190 | cl_git_pass(test_arraysize_add(10, 10)); | |
191 | cl_git_pass(test_arraysize_add(1000, 1000)); | |
192 | cl_git_pass(test_arraysize_add(SIZE_MAX-10, 10)); | |
193 | ||
194 | cl_git_fail(test_arraysize_multiply(SIZE_MAX-1, 2)); | |
195 | cl_git_fail(test_arraysize_multiply(SIZE_MAX, SIZE_MAX)); | |
196 | ||
ac3d33df JK |
197 | cl_assert_equal_i(GIT_ERROR_NOMEMORY, git_error_last()->klass); |
198 | cl_assert_equal_s("Out of memory", git_error_last()->message); | |
392702ee | 199 | } |
2884cc42 | 200 | |
ad5611d8 | 201 | void test_errors__integer_overflow_sets_oom(void) |
2884cc42 | 202 | { |
f1453c59 ET |
203 | size_t out; |
204 | ||
ac3d33df | 205 | git_error_clear(); |
f1453c59 | 206 | cl_assert(!GIT_ADD_SIZET_OVERFLOW(&out, SIZE_MAX-1, 1)); |
ac3d33df | 207 | cl_assert_equal_p(NULL, git_error_last()); |
2884cc42 | 208 | |
ac3d33df | 209 | git_error_clear(); |
f1453c59 | 210 | cl_assert(!GIT_ADD_SIZET_OVERFLOW(&out, 42, 69)); |
ac3d33df | 211 | cl_assert_equal_p(NULL, git_error_last()); |
2884cc42 | 212 | |
ac3d33df | 213 | git_error_clear(); |
f1453c59 | 214 | cl_assert(GIT_ADD_SIZET_OVERFLOW(&out, SIZE_MAX, SIZE_MAX)); |
ac3d33df JK |
215 | cl_assert_equal_i(GIT_ERROR_NOMEMORY, git_error_last()->klass); |
216 | cl_assert_equal_s("Out of memory", git_error_last()->message); | |
2884cc42 | 217 | |
ac3d33df | 218 | git_error_clear(); |
f1453c59 | 219 | cl_assert(GIT_ADD_SIZET_OVERFLOW(&out, SIZE_MAX, SIZE_MAX)); |
ac3d33df JK |
220 | cl_assert_equal_i(GIT_ERROR_NOMEMORY, git_error_last()->klass); |
221 | cl_assert_equal_s("Out of memory", git_error_last()->message); | |
2884cc42 | 222 | } |