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 <gtest/gtest.h>
19 
20 #include <openssl/crypto.h>
21 #include <openssl/err.h>
22 #include <openssl/mem.h>
23 
24 #include "./internal.h"
25 
26 #if defined(OPENSSL_WINDOWS)
27 OPENSSL_MSVC_PRAGMA(warning(push, 3))
28 #include <windows.h>
OPENSSL_MSVC_PRAGMA(warning (pop))29 OPENSSL_MSVC_PRAGMA(warning(pop))
30 #else
31 #include <errno.h>
32 #endif
33 
34 
35 TEST(ErrTest, Overflow) {
36   for (unsigned i = 0; i < ERR_NUM_ERRORS*2; i++) {
37     ERR_put_error(1, 0 /* unused */, i+1, "test", 1);
38   }
39 
40   for (unsigned i = 0; i < ERR_NUM_ERRORS - 1; i++) {
41     SCOPED_TRACE(i);
42     uint32_t err = ERR_get_error();
43     // Errors are returned in order they were pushed, with the least recent ones
44     // removed, up to |ERR_NUM_ERRORS - 1| errors. So the errors returned are
45     // |ERR_NUM_ERRORS + 2| through |ERR_NUM_ERRORS * 2|, inclusive.
46     EXPECT_NE(0u, err);
47     EXPECT_EQ(static_cast<int>(i + ERR_NUM_ERRORS + 2), ERR_GET_REASON(err));
48   }
49 
50   EXPECT_EQ(0u, ERR_get_error());
51 }
52 
TEST(ErrTest,PutError)53 TEST(ErrTest, PutError) {
54   ASSERT_EQ(0u, ERR_get_error())
55       << "ERR_get_error returned value before an error was added.";
56 
57   ERR_put_error(1, 0 /* unused */, 2, "test", 4);
58   ERR_add_error_data(1, "testing");
59 
60   int peeked_line, line, peeked_flags, flags;
61   const char *peeked_file, *file, *peeked_data, *data;
62   uint32_t peeked_packed_error =
63       ERR_peek_error_line_data(&peeked_file, &peeked_line, &peeked_data,
64                                &peeked_flags);
65   uint32_t packed_error = ERR_get_error_line_data(&file, &line, &data, &flags);
66 
67   EXPECT_EQ(peeked_packed_error, packed_error);
68   EXPECT_EQ(peeked_file, file);
69   EXPECT_EQ(peeked_data, data);
70   EXPECT_EQ(peeked_flags, flags);
71 
72   EXPECT_STREQ("test", file);
73   EXPECT_EQ(4, line);
74   EXPECT_TRUE(flags & ERR_FLAG_STRING);
75   EXPECT_EQ(1, ERR_GET_LIB(packed_error));
76   EXPECT_EQ(2, ERR_GET_REASON(packed_error));
77   EXPECT_STREQ("testing", data);
78 }
79 
TEST(ErrTest,ClearError)80 TEST(ErrTest, ClearError) {
81   ASSERT_EQ(0u, ERR_get_error())
82       << "ERR_get_error returned value before an error was added.";
83 
84   ERR_put_error(1, 0 /* unused */, 2, "test", 4);
85   ERR_clear_error();
86 
87   // The error queue should be cleared.
88   EXPECT_EQ(0u, ERR_get_error());
89 }
90 
TEST(ErrTest,Print)91 TEST(ErrTest, Print) {
92   ERR_put_error(1, 0 /* unused */, 2, "test", 4);
93   ERR_add_error_data(1, "testing");
94   uint32_t packed_error = ERR_get_error();
95 
96   char buf[256];
97   for (size_t i = 0; i <= sizeof(buf); i++) {
98     ERR_error_string_n(packed_error, buf, i);
99   }
100 }
101 
TEST(ErrTest,Release)102 TEST(ErrTest, Release) {
103   ERR_put_error(1, 0 /* unused */, 2, "test", 4);
104   ERR_remove_thread_state(NULL);
105 
106   // The error queue should be cleared.
107   EXPECT_EQ(0u, ERR_get_error());
108 }
109 
HasSuffix(const char * str,const char * suffix)110 static bool HasSuffix(const char *str, const char *suffix) {
111   size_t suffix_len = strlen(suffix);
112   size_t str_len = strlen(str);
113   if (str_len < suffix_len) {
114     return false;
115   }
116   return strcmp(str + str_len - suffix_len, suffix) == 0;
117 }
118 
TEST(ErrTest,PutMacro)119 TEST(ErrTest, PutMacro) {
120   int expected_line = __LINE__ + 1;
121   OPENSSL_PUT_ERROR(USER, ERR_R_INTERNAL_ERROR);
122 
123   int line;
124   const char *file;
125   uint32_t error = ERR_get_error_line(&file, &line);
126 
127   EXPECT_PRED2(HasSuffix, file, "err_test.cc");
128   EXPECT_EQ(expected_line, line);
129   EXPECT_EQ(ERR_LIB_USER, ERR_GET_LIB(error));
130   EXPECT_EQ(ERR_R_INTERNAL_ERROR, ERR_GET_REASON(error));
131 }
132 
TEST(ErrTest,SaveAndRestore)133 TEST(ErrTest, SaveAndRestore) {
134   // Restoring no state clears the error queue, including error data.
135   ERR_put_error(1, 0 /* unused */, 1, "test1.c", 1);
136   ERR_put_error(2, 0 /* unused */, 2, "test2.c", 2);
137   ERR_add_error_data(1, "data1");
138   ERR_restore_state(nullptr);
139   EXPECT_EQ(0u, ERR_get_error());
140 
141   // Add some entries to the error queue and save it.
142   ERR_put_error(1, 0 /* unused */, 1, "test1.c", 1);
143   ERR_add_error_data(1, "data1");
144   ERR_put_error(2, 0 /* unused */, 2, "test2.c", 2);
145   ERR_put_error(3, 0 /* unused */, 3, "test3.c", 3);
146   ERR_add_error_data(1, "data3");
147   bssl::UniquePtr<ERR_SAVE_STATE> saved(ERR_save_state());
148   ASSERT_TRUE(saved);
149 
150   // The existing error queue entries still exist.
151   int line, flags;
152   const char *file, *data;
153   uint32_t packed_error = ERR_get_error_line_data(&file, &line, &data, &flags);
154   EXPECT_EQ(ERR_GET_LIB(packed_error), 1);
155   EXPECT_EQ(ERR_GET_REASON(packed_error), 1);
156   EXPECT_STREQ("test1.c", file);
157   EXPECT_EQ(line, 1);
158   EXPECT_STREQ(data, "data1");
159   EXPECT_EQ(flags, ERR_FLAG_STRING);
160 
161   // The state may be restored, both over an empty and non-empty state.
162   for (unsigned i = 0; i < 2; i++) {
163     SCOPED_TRACE(i);
164     ERR_restore_state(saved.get());
165 
166     packed_error = ERR_get_error_line_data(&file, &line, &data, &flags);
167     EXPECT_EQ(ERR_GET_LIB(packed_error), 1);
168     EXPECT_EQ(ERR_GET_REASON(packed_error), 1);
169     EXPECT_STREQ("test1.c", file);
170     EXPECT_EQ(line, 1);
171     EXPECT_STREQ(data, "data1");
172     EXPECT_EQ(flags, ERR_FLAG_STRING);
173 
174     packed_error = ERR_get_error_line_data(&file, &line, &data, &flags);
175     EXPECT_EQ(ERR_GET_LIB(packed_error), 2);
176     EXPECT_EQ(ERR_GET_REASON(packed_error), 2);
177     EXPECT_STREQ("test2.c", file);
178     EXPECT_EQ(line, 2);
179     EXPECT_STREQ(data, "");  // No error data is reported as the empty string.
180     EXPECT_EQ(flags, 0);
181 
182     packed_error = ERR_get_error_line_data(&file, &line, &data, &flags);
183     EXPECT_EQ(ERR_GET_LIB(packed_error), 3);
184     EXPECT_EQ(ERR_GET_REASON(packed_error), 3);
185     EXPECT_STREQ("test3.c", file);
186     EXPECT_EQ(line, 3);
187     EXPECT_STREQ(data, "data3");
188     EXPECT_EQ(flags, ERR_FLAG_STRING);
189 
190     // The error queue is now empty for the next iteration.
191     EXPECT_EQ(0u, ERR_get_error());
192   }
193 
194   // Test a case where the error queue wraps around. The first set of errors
195   // will all be discarded, but result in wrapping the list around.
196   ERR_clear_error();
197   for (unsigned i = 0; i < ERR_NUM_ERRORS / 2; i++) {
198     ERR_put_error(0, 0 /* unused */, 0, "invalid", 0);
199   }
200   for (unsigned i = 1; i < ERR_NUM_ERRORS; i++) {
201     ERR_put_error(i, 0 /* unused */, i, "test", i);
202   }
203   saved.reset(ERR_save_state());
204 
205   // The state may be restored, both over an empty and non-empty state. Pop one
206   // error off so the first iteration is tested to not be a no-op.
207   ERR_get_error();
208   for (int i = 0; i < 2; i++) {
209     SCOPED_TRACE(i);
210     ERR_restore_state(saved.get());
211     for (int j = 1; j < ERR_NUM_ERRORS; j++) {
212       SCOPED_TRACE(j);
213       packed_error = ERR_get_error_line_data(&file, &line, &data, &flags);
214       EXPECT_EQ(ERR_GET_LIB(packed_error), j);
215       EXPECT_EQ(ERR_GET_REASON(packed_error), j);
216       EXPECT_STREQ("test", file);
217       EXPECT_EQ(line, j);
218     }
219     // The error queue is now empty for the next iteration.
220     EXPECT_EQ(0u, ERR_get_error());
221   }
222 }
223 
224 // Querying the error queue should not affect the OS error.
225 #if defined(OPENSSL_WINDOWS)
TEST(ErrTest,PreservesLastError)226 TEST(ErrTest, PreservesLastError) {
227   SetLastError(ERROR_INVALID_FUNCTION);
228   ERR_get_error();
229   EXPECT_EQ(static_cast<DWORD>(ERROR_INVALID_FUNCTION), GetLastError());
230 }
231 #else
TEST(ErrTest,PreservesErrno)232 TEST(ErrTest, PreservesErrno) {
233   errno = EINVAL;
234   ERR_get_error();
235   EXPECT_EQ(EINVAL, errno);
236 }
237 #endif
238