Convert err_test to GTest.

BUG=129

Change-Id: I227ffa2da4e220075de296fb5b94d043f4e032e0
Reviewed-on: https://boringssl-review.googlesource.com/13627
Reviewed-by: Steven Valdez <svaldez@google.com>
Reviewed-by: David Benjamin <davidben@google.com>
Commit-Queue: David Benjamin <davidben@google.com>
CQ-Verified: CQ bot account: commit-bot@chromium.org <commit-bot@chromium.org>
This commit is contained in:
David Benjamin 2017-02-05 03:01:25 -05:00 committed by CQ bot account: commit-bot@chromium.org
parent 908ac19e8e
commit bc6ef7a83f
4 changed files with 36 additions and 88 deletions

View File

@ -214,6 +214,7 @@ add_executable(
dh/dh_test.cc dh/dh_test.cc
dsa/dsa_test.cc dsa/dsa_test.cc
err/err_test.cc
$<TARGET_OBJECTS:gtest_main> $<TARGET_OBJECTS:gtest_main>
$<TARGET_OBJECTS:test_support> $<TARGET_OBJECTS:test_support>

View File

@ -37,14 +37,3 @@ add_library(
err.c err.c
err_data.c err_data.c
) )
add_executable(
err_test
err_test.cc
$<TARGET_OBJECTS:test_support>
)
target_link_libraries(err_test crypto)
add_dependencies(all_tests err_test)

View File

