]> git.proxmox.com Git - libgit2.git/blob - tests/util/errors.c
New upstream version 1.5.0+ds
[libgit2.git] / tests / util / errors.c
1 #include "clar_libgit2.h"
2
3 void test_errors__public_api(void)
4 {
5 char *str_in_error;
6
7 git_error_clear();
8 cl_assert(git_error_last() == NULL);
9
10 git_error_set_oom();
11
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");
15 cl_assert(str_in_error != NULL);
16
17 git_error_clear();
18
19 git_error_set_str(GIT_ERROR_REPOSITORY, "This is a test");
20
21 cl_assert(git_error_last() != NULL);
22 str_in_error = strstr(git_error_last()->message, "This is a test");
23 cl_assert(str_in_error != NULL);
24
25 git_error_clear();
26 cl_assert(git_error_last() == NULL);
27 }
28
29 #include "common.h"
30 #include "util.h"
31 #include "posix.h"
32
33 void test_errors__new_school(void)
34 {
35 char *str_in_error;
36
37 git_error_clear();
38 cl_assert(git_error_last() == NULL);
39
40 git_error_set_oom(); /* internal fn */
41
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");
45 cl_assert(str_in_error != NULL);
46
47 git_error_clear();
48
49 git_error_set(GIT_ERROR_REPOSITORY, "This is a test"); /* internal fn */
50
51 cl_assert(git_error_last() != NULL);
52 str_in_error = strstr(git_error_last()->message, "This is a test");
53 cl_assert(str_in_error != NULL);
54
55 git_error_clear();
56 cl_assert(git_error_last() == NULL);
57
58 do {
59 struct stat st;
60 memset(&st, 0, sizeof(st));
61 cl_assert(p_lstat("this_file_does_not_exist", &st) < 0);
62 GIT_UNUSED(st);
63 } while (false);
64 git_error_set(GIT_ERROR_OS, "stat failed"); /* internal fn */
65
66 cl_assert(git_error_last() != NULL);
67 str_in_error = strstr(git_error_last()->message, "stat failed");
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
73 git_error_clear();
74
75 /* The MSDN docs use this to generate a sample error */
76 cl_assert(GetProcessId(NULL) == 0);
77 git_error_set(GIT_ERROR_OS, "GetProcessId failed"); /* internal fn */
78
79 cl_assert(git_error_last() != NULL);
80 str_in_error = strstr(git_error_last()->message, "GetProcessId failed");
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
86 git_error_clear();
87 }
88
89 void test_errors__restore(void)
90 {
91 git_error_state err_state = {0};
92
93 git_error_clear();
94 cl_assert(git_error_last() == NULL);
95
96 cl_assert_equal_i(0, git_error_state_capture(&err_state, 0));
97
98 memset(&err_state, 0x0, sizeof(git_error_state));
99
100 git_error_set(42, "Foo: %s", "bar");
101 cl_assert_equal_i(-1, git_error_state_capture(&err_state, -1));
102
103 cl_assert(git_error_last() == NULL);
104
105 git_error_set(99, "Bar: %s", "foo");
106
107 git_error_state_restore(&err_state);
108
109 cl_assert_equal_i(42, git_error_last()->klass);
110 cl_assert_equal_s("Foo: bar", git_error_last()->message);
111 }
112
113 void test_errors__free_state(void)
114 {
115 git_error_state err_state = {0};
116
117 git_error_clear();
118
119 git_error_set(42, "Foo: %s", "bar");
120 cl_assert_equal_i(-1, git_error_state_capture(&err_state, -1));
121
122 git_error_set(99, "Bar: %s", "foo");
123
124 git_error_state_free(&err_state);
125
126 cl_assert_equal_i(99, git_error_last()->klass);
127 cl_assert_equal_s("Bar: foo", git_error_last()->message);
128
129 git_error_state_restore(&err_state);
130
131 cl_assert(git_error_last() == NULL);
132 }
133
134 void test_errors__restore_oom(void)
135 {
136 git_error_state err_state = {0};
137 const git_error *oom_error = NULL;
138
139 git_error_clear();
140
141 git_error_set_oom(); /* internal fn */
142 oom_error = git_error_last();
143 cl_assert(oom_error);
144
145 cl_assert_equal_i(-1, git_error_state_capture(&err_state, -1));
146
147 cl_assert(git_error_last() == NULL);
148 cl_assert_equal_i(GIT_ERROR_NOMEMORY, err_state.error_msg.klass);
149 cl_assert_equal_s("Out of memory", err_state.error_msg.message);
150
151 git_error_state_restore(&err_state);
152
153 cl_assert(git_error_last()->klass == GIT_ERROR_NOMEMORY);
154 cl_assert_(git_error_last() == oom_error, "static oom error not restored");
155
156 git_error_clear();
157 }
158
159 static int test_arraysize_multiply(size_t nelem, size_t size)
160 {
161 size_t out;
162 GIT_ERROR_CHECK_ALLOC_MULTIPLY(&out, nelem, size);
163 return 0;
164 }
165
166 void test_errors__integer_overflow_alloc_multiply(void)
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
177 cl_assert_equal_i(GIT_ERROR_NOMEMORY, git_error_last()->klass);
178 cl_assert_equal_s("Out of memory", git_error_last()->message);
179 }
180
181 static int test_arraysize_add(size_t one, size_t two)
182 {
183 size_t out;
184 GIT_ERROR_CHECK_ALLOC_ADD(&out, one, two);
185 return 0;
186 }
187
188 void test_errors__integer_overflow_alloc_add(void)
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
197 cl_assert_equal_i(GIT_ERROR_NOMEMORY, git_error_last()->klass);
198 cl_assert_equal_s("Out of memory", git_error_last()->message);
199 }
200
201 void test_errors__integer_overflow_sets_oom(void)
202 {
203 size_t out;
204
205 git_error_clear();
206 cl_assert(!GIT_ADD_SIZET_OVERFLOW(&out, SIZE_MAX-1, 1));
207 cl_assert_equal_p(NULL, git_error_last());
208
209 git_error_clear();
210 cl_assert(!GIT_ADD_SIZET_OVERFLOW(&out, 42, 69));
211 cl_assert_equal_p(NULL, git_error_last());
212
213 git_error_clear();
214 cl_assert(GIT_ADD_SIZET_OVERFLOW(&out, SIZE_MAX, SIZE_MAX));
215 cl_assert_equal_i(GIT_ERROR_NOMEMORY, git_error_last()->klass);
216 cl_assert_equal_s("Out of memory", git_error_last()->message);
217
218 git_error_clear();
219 cl_assert(GIT_ADD_SIZET_OVERFLOW(&out, SIZE_MAX, SIZE_MAX));
220 cl_assert_equal_i(GIT_ERROR_NOMEMORY, git_error_last()->klass);
221 cl_assert_equal_s("Out of memory", git_error_last()->message);
222 }