]>
Commit | Line | Data |
---|---|---|
2de108df DB |
1 | /* |
2 | * Copyright 2018 The OpenSSL Project Authors. All Rights Reserved. | |
3 | * | |
909f1a2e | 4 | * Licensed under the Apache License 2.0 (the "License"). You may not use |
2de108df DB |
5 | * this file except in compliance with the License. You can obtain a copy |
6 | * in the file LICENSE in the source distribution or at | |
7 | * https://www.openssl.org/source/license.html | |
8 | */ | |
9 | ||
10 | #include <openssl/opensslconf.h> | |
11 | #include <openssl/err.h> | |
12 | ||
13 | #include "testutil.h" | |
14 | ||
15 | #if defined(OPENSSL_SYS_WINDOWS) | |
16 | # include <windows.h> | |
17 | #else | |
18 | # include <errno.h> | |
19 | #endif | |
20 | ||
21 | /* Test that querying the error queue preserves the OS error. */ | |
22 | static int preserves_system_error(void) | |
23 | { | |
24 | #if defined(OPENSSL_SYS_WINDOWS) | |
25 | SetLastError(ERROR_INVALID_FUNCTION); | |
26 | ERR_get_error(); | |
27 | return TEST_int_eq(GetLastError(), ERROR_INVALID_FUNCTION); | |
28 | #else | |
29 | errno = EINVAL; | |
30 | ERR_get_error(); | |
31 | return TEST_int_eq(errno, EINVAL); | |
32 | #endif | |
33 | } | |
34 | ||
8908d18c RS |
35 | /* Test that calls to ERR_add_error_[v]data append */ |
36 | static int vdata_appends(void) | |
37 | { | |
38 | const char *data; | |
39 | ||
40 | CRYPTOerr(0, ERR_R_MALLOC_FAILURE); | |
41 | ERR_add_error_data(1, "hello "); | |
42 | ERR_add_error_data(1, "world"); | |
43 | ERR_get_error_line_data(NULL, NULL, &data, NULL); | |
44 | return TEST_str_eq(data, "hello world"); | |
45 | } | |
46 | ||
8648a50a | 47 | static int raised_error(void) |
56c3a135 | 48 | { |
b9a75806 | 49 | const char *f, *data; |
56c3a135 RS |
50 | int l; |
51 | unsigned long e; | |
8648a50a RL |
52 | |
53 | /* | |
54 | * When OPENSSL_NO_ERR or OPENSSL_NO_FILENAMES, no file name or line | |
55 | * number is saved, so no point checking them. | |
56 | */ | |
57 | #if !defined(OPENSSL_NO_FILENAMES) && !defined(OPENSSL_NO_ERR) | |
b9a75806 MC |
58 | const char *file; |
59 | int line; | |
56c3a135 | 60 | |
add8c8e9 | 61 | file = __FILE__; |
b9a75806 MC |
62 | line = __LINE__ + 2; /* The error is generated on the ERR_raise_data line */ |
63 | #endif | |
ff988500 RS |
64 | ERR_raise_data(ERR_LIB_SYS, ERR_R_INTERNAL_ERROR, |
65 | "calling exit()"); | |
56c3a135 RS |
66 | if (!TEST_ulong_ne(e = ERR_get_error_line_data(&f, &l, &data, NULL), 0) |
67 | || !TEST_int_eq(ERR_GET_REASON(e), ERR_R_INTERNAL_ERROR) | |
8648a50a | 68 | #if !defined(OPENSSL_NO_FILENAMES) && !defined(OPENSSL_NO_ERR) |
56c3a135 RS |
69 | || !TEST_int_eq(l, line) |
70 | || !TEST_str_eq(f, file) | |
b9a75806 | 71 | #endif |
ff988500 | 72 | || !TEST_str_eq(data, "calling exit()")) |
56c3a135 RS |
73 | return 0; |
74 | return 1; | |
75 | } | |
76 | ||
2de108df DB |
77 | int setup_tests(void) |
78 | { | |
79 | ADD_TEST(preserves_system_error); | |
8908d18c | 80 | ADD_TEST(vdata_appends); |
8648a50a | 81 | ADD_TEST(raised_error); |
2de108df DB |
82 | return 1; |
83 | } |