1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "gtest/gtest.h"
#include "gmock/gmock.h"
#include "nsIWifiListener.h"
#include "nsWifiMonitor.h"
#include "nsWifiAccessPoint.h"
#include "WifiScanner.h"
#include "nsCOMPtr.h"
#include "mozilla/Preferences.h"
#include "mozilla/Services.h"
#include "nsIObserverService.h"
#include "nsINetworkLinkService.h"
#include "mozilla/SpinEventLoopUntil.h"
#include "nsNetCID.h"
#include "nsServiceManagerUtils.h"
#if defined(XP_WIN) && defined(_M_IX86)
# include <objbase.h> // STDMETHODCALLTYPE
#endif
// Tests that wifi scanning happens on the right network change events,
// and that wifi-scan polling is operable on mobile networks.
using ::testing::AtLeast;
using ::testing::Exactly;
static mozilla::LazyLogModule gLog("TestWifiMonitor");
#define LOGI(x) MOZ_LOG(gLog, mozilla::LogLevel::Info, x)
#define LOGD(x) MOZ_LOG(gLog, mozilla::LogLevel::Debug, x)
namespace mozilla {
// Timeout if no updates are received for 1s.
static const uint32_t WIFI_SCAN_TEST_RESULT_TIMEOUT_MS = 1000;
// ID counter, used to make sure each call to GetAccessPointsFromWLAN
// returns "new" access points.
static int gCurrentId = 0;
static uint32_t gNumScanResults = 0;
template <typename T>
bool IsNonZero(const T&) {
return true;
}
template <>
bool IsNonZero<int>(const int& aVal) {
return aVal != 0;
}
struct LinkTypeMobility {
const char* mLinkType;
bool mIsMobile;
};
class MockWifiScanner : public WifiScanner {
public:
MOCK_METHOD(nsresult, GetAccessPointsFromWLAN,
(nsTArray<RefPtr<nsIWifiAccessPoint>> & aAccessPoints),
(override));
};
class MockWifiListener : public nsIWifiListener {
virtual ~MockWifiListener() = default;
public:
NS_DECL_THREADSAFE_ISUPPORTS
#if defined(XP_WIN) && defined(_M_IX86)
MOCK_METHOD(nsresult, OnChange,
(const nsTArray<RefPtr<nsIWifiAccessPoint>>& accessPoints),
(override, Calltype(STDMETHODCALLTYPE)));
MOCK_METHOD(nsresult, OnError, (nsresult error),
(override, Calltype(STDMETHODCALLTYPE)));
#else
MOCK_METHOD(nsresult, OnChange,
(const nsTArray<RefPtr<nsIWifiAccessPoint>>& accessPoints),
(override));
MOCK_METHOD(nsresult, OnError, (nsresult error), (override));
#endif
};
NS_IMPL_ISUPPORTS(MockWifiListener, nsIWifiListener)
class TestWifiMonitor : public ::testing::Test {
public:
TestWifiMonitor() {
mObs = mozilla::services::GetObserverService();
MOZ_ASSERT(mObs);
nsresult rv;
nsCOMPtr<nsINetworkLinkService> nls =
do_GetService(NS_NETWORK_LINK_SERVICE_CONTRACTID, &rv);
EXPECT_TRUE(NS_SUCCEEDED(rv));
EXPECT_TRUE(nls);
rv = nls->GetLinkType(&mOrigLinkType);
EXPECT_TRUE(NS_SUCCEEDED(rv));
rv = nls->GetIsLinkUp(&mOrigIsLinkUp);
EXPECT_TRUE(NS_SUCCEEDED(rv));
rv = nls->GetLinkStatusKnown(&mOrigLinkStatusKnown);
EXPECT_TRUE(NS_SUCCEEDED(rv));
// Reduce wifi-polling interval. 0 turns polling off.
mOldScanInterval = Preferences::GetInt(WIFI_SCAN_INTERVAL_MS_PREF);
Preferences::SetInt(WIFI_SCAN_INTERVAL_MS_PREF, 10);
}
~TestWifiMonitor() {
Preferences::SetInt(WIFI_SCAN_INTERVAL_MS_PREF, mOldScanInterval);
// Restore network link type
const char* linkType = nullptr;
switch (mOrigLinkType) {
case nsINetworkLinkService::LINK_TYPE_UNKNOWN:
linkType = NS_NETWORK_LINK_TYPE_UNKNOWN;
break;
case nsINetworkLinkService::LINK_TYPE_ETHERNET:
linkType = NS_NETWORK_LINK_TYPE_ETHERNET;
break;
case nsINetworkLinkService::LINK_TYPE_USB:
linkType = NS_NETWORK_LINK_TYPE_USB;
break;
case nsINetworkLinkService::LINK_TYPE_WIFI:
linkType = NS_NETWORK_LINK_TYPE_WIFI;
break;
case nsINetworkLinkService::LINK_TYPE_MOBILE:
linkType = NS_NETWORK_LINK_TYPE_MOBILE;
break;
case nsINetworkLinkService::LINK_TYPE_WIMAX:
linkType = NS_NETWORK_LINK_TYPE_WIMAX;
break;
}
EXPECT_TRUE(linkType);
mObs->NotifyObservers(nullptr, NS_NETWORK_LINK_TYPE_TOPIC,
NS_ConvertUTF8toUTF16(linkType).get());
const char* linkStatus = nullptr;
if (mOrigLinkStatusKnown) {
if (mOrigIsLinkUp) {
linkStatus = NS_NETWORK_LINK_DATA_UP;
} else {
linkStatus = NS_NETWORK_LINK_DATA_DOWN;
}
} else {
linkStatus = NS_NETWORK_LINK_DATA_UNKNOWN;
}
EXPECT_TRUE(linkStatus);
mObs->NotifyObservers(nullptr, NS_NETWORK_LINK_TOPIC,
NS_ConvertUTF8toUTF16(linkStatus).get());
}
void SetUp() override {
mWifiMonitor = MakeRefPtr<nsWifiMonitor>(MakeUnique<MockWifiScanner>());
EXPECT_TRUE(!mWifiMonitor->IsPolling());
mWifiListener = new MockWifiListener();
LOGI(("monitor: %p | scanner: %p | listener: %p", mWifiMonitor.get(),
mWifiMonitor->mWifiScanner.get(), mWifiListener.get()));
}
void TearDown() override {
::testing::Mock::VerifyAndClearExpectations(
mWifiMonitor->mWifiScanner.get());
::testing::Mock::VerifyAndClearExpectations(mWifiListener.get());
// Manually disconnect observers so that the monitor can be destroyed.
// This would normally be done on xpcom-shutdown but that is sent after
// the tests run, which is too late to avoid a gtest memory-leak error.
mWifiMonitor->Close();
mWifiMonitor = nullptr;
mWifiListener = nullptr;
gCurrentId = 0;
}
protected:
// Fn must be the type of a parameterless function that returns a boolean
// indicating success.
template <typename Card1, typename Card2, typename Card3, typename Fn>
void CheckAsync(Card1&& nScans, Card2&& nChanges, Card3&& nErrors, Fn fn) {
// Only add WillRepeatedly handler if scans is more than 0, to avoid a
// VERY LOUD gtest warning.
if (IsNonZero(nScans)) {
EXPECT_CALL(
*static_cast<MockWifiScanner*>(mWifiMonitor->mWifiScanner.get()),
GetAccessPointsFromWLAN)
.Times(nScans)
.WillRepeatedly(
[](nsTArray<RefPtr<nsIWifiAccessPoint>>& aAccessPoints) {
EXPECT_TRUE(!NS_IsMainThread());
EXPECT_TRUE(aAccessPoints.IsEmpty());
nsWifiAccessPoint* ap = new nsWifiAccessPoint();
// Signal will be unique so we won't match the prior access
// point list.
ap->mSignal = gCurrentId++;
aAccessPoints.AppendElement(RefPtr(ap));
return NS_OK;
});
} else {
EXPECT_CALL(
*static_cast<MockWifiScanner*>(mWifiMonitor->mWifiScanner.get()),
GetAccessPointsFromWLAN)
.Times(nScans);
}
if (IsNonZero(nChanges)) {
EXPECT_CALL(*mWifiListener, OnChange)
.Times(nChanges)
.WillRepeatedly(
[](const nsTArray<RefPtr<nsIWifiAccessPoint>>& aAccessPoints) {
EXPECT_TRUE(NS_IsMainThread());
EXPECT_EQ(aAccessPoints.Length(), 1u);
++gNumScanResults;
return NS_OK;
});
} else {
EXPECT_CALL(*mWifiListener, OnChange).Times(nChanges);
}
if (IsNonZero(nErrors)) {
EXPECT_CALL(*mWifiListener, OnError)
.Times(nErrors)
.WillRepeatedly([](nsresult aError) {
EXPECT_TRUE(NS_IsMainThread());
return NS_OK;
});
} else {
EXPECT_CALL(*mWifiListener, OnError).Times(nErrors);
}
EXPECT_TRUE(fn());
}
void CheckStartWatching(bool aShouldPoll) {
LOGI(("CheckStartWatching | aShouldPoll: %s",
aShouldPoll ? "true" : "false"));
CheckAsync(aShouldPoll ? AtLeast(1) : Exactly(1) /* nScans */,
aShouldPoll ? AtLeast(1) : Exactly(1) /* nChanges */,
0 /* nErrors */, [&] {
return NS_SUCCEEDED(mWifiMonitor->StartWatching(
mWifiListener, aShouldPoll)) &&
WaitForScanResult(1);
});
}
void CheckStopWatching() {
LOGI(("CheckStopWatching"));
CheckAsync(0 /* nScans */, 0 /* nChanges */, 0 /* nErrors */, [&] {
return NS_SUCCEEDED(mWifiMonitor->StopWatching(mWifiListener));
});
}
void CheckNotifyAndNoScanResults(const char* aTopic, const char16_t* aData) {
LOGI(("CheckNotifyAndNoScanResults: (%s, %s)", aTopic,
NS_ConvertUTF16toUTF8(aData).get()));
CheckAsync(0 /* nScans */, 0 /* nChanges */, 0 /* nErrors */, [&] {
return NS_SUCCEEDED(mObs->NotifyObservers(nullptr, aTopic, aData));
});
}
void CheckNotifyAndWaitForScanResult(const char* aTopic,
const char16_t* aData) {
LOGI(("CheckNotifyAndWaitForScanResult: (%s, %s)", aTopic,
NS_ConvertUTF16toUTF8(aData).get()));
CheckAsync(
AtLeast(1) /* nScans */, AtLeast(1) /* nChanges */, 0 /* nErrors */,
[&] {
return NS_SUCCEEDED(mObs->NotifyObservers(nullptr, aTopic, aData)) &&
WaitForScanResult(1);
});
}
void CheckNotifyAndWaitForPollingScans(const char* aTopic,
const char16_t* aData) {
LOGI(("CheckNotifyAndWaitForPollingScans: (%s, %s)", aTopic,
NS_ConvertUTF16toUTF8(aData).get()));
CheckAsync(
AtLeast(2) /* nScans */, AtLeast(2) /* nChanges */, 0 /* nErrors */,
[&] {
// Wait for more than one scan.
return NS_SUCCEEDED(mObs->NotifyObservers(nullptr, aTopic, aData)) &&
WaitForScanResult(2);
});
}
void CheckMessages(bool aShouldPoll) {
// NS_NETWORK_LINK_TOPIC messages should cause a new scan.
const char* kLinkTopicDatas[] = {
NS_NETWORK_LINK_DATA_UP, NS_NETWORK_LINK_DATA_DOWN,
NS_NETWORK_LINK_DATA_CHANGED, NS_NETWORK_LINK_DATA_UNKNOWN};
auto checkFn = aShouldPoll
? &TestWifiMonitor::CheckNotifyAndWaitForPollingScans
: &TestWifiMonitor::CheckNotifyAndWaitForScanResult;
for (const auto& data : kLinkTopicDatas) {
CheckStartWatching(aShouldPoll);
EXPECT_EQ(mWifiMonitor->IsPolling(), aShouldPoll);
(this->*checkFn)(NS_NETWORK_LINK_TOPIC,
NS_ConvertUTF8toUTF16(data).get());
EXPECT_EQ(mWifiMonitor->IsPolling(), aShouldPoll);
CheckStopWatching();
EXPECT_TRUE(!mWifiMonitor->IsPolling());
}
// NS_NETWORK_LINK_TYPE_TOPIC should cause wifi scan polling iff the topic
// says we have switched to a mobile network (LINK_TYPE_MOBILE or
// LINK_TYPE_WIMAX) or we are polling the wifi-scanner (aShouldPoll).
const LinkTypeMobility kLinkTypeTopicDatas[] = {
{NS_NETWORK_LINK_TYPE_UNKNOWN, false /* mIsMobile */},
{NS_NETWORK_LINK_TYPE_ETHERNET, false},
{NS_NETWORK_LINK_TYPE_USB, false},
{NS_NETWORK_LINK_TYPE_WIFI, false},
{NS_NETWORK_LINK_TYPE_WIMAX, true},
{NS_NETWORK_LINK_TYPE_MOBILE, true}};
for (const auto& data : kLinkTypeTopicDatas) {
bool shouldPoll = (aShouldPoll || data.mIsMobile);
checkFn = shouldPoll ? &TestWifiMonitor::CheckNotifyAndWaitForPollingScans
: &TestWifiMonitor::CheckNotifyAndNoScanResults;
CheckStartWatching(aShouldPoll);
(this->*checkFn)(NS_NETWORK_LINK_TYPE_TOPIC,
NS_ConvertUTF8toUTF16(data.mLinkType).get());
EXPECT_EQ(mWifiMonitor->IsPolling(), shouldPoll);
CheckStopWatching();
EXPECT_TRUE(!mWifiMonitor->IsPolling());
}
}
bool WaitForScanResult(uint32_t nScansToWaitFor) {
uint32_t oldScanResults = gNumScanResults;
auto startTime = TimeStamp::Now();
return mozilla::SpinEventLoopUntil(
"TestWifiMonitor::WaitForScanResult"_ns, [&]() {
LOGD(
("gNumScanResults: %d | oldScanResults: %d | "
"nScansToWaitFor: %d | timeMs: %d",
(int)gNumScanResults, (int)oldScanResults, (int)nScansToWaitFor,
(int)(TimeStamp::Now() - startTime).ToMilliseconds()));
return gNumScanResults >= (oldScanResults + nScansToWaitFor) ||
(TimeStamp::Now() - startTime).ToMilliseconds() >
WIFI_SCAN_TEST_RESULT_TIMEOUT_MS * nScansToWaitFor;
});
}
RefPtr<nsWifiMonitor> mWifiMonitor;
nsCOMPtr<nsIObserverService> mObs;
RefPtr<MockWifiListener> mWifiListener;
int mOldScanInterval;
uint32_t mOrigLinkType = 0;
bool mOrigIsLinkUp = false;
bool mOrigLinkStatusKnown = false;
};
TEST_F(TestWifiMonitor, WifiScanNoPolling) { CheckMessages(false); }
TEST_F(TestWifiMonitor, WifiScanPolling) { CheckMessages(true); }
} // namespace mozilla
|