File: network_sms_handler_unittest.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (580 lines) | stat: -rw-r--r-- 24,194 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chromeos/ash/components/network/network_sms_handler.h"

#include <memory>
#include <set>
#include <string>

#include "base/command_line.h"
#include "base/containers/contains.h"
#include "base/memory/raw_ptr.h"
#include "base/run_loop.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/task_environment.h"
#include "base/values.h"
#include "chromeos/ash/components/dbus/shill/fake_sms_client.h"
#include "chromeos/ash/components/dbus/shill/modem_messaging_client.h"
#include "chromeos/ash/components/dbus/shill/shill_clients.h"
#include "chromeos/ash/components/dbus/shill/shill_device_client.h"
#include "chromeos/ash/components/dbus/shill/shill_service_client.h"
#include "chromeos/dbus/constants/dbus_switches.h"
#include "dbus/object_path.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/cros_system_api/dbus/service_constants.h"
#include "third_party/cros_system_api/dbus/shill/dbus-constants.h"

namespace ash {

namespace {

const char kCellularDevicePath[] = "/org/freedesktop/ModemManager1/stub/0";
const char kCellularDeviceObjectPath1[] =
    "/org/freedesktop/ModemManager1/stub/0/Modem/0";
const char kCellularDeviceObjectPath2[] =
    "/org/freedesktop/ModemManager1/stub/0/Modem/1";
const char kSmsPath[] = "/SMS/0";
constexpr char kTestCellularServicePath1[] = "/service/stub/0";
constexpr char kTestCellularServicePath2[] = "/service/stub/1";
constexpr char kTestGuid1[] = "1";
constexpr char kTestGuid2[] = "2";
constexpr char kTestIccid1[] = "000000000000000000";
constexpr char kTestIccid2[] = "000000000000000001";

class TestObserver : public NetworkSmsHandler::Observer {
 public:
  TestObserver() = default;
  ~TestObserver() override = default;

  void MessageReceived(const base::Value::Dict& message) override {
    const std::string* text = message.FindString(NetworkSmsHandler::kTextKey);
    if (text)
      messages_.insert(*text);
  }

  void MessageReceivedFromNetwork(const std::string& guid,
                                  const TextMessageData& mesage_data) override {
    if (mesage_data.text.has_value()) {
      messages_.insert(*mesage_data.text);
      guid_messages_map_[guid].insert(*mesage_data.text);
    }
    if (mesage_data.number.has_value()) {
      EXPECT_EQ(FakeSMSClient::kNumber, *mesage_data.number);
    }

    if (mesage_data.timestamp.has_value()) {
      EXPECT_EQ(FakeSMSClient::kTimestamp, *mesage_data.timestamp);
    }
  }

  void ClearMessages() {
    messages_.clear();
    guid_messages_map_.clear();
  }

  // Returns the count for messages received across all cellular networks.
  int message_count() { return messages_.size(); }

  // Returns all received messages.
  const std::set<std::string>& messages() const {
    return messages_;
  }

  // Returns messages for the network with the given |guid|.
  const std::set<std::string>& messages(const std::string& guid) {
    return guid_messages_map_[guid];
  }

 private:
  std::set<std::string> messages_;
  base::flat_map<std::string, std::set<std::string>> guid_messages_map_;
};

}  // namespace

class NetworkSmsHandlerTest : public testing::Test {
 public:
  NetworkSmsHandlerTest()
      : task_environment_(
            base::test::SingleThreadTaskEnvironment::MainThreadType::UI,
            base::test::TaskEnvironment::TimeSource::MOCK_TIME) {}
  ~NetworkSmsHandlerTest() override = default;

