gtest_utils.h 6.2 KB

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