1 // Copyright 2019 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 "chrome/browser/sharing/sharing_sync_preference.h"
6 
7 #include <memory>
8 
9 #include "base/guid.h"
10 #include "base/time/time.h"
11 #include "base/values.h"
12 #include "chrome/browser/sharing/fake_device_info.h"
13 #include "chrome/browser/sharing/proto/sharing_message.pb.h"
14 #include "chrome/common/pref_names.h"
15 #include "components/prefs/scoped_user_pref_update.h"
16 #include "components/sync_device_info/device_info.h"
17 #include "components/sync_device_info/fake_device_info_sync_service.h"
18 #include "components/sync_device_info/fake_device_info_tracker.h"
19 #include "components/sync_device_info/fake_local_device_info_provider.h"
20 #include "components/sync_preferences/testing_pref_service_syncable.h"
21 #include "testing/gtest/include/gtest/gtest.h"
22 
23 namespace {
24 
25 const char kVapidKeyStr[] = "test_vapid_key";
26 const std::vector<uint8_t> kVapidKey =
27     std::vector<uint8_t>(std::begin(kVapidKeyStr), std::end(kVapidKeyStr));
28 
29 const char kDeviceVapidFcmToken[] = "test_vapid_fcm_token";
30 const char kDeviceVapidAuthToken[] = "test_vapid_auth_token";
31 const char kDeviceVapidP256dh[] = "test_vapid_p256dh";
32 const char kDeviceSenderIdFcmToken[] = "test_sender_id_fcm_token";
33 const char kDeviceSenderIdAuthToken[] = "test_sender_id_auth_token";
34 const char kDeviceSenderIdP256dh[] = "test_sender_id_p256dh";
35 
36 const char kAuthorizedEntity[] = "authorized_entity";
37 
38 const char kSharingInfoEnabledFeatures[] = "enabled_features";
39 
40 }  // namespace
41 
42 class SharingSyncPreferenceTest : public testing::Test {
43  protected:
SharingSyncPreferenceTest()44   SharingSyncPreferenceTest()
45       : sharing_sync_preference_(&prefs_, &fake_device_info_sync_service_) {
46     SharingSyncPreference::RegisterProfilePrefs(prefs_.registry());
47   }
48 
GetDefaultSharingInfo()49   syncer::DeviceInfo::SharingInfo GetDefaultSharingInfo() {
50     return syncer::DeviceInfo::SharingInfo(
51         {kDeviceVapidFcmToken, kDeviceVapidP256dh, kDeviceVapidAuthToken},
52         {kDeviceSenderIdFcmToken, kDeviceSenderIdP256dh,
53          kDeviceSenderIdAuthToken},
54         std::set<sync_pb::SharingSpecificFields::EnabledFeatures>{
55             sync_pb::SharingSpecificFields::CLICK_TO_CALL_V2});
56   }
57 
AddEnabledFeature(int feature)58   void AddEnabledFeature(int feature) {
59     const base::DictionaryValue* registration =
60         prefs_.GetDictionary(prefs::kSharingLocalSharingInfo);
61     base::Value enabled_features =
62         registration->FindListKey(kSharingInfoEnabledFeatures)->Clone();
63 
64     enabled_features.Append(feature);
65 
66     DictionaryPrefUpdate local_sharing_info_update(
67         &prefs_, prefs::kSharingLocalSharingInfo);
68     local_sharing_info_update->SetKey(kSharingInfoEnabledFeatures,
69                                       std::move(enabled_features));
70   }
71 
72   sync_preferences::TestingPrefServiceSyncable prefs_;
73   syncer::FakeDeviceInfoSyncService fake_device_info_sync_service_;
74   SharingSyncPreference sharing_sync_preference_;
75 };
76 
TEST_F(SharingSyncPreferenceTest,UpdateVapidKeys)77 TEST_F(SharingSyncPreferenceTest, UpdateVapidKeys) {
78   EXPECT_EQ(base::nullopt, sharing_sync_preference_.GetVapidKey());
79   sharing_sync_preference_.SetVapidKey(kVapidKey);
80   EXPECT_EQ(kVapidKey, sharing_sync_preference_.GetVapidKey());
81 }
82 
TEST_F(SharingSyncPreferenceTest,SyncAndRemoveLocalDevice)83 TEST_F(SharingSyncPreferenceTest, SyncAndRemoveLocalDevice) {
84   const syncer::DeviceInfo* local_device_info =
85       fake_device_info_sync_service_.GetLocalDeviceInfoProvider()
86           ->GetLocalDeviceInfo();
87   fake_device_info_sync_service_.GetDeviceInfoTracker()->Add(local_device_info);
88 
89   // Setting SharingInfo should trigger RefreshLocalDeviceInfoCount.
90   auto sharing_info = GetDefaultSharingInfo();
91   sharing_sync_preference_.SetLocalSharingInfo(sharing_info);
92   EXPECT_EQ(1, fake_device_info_sync_service_.RefreshLocalDeviceInfoCount());
93 
94   // Assume LocalDeviceInfoProvider is updated now.
95   fake_device_info_sync_service_.GetLocalDeviceInfoProvider()
96       ->GetMutableDeviceInfo()
97       ->set_sharing_info(sharing_info);
98 
99   // Setting exactly the same SharingInfo in LocalDeviceInfoProvider shouldn't
100   // trigger RefreshLocalDeviceInfoCount.
101   sharing_sync_preference_.SetLocalSharingInfo(sharing_info);
102   EXPECT_EQ(1, fake_device_info_sync_service_.RefreshLocalDeviceInfoCount());
103 
104   // Clearing SharingInfo should trigger RefreshLocalDeviceInfoCount.
105   sharing_sync_preference_.ClearLocalSharingInfo();
106   EXPECT_EQ(2, fake_device_info_sync_service_.RefreshLocalDeviceInfoCount());
107 }
108 
TEST_F(SharingSyncPreferenceTest,FCMRegistrationGetSet)109 TEST_F(SharingSyncPreferenceTest, FCMRegistrationGetSet) {
110   EXPECT_FALSE(sharing_sync_preference_.GetFCMRegistration());
111 
112   base::Time time_now = base::Time::Now();
113   sharing_sync_preference_.SetFCMRegistration(
114       SharingSyncPreference::FCMRegistration(kAuthorizedEntity, time_now));
115 
116   auto fcm_registration = sharing_sync_preference_.GetFCMRegistration();
117   EXPECT_TRUE(fcm_registration);
118   EXPECT_EQ(kAuthorizedEntity, fcm_registration->authorized_entity);
119   EXPECT_EQ(time_now, fcm_registration->timestamp);
120 
121   // Set FCM registration without authorized entity.
122   sharing_sync_preference_.SetFCMRegistration(
123       SharingSyncPreference::FCMRegistration(base::nullopt, time_now));
124 
125   fcm_registration = sharing_sync_preference_.GetFCMRegistration();
126   EXPECT_TRUE(fcm_registration);
127   EXPECT_FALSE(fcm_registration->authorized_entity);
128   EXPECT_EQ(time_now, fcm_registration->timestamp);
129 
130   sharing_sync_preference_.ClearFCMRegistration();
131   EXPECT_FALSE(sharing_sync_preference_.GetFCMRegistration());
132 }
133 
TEST_F(SharingSyncPreferenceTest,GetLocalSharingInfoForSync)134 TEST_F(SharingSyncPreferenceTest, GetLocalSharingInfoForSync) {
135   auto sharing_info = GetDefaultSharingInfo();
136   sharing_sync_preference_.SetLocalSharingInfo(sharing_info);
137 
138   EXPECT_EQ(sharing_info,
139             SharingSyncPreference::GetLocalSharingInfoForSync(&prefs_));
140 }
141 
TEST_F(SharingSyncPreferenceTest,GetLocalSharingInfoForSync_InvalidEnum)142 TEST_F(SharingSyncPreferenceTest, GetLocalSharingInfoForSync_InvalidEnum) {
143   auto sharing_info = GetDefaultSharingInfo();
144   sharing_sync_preference_.SetLocalSharingInfo(sharing_info);
145 
146   // Add invalid enabled feature enum value.
147   AddEnabledFeature(/*feature=*/-1);
148 
149   // Expect invalid enum value to be filtered out.
150   EXPECT_EQ(sharing_info,
151             SharingSyncPreference::GetLocalSharingInfoForSync(&prefs_));
152 }
153