1 /* Copyright (c) 2016, 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 #include <gtest/gtest.h>
16
17 #include <stdio.h>
18
19 #include <openssl/err.h>
20 #include <openssl/crypto.h>
21
22 #if defined(OPENSSL_WINDOWS)
23 OPENSSL_MSVC_PRAGMA(warning(push, 3))
24 #include <winsock2.h>
25 OPENSSL_MSVC_PRAGMA(warning(pop))
26 #endif
27
28
29 namespace {
30
31 class ErrorTestEventListener : public testing::EmptyTestEventListener {
32 public:
ErrorTestEventListener()33 ErrorTestEventListener() {}
~ErrorTestEventListener()34 ~ErrorTestEventListener() override {}
35
OnTestEnd(const testing::TestInfo & test_info)36 void OnTestEnd(const testing::TestInfo &test_info) override {
37 // If the test failed, print any errors left in the error queue.
38 if (test_info.result()->Failed()) {
39 ERR_print_errors_fp(stdout);
40 }
41
42 // Clean up the error queue for the next run.
43 ERR_clear_error();
44 }
45 };
46
47 } // namespace
48
main(int argc,char ** argv)49 int main(int argc, char **argv) {
50 CRYPTO_library_init();
51
52 #if defined(OPENSSL_WINDOWS)
53 // Initialize Winsock.
54 WORD wsa_version = MAKEWORD(2, 2);
55 WSADATA wsa_data;
56 int wsa_err = WSAStartup(wsa_version, &wsa_data);
57 if (wsa_err != 0) {
58 fprintf(stderr, "WSAStartup failed: %d\n", wsa_err);
59 return 1;
60 }
61 if (wsa_data.wVersion != wsa_version) {
62 fprintf(stderr, "Didn't get expected version: %x\n", wsa_data.wVersion);
63 return 1;
64 }
65 #endif
66
67 testing::InitGoogleTest(&argc, argv);
68 testing::UnitTest::GetInstance()->listeners().Append(
69 new ErrorTestEventListener);
70 return RUN_ALL_TESTS();
71 }
72