File: paired_key_verification_runner_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 (367 lines) | stat: -rw-r--r-- 15,055 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
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
// 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/paired_key_verification_runner.h"

#include <stdint.h>

#include <optional>
#include <vector>

#include "base/functional/callback.h"
#include "base/functional/callback_helpers.h"
#include "base/run_loop.h"
#include "base/test/bind.h"
#include "base/time/time.h"
#include "chrome/browser/nearby_sharing/certificates/fake_nearby_share_certificate_manager.h"
#include "chrome/browser/nearby_sharing/certificates/nearby_share_decrypted_public_certificate.h"
#include "chrome/browser/nearby_sharing/certificates/test_util.h"
#include "chrome/browser/nearby_sharing/incoming_frames_reader.h"
#include "chrome/browser/nearby_sharing/share_target.h"
#include "chrome/services/sharing/public/proto/wire_format.pb.h"
#include "chromeos/ash/components/nearby/common/connections_manager/fake_nearby_connection.h"
#include "chromeos/ash/services/nearby/public/cpp/mock_nearby_process_manager.h"
#include "content/public/test/browser_task_environment.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace {

const char kEndpointId[] = "test_endpoint_id";
const std::vector<uint8_t> kAuthToken = {0, 1, 2};

const std::vector<uint8_t> kPrivateCertificateHashAuthToken = {
    0x8b, 0xcb, 0xa2, 0xf8, 0xe4, 0x06};
const std::vector<uint8_t> kIncomingConnectionSignedData = {
    0x30, 0x45, 0x02, 0x20, 0x4f, 0x83, 0x72, 0xbd, 0x02, 0x70, 0xd9, 0xda,
    0x62, 0x83, 0x5d, 0xb2, 0xdc, 0x6e, 0x3f, 0xa6, 0xa8, 0xa1, 0x4f, 0x5f,
    0xd3, 0xe3, 0xd9, 0x1a, 0x5d, 0x2d, 0x61, 0xd2, 0x6c, 0xdd, 0x8d, 0xa5,
    0x02, 0x21, 0x00, 0xd4, 0xe1, 0x1d, 0x14, 0xcb, 0x58, 0xf7, 0x02, 0xd5,
    0xab, 0x48, 0xe2, 0x2f, 0xcb, 0xc0, 0x53, 0x41, 0x06, 0x50, 0x65, 0x95,
    0x19, 0xa9, 0x22, 0x92, 0x00, 0x42, 0x01, 0x26, 0x25, 0xcb, 0x8c};
const std::vector<uint8_t> kIncomingConnectionInvalidSignedData = {
    0x72, 0xe5, 0x40, 0xa6, 0xdd, 0x1d, 0xfd, 0x44, 0xf2, 0x55, 0xd5, 0x15,
    0x61, 0xe1, 0xe6, 0x15, 0x93, 0xa5, 0xf9, 0x94, 0x16, 0xcd, 0x8b, 0x0a,
    0xb3, 0xca, 0xe6, 0x10, 0x6d, 0x22, 0x3e, 0x1e, 0x3f, 0x34, 0x23, 0x4f,
    0xc2, 0x07, 0xae, 0xcc, 0xb0, 0xb2, 0x5e, 0x26, 0xca, 0x01, 0xd8, 0xe2,
    0x3e, 0xc4, 0x54, 0x0b, 0xfd, 0xc4, 0xf1, 0xa7, 0xf5, 0xa8, 0x63, 0x10,
    0x46, 0x48, 0xdc, 0x98, 0x0b, 0x8f, 0xc3, 0xee, 0xb4, 0x99, 0xb6};

const base::TimeDelta kTimeout = base::Seconds(1);

class MockIncomingFramesReader : public IncomingFramesReader {
 public:
  MockIncomingFramesReader(ash::nearby::NearbyProcessManager* process_manager,
                           NearbyConnection* connection)
      : IncomingFramesReader(process_manager, connection) {}

  MOCK_METHOD(void,
              ReadFrame,
              (base::OnceCallback<
                  void(std::optional<sharing::mojom::V1FramePtr>)> callback),
              (override));

  MOCK_METHOD(
      void,
      ReadFrame,
      (sharing::mojom::V1Frame::Tag frame_type,
       base::OnceCallback<void(std::optional<sharing::mojom::V1FramePtr>)>
           callback,
       base::TimeDelta timeout),
      (override));
};

