File: receive_messages_express_unittest.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; 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,811; 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 (327 lines) | stat: -rw-r--r-- 11,708 bytes parent folder | download | duplicates (5)
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
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/nearby_sharing/instantmessaging/receive_messages_express.h"

#include "base/run_loop.h"
#include "base/test/bind.h"
#include "base/test/task_environment.h"
#include "chrome/browser/nearby_sharing/instantmessaging/constants.h"
#include "chrome/browser/nearby_sharing/instantmessaging/fake_token_fetcher.h"
#include "chrome/browser/nearby_sharing/instantmessaging/proto/instantmessaging.pb.h"
#include "components/signin/public/identity_manager/identity_test_environment.h"
#include "services/network/public/cpp/shared_url_loader_factory.h"
#include "services/network/public/cpp/weak_wrapper_shared_url_loader_factory.h"
#include "services/network/test/test_url_loader_factory.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace {

const char kSelfId[] = "self_id";
const char kOAuthToken[] = "oauth_token";
const char kTestAccount[] = "test@test.test";
const char kCountryCode[] = "US";

chrome_browser_nearby_sharing_instantmessaging::ReceiveMessagesResponse
CreateReceiveMessagesResponse(const std::string& msg) {
  chrome_browser_nearby_sharing_instantmessaging::ReceiveMessagesResponse
      response;
  response.mutable_inbox_message()->set_message(msg);
  return response;
}

chrome_browser_nearby_sharing_instantmessaging::ReceiveMessagesResponse
CreateFastPathReadyResponse() {
  chrome_browser_nearby_sharing_instantmessaging::ReceiveMessagesResponse
      response;
  response.mutable_fast_path_ready();
  return response;
}

chrome_browser_nearby_sharing_instantmessaging::StreamBody BuildResponseProto(
    const std::vector<std::string>& messages,
    bool include_fast_path_ready = true) {
  chrome_browser_nearby_sharing_instantmessaging::StreamBody stream_body;
  if (include_fast_path_ready) {
    stream_body.add_messages(CreateFastPathReadyResponse().SerializeAsString());
  }
  for (const auto& msg : messages) {
    stream_body.add_messages(
        CreateReceiveMessagesResponse(msg).SerializeAsString());
  }
  return stream_body;
}

}  // namespace

class FakeIncomingMessagesListener
    : public sharing::mojom::IncomingMessagesListener {
 public:
  ~FakeIncomingMessagesListener() override = default;

  void OnMessage(const std::string& message) override {
    messages_received_.push_back(message);
  }

  void OnComplete(bool success) override { on_complete_result_ = success; }

  const std::vector<std::string>& messages_received() {
    return messages_received_;
  }

  std::optional<bool> on_complete_result() { return on_complete_result_; }

 private:
  std::vector<std::string> messages_received_;
  std::optional<bool> on_complete_result_;
};

class ReceiveMessagesExpressTest : public testing::Test {
 public:
  ReceiveMessagesExpressTest()
      : test_shared_loader_factory_(
            test_url_loader_factory_.GetSafeWeakWrapper()) {
    identity_test_environment_.MakePrimaryAccountAvailable(
        kTestAccount, signin::ConsentLevel::kSignin);
  }
  ~ReceiveMessagesExpressTest() override = default;

  sharing::mojom::LocationHintPtr CountryCodeLocationHint(
      std::string country_code) {
    sharing::mojom::LocationHintPtr location_hint_ptr =
        sharing::mojom::LocationHint::New();
    location_hint_ptr->location = country_code;
    location_hint_ptr->format =
        sharing::mojom::LocationStandardFormat::ISO_3166_1_ALPHA_2;
    return location_hint_ptr;
  }

  network::TestURLLoaderFactory& GetTestUrlLoaderFactory() {
    return test_url_loader_factory_;
  }

  int NumMessagesReceived() {
    return message_listener_.messages_received().size();
  }

  const std::vector<std::string>& GetMessagesReceived() {
    return message_listener_.messages_received();
  }

  std::optional<bool> OnCompleteResult() {
    return message_listener_.on_complete_result();
  }

  std::string GetFastPathOnlyResponse() {
    return BuildResponseProto({}, /*include_fast_path_ready=*/true)
        .SerializeAsString();
  }

