File: nearby_endpoint_finder_impl_unittest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (232 lines) | stat: -rw-r--r-- 7,525 bytes parent folder | download | duplicates (6)
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
// 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/ash/secure_channel/nearby_endpoint_finder_impl.h"

#include <memory>
#include <vector>

#include "base/run_loop.h"
#include "base/test/task_environment.h"
#include "chromeos/ash/services/nearby/public/cpp/mock_nearby_connections.h"
#include "chromeos/ash/services/nearby/public/mojom/nearby_connections_types.mojom-shared.h"
#include "chromeos/ash/services/secure_channel/public/mojom/nearby_connector.mojom.h"
#include "mojo/public/cpp/bindings/remote.h"
#include "mojo/public/cpp/bindings/shared_remote.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace ash {
namespace secure_channel {
namespace {

using ::nearby::connections::mojom::DiscoveredEndpointInfo;
using ::nearby::connections::mojom::DiscoveredEndpointInfoPtr;
using ::nearby::connections::mojom::EndpointDiscoveryListener;
using ::nearby::connections::mojom::Status;
using ::testing::_;
using ::testing::Invoke;

const std::vector<uint8_t> GetEid() {
  return std::vector<uint8_t>{0, 1};
}

const std::vector<uint8_t>& GetBluetoothAddress() {
  static const std::vector<uint8_t> address{0, 1, 2, 3, 4, 5};
  return address;
}

}  // namespace

class NearbyEndpointFinderImplTest : public testing::Test {
 protected:
  NearbyEndpointFinderImplTest() = default;
  ~NearbyEndpointFinderImplTest() override = default;

  // testing::Test:
  void SetUp() override {
    finder_ = NearbyEndpointFinderImpl::Factory::Create(
        mock_nearby_connections_.shared_remote());
  }

  void FindEndpoint() {
    base::RunLoop run_loop;
    EXPECT_CALL(mock_nearby_connections_, StartDiscovery(_, _, _, _))
        .WillOnce(Invoke(
            [&](const std::string& service_id, DiscoveryOptionsPtr options,
                mojo::PendingRemote<EndpointDiscoveryListener> listener,
                NearbyConnectionsMojom::StartDiscoveryCallback callback) {
              start_discovery_callback_ = std::move(callback);
              endpoint_discovery_listener_.Bind(std::move(listener));
              run_loop.Quit();
            }));

    finder_->FindEndpoint(
        GetBluetoothAddress(), GetEid(),
        base::BindOnce(&NearbyEndpointFinderImplTest::OnEndpointFound,
                       base::Unretained(this)),
        base::BindOnce(
            &NearbyEndpointFinderImplTest::OnEndpointDiscoveryFailure,
            base::Unretained(this)));

    run_loop.Run();
  }

  void InvokeStartDiscoveryCallback(bool success) {
    base::RunLoop run_loop;

    if (!success) {
      result_closure_ = run_loop.QuitClosure();
      std::move(start_discovery_callback_).Run(Status::kError);
      run_loop.Run();
      return;
    }

    EXPECT_CALL(mock_nearby_connections_,
                InjectBluetoothEndpoint(_, _, _, _, _))
        .WillOnce(Invoke(
            [&](const std::string& service_id, const std::string& endpoint_id,
                const std::vector<uint8_t>& endpoint_info,
                const std::vector<uint8_t>& remote_bluetooth_mac_address,
                NearbyConnectionsMojom::InjectBluetoothEndpointCallback
                    callback) {
              endpoint_id_ = endpoint_id;
              endpoint_info_ = endpoint_info;
              inject_endpoint_callback_ = std::move(callback);
              run_loop.Quit();
            }));

    std::move(start_discovery_callback_).Run(Status::kSuccess);
    run_loop.Run();
  }

  void InvokeInjectEndpointCallback(bool success) {
    base::RunLoop run_loop;

    if (!success) {
      result_closure_ = run_loop.QuitClosure();
      std::move(inject_endpoint_callback_).Run(Status::kError);
      run_loop.Run();
      return;
    }

    std::move(inject_endpoint_callback_).Run(Status::kSuccess);
  }

