File: cable_module_android_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 (133 lines) | stat: -rw-r--r-- 6,693 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
// 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 "chrome/browser/webauthn/android/cable_module_android.h"

#include <algorithm>
#include <variant>

#include "components/prefs/testing_pref_service.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace {

constexpr uint8_t kCBOR[] = {
    0xa6, 0x01, 0x58, 0x98, 0x63, 0x5f, 0x31, 0x48, 0x4f, 0x56, 0x39, 0x47,
    0x45, 0x33, 0x55, 0x3a, 0x41, 0x50, 0x41, 0x39, 0x31, 0x62, 0x48, 0x67,
    0x55, 0x67, 0x74, 0x70, 0x62, 0x33, 0x66, 0x73, 0x34, 0x47, 0x7a, 0x5a,
    0x36, 0x79, 0x62, 0x77, 0x69, 0x36, 0x38, 0x77, 0x4b, 0x75, 0x4f, 0x4d,
    0x79, 0x4b, 0x64, 0x35, 0x6d, 0x47, 0x53, 0x58, 0x4d, 0x73, 0x49, 0x66,
    0x70, 0x63, 0x67, 0x6a, 0x61, 0x4e, 0x5f, 0x66, 0x6f, 0x57, 0x61, 0x4e,
    0x30, 0x4d, 0x6c, 0x42, 0x4e, 0x35, 0x7a, 0x34, 0x67, 0x45, 0x46, 0x43,
    0x2d, 0x52, 0x6a, 0x57, 0x70, 0x34, 0x72, 0x4b, 0x46, 0x5a, 0x35, 0x32,
    0x65, 0x59, 0x48, 0x37, 0x36, 0x73, 0x54, 0x33, 0x37, 0x41, 0x66, 0x61,
    0x43, 0x78, 0x68, 0x72, 0x6d, 0x71, 0x65, 0x53, 0x4c, 0x34, 0x6f, 0x30,
    0x6a, 0x72, 0x4d, 0x61, 0x30, 0x78, 0x68, 0x47, 0x4e, 0x37, 0x37, 0x77,
    0x41, 0x71, 0x74, 0x39, 0x56, 0x4e, 0x55, 0x6b, 0x67, 0x4c, 0x31, 0x4e,
    0x55, 0x7a, 0x68, 0x5f, 0x77, 0x44, 0x4e, 0x4e, 0x74, 0x4f, 0x6f, 0x56,
    0x02, 0x48, 0x34, 0x4c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x58,
    0x20, 0xa2, 0xe2, 0x5e, 0x25, 0xdf, 0x51, 0xd8, 0x60, 0x83, 0x15, 0xe1,
    0x7f, 0xbf, 0xf0, 0x18, 0xf5, 0x6b, 0x2a, 0xa4, 0x80, 0x1b, 0xb3, 0xad,
    0x5d, 0x45, 0x38, 0x6b, 0xb2, 0x0f, 0x9c, 0xea, 0x8c, 0x04, 0x58, 0x41,
    0x04, 0x5b, 0xe6, 0x5b, 0x65, 0x4a, 0xd5, 0x6d, 0x2e, 0x26, 0x63, 0xf3,
    0x69, 0x62, 0xd0, 0x81, 0xd8, 0x79, 0x17, 0x6f, 0x58, 0xf5, 0xe1, 0xe1,
    0x5e, 0x76, 0x41, 0xa4, 0x00, 0xa2, 0x00, 0xae, 0x94, 0x9e, 0x53, 0x20,
    0xaa, 0x26, 0x08, 0xe4, 0xa7, 0xfa, 0xc3, 0xb0, 0x11, 0x22, 0x23, 0x79,
    0x3b, 0xd7, 0xd1, 0x93, 0x78, 0xbb, 0x25, 0x60, 0x79, 0x8a, 0xce, 0xc8,
    0xa6, 0xa7, 0xfd, 0xa9, 0x79, 0x05, 0x68, 0x50, 0x69, 0x78, 0x65, 0x6c,
    0x20, 0x36, 0x61, 0x19, 0x03, 0xe7, 0xf5,
};

}

