| 1 |
// Copyright (c) 2017, Google Inc. |
| 2 |
// SPDX-License-Identifier: ISC |
| 3 |
|
| 4 |
#ifndef OPENSSL_HEADER_CRYPTO_TEST_GTEST_MAIN_H |
| 5 |
#define OPENSSL_HEADER_CRYPTO_TEST_GTEST_MAIN_H |
| 6 |
|
| 7 |
#include <stdio.h> |
| 8 |
#include <stdlib.h> |
| 9 |
|
| 10 |
#include <gtest/gtest.h> |
| 11 |
|
| 12 |
#include <openssl/crypto.h> |
| 13 |
#include <openssl/err.h> |
| 14 |
|
| 15 |
#if defined(OPENSSL_WINDOWS) |
| 16 |
OPENSSL_MSVC_PRAGMA(warning(push, 3)) |
| 17 |
#include <winsock2.h> |
| 18 |
OPENSSL_MSVC_PRAGMA(warning(pop)) |
| 19 |
#else |
| 20 |
#include <signal.h> |
| 21 |
#endif |
| 22 |
|
| 23 |
|
| 24 |
BSSL_NAMESPACE_BEGIN |
| 25 |
|
| 26 |
class ErrorTestEventListener : public testing::EmptyTestEventListener { |
| 27 |
public: |
| 28 |
ErrorTestEventListener() {} |
| 29 |
~ErrorTestEventListener() override {} |
| 30 |
|
| 31 |
void OnTestEnd(const testing::TestInfo &test_info) override { |
| 32 |
if (test_info.result()->Failed()) { |
| 33 |
// The test failed. Print any errors left in the error queue. |
| 34 |
ERR_print_errors_fp(stdout); |
| 35 |
} else { |
| 36 |
// The test succeeded, so any failed operations are expected. Clear the |
| 37 |
// error queue without printing. |
| 38 |
ERR_clear_error(); |
| 39 |
} |
| 40 |
} |
| 41 |
}; |
| 42 |
|
| 43 |
// SetupGoogleTest should be called by the test runner after |
| 44 |
// testing::InitGoogleTest has been called and before RUN_ALL_TESTS. |
| 45 |
inline void SetupGoogleTest() { |
| 46 |
CRYPTO_library_init(); |
| 47 |
|
| 48 |
#if defined(OPENSSL_WINDOWS) |
| 49 |
// Initialize Winsock. |
| 50 |
WORD wsa_version = MAKEWORD(2, 2); |
| 51 |
WSADATA wsa_data; |
| 52 |
int wsa_err = WSAStartup(wsa_version, &wsa_data); |
| 53 |
if (wsa_err != 0) { |
| 54 |
fprintf(stderr, "WSAStartup failed: %d\n", wsa_err); |
| 55 |
exit(1); |
| 56 |
} |
| 57 |
if (wsa_data.wVersion != wsa_version) { |
| 58 |
fprintf(stderr, "Didn't get expected version: %x\n", wsa_data.wVersion); |
| 59 |
exit(1); |
| 60 |
} |
| 61 |
#else |
| 62 |
// Some tests create pipes. We check return values, so avoid being killed by |
| 63 |
// |SIGPIPE|. |
| 64 |
signal(SIGPIPE, SIG_IGN); |
| 65 |
#endif |
| 66 |
|
| 67 |
testing::UnitTest::GetInstance()->listeners().Append( |
| 68 |
new ErrorTestEventListener); |
| 69 |
} |
| 70 |
|
| 71 |
BSSL_NAMESPACE_END |
| 72 |
|
| 73 |
|
| 74 |
#endif // OPENSSL_HEADER_CRYPTO_TEST_GTEST_MAIN_H |