1 //
2 // Copyright (C) 2014 The Android Open Source Project
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16
17 #include "shill/cellular/mobile_operator_info.h"
18
19 #include <fstream>
20 #include <map>
21 #include <ostream>
22 #include <set>
23 #include <vector>
24
25 #include <base/files/file_util.h>
26 #include <base/macros.h>
27 #include <gmock/gmock.h>
28 #include <gtest/gtest.h>
29
30 #include "shill/cellular/mobile_operator_info_impl.h"
31 #include "shill/logging.h"
32 #include "shill/test_event_dispatcher.h"
33
34 // These files contain binary protobuf definitions used by the following tests
35 // inside the namespace ::mobile_operator_db
36 #define IN_MOBILE_OPERATOR_INFO_UNITTEST_CC
37 #include "shill/mobile_operator_db/test_protos/data_test.h"
38 #include "shill/mobile_operator_db/test_protos/init_test_empty_db_init.h"
39 #include "shill/mobile_operator_db/test_protos/init_test_multiple_db_init_1.h"
40 #include "shill/mobile_operator_db/test_protos/init_test_multiple_db_init_2.h"
41 #include "shill/mobile_operator_db/test_protos/init_test_successful_init.h"
42 #include "shill/mobile_operator_db/test_protos/main_test.h"
43 #undef IN_MOBILE_OPERATOR_INFO_UNITTEST_CC
44
45 using base::FilePath;
46 using shill::mobile_operator_db::MobileOperatorDB;
47 using std::map;
48 using std::ofstream;
49 using std::set;
50 using std::string;
51 using std::vector;
52 using testing::Mock;
53 using testing::Test;
54 using testing::Values;
55 using testing::WithParamInterface;
56
57 // The tests run from the fixture |MobileOperatorInfoMainTest| and
58 // |MobileOperatorDataTest| can be run in two modes:
59 // - strict event checking: We check that an event is raised for each update
60 // to the state of the object.
61 // - non-strict event checking: We check that a single event is raised as a
62 // result of many updates to the object.
63 // The first case corresponds to a very aggressive event loop, that dispatches
64 // events as soon as they are posted; the second one corresponds to an
65 // over-crowded event loop that only dispatches events just before we verify
66 // that events were raised.
67 //
68 // We use ::testing::WithParamInterface to templatize the test fixtures to do
69 // string/non-strict event checking. When writing test cases using these
70 // fixtures, use the |Update*|, |ExpectEventCount|, |VerifyEventCount| functions
71 // provided by the fixture, and write the test as if event checking is strict.
72 //
73 // For |MobileOperatorObserverTest|, only the strict event checking case makes
74 // sense, so we only instantiate that.
75 namespace shill {
76
77 namespace {
78
79 enum EventCheckingPolicy {
80 kEventCheckingPolicyStrict,
81 kEventCheckingPolicyNonStrict
82 };
83
84 } // namespace
85
86 class MockMobileOperatorInfoObserver : public MobileOperatorInfo::Observer {
87 public:
MockMobileOperatorInfoObserver()88 MockMobileOperatorInfoObserver() {}
~MockMobileOperatorInfoObserver()89 virtual ~MockMobileOperatorInfoObserver() {}
90
91 MOCK_METHOD0(OnOperatorChanged, void());
92 };
93
94 class MobileOperatorInfoInitTest : public Test {
95 public:
MobileOperatorInfoInitTest()96 MobileOperatorInfoInitTest()
97 : operator_info_(new MobileOperatorInfo(&dispatcher_, "Operator")),
98 operator_info_impl_(operator_info_->impl()) {}
99
TearDown()100 void TearDown() override {
101 for (const auto& tmp_db_path : tmp_db_paths_) {
102 base::DeleteFile(tmp_db_path, false);
103 }
104 }
105
106 protected:
AddDatabase(const unsigned char database_data[],size_t num_elems)107 void AddDatabase(const unsigned char database_data[], size_t num_elems) {
108 FilePath tmp_db_path;
109 CHECK(base::CreateTemporaryFile(&tmp_db_path));
110 tmp_db_paths_.push_back(tmp_db_path);
111
112 ofstream tmp_db(tmp_db_path.value(), ofstream::binary);
113 for (size_t i = 0; i < num_elems; ++i) {
114 tmp_db << database_data[i];
115 }
116 tmp_db.close();
117 operator_info_->AddDatabasePath(tmp_db_path);
118 }
119
AssertDatabaseEmpty()120 void AssertDatabaseEmpty() {
121 EXPECT_EQ(0, operator_info_impl_->database()->mno_size());
122 EXPECT_EQ(0, operator_info_impl_->database()->imvno_size());
123 }
124
GetDatabase()125 const MobileOperatorDB* GetDatabase() {
126 return operator_info_impl_->database();
127 }
128
129 EventDispatcherForTest dispatcher_;
130 vector<FilePath> tmp_db_paths_;
131 std::unique_ptr<MobileOperatorInfo> operator_info_;
132 // Owned by |operator_info_| and tied to its life cycle.
133 MobileOperatorInfoImpl* operator_info_impl_;
134
135 private:
136 DISALLOW_COPY_AND_ASSIGN(MobileOperatorInfoInitTest);
137 };
138
TEST_F(MobileOperatorInfoInitTest,FailedInitNoPath)139 TEST_F(MobileOperatorInfoInitTest, FailedInitNoPath) {
140 // - Initialize object with no database paths set
141 // - Verify that initialization fails.
142 operator_info_->ClearDatabasePaths();
143 EXPECT_FALSE(operator_info_->Init());
144 AssertDatabaseEmpty();
145 }
146
TEST_F(MobileOperatorInfoInitTest,FailedInitBadPath)147 TEST_F(MobileOperatorInfoInitTest, FailedInitBadPath) {
148 // - Initialize object with non-existent path.
149 // - Verify that initialization fails.
150 const FilePath database_path("nonexistent.pbf");
151 operator_info_->ClearDatabasePaths();
152 operator_info_->AddDatabasePath(database_path);
153 EXPECT_FALSE(operator_info_->Init());
154 AssertDatabaseEmpty();
155 }
156
TEST_F(MobileOperatorInfoInitTest,FailedInitBadDatabase)157 TEST_F(MobileOperatorInfoInitTest, FailedInitBadDatabase) {
158 // - Initialize object with malformed database.
159 // - Verify that initialization fails.
160 // TODO(pprabhu): It's hard to get a malformed database in binary format.
161 }
162
TEST_F(MobileOperatorInfoInitTest,EmptyDBInit)163 TEST_F(MobileOperatorInfoInitTest, EmptyDBInit) {
164 // - Initialize the object with a database file that is empty.
165 // - Verify that initialization succeeds, and that the database is empty.
166 operator_info_->ClearDatabasePaths();
167 // Can't use arraysize on empty array.
168 AddDatabase(mobile_operator_db::init_test_empty_db_init, 0);
169 EXPECT_TRUE(operator_info_->Init());
170 AssertDatabaseEmpty();
171 }
172
TEST_F(MobileOperatorInfoInitTest,SuccessfulInit)173 TEST_F(MobileOperatorInfoInitTest, SuccessfulInit) {
174 operator_info_->ClearDatabasePaths();
175 AddDatabase(mobile_operator_db::init_test_successful_init,
176 arraysize(mobile_operator_db::init_test_successful_init));
177 EXPECT_TRUE(operator_info_->Init());
178 EXPECT_GT(GetDatabase()->mno_size(), 0);
179 EXPECT_GT(GetDatabase()->imvno_size(), 0);
180 }
181
TEST_F(MobileOperatorInfoInitTest,MultipleDBInit)182 TEST_F(MobileOperatorInfoInitTest, MultipleDBInit) {
183 // - Initialize the object with two database files.
184 // - Verify that intialization succeeds, and both databases are loaded.
185 operator_info_->ClearDatabasePaths();
186 AddDatabase(mobile_operator_db::init_test_multiple_db_init_1,
187 arraysize(mobile_operator_db::init_test_multiple_db_init_1));
188 AddDatabase(mobile_operator_db::init_test_multiple_db_init_2,
189 arraysize(mobile_operator_db::init_test_multiple_db_init_2));
190 operator_info_->Init();
191 EXPECT_GT(GetDatabase()->mno_size(), 0);
192 EXPECT_GT(GetDatabase()->imvno_size(), 0);
193 }
194
TEST_F(MobileOperatorInfoInitTest,InitWithObserver)195 TEST_F(MobileOperatorInfoInitTest, InitWithObserver) {
196 // - Add an Observer.
197 // - Initialize the object with empty database file.
198 // - Verify innitialization succeeds.
199 MockMobileOperatorInfoObserver dumb_observer;
200
201 operator_info_->ClearDatabasePaths();
202 // Can't use arraysize with empty array.
203 AddDatabase(mobile_operator_db::init_test_empty_db_init, 0);
204 operator_info_->AddObserver(&dumb_observer);
205 EXPECT_TRUE(operator_info_->Init());
206 }
207
208 class MobileOperatorInfoMainTest
209 : public MobileOperatorInfoInitTest,
210 public WithParamInterface<EventCheckingPolicy> {
211 public:
MobileOperatorInfoMainTest()212 MobileOperatorInfoMainTest()
213 : MobileOperatorInfoInitTest(),
214 event_checking_policy_(GetParam()) {}
215
SetUp()216 virtual void SetUp() {
217 operator_info_->ClearDatabasePaths();
218 AddDatabase(mobile_operator_db::main_test,
219 arraysize(mobile_operator_db::main_test));
220 operator_info_->Init();
221 operator_info_->AddObserver(&observer_);
222 }
223
224 protected:
225 // ///////////////////////////////////////////////////////////////////////////
226 // Helper functions.
VerifyMNOWithUUID(const string & uuid)227 void VerifyMNOWithUUID(const string& uuid) {
228 EXPECT_TRUE(operator_info_->IsMobileNetworkOperatorKnown());
229 EXPECT_FALSE(operator_info_->IsMobileVirtualNetworkOperatorKnown());
230 EXPECT_EQ(uuid, operator_info_->uuid());
231 }
232
VerifyMVNOWithUUID(const string & uuid)233 void VerifyMVNOWithUUID(const string& uuid) {
234 EXPECT_TRUE(operator_info_->IsMobileNetworkOperatorKnown());
235 EXPECT_TRUE(operator_info_->IsMobileVirtualNetworkOperatorKnown());
236 EXPECT_EQ(uuid, operator_info_->uuid());
237 }
238
VerifyNoMatch()239 void VerifyNoMatch() {
240 EXPECT_FALSE(operator_info_->IsMobileNetworkOperatorKnown());
241 EXPECT_FALSE(operator_info_->IsMobileVirtualNetworkOperatorKnown());
242 EXPECT_EQ("", operator_info_->uuid());
243 }
244
ExpectEventCount(int count)245 void ExpectEventCount(int count) {
246 // In case we're running in the non-strict event checking mode, we only
247 // expect one overall event to be raised for all the updates.
248 if (event_checking_policy_ == kEventCheckingPolicyNonStrict) {
249 count = (count > 0) ? 1 : 0;
250 }
251 EXPECT_CALL(observer_, OnOperatorChanged()).Times(count);
252 }
253
VerifyEventCount()254 void VerifyEventCount() {
255 dispatcher_.DispatchPendingEvents();
256 Mock::VerifyAndClearExpectations(&observer_);
257 }
258
ResetOperatorInfo()259 void ResetOperatorInfo() {
260 operator_info_->Reset();
261 // Eat up any events caused by |Reset|.
262 dispatcher_.DispatchPendingEvents();
263 VerifyNoMatch();
264 }
265
266 // Use these wrappers to send updates to |operator_info_|. These wrappers
267 // optionally run the dispatcher if we want strict checking of the number of
268 // events raised.
UpdateMCCMNC(const std::string & mccmnc)269 void UpdateMCCMNC(const std::string& mccmnc) {
270 operator_info_->UpdateMCCMNC(mccmnc);
271 DispatchPendingEventsIfStrict();
272 }
273
UpdateSID(const std::string & sid)274 void UpdateSID(const std::string& sid) {
275 operator_info_->UpdateSID(sid);
276 DispatchPendingEventsIfStrict();
277 }
278
UpdateIMSI(const std::string & imsi)279 void UpdateIMSI(const std::string& imsi) {
280 operator_info_->UpdateIMSI(imsi);
281 DispatchPendingEventsIfStrict();
282 }
283
UpdateICCID(const std::string & iccid)284 void UpdateICCID(const std::string& iccid) {
285 operator_info_->UpdateICCID(iccid);
286 DispatchPendingEventsIfStrict();
287 }
288
UpdateNID(const std::string & nid)289 void UpdateNID(const std::string& nid) {
290 operator_info_->UpdateNID(nid);
291 DispatchPendingEventsIfStrict();
292 }
293
UpdateOperatorName(const std::string & operator_name)294 void UpdateOperatorName(const std::string& operator_name) {
295 operator_info_->UpdateOperatorName(operator_name);
296 DispatchPendingEventsIfStrict();
297 }
298
UpdateOnlinePortal(const std::string & url,const std::string & method,const std::string & post_data)299 void UpdateOnlinePortal(const std::string& url,
300 const std::string& method,
301 const std::string& post_data) {
302 operator_info_->UpdateOnlinePortal(url, method, post_data);
303 DispatchPendingEventsIfStrict();
304 }
305
DispatchPendingEventsIfStrict()306 void DispatchPendingEventsIfStrict() {
307 if (event_checking_policy_ == kEventCheckingPolicyStrict) {
308 dispatcher_.DispatchPendingEvents();
309 }
310 }
311
312 // ///////////////////////////////////////////////////////////////////////////
313 // Data.
314 MockMobileOperatorInfoObserver observer_;
315 const EventCheckingPolicy event_checking_policy_;
316
317 private:
318 DISALLOW_COPY_AND_ASSIGN(MobileOperatorInfoMainTest);
319 };
320
TEST_P(MobileOperatorInfoMainTest,InitialConditions)321 TEST_P(MobileOperatorInfoMainTest, InitialConditions) {
322 // - Initialize a new object.
323 // - Verify that all initial values of properties are reasonable.
324 EXPECT_FALSE(operator_info_->IsMobileNetworkOperatorKnown());
325 EXPECT_FALSE(operator_info_->IsMobileVirtualNetworkOperatorKnown());
326 EXPECT_TRUE(operator_info_->uuid().empty());
327 EXPECT_TRUE(operator_info_->operator_name().empty());
328 EXPECT_TRUE(operator_info_->country().empty());
329 EXPECT_TRUE(operator_info_->mccmnc().empty());
330 EXPECT_TRUE(operator_info_->sid().empty());
331 EXPECT_TRUE(operator_info_->nid().empty());
332 EXPECT_TRUE(operator_info_->mccmnc_list().empty());
333 EXPECT_TRUE(operator_info_->sid_list().empty());
334 EXPECT_TRUE(operator_info_->operator_name_list().empty());
335 EXPECT_TRUE(operator_info_->apn_list().empty());
336 EXPECT_TRUE(operator_info_->olp_list().empty());
337 EXPECT_TRUE(operator_info_->activation_code().empty());
338 EXPECT_FALSE(operator_info_->requires_roaming());
339 }
340
TEST_P(MobileOperatorInfoMainTest,MNOByMCCMNC)341 TEST_P(MobileOperatorInfoMainTest, MNOByMCCMNC) {
342 // message: Has an MNO with no MVNO.
343 // match by: MCCMNC.
344 // verify: Observer event, uuid.
345
346 ExpectEventCount(0);
347 UpdateMCCMNC("101999"); // No match.
348 VerifyEventCount();
349 VerifyNoMatch();
350
351 ExpectEventCount(1);
352 UpdateMCCMNC("101001");
353 VerifyEventCount();
354 VerifyMNOWithUUID("uuid101");
355
356 ExpectEventCount(1);
357 UpdateMCCMNC("101999");
358 VerifyEventCount();
359 VerifyNoMatch();
360 }
361
TEST_P(MobileOperatorInfoMainTest,MNOByMCCMNCMultipleMCCMNCOptions)362 TEST_P(MobileOperatorInfoMainTest, MNOByMCCMNCMultipleMCCMNCOptions) {
363 // message: Has an MNO with no MCCMNC.
364 // match by: One of the MCCMNCs of the multiple ones in the MNO.
365 // verify: Observer event, uuid.
366 ExpectEventCount(1);
367 UpdateMCCMNC("102002");
368 VerifyEventCount();
369 VerifyMNOWithUUID("uuid102");
370 }
371
TEST_P(MobileOperatorInfoMainTest,MNOByMCCMNCMultipleMNOOptions)372 TEST_P(MobileOperatorInfoMainTest, MNOByMCCMNCMultipleMNOOptions) {
373 // message: Two messages with the same MCCMNC.
374 // match by: Both MNOs matched, one is earmarked.
375 // verify: The earmarked MNO is picked.
376 ExpectEventCount(1);
377 UpdateMCCMNC("124001");
378 VerifyEventCount();
379 VerifyMNOWithUUID("uuid124002");
380 }
381
TEST_P(MobileOperatorInfoMainTest,MNOByOperatorName)382 TEST_P(MobileOperatorInfoMainTest, MNOByOperatorName) {
383 // message: Has an MNO with no MVNO.
384 // match by: OperatorName.
385 // verify: Observer event, uuid.
386 ExpectEventCount(0);
387 UpdateOperatorName("name103999"); // No match.
388 VerifyEventCount();
389 VerifyNoMatch();
390
391 ExpectEventCount(1);
392 UpdateOperatorName("name103");
393 VerifyEventCount();
394 VerifyMNOWithUUID("uuid103");
395
396 ExpectEventCount(1);
397 UpdateOperatorName("name103999"); // No match.
398 VerifyEventCount();
399 VerifyNoMatch();
400 }
401
TEST_P(MobileOperatorInfoMainTest,MNOByOperatorNameMultipleMNOOptions)402 TEST_P(MobileOperatorInfoMainTest, MNOByOperatorNameMultipleMNOOptions) {
403 // message: Two messages with the same operator name.
404 // match by: Both MNOs matched, one is earmarked.
405 // verify: The earmarked MNO is picked.
406 ExpectEventCount(1);
407 UpdateOperatorName("name125001");
408 VerifyEventCount();
409 VerifyMNOWithUUID("uuid125002");
410 }
411
TEST_P(MobileOperatorInfoMainTest,MNOByOperatorNameAggressiveMatch)412 TEST_P(MobileOperatorInfoMainTest, MNOByOperatorNameAggressiveMatch) {
413 // These network operators match by name but only after normalizing the names.
414 // Both the name from the database and the name provided to
415 // |UpdateOperatoName| must be normalized for this test to pass.
416 ExpectEventCount(1);
417 UpdateOperatorName("name126001 casedoesnotmatch");
418 VerifyEventCount();
419 VerifyMNOWithUUID("uuid126001");
420
421 ResetOperatorInfo();
422 ExpectEventCount(1);
423 UpdateOperatorName("name126002 CaseStillDoesNotMatch");
424 VerifyEventCount();
425 VerifyMNOWithUUID("uuid126002");
426
427 ResetOperatorInfo();
428 ExpectEventCount(1);
429 UpdateOperatorName("name126003GiveMeMoreSpace");
430 VerifyEventCount();
431 VerifyMNOWithUUID("uuid126003");
432
433 ResetOperatorInfo();
434 ExpectEventCount(1);
435 UpdateOperatorName("name126004 Too Much Air Here");
436 VerifyEventCount();
437 VerifyMNOWithUUID("uuid126004");
438
439 ResetOperatorInfo();
440 ExpectEventCount(1);
441 UpdateOperatorName("näméwithNon-Äσ¢ii");
442 VerifyEventCount();
443 VerifyMNOWithUUID("uuid126005");
444 }
445
TEST_P(MobileOperatorInfoMainTest,MNOByOperatorNameWithLang)446 TEST_P(MobileOperatorInfoMainTest, MNOByOperatorNameWithLang) {
447 // message: Has an MNO with no MVNO.
448 // match by: OperatorName.
449 // verify: Observer event, fields.
450 ExpectEventCount(1);
451 UpdateOperatorName("name105");
452 VerifyEventCount();
453 VerifyMNOWithUUID("uuid105");
454 }
455
TEST_P(MobileOperatorInfoMainTest,MNOByOperatorNameMultipleNameOptions)456 TEST_P(MobileOperatorInfoMainTest, MNOByOperatorNameMultipleNameOptions) {
457 // message: Has an MNO with no MVNO.
458 // match by: OperatorName, one of the multiple present in the MNO.
459 // verify: Observer event, fields.
460 ExpectEventCount(1);
461 UpdateOperatorName("name104002");
462 VerifyEventCount();
463 VerifyMNOWithUUID("uuid104");
464 }
465
TEST_P(MobileOperatorInfoMainTest,MNOByMCCMNCAndOperatorName)466 TEST_P(MobileOperatorInfoMainTest, MNOByMCCMNCAndOperatorName) {
467 // message: Has MNOs with no MVNO.
468 // match by: MCCMNC finds two candidates (first one is chosen), Name narrows
469 // down to one.
470 // verify: Observer event, fields.
471 // This is merely a MCCMNC update.
472 ExpectEventCount(1);
473 UpdateMCCMNC("106001");
474 VerifyEventCount();
475 VerifyMNOWithUUID("uuid106001");
476
477 ExpectEventCount(1);
478 UpdateOperatorName("name106002");
479 VerifyEventCount();
480 VerifyMNOWithUUID("uuid106002");
481
482 ResetOperatorInfo();
483 // Try updates in reverse order.
484 ExpectEventCount(1);
485 UpdateOperatorName("name106001");
486 VerifyEventCount();
487 VerifyMNOWithUUID("uuid106001");
488 }
489
TEST_P(MobileOperatorInfoMainTest,MNOByOperatorNameAndMCCMNC)490 TEST_P(MobileOperatorInfoMainTest, MNOByOperatorNameAndMCCMNC) {
491 // message: Has MNOs with no MVNO.
492 // match by: OperatorName finds two (first one is chosen), MCCMNC narrows down
493 // to one.
494 // verify: Observer event, fields.
495 // This is merely an OperatorName update.
496 ExpectEventCount(1);
497 UpdateOperatorName("name107");
498 VerifyEventCount();
499 VerifyMNOWithUUID("uuid107001");
500
501 ExpectEventCount(1);
502 UpdateMCCMNC("107002");
503 VerifyEventCount();
504 VerifyMNOWithUUID("uuid107002");
505
506 ResetOperatorInfo();
507 // Try updates in reverse order.
508 ExpectEventCount(1);
509 UpdateMCCMNC("107001");
510 VerifyEventCount();
511 VerifyMNOWithUUID("uuid107001");
512 }
513
TEST_P(MobileOperatorInfoMainTest,MNOByMCCMNCOverridesOperatorName)514 TEST_P(MobileOperatorInfoMainTest, MNOByMCCMNCOverridesOperatorName) {
515 // message: Has MNOs with no MVNO.
516 // match by: First MCCMNC finds one. Then, OperatorName matches another.
517 // verify: MCCMNC match prevails. No change on OperatorName update.
518 ExpectEventCount(1);
519 UpdateMCCMNC("108001");
520 VerifyEventCount();
521 VerifyMNOWithUUID("uuid108001");
522
523 // An event is sent for the updated OperatorName.
524 ExpectEventCount(1);
525 UpdateOperatorName("name108002"); // Does not match.
526 VerifyEventCount();
527 VerifyMNOWithUUID("uuid108001");
528 // OperatorName from the database is given preference over the user supplied
529 // one.
530 EXPECT_EQ("name108001", operator_info_->operator_name());
531
532 ResetOperatorInfo();
533 // message: Same as above.
534 // match by: First OperatorName finds one, then MCCMNC overrides it.
535 // verify: Two events, MCCMNC one overriding the OperatorName one.
536 ExpectEventCount(1);
537 UpdateOperatorName("name108001");
538 VerifyEventCount();
539 VerifyMNOWithUUID("uuid108001");
540
541 ExpectEventCount(1);
542 UpdateMCCMNC("108002");
543 VerifyEventCount();
544 VerifyMNOWithUUID("uuid108002");
545 EXPECT_EQ("name108002", operator_info_->operator_name());
546
547 // message: Same as above.
548 // match by: First a *wrong* MCCMNC update, followed by the correct Name
549 // update.
550 // verify: No MNO, since MCCMNC is given precedence.
551 ResetOperatorInfo();
552 ExpectEventCount(0);
553 UpdateMCCMNC("108999"); // Does not match.
554 UpdateOperatorName("name108001");
555 VerifyEventCount();
556 VerifyNoMatch();
557 }
558
TEST_P(MobileOperatorInfoMainTest,MNOByIMSI)559 TEST_P(MobileOperatorInfoMainTest, MNOByIMSI) {
560 // message: Has MNO with no MVNO.
561 // match by: MCCMNC part of IMSI of length 5 / 6.
562 ExpectEventCount(0);
563 UpdateIMSI("109"); // Too short.
564 VerifyEventCount();
565 VerifyNoMatch();
566
567 ExpectEventCount(0);
568 UpdateIMSI("109995432154321"); // No match.
569 VerifyEventCount();
570 VerifyNoMatch();
571
572 ResetOperatorInfo();
573 // Short MCCMNC match.
574 ExpectEventCount(1);
575 UpdateIMSI("109015432154321"); // First 5 digits match.
576 VerifyEventCount();
577 VerifyMNOWithUUID("uuid10901");
578
579 ResetOperatorInfo();
580 // Long MCCMNC match.
581 ExpectEventCount(1);
582 UpdateIMSI("10900215432154321"); // First 6 digits match.
583 VerifyEventCount();
584 VerifyMNOWithUUID("uuid109002");
585 }
586
TEST_P(MobileOperatorInfoMainTest,MNOByMCCMNCOverridesIMSI)587 TEST_P(MobileOperatorInfoMainTest, MNOByMCCMNCOverridesIMSI) {
588 // message: Has MNOs with no MVNO.
589 // match by: One matches MCCMNC, then one matches a different MCCMNC substring
590 // of IMSI
591 // verify: Observer event for the first match, all fields. Second Update
592 // ignored.
593 ExpectEventCount(1);
594 UpdateMCCMNC("110001");
595 VerifyEventCount();
596 VerifyMNOWithUUID("uuid110001");
597
598 // MNO remains unchanged on a mismatched IMSI update.
599 ExpectEventCount(0);
600 UpdateIMSI("1100025432154321"); // First 6 digits match.
601 VerifyEventCount();
602 VerifyMNOWithUUID("uuid110001");
603
604 // MNO remains uncnaged on an invalid IMSI update.
605 ExpectEventCount(0);
606 UpdateIMSI("1100035432154321"); // Prefix does not match.
607 VerifyEventCount();
608 VerifyMNOWithUUID("uuid110001");
609
610 ExpectEventCount(0);
611 UpdateIMSI("110"); // Too small.
612 VerifyEventCount();
613 VerifyMNOWithUUID("uuid110001");
614
615 ResetOperatorInfo();
616 // Same as above, but this time, match with IMSI, followed by a contradictory
617 // MCCMNC update. The second update should override the first one.
618 ExpectEventCount(1);
619 UpdateIMSI("1100025432154321"); // First 6 digits match.
620 VerifyEventCount();
621 VerifyMNOWithUUID("uuid110002");
622
623 ExpectEventCount(1);
624 UpdateMCCMNC("110001");
625 VerifyEventCount();
626 VerifyMNOWithUUID("uuid110001");
627 }
628
TEST_P(MobileOperatorInfoMainTest,MNOUchangedBySecondaryUpdates)629 TEST_P(MobileOperatorInfoMainTest, MNOUchangedBySecondaryUpdates) {
630 // This test verifies that only some updates affect the MNO.
631 // message: Has MNOs with no MVNO.
632 // match by: First matches the MCCMNC. Later, MNOs with a different MCCMNC
633 // matchs the given SID, NID, ICCID.
634 // verify: Only one Observer event, on the first MCCMNC match.
635 ExpectEventCount(1);
636 UpdateMCCMNC("111001");
637 VerifyEventCount();
638 VerifyMNOWithUUID("uuid111001");
639
640 ExpectEventCount(1); // NID change event.
641 UpdateNID("111202");
642 VerifyEventCount();
643 VerifyMNOWithUUID("uuid111001");
644 }
645
TEST_P(MobileOperatorInfoMainTest,MVNODefaultMatch)646 TEST_P(MobileOperatorInfoMainTest, MVNODefaultMatch) {
647 // message: MNO with one MVNO (no filter).
648 // match by: MNO matches by MCCMNC.
649 // verify: Observer event for MVNO match. Uuid match the MVNO.
650 // second update: ICCID.
651 // verify: No observer event, match remains unchanged.
652 ExpectEventCount(1);
653 UpdateMCCMNC("112001");
654 VerifyEventCount();
655 VerifyMVNOWithUUID("uuid112002");
656
657 ExpectEventCount(0);
658 UpdateICCID("112002");
659 VerifyEventCount();
660 VerifyMVNOWithUUID("uuid112002");
661 }
662
TEST_P(MobileOperatorInfoMainTest,MVNONameMatch)663 TEST_P(MobileOperatorInfoMainTest, MVNONameMatch) {
664 // message: MNO with one MVNO (name filter).
665 // match by: MNO matches by MCCMNC,
666 // MVNO fails to match by fist name update,
667 // then MVNO matches by name.
668 // verify: Two Observer events: MNO followed by MVNO.
669 ExpectEventCount(1);
670 UpdateMCCMNC("113001");
671 VerifyEventCount();
672 VerifyMNOWithUUID("uuid113001");
673
674 ExpectEventCount(1);
675 UpdateOperatorName("name113999"); // No match.
676 VerifyEventCount();
677 VerifyMNOWithUUID("uuid113001");
678 // Name from the database is given preference.
679 EXPECT_EQ("name113001", operator_info_->operator_name());
680
681 ExpectEventCount(1);
682 UpdateOperatorName("name113002");
683 VerifyEventCount();
684 VerifyMVNOWithUUID("uuid113002");
685 EXPECT_EQ("name113002", operator_info_->operator_name());
686 }
687
TEST_P(MobileOperatorInfoMainTest,MVNONameMalformedRegexMatch)688 TEST_P(MobileOperatorInfoMainTest, MVNONameMalformedRegexMatch) {
689 // message: MNO with one MVNO (name filter with a malformed regex).
690 // match by: MNO matches by MCCMNC.
691 // MVNO does not match
692 ExpectEventCount(2);
693 UpdateMCCMNC("114001");
694 UpdateOperatorName("name[");
695 VerifyEventCount();
696 VerifyMNOWithUUID("uuid114001");
697 }
698
TEST_P(MobileOperatorInfoMainTest,MVNONameSubexpressionRegexMatch)699 TEST_P(MobileOperatorInfoMainTest, MVNONameSubexpressionRegexMatch) {
700 // message: MNO with one MVNO (name filter with simple regex).
701 // match by: MNO matches by MCCMNC.
702 // MVNO does not match with a name whose subexpression matches the
703 // regex.
704 ExpectEventCount(2); // One event for just the name update.
705 UpdateMCCMNC("115001");
706 UpdateOperatorName("name115_ExtraCrud");
707 VerifyEventCount();
708 VerifyMNOWithUUID("uuid115001");
709
710 ResetOperatorInfo();
711 ExpectEventCount(2); // One event for just the name update.
712 UpdateMCCMNC("115001");
713 UpdateOperatorName("ExtraCrud_name115");
714 VerifyEventCount();
715 VerifyMNOWithUUID("uuid115001");
716
717 ResetOperatorInfo();
718 ExpectEventCount(2); // One event for just the name update.
719 UpdateMCCMNC("115001");
720 UpdateOperatorName("ExtraCrud_name115_ExtraCrud");
721 VerifyEventCount();
722 VerifyMNOWithUUID("uuid115001");
723
724 ResetOperatorInfo();
725 ExpectEventCount(2); // One event for just the name update.
726 UpdateMCCMNC("115001");
727 UpdateOperatorName("name_ExtraCrud_115");
728 VerifyEventCount();
729 VerifyMNOWithUUID("uuid115001");
730
731 ResetOperatorInfo();
732 ExpectEventCount(2);
733 UpdateMCCMNC("115001");
734 UpdateOperatorName("name115");
735 VerifyEventCount();
736 VerifyMVNOWithUUID("uuid115002");
737 }
738
TEST_P(MobileOperatorInfoMainTest,MVNONameRegexMatch)739 TEST_P(MobileOperatorInfoMainTest, MVNONameRegexMatch) {
740 // message: MNO with one MVNO (name filter with non-trivial regex).
741 // match by: MNO matches by MCCMNC.
742 // MVNO fails to match several times with different strings.
743 // MVNO matches several times with different values.
744
745 // Make sure we're not taking the regex literally!
746 ExpectEventCount(2);
747 UpdateMCCMNC("116001");
748 UpdateOperatorName("name[a-zA-Z_]*116[0-9]{0,3}");
749 VerifyEventCount();
750 VerifyMNOWithUUID("uuid116001");
751
752 ResetOperatorInfo();
753 ExpectEventCount(2);
754 UpdateMCCMNC("116001");
755 UpdateOperatorName("name[a-zA-Z_]116[0-9]");
756 VerifyEventCount();
757 VerifyMNOWithUUID("uuid116001");
758
759 ResetOperatorInfo();
760 ExpectEventCount(2);
761 UpdateMCCMNC("116001");
762 UpdateOperatorName("nameb*1167");
763 VerifyEventCount();
764 VerifyMNOWithUUID("uuid116001");
765
766 // Success!
767 ResetOperatorInfo();
768 ExpectEventCount(2);
769 UpdateMCCMNC("116001");
770 UpdateOperatorName("name116");
771 VerifyEventCount();
772 VerifyMVNOWithUUID("uuid116002");
773
774 ResetOperatorInfo();
775 ExpectEventCount(2);
776 UpdateMCCMNC("116001");
777 UpdateOperatorName("nameSomeWord116");
778 VerifyEventCount();
779 VerifyMVNOWithUUID("uuid116002");
780
781 ResetOperatorInfo();
782 ExpectEventCount(2);
783 UpdateMCCMNC("116001");
784 UpdateOperatorName("name116567");
785 VerifyEventCount();
786 VerifyMVNOWithUUID("uuid116002");
787 }
788
TEST_P(MobileOperatorInfoMainTest,MVNONameMatchMultipleFilters)789 TEST_P(MobileOperatorInfoMainTest, MVNONameMatchMultipleFilters) {
790 // message: MNO with one MVNO with two name filters.
791 // match by: MNO matches by MCCMNC.
792 // MVNO first fails on the second filter alone.
793 // MVNO fails on the first filter alone.
794 // MVNO matches on both filters.
795 ExpectEventCount(2);
796 UpdateMCCMNC("117001");
797 UpdateOperatorName("nameA_crud");
798 VerifyEventCount();
799 VerifyMNOWithUUID("uuid117001");
800
801 ResetOperatorInfo();
802 ExpectEventCount(2);
803 UpdateMCCMNC("117001");
804 UpdateOperatorName("crud_nameB");
805 VerifyEventCount();
806 VerifyMNOWithUUID("uuid117001");
807
808 ResetOperatorInfo();
809 ExpectEventCount(2);
810 UpdateMCCMNC("117001");
811 UpdateOperatorName("crud_crud");
812 VerifyEventCount();
813 VerifyMNOWithUUID("uuid117001");
814
815 ResetOperatorInfo();
816 ExpectEventCount(2);
817 UpdateMCCMNC("117001");
818 UpdateOperatorName("nameA_nameB");
819 VerifyEventCount();
820 VerifyMVNOWithUUID("uuid117002");
821 }
822
TEST_P(MobileOperatorInfoMainTest,MVNOIMSIMatch)823 TEST_P(MobileOperatorInfoMainTest, MVNOIMSIMatch) {
824 // message: MNO with one MVNO (imsi filter).
825 // match by: MNO matches by MCCMNC,
826 // MVNO fails to match by fist imsi update,
827 // then MVNO matches by imsi.
828 // verify: Two Observer events: MNO followed by MVNO.
829 ExpectEventCount(1);
830 UpdateMCCMNC("118001");
831 VerifyEventCount();
832 VerifyMNOWithUUID("uuid118001");
833
834 ExpectEventCount(0);
835 UpdateIMSI("1180011234512345"); // No match.
836 VerifyEventCount();
837 VerifyMNOWithUUID("uuid118001");
838
839 ExpectEventCount(1);
840 UpdateIMSI("1180015432154321");
841 VerifyEventCount();
842 VerifyMVNOWithUUID("uuid118002");
843 }
844
TEST_P(MobileOperatorInfoMainTest,MVNOICCIDMatch)845 TEST_P(MobileOperatorInfoMainTest, MVNOICCIDMatch) {
846 // message: MNO with one MVNO (iccid filter).
847 // match by: MNO matches by MCCMNC,
848 // MVNO fails to match by fist iccid update,
849 // then MVNO matches by iccid.
850 // verify: Two Observer events: MNO followed by MVNO.
851 ExpectEventCount(1);
852 UpdateMCCMNC("119001");
853 VerifyEventCount();
854 VerifyMNOWithUUID("uuid119001");
855
856 ExpectEventCount(0);
857 UpdateICCID("119987654321"); // No match.
858 VerifyEventCount();
859 VerifyMNOWithUUID("uuid119001");
860
861 ExpectEventCount(1);
862 UpdateICCID("119123456789");
863 VerifyEventCount();
864 VerifyMVNOWithUUID("uuid119002");
865 }
866
TEST_P(MobileOperatorInfoMainTest,MVNOSIDMatch)867 TEST_P(MobileOperatorInfoMainTest, MVNOSIDMatch) {
868 // message: MNO with one MVNO (sid filter).
869 // match by: MNO matches by SID,
870 // MVNO fails to match by fist sid update,
871 // then MVNO matches by sid.
872 // verify: Two Observer events: MNO followed by MVNO.
873 ExpectEventCount(0);
874 UpdateSID("120999"); // No match.
875 VerifyEventCount();
876 VerifyNoMatch();
877
878 ExpectEventCount(1);
879 UpdateSID("120001"); // Only MNO matches.
880 VerifyEventCount();
881 VerifyMNOWithUUID("uuid120001");
882 EXPECT_EQ("120001", operator_info_->sid());
883
884 ExpectEventCount(1);
885 UpdateSID("120002"); // MVNO matches as well.
886 VerifyEventCount();
887 VerifyMVNOWithUUID("uuid120002");
888 EXPECT_EQ("120002", operator_info_->sid());
889 }
890
TEST_P(MobileOperatorInfoMainTest,MVNOAllMatch)891 TEST_P(MobileOperatorInfoMainTest, MVNOAllMatch) {
892 // message: MNO with following MVNOS:
893 // - one with no filter.
894 // - one with name filter.
895 // - one with imsi filter.
896 // - one with iccid filter.
897 // - one with name and iccid filter.
898 // verify:
899 // - initial MCCMNC matches the default MVNO directly (not MNO)
900 // - match each of the MVNOs in turn.
901 // - give super set information that does not match any MVNO correctly,
902 // verify that the MNO matches.
903 ExpectEventCount(1);
904 UpdateMCCMNC("121001");
905 VerifyEventCount();
906 VerifyMNOWithUUID("uuid121001");
907
908 ResetOperatorInfo();
909 ExpectEventCount(2);
910 UpdateMCCMNC("121001");
911 UpdateOperatorName("name121003");
912 VerifyEventCount();
913 VerifyMVNOWithUUID("uuid121003");
914
915 ResetOperatorInfo();
916 ExpectEventCount(2);
917 UpdateMCCMNC("121001");
918 UpdateIMSI("1210045432154321");
919 VerifyEventCount();
920 VerifyMVNOWithUUID("uuid121004");
921
922 ResetOperatorInfo();
923 ExpectEventCount(2);
924 UpdateMCCMNC("121001");
925 UpdateICCID("121005123456789");
926 VerifyEventCount();
927 VerifyMVNOWithUUID("uuid121005");
928
929 ResetOperatorInfo();
930 ExpectEventCount(3);
931 UpdateMCCMNC("121001");
932 UpdateOperatorName("name121006");
933 VerifyMNOWithUUID("uuid121001");
934 UpdateICCID("121006123456789");
935 VerifyEventCount();
936 VerifyMVNOWithUUID("uuid121006");
937 }
938
TEST_P(MobileOperatorInfoMainTest,MVNOMatchAndMismatch)939 TEST_P(MobileOperatorInfoMainTest, MVNOMatchAndMismatch) {
940 // message: MNO with one MVNO with name filter.
941 // match by: MNO matches by MCCMNC
942 // MVNO matches by name.
943 // Second name update causes the MVNO to not match again.
944 ExpectEventCount(1);
945 UpdateMCCMNC("113001");
946 VerifyEventCount();
947 VerifyMNOWithUUID("uuid113001");
948
949 ExpectEventCount(1);
950 UpdateOperatorName("name113002");
951 VerifyEventCount();
952 VerifyMVNOWithUUID("uuid113002");
953 EXPECT_EQ("name113002", operator_info_->operator_name());
954
955 ExpectEventCount(1);
956 UpdateOperatorName("name113999"); // No match.
957 VerifyEventCount();
958 VerifyMNOWithUUID("uuid113001");
959 // Name from database is given preference.
960 EXPECT_EQ("name113001", operator_info_->operator_name());
961 }
962
TEST_P(MobileOperatorInfoMainTest,MVNOMatchAndReset)963 TEST_P(MobileOperatorInfoMainTest, MVNOMatchAndReset) {
964 // message: MVNO with name filter.
965 // verify;
966 // - match MVNO by name.
967 // - Reset object, verify Observer event, and not match.
968 // - match MVNO by name again.
969 ExpectEventCount(1);
970 UpdateMCCMNC("113001");
971 VerifyEventCount();
972 ExpectEventCount(1);
973 VerifyMNOWithUUID("uuid113001");
974 UpdateOperatorName("name113002");
975 VerifyEventCount();
976 VerifyMVNOWithUUID("uuid113002");
977 EXPECT_EQ("name113002", operator_info_->operator_name());
978
979 ExpectEventCount(1);
980 operator_info_->Reset();
981 VerifyEventCount();
982 VerifyNoMatch();
983
984 ExpectEventCount(1);
985 UpdateMCCMNC("113001");
986 VerifyEventCount();
987 VerifyMNOWithUUID("uuid113001");
988 ExpectEventCount(1);
989 UpdateOperatorName("name113002");
990 VerifyEventCount();
991 VerifyMVNOWithUUID("uuid113002");
992 EXPECT_EQ("name113002", operator_info_->operator_name());
993 }
994
995 // Here, we rely on our knowledge about the implementation: The SID and MCCMNC
996 // updates follow the same code paths, and so we can get away with not testing
997 // all the scenarios we test above for MCCMNC. Instead, we only do basic testing
998 // to make sure that SID upates operator as MCCMNC updates do.
TEST_P(MobileOperatorInfoMainTest,MNOBySID)999 TEST_P(MobileOperatorInfoMainTest, MNOBySID) {
1000 // message: Has an MNO with no MVNO.
1001 // match by: SID.
1002 // verify: Observer event, uuid.
1003
1004 ExpectEventCount(0);
1005 UpdateSID("1229"); // No match.
1006 VerifyEventCount();
1007 VerifyNoMatch();
1008
1009 ExpectEventCount(1);
1010 UpdateSID("1221");
1011 VerifyEventCount();
1012 VerifyMNOWithUUID("uuid1221");
1013
1014 ExpectEventCount(1);
1015 UpdateSID("1229"); // No Match.
1016 VerifyEventCount();
1017 VerifyNoMatch();
1018 }
1019
TEST_P(MobileOperatorInfoMainTest,MNOByMCCMNCAndSID)1020 TEST_P(MobileOperatorInfoMainTest, MNOByMCCMNCAndSID) {
1021 // message: Has an MNO with no MVNO.
1022 // match by: SID / MCCMNC alternately.
1023 // verify: Observer event, uuid.
1024
1025 ExpectEventCount(0);
1026 UpdateMCCMNC("123999"); // NO match.
1027 UpdateSID("1239"); // No match.
1028 VerifyEventCount();
1029 VerifyNoMatch();
1030
1031 ExpectEventCount(1);
1032 UpdateMCCMNC("123001");
1033 VerifyEventCount();
1034 VerifyMNOWithUUID("uuid123001");
1035
1036 ExpectEventCount(1);
1037 operator_info_->Reset();
1038 VerifyEventCount();
1039 VerifyNoMatch();
1040
1041 ExpectEventCount(1);
1042 UpdateSID("1232");
1043 VerifyEventCount();
1044 VerifyMNOWithUUID("uuid1232");
1045
1046 ExpectEventCount(1);
1047 operator_info_->Reset();
1048 VerifyEventCount();
1049 VerifyNoMatch();
1050
1051 ExpectEventCount(1);
1052 UpdateMCCMNC("123001");
1053 VerifyEventCount();
1054 VerifyMNOWithUUID("uuid123001");
1055 }
1056
1057 class MobileOperatorInfoDataTest : public MobileOperatorInfoMainTest {
1058 public:
MobileOperatorInfoDataTest()1059 MobileOperatorInfoDataTest() : MobileOperatorInfoMainTest() {}
1060
1061 // Same as MobileOperatorInfoMainTest, except that the database used is
1062 // different.
SetUp()1063 virtual void SetUp() {
1064 operator_info_->ClearDatabasePaths();
1065 AddDatabase(mobile_operator_db::data_test,
1066 arraysize(mobile_operator_db::data_test));
1067 operator_info_->Init();
1068 operator_info_->AddObserver(&observer_);
1069 }
1070
1071 protected:
1072 // This is a function that does a best effort verification of the information
1073 // that is obtained from the database by the MobileOperatorInfo object against
1074 // expectations stored in the form of data members in this class.
1075 // This is not a full proof check. In particular:
1076 // - It is unspecified in some case which of the values from a list is
1077 // exposed as a property. For example, at best, we can check that |sid| is
1078 // non-empty.
1079 // - It is not robust to "" as property values at times.
VerifyDatabaseData()1080 void VerifyDatabaseData() {
1081 EXPECT_EQ(country_, operator_info_->country());
1082 EXPECT_EQ(requires_roaming_, operator_info_->requires_roaming());
1083 EXPECT_EQ(activation_code_, operator_info_->activation_code());
1084
1085 EXPECT_EQ(mccmnc_list_.size(), operator_info_->mccmnc_list().size());
1086 set<string> mccmnc_set(operator_info_->mccmnc_list().begin(),
1087 operator_info_->mccmnc_list().end());
1088 for (const auto& mccmnc : mccmnc_list_) {
1089 EXPECT_TRUE(mccmnc_set.find(mccmnc) != mccmnc_set.end());
1090 }
1091 if (mccmnc_list_.size() > 0) {
1092 // It is not specified which entry will be chosen, but mccmnc() must be
1093 // non empty.
1094 EXPECT_FALSE(operator_info_->mccmnc().empty());
1095 }
1096
1097 VerifyNameListsMatch(operator_name_list_,
1098 operator_info_->operator_name_list());
1099
1100 // This comparison breaks if two apns have the same |apn| field.
1101 EXPECT_EQ(apn_list_.size(), operator_info_->apn_list().size());
1102 map<string, const MobileOperatorInfo::MobileAPN*> mobile_apns;
1103 for (const auto& apn_node : operator_info_->apn_list()) {
1104 mobile_apns[apn_node->apn] = apn_node;
1105 }
1106 for (const auto& apn_lhs : apn_list_) {
1107 ASSERT_TRUE(mobile_apns.find(apn_lhs->apn) != mobile_apns.end());
1108 const auto& apn_rhs = mobile_apns[apn_lhs->apn];
1109 // Only comparing apn, name, username, password.
1110 EXPECT_EQ(apn_lhs->apn, apn_rhs->apn);
1111 EXPECT_EQ(apn_lhs->username, apn_rhs->username);
1112 EXPECT_EQ(apn_lhs->password, apn_rhs->password);
1113 VerifyNameListsMatch(apn_lhs->operator_name_list,
1114 apn_rhs->operator_name_list);
1115 }
1116
1117 EXPECT_EQ(olp_list_.size(), operator_info_->olp_list().size());
1118 // This comparison breaks if two OLPs have the same |url|.
1119 map<string, MobileOperatorInfo::OnlinePortal> olps;
1120 for (const auto& olp : operator_info_->olp_list()) {
1121 olps[olp.url] = olp;
1122 }
1123 for (const auto& olp : olp_list_) {
1124 ASSERT_TRUE(olps.find(olp.url) != olps.end());
1125 const auto& olp_rhs = olps[olp.url];
1126 EXPECT_EQ(olp.method, olp_rhs.method);
1127 EXPECT_EQ(olp.post_data, olp_rhs.post_data);
1128 }
1129
1130 EXPECT_EQ(sid_list_.size(), operator_info_->sid_list().size());
1131 set<string> sid_set(operator_info_->sid_list().begin(),
1132 operator_info_->sid_list().end());
1133 for (const auto& sid : sid_list_) {
1134 EXPECT_TRUE(sid_set.find(sid) != sid_set.end());
1135 }
1136 if (sid_list_.size() > 0) {
1137 // It is not specified which entry will be chosen, but |sid()| must be
1138 // non-empty.
1139 EXPECT_FALSE(operator_info_->sid().empty());
1140 }
1141 }
1142
1143 // This function does some extra checks for the user data that can not be done
1144 // when data is obtained from the database.
VerifyUserData()1145 void VerifyUserData() {
1146 EXPECT_EQ(sid_, operator_info_->sid());
1147 }
1148
VerifyNameListsMatch(const vector<MobileOperatorInfo::LocalizedName> & operator_name_list_lhs,const vector<MobileOperatorInfo::LocalizedName> & operator_name_list_rhs)1149 void VerifyNameListsMatch(
1150 const vector<MobileOperatorInfo::LocalizedName>& operator_name_list_lhs,
1151 const vector<MobileOperatorInfo::LocalizedName>& operator_name_list_rhs) {
1152 // This comparison breaks if two localized names have the same |name|.
1153 map<string, MobileOperatorInfo::LocalizedName> localized_names;
1154 for (const auto& localized_name : operator_name_list_rhs) {
1155 localized_names[localized_name.name] = localized_name;
1156 }
1157 for (const auto& localized_name : operator_name_list_lhs) {
1158 EXPECT_TRUE(localized_names.find(localized_name.name) !=
1159 localized_names.end());
1160 EXPECT_EQ(localized_name.language,
1161 localized_names[localized_name.name].language);
1162 }
1163 }
1164
1165 // Use this function to pre-popluate all the data members of this object with
1166 // values matching the MNO for the database in |data_test.prototxt|.
PopulateMNOData()1167 void PopulateMNOData() {
1168 country_ = "us";
1169 requires_roaming_ = true;
1170 activation_code_ = "open sesame";
1171
1172 mccmnc_list_.clear();
1173 mccmnc_list_.push_back("200001");
1174 mccmnc_list_.push_back("200002");
1175 mccmnc_list_.push_back("200003");
1176
1177 operator_name_list_.clear();
1178 operator_name_list_.push_back({"name200001", "en"});
1179 operator_name_list_.push_back({"name200002", ""});
1180
1181 apn_list_.clear();
1182 MobileOperatorInfo::MobileAPN* apn;
1183 apn = new MobileOperatorInfo::MobileAPN();
1184 apn->apn = "test@test.com";
1185 apn->username = "testuser";
1186 apn->password = "is_public_boohoohoo";
1187 apn->operator_name_list.push_back({"name200003", "hi"});
1188 apn_list_.push_back(apn); // Takes ownership.
1189
1190 olp_list_.clear();
1191 olp_list_.push_back({"some@random.com", "POST", "random_data"});
1192
1193 sid_list_.clear();
1194 sid_list_.push_back("200123");
1195 sid_list_.push_back("200234");
1196 sid_list_.push_back("200345");
1197 }
1198
1199 // Use this function to pre-populate all the data members of this object with
1200 // values matching the MVNO for the database in |data_test.prototext|.
PopulateMVNOData()1201 void PopulateMVNOData() {
1202 country_ = "ca";
1203 requires_roaming_ = false;
1204 activation_code_ = "khul ja sim sim";
1205
1206 mccmnc_list_.clear();
1207 mccmnc_list_.push_back("200001");
1208 mccmnc_list_.push_back("200102");
1209
1210 operator_name_list_.clear();
1211 operator_name_list_.push_back({"name200101", "en"});
1212 operator_name_list_.push_back({"name200102", ""});
1213
1214 apn_list_.clear();
1215 MobileOperatorInfo::MobileAPN* apn;
1216 apn = new MobileOperatorInfo::MobileAPN();
1217 apn->apn = "test2@test.com";
1218 apn->username = "testuser2";
1219 apn->password = "is_public_boohoohoo_too";
1220 apn_list_.push_back(apn); // Takes ownership.
1221
1222 olp_list_.clear();
1223 olp_list_.push_back({"someother@random.com", "GET", ""});
1224
1225 sid_list_.clear();
1226 sid_list_.push_back("200345");
1227 }
1228
1229 // Data to be verified against the database.
1230 string country_;
1231 bool requires_roaming_;
1232 string activation_code_;
1233 vector<string> mccmnc_list_;
1234 vector<MobileOperatorInfo::LocalizedName> operator_name_list_;
1235 ScopedVector<MobileOperatorInfo::MobileAPN> apn_list_;
1236 vector<MobileOperatorInfo::OnlinePortal> olp_list_;
1237 vector<string> sid_list_;
1238
1239 // Extra data to be verified only against user updates.
1240 string sid_;
1241
1242 private:
1243 DISALLOW_COPY_AND_ASSIGN(MobileOperatorInfoDataTest);
1244 };
1245
1246
TEST_P(MobileOperatorInfoDataTest,MNODetailedInformation)1247 TEST_P(MobileOperatorInfoDataTest, MNODetailedInformation) {
1248 // message: MNO with all the information filled in.
1249 // match by: MNO matches by MCCMNC
1250 // verify: All information is correctly loaded.
1251 ExpectEventCount(1);
1252 UpdateMCCMNC("200001");
1253 VerifyEventCount();
1254 VerifyMNOWithUUID("uuid200001");
1255
1256 PopulateMNOData();
1257 VerifyDatabaseData();
1258 }
1259
TEST_P(MobileOperatorInfoDataTest,MVNOInheritsInformation)1260 TEST_P(MobileOperatorInfoDataTest, MVNOInheritsInformation) {
1261 // message: MVNO with name filter.
1262 // verify: All the missing fields are carried over to the MVNO from MNO.
1263 ExpectEventCount(2);
1264 UpdateMCCMNC("200001");
1265 UpdateOperatorName("name200201");
1266 VerifyEventCount();
1267 VerifyMVNOWithUUID("uuid200201");
1268
1269 PopulateMNOData();
1270 VerifyDatabaseData();
1271 }
1272
TEST_P(MobileOperatorInfoDataTest,MVNOOverridesInformation)1273 TEST_P(MobileOperatorInfoDataTest, MVNOOverridesInformation) {
1274 // match by: MNO matches by MCCMNC, MVNO by name.
1275 // verify: All information is correctly loaded.
1276 // The MVNO in this case overrides the information provided by MNO.
1277 ExpectEventCount(2);
1278 UpdateMCCMNC("200001");
1279 UpdateOperatorName("name200101");
1280 VerifyEventCount();
1281 VerifyMVNOWithUUID("uuid200101");
1282
1283 PopulateMVNOData();
1284 VerifyDatabaseData();
1285 }
1286
TEST_P(MobileOperatorInfoDataTest,NoUpdatesBeforeMNOMatch)1287 TEST_P(MobileOperatorInfoDataTest, NoUpdatesBeforeMNOMatch) {
1288 // message: MVNO.
1289 // - do not match MNO with mccmnc/name
1290 // - on different updates, verify no events.
1291 ExpectEventCount(0);
1292 UpdateMCCMNC("200999"); // No match.
1293 UpdateOperatorName("name200001"); // matches MNO
1294 UpdateOperatorName("name200101"); // matches MVNO filter.
1295 UpdateSID("200999"); // No match.
1296 VerifyEventCount();
1297 VerifyNoMatch();
1298 }
1299
TEST_P(MobileOperatorInfoDataTest,UserUpdatesOverrideMVNO)1300 TEST_P(MobileOperatorInfoDataTest, UserUpdatesOverrideMVNO) {
1301 // - match MVNO.
1302 // - send updates to properties and verify events are raised and values of
1303 // updated properties override the ones provided by the database.
1304 string imsi {"2009991234512345"};
1305 string iccid {"200999123456789"};
1306 string olp_url {"url@url.com"};
1307 string olp_method {"POST"};
1308 string olp_post_data {"data"};
1309
1310 // Determine MVNO.
1311 ExpectEventCount(2);
1312 UpdateMCCMNC("200001");
1313 UpdateOperatorName("name200101");
1314 VerifyEventCount();
1315 VerifyMVNOWithUUID("uuid200101");
1316
1317 // Send updates.
1318 ExpectEventCount(1);
1319 UpdateOnlinePortal(olp_url, olp_method, olp_post_data);
1320 UpdateIMSI(imsi);
1321 // No event raised because imsi is not exposed.
1322 UpdateICCID(iccid);
1323 // No event raised because ICCID is not exposed.
1324
1325 VerifyEventCount();
1326
1327 // Update our expectations.
1328 PopulateMVNOData();
1329 olp_list_.push_back({olp_url, olp_method, olp_post_data});
1330
1331 VerifyDatabaseData();
1332 }
1333
TEST_P(MobileOperatorInfoDataTest,CachedUserUpdatesOverrideMVNO)1334 TEST_P(MobileOperatorInfoDataTest, CachedUserUpdatesOverrideMVNO) {
1335 // message: MVNO.
1336 // - First send updates that don't identify an MNO.
1337 // - Then identify an MNO and MVNO.
1338 // - verify that all the earlier updates are cached, and override the MVNO
1339 // information.
1340 string imsi {"2009991234512345"};
1341 string iccid {"200999123456789"};
1342 string sid {"200999"};
1343 string olp_url {"url@url.com"};
1344 string olp_method {"POST"};
1345 string olp_post_data {"data"};
1346
1347 // Send updates.
1348 ExpectEventCount(0);
1349 UpdateSID(sid);
1350 UpdateOnlinePortal(olp_url, olp_method, olp_post_data);
1351 UpdateIMSI(imsi);
1352 UpdateICCID(iccid);
1353 VerifyEventCount();
1354
1355 // Determine MVNO.
1356 ExpectEventCount(2);
1357 UpdateMCCMNC("200001");
1358 UpdateOperatorName("name200101");
1359 VerifyEventCount();
1360 VerifyMVNOWithUUID("uuid200101");
1361
1362 // Update our expectations.
1363 PopulateMVNOData();
1364 sid_ = sid;
1365 sid_list_.push_back(sid);
1366 olp_list_.push_back({olp_url, olp_method, olp_post_data});
1367
1368 VerifyDatabaseData();
1369 VerifyUserData();
1370 }
1371
TEST_P(MobileOperatorInfoDataTest,RedundantUserUpdatesMVNO)1372 TEST_P(MobileOperatorInfoDataTest, RedundantUserUpdatesMVNO) {
1373 // - match MVNO.
1374 // - send redundant updates to properties.
1375 // - Verify no events, no updates to properties.
1376
1377 // Identify MVNO.
1378 ExpectEventCount(2);
1379 UpdateMCCMNC("200001");
1380 UpdateOperatorName("name200101");
1381 VerifyEventCount();
1382 VerifyMVNOWithUUID("uuid200101");
1383
1384 // Send redundant updates.
1385 // TODO(pprabhu)
1386 // |UpdateOnlinePortal| leads to an event because this is the first time this
1387 // value are set *by the user*. Although the values from the database were the
1388 // same, we did not use those values for filters. It would be ideal to not
1389 // raise these redundant events (since no public information about the object
1390 // changed), but I haven't invested in doing so yet.
1391 ExpectEventCount(1);
1392 UpdateOperatorName(operator_info_->operator_name());
1393 UpdateOnlinePortal("someother@random.com", "GET", "");
1394 VerifyEventCount();
1395 PopulateMVNOData();
1396 VerifyDatabaseData();
1397 }
1398
TEST_P(MobileOperatorInfoDataTest,RedundantCachedUpdatesMVNO)1399 TEST_P(MobileOperatorInfoDataTest, RedundantCachedUpdatesMVNO) {
1400 // message: MVNO.
1401 // - First send updates that don't identify MVNO, but match the data.
1402 // - Then idenityf an MNO and MVNO.
1403 // - verify that redundant information occurs only once.
1404
1405 // Send redundant updates.
1406 ExpectEventCount(2);
1407 UpdateSID(operator_info_->sid());
1408 UpdateOperatorName(operator_info_->operator_name());
1409 UpdateOnlinePortal("someother@random.com", "GET", "");
1410
1411 // Identify MVNO.
1412 UpdateMCCMNC("200001");
1413 UpdateOperatorName("name200101");
1414 VerifyEventCount();
1415 VerifyMVNOWithUUID("uuid200101");
1416
1417 PopulateMVNOData();
1418 VerifyDatabaseData();
1419 }
1420
TEST_P(MobileOperatorInfoDataTest,ResetClearsInformation)1421 TEST_P(MobileOperatorInfoDataTest, ResetClearsInformation) {
1422 // Repeatedly reset the object and check M[V]NO identification and data.
1423 ExpectEventCount(2);
1424 UpdateMCCMNC("200001");
1425 UpdateOperatorName("name200201");
1426 VerifyEventCount();
1427 VerifyMVNOWithUUID("uuid200201");
1428 PopulateMNOData();
1429 VerifyDatabaseData();
1430
1431 ExpectEventCount(1);
1432 operator_info_->Reset();
1433 VerifyEventCount();
1434 VerifyNoMatch();
1435
1436 ExpectEventCount(2);
1437 UpdateMCCMNC("200001");
1438 UpdateOperatorName("name200101");
1439 VerifyEventCount();
1440 VerifyMVNOWithUUID("uuid200101");
1441 PopulateMVNOData();
1442 VerifyDatabaseData();
1443
1444 ExpectEventCount(1);
1445 operator_info_->Reset();
1446 VerifyEventCount();
1447 VerifyNoMatch();
1448
1449 ExpectEventCount(1);
1450 UpdateMCCMNC("200001");
1451 VerifyEventCount();
1452 VerifyMNOWithUUID("uuid200001");
1453 PopulateMNOData();
1454 VerifyDatabaseData();
1455 }
1456
TEST_P(MobileOperatorInfoDataTest,FilteredOLP)1457 TEST_P(MobileOperatorInfoDataTest, FilteredOLP) {
1458 // We only check basic filter matching, using the fact that the regex matching
1459 // code is shared with the MVNO filtering, and is already well tested.
1460 // (1) None of the filters match.
1461 ExpectEventCount(1);
1462 UpdateMCCMNC("200001");
1463 VerifyEventCount();
1464 VerifyMNOWithUUID("uuid200001");
1465
1466 ASSERT_EQ(1, operator_info_->olp_list().size());
1467 // Just check that the filtered OLPs are not in the list.
1468 EXPECT_NE("olp@mccmnc", operator_info_->olp_list()[0].url);
1469 EXPECT_NE("olp@sid", operator_info_->olp_list()[0].url);
1470
1471 // (2) MCCMNC filter matches.
1472 ExpectEventCount(1);
1473 operator_info_->Reset();
1474 VerifyEventCount();
1475 VerifyNoMatch();
1476
1477 ExpectEventCount(1);
1478 UpdateMCCMNC("200003");
1479 VerifyEventCount();
1480 VerifyMNOWithUUID("uuid200001");
1481
1482 ASSERT_EQ(2, operator_info_->olp_list().size());
1483 EXPECT_NE("olp@sid", operator_info_->olp_list()[0].url);
1484 bool found_olp_by_mccmnc = false;
1485 for (const auto& olp : operator_info_->olp_list()) {
1486 found_olp_by_mccmnc |= ("olp@mccmnc" == olp.url);
1487 }
1488 EXPECT_TRUE(found_olp_by_mccmnc);
1489
1490 // (3) SID filter matches.
1491 ExpectEventCount(1);
1492 operator_info_->Reset();
1493 VerifyEventCount();
1494 VerifyNoMatch();
1495
1496 ExpectEventCount(1);
1497 UpdateSID("200345");
1498 VerifyEventCount();
1499 VerifyMNOWithUUID("uuid200001");
1500
1501 ASSERT_EQ(2, operator_info_->olp_list().size());
1502 EXPECT_NE("olp@mccmnc", operator_info_->olp_list()[0].url);
1503 bool found_olp_by_sid = false;
1504 for (const auto& olp : operator_info_->olp_list()) {
1505 found_olp_by_sid |= ("olp@sid" == olp.url);
1506 }
1507 EXPECT_TRUE(found_olp_by_sid);
1508 }
1509
1510 class MobileOperatorInfoObserverTest : public MobileOperatorInfoMainTest {
1511 public:
MobileOperatorInfoObserverTest()1512 MobileOperatorInfoObserverTest() : MobileOperatorInfoMainTest() {}
1513
1514 // Same as |MobileOperatorInfoMainTest::SetUp|, except that we don't add a
1515 // default observer.
SetUp()1516 virtual void SetUp() {
1517 operator_info_->ClearDatabasePaths();
1518 AddDatabase(mobile_operator_db::data_test,
1519 arraysize(mobile_operator_db::data_test));
1520 operator_info_->Init();
1521 }
1522
1523 protected:
1524 // ///////////////////////////////////////////////////////////////////////////
1525 // Data.
1526 MockMobileOperatorInfoObserver second_observer_;
1527
1528 private:
1529 DISALLOW_COPY_AND_ASSIGN(MobileOperatorInfoObserverTest);
1530 };
1531
TEST_P(MobileOperatorInfoObserverTest,NoObserver)1532 TEST_P(MobileOperatorInfoObserverTest, NoObserver) {
1533 // - Don't add any observers, and then cause an MVNO update to occur.
1534 // - Verify no crash.
1535 UpdateMCCMNC("200001");
1536 UpdateOperatorName("name200101");
1537 }
1538
TEST_P(MobileOperatorInfoObserverTest,MultipleObservers)1539 TEST_P(MobileOperatorInfoObserverTest, MultipleObservers) {
1540 // - Add two observers, and then cause an MVNO update to occur.
1541 // - Verify both observers are notified.
1542 operator_info_->AddObserver(&observer_);
1543 operator_info_->AddObserver(&second_observer_);
1544
1545 EXPECT_CALL(observer_, OnOperatorChanged()).Times(2);
1546 EXPECT_CALL(second_observer_, OnOperatorChanged()).Times(2);
1547 UpdateMCCMNC("200001");
1548 UpdateOperatorName("name200101");
1549 VerifyMVNOWithUUID("uuid200101");
1550
1551 dispatcher_.DispatchPendingEvents();
1552 }
1553
TEST_P(MobileOperatorInfoObserverTest,LateObserver)1554 TEST_P(MobileOperatorInfoObserverTest, LateObserver) {
1555 // - Add one observer, and verify it gets an MVNO update.
1556 operator_info_->AddObserver(&observer_);
1557
1558 EXPECT_CALL(observer_, OnOperatorChanged()).Times(2);
1559 EXPECT_CALL(second_observer_, OnOperatorChanged()).Times(0);
1560 UpdateMCCMNC("200001");
1561 UpdateOperatorName("name200101");
1562 VerifyMVNOWithUUID("uuid200101");
1563 dispatcher_.DispatchPendingEvents();
1564 Mock::VerifyAndClearExpectations(&observer_);
1565 Mock::VerifyAndClearExpectations(&second_observer_);
1566
1567 EXPECT_CALL(observer_, OnOperatorChanged()).Times(1);
1568 EXPECT_CALL(second_observer_, OnOperatorChanged()).Times(0);
1569 operator_info_->Reset();
1570 VerifyNoMatch();
1571 dispatcher_.DispatchPendingEvents();
1572 Mock::VerifyAndClearExpectations(&observer_);
1573 Mock::VerifyAndClearExpectations(&second_observer_);
1574
1575 // - Add another observer, verify both get an MVNO update.
1576 operator_info_->AddObserver(&second_observer_);
1577
1578 EXPECT_CALL(observer_, OnOperatorChanged()).Times(2);
1579 EXPECT_CALL(second_observer_, OnOperatorChanged()).Times(2);
1580 UpdateMCCMNC("200001");
1581 UpdateOperatorName("name200101");
1582 VerifyMVNOWithUUID("uuid200101");
1583 dispatcher_.DispatchPendingEvents();
1584 Mock::VerifyAndClearExpectations(&observer_);
1585 Mock::VerifyAndClearExpectations(&second_observer_);
1586
1587 EXPECT_CALL(observer_, OnOperatorChanged()).Times(1);
1588 EXPECT_CALL(second_observer_, OnOperatorChanged()).Times(1);
1589 operator_info_->Reset();
1590 VerifyNoMatch();
1591 dispatcher_.DispatchPendingEvents();
1592 Mock::VerifyAndClearExpectations(&observer_);
1593 Mock::VerifyAndClearExpectations(&second_observer_);
1594
1595 // - Remove an observer, verify it no longer gets updates.
1596 operator_info_->RemoveObserver(&observer_);
1597
1598 EXPECT_CALL(observer_, OnOperatorChanged()).Times(0);
1599 EXPECT_CALL(second_observer_, OnOperatorChanged()).Times(2);
1600 UpdateMCCMNC("200001");
1601 UpdateOperatorName("name200101");
1602 VerifyMVNOWithUUID("uuid200101");
1603 dispatcher_.DispatchPendingEvents();
1604 Mock::VerifyAndClearExpectations(&observer_);
1605 Mock::VerifyAndClearExpectations(&second_observer_);
1606
1607 EXPECT_CALL(observer_, OnOperatorChanged()).Times(0);
1608 EXPECT_CALL(second_observer_, OnOperatorChanged()).Times(1);
1609 operator_info_->Reset();
1610 VerifyNoMatch();
1611 dispatcher_.DispatchPendingEvents();
1612 Mock::VerifyAndClearExpectations(&observer_);
1613 Mock::VerifyAndClearExpectations(&second_observer_);
1614 }
1615
1616 INSTANTIATE_TEST_CASE_P(MobileOperatorInfoMainTestInstance,
1617 MobileOperatorInfoMainTest,
1618 Values(kEventCheckingPolicyStrict,
1619 kEventCheckingPolicyNonStrict));
1620 INSTANTIATE_TEST_CASE_P(MobileOperatorInfoDataTestInstance,
1621 MobileOperatorInfoDataTest,
1622 Values(kEventCheckingPolicyStrict,
1623 kEventCheckingPolicyNonStrict));
1624 // It only makes sense to do strict checking here.
1625 INSTANTIATE_TEST_CASE_P(MobileOperatorInfoObserverTestInstance,
1626 MobileOperatorInfoObserverTest,
1627 Values(kEventCheckingPolicyStrict));
1628 } // namespace shill
1629