  void InvokeOnEndpointFound() {
    base::RunLoop run_loop;
    EXPECT_CALL(mock_nearby_connections_, StopDiscovery(_, _))
        .WillOnce(
            Invoke([&](const std::string& service_id,
                       NearbyConnectionsMojom::StopDiscoveryCallback callback) {
              stop_discovery_callback_ = std::move(callback);
              run_loop.Quit();
            }));

    endpoint_discovery_listener_->OnEndpointFound(
        endpoint_id_,
        DiscoveredEndpointInfo::New(endpoint_info_, mojom::kServiceId));
    run_loop.Run();
  }

  void InvokeStopDiscoveryCallback(bool success) {
    base::RunLoop run_loop;
    result_closure_ = run_loop.QuitClosure();
    std::move(stop_discovery_callback_)
        .Run(success ? Status::kSuccess : Status::kError);
    run_loop.Run();
  }

  void DeleteFinder(bool expected_to_stop_discovery) {
    if (!expected_to_stop_discovery) {
      EXPECT_CALL(mock_nearby_connections_, StopDiscovery(_, _)).Times(0);
      finder_.reset();
      return;
    }

    base::RunLoop run_loop;
    EXPECT_CALL(mock_nearby_connections_, StopDiscovery(_, _))
        .WillOnce(
            Invoke([&](const std::string& service_id,
                       NearbyConnectionsMojom::StopDiscoveryCallback callback) {
              std::move(callback).Run(Status::kSuccess);
              run_loop.Quit();
            }));

    finder_.reset();
    run_loop.Run();
  }

  bool has_failed_ = false;

 private:
  void OnEndpointFound(const std::string& endpoint_id,
                       DiscoveredEndpointInfoPtr endpoint_info) {
    EXPECT_EQ(endpoint_id_, endpoint_id);
    std::move(result_closure_).Run();
  }

  void OnEndpointDiscoveryFailure(::nearby::connections::mojom::Status status) {
    has_failed_ = true;
    std::move(result_closure_).Run();
  }

  base::test::TaskEnvironment task_environment_;
  nearby::MockNearbyConnections mock_nearby_connections_;

  std::unique_ptr<NearbyEndpointFinder> finder_;

  base::OnceClosure result_closure_;
  NearbyConnectionsMojom::StartDiscoveryCallback start_discovery_callback_;
  std::string endpoint_id_;
  std::vector<uint8_t> endpoint_info_;
  NearbyConnectionsMojom::InjectBluetoothEndpointCallback
      inject_endpoint_callback_;
  NearbyConnectionsMojom::StopDiscoveryCallback stop_discovery_callback_;

  mojo::Remote<EndpointDiscoveryListener> endpoint_discovery_listener_;
};

TEST_F(NearbyEndpointFinderImplTest, Success) {
  FindEndpoint();
  InvokeStartDiscoveryCallback(/*success=*/true);
  InvokeInjectEndpointCallback(/*success=*/true);
  InvokeOnEndpointFound();
  InvokeStopDiscoveryCallback(/*success=*/true);
  DeleteFinder(/*expected_to_stop_discovery=*/false);

  EXPECT_FALSE(has_failed_);
}

TEST_F(NearbyEndpointFinderImplTest, FailStartingDiscovery) {
  FindEndpoint();
  InvokeStartDiscoveryCallback(/*success=*/false);
  DeleteFinder(/*expected_to_stop_discovery=*/false);

  EXPECT_TRUE(has_failed_);
}

// Failing on CrOS ASAN: crbug.com/1290882
TEST_F(NearbyEndpointFinderImplTest, DISABLED_FailInjectingEndpoint) {
  FindEndpoint();
  InvokeStartDiscoveryCallback(/*success=*/true);
  InvokeInjectEndpointCallback(/*success=*/false);
  DeleteFinder(/*expected_to_stop_discovery=*/true);

  EXPECT_TRUE(has_failed_);
}

TEST_F(NearbyEndpointFinderImplTest, FailStoppingDiscovery) {
  FindEndpoint();
  InvokeStartDiscoveryCallback(/*success=*/true);
  InvokeInjectEndpointCallback(/*success=*/true);
  InvokeOnEndpointFound();
  InvokeStopDiscoveryCallback(/*success=*/false);
  DeleteFinder(/*expected_to_stop_discovery=*/false);

  EXPECT_TRUE(has_failed_);
}

}  // namespace secure_channel
}  // namespace ash