File: fcm_topic_subscriber_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 (167 lines) | stat: -rw-r--r-- 5,611 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
// Copyright 2023 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/carrier_lock/fcm_topic_subscriber_impl.h"

#include "base/base64.h"
#include "base/strings/string_util.h"
#include "base/test/repeating_test_future.h"
#include "base/test/task_environment.h"
#include "base/test/test_future.h"
#include "base/time/time.h"
#include "components/gcm_driver/instance_id/fake_gcm_driver_for_instance_id.h"
#include "services/network/public/cpp/weak_wrapper_shared_url_loader_factory.h"
#include "services/network/test/test_url_loader_factory.h"
#include "services/network/test/test_utils.h"
#include "testing/gmock/include/gmock/gmock-matchers.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace ash::carrier_lock {

namespace {

const char kEmbeddedAppIdKey[] = "gcmb";
const char kFcmAppId[] = "com.google.chromeos.carrier_lock";
const char kFcmSenderId[] = "1067228791894";
const char kFcmTopic[] = "/topics/testtopic";
const uint64_t kTestAndroidSecret = 1234;
const uint64_t kTestAndroidId = 1234;
const char kFcmUrl[] = "https://android.clients.google.com/c2dm/register3";

}  // namespace

class FcmTopicSubscriberTest : public testing::Test {
 public:
  FcmTopicSubscriberTest() = default;
  FcmTopicSubscriberTest(const FcmTopicSubscriberTest&) = delete;
  FcmTopicSubscriberTest& operator=(const FcmTopicSubscriberTest&) = delete;
  ~FcmTopicSubscriberTest() override = default;

  void NotificationCallback(bool) {}

 protected:
  // testing::Test:
  void SetUp() override {
    shared_factory_ =
        base::MakeRefCounted<network::WeakWrapperSharedURLLoaderFactory>(
            &test_url_loader_factory_);
    fcm_ = std::make_unique<FcmTopicSubscriberImpl>(
        &gcm_driver_, kFcmAppId, kFcmSenderId, shared_factory_);
    fcm_->android_id_ = kTestAndroidId;
    fcm_->android_secret_ = kTestAndroidSecret;
    fcm_->set_is_testing(true);
    fcm_->Initialize(base::BindRepeating(
        &FcmTopicSubscriberTest::NotificationCallback, base::Unretained(this)));
  }

  void TearDown() override { fcm_.reset(); }

  std::unique_ptr<FcmTopicSubscriberImpl> fcm_;
  base::test::TaskEnvironment task_environment_;
  scoped_refptr<network::SharedURLLoaderFactory> shared_factory_;
  network::TestURLLoaderFactory test_url_loader_factory_;
  instance_id::FakeGCMDriverForInstanceID gcm_driver_;
};

TEST_F(FcmTopicSubscriberTest, CarrierLockSubscribeTopicSuccess) {
  base::test::TestFuture<Result> future;

  // Request token and subscribe with valid topic
  fcm_->SubscribeTopic(
      kFcmTopic,
      future.GetCallback());
  EXPECT_TRUE(test_url_loader_factory_.SimulateResponseForPendingRequest(
      GURL(kFcmUrl), network::URLLoaderCompletionStatus(net::OK),
      network::CreateURLResponseHead(net::HTTP_OK), std::string("{}")));

  // Wait for callback
  EXPECT_EQ(Result::kSuccess, future.Get());
  EXPECT_NE(std::string(), fcm_->token());
}

TEST_F(FcmTopicSubscriberTest, CarrierLockTestNotifications) {
  base::test::TestFuture<Result> future;
  base::test::RepeatingTestFuture<bool> notifications;

  // Request token and subscribe with valid topic
  fcm_->Initialize(notifications.GetCallback());
  fcm_->SubscribeTopic(kFcmTopic, future.GetCallback());
  EXPECT_TRUE(test_url_loader_factory_.SimulateResponseForPendingRequest(
      GURL(kFcmUrl), network::URLLoaderCompletionStatus(net::OK),
      network::CreateURLResponseHead(net::HTTP_OK), std::string("{}")));

  // Wait for subscription callback
  EXPECT_EQ(Result::kSuccess, future.Take());
  EXPECT_NE(std::string(), fcm_->token());

  // Send fake notification with sender id
  gcm::IncomingMessage message;
  message.data[kEmbeddedAppIdKey] = kFcmAppId;
  message.sender_id = kFcmSenderId;
  gcm_driver_.DispatchMessage(kFcmAppId, message);

  // Wait for notification callback
  EXPECT_FALSE(notifications.Take());

  // Send fake notification from topic
  message.sender_id = kFcmTopic;
  gcm_driver_.DispatchMessage(kFcmAppId, message);

  // Wait for notification callback
  EXPECT_TRUE(notifications.Take());
}

TEST_F(FcmTopicSubscriberTest, CarrierLockSubscribeTopicTwice) {
  base::test::TestFuture<Result> future;

  // Request token and subscribe with valid topic
  fcm_->SubscribeTopic(
      kFcmTopic,
      future.GetCallback());
  fcm_->SubscribeTopic(
      kFcmTopic,
      future.GetCallback());

  // Wait for callback
  EXPECT_EQ(Result::kHandlerBusy, future.Take());
  EXPECT_EQ(std::string(), fcm_->token());
}

TEST_F(FcmTopicSubscriberTest, CarrierLockSubscribeTopicFail) {
  base::test::TestFuture<Result> future;

  // Request token and subscribe with empty topic
  fcm_->SubscribeTopic(
      std::string(),
      future.GetCallback());

  // Wait for callback
  EXPECT_EQ(Result::kInvalidInput, future.Take());
  EXPECT_NE(std::string(), fcm_->token());
}

TEST_F(FcmTopicSubscriberTest, CarrierLockGetTokenAndSubscribe) {
  base::test::TestFuture<Result> future;

  // Only request token
  fcm_->RequestToken(
      future.GetCallback());

  // Wait for callback
  EXPECT_EQ(Result::kSuccess, future.Take());
  EXPECT_NE(std::string(), fcm_->token());

  // Subscribe to valid topic
  fcm_->SubscribeTopic(
      kFcmTopic,
      future.GetCallback());
  EXPECT_TRUE(test_url_loader_factory_.SimulateResponseForPendingRequest(
      GURL(kFcmUrl), network::URLLoaderCompletionStatus(net::OK),
      network::CreateURLResponseHead(net::HTTP_OK), std::string("{}")));

  // Wait for callback
  EXPECT_EQ(Result::kSuccess, future.Take());
}

}  // namespace ash::carrier_lock