2014-06-20 20:00:00 +01:00
|
|
|
/* Copyright (c) 2014, Google Inc.
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
|
|
|
|
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
|
|
|
|
* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
|
|
|
|
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2015-01-31 01:08:37 +00:00
|
|
|
#include <string.h>
|
2014-06-20 20:00:00 +01:00
|
|
|
|
2017-02-05 08:01:25 +00:00
|
|
|
#include <gtest/gtest.h>
|
|
|
|
|
2014-09-12 00:11:15 +01:00
|
|
|
#include <openssl/crypto.h>
|
2014-06-20 20:00:00 +01:00
|
|
|
#include <openssl/err.h>
|
|
|
|
#include <openssl/mem.h>
|
|
|
|
|
|
|
|
|
2017-02-05 08:01:25 +00:00
|
|
|
TEST(ErrTest, Overflow) {
|
2015-03-26 05:46:52 +00:00
|
|
|
for (unsigned i = 0; i < ERR_NUM_ERRORS*2; i++) {
|
2015-10-28 22:03:21 +00:00
|
|
|
ERR_put_error(1, 0 /* unused */, i+1, "test", 1);
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|
|
|
|
|
2015-03-26 05:46:52 +00:00
|
|
|
for (unsigned i = 0; i < ERR_NUM_ERRORS - 1; i++) {
|
2017-02-05 08:01:25 +00:00
|
|
|
SCOPED_TRACE(i);
|
2015-01-06 16:49:25 +00:00
|
|
|
uint32_t err = ERR_get_error();
|
2017-08-18 19:06:02 +01:00
|
|
|
// Errors are returned in order they were pushed, with the least recent ones
|
|
|
|
// removed, up to |ERR_NUM_ERRORS - 1| errors. So the errors returned are
|
|
|
|
// |ERR_NUM_ERRORS + 2| through |ERR_NUM_ERRORS * 2|, inclusive.
|
2017-02-05 08:01:25 +00:00
|
|
|
EXPECT_NE(0u, err);
|
|
|
|
EXPECT_EQ(static_cast<int>(i + ERR_NUM_ERRORS + 2), ERR_GET_REASON(err));
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|
|
|
|
|
2017-02-05 08:01:25 +00:00
|
|
|
EXPECT_EQ(0u, ERR_get_error());
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|
|
|
|
|
2017-02-05 08:01:25 +00:00
|
|
|
TEST(ErrTest, PutError) {
|
|
|
|
ASSERT_EQ(0u, ERR_get_error())
|
|
|
|
<< "ERR_get_error returned value before an error was added.";
|
2014-06-20 20:00:00 +01:00
|
|
|
|
2015-10-28 22:03:21 +00:00
|
|
|
ERR_put_error(1, 0 /* unused */, 2, "test", 4);
|
2014-06-20 20:00:00 +01:00
|
|
|
ERR_add_error_data(1, "testing");
|
|
|
|
|
2015-03-26 05:46:52 +00:00
|
|
|
int peeked_line, line, peeked_flags, flags;
|
|
|
|
const char *peeked_file, *file, *peeked_data, *data;
|
|
|
|
uint32_t peeked_packed_error =
|
|
|
|
ERR_peek_error_line_data(&peeked_file, &peeked_line, &peeked_data,
|
|
|
|
&peeked_flags);
|
|
|
|
uint32_t packed_error = ERR_get_error_line_data(&file, &line, &data, &flags);
|
2014-11-08 17:51:36 +00:00
|
|
|
|
2017-02-05 08:01:25 +00:00
|
|
|
EXPECT_EQ(peeked_packed_error, packed_error);
|
|
|
|
EXPECT_EQ(peeked_file, file);
|
|
|
|
EXPECT_EQ(peeked_data, data);
|
|
|
|
EXPECT_EQ(peeked_flags, flags);
|
|
|
|
|
|
|
|
EXPECT_STREQ("test", file);
|
|
|
|
EXPECT_EQ(4, line);
|
|
|
|
EXPECT_TRUE(flags & ERR_FLAG_STRING);
|
|
|
|
EXPECT_EQ(1, ERR_GET_LIB(packed_error));
|
|
|
|
EXPECT_EQ(2, ERR_GET_REASON(packed_error));
|
|
|
|
EXPECT_STREQ("testing", data);
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|
|
|
|
|
2017-02-05 08:01:25 +00:00
|
|
|
TEST(ErrTest, ClearError) {
|
|
|
|
ASSERT_EQ(0u, ERR_get_error())
|
|
|
|
<< "ERR_get_error returned value before an error was added.";
|
2014-06-20 20:00:00 +01:00
|
|
|
|
2015-10-28 22:03:21 +00:00
|
|
|
ERR_put_error(1, 0 /* unused */, 2, "test", 4);
|
2014-06-20 20:00:00 +01:00
|
|
|
ERR_clear_error();
|
|
|
|
|
2017-02-05 08:01:25 +00:00
|
|
|
// The error queue should be cleared.
|
|
|
|
EXPECT_EQ(0u, ERR_get_error());
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|
|
|
|
|
2017-02-05 08:01:25 +00:00
|
|
|
TEST(ErrTest, Print) {
|
2015-10-28 22:03:21 +00:00
|
|
|
ERR_put_error(1, 0 /* unused */, 2, "test", 4);
|
2014-06-20 20:00:00 +01:00
|
|
|
ERR_add_error_data(1, "testing");
|
2015-03-26 05:46:52 +00:00
|
|
|
uint32_t packed_error = ERR_get_error();
|
2014-06-20 20:00:00 +01:00
|
|
|
|
2015-03-26 05:46:52 +00:00
|
|
|
char buf[256];
|
|
|
|
for (size_t i = 0; i <= sizeof(buf); i++) {
|
2014-06-20 20:00:00 +01:00
|
|
|
ERR_error_string_n(packed_error, buf, i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-05 08:01:25 +00:00
|
|
|
TEST(ErrTest, Release) {
|
2015-10-28 22:03:21 +00:00
|
|
|
ERR_put_error(1, 0 /* unused */, 2, "test", 4);
|
2014-08-06 00:54:47 +01:00
|
|
|
ERR_remove_thread_state(NULL);
|
2017-02-05 08:01:25 +00:00
|
|
|
|
|
|
|
// The error queue should be cleared.
|
|
|
|
EXPECT_EQ(0u, ERR_get_error());
|
2014-08-06 00:54:47 +01:00
|
|
|
}
|
|
|
|
|
2015-06-29 04:36:21 +01:00
|
|
|
static bool HasSuffix(const char *str, const char *suffix) {
|
|
|
|
size_t suffix_len = strlen(suffix);
|
|
|
|
size_t str_len = strlen(str);
|
|
|
|
if (str_len < suffix_len) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return strcmp(str + str_len - suffix_len, suffix) == 0;
|
|
|
|
}
|
|
|
|
|
2017-02-05 08:01:25 +00:00
|
|
|
TEST(ErrTest, PutMacro) {
|
2015-06-29 04:36:21 +01:00
|
|
|
int expected_line = __LINE__ + 1;
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(USER, ERR_R_INTERNAL_ERROR);
|
2015-06-29 04:36:21 +01:00
|
|
|
|
|
|
|
int line;
|
|
|
|
const char *file;
|
|
|
|
uint32_t error = ERR_get_error_line(&file, &line);
|
|
|
|
|
2017-02-05 08:01:25 +00:00
|
|
|
EXPECT_PRED2(HasSuffix, file, "err_test.cc");
|
|
|
|
EXPECT_EQ(expected_line, line);
|
|
|
|
EXPECT_EQ(ERR_LIB_USER, ERR_GET_LIB(error));
|
|
|
|
EXPECT_EQ(ERR_R_INTERNAL_ERROR, ERR_GET_REASON(error));
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|