Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/media/mtransport/test/gtest_utils.h
Line
Count
Source (jump to first uncovered line)
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
51
#include "gtest_ringbuffer_dumper.h"
52
#include "mtransport_test_utils.h"
53
#include "nss.h"
54
#include "ssl.h"
55
56
extern "C" {
57
#include "registry.h"
58
#include "transport_addr.h"
59
}
60
61
// Wait up to timeout seconds for expression to be true
62
#define WAIT(expression, timeout) \
63
0
  do { \
64
0
    for (PRIntervalTime start = PR_IntervalNow(); !(expression) && \
65
0
           ! ((PR_IntervalNow() - start) > PR_MillisecondsToInterval(timeout));) { \
66
0
      PR_Sleep(10); \
67
0
    } \
68
0
  } 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
0
  do { \
75
0
    for (PRIntervalTime start = PR_IntervalNow(); !(res = (expression)) && \
76
0
           ! ((PR_IntervalNow() - start) > PR_MillisecondsToInterval(timeout));) { \
77
0
      PR_Sleep(10); \
78
0
    } \
79
0
  } while(0)
80
81
#define ASSERT_TRUE_WAIT(expression, timeout) \
82
0
  do { \
83
0
    bool res; \
84
0
    WAIT_(expression, timeout, res); \
85
0
    ASSERT_TRUE(res); \
86
0
  } while(0)
87
88
#define EXPECT_TRUE_WAIT(expression, timeout) \
89
0
  do { \
90
0
    bool res; \
91
0
    WAIT_(expression, timeout, res); \
92
0
    EXPECT_TRUE(res); \
93
0
  } while(0)
94
95
#define ASSERT_EQ_WAIT(expected, actual, timeout) \
96
0
  do { \
97
0
    WAIT(expected == actual, timeout); \
98
0
    ASSERT_EQ(expected, actual); \
99
0
  } while(0)
100
101
using test::RingbufferDumper;
102
103
class MtransportTest : public ::testing::Test {
104
public:
105
  MtransportTest()
106
    : test_utils_(nullptr)
107
    , dumper_(nullptr)
108
0
  {
109
0
  }
110
111
0
  void SetUp() override {
112
0
    test_utils_ = new MtransportTestUtils();
113
0
    NSS_NoDB_Init(nullptr);
114
0
    NSS_SetDomesticPolicy();
115
0
116
0
    NR_reg_init(NR_REG_MODE_LOCAL);
117
0
118
0
    // Attempt to load env vars used by tests.
119
0
    GetEnvironment("TURN_SERVER_ADDRESS", turn_server_);
120
0
    GetEnvironment("TURN_SERVER_USER", turn_user_);
121
0
    GetEnvironment("TURN_SERVER_PASSWORD", turn_password_);
122
0
    GetEnvironment("STUN_SERVER_ADDRESS", stun_server_address_);
123
0
    GetEnvironment("STUN_SERVER_HOSTNAME", stun_server_hostname_);
124
0
125
0
    std::string disable_non_local;
126
0
    GetEnvironment("MOZ_DISABLE_NONLOCAL_CONNECTIONS", disable_non_local);
127
0
    std::string upload_dir;
128
0
    GetEnvironment("MOZ_UPLOAD_DIR", upload_dir);
129
0
130
0
    if ((!disable_non_local.empty() && disable_non_local != "0") ||
131
0
        !upload_dir.empty()) {
132
0
      // We're assuming that MOZ_UPLOAD_DIR is only set on tbpl;
133
0
      // MOZ_DISABLE_NONLOCAL_CONNECTIONS probably should be set when running the
134
0
      // cpp unit-tests, but is not presently.
135
0
      stun_server_address_ = "";
136
0
      stun_server_hostname_ = "";
137
0
      turn_server_ = "";
138
0
    }
139
0
140
0
    // Some tests are flaky and need to check if they're supposed to run.
141
0
    webrtc_enabled_ = CheckEnvironmentFlag("MOZ_WEBRTC_TESTS");
142
0
143
0
    ::testing::TestEventListeners& listeners =
144
0
        ::testing::UnitTest::GetInstance()->listeners();
145
0
146
0
    dumper_ = new RingbufferDumper(test_utils_);
147
0
    listeners.Append(dumper_);
148
0
  }
149
150
0
  void TearDown() override {
151
0
    ::testing::UnitTest::GetInstance()->listeners().Release(dumper_);
152
0
    delete dumper_;
153
0
    delete test_utils_;
154
0
  }
155
156
0
  void GetEnvironment(const char* aVar, std::string& out) {
157
0
    char* value = getenv(aVar);
158
0
    if (value) {
159
0
      out = value;
160
0
    }
161
0
  }
162
163
0
  bool CheckEnvironmentFlag(const char* aVar) {
164
0
    std::string value;
165
0
    GetEnvironment(aVar, value);
166
0
    return value == "1";
167
0
  }
168
169
0
  bool WarnIfTurnNotConfigured() const {
170
0
    bool configured =
171
0
        !turn_server_.empty() &&
172
0
        !turn_user_.empty() &&
173
0
        !turn_password_.empty();
174
0
175
0
    if (configured) {
176
0
      nr_transport_addr addr;
177
0
      if (nr_str_port_to_transport_addr(turn_server_.c_str(), 3478,
178
0
                                        IPPROTO_UDP, &addr)) {
179
0
        printf("Invalid TURN_SERVER_ADDRESS \"%s\". Only IP numbers supported.\n",
180
0
               turn_server_.c_str());
181
0
        configured = false;
182
0
      }
183
0
    } else {
184
0
      printf(
185
0
        "Set TURN_SERVER_ADDRESS, TURN_SERVER_USER, and TURN_SERVER_PASSWORD\n"
186
0
        "environment variables to run this test\n");
187
0
    }
188
0
189
0
    return !configured;
190
0
  }
191
192
  MtransportTestUtils* test_utils_;
193
  RingbufferDumper* dumper_;
194
195
  std::string turn_server_;
196
  std::string turn_user_;
197
  std::string turn_password_;
198
  std::string stun_server_address_;
199
  std::string stun_server_hostname_;
200
201
  bool webrtc_enabled_;
202
};
203
#endif