@ -15,40 +15,34 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <gtest/gtest.h>
#include <openssl/crypto.h> #include <openssl/crypto.h>
#include <openssl/err.h> #include <openssl/err.h>
#include <openssl/mem.h> #include <openssl/mem.h>
static bool TestOverflow() { TEST(ErrTest, Overflow) {
for (unsigned i = 0; i < ERR_NUM_ERRORS*2; i++) { for (unsigned i = 0; i < ERR_NUM_ERRORS*2; i++) {
ERR_put_error(1, 0 /* unused */, i+1, "test", 1); ERR_put_error(1, 0 /* unused */, i+1, "test", 1);
} }
for (unsigned i = 0; i < ERR_NUM_ERRORS - 1; i++) { for (unsigned i = 0; i < ERR_NUM_ERRORS - 1; i++) {
SCOPED_TRACE(i);
uint32_t err = ERR_get_error(); uint32_t err = ERR_get_error();
/* Errors are returned in order they were pushed, with the least recent ones /* 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 * removed, up to |ERR_NUM_ERRORS - 1| errors. So the errors returned are
* |ERR_NUM_ERRORS + 2| through |ERR_NUM_ERRORS * 2|, inclusive. */ * |ERR_NUM_ERRORS + 2| through |ERR_NUM_ERRORS * 2|, inclusive. */
if (err == 0 || ((unsigned)ERR_GET_REASON(err)) != i + ERR_NUM_ERRORS + 2) { EXPECT_NE(0u, err);
fprintf(stderr, "ERR_get_error failed at %u\n", i); EXPECT_EQ(static_cast<int>(i + ERR_NUM_ERRORS + 2), ERR_GET_REASON(err));
return false;
}
} }
if (ERR_get_error() != 0) { EXPECT_EQ(0u, ERR_get_error());
fprintf(stderr, "ERR_get_error more than the expected number of values.\n");
return false;
} }
return true; TEST(ErrTest, PutError) {
} ASSERT_EQ(0u, ERR_get_error())
<< "ERR_get_error returned value before an error was added.";
static bool TestPutError() {
if (ERR_get_error() != 0) {
fprintf(stderr, "ERR_get_error returned value before an error was added.\n");
return false;
}
ERR_put_error(1, 0 /* unused */, 2, "test", 4); ERR_put_error(1, 0 /* unused */, 2, "test", 4);
ERR_add_error_data(1, "testing"); ERR_add_error_data(1, "testing");
@ -60,45 +54,31 @@ static bool TestPutError() {
&peeked_flags); &peeked_flags);
uint32_t packed_error = ERR_get_error_line_data(&file, &line, &data, &flags); uint32_t packed_error = ERR_get_error_line_data(&file, &line, &data, &flags);
if (peeked_packed_error != packed_error || EXPECT_EQ(peeked_packed_error, packed_error);
peeked_file != file || EXPECT_EQ(peeked_file, file);
peeked_data != data || EXPECT_EQ(peeked_data, data);
peeked_flags != flags) { EXPECT_EQ(peeked_flags, flags);
fprintf(stderr, "Bad peeked error data returned.\n");
return false; 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);
} }
if (strcmp(file, "test") != 0 || TEST(ErrTest, ClearError) {
line != 4 || ASSERT_EQ(0u, ERR_get_error())
(flags & ERR_FLAG_STRING) == 0 || << "ERR_get_error returned value before an error was added.";
ERR_GET_LIB(packed_error) != 1 ||
ERR_GET_REASON(packed_error) != 2 ||
strcmp(data, "testing") != 0) {
fprintf(stderr, "Bad error data returned.\n");
return false;
}
return true;
}
static bool TestClearError() {
if (ERR_get_error() != 0) {
fprintf(stderr, "ERR_get_error returned value before an error was added.\n");
return false;
}
ERR_put_error(1, 0 /* unused */, 2, "test", 4); ERR_put_error(1, 0 /* unused */, 2, "test", 4);
ERR_clear_error(); ERR_clear_error();
if (ERR_get_error() != 0) { // The error queue should be cleared.
fprintf(stderr, "Error remained after clearing.\n"); EXPECT_EQ(0u, ERR_get_error());
return false;
} }
return true; TEST(ErrTest, Print) {
}
static bool TestPrint() {
ERR_put_error(1, 0 /* unused */, 2, "test", 4); ERR_put_error(1, 0 /* unused */, 2, "test", 4);
ERR_add_error_data(1, "testing"); ERR_add_error_data(1, "testing");
uint32_t packed_error = ERR_get_error(); uint32_t packed_error = ERR_get_error();
@ -107,14 +87,14 @@ static bool TestPrint() {
for (size_t i = 0; i <= sizeof(buf); i++) { for (size_t i = 0; i <= sizeof(buf); i++) {
ERR_error_string_n(packed_error, buf, i); ERR_error_string_n(packed_error, buf, i);
} }
return true;
} }
static bool TestRelease() { TEST(ErrTest, Release) {
ERR_put_error(1, 0 /* unused */, 2, "test", 4); ERR_put_error(1, 0 /* unused */, 2, "test", 4);
ERR_remove_thread_state(NULL); ERR_remove_thread_state(NULL);
return true;
// The error queue should be cleared.
EXPECT_EQ(0u, ERR_get_error());
} }
static bool HasSuffix(const char *str, const char *suffix) { static bool HasSuffix(const char *str, const char *suffix) {
@ -126,7 +106,7 @@ static bool HasSuffix(const char *str, const char *suffix) {
return strcmp(str + str_len - suffix_len, suffix) == 0; return strcmp(str + str_len - suffix_len, suffix) == 0;
} }
static bool TestPutMacro() { TEST(ErrTest, PutMacro) {
int expected_line = __LINE__ + 1; int expected_line = __LINE__ + 1;
OPENSSL_PUT_ERROR(USER, ERR_R_INTERNAL_ERROR); OPENSSL_PUT_ERROR(USER, ERR_R_INTERNAL_ERROR);
@ -134,29 +114,8 @@ static bool TestPutMacro() {
const char *file; const char *file;
uint32_t error = ERR_get_error_line(&file, &line); uint32_t error = ERR_get_error_line(&file, &line);
if (!HasSuffix(file, "err_test.cc") || EXPECT_PRED2(HasSuffix, file, "err_test.cc");
line != expected_line || EXPECT_EQ(expected_line, line);
ERR_GET_LIB(error) != ERR_LIB_USER || EXPECT_EQ(ERR_LIB_USER, ERR_GET_LIB(error));
ERR_GET_REASON(error) != ERR_R_INTERNAL_ERROR) { EXPECT_EQ(ERR_R_INTERNAL_ERROR, ERR_GET_REASON(error));
fprintf(stderr, "Bad error data returned.\n");
return false;
}
return true;
}
int main() {
CRYPTO_library_init();
if (!TestOverflow() ||
!TestPutError() ||
!TestClearError() ||
!TestPrint() ||
!TestRelease() ||
!TestPutMacro()) {
return 1;
}
printf("PASS\n");
return 0;
} }

View File

@ -40,7 +40,6 @@
["crypto/ecdsa/ecdsa_sign_test", "crypto/ecdsa/ecdsa_sign_tests.txt"], ["crypto/ecdsa/ecdsa_sign_test", "crypto/ecdsa/ecdsa_sign_tests.txt"],
["crypto/ecdsa/ecdsa_test"], ["crypto/ecdsa/ecdsa_test"],
["crypto/ecdsa/ecdsa_verify_test", "crypto/ecdsa/ecdsa_verify_tests.txt"], ["crypto/ecdsa/ecdsa_verify_test", "crypto/ecdsa/ecdsa_verify_tests.txt"],
["crypto/err/err_test"],
["crypto/evp/evp_extra_test"], ["crypto/evp/evp_extra_test"],
["crypto/evp/evp_test", "crypto/evp/evp_tests.txt"], ["crypto/evp/evp_test", "crypto/evp/evp_tests.txt"],
["crypto/evp/pbkdf_test"], ["crypto/evp/pbkdf_test"],