File: consent_sync_bridge_impl_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 (420 lines) | stat: -rw-r--r-- 16,795 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
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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/consent_auditor/consent_sync_bridge_impl.h"

#include <map>
#include <set>
#include <utility>

#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/run_loop.h"
#include "base/test/task_environment.h"
#include "components/sync/model/data_batch.h"
#include "components/sync/protocol/entity_specifics.pb.h"
#include "components/sync/protocol/user_consent_specifics.pb.h"
#include "components/sync/test/data_type_store_test_util.h"
#include "components/sync/test/mock_data_type_local_change_processor.h"
#include "google_apis/gaia/gaia_id.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace consent_auditor {
namespace {

using sync_pb::UserConsentSpecifics;
using syncer::DataBatch;
using syncer::DataTypeStore;
using syncer::DataTypeStoreTestUtil;
using syncer::DataTypeSyncBridge;
using syncer::EntityChange;
using syncer::EntityChangeList;
using syncer::EntityData;
using syncer::MetadataChangeList;
using syncer::MockDataTypeLocalChangeProcessor;
using syncer::OnceDataTypeStoreFactory;
using testing::_;
using testing::ElementsAre;
using testing::Eq;
using testing::InvokeWithoutArgs;
using testing::IsEmpty;
using testing::IsNull;
using testing::NotNull;
using testing::Pair;
using testing::Pointee;
using testing::Property;
using testing::Return;
using testing::SaveArg;
using testing::SizeIs;
using testing::UnorderedElementsAre;
using testing::WithArg;
using WriteBatch = DataTypeStore::WriteBatch;

constexpr GaiaId::Literal kDefaultGaiaId("gaia_id");

MATCHER_P(MatchesUserConsent, expected, "") {
  if (!arg.has_user_consent()) {
    *result_listener << "which is not a user consent";
    return false;
  }
  const UserConsentSpecifics& actual = arg.user_consent();
  if (actual.client_consent_time_usec() !=
      expected.client_consent_time_usec()) {
    return false;
  }
  return true;
}

UserConsentSpecifics CreateSpecifics(int64_t client_consent_time_usec) {
  UserConsentSpecifics specifics;
  specifics.set_client_consent_time_usec(client_consent_time_usec);
  specifics.set_obfuscated_gaia_id(kDefaultGaiaId.ToString());
  return specifics;
}

std::unique_ptr<UserConsentSpecifics> SpecificsUniquePtr(
    int64_t client_consent_time_usec) {
  return std::make_unique<UserConsentSpecifics>(
      CreateSpecifics(client_consent_time_usec));
}

class ConsentSyncBridgeImplTest : public testing::Test {
 protected:
  ConsentSyncBridgeImplTest() { ResetBridge(); }

  void ResetBridge() {
    OnceDataTypeStoreFactory store_factory;
    if (bridge_) {
      // Carry over the underlying store from previous bridge instances.
      std::unique_ptr<DataTypeStore> store = bridge_->StealStoreForTest();
      bridge_.reset();
      store_factory =
          DataTypeStoreTestUtil::MoveStoreToFactory(std::move(store));
    } else {
      store_factory = DataTypeStoreTestUtil::FactoryForInMemoryStoreForTest();
    }

    bridge_ = std::make_unique<ConsentSyncBridgeImpl>(
        std::move(store_factory), mock_processor_.CreateForwardingProcessor());
  }

  void WaitUntilModelReadyToSync(const GaiaId& gaia_id) {
    base::RunLoop loop;
    base::RepeatingClosure quit_closure = loop.QuitClosure();
    // Let the bridge initialize fully, which should run ModelReadyToSync().
    ON_CALL(*processor(), ModelReadyToSync(_))
        .WillByDefault(InvokeWithoutArgs([=]() { quit_closure.Run(); }));
    loop.Run();
    ON_CALL(*processor(), IsTrackingMetadata()).WillByDefault(Return(true));
    ON_CALL(*processor(), TrackedGaiaId()).WillByDefault(Return(gaia_id));
  }