TEST(CableModuleAndroidTest, PaaskInfoFromCBOR) {
  // This CBOR was captured from Play Services.
  std::optional<syncer::DeviceInfo::PhoneAsASecurityKeyInfo> result =
      webauthn::authenticator::internal::PaaskInfoFromCBOR(kCBOR);
  CHECK(result);

  constexpr uint8_t kExpectedPublicKey[] = {
      0x04, 0x5b, 0xe6, 0x5b, 0x65, 0x4a, 0xd5, 0x6d, 0x2e, 0x26, 0x63,
      0xf3, 0x69, 0x62, 0xd0, 0x81, 0xd8, 0x79, 0x17, 0x6f, 0x58, 0xf5,
      0xe1, 0xe1, 0x5e, 0x76, 0x41, 0xa4, 0x00, 0xa2, 0x00, 0xae, 0x94,
      0x9e, 0x53, 0x20, 0xaa, 0x26, 0x08, 0xe4, 0xa7, 0xfa, 0xc3, 0xb0,
      0x11, 0x22, 0x23, 0x79, 0x3b, 0xd7, 0xd1, 0x93, 0x78, 0xbb, 0x25,
      0x60, 0x79, 0x8a, 0xce, 0xc8, 0xa6, 0xa7, 0xfd, 0xa9, 0x79,
  };
  constexpr uint8_t kExpectedSecret[] = {
      0xa2, 0xe2, 0x5e, 0x25, 0xdf, 0x51, 0xd8, 0x60, 0x83, 0x15, 0xe1,
      0x7f, 0xbf, 0xf0, 0x18, 0xf5, 0x6b, 0x2a, 0xa4, 0x80, 0x1b, 0xb3,
      0xad, 0x5d, 0x45, 0x38, 0x6b, 0xb2, 0x0f, 0x9c, 0xea, 0x8c,
  };
  constexpr uint8_t kExpectedContactID[] = {
      0x63, 0x5f, 0x31, 0x48, 0x4f, 0x56, 0x39, 0x47, 0x45, 0x33, 0x55, 0x3a,
      0x41, 0x50, 0x41, 0x39, 0x31, 0x62, 0x48, 0x67, 0x55, 0x67, 0x74, 0x70,
      0x62, 0x33, 0x66, 0x73, 0x34, 0x47, 0x7a, 0x5a, 0x36, 0x79, 0x62, 0x77,
      0x69, 0x36, 0x38, 0x77, 0x4b, 0x75, 0x4f, 0x4d, 0x79, 0x4b, 0x64, 0x35,
      0x6d, 0x47, 0x53, 0x58, 0x4d, 0x73, 0x49, 0x66, 0x70, 0x63, 0x67, 0x6a,
      0x61, 0x4e, 0x5f, 0x66, 0x6f, 0x57, 0x61, 0x4e, 0x30, 0x4d, 0x6c, 0x42,
      0x4e, 0x35, 0x7a, 0x34, 0x67, 0x45, 0x46, 0x43, 0x2d, 0x52, 0x6a, 0x57,
      0x70, 0x34, 0x72, 0x4b, 0x46, 0x5a, 0x35, 0x32, 0x65, 0x59, 0x48, 0x37,
      0x36, 0x73, 0x54, 0x33, 0x37, 0x41, 0x66, 0x61, 0x43, 0x78, 0x68, 0x72,
      0x6d, 0x71, 0x65, 0x53, 0x4c, 0x34, 0x6f, 0x30, 0x6a, 0x72, 0x4d, 0x61,
      0x30, 0x78, 0x68, 0x47, 0x4e, 0x37, 0x37, 0x77, 0x41, 0x71, 0x74, 0x39,
      0x56, 0x4e, 0x55, 0x6b, 0x67, 0x4c, 0x31, 0x4e, 0x55, 0x7a, 0x68, 0x5f,
      0x77, 0x44, 0x4e, 0x4e, 0x74, 0x4f, 0x6f, 0x56,
  };

  EXPECT_EQ(result->tunnel_server_domain, 0);
  EXPECT_EQ(result->id, 19508u);
  EXPECT_TRUE(
      std::ranges::equal(result->peer_public_key_x962, kExpectedPublicKey));
  EXPECT_TRUE(std::ranges::equal(result->secret, kExpectedSecret));
  EXPECT_TRUE(std::ranges::equal(result->contact_id, kExpectedContactID));

  const std::vector<uint8_t> reserialized =
      webauthn::authenticator::internal::CBORFromPaaskInfo(*result);
  const std::optional<syncer::DeviceInfo::PhoneAsASecurityKeyInfo> result2 =
      webauthn::authenticator::internal::PaaskInfoFromCBOR(reserialized);
  CHECK(result2);
  EXPECT_EQ(result->tunnel_server_domain, result2->tunnel_server_domain);
  EXPECT_EQ(result->id, result2->id);
  EXPECT_TRUE(std::ranges::equal(result->peer_public_key_x962,
                                 result2->peer_public_key_x962));
  EXPECT_TRUE(std::ranges::equal(result->secret, result2->secret));
  EXPECT_TRUE(std::ranges::equal(result->contact_id, result2->contact_id));
}

TEST(CableModuleAndroidTest, Cache) {
  using webauthn::authenticator::internal::CacheResult;
  using NotReady = syncer::DeviceInfo::PhoneAsASecurityKeyInfo::NotReady;
  using NoSupport = syncer::DeviceInfo::PhoneAsASecurityKeyInfo::NoSupport;
  using PhoneAsASecurityKeyInfo = syncer::DeviceInfo::PhoneAsASecurityKeyInfo;

  TestingPrefServiceSimple dummy_prefs;
  webauthn::authenticator::RegisterLocalState(dummy_prefs.registry());
  syncer::DeviceInfo::PhoneAsASecurityKeyInfo::StatusOrInfo result;

  result = CacheResult(NotReady(), &dummy_prefs);
  EXPECT_TRUE(std::get_if<NotReady>(&result));

  result = CacheResult(NoSupport(), &dummy_prefs);
  EXPECT_TRUE(std::get_if<NoSupport>(&result));

  // The "NoSupport" state should have been cached.
  result = CacheResult(NotReady(), &dummy_prefs);
  EXPECT_TRUE(std::get_if<NoSupport>(&result));

  std::optional<PhoneAsASecurityKeyInfo> paask_info =
      webauthn::authenticator::internal::PaaskInfoFromCBOR(kCBOR);
  CHECK(paask_info);
  result = CacheResult(*paask_info, &dummy_prefs);
  EXPECT_TRUE(std::get_if<PhoneAsASecurityKeyInfo>(&result));

  // Now the `PhoneAsASecurityKeyInfo` should have been cached.
  result = CacheResult(NotReady(), &dummy_prefs);
  EXPECT_TRUE(std::get_if<PhoneAsASecurityKeyInfo>(&result));

  // Corrupt data should be ignored.
  dummy_prefs.SetString("webauthn.authenticator_info", "AAAA");
  result = CacheResult(NotReady(), &dummy_prefs);
  EXPECT_TRUE(std::get_if<NotReady>(&result));
}