xref: /aosp_15_r20/external/cronet/third_party/boringssl/src/crypto/test/gtest_main.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
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 #include "../internal.h"
35 
36 
37 BSSL_NAMESPACE_BEGIN
38 
39 class TestEventListener : public testing::EmptyTestEventListener {
40  public:
41   TestEventListener() {}
42   ~TestEventListener() override {}
43 
44   void OnTestEnd(const testing::TestInfo &test_info) override {
45     if (test_info.result()->Failed()) {
46       // The test failed. Print any errors left in the error queue.
47       ERR_print_errors_fp(stdout);
48     } else {
49       // The test succeeded, so any failed operations are expected. Clear the
50       // error queue without printing.
51       ERR_clear_error();
52     }
53 
54     // Malloc failure testing is quadratic in the number of mallocs. Running
55     // multiple tests sequentially thus scales badly. Reset the malloc counter
56     // between tests. This way we will test, each test with the first allocation
57     // failing, then the second, and so on, until the test with the most
58     // allocations runs out.
59     OPENSSL_reset_malloc_counter_for_testing();
60   }
61 };
62 
63 // SetupGoogleTest should be called by the test runner after
64 // testing::InitGoogleTest has been called and before RUN_ALL_TESTS.
SetupGoogleTest()65 inline void SetupGoogleTest() {
66   CRYPTO_library_init();
67 
68 #if defined(OPENSSL_WINDOWS)
69   // Initialize Winsock.
70   WORD wsa_version = MAKEWORD(2, 2);
71   WSADATA wsa_data;
72   int wsa_err = WSAStartup(wsa_version, &wsa_data);
73   if (wsa_err != 0) {
74     fprintf(stderr, "WSAStartup failed: %d\n", wsa_err);
75     exit(1);
76   }
77   if (wsa_data.wVersion != wsa_version) {
78     fprintf(stderr, "Didn't get expected version: %x\n", wsa_data.wVersion);
79     exit(1);
80   }
81 #else
82   // Some tests create pipes. We check return values, so avoid being killed by
83   // |SIGPIPE|.
84   signal(SIGPIPE, SIG_IGN);
85 #endif
86 
87   testing::UnitTest::GetInstance()->listeners().Append(new TestEventListener);
88 }
89 
90 BSSL_NAMESPACE_END
91 
92 
93 #endif  // OPENSSL_HEADER_CRYPTO_TEST_GTEST_MAIN_H
94