blob: 24088499a27659c080c9ee0873c96279963d8bfa [file] [log] [blame]
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -07001// Copyright (c) 2014 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Ben Chanc54afe52014-11-05 10:28:08 -08005#include "shill/cellular/mobile_operator_info.h"
Alex Deymocddd2d02014-11-10 19:55:35 -08006
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -07007#include <fstream>
8#include <map>
9#include <ostream>
10#include <set>
11#include <vector>
12
Ben Chan11c213f2014-09-05 08:21:06 -070013#include <base/files/file_util.h>
Ben Chancc67c522014-09-03 07:19:18 -070014#include <base/macros.h>
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -070015#include <gmock/gmock.h>
16#include <gtest/gtest.h>
17
Ben Chanc54afe52014-11-05 10:28:08 -080018#include "shill/cellular/mobile_operator_info_impl.h"
19#include "shill/event_dispatcher.h"
20#include "shill/logging.h"
21
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -070022// These files contain binary protobuf definitions used by the following tests
23// inside the namespace ::mobile_operator_db
24#define IN_MOBILE_OPERATOR_INFO_UNITTEST_CC
Ben Chanc54afe52014-11-05 10:28:08 -080025#include "shill/mobile_operator_db/test_protos/data_test.h"
26#include "shill/mobile_operator_db/test_protos/init_test_empty_db_init.h"
27#include "shill/mobile_operator_db/test_protos/init_test_multiple_db_init_1.h"
28#include "shill/mobile_operator_db/test_protos/init_test_multiple_db_init_2.h"
29#include "shill/mobile_operator_db/test_protos/init_test_successful_init.h"
30#include "shill/mobile_operator_db/test_protos/main_test.h"
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -070031#undef IN_MOBILE_OPERATOR_INFO_UNITTEST_CC
32
33using base::FilePath;
34using shill::mobile_operator_db::MobileOperatorDB;
35using std::map;
36using std::ofstream;
37using std::set;
38using std::string;
39using std::vector;
40using testing::Mock;
41using testing::Test;
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -070042using testing::Values;
43using testing::WithParamInterface;
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -070044
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -070045// The tests run from the fixture |MobileOperatorInfoMainTest| and
46// |MobileOperatorDataTest| can be run in two modes:
47// - strict event checking: We check that an event is raised for each update
48// to the state of the object.
49// - non-strict event checking: We check that a single event is raised as a
50// result of many updates to the object.
51// The first case corresponds to a very aggressive event loop, that dispatches
52// events as soon as they are posted; the second one corresponds to an
53// over-crowded event loop that only dispatches events just before we verify
54// that events were raised.
55//
56// We use ::testing::WithParamInterface to templatize the test fixtures to do
57// string/non-strict event checking. When writing test cases using these
58// fixtures, use the |Update*|, |ExpectEventCount|, |VerifyEventCount| functions
59// provided by the fixture, and write the test as if event checking is strict.
60//
61// For |MobileOperatorObserverTest|, only the strict event checking case makes
62// sense, so we only instantiate that.
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -070063namespace shill {
64
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -070065namespace {
66
67enum EventCheckingPolicy {
68 kEventCheckingPolicyStrict,
69 kEventCheckingPolicyNonStrict
70};
71
72} // namespace
73
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -070074class MockMobileOperatorInfoObserver : public MobileOperatorInfo::Observer {
75 public:
76 MockMobileOperatorInfoObserver() {}
77 virtual ~MockMobileOperatorInfoObserver() {}
78
79 MOCK_METHOD0(OnOperatorChanged, void());
80};
81
82class MobileOperatorInfoInitTest : public Test {
83 public:
84 MobileOperatorInfoInitTest()
Miao-chen Chou70190b32014-05-14 18:24:30 -070085 : operator_info_(new MobileOperatorInfo(&dispatcher_, "Operator")),
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -070086 operator_info_impl_(operator_info_->impl()) {}
87
Alex Vakulenko016fa0e2014-08-11 15:59:58 -070088 void TearDown() override {
Paul Stewart2f6c7892015-06-16 13:13:10 -070089 for (const auto& tmp_db_path : tmp_db_paths_) {
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -070090 base::DeleteFile(tmp_db_path, false);
91 }
92 }
93
94 protected:
95 void AddDatabase(const unsigned char database_data[], size_t num_elems) {
96 FilePath tmp_db_path;
97 CHECK(base::CreateTemporaryFile(&tmp_db_path));
98 tmp_db_paths_.push_back(tmp_db_path);
99
100 ofstream tmp_db(tmp_db_path.value(), ofstream::binary);
101 for (size_t i = 0; i < num_elems; ++i) {
102 tmp_db << database_data[i];
103 }
104 tmp_db.close();
105 operator_info_->AddDatabasePath(tmp_db_path);
106 }
107
108 void AssertDatabaseEmpty() {
109 EXPECT_EQ(0, operator_info_impl_->database()->mno_size());
110 EXPECT_EQ(0, operator_info_impl_->database()->imvno_size());
111 }
112
Paul Stewart2f6c7892015-06-16 13:13:10 -0700113 const MobileOperatorDB* GetDatabase() {
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -0700114 return operator_info_impl_->database();
115 }
116
117 EventDispatcher dispatcher_;
118 vector<FilePath> tmp_db_paths_;
Ben Chanc20ed132014-10-16 12:25:03 -0700119 std::unique_ptr<MobileOperatorInfo> operator_info_;
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -0700120 // Owned by |operator_info_| and tied to its life cycle.
Paul Stewart2f6c7892015-06-16 13:13:10 -0700121 MobileOperatorInfoImpl* operator_info_impl_;
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -0700122
Alex Vakulenko8a532292014-06-16 17:18:44 -0700123 private:
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -0700124 DISALLOW_COPY_AND_ASSIGN(MobileOperatorInfoInitTest);
125};
126
127TEST_F(MobileOperatorInfoInitTest, FailedInitNoPath) {
128 // - Initialize object with no database paths set
129 // - Verify that initialization fails.
130 operator_info_->ClearDatabasePaths();
131 EXPECT_FALSE(operator_info_->Init());
132 AssertDatabaseEmpty();
133}
134
135TEST_F(MobileOperatorInfoInitTest, FailedInitBadPath) {
136 // - Initialize object with non-existent path.
137 // - Verify that initialization fails.
138 const FilePath database_path("nonexistent.pbf");
139 operator_info_->ClearDatabasePaths();
140 operator_info_->AddDatabasePath(database_path);
141 EXPECT_FALSE(operator_info_->Init());
142 AssertDatabaseEmpty();
143}
144
145TEST_F(MobileOperatorInfoInitTest, FailedInitBadDatabase) {
146 // - Initialize object with malformed database.
147 // - Verify that initialization fails.
148 // TODO(pprabhu): It's hard to get a malformed database in binary format.
149}
150
151TEST_F(MobileOperatorInfoInitTest, EmptyDBInit) {
152 // - Initialize the object with a database file that is empty.
153 // - Verify that initialization succeeds, and that the database is empty.
154 operator_info_->ClearDatabasePaths();
155 // Can't use arraysize on empty array.
156 AddDatabase(mobile_operator_db::init_test_empty_db_init, 0);
157 EXPECT_TRUE(operator_info_->Init());
158 AssertDatabaseEmpty();
159}
160
161TEST_F(MobileOperatorInfoInitTest, SuccessfulInit) {
162 operator_info_->ClearDatabasePaths();
163 AddDatabase(mobile_operator_db::init_test_successful_init,
164 arraysize(mobile_operator_db::init_test_successful_init));
165 EXPECT_TRUE(operator_info_->Init());
166 EXPECT_GT(GetDatabase()->mno_size(), 0);
167 EXPECT_GT(GetDatabase()->imvno_size(), 0);
168}
169
170TEST_F(MobileOperatorInfoInitTest, MultipleDBInit) {
171 // - Initialize the object with two database files.
172 // - Verify that intialization succeeds, and both databases are loaded.
173 operator_info_->ClearDatabasePaths();
174 AddDatabase(mobile_operator_db::init_test_multiple_db_init_1,
175 arraysize(mobile_operator_db::init_test_multiple_db_init_1));
176 AddDatabase(mobile_operator_db::init_test_multiple_db_init_2,
177 arraysize(mobile_operator_db::init_test_multiple_db_init_2));
178 operator_info_->Init();
179 EXPECT_GT(GetDatabase()->mno_size(), 0);
180 EXPECT_GT(GetDatabase()->imvno_size(), 0);
181}
182
183TEST_F(MobileOperatorInfoInitTest, InitWithObserver) {
184 // - Add an Observer.
185 // - Initialize the object with empty database file.
186 // - Verify innitialization succeeds.
187 MockMobileOperatorInfoObserver dumb_observer;
188
189 operator_info_->ClearDatabasePaths();
190 // Can't use arraysize with empty array.
191 AddDatabase(mobile_operator_db::init_test_empty_db_init, 0);
192 operator_info_->AddObserver(&dumb_observer);
193 EXPECT_TRUE(operator_info_->Init());
194}
195
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -0700196class MobileOperatorInfoMainTest
197 : public MobileOperatorInfoInitTest,
198 public WithParamInterface<EventCheckingPolicy> {
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -0700199 public:
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -0700200 MobileOperatorInfoMainTest()
201 : MobileOperatorInfoInitTest(),
202 event_checking_policy_(GetParam()) {}
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -0700203
204 virtual void SetUp() {
205 operator_info_->ClearDatabasePaths();
206 AddDatabase(mobile_operator_db::main_test,
207 arraysize(mobile_operator_db::main_test));
208 operator_info_->Init();
209 operator_info_->AddObserver(&observer_);
210 }
211
212 protected:
213 // ///////////////////////////////////////////////////////////////////////////
214 // Helper functions.
Paul Stewart2f6c7892015-06-16 13:13:10 -0700215 void VerifyMNOWithUUID(const string& uuid) {
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -0700216 EXPECT_TRUE(operator_info_->IsMobileNetworkOperatorKnown());
217 EXPECT_FALSE(operator_info_->IsMobileVirtualNetworkOperatorKnown());
218 EXPECT_EQ(uuid, operator_info_->uuid());
219 }
220
Paul Stewart2f6c7892015-06-16 13:13:10 -0700221 void VerifyMVNOWithUUID(const string& uuid) {
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -0700222 EXPECT_TRUE(operator_info_->IsMobileNetworkOperatorKnown());
223 EXPECT_TRUE(operator_info_->IsMobileVirtualNetworkOperatorKnown());
224 EXPECT_EQ(uuid, operator_info_->uuid());
225 }
226
227 void VerifyNoMatch() {
228 EXPECT_FALSE(operator_info_->IsMobileNetworkOperatorKnown());
229 EXPECT_FALSE(operator_info_->IsMobileVirtualNetworkOperatorKnown());
230 EXPECT_EQ("", operator_info_->uuid());
231 }
232
233 void ExpectEventCount(int count) {
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -0700234 // In case we're running in the non-strict event checking mode, we only
235 // expect one overall event to be raised for all the updates.
236 if (event_checking_policy_ == kEventCheckingPolicyNonStrict) {
237 count = (count > 0) ? 1 : 0;
238 }
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -0700239 EXPECT_CALL(observer_, OnOperatorChanged()).Times(count);
240 }
241
242 void VerifyEventCount() {
243 dispatcher_.DispatchPendingEvents();
244 Mock::VerifyAndClearExpectations(&observer_);
245 }
246
247 void ResetOperatorInfo() {
248 operator_info_->Reset();
249 // Eat up any events caused by |Reset|.
250 dispatcher_.DispatchPendingEvents();
251 VerifyNoMatch();
252 }
253
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -0700254 // Use these wrappers to send updates to |operator_info_|. These wrappers
255 // optionally run the dispatcher if we want strict checking of the number of
256 // events raised.
Paul Stewart2f6c7892015-06-16 13:13:10 -0700257 void UpdateMCCMNC(const std::string& mccmnc) {
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -0700258 operator_info_->UpdateMCCMNC(mccmnc);
259 DispatchPendingEventsIfStrict();
260 }
261
Paul Stewart2f6c7892015-06-16 13:13:10 -0700262 void UpdateSID(const std::string& sid) {
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -0700263 operator_info_->UpdateSID(sid);
264 DispatchPendingEventsIfStrict();
265 }
266
Paul Stewart2f6c7892015-06-16 13:13:10 -0700267 void UpdateIMSI(const std::string& imsi) {
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -0700268 operator_info_->UpdateIMSI(imsi);
269 DispatchPendingEventsIfStrict();
270 }
271
Paul Stewart2f6c7892015-06-16 13:13:10 -0700272 void UpdateICCID(const std::string& iccid) {
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -0700273 operator_info_->UpdateICCID(iccid);
274 DispatchPendingEventsIfStrict();
275 }
276
Paul Stewart2f6c7892015-06-16 13:13:10 -0700277 void UpdateNID(const std::string& nid) {
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -0700278 operator_info_->UpdateNID(nid);
279 DispatchPendingEventsIfStrict();
280 }
281
Paul Stewart2f6c7892015-06-16 13:13:10 -0700282 void UpdateOperatorName(const std::string& operator_name) {
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -0700283 operator_info_->UpdateOperatorName(operator_name);
284 DispatchPendingEventsIfStrict();
285 }
286
Paul Stewart2f6c7892015-06-16 13:13:10 -0700287 void UpdateOnlinePortal(const std::string& url,
288 const std::string& method,
289 const std::string& post_data) {
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -0700290 operator_info_->UpdateOnlinePortal(url, method, post_data);
291 DispatchPendingEventsIfStrict();
292 }
293
294 void DispatchPendingEventsIfStrict() {
295 if (event_checking_policy_ == kEventCheckingPolicyStrict) {
296 dispatcher_.DispatchPendingEvents();
297 }
298 }
299
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -0700300 // ///////////////////////////////////////////////////////////////////////////
301 // Data.
302 MockMobileOperatorInfoObserver observer_;
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -0700303 const EventCheckingPolicy event_checking_policy_;
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -0700304
Alex Vakulenko8a532292014-06-16 17:18:44 -0700305 private:
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -0700306 DISALLOW_COPY_AND_ASSIGN(MobileOperatorInfoMainTest);
307};
308
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -0700309TEST_P(MobileOperatorInfoMainTest, InitialConditions) {
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -0700310 // - Initialize a new object.
311 // - Verify that all initial values of properties are reasonable.
312 EXPECT_FALSE(operator_info_->IsMobileNetworkOperatorKnown());
313 EXPECT_FALSE(operator_info_->IsMobileVirtualNetworkOperatorKnown());
314 EXPECT_TRUE(operator_info_->uuid().empty());
315 EXPECT_TRUE(operator_info_->operator_name().empty());
316 EXPECT_TRUE(operator_info_->country().empty());
317 EXPECT_TRUE(operator_info_->mccmnc().empty());
318 EXPECT_TRUE(operator_info_->sid().empty());
319 EXPECT_TRUE(operator_info_->nid().empty());
320 EXPECT_TRUE(operator_info_->mccmnc_list().empty());
321 EXPECT_TRUE(operator_info_->sid_list().empty());
322 EXPECT_TRUE(operator_info_->operator_name_list().empty());
323 EXPECT_TRUE(operator_info_->apn_list().empty());
324 EXPECT_TRUE(operator_info_->olp_list().empty());
325 EXPECT_TRUE(operator_info_->activation_code().empty());
326 EXPECT_FALSE(operator_info_->requires_roaming());
327}
328
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -0700329TEST_P(MobileOperatorInfoMainTest, MNOByMCCMNC) {
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -0700330 // message: Has an MNO with no MVNO.
331 // match by: MCCMNC.
332 // verify: Observer event, uuid.
333
334 ExpectEventCount(0);
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -0700335 UpdateMCCMNC("101999"); // No match.
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -0700336 VerifyEventCount();
337 VerifyNoMatch();
338
339 ExpectEventCount(1);
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -0700340 UpdateMCCMNC("101001");
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -0700341 VerifyEventCount();
342 VerifyMNOWithUUID("uuid101");
343
344 ExpectEventCount(1);
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -0700345 UpdateMCCMNC("101999");
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -0700346 VerifyEventCount();
347 VerifyNoMatch();
348}
349
Prathmesh Prabhu53f47a62014-07-16 14:06:55 -0700350TEST_P(MobileOperatorInfoMainTest, MNOByMCCMNCMultipleMCCMNCOptions) {
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -0700351 // message: Has an MNO with no MCCMNC.
352 // match by: One of the MCCMNCs of the multiple ones in the MNO.
353 // verify: Observer event, uuid.
354 ExpectEventCount(1);
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -0700355 UpdateMCCMNC("102002");
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -0700356 VerifyEventCount();
357 VerifyMNOWithUUID("uuid102");
358}
359
Prathmesh Prabhu53f47a62014-07-16 14:06:55 -0700360TEST_P(MobileOperatorInfoMainTest, MNOByMCCMNCMultipleMNOOptions) {
361 // message: Two messages with the same MCCMNC.
362 // match by: Both MNOs matched, one is earmarked.
363 // verify: The earmarked MNO is picked.
364 ExpectEventCount(1);
365 UpdateMCCMNC("124001");
366 VerifyEventCount();
367 VerifyMNOWithUUID("uuid124002");
368}
369
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -0700370TEST_P(MobileOperatorInfoMainTest, MNOByOperatorName) {
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -0700371 // message: Has an MNO with no MVNO.
372 // match by: OperatorName.
373 // verify: Observer event, uuid.
374 ExpectEventCount(0);
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -0700375 UpdateOperatorName("name103999"); // No match.
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -0700376 VerifyEventCount();
377 VerifyNoMatch();
378
379 ExpectEventCount(1);
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -0700380 UpdateOperatorName("name103");
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -0700381 VerifyEventCount();
382 VerifyMNOWithUUID("uuid103");
383
384 ExpectEventCount(1);
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -0700385 UpdateOperatorName("name103999"); // No match.
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -0700386 VerifyEventCount();
387 VerifyNoMatch();
388}
389
Prathmesh Prabhu53f47a62014-07-16 14:06:55 -0700390TEST_P(MobileOperatorInfoMainTest, MNOByOperatorNameMultipleMNOOptions) {
391 // message: Two messages with the same operator name.
392 // match by: Both MNOs matched, one is earmarked.
393 // verify: The earmarked MNO is picked.
394 ExpectEventCount(1);
395 UpdateOperatorName("name125001");
396 VerifyEventCount();
397 VerifyMNOWithUUID("uuid125002");
398}
399
Prathmesh Prabhu145a5da2014-07-16 17:00:56 -0700400TEST_P(MobileOperatorInfoMainTest, MNOByOperatorNameAggressiveMatch) {
401 // These network operators match by name but only after normalizing the names.
402 // Both the name from the database and the name provided to
403 // |UpdateOperatoName| must be normalized for this test to pass.
404 ExpectEventCount(1);
405 UpdateOperatorName("name126001 casedoesnotmatch");
406 VerifyEventCount();
407 VerifyMNOWithUUID("uuid126001");
408
409 ResetOperatorInfo();
410 ExpectEventCount(1);
411 UpdateOperatorName("name126002 CaseStillDoesNotMatch");
412 VerifyEventCount();
413 VerifyMNOWithUUID("uuid126002");
414
415 ResetOperatorInfo();
416 ExpectEventCount(1);
417 UpdateOperatorName("name126003GiveMeMoreSpace");
418 VerifyEventCount();
419 VerifyMNOWithUUID("uuid126003");
420
421 ResetOperatorInfo();
422 ExpectEventCount(1);
423 UpdateOperatorName("name126004 Too Much Air Here");
424 VerifyEventCount();
425 VerifyMNOWithUUID("uuid126004");
426
427 ResetOperatorInfo();
428 ExpectEventCount(1);
429 UpdateOperatorName("näméwithNon-Äσ¢ii");
430 VerifyEventCount();
431 VerifyMNOWithUUID("uuid126005");
432}
433
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -0700434TEST_P(MobileOperatorInfoMainTest, MNOByOperatorNameWithLang) {
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -0700435 // message: Has an MNO with no MVNO.
436 // match by: OperatorName.
437 // verify: Observer event, fields.
438 ExpectEventCount(1);
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -0700439 UpdateOperatorName("name105");
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -0700440 VerifyEventCount();
441 VerifyMNOWithUUID("uuid105");
442}
443
Prathmesh Prabhu53f47a62014-07-16 14:06:55 -0700444TEST_P(MobileOperatorInfoMainTest, MNOByOperatorNameMultipleNameOptions) {
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -0700445 // message: Has an MNO with no MVNO.
446 // match by: OperatorName, one of the multiple present in the MNO.
447 // verify: Observer event, fields.
448 ExpectEventCount(1);
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -0700449 UpdateOperatorName("name104002");
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -0700450 VerifyEventCount();
451 VerifyMNOWithUUID("uuid104");
452}
453
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -0700454TEST_P(MobileOperatorInfoMainTest, MNOByMCCMNCAndOperatorName) {
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -0700455 // message: Has MNOs with no MVNO.
Prathmesh Prabhu53f47a62014-07-16 14:06:55 -0700456 // match by: MCCMNC finds two candidates (first one is chosen), Name narrows
457 // down to one.
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -0700458 // verify: Observer event, fields.
459 // This is merely a MCCMNC update.
Prathmesh Prabhu53f47a62014-07-16 14:06:55 -0700460 ExpectEventCount(1);
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -0700461 UpdateMCCMNC("106001");
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -0700462 VerifyEventCount();
Prathmesh Prabhu53f47a62014-07-16 14:06:55 -0700463 VerifyMNOWithUUID("uuid106001");
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -0700464
465 ExpectEventCount(1);
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -0700466 UpdateOperatorName("name106002");
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -0700467 VerifyEventCount();
468 VerifyMNOWithUUID("uuid106002");
469
470 ResetOperatorInfo();
471 // Try updates in reverse order.
472 ExpectEventCount(1);
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -0700473 UpdateOperatorName("name106001");
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -0700474 VerifyEventCount();
475 VerifyMNOWithUUID("uuid106001");
476}
477
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -0700478TEST_P(MobileOperatorInfoMainTest, MNOByOperatorNameAndMCCMNC) {
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -0700479 // message: Has MNOs with no MVNO.
Prathmesh Prabhu53f47a62014-07-16 14:06:55 -0700480 // match by: OperatorName finds two (first one is chosen), MCCMNC narrows down
481 // to one.
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -0700482 // verify: Observer event, fields.
483 // This is merely an OperatorName update.
Prathmesh Prabhu53f47a62014-07-16 14:06:55 -0700484 ExpectEventCount(1);
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -0700485 UpdateOperatorName("name107");
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -0700486 VerifyEventCount();
Prathmesh Prabhu53f47a62014-07-16 14:06:55 -0700487 VerifyMNOWithUUID("uuid107001");
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -0700488
489 ExpectEventCount(1);
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -0700490 UpdateMCCMNC("107002");
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -0700491 VerifyEventCount();
492 VerifyMNOWithUUID("uuid107002");
493
494 ResetOperatorInfo();
495 // Try updates in reverse order.
496 ExpectEventCount(1);
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -0700497 UpdateMCCMNC("107001");
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -0700498 VerifyEventCount();
499 VerifyMNOWithUUID("uuid107001");
500}
501
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -0700502TEST_P(MobileOperatorInfoMainTest, MNOByMCCMNCOverridesOperatorName) {
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -0700503 // message: Has MNOs with no MVNO.
504 // match by: First MCCMNC finds one. Then, OperatorName matches another.
505 // verify: MCCMNC match prevails. No change on OperatorName update.
506 ExpectEventCount(1);
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -0700507 UpdateMCCMNC("108001");
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -0700508 VerifyEventCount();
509 VerifyMNOWithUUID("uuid108001");
510
511 // An event is sent for the updated OperatorName.
512 ExpectEventCount(1);
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -0700513 UpdateOperatorName("name108002"); // Does not match.
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -0700514 VerifyEventCount();
515 VerifyMNOWithUUID("uuid108001");
Prathmesh Prabhu82f8b022014-07-28 14:55:37 -0700516 // OperatorName from the database is given preference over the user supplied
517 // one.
518 EXPECT_EQ("name108001", operator_info_->operator_name());
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -0700519
520 ResetOperatorInfo();
521 // message: Same as above.
522 // match by: First OperatorName finds one, then MCCMNC overrides it.
523 // verify: Two events, MCCMNC one overriding the OperatorName one.
524 ExpectEventCount(1);
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -0700525 UpdateOperatorName("name108001");
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -0700526 VerifyEventCount();
527 VerifyMNOWithUUID("uuid108001");
528
529 ExpectEventCount(1);
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -0700530 UpdateMCCMNC("108002");
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -0700531 VerifyEventCount();
532 VerifyMNOWithUUID("uuid108002");
Prathmesh Prabhu82f8b022014-07-28 14:55:37 -0700533 EXPECT_EQ("name108002", operator_info_->operator_name());
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -0700534
535 // message: Same as above.
536 // match by: First a *wrong* MCCMNC update, followed by the correct Name
537 // update.
538 // verify: No MNO, since MCCMNC is given precedence.
539 ResetOperatorInfo();
540 ExpectEventCount(0);
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -0700541 UpdateMCCMNC("108999"); // Does not match.
542 UpdateOperatorName("name108001");
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -0700543 VerifyEventCount();
544 VerifyNoMatch();
545}
546
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -0700547TEST_P(MobileOperatorInfoMainTest, MNOByIMSI) {
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -0700548 // message: Has MNO with no MVNO.
549 // match by: MCCMNC part of IMSI of length 5 / 6.
550 ExpectEventCount(0);
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -0700551 UpdateIMSI("109"); // Too short.
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -0700552 VerifyEventCount();
553 VerifyNoMatch();
554
555 ExpectEventCount(0);
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -0700556 UpdateIMSI("109995432154321"); // No match.
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -0700557 VerifyEventCount();
558 VerifyNoMatch();
559
560 ResetOperatorInfo();
561 // Short MCCMNC match.
562 ExpectEventCount(1);
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -0700563 UpdateIMSI("109015432154321"); // First 5 digits match.
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -0700564 VerifyEventCount();
565 VerifyMNOWithUUID("uuid10901");
566
567 ResetOperatorInfo();
568 // Long MCCMNC match.
569 ExpectEventCount(1);
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -0700570 UpdateIMSI("10900215432154321"); // First 6 digits match.
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -0700571 VerifyEventCount();
572 VerifyMNOWithUUID("uuid109002");
573}
574
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -0700575TEST_P(MobileOperatorInfoMainTest, MNOByMCCMNCOverridesIMSI) {
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -0700576 // message: Has MNOs with no MVNO.
577 // match by: One matches MCCMNC, then one matches a different MCCMNC substring
578 // of IMSI
579 // verify: Observer event for the first match, all fields. Second Update
580 // ignored.
581 ExpectEventCount(1);
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -0700582 UpdateMCCMNC("110001");
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -0700583 VerifyEventCount();
584 VerifyMNOWithUUID("uuid110001");
585
586 // MNO remains unchanged on a mismatched IMSI update.
587 ExpectEventCount(0);
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -0700588 UpdateIMSI("1100025432154321"); // First 6 digits match.
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -0700589 VerifyEventCount();
590 VerifyMNOWithUUID("uuid110001");
591
592 // MNO remains uncnaged on an invalid IMSI update.
593 ExpectEventCount(0);
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -0700594 UpdateIMSI("1100035432154321"); // Prefix does not match.
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -0700595 VerifyEventCount();
596 VerifyMNOWithUUID("uuid110001");
597
598 ExpectEventCount(0);
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -0700599 UpdateIMSI("110"); // Too small.
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -0700600 VerifyEventCount();
601 VerifyMNOWithUUID("uuid110001");
602
603 ResetOperatorInfo();
604 // Same as above, but this time, match with IMSI, followed by a contradictory
605 // MCCMNC update. The second update should override the first one.
606 ExpectEventCount(1);
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -0700607 UpdateIMSI("1100025432154321"); // First 6 digits match.
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -0700608 VerifyEventCount();
609 VerifyMNOWithUUID("uuid110002");
610
611 ExpectEventCount(1);
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -0700612 UpdateMCCMNC("110001");
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -0700613 VerifyEventCount();
614 VerifyMNOWithUUID("uuid110001");
615}
616
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -0700617TEST_P(MobileOperatorInfoMainTest, MNOUchangedBySecondaryUpdates) {
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -0700618 // This test verifies that only some updates affect the MNO.
619 // message: Has MNOs with no MVNO.
620 // match by: First matches the MCCMNC. Later, MNOs with a different MCCMNC
621 // matchs the given SID, NID, ICCID.
622 // verify: Only one Observer event, on the first MCCMNC match.
623 ExpectEventCount(1);
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -0700624 UpdateMCCMNC("111001");
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -0700625 VerifyEventCount();
626 VerifyMNOWithUUID("uuid111001");
627
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -0700628 ExpectEventCount(1); // NID change event.
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -0700629 UpdateNID("111202");
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -0700630 VerifyEventCount();
631 VerifyMNOWithUUID("uuid111001");
632}
633
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -0700634TEST_P(MobileOperatorInfoMainTest, MVNODefaultMatch) {
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -0700635 // message: MNO with one MVNO (no filter).
636 // match by: MNO matches by MCCMNC.
637 // verify: Observer event for MVNO match. Uuid match the MVNO.
638 // second update: ICCID.
639 // verify: No observer event, match remains unchanged.
640 ExpectEventCount(1);
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -0700641 UpdateMCCMNC("112001");
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -0700642 VerifyEventCount();
643 VerifyMVNOWithUUID("uuid112002");
644
645 ExpectEventCount(0);
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -0700646 UpdateICCID("112002");
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -0700647 VerifyEventCount();
648 VerifyMVNOWithUUID("uuid112002");
649}
650
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -0700651TEST_P(MobileOperatorInfoMainTest, MVNONameMatch) {
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -0700652 // message: MNO with one MVNO (name filter).
653 // match by: MNO matches by MCCMNC,
654 // MVNO fails to match by fist name update,
655 // then MVNO matches by name.
656 // verify: Two Observer events: MNO followed by MVNO.
657 ExpectEventCount(1);
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -0700658 UpdateMCCMNC("113001");
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -0700659 VerifyEventCount();
660 VerifyMNOWithUUID("uuid113001");
661
662 ExpectEventCount(1);
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -0700663 UpdateOperatorName("name113999"); // No match.
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -0700664 VerifyEventCount();
665 VerifyMNOWithUUID("uuid113001");
Prathmesh Prabhu82f8b022014-07-28 14:55:37 -0700666 // Name from the database is given preference.
667 EXPECT_EQ("name113001", operator_info_->operator_name());
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -0700668
669 ExpectEventCount(1);
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -0700670 UpdateOperatorName("name113002");
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -0700671 VerifyEventCount();
672 VerifyMVNOWithUUID("uuid113002");
673 EXPECT_EQ("name113002", operator_info_->operator_name());
674}
675
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -0700676TEST_P(MobileOperatorInfoMainTest, MVNONameMalformedRegexMatch) {
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -0700677 // message: MNO with one MVNO (name filter with a malformed regex).
678 // match by: MNO matches by MCCMNC.
679 // MVNO does not match
680 ExpectEventCount(2);
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -0700681 UpdateMCCMNC("114001");
682 UpdateOperatorName("name[");
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -0700683 VerifyEventCount();
684 VerifyMNOWithUUID("uuid114001");
685}
686
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -0700687TEST_P(MobileOperatorInfoMainTest, MVNONameSubexpressionRegexMatch) {
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -0700688 // message: MNO with one MVNO (name filter with simple regex).
689 // match by: MNO matches by MCCMNC.
690 // MVNO does not match with a name whose subexpression matches the
691 // regex.
692 ExpectEventCount(2); // One event for just the name update.
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -0700693 UpdateMCCMNC("115001");
694 UpdateOperatorName("name115_ExtraCrud");
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -0700695 VerifyEventCount();
696 VerifyMNOWithUUID("uuid115001");
697
698 ResetOperatorInfo();
699 ExpectEventCount(2); // One event for just the name update.
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -0700700 UpdateMCCMNC("115001");
701 UpdateOperatorName("ExtraCrud_name115");
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -0700702 VerifyEventCount();
703 VerifyMNOWithUUID("uuid115001");
704
705 ResetOperatorInfo();
706 ExpectEventCount(2); // One event for just the name update.
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -0700707 UpdateMCCMNC("115001");
708 UpdateOperatorName("ExtraCrud_name115_ExtraCrud");
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -0700709 VerifyEventCount();
710 VerifyMNOWithUUID("uuid115001");
711
712 ResetOperatorInfo();
713 ExpectEventCount(2); // One event for just the name update.
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -0700714 UpdateMCCMNC("115001");
715 UpdateOperatorName("name_ExtraCrud_115");
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -0700716 VerifyEventCount();
717 VerifyMNOWithUUID("uuid115001");
718
719 ResetOperatorInfo();
720 ExpectEventCount(2);
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -0700721 UpdateMCCMNC("115001");
722 UpdateOperatorName("name115");
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -0700723 VerifyEventCount();
724 VerifyMVNOWithUUID("uuid115002");
725}
726
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -0700727TEST_P(MobileOperatorInfoMainTest, MVNONameRegexMatch) {
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -0700728 // message: MNO with one MVNO (name filter with non-trivial regex).
729 // match by: MNO matches by MCCMNC.
730 // MVNO fails to match several times with different strings.
731 // MVNO matches several times with different values.
732
733 // Make sure we're not taking the regex literally!
734 ExpectEventCount(2);
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -0700735 UpdateMCCMNC("116001");
736 UpdateOperatorName("name[a-zA-Z_]*116[0-9]{0,3}");
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -0700737 VerifyEventCount();
738 VerifyMNOWithUUID("uuid116001");
739
740 ResetOperatorInfo();
741 ExpectEventCount(2);
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -0700742 UpdateMCCMNC("116001");
743 UpdateOperatorName("name[a-zA-Z_]116[0-9]");
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -0700744 VerifyEventCount();
745 VerifyMNOWithUUID("uuid116001");
746
747 ResetOperatorInfo();
748 ExpectEventCount(2);
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -0700749 UpdateMCCMNC("116001");
750 UpdateOperatorName("nameb*1167");
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -0700751 VerifyEventCount();
752 VerifyMNOWithUUID("uuid116001");
753
754 // Success!
755 ResetOperatorInfo();
756 ExpectEventCount(2);
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -0700757 UpdateMCCMNC("116001");
758 UpdateOperatorName("name116");
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -0700759 VerifyEventCount();
760 VerifyMVNOWithUUID("uuid116002");
761
762 ResetOperatorInfo();
763 ExpectEventCount(2);
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -0700764 UpdateMCCMNC("116001");
765 UpdateOperatorName("nameSomeWord116");
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -0700766 VerifyEventCount();
767 VerifyMVNOWithUUID("uuid116002");
768
769 ResetOperatorInfo();
770 ExpectEventCount(2);
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -0700771 UpdateMCCMNC("116001");
772 UpdateOperatorName("name116567");
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -0700773 VerifyEventCount();
774 VerifyMVNOWithUUID("uuid116002");
775}
776
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -0700777TEST_P(MobileOperatorInfoMainTest, MVNONameMatchMultipleFilters) {
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -0700778 // message: MNO with one MVNO with two name filters.
779 // match by: MNO matches by MCCMNC.
780 // MVNO first fails on the second filter alone.
781 // MVNO fails on the first filter alone.
782 // MVNO matches on both filters.
783 ExpectEventCount(2);
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -0700784 UpdateMCCMNC("117001");
785 UpdateOperatorName("nameA_crud");
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -0700786 VerifyEventCount();
787 VerifyMNOWithUUID("uuid117001");
788
789 ResetOperatorInfo();
790 ExpectEventCount(2);
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -0700791 UpdateMCCMNC("117001");
792 UpdateOperatorName("crud_nameB");
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -0700793 VerifyEventCount();
794 VerifyMNOWithUUID("uuid117001");
795
796 ResetOperatorInfo();
797 ExpectEventCount(2);
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -0700798 UpdateMCCMNC("117001");
799 UpdateOperatorName("crud_crud");
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -0700800 VerifyEventCount();
801 VerifyMNOWithUUID("uuid117001");
802
803 ResetOperatorInfo();
804 ExpectEventCount(2);
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -0700805 UpdateMCCMNC("117001");
806 UpdateOperatorName("nameA_nameB");
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -0700807 VerifyEventCount();
808 VerifyMVNOWithUUID("uuid117002");
809}
810
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -0700811TEST_P(MobileOperatorInfoMainTest, MVNOIMSIMatch) {
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -0700812 // message: MNO with one MVNO (imsi filter).
813 // match by: MNO matches by MCCMNC,
814 // MVNO fails to match by fist imsi update,
815 // then MVNO matches by imsi.
816 // verify: Two Observer events: MNO followed by MVNO.
817 ExpectEventCount(1);
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -0700818 UpdateMCCMNC("118001");
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -0700819 VerifyEventCount();
820 VerifyMNOWithUUID("uuid118001");
821
822 ExpectEventCount(0);
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -0700823 UpdateIMSI("1180011234512345"); // No match.
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -0700824 VerifyEventCount();
825 VerifyMNOWithUUID("uuid118001");
826
827 ExpectEventCount(1);
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -0700828 UpdateIMSI("1180015432154321");
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -0700829 VerifyEventCount();
830 VerifyMVNOWithUUID("uuid118002");
831}
832
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -0700833TEST_P(MobileOperatorInfoMainTest, MVNOICCIDMatch) {
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -0700834 // message: MNO with one MVNO (iccid filter).
835 // match by: MNO matches by MCCMNC,
836 // MVNO fails to match by fist iccid update,
837 // then MVNO matches by iccid.
838 // verify: Two Observer events: MNO followed by MVNO.
839 ExpectEventCount(1);
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -0700840 UpdateMCCMNC("119001");
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -0700841 VerifyEventCount();
842 VerifyMNOWithUUID("uuid119001");
843
844 ExpectEventCount(0);
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -0700845 UpdateICCID("119987654321"); // No match.
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -0700846 VerifyEventCount();
847 VerifyMNOWithUUID("uuid119001");
848
849 ExpectEventCount(1);
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -0700850 UpdateICCID("119123456789");
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -0700851 VerifyEventCount();
852 VerifyMVNOWithUUID("uuid119002");
853}
854
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -0700855TEST_P(MobileOperatorInfoMainTest, MVNOSIDMatch) {
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -0700856 // message: MNO with one MVNO (sid filter).
Prathmesh Prabhuc1175bf2014-04-15 16:58:05 -0700857 // match by: MNO matches by SID,
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -0700858 // MVNO fails to match by fist sid update,
859 // then MVNO matches by sid.
860 // verify: Two Observer events: MNO followed by MVNO.
Prathmesh Prabhuc1175bf2014-04-15 16:58:05 -0700861 ExpectEventCount(0);
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -0700862 UpdateSID("120999"); // No match.
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -0700863 VerifyEventCount();
Prathmesh Prabhuc1175bf2014-04-15 16:58:05 -0700864 VerifyNoMatch();
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -0700865
866 ExpectEventCount(1);
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -0700867 UpdateSID("120001"); // Only MNO matches.
Prathmesh Prabhuc1175bf2014-04-15 16:58:05 -0700868 VerifyEventCount();
869 VerifyMNOWithUUID("uuid120001");
870 EXPECT_EQ("120001", operator_info_->sid());
871
872 ExpectEventCount(1);
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -0700873 UpdateSID("120002"); // MVNO matches as well.
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -0700874 VerifyEventCount();
875 VerifyMVNOWithUUID("uuid120002");
Prathmesh Prabhuc1175bf2014-04-15 16:58:05 -0700876 EXPECT_EQ("120002", operator_info_->sid());
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -0700877}
878
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -0700879TEST_P(MobileOperatorInfoMainTest, MVNOAllMatch) {
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -0700880 // message: MNO with following MVNOS:
881 // - one with no filter.
882 // - one with name filter.
883 // - one with imsi filter.
884 // - one with iccid filter.
885 // - one with name and iccid filter.
886 // verify:
887 // - initial MCCMNC matches the default MVNO directly (not MNO)
888 // - match each of the MVNOs in turn.
889 // - give super set information that does not match any MVNO correctly,
890 // verify that the MNO matches.
891 ExpectEventCount(1);
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -0700892 UpdateMCCMNC("121001");
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -0700893 VerifyEventCount();
894 VerifyMNOWithUUID("uuid121001");
895
896 ResetOperatorInfo();
897 ExpectEventCount(2);
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -0700898 UpdateMCCMNC("121001");
899 UpdateOperatorName("name121003");
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -0700900 VerifyEventCount();
901 VerifyMVNOWithUUID("uuid121003");
902
903 ResetOperatorInfo();
904 ExpectEventCount(2);
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -0700905 UpdateMCCMNC("121001");
906 UpdateIMSI("1210045432154321");
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -0700907 VerifyEventCount();
908 VerifyMVNOWithUUID("uuid121004");
909
910 ResetOperatorInfo();
911 ExpectEventCount(2);
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -0700912 UpdateMCCMNC("121001");
913 UpdateICCID("121005123456789");
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -0700914 VerifyEventCount();
915 VerifyMVNOWithUUID("uuid121005");
916
917 ResetOperatorInfo();
918 ExpectEventCount(3);
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -0700919 UpdateMCCMNC("121001");
920 UpdateOperatorName("name121006");
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -0700921 VerifyMNOWithUUID("uuid121001");
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -0700922 UpdateICCID("121006123456789");
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -0700923 VerifyEventCount();
924 VerifyMVNOWithUUID("uuid121006");
925}
926
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -0700927TEST_P(MobileOperatorInfoMainTest, MVNOMatchAndMismatch) {
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -0700928 // message: MNO with one MVNO with name filter.
929 // match by: MNO matches by MCCMNC
930 // MVNO matches by name.
931 // Second name update causes the MVNO to not match again.
932 ExpectEventCount(1);
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -0700933 UpdateMCCMNC("113001");
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -0700934 VerifyEventCount();
935 VerifyMNOWithUUID("uuid113001");
936
937 ExpectEventCount(1);
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -0700938 UpdateOperatorName("name113002");
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -0700939 VerifyEventCount();
940 VerifyMVNOWithUUID("uuid113002");
941 EXPECT_EQ("name113002", operator_info_->operator_name());
942
943 ExpectEventCount(1);
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -0700944 UpdateOperatorName("name113999"); // No match.
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -0700945 VerifyEventCount();
946 VerifyMNOWithUUID("uuid113001");
Prathmesh Prabhu82f8b022014-07-28 14:55:37 -0700947 // Name from database is given preference.
948 EXPECT_EQ("name113001", operator_info_->operator_name());
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -0700949}
950
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -0700951TEST_P(MobileOperatorInfoMainTest, MVNOMatchAndReset) {
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -0700952 // message: MVNO with name filter.
953 // verify;
954 // - match MVNO by name.
955 // - Reset object, verify Observer event, and not match.
956 // - match MVNO by name again.
957 ExpectEventCount(1);
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -0700958 UpdateMCCMNC("113001");
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -0700959 VerifyEventCount();
960 ExpectEventCount(1);
961 VerifyMNOWithUUID("uuid113001");
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -0700962 UpdateOperatorName("name113002");
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -0700963 VerifyEventCount();
964 VerifyMVNOWithUUID("uuid113002");
965 EXPECT_EQ("name113002", operator_info_->operator_name());
966
967 ExpectEventCount(1);
968 operator_info_->Reset();
969 VerifyEventCount();
970 VerifyNoMatch();
971
972 ExpectEventCount(1);
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -0700973 UpdateMCCMNC("113001");
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -0700974 VerifyEventCount();
975 VerifyMNOWithUUID("uuid113001");
976 ExpectEventCount(1);
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -0700977 UpdateOperatorName("name113002");
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -0700978 VerifyEventCount();
979 VerifyMVNOWithUUID("uuid113002");
980 EXPECT_EQ("name113002", operator_info_->operator_name());
981}
982
Prathmesh Prabhuc1175bf2014-04-15 16:58:05 -0700983// Here, we rely on our knowledge about the implementation: The SID and MCCMNC
984// updates follow the same code paths, and so we can get away with not testing
985// all the scenarios we test above for MCCMNC. Instead, we only do basic testing
986// to make sure that SID upates operator as MCCMNC updates do.
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -0700987TEST_P(MobileOperatorInfoMainTest, MNOBySID) {
Prathmesh Prabhuc1175bf2014-04-15 16:58:05 -0700988 // message: Has an MNO with no MVNO.
989 // match by: SID.
990 // verify: Observer event, uuid.
991
992 ExpectEventCount(0);
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -0700993 UpdateSID("1229"); // No match.
Prathmesh Prabhuc1175bf2014-04-15 16:58:05 -0700994 VerifyEventCount();
995 VerifyNoMatch();
996
997 ExpectEventCount(1);
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -0700998 UpdateSID("1221");
Prathmesh Prabhuc1175bf2014-04-15 16:58:05 -0700999 VerifyEventCount();
1000 VerifyMNOWithUUID("uuid1221");
1001
1002 ExpectEventCount(1);
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -07001003 UpdateSID("1229"); // No Match.
Prathmesh Prabhuc1175bf2014-04-15 16:58:05 -07001004 VerifyEventCount();
1005 VerifyNoMatch();
1006}
1007
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -07001008TEST_P(MobileOperatorInfoMainTest, MNOByMCCMNCAndSID) {
Prathmesh Prabhuc1175bf2014-04-15 16:58:05 -07001009 // message: Has an MNO with no MVNO.
1010 // match by: SID / MCCMNC alternately.
1011 // verify: Observer event, uuid.
1012
1013 ExpectEventCount(0);
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -07001014 UpdateMCCMNC("123999"); // NO match.
1015 UpdateSID("1239"); // No match.
Prathmesh Prabhuc1175bf2014-04-15 16:58:05 -07001016 VerifyEventCount();
1017 VerifyNoMatch();
1018
1019 ExpectEventCount(1);
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -07001020 UpdateMCCMNC("123001");
Prathmesh Prabhuc1175bf2014-04-15 16:58:05 -07001021 VerifyEventCount();
1022 VerifyMNOWithUUID("uuid123001");
1023
1024 ExpectEventCount(1);
1025 operator_info_->Reset();
1026 VerifyEventCount();
1027 VerifyNoMatch();
1028
1029 ExpectEventCount(1);
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -07001030 UpdateSID("1232");
Prathmesh Prabhuc1175bf2014-04-15 16:58:05 -07001031 VerifyEventCount();
1032 VerifyMNOWithUUID("uuid1232");
1033
1034 ExpectEventCount(1);
1035 operator_info_->Reset();
1036 VerifyEventCount();
1037 VerifyNoMatch();
1038
1039 ExpectEventCount(1);
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -07001040 UpdateMCCMNC("123001");
Prathmesh Prabhuc1175bf2014-04-15 16:58:05 -07001041 VerifyEventCount();
1042 VerifyMNOWithUUID("uuid123001");
1043}
1044
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -07001045class MobileOperatorInfoDataTest : public MobileOperatorInfoMainTest {
1046 public:
Alex Vakulenko8a532292014-06-16 17:18:44 -07001047 MobileOperatorInfoDataTest() : MobileOperatorInfoMainTest() {}
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -07001048
1049 // Same as MobileOperatorInfoMainTest, except that the database used is
1050 // different.
1051 virtual void SetUp() {
1052 operator_info_->ClearDatabasePaths();
1053 AddDatabase(mobile_operator_db::data_test,
1054 arraysize(mobile_operator_db::data_test));
1055 operator_info_->Init();
1056 operator_info_->AddObserver(&observer_);
1057 }
1058
1059 protected:
1060 // This is a function that does a best effort verification of the information
1061 // that is obtained from the database by the MobileOperatorInfo object against
1062 // expectations stored in the form of data members in this class.
1063 // This is not a full proof check. In particular:
1064 // - It is unspecified in some case which of the values from a list is
1065 // exposed as a property. For example, at best, we can check that |sid| is
1066 // non-empty.
1067 // - It is not robust to "" as property values at times.
1068 void VerifyDatabaseData() {
1069 EXPECT_EQ(country_, operator_info_->country());
1070 EXPECT_EQ(requires_roaming_, operator_info_->requires_roaming());
1071 EXPECT_EQ(activation_code_, operator_info_->activation_code());
1072
1073 EXPECT_EQ(mccmnc_list_.size(), operator_info_->mccmnc_list().size());
1074 set<string> mccmnc_set(operator_info_->mccmnc_list().begin(),
1075 operator_info_->mccmnc_list().end());
Paul Stewart2f6c7892015-06-16 13:13:10 -07001076 for (const auto& mccmnc : mccmnc_list_) {
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -07001077 EXPECT_TRUE(mccmnc_set.find(mccmnc) != mccmnc_set.end());
1078 }
1079 if (mccmnc_list_.size() > 0) {
1080 // It is not specified which entry will be chosen, but mccmnc() must be
1081 // non empty.
1082 EXPECT_FALSE(operator_info_->mccmnc().empty());
1083 }
1084
1085 VerifyNameListsMatch(operator_name_list_,
1086 operator_info_->operator_name_list());
1087
1088 // This comparison breaks if two apns have the same |apn| field.
1089 EXPECT_EQ(apn_list_.size(), operator_info_->apn_list().size());
Paul Stewart2f6c7892015-06-16 13:13:10 -07001090 map<string, const MobileOperatorInfo::MobileAPN*> mobile_apns;
1091 for (const auto& apn_node : operator_info_->apn_list()) {
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -07001092 mobile_apns[apn_node->apn] = apn_node;
1093 }
Paul Stewart2f6c7892015-06-16 13:13:10 -07001094 for (const auto& apn_lhs : apn_list_) {
Prathmesh Prabhuf90cf1a2014-04-15 16:51:19 -07001095 ASSERT_TRUE(mobile_apns.find(apn_lhs->apn) != mobile_apns.end());
Paul Stewart2f6c7892015-06-16 13:13:10 -07001096 const auto& apn_rhs = mobile_apns[apn_lhs->apn];
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -07001097 // Only comparing apn, name, username, password.
1098 EXPECT_EQ(apn_lhs->apn, apn_rhs->apn);
1099 EXPECT_EQ(apn_lhs->username, apn_rhs->username);
1100 EXPECT_EQ(apn_lhs->password, apn_rhs->password);
1101 VerifyNameListsMatch(apn_lhs->operator_name_list,
1102 apn_rhs->operator_name_list);
1103 }
1104
1105 EXPECT_EQ(olp_list_.size(), operator_info_->olp_list().size());
1106 // This comparison breaks if two OLPs have the same |url|.
1107 map<string, MobileOperatorInfo::OnlinePortal> olps;
Paul Stewart2f6c7892015-06-16 13:13:10 -07001108 for (const auto& olp : operator_info_->olp_list()) {
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -07001109 olps[olp.url] = olp;
1110 }
Paul Stewart2f6c7892015-06-16 13:13:10 -07001111 for (const auto& olp : olp_list_) {
Prathmesh Prabhuf90cf1a2014-04-15 16:51:19 -07001112 ASSERT_TRUE(olps.find(olp.url) != olps.end());
Paul Stewart2f6c7892015-06-16 13:13:10 -07001113 const auto& olp_rhs = olps[olp.url];
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -07001114 EXPECT_EQ(olp.method, olp_rhs.method);
1115 EXPECT_EQ(olp.post_data, olp_rhs.post_data);
1116 }
1117
1118 EXPECT_EQ(sid_list_.size(), operator_info_->sid_list().size());
1119 set<string> sid_set(operator_info_->sid_list().begin(),
1120 operator_info_->sid_list().end());
Paul Stewart2f6c7892015-06-16 13:13:10 -07001121 for (const auto& sid : sid_list_) {
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -07001122 EXPECT_TRUE(sid_set.find(sid) != sid_set.end());
1123 }
1124 if (sid_list_.size() > 0) {
1125 // It is not specified which entry will be chosen, but |sid()| must be
1126 // non-empty.
1127 EXPECT_FALSE(operator_info_->sid().empty());
1128 }
1129 }
1130
1131 // This function does some extra checks for the user data that can not be done
1132 // when data is obtained from the database.
1133 void VerifyUserData() {
1134 EXPECT_EQ(sid_, operator_info_->sid());
1135 }
1136
1137 void VerifyNameListsMatch(
Paul Stewart2f6c7892015-06-16 13:13:10 -07001138 const vector<MobileOperatorInfo::LocalizedName>& operator_name_list_lhs,
1139 const vector<MobileOperatorInfo::LocalizedName>& operator_name_list_rhs) {
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -07001140 // This comparison breaks if two localized names have the same |name|.
1141 map<string, MobileOperatorInfo::LocalizedName> localized_names;
Paul Stewart2f6c7892015-06-16 13:13:10 -07001142 for (const auto& localized_name : operator_name_list_rhs) {
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -07001143 localized_names[localized_name.name] = localized_name;
1144 }
Paul Stewart2f6c7892015-06-16 13:13:10 -07001145 for (const auto& localized_name : operator_name_list_lhs) {
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -07001146 EXPECT_TRUE(localized_names.find(localized_name.name) !=
1147 localized_names.end());
1148 EXPECT_EQ(localized_name.language,
1149 localized_names[localized_name.name].language);
1150 }
1151 }
1152
1153 // Use this function to pre-popluate all the data members of this object with
1154 // values matching the MNO for the database in |data_test.prototxt|.
1155 void PopulateMNOData() {
1156 country_ = "us";
1157 requires_roaming_ = true;
1158 activation_code_ = "open sesame";
1159
1160 mccmnc_list_.clear();
1161 mccmnc_list_.push_back("200001");
1162 mccmnc_list_.push_back("200002");
Prathmesh Prabhu0db0bf02014-05-06 15:17:44 -07001163 mccmnc_list_.push_back("200003");
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -07001164
1165 operator_name_list_.clear();
1166 operator_name_list_.push_back({"name200001", "en"});
1167 operator_name_list_.push_back({"name200002", ""});
1168
1169 apn_list_.clear();
Paul Stewart2f6c7892015-06-16 13:13:10 -07001170 MobileOperatorInfo::MobileAPN* apn;
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -07001171 apn = new MobileOperatorInfo::MobileAPN();
1172 apn->apn = "test@test.com";
1173 apn->username = "testuser";
1174 apn->password = "is_public_boohoohoo";
1175 apn->operator_name_list.push_back({"name200003", "hi"});
1176 apn_list_.push_back(apn); // Takes ownership.
1177
1178 olp_list_.clear();
1179 olp_list_.push_back({"some@random.com", "POST", "random_data"});
1180
1181 sid_list_.clear();
1182 sid_list_.push_back("200123");
1183 sid_list_.push_back("200234");
Prathmesh Prabhu0db0bf02014-05-06 15:17:44 -07001184 sid_list_.push_back("200345");
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -07001185 }
1186
1187 // Use this function to pre-populate all the data members of this object with
1188 // values matching the MVNO for the database in |data_test.prototext|.
1189 void PopulateMVNOData() {
1190 country_ = "ca";
1191 requires_roaming_ = false;
1192 activation_code_ = "khul ja sim sim";
1193
1194 mccmnc_list_.clear();
1195 mccmnc_list_.push_back("200001");
1196 mccmnc_list_.push_back("200102");
1197
1198 operator_name_list_.clear();
1199 operator_name_list_.push_back({"name200101", "en"});
1200 operator_name_list_.push_back({"name200102", ""});
1201
1202 apn_list_.clear();
Paul Stewart2f6c7892015-06-16 13:13:10 -07001203 MobileOperatorInfo::MobileAPN* apn;
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -07001204 apn = new MobileOperatorInfo::MobileAPN();
1205 apn->apn = "test2@test.com";
1206 apn->username = "testuser2";
1207 apn->password = "is_public_boohoohoo_too";
1208 apn_list_.push_back(apn); // Takes ownership.
1209
1210 olp_list_.clear();
1211 olp_list_.push_back({"someother@random.com", "GET", ""});
1212
1213 sid_list_.clear();
1214 sid_list_.push_back("200345");
1215 }
1216
1217 // Data to be verified against the database.
1218 string country_;
1219 bool requires_roaming_;
1220 string activation_code_;
1221 vector<string> mccmnc_list_;
1222 vector<MobileOperatorInfo::LocalizedName> operator_name_list_;
1223 ScopedVector<MobileOperatorInfo::MobileAPN> apn_list_;
1224 vector<MobileOperatorInfo::OnlinePortal> olp_list_;
1225 vector<string> sid_list_;
1226
1227 // Extra data to be verified only against user updates.
1228 string sid_;
1229
Alex Vakulenko8a532292014-06-16 17:18:44 -07001230 private:
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -07001231 DISALLOW_COPY_AND_ASSIGN(MobileOperatorInfoDataTest);
1232};
1233
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -07001234
1235TEST_P(MobileOperatorInfoDataTest, MNODetailedInformation) {
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -07001236 // message: MNO with all the information filled in.
1237 // match by: MNO matches by MCCMNC
1238 // verify: All information is correctly loaded.
1239 ExpectEventCount(1);
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -07001240 UpdateMCCMNC("200001");
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -07001241 VerifyEventCount();
1242 VerifyMNOWithUUID("uuid200001");
1243
1244 PopulateMNOData();
1245 VerifyDatabaseData();
1246}
1247
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -07001248TEST_P(MobileOperatorInfoDataTest, MVNOInheritsInformation) {
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -07001249 // message: MVNO with name filter.
1250 // verify: All the missing fields are carried over to the MVNO from MNO.
1251 ExpectEventCount(2);
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -07001252 UpdateMCCMNC("200001");
1253 UpdateOperatorName("name200201");
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -07001254 VerifyEventCount();
1255 VerifyMVNOWithUUID("uuid200201");
1256
1257 PopulateMNOData();
1258 VerifyDatabaseData();
1259}
1260
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -07001261TEST_P(MobileOperatorInfoDataTest, MVNOOverridesInformation) {
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -07001262 // match by: MNO matches by MCCMNC, MVNO by name.
1263 // verify: All information is correctly loaded.
1264 // The MVNO in this case overrides the information provided by MNO.
1265 ExpectEventCount(2);
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -07001266 UpdateMCCMNC("200001");
1267 UpdateOperatorName("name200101");
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -07001268 VerifyEventCount();
1269 VerifyMVNOWithUUID("uuid200101");
1270
1271 PopulateMVNOData();
1272 VerifyDatabaseData();
1273}
1274
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -07001275TEST_P(MobileOperatorInfoDataTest, NoUpdatesBeforeMNOMatch) {
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -07001276 // message: MVNO.
1277 // - do not match MNO with mccmnc/name
1278 // - on different updates, verify no events.
1279 ExpectEventCount(0);
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -07001280 UpdateMCCMNC("200999"); // No match.
1281 UpdateOperatorName("name200001"); // matches MNO
1282 UpdateOperatorName("name200101"); // matches MVNO filter.
1283 UpdateSID("200999"); // No match.
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -07001284 VerifyEventCount();
1285 VerifyNoMatch();
1286}
1287
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -07001288TEST_P(MobileOperatorInfoDataTest, UserUpdatesOverrideMVNO) {
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -07001289 // - match MVNO.
1290 // - send updates to properties and verify events are raised and values of
1291 // updated properties override the ones provided by the database.
1292 string imsi {"2009991234512345"};
1293 string iccid {"200999123456789"};
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -07001294 string olp_url {"url@url.com"};
1295 string olp_method {"POST"};
1296 string olp_post_data {"data"};
1297
1298 // Determine MVNO.
1299 ExpectEventCount(2);
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -07001300 UpdateMCCMNC("200001");
1301 UpdateOperatorName("name200101");
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -07001302 VerifyEventCount();
1303 VerifyMVNOWithUUID("uuid200101");
1304
1305 // Send updates.
Prathmesh Prabhuc1175bf2014-04-15 16:58:05 -07001306 ExpectEventCount(1);
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -07001307 UpdateOnlinePortal(olp_url, olp_method, olp_post_data);
1308 UpdateIMSI(imsi);
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -07001309 // No event raised because imsi is not exposed.
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -07001310 UpdateICCID(iccid);
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -07001311 // No event raised because ICCID is not exposed.
1312
1313 VerifyEventCount();
1314
1315 // Update our expectations.
1316 PopulateMVNOData();
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -07001317 olp_list_.push_back({olp_url, olp_method, olp_post_data});
1318
1319 VerifyDatabaseData();
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -07001320}
1321
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -07001322TEST_P(MobileOperatorInfoDataTest, CachedUserUpdatesOverrideMVNO) {
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -07001323 // message: MVNO.
1324 // - First send updates that don't identify an MNO.
1325 // - Then identify an MNO and MVNO.
1326 // - verify that all the earlier updates are cached, and override the MVNO
1327 // information.
1328 string imsi {"2009991234512345"};
1329 string iccid {"200999123456789"};
1330 string sid {"200999"};
1331 string olp_url {"url@url.com"};
1332 string olp_method {"POST"};
1333 string olp_post_data {"data"};
1334
1335 // Send updates.
1336 ExpectEventCount(0);
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -07001337 UpdateSID(sid);
1338 UpdateOnlinePortal(olp_url, olp_method, olp_post_data);
1339 UpdateIMSI(imsi);
1340 UpdateICCID(iccid);
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -07001341 VerifyEventCount();
1342
1343 // Determine MVNO.
1344 ExpectEventCount(2);
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -07001345 UpdateMCCMNC("200001");
1346 UpdateOperatorName("name200101");
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -07001347 VerifyEventCount();
1348 VerifyMVNOWithUUID("uuid200101");
1349
1350 // Update our expectations.
1351 PopulateMVNOData();
1352 sid_ = sid;
1353 sid_list_.push_back(sid);
1354 olp_list_.push_back({olp_url, olp_method, olp_post_data});
1355
1356 VerifyDatabaseData();
1357 VerifyUserData();
1358}
1359
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -07001360TEST_P(MobileOperatorInfoDataTest, RedundantUserUpdatesMVNO) {
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -07001361 // - match MVNO.
1362 // - send redundant updates to properties.
1363 // - Verify no events, no updates to properties.
1364
1365 // Identify MVNO.
1366 ExpectEventCount(2);
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -07001367 UpdateMCCMNC("200001");
1368 UpdateOperatorName("name200101");
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -07001369 VerifyEventCount();
1370 VerifyMVNOWithUUID("uuid200101");
1371
1372 // Send redundant updates.
1373 // TODO(pprabhu)
Prathmesh Prabhuc1175bf2014-04-15 16:58:05 -07001374 // |UpdateOnlinePortal| leads to an event because this is the first time this
1375 // value are set *by the user*. Although the values from the database were the
1376 // same, we did not use those values for filters. It would be ideal to not
1377 // raise these redundant events (since no public information about the object
1378 // changed), but I haven't invested in doing so yet.
1379 ExpectEventCount(1);
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -07001380 UpdateOperatorName(operator_info_->operator_name());
1381 UpdateOnlinePortal("someother@random.com", "GET", "");
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -07001382 VerifyEventCount();
1383 PopulateMVNOData();
1384 VerifyDatabaseData();
1385}
1386
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -07001387TEST_P(MobileOperatorInfoDataTest, RedundantCachedUpdatesMVNO) {
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -07001388 // message: MVNO.
1389 // - First send updates that don't identify MVNO, but match the data.
1390 // - Then idenityf an MNO and MVNO.
1391 // - verify that redundant information occurs only once.
1392
1393 // Send redundant updates.
1394 ExpectEventCount(2);
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -07001395 UpdateSID(operator_info_->sid());
1396 UpdateOperatorName(operator_info_->operator_name());
1397 UpdateOnlinePortal("someother@random.com", "GET", "");
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -07001398
1399 // Identify MVNO.
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -07001400 UpdateMCCMNC("200001");
1401 UpdateOperatorName("name200101");
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -07001402 VerifyEventCount();
1403 VerifyMVNOWithUUID("uuid200101");
1404
1405 PopulateMVNOData();
1406 VerifyDatabaseData();
1407}
1408
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -07001409TEST_P(MobileOperatorInfoDataTest, ResetClearsInformation) {
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -07001410 // Repeatedly reset the object and check M[V]NO identification and data.
1411 ExpectEventCount(2);
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -07001412 UpdateMCCMNC("200001");
1413 UpdateOperatorName("name200201");
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -07001414 VerifyEventCount();
1415 VerifyMVNOWithUUID("uuid200201");
1416 PopulateMNOData();
1417 VerifyDatabaseData();
1418
1419 ExpectEventCount(1);
1420 operator_info_->Reset();
1421 VerifyEventCount();
1422 VerifyNoMatch();
1423
1424 ExpectEventCount(2);
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -07001425 UpdateMCCMNC("200001");
1426 UpdateOperatorName("name200101");
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -07001427 VerifyEventCount();
1428 VerifyMVNOWithUUID("uuid200101");
1429 PopulateMVNOData();
1430 VerifyDatabaseData();
1431
1432 ExpectEventCount(1);
1433 operator_info_->Reset();
1434 VerifyEventCount();
1435 VerifyNoMatch();
1436
1437 ExpectEventCount(1);
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -07001438 UpdateMCCMNC("200001");
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -07001439 VerifyEventCount();
1440 VerifyMNOWithUUID("uuid200001");
1441 PopulateMNOData();
1442 VerifyDatabaseData();
1443}
1444
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -07001445TEST_P(MobileOperatorInfoDataTest, FilteredOLP) {
Prathmesh Prabhu0db0bf02014-05-06 15:17:44 -07001446 // We only check basic filter matching, using the fact that the regex matching
1447 // code is shared with the MVNO filtering, and is already well tested.
1448 // (1) None of the filters match.
1449 ExpectEventCount(1);
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -07001450 UpdateMCCMNC("200001");
Prathmesh Prabhu0db0bf02014-05-06 15:17:44 -07001451 VerifyEventCount();
1452 VerifyMNOWithUUID("uuid200001");
1453
1454 ASSERT_EQ(1, operator_info_->olp_list().size());
1455 // Just check that the filtered OLPs are not in the list.
1456 EXPECT_NE("olp@mccmnc", operator_info_->olp_list()[0].url);
1457 EXPECT_NE("olp@sid", operator_info_->olp_list()[0].url);
1458
1459 // (2) MCCMNC filter matches.
1460 ExpectEventCount(1);
1461 operator_info_->Reset();
1462 VerifyEventCount();
1463 VerifyNoMatch();
1464
1465 ExpectEventCount(1);
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -07001466 UpdateMCCMNC("200003");
Prathmesh Prabhu0db0bf02014-05-06 15:17:44 -07001467 VerifyEventCount();
1468 VerifyMNOWithUUID("uuid200001");
1469
1470 ASSERT_EQ(2, operator_info_->olp_list().size());
1471 EXPECT_NE("olp@sid", operator_info_->olp_list()[0].url);
1472 bool found_olp_by_mccmnc = false;
Paul Stewart2f6c7892015-06-16 13:13:10 -07001473 for (const auto& olp : operator_info_->olp_list()) {
Prathmesh Prabhu0db0bf02014-05-06 15:17:44 -07001474 found_olp_by_mccmnc |= ("olp@mccmnc" == olp.url);
1475 }
1476 EXPECT_TRUE(found_olp_by_mccmnc);
1477
1478 // (3) SID filter matches.
1479 ExpectEventCount(1);
1480 operator_info_->Reset();
1481 VerifyEventCount();
1482 VerifyNoMatch();
1483
1484 ExpectEventCount(1);
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -07001485 UpdateSID("200345");
Prathmesh Prabhu0db0bf02014-05-06 15:17:44 -07001486 VerifyEventCount();
1487 VerifyMNOWithUUID("uuid200001");
1488
1489 ASSERT_EQ(2, operator_info_->olp_list().size());
1490 EXPECT_NE("olp@mccmnc", operator_info_->olp_list()[0].url);
1491 bool found_olp_by_sid = false;
Paul Stewart2f6c7892015-06-16 13:13:10 -07001492 for (const auto& olp : operator_info_->olp_list()) {
Prathmesh Prabhu0db0bf02014-05-06 15:17:44 -07001493 found_olp_by_sid |= ("olp@sid" == olp.url);
1494 }
1495 EXPECT_TRUE(found_olp_by_sid);
1496}
1497
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -07001498class MobileOperatorInfoObserverTest : public MobileOperatorInfoMainTest {
1499 public:
1500 MobileOperatorInfoObserverTest() : MobileOperatorInfoMainTest() {}
1501
1502 // Same as |MobileOperatorInfoMainTest::SetUp|, except that we don't add a
1503 // default observer.
1504 virtual void SetUp() {
1505 operator_info_->ClearDatabasePaths();
1506 AddDatabase(mobile_operator_db::data_test,
1507 arraysize(mobile_operator_db::data_test));
1508 operator_info_->Init();
1509 }
1510
1511 protected:
1512 // ///////////////////////////////////////////////////////////////////////////
1513 // Data.
1514 MockMobileOperatorInfoObserver second_observer_;
1515
Alex Vakulenko8a532292014-06-16 17:18:44 -07001516 private:
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -07001517 DISALLOW_COPY_AND_ASSIGN(MobileOperatorInfoObserverTest);
1518};
1519
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -07001520TEST_P(MobileOperatorInfoObserverTest, NoObserver) {
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -07001521 // - Don't add any observers, and then cause an MVNO update to occur.
1522 // - Verify no crash.
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -07001523 UpdateMCCMNC("200001");
1524 UpdateOperatorName("name200101");
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -07001525}
1526
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -07001527TEST_P(MobileOperatorInfoObserverTest, MultipleObservers) {
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -07001528 // - Add two observers, and then cause an MVNO update to occur.
1529 // - Verify both observers are notified.
1530 operator_info_->AddObserver(&observer_);
1531 operator_info_->AddObserver(&second_observer_);
1532
1533 EXPECT_CALL(observer_, OnOperatorChanged()).Times(2);
1534 EXPECT_CALL(second_observer_, OnOperatorChanged()).Times(2);
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -07001535 UpdateMCCMNC("200001");
1536 UpdateOperatorName("name200101");
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -07001537 VerifyMVNOWithUUID("uuid200101");
1538
1539 dispatcher_.DispatchPendingEvents();
1540}
1541
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -07001542TEST_P(MobileOperatorInfoObserverTest, LateObserver) {
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -07001543 // - Add one observer, and verify it gets an MVNO update.
1544 operator_info_->AddObserver(&observer_);
1545
1546 EXPECT_CALL(observer_, OnOperatorChanged()).Times(2);
1547 EXPECT_CALL(second_observer_, OnOperatorChanged()).Times(0);
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -07001548 UpdateMCCMNC("200001");
1549 UpdateOperatorName("name200101");
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -07001550 VerifyMVNOWithUUID("uuid200101");
1551 dispatcher_.DispatchPendingEvents();
1552 Mock::VerifyAndClearExpectations(&observer_);
1553 Mock::VerifyAndClearExpectations(&second_observer_);
1554
1555 EXPECT_CALL(observer_, OnOperatorChanged()).Times(1);
1556 EXPECT_CALL(second_observer_, OnOperatorChanged()).Times(0);
1557 operator_info_->Reset();
1558 VerifyNoMatch();
1559 dispatcher_.DispatchPendingEvents();
1560 Mock::VerifyAndClearExpectations(&observer_);
1561 Mock::VerifyAndClearExpectations(&second_observer_);
1562
1563 // - Add another observer, verify both get an MVNO update.
1564 operator_info_->AddObserver(&second_observer_);
1565
1566 EXPECT_CALL(observer_, OnOperatorChanged()).Times(2);
1567 EXPECT_CALL(second_observer_, OnOperatorChanged()).Times(2);
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -07001568 UpdateMCCMNC("200001");
1569 UpdateOperatorName("name200101");
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -07001570 VerifyMVNOWithUUID("uuid200101");
1571 dispatcher_.DispatchPendingEvents();
1572 Mock::VerifyAndClearExpectations(&observer_);
1573 Mock::VerifyAndClearExpectations(&second_observer_);
1574
1575 EXPECT_CALL(observer_, OnOperatorChanged()).Times(1);
1576 EXPECT_CALL(second_observer_, OnOperatorChanged()).Times(1);
1577 operator_info_->Reset();
1578 VerifyNoMatch();
1579 dispatcher_.DispatchPendingEvents();
1580 Mock::VerifyAndClearExpectations(&observer_);
1581 Mock::VerifyAndClearExpectations(&second_observer_);
1582
1583 // - Remove an observer, verify it no longer gets updates.
1584 operator_info_->RemoveObserver(&observer_);
1585
1586 EXPECT_CALL(observer_, OnOperatorChanged()).Times(0);
1587 EXPECT_CALL(second_observer_, OnOperatorChanged()).Times(2);
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -07001588 UpdateMCCMNC("200001");
1589 UpdateOperatorName("name200101");
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -07001590 VerifyMVNOWithUUID("uuid200101");
1591 dispatcher_.DispatchPendingEvents();
1592 Mock::VerifyAndClearExpectations(&observer_);
1593 Mock::VerifyAndClearExpectations(&second_observer_);
1594
1595 EXPECT_CALL(observer_, OnOperatorChanged()).Times(0);
1596 EXPECT_CALL(second_observer_, OnOperatorChanged()).Times(1);
1597 operator_info_->Reset();
1598 VerifyNoMatch();
1599 dispatcher_.DispatchPendingEvents();
1600 Mock::VerifyAndClearExpectations(&observer_);
1601 Mock::VerifyAndClearExpectations(&second_observer_);
1602}
1603
Prathmesh Prabhu347ff6d2014-05-09 09:28:02 -07001604INSTANTIATE_TEST_CASE_P(MobileOperatorInfoMainTestInstance,
1605 MobileOperatorInfoMainTest,
1606 Values(kEventCheckingPolicyStrict,
1607 kEventCheckingPolicyNonStrict));
1608INSTANTIATE_TEST_CASE_P(MobileOperatorInfoDataTestInstance,
1609 MobileOperatorInfoDataTest,
1610 Values(kEventCheckingPolicyStrict,
1611 kEventCheckingPolicyNonStrict));
1612// It only makes sense to do strict checking here.
1613INSTANTIATE_TEST_CASE_P(MobileOperatorInfoObserverTestInstance,
1614 MobileOperatorInfoObserverTest,
1615 Values(kEventCheckingPolicyStrict));
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -07001616} // namespace shill