| 6 | // |
| 7 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 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 | #include <winsock2.h> |
| 28 | #else |
| 29 | #include <signal.h> |
| 30 | #endif |
| 31 |
| 32 | #include "../internal.h" |
| 33 |
| 34 |
| 35 | BSSL_NAMESPACE_BEGIN |
| 36 |
| 37 | class TestEventListener : public testing::EmptyTestEventListener { |
| 38 | public: |
| 39 | TestEventListener() {} |
| 40 | ~TestEventListener() 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 | // Malloc failure testing is quadratic in the number of mallocs. Running |
| 53 | // multiple tests sequentially thus scales badly. Reset the malloc counter |
| 54 | // between tests. This way we will test, each test with the first allocation |
| 55 | // failing, then the second, and so on, until the test with the most |
| 56 | // allocations runs out. |
| 57 | OPENSSL_reset_malloc_counter_for_testing(); |
| 58 | } |
| 59 | }; |
| 60 |
| 61 | // SetupGoogleTest should be called by the test runner after |
| 62 | // testing::InitGoogleTest has been called and before RUN_ALL_TESTS. |
| 63 | inline void SetupGoogleTest() { |
| 64 | #if defined(OPENSSL_WINDOWS) |
| 65 | // Initialize Winsock. |
| 66 | WORD wsa_version = MAKEWORD(2, 2); |
| 67 | WSADATA wsa_data; |
| 68 | int wsa_err = WSAStartup(wsa_version, &wsa_data); |
| 69 | if (wsa_err != 0) { |
| 70 | fprintf(stderr, "WSAStartup failed: %d\n", wsa_err); |
| 71 | exit(1); |
| 72 | } |
| 73 | if (wsa_data.wVersion != wsa_version) { |
| 74 | fprintf(stderr, "Didn't get expected version: %x\n", wsa_data.wVersion); |
| 75 | exit(1); |
| 76 | } |
| 77 | #else |
| 78 | // Some tests create pipes. We check return values, so avoid being killed by |
| 79 | // |SIGPIPE|. |
| 80 | signal(SIGPIPE, SIG_IGN); |
| 81 | #endif |
| 82 |
| 83 | testing::UnitTest::GetInstance()->listeners().Append(new TestEventListener); |
| 84 | } |
| 85 |
| 86 | BSSL_NAMESPACE_END |
| 87 |
| 88 |
| 89 | #endif // OPENSSL_HEADER_CRYPTO_TEST_GTEST_MAIN_H |
| 90 |