PairedKeyVerificationRunner::PairedKeyVerificationResult Merge(
    PairedKeyVerificationRunner::PairedKeyVerificationResult local_result,
    sharing::mojom::PairedKeyResultFrame::Status remote_result) {
  if (remote_result == sharing::mojom::PairedKeyResultFrame_Status::kFail ||
      local_result ==
          PairedKeyVerificationRunner::PairedKeyVerificationResult::kFail) {
    return PairedKeyVerificationRunner::PairedKeyVerificationResult::kFail;
  }

  if (remote_result == sharing::mojom::PairedKeyResultFrame_Status::kSuccess &&
      local_result ==
          PairedKeyVerificationRunner::PairedKeyVerificationResult::kSuccess) {
    return PairedKeyVerificationRunner::PairedKeyVerificationResult::kSuccess;
  }

  return PairedKeyVerificationRunner::PairedKeyVerificationResult::kUnable;
}

}  // namespace

class PairedKeyVerificationRunnerTest : public testing::Test {
 public:
  enum class ReturnFrameType {
    // Return std::nullopt for the frame.
    kNull,
    // Return an empty frame.
    kEmpty,
    // Return a valid frame.
    kValid,
    // Return a valid frame where |signed_data| is invalid, but
    // |optional_signed_data| is valid.
    kOptionalSignedDataValid,
    // Return an invalid frame where both |signed_data| and
    // |optional_signed_data| are invalid.
    kOptionalSignedDataInvalid,
  };

  PairedKeyVerificationRunnerTest()
      : frames_reader_(&process_manager_, &connection_) {}

  void SetUp() override { share_target_.is_incoming = true; }

  void RunVerification(bool use_valid_public_certificate,
                       bool restricted_to_contacts,
                       PairedKeyVerificationRunner::PairedKeyVerificationResult
                           expected_result) {
    std::optional<NearbyShareDecryptedPublicCertificate> public_certificate =
        use_valid_public_certificate
            ? std::make_optional<NearbyShareDecryptedPublicCertificate>(
                  GetNearbyShareTestDecryptedPublicCertificate())
            : std::nullopt;

    PairedKeyVerificationRunner runner(
        share_target_, kEndpointId, kAuthToken, &connection_,
        std::move(public_certificate), &certificate_manager_,
        nearby_share::mojom::Visibility::kAllContacts, restricted_to_contacts,
        &frames_reader_, kTimeout);

    base::RunLoop run_loop;
    runner.Run(base::BindLambdaForTesting(
        [&](PairedKeyVerificationRunner::PairedKeyVerificationResult result) {
          EXPECT_EQ(expected_result, result);
          run_loop.Quit();
        }));
    run_loop.Run();
  }

  void SetUpPairedKeyEncryptionFrame(ReturnFrameType frame_type) {
    EXPECT_CALL(
        frames_reader_,
        ReadFrame(
            testing::Eq(sharing::mojom::V1Frame::Tag::kPairedKeyEncryption),
            testing::_, testing::Eq(kTimeout)))
        .WillOnce(testing::WithArg<1>(testing::Invoke(
            [frame_type](
                base::OnceCallback<void(
                    std::optional<sharing::mojom::V1FramePtr>)> callback) {
              if (frame_type == ReturnFrameType::kNull) {
                std::move(callback).Run(std::nullopt);
                return;
              }

              sharing::mojom::V1FramePtr mojo_v1frame;

              if (frame_type == ReturnFrameType::kValid) {
                mojo_v1frame = sharing::mojom::V1Frame::NewPairedKeyEncryption(
                    sharing::mojom::PairedKeyEncryptionFrame::New(
                        kIncomingConnectionSignedData,
                        kPrivateCertificateHashAuthToken, std::nullopt));
              } else if (frame_type ==
                         ReturnFrameType::kOptionalSignedDataValid) {
                mojo_v1frame = sharing::mojom::V1Frame::NewPairedKeyEncryption(
                    sharing::mojom::PairedKeyEncryptionFrame::New(
                        kIncomingConnectionInvalidSignedData,
                        kPrivateCertificateHashAuthToken,
                        kIncomingConnectionSignedData));
              } else if (frame_type ==
                         ReturnFrameType::kOptionalSignedDataInvalid) {
                mojo_v1frame = sharing::mojom::V1Frame::NewPairedKeyEncryption(
                    sharing::mojom::PairedKeyEncryptionFrame::New(
                        kIncomingConnectionInvalidSignedData,
                        kPrivateCertificateHashAuthToken,
                        kIncomingConnectionInvalidSignedData));
              } else {
                mojo_v1frame = sharing::mojom::V1Frame::NewPairedKeyEncryption(
                    sharing::mojom::PairedKeyEncryptionFrame::New());
              }

              std::move(callback).Run(std::move(mojo_v1frame));
            })));
  }

