Debian Codegraph @

crt/aws-c-common/tests/error_test.c

raw symbols SCIP
1 /*
2 * Copyright 2010-2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License").
5 * You may not use this file except in compliance with the License.
6 * A copy of the License is located at
7 *
8 * http://aws.amazon.com/apache2.0
9 *
10 * or in the "license" file accompanying this file. This file is distributed
11 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12 * express or implied. See the License for the specific language governing
13 * permissions and limitations under the License.
14 */
15
16 #include <aws/common/error.h>
17
18 #include <aws/common/thread.h>
19 #include <aws/testing/aws_test_harness.h>
20
21 static struct aws_error_info s_errors[] = {
22 AWS_DEFINE_ERROR_INFO(1024, "test error 1", "test lib"),
23 AWS_DEFINE_ERROR_INFO(1025, "test error 2", "test lib"),
24 };
25
26 static struct aws_error_info_list s_errors_list = {
27 .error_list = s_errors,
28 .count = AWS_ARRAY_SIZE(s_errors),
29 };
30
31 static int s_setup_errors_test_fn(struct aws_allocator *allocator, void *ctx) {
32 (void)allocator;
33 (void)ctx;
34
35 aws_reset_error();
36 aws_set_global_error_handler_fn(NULL, NULL);
37 aws_set_thread_local_error_handler_fn(NULL, NULL);
38 aws_register_error_info(&s_errors_list);
39
40 return AWS_OP_SUCCESS;
41 }
42
43 static int s_teardown_errors_test_fn(struct aws_allocator *allocator, int setup_res, void *ctx) {
44 (void)allocator;
45 (void)setup_res;
46 (void)ctx;
47
48 aws_reset_error();
49 aws_set_global_error_handler_fn(NULL, NULL);
50 aws_set_thread_local_error_handler_fn(NULL, NULL);
51
52 return AWS_OP_SUCCESS;
53 }
54
55 static int s_raise_errors_test_fn(struct aws_allocator *allocator, void *ctx) {
56 (void)allocator;
57 (void)ctx;
58
59 int error = aws_last_error();
60
61 ASSERT_NULL(error, "error should be initialized to NULL");
62 ASSERT_INT_EQUALS(0, aws_last_error(), "error code should be initialized to 0");
63
64 struct aws_error_info test_error_1 = s_errors[0];
65 struct aws_error_info test_error_2 = s_errors[1];
66
67 ASSERT_INT_EQUALS(-1, aws_raise_error(test_error_1.error_code), "Raise error should return failure code.");
68 error = aws_last_error();
69 ASSERT_INT_EQUALS(
70 test_error_1.error_code, error, "Expected error code %d, but was %d", test_error_1.error_code, error);
71
72 ASSERT_STR_EQUALS(
73 test_error_1.error_str,
74 aws_error_str(error),
75 "Expected error string %s, but got %s",
76 test_error_1.error_str,
77 aws_error_str(error));
78 ASSERT_STR_EQUALS(
79 test_error_1.lib_name,
80 aws_error_lib_name(error),
81 "Expected error libname %s, but got %s",
82 test_error_1.lib_name,
83 aws_error_lib_name(error));
84
85 ASSERT_INT_EQUALS(-1, aws_raise_error(test_error_2.error_code), "Raise error should return failure code.");
86 error = aws_last_error();
87
88 ASSERT_INT_EQUALS(
89 test_error_2.error_code, error, "Expected error code %d, but was %d", test_error_2.error_code, error);
90
91 error = aws_last_error();
92 ASSERT_NOT_NULL(error, "last error should not have been null");
93 ASSERT_STR_EQUALS(
94 test_error_2.error_str,
95 aws_error_str(error),
96 "Expected error string %s, but got %s",
97 test_error_2.error_str,
98 aws_error_str(error));
99 ASSERT_STR_EQUALS(
100 test_error_2.lib_name,
101 aws_error_lib_name(error),
102 "Expected error libname %s, but got %s",
103 test_error_2.lib_name,
104 aws_error_lib_name(error));
105
106 aws_reset_error();
107 error = aws_last_error();
108 ASSERT_NULL(error, "error should be reset to NULL");
109 ASSERT_INT_EQUALS(0, aws_last_error(), "error code should be reset to 0");
110 return 0;
111 }
112
113 static int s_reset_errors_test_fn(struct aws_allocator *allocator, void *ctx) {
114 (void)allocator;
115 (void)ctx;
116
117 struct aws_error_info test_error_1 = s_errors[0];
118 struct aws_error_info test_error_2 = s_errors[1];
119
120 aws_raise_error(test_error_2.error_code);
121 aws_restore_error(test_error_1.error_code);
122
123 int error = aws_last_error();
124 ASSERT_NOT_NULL(error, "last error should not have been null");
125 ASSERT_INT_EQUALS(
126 test_error_1.error_code, error, "Expected error code %d, but was %d", test_error_1.error_code, error);
127 ASSERT_STR_EQUALS(
128 test_error_1.error_str,
129 aws_error_str(error),
130 "Expected error string %s, but got %s",
131 test_error_1.error_str,
132 aws_error_str(error));
133 ASSERT_STR_EQUALS(
134 test_error_1.lib_name,
135 aws_error_lib_name(error),
136 "Expected error libname %s, but got %s",
137 test_error_1.lib_name,
138 aws_error_lib_name(error));
139
140 return 0;
141 }
142
143 struct error_test_cb_data {
144 int global_cb_called;
145 int tl_cb_called;
146 int last_seen;
147 };
148
149 static void s_error_test_global_cb(int err, void *ctx) {
150 struct error_test_cb_data *cb_data = (struct error_test_cb_data *)ctx;
151 cb_data->global_cb_called = 1;
152 cb_data->last_seen = err;
153 }
154
155 static void s_error_test_thread_local_cb(int err, void *ctx) {
156 struct error_test_cb_data *cb_data = (struct error_test_cb_data *)ctx;
157 cb_data->tl_cb_called = 1;
158 cb_data->last_seen = err;
159 }
160
161 static int s_error_callback_test_fn(struct aws_allocator *allocator, void *ctx) {
162 (void)allocator;
163 (void)ctx;
164
165 struct error_test_cb_data cb_data = {.last_seen = 0, .global_cb_called = 0, .tl_cb_called = 0};
166
167 struct aws_error_info test_error_1 = s_errors[0];
168 struct aws_error_info test_error_2 = s_errors[1];
169
170 aws_error_handler_fn *old_fn = aws_set_global_error_handler_fn(s_error_test_global_cb, &cb_data);
171 ASSERT_NULL(old_fn, "setting the global error callback the first time should return null");
172 aws_raise_error(test_error_1.error_code);
173
174 ASSERT_NOT_NULL(cb_data.last_seen, "last error should not have been null");
175 ASSERT_TRUE(cb_data.global_cb_called, "Global Callback should have been invoked");
176 ASSERT_FALSE(cb_data.tl_cb_called, "Thread Local Callback should not have been invoked");
177
178 ASSERT_INT_EQUALS(
179 test_error_1.error_code,
180 cb_data.last_seen,
181 "Expected error code %d, but was %d",
182 test_error_1.error_code,
183 cb_data.last_seen);
184 ASSERT_STR_EQUALS(
185 test_error_1.error_str,
186 aws_error_str(cb_data.last_seen),
187 "Expected error string %s, but got %s",
188 test_error_1.error_str,
189 aws_error_str(cb_data.last_seen));
190 ASSERT_STR_EQUALS(
191 test_error_1.lib_name,
192 aws_error_lib_name(cb_data.last_seen),
193 "Expected error libname %s, but got %s",
194 test_error_1.lib_name,
195 aws_error_lib_name(cb_data.last_seen));
196
197 cb_data.last_seen = 0;
198 cb_data.global_cb_called = 0;
199 old_fn = aws_set_thread_local_error_handler_fn(s_error_test_thread_local_cb, &cb_data);
200 ASSERT_NULL(old_fn, "setting the global error callback the first time should return null");
201
202 aws_raise_error(test_error_2.error_code);
203 ASSERT_INT_EQUALS(
204 test_error_2.error_code,
205 aws_last_error(),
206 "Expected error code %d, but was %d",
207 test_error_2.error_code,
208 aws_last_error());
209
210 ASSERT_NOT_NULL(cb_data.last_seen, "last error should not have been null");
211 ASSERT_FALSE(cb_data.global_cb_called, "Global Callback should not have been invoked");
212 ASSERT_TRUE(cb_data.tl_cb_called, "Thread local Callback should have been invoked");
213
214 ASSERT_INT_EQUALS(
215 test_error_2.error_code,
216 cb_data.last_seen,
217 "Expected error code %d, but was %d",
218 test_error_2.error_code,
219 cb_data.last_seen);
220 ASSERT_STR_EQUALS(
221 test_error_2.error_str,
222 aws_error_str(cb_data.last_seen),
223 "Expected error string %s, but got %s",
224 test_error_2.error_str,
225 aws_error_str(cb_data.last_seen));
226 ASSERT_STR_EQUALS(
227 test_error_2.lib_name,
228 aws_error_lib_name(cb_data.last_seen),
229 "Expected error libname %s, but got %s",
230 test_error_2.lib_name,
231 aws_error_lib_name(cb_data.last_seen));
232
233 old_fn = aws_set_thread_local_error_handler_fn(NULL, NULL);
234 ASSERT_PTR_EQUALS(
235 s_error_test_thread_local_cb,
236 old_fn,
237 "Setting a new thread local error callback should have returned the most recent value");
238 old_fn = aws_set_global_error_handler_fn(NULL, NULL);
239 ASSERT_PTR_EQUALS(
240 s_error_test_global_cb,
241 old_fn,
242 "Setting a new global error callback should have returned the most recent value");
243
244 return 0;
245 }
246
247 static int s_unknown_error_code_in_slot_test_fn(struct aws_allocator *allocator, void *ctx) {
248 (void)allocator;
249 (void)ctx;
250
251 int error = aws_last_error();
252
253 ASSERT_NULL(error, "error should be initialized to NULL");
254 ASSERT_INT_EQUALS(0, aws_last_error(), "error code should be initialized to 0");
255
256 struct aws_error_info test_error_2 = s_errors[1];
257
258 aws_raise_error(test_error_2.error_code + 1);
259 error = aws_last_error();
260 /* error code should still propogate */
261 ASSERT_INT_EQUALS(
262 test_error_2.error_code + 1, error, "Expected error code %d, but was %d", test_error_2.error_code + 1, error);
263
264 /* string should be invalid though */
265 ASSERT_STR_EQUALS(
266 "Unknown Error Code",
267 aws_error_str(error),
268 "Expected error string %s, but got %s",
269 "Unknown Error Code",
270 aws_error_str(error));
271 ASSERT_STR_EQUALS(
272 "Unknown Error Code",
273 aws_error_lib_name(error),
274 "Expected error string %s, but got %s",
275 "Unknown Error Code",
276 aws_error_lib_name(error));
277 ASSERT_STR_EQUALS(
278 "Unknown Error Code",
279 aws_error_debug_str(error),
280 "Expected error string %s, but got %s",
281 "Unknown Error Code",
282 aws_error_debug_str(error));
283
284 return 0;
285 }
286
287 static int s_unknown_error_code_no_slot_test_fn(struct aws_allocator *allocator, void *ctx) {
288 (void)allocator;
289 (void)ctx;
290
291 int error = aws_last_error();
292
293 ASSERT_NULL(error, "error should be initialized to NULL");
294 ASSERT_INT_EQUALS(0, aws_last_error(), "error code should be initialized to 0");
295
296 int non_slotted_error_code = 3000;
297 aws_raise_error(non_slotted_error_code);
298 error = aws_last_error();
299 /* error code should still propogate */
300 ASSERT_INT_EQUALS(
301 non_slotted_error_code, error, "Expected error code %d, but was %d", non_slotted_error_code, error);
302
303 /* string should be invalid though */
304 ASSERT_STR_EQUALS(
305 "Unknown Error Code",
306 aws_error_str(error),
307 "Expected error string %s, but got %s",
308 "Unknown Error Code",
309 aws_error_str(error));
310 ASSERT_STR_EQUALS(
311 "Unknown Error Code",
312 aws_error_lib_name(error),
313 "Expected error string %s, but got %s",
314 "Unknown Error Code",
315 aws_error_lib_name(error));
316 ASSERT_STR_EQUALS(
317 "Unknown Error Code",
318 aws_error_debug_str(error),
319 "Expected error string %s, but got %s",
320 "Unknown Error Code",
321 aws_error_debug_str(error));
322
323 return 0;
324 }
325
326 static int s_unknown_error_code_range_too_large_test_fn(struct aws_allocator *allocator, void *ctx) {
327 (void)allocator;
328 (void)ctx;
329
330 int error = aws_last_error();
331
332 ASSERT_NULL(error, "error should be initialized to NULL");
333 ASSERT_INT_EQUALS(0, aws_last_error(), "error code should be initialized to 0");
334
335 int oor_error_code = 10001;
336 aws_raise_error(oor_error_code);
337 error = aws_last_error();
338 /* error code should still propogate */
339 ASSERT_INT_EQUALS(oor_error_code, error, "Expected error code %d, but was %d", oor_error_code, error);
340
341 /* string should be invalid though */
342 ASSERT_STR_EQUALS(
343 "Unknown Error Code",
344 aws_error_str(error),
345 "Expected error string %s, but got %s",
346 "Unknown Error Code",
347 aws_error_str(error));
348 ASSERT_STR_EQUALS(
349 "Unknown Error Code",
350 aws_error_lib_name(error),
351 "Expected error string %s, but got %s",
352 "Unknown Error Code",
353 aws_error_lib_name(error));
354 ASSERT_STR_EQUALS(
355 "Unknown Error Code",
356 aws_error_debug_str(error),
357 "Expected error string %s, but got %s",
358 "Unknown Error Code",
359 aws_error_debug_str(error));
360
361 return 0;
362 }
363
364 struct error_thread_test_data {
365 int thread_1_code;
366 int thread_1_get_last_code;
367 aws_thread_id_t thread_1_id;
368 int thread_1_encountered_count;
369 int thread_2_code;
370 int thread_2_get_last_code;
371 int thread_2_encountered_count;
372 aws_thread_id_t thread_2_id;
373 };
374
375 static void s_error_thread_test_thread_local_cb(int err, void *ctx) {
376 struct error_thread_test_data *cb_data = (struct error_thread_test_data *)ctx;
377
378 aws_thread_id_t thread_id = aws_thread_current_thread_id();
379
380 if (aws_thread_thread_id_equal(thread_id, cb_data->thread_1_id)) {
381 cb_data->thread_1_code = err;
382 cb_data->thread_1_get_last_code = aws_last_error();
383 cb_data->thread_1_encountered_count += 1;
384 return;
385 }
386
387 cb_data->thread_2_code = err;
388 cb_data->thread_2_get_last_code = aws_last_error();
389 cb_data->thread_2_id = aws_thread_current_thread_id();
390 cb_data->thread_2_encountered_count += 1;
391 }
392
393 static void s_error_thread_fn(void *arg) {
394 aws_set_thread_local_error_handler_fn(s_error_thread_test_thread_local_cb, arg);
395
396 aws_raise_error(15);
397 }
398
399 static int s_error_code_cross_thread_test_fn(struct aws_allocator *allocator, void *ctx) {
400 (void)ctx;
401
402 struct error_thread_test_data test_data = {
403 .thread_1_code = 0,
404 .thread_1_get_last_code = 0,
405 .thread_1_encountered_count = 0,
406 .thread_2_code = 0,
407 .thread_2_get_last_code = 0,
408 .thread_2_encountered_count = 0,
409 .thread_2_id = 0,
410 };
411
412 test_data.thread_1_id = aws_thread_current_thread_id();
413
414 aws_set_thread_local_error_handler_fn(s_error_thread_test_thread_local_cb, &test_data);
415
416 int thread_1_error_code_expected = 5;
417 aws_raise_error(thread_1_error_code_expected);
418
419 struct aws_thread thread;
420 aws_thread_init(&thread, allocator);
421 ASSERT_SUCCESS(
422 aws_thread_launch(&thread, s_error_thread_fn, &test_data, NULL),
423 "Thread creation failed with error %d",
424 aws_last_error());
425 ASSERT_SUCCESS(aws_thread_join(&thread), "Thread join failed with error %d", aws_last_error());
426 aws_thread_clean_up(&thread);
427 ASSERT_INT_EQUALS(
428 1,
429 test_data.thread_1_encountered_count,
430 "The thread local CB should only have triggered for the first thread once.");
431 ASSERT_INT_EQUALS(
432 1,
433 test_data.thread_2_encountered_count,
434 "The thread local CB should only have triggered for the second thread once.");
435 ASSERT_FALSE(test_data.thread_2_id == 0, "thread 2 id should have been set to something other than 0");
436 ASSERT_FALSE(test_data.thread_2_id == test_data.thread_1_id, "threads 1 and 2 should be different ids");
437 ASSERT_INT_EQUALS(
438 thread_1_error_code_expected,
439 aws_last_error(),
440 "Thread 1's error should not have changed when thread 2 raised an error.");
441 ASSERT_INT_EQUALS(
442 thread_1_error_code_expected, test_data.thread_1_code, "Thread 1 code should have matched the original error.");
443 ASSERT_INT_EQUALS(
444 thread_1_error_code_expected,
445 test_data.thread_1_get_last_code,
446 "Thread 1 get last error code should have matched the original error.");
447 ASSERT_INT_EQUALS(15, test_data.thread_2_code, "Thread 2 code should have matched the thread 2 error.");
448 ASSERT_INT_EQUALS(
449 15, test_data.thread_2_get_last_code, "Thread 2 get last error code should have matched the thread 2 error.");
450
451 return 0;
452 }
453
454 static int s_aws_load_error_strings_test(struct aws_allocator *allocator, void *ctx) {
455 (void)ctx;
456
457 /* Load aws-c-common's actual error info.
458 * This will fail if the error info list is out of sync with the error enums. */
459 aws_common_library_init(allocator);
460 return AWS_OP_SUCCESS;
461 }
462
463 static int s_aws_assume_compiles_test(struct aws_allocator *allocator, void *ctx) {
464 (void)allocator;
465 (void)ctx;
466
467 AWS_ASSUME(true);
468
469 if (false) {
470 AWS_UNREACHABLE();
471 }
472
473 return AWS_OP_SUCCESS;
474 }
475
476 AWS_TEST_CASE_FIXTURE(
477 raise_errors_test,
478 s_setup_errors_test_fn,
479 s_raise_errors_test_fn,
480 s_teardown_errors_test_fn,
481 NULL)
482 AWS_TEST_CASE_FIXTURE(
483 error_callback_test,
484 s_setup_errors_test_fn,
485 s_error_callback_test_fn,
486 s_teardown_errors_test_fn,
487 NULL)
488 AWS_TEST_CASE_FIXTURE(
489 reset_errors_test,
490 s_setup_errors_test_fn,
491 s_reset_errors_test_fn,
492 s_teardown_errors_test_fn,
493 NULL)
494 AWS_TEST_CASE_FIXTURE(
495 unknown_error_code_in_slot_test,
496 s_setup_errors_test_fn,
497 s_unknown_error_code_in_slot_test_fn,
498 s_teardown_errors_test_fn,
499 NULL)
500 AWS_TEST_CASE_FIXTURE(
501 unknown_error_code_no_slot_test,
502 s_setup_errors_test_fn,
503 s_unknown_error_code_no_slot_test_fn,
504 s_teardown_errors_test_fn,
505 NULL)
506 AWS_TEST_CASE_FIXTURE(
507 unknown_error_code_range_too_large_test,
508 s_setup_errors_test_fn,
509 s_unknown_error_code_range_too_large_test_fn,
510 s_teardown_errors_test_fn,
511 NULL)
512 AWS_TEST_CASE_FIXTURE(
513 error_code_cross_thread_test,
514 s_setup_errors_test_fn,
515 s_error_code_cross_thread_test_fn,
516 s_teardown_errors_test_fn,
517 NULL)
518 AWS_TEST_CASE(aws_load_error_strings_test, s_aws_load_error_strings_test)
519 AWS_TEST_CASE(aws_assume_compiles_test, s_aws_assume_compiles_test)