  void StartReceivingMessages(base::RunLoop* run_loop, bool token_successful) {
    ReceiveMessagesExpress::StartReceiveSession(
        kSelfId, CountryCodeLocationHint(kCountryCode),
        listener_receiver_.BindNewPipeAndPassRemote(),
        base::BindOnce(&ReceiveMessagesExpressTest::OnStartReceivingMessages,
                       base::Unretained(this), run_loop),
        identity_test_environment_.identity_manager(),
        test_shared_loader_factory_);

    // This allows the token fetcher to resolve.
    identity_test_environment_
        .WaitForAccessTokenRequestIfNecessaryAndRespondWithToken(
            token_successful ? kOAuthToken : "", base::Time::Now());
  }

  void OnStartReceivingMessages(
      base::RunLoop* run_loop,
      bool success,
      mojo::PendingRemote<sharing::mojom::ReceiveMessagesSession>
          pending_remote) {
    start_receive_success_ = success;
    session_pending_remote_ = std::move(pending_remote);
    if (run_loop) {
      run_loop->Quit();
    }
  }

 protected:
  base::test::SingleThreadTaskEnvironment task_environment_;
  signin::IdentityTestEnvironment identity_test_environment_;
  network::TestURLLoaderFactory test_url_loader_factory_;
  scoped_refptr<network::SharedURLLoaderFactory> test_shared_loader_factory_;

  std::optional<bool> start_receive_success_;
  FakeIncomingMessagesListener message_listener_;
  mojo::Receiver<sharing::mojom::IncomingMessagesListener> listener_receiver_{
      &message_listener_};

  mojo::PendingRemote<sharing::mojom::ReceiveMessagesSession>
      session_pending_remote_;
};

TEST_F(ReceiveMessagesExpressTest, OAuthTokenFailed) {
  base::RunLoop run_loop;

  StartReceivingMessages(&run_loop, /*token_success=*/false);
  ASSERT_EQ(0, GetTestUrlLoaderFactory().NumPending());

  run_loop.Run();

  // Token fetch will fail here so we won't receive any messages
  EXPECT_EQ(0, NumMessagesReceived());
  ASSERT_TRUE(start_receive_success_.has_value());
  EXPECT_FALSE(*start_receive_success_);
}

TEST_F(ReceiveMessagesExpressTest, HttpResponseError) {
  base::RunLoop run_loop;

  StartReceivingMessages(&run_loop, /*token_success=*/true);

  ASSERT_TRUE(
      GetTestUrlLoaderFactory().IsPending(kInstantMessagingReceiveMessageAPI));
  std::string response = BuildResponseProto({"message"}).SerializeAsString();

  // Calls OnComplete(false) in ReceiveMessagesExpress. OnDataReceived() is not
  // called.
  GetTestUrlLoaderFactory().AddResponse(kInstantMessagingReceiveMessageAPI,
                                        response, net::HTTP_FORBIDDEN);
  run_loop.Run();

  EXPECT_EQ(0, NumMessagesReceived());
  ASSERT_TRUE(start_receive_success_.has_value());
  EXPECT_FALSE(*start_receive_success_);
}

TEST_F(ReceiveMessagesExpressTest, SuccessfulResponse) {
  base::RunLoop run_loop;

  StartReceivingMessages(&run_loop, /*token_success=*/true);

  ASSERT_EQ(1, GetTestUrlLoaderFactory().NumPending());
  ASSERT_TRUE(
      GetTestUrlLoaderFactory().IsPending(kInstantMessagingReceiveMessageAPI));

  std::vector<std::string> messages = {"quick brown", "fox"};
  std::string response = BuildResponseProto(messages).SerializeAsString();

  // Calls OnDataReceived() in ReceiveMessagesExpress.
  GetTestUrlLoaderFactory().AddResponse(kInstantMessagingReceiveMessageAPI,
                                        response, net::HTTP_OK);
  ASSERT_EQ(0, GetTestUrlLoaderFactory().NumPending());
  run_loop.Run();

  EXPECT_EQ(messages, GetMessagesReceived());
}

