gtest_utils.h (7246B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=2 et sw=2 tw=80: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this file, 5 * You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 // Utilities to wrap gtest, based on libjingle's gunit 8 9 // Some sections of this code are under the following license: 10 11 /* 12 * libjingle 13 * Copyright 2004--2008, Google Inc. 14 * 15 * Redistribution and use in source and binary forms, with or without 16 * modification, are permitted provided that the following conditions are met: 17 * 18 * 1. Redistributions of source code must retain the above copyright notice, 19 * this list of conditions and the following disclaimer. 20 * 2. Redistributions in binary form must reproduce the above copyright notice, 21 * this list of conditions and the following disclaimer in the documentation 22 * and/or other materials provided with the distribution. 23 * 3. The name of the author may not be used to endorse or promote products 24 * derived from this software without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 27 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 28 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO 29 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 30 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 31 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 32 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 33 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 34 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 35 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 36 */ 37 38 // Original author: ekr@rtfm.com 39 #ifndef gtest_utils__h__ 40 #define gtest_utils__h__ 41 42 #include <iostream> 43 44 #include "nspr.h" 45 #include "prinrval.h" 46 #include "prthread.h" 47 48 #define GTEST_HAS_RTTI 0 49 #include "gtest/gtest.h" 50 #include "gtest_ringbuffer_dumper.h" 51 #include "mtransport_test_utils.h" 52 #include "nss.h" 53 #include "ssl.h" 54 55 extern "C" { 56 #include "registry.h" 57 #include "transport_addr.h" 58 } 59 60 // Wait up to timeout seconds for expression to be true 61 #define WAIT(expression, timeout) \ 62 do { \ 63 for (PRIntervalTime start = PR_IntervalNow(); \ 64 !(expression) && !((PR_IntervalNow() - start) > \ 65 PR_MillisecondsToInterval(timeout));) { \ 66 PR_Sleep(10); \ 67 } \ 68 } while (0) 69 70 // Same as GTEST_WAIT, but stores the result in res. Used when 71 // you also want the result of expression but wish to avoid 72 // double evaluation. 73 #define WAIT_(expression, timeout, res) \ 74 do { \ 75 for (PRIntervalTime start = PR_IntervalNow(); \ 76 !(res = (expression)) && !((PR_IntervalNow() - start) > \ 77 PR_MillisecondsToInterval(timeout));) { \ 78 PR_Sleep(10); \ 79 } \ 80 } while (0) 81 82 #define ASSERT_TRUE_WAIT(expression, timeout) \ 83 do { \ 84 bool res; \ 85 WAIT_(expression, timeout, res); \ 86 ASSERT_TRUE(res); \ 87 } while (0) 88 89 #define EXPECT_TRUE_WAIT(expression, timeout) \ 90 do { \ 91 bool res; \ 92 WAIT_(expression, timeout, res); \ 93 EXPECT_TRUE(res); \ 94 } while (0) 95 96 #define ASSERT_EQ_WAIT(expected, actual, timeout) \ 97 do { \ 98 WAIT(expected == actual, timeout); \ 99 ASSERT_EQ(expected, actual); \ 100 } while (0) 101 102 using test::RingbufferDumper; 103 104 class MtransportTest : public ::testing::Test { 105 public: 106 MtransportTest() : test_utils_(nullptr), dumper_(nullptr) {} 107 108 void SetUp() override { 109 test_utils_ = new MtransportTestUtils(); 110 NSS_NoDB_Init(nullptr); 111 NSS_SetDomesticPolicy(); 112 113 NR_reg_init(); 114 115 // Attempt to load env vars used by tests. 116 GetEnvironment("TURN_SERVER_ADDRESS", turn_server_); 117 GetEnvironment("TURN_SERVER_USER", turn_user_); 118 GetEnvironment("TURN_SERVER_PASSWORD", turn_password_); 119 GetEnvironment("STUN_SERVER_ADDRESS", stun_server_address_); 120 GetEnvironment("STUN_SERVER_HOSTNAME", stun_server_hostname_); 121 122 std::string disable_non_local; 123 GetEnvironment("MOZ_DISABLE_NONLOCAL_CONNECTIONS", disable_non_local); 124 std::string upload_dir; 125 GetEnvironment("MOZ_UPLOAD_DIR", upload_dir); 126 127 if ((!disable_non_local.empty() && disable_non_local != "0") || 128 !upload_dir.empty()) { 129 // We're assuming that MOZ_UPLOAD_DIR is only set on tbpl; 130 // MOZ_DISABLE_NONLOCAL_CONNECTIONS probably should be set when running 131 // the cpp unit-tests, but is not presently. 132 stun_server_address_ = ""; 133 stun_server_hostname_ = ""; 134 turn_server_ = ""; 135 } 136 137 // Some tests are flaky and need to check if they're supposed to run. 138 webrtc_enabled_ = CheckEnvironmentFlag("MOZ_WEBRTC_TESTS"); 139 140 ::testing::TestEventListeners& listeners = 141 ::testing::UnitTest::GetInstance()->listeners(); 142 143 dumper_ = new RingbufferDumper(test_utils_); 144 listeners.Append(dumper_); 145 } 146 147 void TearDown() override { 148 ::testing::UnitTest::GetInstance()->listeners().Release(dumper_); 149 delete dumper_; 150 delete test_utils_; 151 } 152 153 void GetEnvironment(const char* aVar, std::string& out) { 154 char* value = getenv(aVar); 155 if (value) { 156 out = value; 157 } 158 } 159 160 bool CheckEnvironmentFlag(const char* aVar) { 161 std::string value; 162 GetEnvironment(aVar, value); 163 return value == "1"; 164 } 165 166 bool WarnIfTurnNotConfigured() const { 167 bool configured = 168 !turn_server_.empty() && !turn_user_.empty() && !turn_password_.empty(); 169 170 if (configured) { 171 nr_transport_addr addr; 172 if (nr_str_port_to_transport_addr(turn_server_.c_str(), 3478, IPPROTO_UDP, 173 &addr)) { 174 printf( 175 "Invalid TURN_SERVER_ADDRESS \"%s\". Only IP numbers supported.\n", 176 turn_server_.c_str()); 177 configured = false; 178 } 179 } else { 180 printf( 181 "Set TURN_SERVER_ADDRESS, TURN_SERVER_USER, and " 182 "TURN_SERVER_PASSWORD\n" 183 "environment variables to run this test\n"); 184 } 185 186 return !configured; 187 } 188 189 MtransportTestUtils* test_utils_; 190 RingbufferDumper* dumper_; 191 192 std::string turn_server_; 193 std::string turn_user_; 194 std::string turn_password_; 195 std::string stun_server_address_; 196 std::string stun_server_hostname_; 197 198 bool webrtc_enabled_; 199 }; 200 #endif