  void SetUpPairedKeyResultFrame(
      ReturnFrameType frame_type,
      sharing::mojom::PairedKeyResultFrame::Status status =
          sharing::mojom::PairedKeyResultFrame_Status::kUnknown) {
    EXPECT_CALL(
        frames_reader_,
        ReadFrame(testing::Eq(sharing::mojom::V1Frame::Tag::kPairedKeyResult),
                  testing::_, testing::Eq(kTimeout)))
        .WillOnce(testing::WithArg<1>(testing::Invoke(
            [=](base::OnceCallback<void(
                    std::optional<sharing::mojom::V1FramePtr>)> callback) {
              if (frame_type == ReturnFrameType::kNull) {
                std::move(callback).Run(std::nullopt);
                return;
              }

              sharing::mojom::V1FramePtr mojo_v1frame =
                  sharing::mojom::V1Frame::NewPairedKeyResult(
                      sharing::mojom::PairedKeyResultFrame::New(status));

              std::move(callback).Run(std::move(mojo_v1frame));
            })));
  }

  sharing::nearby::Frame GetWrittenFrame() {
    std::vector<uint8_t> data = connection_.GetWrittenData();
    sharing::nearby::Frame frame;
    frame.ParseFromArray(data.data(), data.size());
    return frame;
  }

  void ExpectPairedKeyEncryptionFrameSent() {
    sharing::nearby::Frame frame = GetWrittenFrame();
    ASSERT_TRUE(frame.has_v1());
    ASSERT_TRUE(frame.v1().has_paired_key_encryption());
  }

  void ExpectCertificateInfoSent() {
    // TODO - Uncomment when crbug.com/1114765 is resolved.
    // sharing::nearby::Frame frame = GetWrittenFrame();
    // ASSERT_TRUE(frame.has_v1());
    // ASSERT_TRUE(frame.v1().has_certificate_info());
  }

  void ExpectPairedKeyResultFrameSent(
      sharing::nearby::PairedKeyResultFrame::Status status) {
    sharing::nearby::Frame frame = GetWrittenFrame();
    ASSERT_TRUE(frame.has_v1());
    ASSERT_TRUE(frame.v1().has_paired_key_result());
    EXPECT_EQ(status, frame.v1().paired_key_result().status());
  }

 protected:
  content::BrowserTaskEnvironment task_environment_;
  FakeNearbyConnection connection_;
  FakeNearbyShareCertificateManager certificate_manager_;
  testing::NiceMock<ash::nearby::MockNearbyProcessManager> process_manager_;
  testing::NiceMock<MockIncomingFramesReader> frames_reader_;
  ShareTarget share_target_;
};

TEST_F(PairedKeyVerificationRunnerTest,
       NullCertificate_InvalidPairedKeyEncryptionFrame_RestrictToContacts) {
  // Empty key encryption frame fails the certificate verification.
  SetUpPairedKeyEncryptionFrame(ReturnFrameType::kEmpty);

  RunVerification(
      /*use_valid_public_certificate=*/false,
      /*restricted_to_contacts=*/true,
      /*expected_result=*/
      PairedKeyVerificationRunner::PairedKeyVerificationResult::kFail);

  ExpectPairedKeyEncryptionFrameSent();
}

TEST_F(PairedKeyVerificationRunnerTest,
       ValidPairedKeyEncryptionFrame_ResultFrameTimedOut) {
  SetUpPairedKeyEncryptionFrame(ReturnFrameType::kValid);

  // Null result frame fails the certificate verification process.
  SetUpPairedKeyResultFrame(ReturnFrameType::kNull);

  RunVerification(
      /*use_valid_public_certificate=*/true,
      /*restricted_to_contacts=*/false,
      /*expected_result=*/
      PairedKeyVerificationRunner::PairedKeyVerificationResult::kFail);

  ExpectPairedKeyEncryptionFrameSent();
  ExpectPairedKeyResultFrameSent(sharing::nearby::PairedKeyResultFrame::UNABLE);
}