TEST_F(ReceiveMessagesExpressTest, SuccessfulPartialResponse) {
  base::RunLoop run_loop;

  StartReceivingMessages(&run_loop, /*token_success=*/true);

  ASSERT_EQ(1, GetTestUrlLoaderFactory().NumPending());
  ASSERT_TRUE(
      GetTestUrlLoaderFactory().IsPending(kInstantMessagingReceiveMessageAPI));

  std::vector<std::string> messages = {"quick brown", "fox"};
  std::string response = BuildResponseProto(messages).SerializeAsString();
  std::string partial_response =
      BuildResponseProto({"partial last message"}).SerializeAsString();
  // Random partial substring.
  response += partial_response.substr(0, 10);

  // Calls OnDataReceived() in ReceiveMessagesExpress.
  GetTestUrlLoaderFactory().AddResponse(kInstantMessagingReceiveMessageAPI,
                                        response, net::HTTP_OK);
  ASSERT_EQ(0, GetTestUrlLoaderFactory().NumPending());
  run_loop.Run();

  EXPECT_EQ(messages, GetMessagesReceived());
}

TEST_F(ReceiveMessagesExpressTest, StopPreventsPendingTransfer) {
  base::RunLoop run_loop;
  StartReceivingMessages(&run_loop, /*token_success=*/true);

  // Calls OnDataReceived() in ReceiveMessagesExpress.
  GetTestUrlLoaderFactory().AddResponse(kInstantMessagingReceiveMessageAPI,
                                        GetFastPathOnlyResponse(),
                                        net::HTTP_OK);
  run_loop.Run();

  EXPECT_TRUE(session_pending_remote_);

  base::RunLoop run_loop_2;
  listener_receiver_.set_disconnect_handler(run_loop_2.QuitClosure());
  {
    mojo::Remote<sharing::mojom::ReceiveMessagesSession> session_remote_(
        std::move(session_pending_remote_));
    session_remote_->StopReceivingMessages();
    // When the remote goes out of scope, the pipe will disconnect and
    // the ReceiveMessageExpress will disconnect and be cleaned up.
  }
  run_loop_2.Run();
}

TEST_F(ReceiveMessagesExpressTest, PendingRemoteCleanupDisconnects) {
  base::RunLoop run_loop;

  StartReceivingMessages(&run_loop, /*token_success=*/true);

  // Calls OnDataReceived() in ReceiveMessagesExpress.
  GetTestUrlLoaderFactory().AddResponse(kInstantMessagingReceiveMessageAPI,
                                        GetFastPathOnlyResponse(),
                                        net::HTTP_OK);
  run_loop.Run();

  // We got our fast path ready and first message, now try stopping the stream.
  EXPECT_TRUE(session_pending_remote_);

  base::RunLoop run_loop_2;
  listener_receiver_.set_disconnect_handler(run_loop_2.QuitClosure());
  // If we reset the session_pending_remote_ before binding everything
  // should disconnect.
  session_pending_remote_.reset();
  run_loop_2.Run();
}

TEST_F(ReceiveMessagesExpressTest, OnCompleteAfterSuccess) {
  base::RunLoop run_loop;
  StartReceivingMessages(&run_loop, /*token_success=*/true);

  std::vector<std::string> messages = {"quick brown", "fox"};
  std::string response = BuildResponseProto(messages).SerializeAsString();
  GetTestUrlLoaderFactory().AddResponse(kInstantMessagingReceiveMessageAPI,
                                        response, net::HTTP_OK);
  run_loop.Run();

  ASSERT_EQ(0, GetTestUrlLoaderFactory().NumPending());
  ASSERT_TRUE(OnCompleteResult().has_value());
  EXPECT_TRUE(OnCompleteResult().value());
}

TEST_F(ReceiveMessagesExpressTest, NoOnCompleteWithoutFastPathReady) {
  base::RunLoop run_loop;
  StartReceivingMessages(&run_loop, /*token_success=*/true);

  std::vector<std::string> messages = {"quick brown", "fox"};
  std::string response =
      BuildResponseProto(messages, /*include_fast_path_ready=*/false)
          .SerializeAsString();
  GetTestUrlLoaderFactory().AddResponse(kInstantMessagingReceiveMessageAPI,
                                        response, net::HTTP_FORBIDDEN);
  run_loop.Run();

  ASSERT_EQ(0, GetTestUrlLoaderFactory().NumPending());
  ASSERT_FALSE(OnCompleteResult().has_value());
}

TEST_F(ReceiveMessagesExpressTest, FastPathTimeout) {
  base::RunLoop run_loop;
  StartReceivingMessages(&run_loop, /*token_success=*/true);
  run_loop.Run();
  ASSERT_TRUE(start_receive_success_.has_value());
  EXPECT_FALSE(start_receive_success_.value());
  ASSERT_FALSE(OnCompleteResult().has_value());
}