• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* 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)
27 OPENSSL_MSVC_PRAGMA(warning(push, 3))
28 #include <winsock2.h>
OPENSSL_MSVC_PRAGMA(warning (pop))29 OPENSSL_MSVC_PRAGMA(warning(pop))
30 #else
31 #include <signal.h>
32 #endif
33 
34 
35 BSSL_NAMESPACE_BEGIN
36 
37 class ErrorTestEventListener : public testing::EmptyTestEventListener {
38  public:
39   ErrorTestEventListener() {}
40   ~ErrorTestEventListener() override {}
41 
42   void OnTestEnd(const testing::TestInfo &test_info) override {
43     if (test_info.result()->Failed()) {
44       // The test failed. Print any errors left in the error queue.
45       ERR_print_errors_fp(stdout);
46     } else {
47       // The test succeeded, so any failed operations are expected. Clear the
48       // error queue without printing.
49       ERR_clear_error();
50     }
51   }
52 };
53 
54 // SetupGoogleTest should be called by the test runner after
55 // testing::InitGoogleTest has been called and before RUN_ALL_TESTS.
SetupGoogleTest()56 inline void SetupGoogleTest() {
57   CRYPTO_library_init();
58 
59 #if defined(OPENSSL_WINDOWS)
60   // Initialize Winsock.
61   WORD wsa_version = MAKEWORD(2, 2);
62   WSADATA wsa_data;
63   int wsa_err = WSAStartup(wsa_version, &wsa_data);
64   if (wsa_err != 0) {
65     fprintf(stderr, "WSAStartup failed: %d\n", wsa_err);
66     exit(1);
67   }
68   if (wsa_data.wVersion != wsa_version) {
69     fprintf(stderr, "Didn't get expected version: %x\n", wsa_data.wVersion);
70     exit(1);
71   }
72 #else
73   // Some tests create pipes. We check return values, so avoid being killed by
74   // |SIGPIPE|.
75   signal(SIGPIPE, SIG_IGN);
76 #endif
77 
78   testing::UnitTest::GetInstance()->listeners().Append(
79       new ErrorTestEventListener);
80 }
81 
82 BSSL_NAMESPACE_END
83 
84 
85 #endif  // OPENSSL_HEADER_CRYPTO_TEST_GTEST_MAIN_H
86