  static std::string GetStorageKey(const UserConsentSpecifics& specifics) {
    return ConsentSyncBridgeImpl::GetStorageKeyFromSpecificsForTest(specifics);
  }

  ConsentSyncBridgeImpl* bridge() { return bridge_.get(); }
  MockDataTypeLocalChangeProcessor* processor() { return &mock_processor_; }

  std::map<std::string, sync_pb::EntitySpecifics> GetAllDataForDebugging() {
    std::unique_ptr<DataBatch> batch = bridge_->GetAllDataForDebugging();
    EXPECT_NE(nullptr, batch);

    std::map<std::string, sync_pb::EntitySpecifics> storage_key_to_specifics;
    if (batch != nullptr) {
      while (batch->HasNext()) {
        const syncer::KeyAndData& pair = batch->Next();
        storage_key_to_specifics[pair.first] = pair.second->specifics;
      }
    }
    return storage_key_to_specifics;
  }

  std::unique_ptr<sync_pb::EntitySpecifics> GetDataForCommit(
      const std::string& storage_key) {
    std::unique_ptr<DataBatch> batch = bridge_->GetDataForCommit({storage_key});
    EXPECT_NE(nullptr, batch);

    std::unique_ptr<sync_pb::EntitySpecifics> specifics;
    if (batch != nullptr && batch->HasNext()) {
      const syncer::KeyAndData& pair = batch->Next();
      specifics =
          std::make_unique<sync_pb::EntitySpecifics>(pair.second->specifics);
      EXPECT_FALSE(batch->HasNext());
    }
    return specifics;
  }