  void SetUp() override {
    // Append '--sms-test-messages' to the command line to tell
    // SMSClientStubImpl to generate a series of test SMS messages.
    base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
    command_line->AppendSwitch(chromeos::switches::kSmsTestMessages);

    shill_clients::InitializeFakes();
    fake_sms_client_ = static_cast<FakeSMSClient*>(SMSClient::Get());
    device_test_ = ShillDeviceClient::Get()->GetTestInterface();
    ASSERT_TRUE(device_test_);
    service_test_ = ShillServiceClient::Get()->GetTestInterface();
    // We want to have only 1 cellular device.
    device_test_->ClearDevices();
    device_test_->AddDevice(kCellularDevicePath, shill::kTypeCellular,
                            "stub_cellular_device2");
    device_test_->SetDeviceProperty(
        kCellularDevicePath, shill::kDBusObjectProperty,
        base::Value(kCellularDeviceObjectPath1), /*notify_changed=*/false);
    SetupCellularModem(kCellularDeviceObjectPath1, kTestCellularServicePath1,
                       kTestGuid1, kTestIccid1, shill::kStateOnline);
    modem_messaging_test_ = ModemMessagingClient::Get()->GetTestInterface();

    // This relies on the stub dbus implementations for ShillManagerClient,
    // ShillDeviceClient, ModemMessagingClient and SMSClient.
    network_sms_handler_.reset(new NetworkSmsHandler());
    network_state_handler_ = NetworkStateHandler::InitializeForTest();
      network_sms_handler_->Init(network_state_handler_.get());

    test_observer_ = std::make_unique<TestObserver>();
    network_sms_handler_->AddObserver(test_observer_.get());
    network_sms_handler_->RequestUpdate();
    base::RunLoop().RunUntilIdle();
  }

  void TearDown() override {
    network_sms_handler_->RemoveObserver(test_observer_.get());
    network_sms_handler_.reset();
    network_state_handler_.reset();
    service_test_ = nullptr;  // Destroyed by shill_clients::Shutdown() below.
    shill_clients::Shutdown();
  }

  void ReceiveSms(const dbus::ObjectPath& object_path,
                  const dbus::ObjectPath& sms_path) {
    modem_messaging_test_->ReceiveSms(object_path, sms_path);
  }

  void CompleteReceiveSms() { fake_sms_client_->CompleteGetAll(); }

  void FastForwardReceiveSmsTimeout() {
    task_environment_.FastForwardBy(NetworkSmsHandler::kFetchSmsDetailsTimeout);
  }

  void SetupCellularModem(const std::string& object_path,
                          const std::string& service_path,
                          const std::string& guid,
                          const std::string& iccid,
                          const std::string& state) {
    device_test_->SetDeviceProperty(
        kCellularDevicePath, shill::kDBusObjectProperty,
        base::Value(object_path), /*notify_changed=*/true);
    device_test_->SetDeviceProperty(kCellularDevicePath, shill::kIccidProperty,
                                    base::Value(iccid),
                                    /*notify_changed=*/true);
    service_test_->AddService(service_path, guid, "", shill::kTypeCellular,
                              state,
                              /*visible=*/true);
    service_test_->SetServiceProperty(service_path, shill::kIccidProperty,
                                      base::Value(iccid));
  }

  void UpdateNetworkState(const std::string& service_path,
                          const std::string& state) {
    service_test_->SetServiceProperty(service_path, shill::kStateProperty,
                                      base::Value(state));
  }

 protected:
  base::test::SingleThreadTaskEnvironment task_environment_;
  raw_ptr<ShillDeviceClient::TestInterface, DanglingUntriaged> device_test_;
  raw_ptr<ModemMessagingClient::TestInterface, DanglingUntriaged>
      modem_messaging_test_;
  raw_ptr<ShillServiceClient::TestInterface> service_test_;
  std::unique_ptr<NetworkSmsHandler> network_sms_handler_;
  std::unique_ptr<TestObserver> test_observer_;