struct TestParameters {
  bool is_target_known;
  bool is_valid_certificate;
  PairedKeyVerificationRunnerTest::ReturnFrameType encryption_frame_type;
  PairedKeyVerificationRunner::PairedKeyVerificationResult result;
} kParameters[] = {
    {true, true, PairedKeyVerificationRunnerTest::ReturnFrameType::kValid,
     PairedKeyVerificationRunner::PairedKeyVerificationResult::kSuccess},
    {true, true,
     PairedKeyVerificationRunnerTest::ReturnFrameType::kOptionalSignedDataValid,
     PairedKeyVerificationRunner::PairedKeyVerificationResult::kSuccess},
    {true, true,
     PairedKeyVerificationRunnerTest::ReturnFrameType::
         kOptionalSignedDataInvalid,
     PairedKeyVerificationRunner::PairedKeyVerificationResult::kFail},
    {true, true, PairedKeyVerificationRunnerTest::ReturnFrameType::kEmpty,
     PairedKeyVerificationRunner::PairedKeyVerificationResult::kFail},
    {true, false, PairedKeyVerificationRunnerTest::ReturnFrameType::kValid,
     PairedKeyVerificationRunner::PairedKeyVerificationResult::kUnable},
    {true, false, PairedKeyVerificationRunnerTest::ReturnFrameType::kEmpty,
     PairedKeyVerificationRunner::PairedKeyVerificationResult::kUnable},
    {false, true, PairedKeyVerificationRunnerTest::ReturnFrameType::kValid,
     PairedKeyVerificationRunner::PairedKeyVerificationResult::kUnable},
};

using KeyVerificationTestParam =
    std::tuple<TestParameters, sharing::mojom::PairedKeyResultFrame_Status>;

class ParameterisedPairedKeyVerificationRunnerTest
    : public PairedKeyVerificationRunnerTest,
      public testing::WithParamInterface<KeyVerificationTestParam> {};

TEST_P(ParameterisedPairedKeyVerificationRunnerTest,
       ValidEncryptionFrame_ValidResultFrame) {
  const TestParameters& params = std::get<0>(GetParam());
  sharing::mojom::PairedKeyResultFrame::Status status = std::get<1>(GetParam());
  PairedKeyVerificationRunner::PairedKeyVerificationResult expected_result =
      Merge(params.result, status);

  share_target_.is_known = params.is_target_known;

  SetUpPairedKeyEncryptionFrame(params.encryption_frame_type);
  SetUpPairedKeyResultFrame(
      PairedKeyVerificationRunnerTest::ReturnFrameType::kValid, status);

  RunVerification(
      /*use_valid_public_certificate=*/params.is_valid_certificate,
      /*restricted_to_contacts=*/false, expected_result);

  ExpectPairedKeyEncryptionFrameSent();
  if (params.encryption_frame_type ==
      PairedKeyVerificationRunnerTest::ReturnFrameType::kValid) {
    ExpectCertificateInfoSent();
  }

  // Check for result frame sent.
  if (!params.is_valid_certificate) {
    ExpectPairedKeyResultFrameSent(
        sharing::nearby::PairedKeyResultFrame::UNABLE);
    return;
  }

  if (params.encryption_frame_type ==
          PairedKeyVerificationRunnerTest::ReturnFrameType::kEmpty ||
      params.encryption_frame_type ==
          PairedKeyVerificationRunnerTest::ReturnFrameType::
              kOptionalSignedDataInvalid) {
    ExpectPairedKeyResultFrameSent(sharing::nearby::PairedKeyResultFrame::FAIL);
    return;
  }

  if (params.is_target_known) {
    ExpectPairedKeyResultFrameSent(
        sharing::nearby::PairedKeyResultFrame::SUCCESS);
  } else {
    ExpectPairedKeyResultFrameSent(
        sharing::nearby::PairedKeyResultFrame::UNABLE);
  }
}

INSTANTIATE_TEST_SUITE_P(
    /*no prefix*/,
    ParameterisedPairedKeyVerificationRunnerTest,
    testing::Combine(
        testing::ValuesIn(kParameters),
        testing::Values(sharing::mojom::PairedKeyResultFrame_Status::kUnknown,
                        sharing::mojom::PairedKeyResultFrame_Status::kSuccess,
                        sharing::mojom::PairedKeyResultFrame_Status::kFail,
                        sharing::mojom::PairedKeyResultFrame_Status::kUnable)));