1 /* Copyright (c) 2014, Google Inc.
2  *
3  * Permission to use, copy, modify, and/or distribute this software for any
4  * purpose with or without fee is hereby granted, provided that the above
5  * copyright notice and this permission notice appear in all copies.
6  *
7  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
10  * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
14 
15 #include <stdio.h>
16 #include <string.h>
17 
18 #include <openssl/crypto.h>
19 #include <openssl/err.h>
20 #include <openssl/mem.h>
21 
22 
TestOverflow()23 static bool TestOverflow() {
24   for (unsigned i = 0; i < ERR_NUM_ERRORS*2; i++) {
25     ERR_put_error(1, 0 /* unused */, i+1, "test", 1);
26   }
27 
28   for (unsigned i = 0; i < ERR_NUM_ERRORS - 1; i++) {
29     uint32_t err = ERR_get_error();
30     /* Errors are returned in order they were pushed, with the least recent ones
31      * removed, up to |ERR_NUM_ERRORS - 1| errors. So the errors returned are
32      * |ERR_NUM_ERRORS + 2| through |ERR_NUM_ERRORS * 2|, inclusive. */
33     if (err == 0 || ((unsigned)ERR_GET_REASON(err)) != i + ERR_NUM_ERRORS + 2) {
34       fprintf(stderr, "ERR_get_error failed at %u\n", i);
35       return false;
36     }
37   }
38 
39   if (ERR_get_error() != 0) {
40     fprintf(stderr, "ERR_get_error more than the expected number of values.\n");
41     return false;
42   }
43 
44   return true;
45 }
46 
TestPutError()47 static bool TestPutError() {
48   if (ERR_get_error() != 0) {
49     fprintf(stderr, "ERR_get_error returned value before an error was added.\n");
50     return false;
51   }
52 
53   ERR_put_error(1, 0 /* unused */, 2, "test", 4);
54   ERR_add_error_data(1, "testing");
55 
56   int peeked_line, line, peeked_flags, flags;
57   const char *peeked_file, *file, *peeked_data, *data;
58   uint32_t peeked_packed_error =
59       ERR_peek_error_line_data(&peeked_file, &peeked_line, &peeked_data,
60                                &peeked_flags);
61   uint32_t packed_error = ERR_get_error_line_data(&file, &line, &data, &flags);
62 
63   if (peeked_packed_error != packed_error ||
64       peeked_file != file ||
65       peeked_data != data ||
66       peeked_flags != flags) {
67     fprintf(stderr, "Bad peeked error data returned.\n");
68     return false;
69   }
70 
71   if (strcmp(file, "test") != 0 ||
72       line != 4 ||
73       (flags & ERR_FLAG_STRING) == 0 ||
74       ERR_GET_LIB(packed_error) != 1 ||
75       ERR_GET_REASON(packed_error) != 2 ||
76       strcmp(data, "testing") != 0) {
77     fprintf(stderr, "Bad error data returned.\n");
78     return false;
79   }
80 
81   return true;
82 }
83 
TestClearError()84 static bool TestClearError() {
85   if (ERR_get_error() != 0) {
86     fprintf(stderr, "ERR_get_error returned value before an error was added.\n");
87     return false;
88   }
89 
90   ERR_put_error(1, 0 /* unused */, 2, "test", 4);
91   ERR_clear_error();
92 
93   if (ERR_get_error() != 0) {
94     fprintf(stderr, "Error remained after clearing.\n");
95     return false;
96   }
97 
98   return true;
99 }
100 
TestPrint()101 static bool TestPrint() {
102   ERR_put_error(1, 0 /* unused */, 2, "test", 4);
103   ERR_add_error_data(1, "testing");
104   uint32_t packed_error = ERR_get_error();
105 
106   char buf[256];
107   for (size_t i = 0; i <= sizeof(buf); i++) {
108     ERR_error_string_n(packed_error, buf, i);
109   }
110 
111   return true;
112 }
113 
TestRelease()114 static bool TestRelease() {
115   ERR_put_error(1, 0 /* unused */, 2, "test", 4);
116   ERR_remove_thread_state(NULL);
117   return true;
118 }
119 
HasSuffix(const char * str,const char * suffix)120 static bool HasSuffix(const char *str, const char *suffix) {
121   size_t suffix_len = strlen(suffix);
122   size_t str_len = strlen(str);
123   if (str_len < suffix_len) {
124     return false;
125   }
126   return strcmp(str + str_len - suffix_len, suffix) == 0;
127 }
128 
TestPutMacro()129 static bool TestPutMacro() {
130   int expected_line = __LINE__ + 1;
131   OPENSSL_PUT_ERROR(USER, ERR_R_INTERNAL_ERROR);
132 
133   int line;
134   const char *file;
135   uint32_t error = ERR_get_error_line(&file, &line);
136 
137   if (!HasSuffix(file, "err_test.cc") ||
138       line != expected_line ||
139       ERR_GET_LIB(error) != ERR_LIB_USER ||
140       ERR_GET_REASON(error) != ERR_R_INTERNAL_ERROR) {
141     fprintf(stderr, "Bad error data returned.\n");
142     return false;
143   }
144 
145   return true;
146 }
147 
main()148 int main() {
149   CRYPTO_library_init();
150 
151   if (!TestOverflow() ||
152       !TestPutError() ||
153       !TestClearError() ||
154       !TestPrint() ||
155       !TestRelease() ||
156       !TestPutMacro()) {
157     return 1;
158   }
159 
160   printf("PASS\n");
161   return 0;
162 }
163