 private:
  std::unique_ptr<NetworkStateHandler> network_state_handler_;
  base::test::ScopedFeatureList features_;
  raw_ptr<FakeSMSClient, DanglingUntriaged> fake_sms_client_;
};

TEST_F(NetworkSmsHandlerTest, DbusStub) {
  EXPECT_EQ(test_observer_->message_count(), 0);

  // Test that no messages have been received yet
  const std::set<std::string>& messages(test_observer_->messages());
  // Note: The following string corresponds to values in
  // ModemMessagingClientStubImpl and SmsClientStubImpl.
  // TODO(stevenjb): Use a TestInterface to set this up to remove dependency.
  const char kMessage1[] = "FakeSMSClient: Test Message: /SMS/0";
  EXPECT_FALSE(base::Contains(messages, kMessage1));

  // Test for messages delivered by signals.
  test_observer_->ClearMessages();
  EXPECT_TRUE(modem_messaging_test_->GetPendingDeleteRequestSmsPath().empty());
  ReceiveSms(dbus::ObjectPath(kCellularDeviceObjectPath1),
             dbus::ObjectPath(kSmsPath));
  CompleteReceiveSms();
  base::RunLoop().RunUntilIdle();

  network_sms_handler_->RequestUpdate();
  base::RunLoop().RunUntilIdle();
  EXPECT_EQ(test_observer_->message_count(), 1);
  EXPECT_TRUE(base::Contains(messages, kMessage1));

  // There should be a request to delete the message after the message has been
  // received. Complete the request.
  EXPECT_EQ(kSmsPath, modem_messaging_test_->GetPendingDeleteRequestSmsPath());
  modem_messaging_test_->CompletePendingDeleteRequest(/*success=*/true);
  base::RunLoop().RunUntilIdle();

  // There should be no more messages to delete.
  EXPECT_TRUE(modem_messaging_test_->GetPendingDeleteRequestSmsPath().empty());

  // Receive two more messages. We shouldn't attempt to delete messages until
  // after we've finished receiving all messages.
  const std::string sms_path_1 = "/SMS/1";
  ReceiveSms(dbus::ObjectPath(kCellularDeviceObjectPath1),
             dbus::ObjectPath(sms_path_1));
  EXPECT_TRUE(modem_messaging_test_->GetPendingDeleteRequestSmsPath().empty());
  const std::string sms_path_2 = "/SMS/2";
  ReceiveSms(dbus::ObjectPath(kCellularDeviceObjectPath1),
             dbus::ObjectPath(sms_path_2));
  EXPECT_TRUE(modem_messaging_test_->GetPendingDeleteRequestSmsPath().empty());

  // Simulate no more messages being received. The last SMS added should be the
  // first one attempted to be deleted.
  CompleteReceiveSms();
  base::RunLoop().RunUntilIdle();
  CompleteReceiveSms();
  base::RunLoop().RunUntilIdle();
  EXPECT_EQ(sms_path_2,
            modem_messaging_test_->GetPendingDeleteRequestSmsPath());
  network_sms_handler_->RequestUpdate();
  base::RunLoop().RunUntilIdle();
  EXPECT_EQ(test_observer_->message_count(), 3);

  // Complete the deletion, another delete request should occur.
  modem_messaging_test_->CompletePendingDeleteRequest(/*success=*/true);
  base::RunLoop().RunUntilIdle();
  EXPECT_EQ(sms_path_1,
            modem_messaging_test_->GetPendingDeleteRequestSmsPath());

  // Receive another message before completing the delete request.
  const std::string sms_path_3 = "/SMS/3";
  ReceiveSms(dbus::ObjectPath(kCellularDeviceObjectPath1),
             dbus::ObjectPath(sms_path_3));
  CompleteReceiveSms();
  EXPECT_EQ(sms_path_1,
            modem_messaging_test_->GetPendingDeleteRequestSmsPath());
  network_sms_handler_->RequestUpdate();
  base::RunLoop().RunUntilIdle();
  EXPECT_EQ(test_observer_->message_count(), 4);

  // Complete the deletion, the last delete request should occur.
  EXPECT_EQ(sms_path_1,
            modem_messaging_test_->GetPendingDeleteRequestSmsPath());
  modem_messaging_test_->CompletePendingDeleteRequest(/*success=*/true);
  base::RunLoop().RunUntilIdle();
  EXPECT_EQ(sms_path_3,
            modem_messaging_test_->GetPendingDeleteRequestSmsPath());

  // Complete the last delete request.
  modem_messaging_test_->CompletePendingDeleteRequest(/*success=*/true);
  base::RunLoop().RunUntilIdle();
  EXPECT_TRUE(modem_messaging_test_->GetPendingDeleteRequestSmsPath().empty());
  network_sms_handler_->RequestUpdate();
  base::RunLoop().RunUntilIdle();
  EXPECT_EQ(test_observer_->message_count(), 4);
}

TEST_F(NetworkSmsHandlerTest, DeleteFailure) {
  EXPECT_EQ(test_observer_->message_count(), 0);

  // Test that no messages have been received yet
  const std::set<std::string>& messages(test_observer_->messages());
  // Note: The following string corresponds to values in
  // ModemMessagingClientStubImpl and SmsClientStubImpl.
  const char kMessage1[] = "FakeSMSClient: Test Message: /SMS/0";
  EXPECT_FALSE(base::Contains(messages, kMessage1));

  // Test for messages delivered by signals.
  test_observer_->ClearMessages();
  EXPECT_TRUE(modem_messaging_test_->GetPendingDeleteRequestSmsPath().empty());
  ReceiveSms(dbus::ObjectPath(kCellularDeviceObjectPath1),
             dbus::ObjectPath(kSmsPath));
  CompleteReceiveSms();
  base::RunLoop().RunUntilIdle();

  network_sms_handler_->RequestUpdate();
  base::RunLoop().RunUntilIdle();
  EXPECT_GE(test_observer_->message_count(), 1);
  EXPECT_TRUE(base::Contains(messages, kMessage1));

  // There should be a request to delete the message after the message has been
  // received.
  EXPECT_EQ(kSmsPath, modem_messaging_test_->GetPendingDeleteRequestSmsPath());

  // Simulate the deletion failing. The delete request should be placed back in
  // the queue, but not invoked.
  modem_messaging_test_->CompletePendingDeleteRequest(/*success=*/false);
  base::RunLoop().RunUntilIdle();
  EXPECT_TRUE(modem_messaging_test_->GetPendingDeleteRequestSmsPath().empty());

  // Receive two more messages. We shouldn't attempt to delete messages until
  // after we've finished receiving all messages.
  const std::string sms_path_1 = "/SMS/1";
  ReceiveSms(dbus::ObjectPath(kCellularDeviceObjectPath1),
             dbus::ObjectPath(sms_path_1));
  EXPECT_TRUE(modem_messaging_test_->GetPendingDeleteRequestSmsPath().empty());
  const std::string sms_path_2 = "/SMS/2";
  ReceiveSms(dbus::ObjectPath(kCellularDeviceObjectPath1),
             dbus::ObjectPath(sms_path_2));
  EXPECT_TRUE(modem_messaging_test_->GetPendingDeleteRequestSmsPath().empty());

  // Simulate no more messages being received. The last SMS added should be the
  // first one attempted to be deleted.
  CompleteReceiveSms();
  base::RunLoop().RunUntilIdle();
  CompleteReceiveSms();
  base::RunLoop().RunUntilIdle();
  EXPECT_EQ(sms_path_2,
            modem_messaging_test_->GetPendingDeleteRequestSmsPath());
  network_sms_handler_->RequestUpdate();
  base::RunLoop().RunUntilIdle();
  EXPECT_GE(test_observer_->message_count(), 3);

  // Simulate the deletion failing, the SMS should be placed back in the queue,
  // but no deletion invoked.
  modem_messaging_test_->CompletePendingDeleteRequest(/*success=*/false);
  base::RunLoop().RunUntilIdle();
  EXPECT_TRUE(modem_messaging_test_->GetPendingDeleteRequestSmsPath().empty());

  // Receive another message, it should be the first message attempted to be
  // deleted.
  const std::string sms_path_3 = "/SMS/3";
  ReceiveSms(dbus::ObjectPath(kCellularDeviceObjectPath1),
             dbus::ObjectPath(sms_path_3));
  CompleteReceiveSms();
  base::RunLoop().RunUntilIdle();
  EXPECT_EQ(sms_path_3,
            modem_messaging_test_->GetPendingDeleteRequestSmsPath());
  network_sms_handler_->RequestUpdate();
  base::RunLoop().RunUntilIdle();
  EXPECT_GE(test_observer_->message_count(), 4);

  // Simulate the deletion succeeding, the second SMS should be attempted to be
  // deleted.
  EXPECT_EQ(sms_path_3,
            modem_messaging_test_->GetPendingDeleteRequestSmsPath());
  modem_messaging_test_->CompletePendingDeleteRequest(/*success=*/true);
  base::RunLoop().RunUntilIdle();
  EXPECT_EQ(sms_path_1,
            modem_messaging_test_->GetPendingDeleteRequestSmsPath());

  // Simulate the deletion succeeding, the first SMS should be attempted to be
  // deleted.
  modem_messaging_test_->CompletePendingDeleteRequest(/*success=*/true);
  base::RunLoop().RunUntilIdle();
  EXPECT_EQ(kSmsPath, modem_messaging_test_->GetPendingDeleteRequestSmsPath());

  // Simulate the deletion succeeding, the third SMS should be attempted to be
  // deleted.
  modem_messaging_test_->CompletePendingDeleteRequest(/*success=*/true);
  base::RunLoop().RunUntilIdle();
  EXPECT_EQ(sms_path_2,
            modem_messaging_test_->GetPendingDeleteRequestSmsPath());
}

TEST_F(NetworkSmsHandlerTest, ReceiveSmsTimeout) {
  EXPECT_EQ(test_observer_->message_count(), 0);

  // Test that no messages have been received yet
  const std::set<std::string>& messages(test_observer_->messages());
  // Note: The following string corresponds to values in
  // ModemMessagingClientStubImpl and SmsClientStubImpl.
  // TODO(stevenjb): Use a TestInterface to set this up to remove dependency.
  const char kMessage1[] = "FakeSMSClient: Test Message: /SMS/0";
  EXPECT_FALSE(base::Contains(messages, kMessage1));

  // Receive 2 SMSes.
  test_observer_->ClearMessages();
  EXPECT_TRUE(modem_messaging_test_->GetPendingDeleteRequestSmsPath().empty());
  ReceiveSms(dbus::ObjectPath(kCellularDeviceObjectPath1),
             dbus::ObjectPath(kSmsPath));
  const std::string sms_path_2 = "/SMS/2";
  ReceiveSms(dbus::ObjectPath(kCellularDeviceObjectPath1),
             dbus::ObjectPath(sms_path_2));
  EXPECT_TRUE(modem_messaging_test_->GetPendingDeleteRequestSmsPath().empty());

  // Simulate the timeout passing for the first SMS. The next SMS in the queue
  // should be processed.
  FastForwardReceiveSmsTimeout();

  // Complete fetching the second SMS' details.
  CompleteReceiveSms();
  base::RunLoop().RunUntilIdle();

  // The second SMS should be found in |messages| but not the first SMS.
  network_sms_handler_->RequestUpdate();
  base::RunLoop().RunUntilIdle();
  EXPECT_EQ(test_observer_->message_count(), 1);
  EXPECT_FALSE(base::Contains(messages, kMessage1));
  EXPECT_TRUE(base::Contains(messages, "FakeSMSClient: Test Message: /SMS/2"));

  // There should be a request to delete the second SMS. Complete the request.
  EXPECT_EQ(sms_path_2,
            modem_messaging_test_->GetPendingDeleteRequestSmsPath());
  modem_messaging_test_->CompletePendingDeleteRequest(/*success=*/true);
  base::RunLoop().RunUntilIdle();

  // There should be a request to delete the first SMS. Complete the request.
  EXPECT_EQ(kSmsPath, modem_messaging_test_->GetPendingDeleteRequestSmsPath());
  modem_messaging_test_->CompletePendingDeleteRequest(/*success=*/true);
  base::RunLoop().RunUntilIdle();

  // There should be no more messages to delete.
  EXPECT_TRUE(modem_messaging_test_->GetPendingDeleteRequestSmsPath().empty());
}

TEST_F(NetworkSmsHandlerTest, EmptyDbusObjectPath) {
  // This test verifies no crash should occur when the device dbus object path
  // is an empty value.
  device_test_->SetDeviceProperty(kCellularDevicePath,
                                  shill::kDBusObjectProperty, base::Value(""),
                                  /*notify_changed=*/true);
  base::RunLoop().RunUntilIdle();
  network_sms_handler_->RequestUpdate();
  base::RunLoop().RunUntilIdle();

  EXPECT_EQ(test_observer_->message_count(), 0);
}

TEST_F(NetworkSmsHandlerTest, DeviceObjectPathChange) {
  // Fake the SIM being switched to a different SIM.
  device_test_->SetDeviceProperty(
      kCellularDevicePath, shill::kDBusObjectProperty,
      base::Value(kCellularDeviceObjectPath2), /*notify_changed=*/true);
  base::RunLoop().RunUntilIdle();
  network_sms_handler_->RequestUpdate();
  base::RunLoop().RunUntilIdle();

  EXPECT_EQ(test_observer_->message_count(), 0);

  // Test that no messages have been received yet
  const std::set<std::string>& messages(test_observer_->messages());
  // Note: The following string corresponds to values in
  // ModemMessagingClientStubImpl and SmsClientStubImpl.
  // TODO(stevenjb): Use a TestInterface to set this up to remove dependency.
  const char kMessage1[] = "FakeSMSClient: Test Message: /SMS/0";
  EXPECT_FALSE(base::Contains(messages, kMessage1));

  // Test for messages delivered by signals.
  test_observer_->ClearMessages();
  ReceiveSms(dbus::ObjectPath(kCellularDeviceObjectPath2),
             dbus::ObjectPath(kSmsPath));
  CompleteReceiveSms();
  base::RunLoop().RunUntilIdle();

  network_sms_handler_->RequestUpdate();
  base::RunLoop().RunUntilIdle();
  EXPECT_GE(test_observer_->message_count(), 1);
  EXPECT_TRUE(base::Contains(messages, kMessage1));
}

TEST_F(NetworkSmsHandlerTest, NetworkGuidTest) {
  // Test that no messages have been received yet
  EXPECT_EQ(test_observer_->message_count(), 0);

  const char kMessage1[] = "FakeSMSClient: Test Message: /SMS/0";
  const char kMessage2[] = "FakeSMSClient: Test Message: /SMS/1";
  EXPECT_EQ(test_observer_->messages().find(kMessage1),
            test_observer_->messages().end());

  // Test for messages for the first network.
  ReceiveSms(dbus::ObjectPath(kCellularDeviceObjectPath1),
             dbus::ObjectPath(kSmsPath));
  CompleteReceiveSms();
  base::RunLoop().RunUntilIdle();

  network_sms_handler_->RequestUpdate();
  base::RunLoop().RunUntilIdle();
  EXPECT_EQ(1u, test_observer_->messages(kTestGuid1).size());
  EXPECT_NE(test_observer_->messages(kTestGuid1).find(kMessage1),
            test_observer_->messages(kTestGuid1).end());
  modem_messaging_test_->CompletePendingDeleteRequest(/*success=*/true);
  base::RunLoop().RunUntilIdle();

  // Switch to a different modem.
  UpdateNetworkState(kTestCellularServicePath1, shill::kStateDisconnecting);
  SetupCellularModem(kCellularDeviceObjectPath2, kTestCellularServicePath2,
                     kTestGuid2, kTestIccid2, shill::kStateOnline);

  base::RunLoop().RunUntilIdle();
  network_sms_handler_->RequestUpdate();

  // Test for messages for messages from the second network.
  EXPECT_EQ(0u, test_observer_->messages(kTestGuid2).size());
  ReceiveSms(dbus::ObjectPath(kCellularDeviceObjectPath2),
             dbus::ObjectPath("/SMS/1"));
  CompleteReceiveSms();
  base::RunLoop().RunUntilIdle();
  EXPECT_EQ(2u, test_observer_->messages().size());
  EXPECT_EQ(1u, test_observer_->messages(kTestGuid2).size());
  EXPECT_NE(test_observer_->messages(kTestGuid2).find(kMessage2),
            test_observer_->messages(kTestGuid2).end());
  EXPECT_EQ(test_observer_->messages(kTestGuid1).find(kMessage2),
            test_observer_->messages(kTestGuid1).end());
}

TEST_F(NetworkSmsHandlerTest, NetworkDelayedActiveNetworkTest) {
  SetupCellularModem(kCellularDeviceObjectPath2, kTestCellularServicePath2,
                     kTestGuid2, kTestIccid2, shill::kStateIdle);
  base::RunLoop().RunUntilIdle();
  network_sms_handler_->RequestUpdate();
  EXPECT_EQ(0u, test_observer_->messages().size());
  ReceiveSms(dbus::ObjectPath(kCellularDeviceObjectPath2),
             dbus::ObjectPath(kSmsPath));
  CompleteReceiveSms();
  base::RunLoop().RunUntilIdle();
  // The message will be sent with the GUID of the currently connected
  // network which is kTestGuid1.
  EXPECT_EQ(1u, test_observer_->messages(kTestGuid1).size());
  EXPECT_EQ(0u, test_observer_->messages(kTestGuid2).size());

  UpdateNetworkState(kTestCellularServicePath1, shill::kStateDisconnecting);
  base::RunLoop().RunUntilIdle();
  ReceiveSms(dbus::ObjectPath(kCellularDeviceObjectPath2),
             dbus::ObjectPath("/SMS/1"));
  CompleteReceiveSms();
  base::RunLoop().RunUntilIdle();
  // No connected network, the GUID of the last connected
  // network will be used.
  EXPECT_EQ(2u, test_observer_->messages(kTestGuid1).size());
  EXPECT_EQ(0u, test_observer_->messages(kTestGuid2).size());

  UpdateNetworkState(kTestCellularServicePath2, shill::kStateOnline);
  base::RunLoop().RunUntilIdle();

  ReceiveSms(dbus::ObjectPath(kCellularDeviceObjectPath2),
             dbus::ObjectPath("/SMS/2"));
  CompleteReceiveSms();
  base::RunLoop().RunUntilIdle();
  // After updating the state, we see that the message is sent with the GUID
  // associated with the last active network.
  EXPECT_EQ(2u, test_observer_->messages(kTestGuid1).size());
  EXPECT_EQ(1u, test_observer_->messages(kTestGuid2).size());
}

TEST_F(NetworkSmsHandlerTest, MessageReceivedNeverConnectedNetwork) {
  UpdateNetworkState(kTestCellularServicePath1, shill::kStateDisconnecting);
  SetupCellularModem(kCellularDeviceObjectPath2, kTestCellularServicePath2,
                     kTestGuid2, kTestIccid2, shill::kStateDisconnecting);

  base::RunLoop().RunUntilIdle();
  network_sms_handler_->RequestUpdate();
  EXPECT_EQ(0u, test_observer_->messages().size());
  ReceiveSms(dbus::ObjectPath(kCellularDeviceObjectPath2),
             dbus::ObjectPath(kSmsPath));
  CompleteReceiveSms();
  base::RunLoop().RunUntilIdle();
  EXPECT_EQ(1u, test_observer_->messages(kTestGuid2).size());
}

}  // namespace ash