OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chromeos/components/tether/local_device_data_provider.h" | |
6 | |
7 #include <string> | |
8 #include <vector> | |
9 | |
10 #include "base/logging.h" | |
11 #include "components/cryptauth/cryptauth_device_manager.h" | |
12 #include "components/cryptauth/cryptauth_enrollment_manager.h" | |
13 #include "components/cryptauth/proto/cryptauth_api.pb.h" | |
14 #include "testing/gmock/include/gmock/gmock.h" | |
15 #include "testing/gtest/include/gtest/gtest.h" | |
16 | |
17 using testing::NiceMock; | |
18 using testing::Return; | |
19 | |
20 namespace chromeos { | |
21 | |
22 namespace tether { | |
23 | |
24 namespace { | |
25 const std::string kDefaultPublicKey = "publicKey"; | |
26 | |
27 const std::string kBeaconSeed1Data = "beaconSeed1Data"; | |
28 const int64_t kBeaconSeed1StartMs = 1000L; | |
29 const int64_t kBeaconSeed1EndMs = 2000L; | |
30 | |
31 const std::string kBeaconSeed2Data = "beaconSeed2Data"; | |
32 const int64_t kBeaconSeed2StartMs = 2000L; | |
33 const int64_t kBeaconSeed2EndMs = 3000L; | |
34 | |
35 cryptauth::BeaconSeed CreateBeaconSeed(const std::string& data, | |
36 int64_t start_ms, | |
37 int64_t end_ms) { | |
38 cryptauth::BeaconSeed seed; | |
39 seed.set_data(data); | |
40 seed.set_start_time_millis(start_ms); | |
41 seed.set_end_time_millis(end_ms); | |
42 return seed; | |
43 } | |
44 } // namespace | |
45 | |
46 class LocalDeviceDataProviderTest : public testing::Test { | |
47 protected: | |
48 class MockProviderContext | |
49 : public LocalDeviceDataProvider::LocalDeviceDataProviderContext { | |
50 public: | |
51 MockProviderContext() {} | |
52 ~MockProviderContext() override {} | |
53 | |
54 MOCK_CONST_METHOD0(GetUserPublicKey, std::string()); | |
55 MOCK_CONST_METHOD0(GetSyncedDevices, | |
56 std::vector<cryptauth::ExternalDeviceInfo>()); | |
57 }; | |
58 | |
59 LocalDeviceDataProviderTest() { | |
60 fake_beacon_seeds_.push_back(CreateBeaconSeed( | |
61 kBeaconSeed1Data, kBeaconSeed1StartMs, kBeaconSeed1EndMs)); | |
62 fake_beacon_seeds_.push_back(CreateBeaconSeed( | |
63 kBeaconSeed2Data, kBeaconSeed2StartMs, kBeaconSeed2EndMs)); | |
64 | |
65 // Has no public key and no BeaconSeeds. | |
66 cryptauth::ExternalDeviceInfo synced_device1; | |
67 fake_synced_devices_.push_back(synced_device1); | |
68 | |
69 // Has no public key and some BeaconSeeds. | |
70 cryptauth::ExternalDeviceInfo synced_device2; | |
71 synced_device2.add_beacon_seeds()->CopyFrom(CreateBeaconSeed( | |
72 kBeaconSeed1Data, kBeaconSeed1StartMs, kBeaconSeed1EndMs)); | |
73 synced_device2.add_beacon_seeds()->CopyFrom(CreateBeaconSeed( | |
74 kBeaconSeed2Data, kBeaconSeed2StartMs, kBeaconSeed2EndMs)); | |
75 fake_synced_devices_.push_back(synced_device2); | |
76 | |
77 // Has another different public key and no BeaconSeeds. | |
78 cryptauth::ExternalDeviceInfo synced_device3; | |
79 synced_device3.set_public_key("anotherPublicKey"); | |
80 fake_synced_devices_.push_back(synced_device3); | |
81 | |
82 // Has different public key and BeaconSeeds. | |
83 cryptauth::ExternalDeviceInfo synced_device4; | |
84 synced_device4.set_public_key("otherPublicKey"); | |
85 synced_device4.add_beacon_seeds()->CopyFrom(CreateBeaconSeed( | |
86 kBeaconSeed1Data, kBeaconSeed1StartMs, kBeaconSeed1EndMs)); | |
87 synced_device4.add_beacon_seeds()->CopyFrom(CreateBeaconSeed( | |
88 kBeaconSeed2Data, kBeaconSeed2StartMs, kBeaconSeed2EndMs)); | |
89 fake_synced_devices_.push_back(synced_device4); | |
90 | |
91 // Has public key but no BeaconSeeds. | |
92 cryptauth::ExternalDeviceInfo synced_device5; | |
93 synced_device5.set_public_key(kDefaultPublicKey); | |
94 fake_synced_devices_.push_back(synced_device5); | |
95 | |
96 // Has public key and BeaconSeeds. | |
97 cryptauth::ExternalDeviceInfo synced_device6; | |
98 synced_device6.set_public_key(kDefaultPublicKey); | |
99 synced_device6.add_beacon_seeds()->CopyFrom(CreateBeaconSeed( | |
100 kBeaconSeed1Data, kBeaconSeed1StartMs, kBeaconSeed1EndMs)); | |
101 synced_device6.add_beacon_seeds()->CopyFrom(CreateBeaconSeed( | |
102 kBeaconSeed2Data, kBeaconSeed2StartMs, kBeaconSeed2EndMs)); | |
103 fake_synced_devices_.push_back(synced_device6); | |
104 } | |
105 | |
106 void SetUp() override { | |
107 mock_context_ = new NiceMock<MockProviderContext>(); | |
108 | |
109 provider_ = base::WrapUnique( | |
110 new LocalDeviceDataProvider(base::WrapUnique(mock_context_))); | |
111 } | |
112 | |
113 std::unique_ptr<LocalDeviceDataProvider> provider_; | |
114 NiceMock<MockProviderContext>* mock_context_; | |
115 | |
116 std::vector<cryptauth::BeaconSeed> fake_beacon_seeds_; | |
117 std::vector<cryptauth::ExternalDeviceInfo> fake_synced_devices_; | |
118 | |
119 private: | |
120 DISALLOW_COPY_AND_ASSIGN(LocalDeviceDataProviderTest); | |
121 }; | |
122 | |
123 TEST_F(LocalDeviceDataProviderTest, TestGetLocalDeviceData_NoPublicKey) { | |
124 ON_CALL(*mock_context_, GetUserPublicKey()) | |
125 .WillByDefault(Return(std::string())); | |
126 ON_CALL(*mock_context_, GetSyncedDevices()) | |
127 .WillByDefault(Return(fake_synced_devices_)); | |
Ryan Hansberry
2016/12/19 20:25:31
nit: make these stub calls a single helper method.
Kyle Horimoto
2016/12/19 21:23:55
I'd like to keep them as-is to make the mocks as e
| |
128 | |
129 std::string public_key; | |
130 std::vector<cryptauth::BeaconSeed> beacon_seeds; | |
131 | |
132 EXPECT_FALSE(provider_->GetLocalDeviceData(&public_key, &beacon_seeds)); | |
133 } | |
134 | |
135 TEST_F(LocalDeviceDataProviderTest, TestGetLocalDeviceData_NoSyncedDevices) { | |
136 ON_CALL(*mock_context_, GetUserPublicKey()) | |
137 .WillByDefault(Return(kDefaultPublicKey)); | |
138 ON_CALL(*mock_context_, GetSyncedDevices()) | |
139 .WillByDefault(Return(std::vector<cryptauth::ExternalDeviceInfo>())); | |
140 | |
141 std::string public_key; | |
142 std::vector<cryptauth::BeaconSeed> beacon_seeds; | |
143 | |
144 EXPECT_FALSE(provider_->GetLocalDeviceData(&public_key, &beacon_seeds)); | |
145 } | |
146 | |
147 TEST_F(LocalDeviceDataProviderTest, | |
148 TestGetLocalDeviceData_NoSyncedDeviceMatchingPublicKey) { | |
149 ON_CALL(*mock_context_, GetUserPublicKey()) | |
150 .WillByDefault(Return(kDefaultPublicKey)); | |
151 ON_CALL(*mock_context_, GetSyncedDevices()) | |
152 .WillByDefault(Return(std::vector<cryptauth::ExternalDeviceInfo>{ | |
153 fake_synced_devices_[0], fake_synced_devices_[1], | |
154 fake_synced_devices_[2], fake_synced_devices_[3]})); | |
155 | |
156 std::string public_key; | |
157 std::vector<cryptauth::BeaconSeed> beacon_seeds; | |
158 | |
159 EXPECT_FALSE(provider_->GetLocalDeviceData(&public_key, &beacon_seeds)); | |
160 } | |
161 | |
162 TEST_F(LocalDeviceDataProviderTest, | |
163 TestGetLocalDeviceData_SyncedDeviceIncludesPublicKeyButNoBeaconSeeds) { | |
164 ON_CALL(*mock_context_, GetUserPublicKey()) | |
165 .WillByDefault(Return(kDefaultPublicKey)); | |
166 ON_CALL(*mock_context_, GetSyncedDevices()) | |
167 .WillByDefault(Return(std::vector<cryptauth::ExternalDeviceInfo>{ | |
168 fake_synced_devices_[4], | |
169 })); | |
170 | |
171 std::string public_key; | |
172 std::vector<cryptauth::BeaconSeed> beacon_seeds; | |
173 | |
174 EXPECT_FALSE(provider_->GetLocalDeviceData(&public_key, &beacon_seeds)); | |
175 } | |
176 | |
177 TEST_F(LocalDeviceDataProviderTest, TestGetLocalDeviceData_Success) { | |
178 ON_CALL(*mock_context_, GetUserPublicKey()) | |
179 .WillByDefault(Return(kDefaultPublicKey)); | |
180 ON_CALL(*mock_context_, GetSyncedDevices()) | |
181 .WillByDefault(Return(fake_synced_devices_)); | |
182 | |
183 std::string public_key; | |
184 std::vector<cryptauth::BeaconSeed> beacon_seeds; | |
185 | |
186 EXPECT_TRUE(provider_->GetLocalDeviceData(&public_key, &beacon_seeds)); | |
187 | |
188 EXPECT_EQ(kDefaultPublicKey, public_key); | |
189 | |
190 ASSERT_EQ(fake_beacon_seeds_.size(), beacon_seeds.size()); | |
191 for (size_t i = 0; i < fake_beacon_seeds_.size(); i++) { | |
192 // Note: google::protobuf::util::MessageDifferencer can only be used to diff | |
193 // Message, but BeaconSeed derives from the incompatible MessageLite class. | |
194 cryptauth::BeaconSeed expected = fake_beacon_seeds_[i]; | |
195 cryptauth::BeaconSeed actual = beacon_seeds[i]; | |
196 EXPECT_EQ(expected.data(), actual.data()); | |
197 EXPECT_EQ(expected.start_time_millis(), actual.start_time_millis()); | |
198 EXPECT_EQ(expected.end_time_millis(), actual.end_time_millis()); | |
199 } | |
200 } | |
201 | |
202 } // namespace tether | |
203 | |
204 } // namespace chromeos | |
OLD | NEW |