| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chromeos/components/tether/local_device_data_provider.h" | 5 #include "chromeos/components/tether/local_device_data_provider.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/memory/ptr_util.h" | 11 #include "base/memory/ptr_util.h" |
| 12 #include "components/cryptauth/cryptauth_device_manager.h" | 12 #include "components/cryptauth/cryptauth_device_manager.h" |
| 13 #include "components/cryptauth/cryptauth_enroller.h" |
| 13 #include "components/cryptauth/cryptauth_enrollment_manager.h" | 14 #include "components/cryptauth/cryptauth_enrollment_manager.h" |
| 15 #include "components/cryptauth/fake_cryptauth_gcm_manager.h" |
| 16 #include "components/cryptauth/fake_cryptauth_service.h" |
| 14 #include "components/cryptauth/proto/cryptauth_api.pb.h" | 17 #include "components/cryptauth/proto/cryptauth_api.pb.h" |
| 18 #include "components/cryptauth/secure_message_delegate.h" |
| 15 #include "testing/gmock/include/gmock/gmock.h" | 19 #include "testing/gmock/include/gmock/gmock.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
| 17 | 21 |
| 18 using testing::NiceMock; | 22 using testing::NiceMock; |
| 19 using testing::Return; | 23 using testing::Return; |
| 20 | 24 |
| 21 namespace chromeos { | 25 namespace chromeos { |
| 22 | 26 |
| 23 namespace tether { | 27 namespace tether { |
| 24 | 28 |
| 25 namespace { | 29 namespace { |
| 26 const std::string kDefaultPublicKey = "publicKey"; | |
| 27 | 30 |
| 28 const std::string kBeaconSeed1Data = "beaconSeed1Data"; | 31 const char kDefaultPublicKey[] = "publicKey"; |
| 32 |
| 33 const char kBeaconSeed1Data[] = "beaconSeed1Data"; |
| 29 const int64_t kBeaconSeed1StartMs = 1000L; | 34 const int64_t kBeaconSeed1StartMs = 1000L; |
| 30 const int64_t kBeaconSeed1EndMs = 2000L; | 35 const int64_t kBeaconSeed1EndMs = 2000L; |
| 31 | 36 |
| 32 const std::string kBeaconSeed2Data = "beaconSeed2Data"; | 37 const char kBeaconSeed2Data[] = "beaconSeed2Data"; |
| 33 const int64_t kBeaconSeed2StartMs = 2000L; | 38 const int64_t kBeaconSeed2StartMs = 2000L; |
| 34 const int64_t kBeaconSeed2EndMs = 3000L; | 39 const int64_t kBeaconSeed2EndMs = 3000L; |
| 35 | 40 |
| 41 class MockCryptAuthDeviceManager : public cryptauth::CryptAuthDeviceManager { |
| 42 public: |
| 43 MockCryptAuthDeviceManager() {} |
| 44 ~MockCryptAuthDeviceManager() override {} |
| 45 |
| 46 MOCK_CONST_METHOD0(GetSyncedDevices, |
| 47 std::vector<cryptauth::ExternalDeviceInfo>()); |
| 48 }; |
| 49 |
| 50 class MockCryptAuthEnrollmentManager |
| 51 : public cryptauth::CryptAuthEnrollmentManager { |
| 52 public: |
| 53 explicit MockCryptAuthEnrollmentManager( |
| 54 cryptauth::FakeCryptAuthGCMManager* fake_cryptauth_gcm_manager) |
| 55 : cryptauth::CryptAuthEnrollmentManager( |
| 56 nullptr /* clock */, |
| 57 nullptr /* enroller_factory */, |
| 58 nullptr /* secure_message_delegate */, |
| 59 cryptauth::GcmDeviceInfo(), |
| 60 fake_cryptauth_gcm_manager, |
| 61 nullptr /* pref_service */) {} |
| 62 ~MockCryptAuthEnrollmentManager() override {} |
| 63 |
| 64 MOCK_CONST_METHOD0(GetUserPublicKey, std::string()); |
| 65 }; |
| 66 |
| 36 cryptauth::BeaconSeed CreateBeaconSeed(const std::string& data, | 67 cryptauth::BeaconSeed CreateBeaconSeed(const std::string& data, |
| 37 int64_t start_ms, | 68 int64_t start_ms, |
| 38 int64_t end_ms) { | 69 int64_t end_ms) { |
| 39 cryptauth::BeaconSeed seed; | 70 cryptauth::BeaconSeed seed; |
| 40 seed.set_data(data); | 71 seed.set_data(data); |
| 41 seed.set_start_time_millis(start_ms); | 72 seed.set_start_time_millis(start_ms); |
| 42 seed.set_end_time_millis(end_ms); | 73 seed.set_end_time_millis(end_ms); |
| 43 return seed; | 74 return seed; |
| 44 } | 75 } |
| 76 |
| 45 } // namespace | 77 } // namespace |
| 46 | 78 |
| 47 class LocalDeviceDataProviderTest : public testing::Test { | 79 class LocalDeviceDataProviderTest : public testing::Test { |
| 48 protected: | 80 protected: |
| 49 class MockProviderDelegate | |
| 50 : public LocalDeviceDataProvider::LocalDeviceDataProviderDelegate { | |
| 51 public: | |
| 52 MockProviderDelegate() {} | |
| 53 ~MockProviderDelegate() override {} | |
| 54 | |
| 55 MOCK_CONST_METHOD0(GetUserPublicKey, std::string()); | |
| 56 MOCK_CONST_METHOD0(GetSyncedDevices, | |
| 57 std::vector<cryptauth::ExternalDeviceInfo>()); | |
| 58 }; | |
| 59 | |
| 60 LocalDeviceDataProviderTest() { | 81 LocalDeviceDataProviderTest() { |
| 61 fake_beacon_seeds_.push_back(CreateBeaconSeed( | 82 fake_beacon_seeds_.push_back(CreateBeaconSeed( |
| 62 kBeaconSeed1Data, kBeaconSeed1StartMs, kBeaconSeed1EndMs)); | 83 kBeaconSeed1Data, kBeaconSeed1StartMs, kBeaconSeed1EndMs)); |
| 63 fake_beacon_seeds_.push_back(CreateBeaconSeed( | 84 fake_beacon_seeds_.push_back(CreateBeaconSeed( |
| 64 kBeaconSeed2Data, kBeaconSeed2StartMs, kBeaconSeed2EndMs)); | 85 kBeaconSeed2Data, kBeaconSeed2StartMs, kBeaconSeed2EndMs)); |
| 65 | 86 |
| 66 // Has no public key and no BeaconSeeds. | 87 // Has no public key and no BeaconSeeds. |
| 67 cryptauth::ExternalDeviceInfo synced_device1; | 88 cryptauth::ExternalDeviceInfo synced_device1; |
| 68 fake_synced_devices_.push_back(synced_device1); | 89 fake_synced_devices_.push_back(synced_device1); |
| 69 | 90 |
| (...skipping 28 matching lines...) Expand all Loading... |
| 98 cryptauth::ExternalDeviceInfo synced_device6; | 119 cryptauth::ExternalDeviceInfo synced_device6; |
| 99 synced_device6.set_public_key(kDefaultPublicKey); | 120 synced_device6.set_public_key(kDefaultPublicKey); |
| 100 synced_device6.add_beacon_seeds()->CopyFrom(CreateBeaconSeed( | 121 synced_device6.add_beacon_seeds()->CopyFrom(CreateBeaconSeed( |
| 101 kBeaconSeed1Data, kBeaconSeed1StartMs, kBeaconSeed1EndMs)); | 122 kBeaconSeed1Data, kBeaconSeed1StartMs, kBeaconSeed1EndMs)); |
| 102 synced_device6.add_beacon_seeds()->CopyFrom(CreateBeaconSeed( | 123 synced_device6.add_beacon_seeds()->CopyFrom(CreateBeaconSeed( |
| 103 kBeaconSeed2Data, kBeaconSeed2StartMs, kBeaconSeed2EndMs)); | 124 kBeaconSeed2Data, kBeaconSeed2StartMs, kBeaconSeed2EndMs)); |
| 104 fake_synced_devices_.push_back(synced_device6); | 125 fake_synced_devices_.push_back(synced_device6); |
| 105 } | 126 } |
| 106 | 127 |
| 107 void SetUp() override { | 128 void SetUp() override { |
| 108 mock_delegate_ = new NiceMock<MockProviderDelegate>(); | 129 mock_device_manager_ = |
| 130 base::WrapUnique(new NiceMock<MockCryptAuthDeviceManager>()); |
| 131 fake_cryptauth_gcm_manager_ = |
| 132 base::MakeUnique<cryptauth::FakeCryptAuthGCMManager>("registrationId"); |
| 133 mock_enrollment_manager_ = |
| 134 base::WrapUnique(new NiceMock<MockCryptAuthEnrollmentManager>( |
| 135 fake_cryptauth_gcm_manager_.get())); |
| 136 |
| 137 fake_cryptauth_service_ = |
| 138 base::MakeUnique<cryptauth::FakeCryptAuthService>(); |
| 139 fake_cryptauth_service_->set_cryptauth_device_manager( |
| 140 mock_device_manager_.get()); |
| 141 fake_cryptauth_service_->set_cryptauth_enrollment_manager( |
| 142 mock_enrollment_manager_.get()); |
| 109 | 143 |
| 110 provider_ = base::WrapUnique( | 144 provider_ = base::WrapUnique( |
| 111 new LocalDeviceDataProvider(base::WrapUnique(mock_delegate_))); | 145 new LocalDeviceDataProvider(fake_cryptauth_service_.get())); |
| 112 } | 146 } |
| 113 | 147 |
| 114 std::unique_ptr<LocalDeviceDataProvider> provider_; | |
| 115 NiceMock<MockProviderDelegate>* mock_delegate_; | |
| 116 | |
| 117 std::vector<cryptauth::BeaconSeed> fake_beacon_seeds_; | 148 std::vector<cryptauth::BeaconSeed> fake_beacon_seeds_; |
| 118 std::vector<cryptauth::ExternalDeviceInfo> fake_synced_devices_; | 149 std::vector<cryptauth::ExternalDeviceInfo> fake_synced_devices_; |
| 119 | 150 |
| 151 std::unique_ptr<cryptauth::FakeCryptAuthGCMManager> |
| 152 fake_cryptauth_gcm_manager_; |
| 153 std::unique_ptr<NiceMock<MockCryptAuthDeviceManager>> mock_device_manager_; |
| 154 std::unique_ptr<NiceMock<MockCryptAuthEnrollmentManager>> |
| 155 mock_enrollment_manager_; |
| 156 std::unique_ptr<cryptauth::FakeCryptAuthService> fake_cryptauth_service_; |
| 157 |
| 158 std::unique_ptr<LocalDeviceDataProvider> provider_; |
| 159 |
| 120 private: | 160 private: |
| 121 DISALLOW_COPY_AND_ASSIGN(LocalDeviceDataProviderTest); | 161 DISALLOW_COPY_AND_ASSIGN(LocalDeviceDataProviderTest); |
| 122 }; | 162 }; |
| 123 | 163 |
| 124 TEST_F(LocalDeviceDataProviderTest, TestGetLocalDeviceData_NoPublicKey) { | 164 TEST_F(LocalDeviceDataProviderTest, TestGetLocalDeviceData_NoPublicKey) { |
| 125 ON_CALL(*mock_delegate_, GetUserPublicKey()) | 165 ON_CALL(*mock_enrollment_manager_, GetUserPublicKey()) |
| 126 .WillByDefault(Return(std::string())); | 166 .WillByDefault(Return(std::string())); |
| 127 ON_CALL(*mock_delegate_, GetSyncedDevices()) | 167 ON_CALL(*mock_device_manager_, GetSyncedDevices()) |
| 128 .WillByDefault(Return(fake_synced_devices_)); | 168 .WillByDefault(Return(fake_synced_devices_)); |
| 129 | 169 |
| 130 std::string public_key; | 170 std::string public_key; |
| 131 std::vector<cryptauth::BeaconSeed> beacon_seeds; | 171 std::vector<cryptauth::BeaconSeed> beacon_seeds; |
| 132 | 172 |
| 133 EXPECT_FALSE(provider_->GetLocalDeviceData(&public_key, &beacon_seeds)); | 173 EXPECT_FALSE(provider_->GetLocalDeviceData(&public_key, &beacon_seeds)); |
| 134 } | 174 } |
| 135 | 175 |
| 136 TEST_F(LocalDeviceDataProviderTest, TestGetLocalDeviceData_NoSyncedDevices) { | 176 TEST_F(LocalDeviceDataProviderTest, TestGetLocalDeviceData_NoSyncedDevices) { |
| 137 ON_CALL(*mock_delegate_, GetUserPublicKey()) | 177 ON_CALL(*mock_enrollment_manager_, GetUserPublicKey()) |
| 138 .WillByDefault(Return(kDefaultPublicKey)); | 178 .WillByDefault(Return(kDefaultPublicKey)); |
| 139 ON_CALL(*mock_delegate_, GetSyncedDevices()) | 179 ON_CALL(*mock_device_manager_, GetSyncedDevices()) |
| 140 .WillByDefault(Return(std::vector<cryptauth::ExternalDeviceInfo>())); | 180 .WillByDefault(Return(std::vector<cryptauth::ExternalDeviceInfo>())); |
| 141 | 181 |
| 142 std::string public_key; | 182 std::string public_key; |
| 143 std::vector<cryptauth::BeaconSeed> beacon_seeds; | 183 std::vector<cryptauth::BeaconSeed> beacon_seeds; |
| 144 | 184 |
| 145 EXPECT_FALSE(provider_->GetLocalDeviceData(&public_key, &beacon_seeds)); | 185 EXPECT_FALSE(provider_->GetLocalDeviceData(&public_key, &beacon_seeds)); |
| 146 } | 186 } |
| 147 | 187 |
| 148 TEST_F(LocalDeviceDataProviderTest, | 188 TEST_F(LocalDeviceDataProviderTest, |
| 149 TestGetLocalDeviceData_NoSyncedDeviceMatchingPublicKey) { | 189 TestGetLocalDeviceData_NoSyncedDeviceMatchingPublicKey) { |
| 150 ON_CALL(*mock_delegate_, GetUserPublicKey()) | 190 ON_CALL(*mock_enrollment_manager_, GetUserPublicKey()) |
| 151 .WillByDefault(Return(kDefaultPublicKey)); | 191 .WillByDefault(Return(kDefaultPublicKey)); |
| 152 ON_CALL(*mock_delegate_, GetSyncedDevices()) | 192 ON_CALL(*mock_device_manager_, GetSyncedDevices()) |
| 153 .WillByDefault(Return(std::vector<cryptauth::ExternalDeviceInfo>{ | 193 .WillByDefault(Return(std::vector<cryptauth::ExternalDeviceInfo>{ |
| 154 fake_synced_devices_[0], fake_synced_devices_[1], | 194 fake_synced_devices_[0], fake_synced_devices_[1], |
| 155 fake_synced_devices_[2], fake_synced_devices_[3]})); | 195 fake_synced_devices_[2], fake_synced_devices_[3]})); |
| 156 | 196 |
| 157 std::string public_key; | 197 std::string public_key; |
| 158 std::vector<cryptauth::BeaconSeed> beacon_seeds; | 198 std::vector<cryptauth::BeaconSeed> beacon_seeds; |
| 159 | 199 |
| 160 EXPECT_FALSE(provider_->GetLocalDeviceData(&public_key, &beacon_seeds)); | 200 EXPECT_FALSE(provider_->GetLocalDeviceData(&public_key, &beacon_seeds)); |
| 161 } | 201 } |
| 162 | 202 |
| 163 TEST_F(LocalDeviceDataProviderTest, | 203 TEST_F(LocalDeviceDataProviderTest, |
| 164 TestGetLocalDeviceData_SyncedDeviceIncludesPublicKeyButNoBeaconSeeds) { | 204 TestGetLocalDeviceData_SyncedDeviceIncludesPublicKeyButNoBeaconSeeds) { |
| 165 ON_CALL(*mock_delegate_, GetUserPublicKey()) | 205 ON_CALL(*mock_enrollment_manager_, GetUserPublicKey()) |
| 166 .WillByDefault(Return(kDefaultPublicKey)); | 206 .WillByDefault(Return(kDefaultPublicKey)); |
| 167 ON_CALL(*mock_delegate_, GetSyncedDevices()) | 207 ON_CALL(*mock_device_manager_, GetSyncedDevices()) |
| 168 .WillByDefault(Return(std::vector<cryptauth::ExternalDeviceInfo>{ | 208 .WillByDefault(Return(std::vector<cryptauth::ExternalDeviceInfo>{ |
| 169 fake_synced_devices_[4], | 209 fake_synced_devices_[4], |
| 170 })); | 210 })); |
| 171 | 211 |
| 172 std::string public_key; | 212 std::string public_key; |
| 173 std::vector<cryptauth::BeaconSeed> beacon_seeds; | 213 std::vector<cryptauth::BeaconSeed> beacon_seeds; |
| 174 | 214 |
| 175 EXPECT_FALSE(provider_->GetLocalDeviceData(&public_key, &beacon_seeds)); | 215 EXPECT_FALSE(provider_->GetLocalDeviceData(&public_key, &beacon_seeds)); |
| 176 } | 216 } |
| 177 | 217 |
| 178 TEST_F(LocalDeviceDataProviderTest, TestGetLocalDeviceData_Success) { | 218 TEST_F(LocalDeviceDataProviderTest, TestGetLocalDeviceData_Success) { |
| 179 ON_CALL(*mock_delegate_, GetUserPublicKey()) | 219 ON_CALL(*mock_enrollment_manager_, GetUserPublicKey()) |
| 180 .WillByDefault(Return(kDefaultPublicKey)); | 220 .WillByDefault(Return(kDefaultPublicKey)); |
| 181 ON_CALL(*mock_delegate_, GetSyncedDevices()) | 221 ON_CALL(*mock_device_manager_, GetSyncedDevices()) |
| 182 .WillByDefault(Return(fake_synced_devices_)); | 222 .WillByDefault(Return(fake_synced_devices_)); |
| 183 | 223 |
| 184 std::string public_key; | 224 std::string public_key; |
| 185 std::vector<cryptauth::BeaconSeed> beacon_seeds; | 225 std::vector<cryptauth::BeaconSeed> beacon_seeds; |
| 186 | 226 |
| 187 EXPECT_TRUE(provider_->GetLocalDeviceData(&public_key, &beacon_seeds)); | 227 EXPECT_TRUE(provider_->GetLocalDeviceData(&public_key, &beacon_seeds)); |
| 188 | 228 |
| 189 EXPECT_EQ(kDefaultPublicKey, public_key); | 229 EXPECT_EQ(kDefaultPublicKey, public_key); |
| 190 | 230 |
| 191 ASSERT_EQ(fake_beacon_seeds_.size(), beacon_seeds.size()); | 231 ASSERT_EQ(fake_beacon_seeds_.size(), beacon_seeds.size()); |
| 192 for (size_t i = 0; i < fake_beacon_seeds_.size(); i++) { | 232 for (size_t i = 0; i < fake_beacon_seeds_.size(); i++) { |
| 193 // Note: google::protobuf::util::MessageDifferencer can only be used to diff | 233 // Note: google::protobuf::util::MessageDifferencer can only be used to diff |
| 194 // Message, but BeaconSeed derives from the incompatible MessageLite class. | 234 // Message, but BeaconSeed derives from the incompatible MessageLite class. |
| 195 cryptauth::BeaconSeed expected = fake_beacon_seeds_[i]; | 235 cryptauth::BeaconSeed expected = fake_beacon_seeds_[i]; |
| 196 cryptauth::BeaconSeed actual = beacon_seeds[i]; | 236 cryptauth::BeaconSeed actual = beacon_seeds[i]; |
| 197 EXPECT_EQ(expected.data(), actual.data()); | 237 EXPECT_EQ(expected.data(), actual.data()); |
| 198 EXPECT_EQ(expected.start_time_millis(), actual.start_time_millis()); | 238 EXPECT_EQ(expected.start_time_millis(), actual.start_time_millis()); |
| 199 EXPECT_EQ(expected.end_time_millis(), actual.end_time_millis()); | 239 EXPECT_EQ(expected.end_time_millis(), actual.end_time_millis()); |
| 200 } | 240 } |
| 201 } | 241 } |
| 202 | 242 |
| 203 } // namespace tether | 243 } // namespace tether |
| 204 | 244 |
| 205 } // namespace chromeos | 245 } // namespace chromeos |
| OLD | NEW |