 private:
  base::test::SingleThreadTaskEnvironment task_environment_;
  testing::NiceMock<MockDataTypeLocalChangeProcessor> mock_processor_;
  std::unique_ptr<ConsentSyncBridgeImpl> bridge_;
};

TEST_F(ConsentSyncBridgeImplTest, ShouldCallModelReadyToSyncOnStartup) {
  EXPECT_CALL(*processor(), ModelReadyToSync(NotNull()));
  WaitUntilModelReadyToSync(kDefaultGaiaId);
}

TEST_F(ConsentSyncBridgeImplTest, ShouldGetDataForCommit) {
  WaitUntilModelReadyToSync(kDefaultGaiaId);
  const UserConsentSpecifics specifics(
      CreateSpecifics(/*client_consent_time_usec=*/1u));
  std::string storage_key;
  EXPECT_CALL(*processor(), Put(_, _, _))
      .WillOnce(WithArg<0>(SaveArg<0>(&storage_key)));
  bridge()->RecordConsent(std::make_unique<UserConsentSpecifics>(specifics));

  // Existing specifics should be returned.
  EXPECT_THAT(GetDataForCommit(storage_key),
              Pointee(MatchesUserConsent(specifics)));
  // GetDataForCommit() should handle arbitrary storage key.
  EXPECT_THAT(GetDataForCommit("bogus"), IsNull());
}

TEST_F(ConsentSyncBridgeImplTest, ShouldRecordSingleConsent) {
  WaitUntilModelReadyToSync(kDefaultGaiaId);
  const UserConsentSpecifics specifics(
      CreateSpecifics(/*client_consent_time_usec=*/1u));
  std::string storage_key;
  EXPECT_CALL(*processor(), Put(_, _, _))
      .WillOnce(WithArg<0>(SaveArg<0>(&storage_key)));
  bridge()->RecordConsent(std::make_unique<UserConsentSpecifics>(specifics));

  EXPECT_THAT(GetDataForCommit(storage_key),
              Pointee(MatchesUserConsent(specifics)));
  EXPECT_THAT(GetAllDataForDebugging(),
              ElementsAre(Pair(storage_key, MatchesUserConsent(specifics))));
}

TEST_F(ConsentSyncBridgeImplTest, ShouldNotDeleteConsentsWhenSyncIsDisabled) {
  WaitUntilModelReadyToSync(kDefaultGaiaId);
  UserConsentSpecifics user_consent_specifics(
      CreateSpecifics(/*client_consent_time_usec=*/2u));
  bridge()->RecordConsent(
      std::make_unique<UserConsentSpecifics>(user_consent_specifics));
  ASSERT_THAT(GetAllDataForDebugging(), SizeIs(1));

  bridge()->ApplyDisableSyncChanges(WriteBatch::CreateMetadataChangeList());
  // The bridge may asynchronously query the store to choose what to delete.
  base::RunLoop().RunUntilIdle();

  // User consent specific must be persisted when sync is disabled.
  EXPECT_THAT(GetAllDataForDebugging(),
              ElementsAre(Pair(_, MatchesUserConsent(user_consent_specifics))));
}

TEST_F(ConsentSyncBridgeImplTest,
       ShouldRecordMultipleConsentsAndDeduplicateByTime) {
  WaitUntilModelReadyToSync(kDefaultGaiaId);
  std::set<std::string> unique_storage_keys;
  EXPECT_CALL(*processor(), Put(_, _, _))
      .Times(4)
      .WillRepeatedly(
          [&unique_storage_keys](const std::string& storage_key,
                                 std::unique_ptr<EntityData> entity_data,
                                 MetadataChangeList* metadata_change_list) {
            unique_storage_keys.insert(storage_key);
          });

  bridge()->RecordConsent(SpecificsUniquePtr(/*client_consent_time_usec=*/1u));
  bridge()->RecordConsent(SpecificsUniquePtr(/*client_consent_time_usec=*/1u));
  bridge()->RecordConsent(SpecificsUniquePtr(/*client_consent_time_usec=*/1u));
  bridge()->RecordConsent(SpecificsUniquePtr(/*client_consent_time_usec=*/2u));

  EXPECT_EQ(2u, unique_storage_keys.size());
  EXPECT_THAT(GetAllDataForDebugging(), SizeIs(2));
}

TEST_F(ConsentSyncBridgeImplTest,
       ShouldDeleteCommitedConsentsAfterApplyIncrementalSyncChanges) {
  WaitUntilModelReadyToSync(kDefaultGaiaId);
  std::string first_storage_key;
  std::string second_storage_key;
  EXPECT_CALL(*processor(), Put(_, _, _))
      .WillOnce(WithArg<0>(SaveArg<0>(&first_storage_key)))
      .WillOnce(WithArg<0>(SaveArg<0>(&second_storage_key)));

  bridge()->RecordConsent(SpecificsUniquePtr(/*client_consent_time_usec=*/1u));
  bridge()->RecordConsent(SpecificsUniquePtr(/*client_consent_time_usec=*/2u));
  ASSERT_THAT(GetAllDataForDebugging(), SizeIs(2));

  syncer::EntityChangeList entity_change_list;
  entity_change_list.push_back(
      EntityChange::CreateDelete(first_storage_key, syncer::EntityData()));
  auto error_on_delete = bridge()->ApplyIncrementalSyncChanges(
      bridge()->CreateMetadataChangeList(), std::move(entity_change_list));
  EXPECT_FALSE(error_on_delete);
  EXPECT_THAT(GetAllDataForDebugging(), SizeIs(1));
  EXPECT_THAT(GetDataForCommit(first_storage_key), IsNull());
  EXPECT_THAT(GetDataForCommit(second_storage_key), NotNull());
}

TEST_F(ConsentSyncBridgeImplTest, ShouldRecordConsentsBeforeSyncEnabled) {
  WaitUntilModelReadyToSync(GaiaId());
  // The consent must be recorded, but not propagated anywhere while the
  // initialization is in progress and sync is still disabled.
  EXPECT_CALL(*processor(), Put(_, _, _)).Times(0);
  bridge()->RecordConsent(SpecificsUniquePtr(/*client_consent_time_usec=*/1u));
  // When sync is enabled, the consent should be reported to the processor.
  ON_CALL(*processor(), IsTrackingMetadata()).WillByDefault(Return(true));
  ON_CALL(*processor(), TrackedGaiaId()).WillByDefault(Return(kDefaultGaiaId));
  EXPECT_CALL(*processor(), Put(_, _, _));
  bridge()->MergeFullSyncData(WriteBatch::CreateMetadataChangeList(),
                              EntityChangeList());
  base::RunLoop().RunUntilIdle();
}

// User consents should be buffered if the store and processor is not fully
// initialized.
TEST_F(ConsentSyncBridgeImplTest,
       ShouldSubmitBufferedConsentsWhenStoreIsInitialized) {
  EXPECT_CALL(*processor(), ModelReadyToSync(_)).Times(0);

  UserConsentSpecifics first_consent =
      CreateSpecifics(/*client_consent_time_usec=*/1u);
  first_consent.set_obfuscated_gaia_id(kDefaultGaiaId.ToString());
  UserConsentSpecifics second_consent =
      CreateSpecifics(/*client_consent_time_usec=*/2u);
  second_consent.set_obfuscated_gaia_id(kDefaultGaiaId.ToString());

  // Record consent before the store is initialized (ModelReadyToSync() not
  // called yet).
  bridge()->RecordConsent(
      std::make_unique<UserConsentSpecifics>(first_consent));

  // Wait until the store is initialized.
  EXPECT_CALL(*processor(), ModelReadyToSync(NotNull()));
  WaitUntilModelReadyToSync(kDefaultGaiaId);

  // Record consent after initializaiton is done.
  bridge()->RecordConsent(
      std::make_unique<UserConsentSpecifics>(second_consent));

  // Both the pre-initialization and post-initialization consents must be
  // handled after initialization as usual.
  EXPECT_THAT(GetAllDataForDebugging(),
              UnorderedElementsAre(Pair(GetStorageKey(first_consent),
                                        MatchesUserConsent(first_consent)),
                                   Pair(GetStorageKey(second_consent),
                                        MatchesUserConsent(second_consent))));
}

TEST_F(ConsentSyncBridgeImplTest,
       ShouldReportPreviouslyPersistedConsentsWhenSyncIsReenabled) {
  WaitUntilModelReadyToSync(kDefaultGaiaId);

  UserConsentSpecifics consent =
      CreateSpecifics(/*client_consent_time_usec=*/1u);
  consent.set_obfuscated_gaia_id(kDefaultGaiaId.ToString());

  bridge()->RecordConsent(std::make_unique<UserConsentSpecifics>(consent));

  // User disables sync, hovewer, the consent hasn't been submitted yet. It is
  // preserved to be submitted when sync is re-enabled.
  bridge()->ApplyDisableSyncChanges(WriteBatch::CreateMetadataChangeList());
  // The bridge may asynchronously query the store to choose what to delete.
  base::RunLoop().RunUntilIdle();

  ASSERT_THAT(GetAllDataForDebugging(), SizeIs(1));

  // Reenable sync.
  EXPECT_CALL(*processor(), Put(GetStorageKey(consent), _, _));
  ON_CALL(*processor(), TrackedGaiaId()).WillByDefault(Return(kDefaultGaiaId));
  bridge()->MergeFullSyncData(WriteBatch::CreateMetadataChangeList(),
                              EntityChangeList());

  // The bridge may asynchronously query the store to choose what to resubmit.
  base::RunLoop().RunUntilIdle();
}

TEST_F(ConsentSyncBridgeImplTest,
       ShouldReportPersistedConsentsOnStartupWithSyncAlreadyEnabled) {
  // Persist a consent while sync is enabled.
  WaitUntilModelReadyToSync(kDefaultGaiaId);
  UserConsentSpecifics consent =
      CreateSpecifics(/*client_consent_time_usec=*/1u);
  consent.set_obfuscated_gaia_id(kDefaultGaiaId.ToString());
  bridge()->RecordConsent(std::make_unique<UserConsentSpecifics>(consent));
  base::RunLoop().RunUntilIdle();
  ASSERT_THAT(GetAllDataForDebugging(), SizeIs(1));

  // Restart the bridge, mimic-ing a browser restart.
  EXPECT_CALL(*processor(), Put(GetStorageKey(consent), _, _));
  ResetBridge();

  // The bridge may asynchronously query the store to choose what to resubmit.
  base::RunLoop().RunUntilIdle();
}

TEST_F(ConsentSyncBridgeImplTest, ShouldReportPersistedConsentsOnSyncEnabled) {
  // Persist a consent before sync is enabled.
  WaitUntilModelReadyToSync(GaiaId());
  UserConsentSpecifics consent =
      CreateSpecifics(/*client_consent_time_usec=*/1u);
  consent.set_obfuscated_gaia_id(kDefaultGaiaId.ToString());
  bridge()->RecordConsent(std::make_unique<UserConsentSpecifics>(consent));
  base::RunLoop().RunUntilIdle();
  ASSERT_THAT(GetAllDataForDebugging(), SizeIs(1));

  // Restart the bridge, mimic-ing a browser restart. We expect no Put()
  // until sync is enabled.
  EXPECT_CALL(*processor(), Put(_, _, _)).Times(0);
  ResetBridge();
  WaitUntilModelReadyToSync(GaiaId());

  // Enable sync.
  EXPECT_CALL(*processor(), Put(GetStorageKey(consent), _, _));
  ON_CALL(*processor(), TrackedGaiaId()).WillByDefault(Return(kDefaultGaiaId));
  bridge()->MergeFullSyncData(WriteBatch::CreateMetadataChangeList(),
                              EntityChangeList());
  base::RunLoop().RunUntilIdle();
}

TEST_F(ConsentSyncBridgeImplTest,
       ShouldResubmitPersistedConsentOnlyIfSameAccount) {
  const GaiaId kFirstGaiaId("first_gaia_id");
  const GaiaId kSecondGaiaId("second_gaia_id");

  WaitUntilModelReadyToSync(kFirstGaiaId);
  UserConsentSpecifics user_consent_specifics(
      CreateSpecifics(/*client_consent_time_usec=*/2u));
  user_consent_specifics.set_obfuscated_gaia_id(kFirstGaiaId.ToString());
  bridge()->RecordConsent(
      std::make_unique<UserConsentSpecifics>(user_consent_specifics));
  ASSERT_THAT(GetAllDataForDebugging(), SizeIs(1));

  bridge()->ApplyDisableSyncChanges(WriteBatch::CreateMetadataChangeList());
  // The bridge may asynchronously query the store to choose what to delete.
  base::RunLoop().RunUntilIdle();

  ASSERT_THAT(GetAllDataForDebugging(),
              ElementsAre(Pair(_, MatchesUserConsent(user_consent_specifics))));

  // A new user signs in and enables sync.
  // The previous account consent should not be resubmited, because the new sync
  // account is different.
  EXPECT_CALL(*processor(), Put(_, _, _)).Times(0);
  ON_CALL(*processor(), TrackedGaiaId()).WillByDefault(Return(kSecondGaiaId));
  bridge()->MergeFullSyncData(WriteBatch::CreateMetadataChangeList(),
                              EntityChangeList());
  base::RunLoop().RunUntilIdle();

  bridge()->ApplyDisableSyncChanges(WriteBatch::CreateMetadataChangeList());
  base::RunLoop().RunUntilIdle();

  // This time their consent should be resubmitted, because it is for the same
  // account.
  EXPECT_CALL(*processor(), Put(GetStorageKey(user_consent_specifics), _, _));
  ON_CALL(*processor(), TrackedGaiaId()).WillByDefault(Return(kFirstGaiaId));
  bridge()->MergeFullSyncData(WriteBatch::CreateMetadataChangeList(),
                              EntityChangeList());
  // The bridge may asynchronously query the store to choose what to resubmit.
  base::RunLoop().RunUntilIdle();
}

}  // namespace

}  // namespace consent_auditor