blob: d21af102d412b6eb7c6dc6c0dd2f52a38148c07c [file] [log] [blame]
David Benjaminafd88c22017-04-26 11:02:43 -04001/* Copyright (c) 2017, 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#ifndef OPENSSL_HEADER_CRYPTO_TEST_GTEST_MAIN_H
16#define OPENSSL_HEADER_CRYPTO_TEST_GTEST_MAIN_H
17
18#include <stdio.h>
19#include <stdlib.h>
20
21#include <gtest/gtest.h>
22
23#include <openssl/crypto.h>
24#include <openssl/err.h>
25
26#if defined(OPENSSL_WINDOWS)
27OPENSSL_MSVC_PRAGMA(warning(push, 3))
28#include <winsock2.h>
29OPENSSL_MSVC_PRAGMA(warning(pop))
30#endif
31
32
33namespace bssl {
34
35class ErrorTestEventListener : public testing::EmptyTestEventListener {
36 public:
37 ErrorTestEventListener() {}
38 ~ErrorTestEventListener() override {}
39
40 void OnTestEnd(const testing::TestInfo &test_info) override {
David Benjaminafd88c22017-04-26 11:02:43 -040041 if (test_info.result()->Failed()) {
David Benjaminbe90bf72017-09-05 13:21:09 -040042 // The test failed. Print any errors left in the error queue.
David Benjaminafd88c22017-04-26 11:02:43 -040043 ERR_print_errors_fp(stdout);
David Benjaminbe90bf72017-09-05 13:21:09 -040044 } else {
45 // The test succeeded, so any failed operations are expected. Clear the
46 // error queue without printing.
47 ERR_clear_error();
David Benjaminafd88c22017-04-26 11:02:43 -040048 }
David Benjaminafd88c22017-04-26 11:02:43 -040049 }
50};
51
52// SetupGoogleTest should be called by the test runner after
53// testing::InitGoogleTest has been called and before RUN_ALL_TESTS.
54inline void SetupGoogleTest() {
55 CRYPTO_library_init();
56
57#if defined(OPENSSL_WINDOWS)
58 // Initialize Winsock.
59 WORD wsa_version = MAKEWORD(2, 2);
60 WSADATA wsa_data;
61 int wsa_err = WSAStartup(wsa_version, &wsa_data);
62 if (wsa_err != 0) {
63 fprintf(stderr, "WSAStartup failed: %d\n", wsa_err);
64 exit(1);
65 }
66 if (wsa_data.wVersion != wsa_version) {
67 fprintf(stderr, "Didn't get expected version: %x\n", wsa_data.wVersion);
68 exit(1);
69 }
70#endif
71
72 testing::UnitTest::GetInstance()->listeners().Append(
73 new ErrorTestEventListener);
74}
75
76} // namespace bssl
77
78
David Benjamin808f8322017-08-18 14:06:02 -040079#endif // OPENSSL_HEADER_CRYPTO_TEST_GTEST_MAIN_H