File: profile_auth_servers_sync_bridge_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 (265 lines) | stat: -rw-r--r-- 9,748 bytes parent folder | download | duplicates (6)
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
// Copyright 2022 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/ash/printing/oauth2/profile_auth_servers_sync_bridge.h"

#include <memory>
#include <set>
#include <string>
#include <vector>

#include "base/run_loop.h"
#include "base/test/bind.h"
#include "base/test/gmock_move_support.h"
#include "base/test/mock_callback.h"
#include "base/test/task_environment.h"
#include "components/sync/model/data_batch.h"
#include "components/sync/model/data_type_store.h"
#include "components/sync/model/entity_change.h"
#include "components/sync/model/in_memory_metadata_change_list.h"
#include "components/sync/protocol/entity_data.h"
#include "components/sync/test/data_type_store_test_util.h"
#include "components/sync/test/mock_data_type_local_change_processor.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"

namespace ash::printing::oauth2 {
namespace {

syncer::EntityData ToEntityData(const std::string& uri) {
  syncer::EntityData entity_data;
  entity_data.specifics.mutable_printers_authorization_server()->set_uri(uri);
  entity_data.name = uri;
  return entity_data;
}

class MockProfileAuthServersSyncBridgeObserver
    : public ProfileAuthServersSyncBridge::Observer {
 public:
  MOCK_METHOD(void, OnProfileAuthorizationServersInitialized, (), (override));
  MOCK_METHOD(void,
              OnProfileAuthorizationServersUpdate,
              (std::set<GURL>, std::set<GURL>),
              (override));
};

class PrintingOAuth2ProfileAuthServersSyncBridgeTest : public testing::Test {
 protected:
  PrintingOAuth2ProfileAuthServersSyncBridgeTest() {
    DCHECK(uri_1u_.is_valid());
    DCHECK(uri_2u_.is_valid());
    DCHECK(uri_3u_.is_valid());
    DCHECK(uri_4u_.is_valid());
  }

  void CreateBridge(const std::vector<std::string>& uris = {}) {
    ON_CALL(mock_processor_, IsTrackingMetadata())
        .WillByDefault(testing::Return(false));
    SaveToLocalStore(uris);
    bridge_ = ProfileAuthServersSyncBridge::CreateForTesting(
        &mock_observer_, mock_processor_.CreateForwardingProcessor(),
        syncer::DataTypeStoreTestUtil::FactoryForForwardingStore(store_.get()));
    base::RunLoop loop;
    EXPECT_CALL(mock_observer_, OnProfileAuthorizationServersInitialized())
        .WillOnce([&loop]() { loop.Quit(); });
    loop.Run();
  }

  void DoInitialMerge(const std::vector<std::string>& records) {
    syncer::EntityChangeList data_change_list;
    for (const std::string& uri : records) {
      data_change_list.push_back(
          syncer::EntityChange::CreateAdd(uri, ToEntityData(uri)));
    }
    ON_CALL(mock_processor_, IsTrackingMetadata())
        .WillByDefault(testing::Return(true));
    std::optional<syncer::ModelError> error = bridge_->MergeFullSyncData(
        bridge_->CreateMetadataChangeList(), std::move(data_change_list));
    ASSERT_FALSE(error);
  }

  void DoApplyIncrementalSyncChanges(const std::vector<std::string>& added,
                                     const std::vector<std::string>& deleted) {
    syncer::EntityChangeList data_change_list;
    for (const std::string& uri : added) {
      data_change_list.push_back(
          syncer::EntityChange::CreateAdd(uri, ToEntityData(uri)));
    }
    for (const std::string& uri : deleted) {
      data_change_list.push_back(
          syncer::EntityChange::CreateDelete(uri, syncer::EntityData()));
    }
    std::optional<syncer::ModelError> error =
        bridge_->ApplyIncrementalSyncChanges(
            bridge_->CreateMetadataChangeList(), std::move(data_change_list));
    ASSERT_FALSE(error);
  }

  std::vector<std::string> GetAllData() {
    std::unique_ptr<syncer::DataBatch> output =
        bridge_->GetAllDataForDebugging();

    std::vector<std::string> uris;
    while (output->HasNext()) {
      auto [key, data] = output->Next();
      uris.emplace_back(key);
    }
    return uris;
  }

  // Example URIs for testing.
  const std::string uri_1_ = "https://a.b.c/123";
  const std::string uri_2_ = "https://def:123/gh";
  const std::string uri_3_ = "https://xyz/ab";
  const std::string uri_4_ = "https://ala.ma.kota/psa?moze";
  const GURL uri_1u_ = GURL(uri_1_);
  const GURL uri_2u_ = GURL(uri_2_);
  const GURL uri_3u_ = GURL(uri_3_);
  const GURL uri_4u_ = GURL(uri_4_);

  testing::StrictMock<MockProfileAuthServersSyncBridgeObserver> mock_observer_;
  testing::NiceMock<syncer::MockDataTypeLocalChangeProcessor> mock_processor_;
  std::unique_ptr<ProfileAuthServersSyncBridge> bridge_;

 private:
  void SaveToLocalStore(const std::vector<std::string>& uris) {
    std::unique_ptr<syncer::DataTypeStore::WriteBatch> batch =
        store_->CreateWriteBatch();
    for (const std::string& uri : uris) {
      sync_pb::PrintersAuthorizationServerSpecifics specifics;
      specifics.set_uri(uri);
      batch->WriteData(uri, specifics.SerializeAsString());
    }

    base::RunLoop loop;
    store_->CommitWriteBatch(
        std::move(batch),
        base::BindLambdaForTesting(
            [&loop](const std::optional<syncer::ModelError>& error) {
              DCHECK(!error);
              loop.Quit();
            }));
    loop.Run();
  }

  // In memory data type store needs to be able to post tasks.
  base::test::SingleThreadTaskEnvironment task_environment_;
  std::unique_ptr<syncer::DataTypeStore> store_ =
      syncer::DataTypeStoreTestUtil::CreateInMemoryStoreForTest();
};

TEST_F(PrintingOAuth2ProfileAuthServersSyncBridgeTest, Initialization) {
  EXPECT_CALL(mock_processor_, ModelReadyToSync(testing::_));
  CreateBridge();
}

TEST_F(PrintingOAuth2ProfileAuthServersSyncBridgeTest, MergeFullSyncData) {
  EXPECT_CALL(mock_observer_,
              OnProfileAuthorizationServersUpdate(std::set{uri_1u_, uri_3u_},
                                                  std::set<GURL>{}));
  CreateBridge({uri_1_, uri_3_});

  EXPECT_CALL(mock_processor_, Put(uri_3_, testing::_, testing::_));
  EXPECT_CALL(mock_observer_, OnProfileAuthorizationServersUpdate(
                                  std::set{uri_2u_}, std::set<GURL>{}));
  DoInitialMerge({uri_1_, uri_2_});

  const std::vector<std::string> uris = GetAllData();
  EXPECT_EQ(uris, (std::vector{uri_1_, uri_2_, uri_3_}));
}

TEST_F(PrintingOAuth2ProfileAuthServersSyncBridgeTest,
       ServersAddedWhenSyncDisabled) {
  CreateBridge();
  bridge_->AddAuthorizationServer(uri_1u_);

  const std::vector<std::string> uris = GetAllData();
  EXPECT_EQ(uris, std::vector{uri_1_});
}

TEST_F(PrintingOAuth2ProfileAuthServersSyncBridgeTest,
       ServersAddedAfterInitialMerge) {
  CreateBridge();
  EXPECT_CALL(mock_observer_, OnProfileAuthorizationServersUpdate(
                                  std::set{uri_2u_}, std::set<GURL>{}));
  DoInitialMerge({uri_2_});
  EXPECT_CALL(mock_processor_, Put(uri_1_, testing::_, testing::_));
  bridge_->AddAuthorizationServer(uri_1u_);

  const std::vector<std::string> uris = GetAllData();
  EXPECT_EQ(uris, (std::vector{uri_1_, uri_2_}));
}

TEST_F(PrintingOAuth2ProfileAuthServersSyncBridgeTest,
       ServersAddedBeforeInitialMerge) {
  CreateBridge();
  bridge_->AddAuthorizationServer(uri_1u_);
  EXPECT_CALL(mock_processor_, Put(uri_1_, testing::_, testing::_));
  EXPECT_CALL(mock_observer_, OnProfileAuthorizationServersUpdate(
                                  std::set{uri_2u_}, std::set<GURL>{}));
  DoInitialMerge({uri_2_});

  const std::vector<std::string> uris = GetAllData();
  EXPECT_EQ(uris, (std::vector{uri_1_, uri_2_}));
}

TEST_F(PrintingOAuth2ProfileAuthServersSyncBridgeTest,
       ApplyIncrementalSyncChanges) {
  CreateBridge();
  DoInitialMerge({});

  EXPECT_CALL(mock_observer_,
              OnProfileAuthorizationServersUpdate(std::set{uri_1u_, uri_2u_},
                                                  std::set<GURL>{}));
  DoApplyIncrementalSyncChanges(/*added=*/{uri_1_, uri_2_}, /*deleted=*/{});
  std::vector<std::string> uris = GetAllData();
  EXPECT_EQ(uris, (std::vector{uri_1_, uri_2_}));

  EXPECT_CALL(mock_observer_, OnProfileAuthorizationServersUpdate(
                                  std::set{uri_3u_}, std::set{uri_1u_}));
  DoApplyIncrementalSyncChanges(/*added=*/{uri_3_}, /*deleted=*/{uri_1_});
  uris = GetAllData();
  EXPECT_EQ(uris, (std::vector{uri_2_, uri_3_}));
}

TEST_F(PrintingOAuth2ProfileAuthServersSyncBridgeTest, GetDataForCommit) {
  CreateBridge();
  EXPECT_CALL(mock_observer_,
              OnProfileAuthorizationServersUpdate(std::set{uri_1u_, uri_2u_},
                                                  std::set<GURL>{}));
  DoInitialMerge({uri_1_, uri_2_});

  std::unique_ptr<syncer::DataBatch> output =
      bridge_->GetDataForCommit({uri_1_, uri_3_});

  ASSERT_TRUE(output);
  std::vector<syncer::KeyAndData> data;
  while (output->HasNext()) {
    data.push_back(output->Next());
  }
  ASSERT_EQ(data.size(), 1u);
  EXPECT_EQ(data[0].first, uri_1_);
  ASSERT_TRUE(data[0].second);
  EXPECT_EQ(
      data[0].second->specifics.mutable_printers_authorization_server()->uri(),
      uri_1_);
}

TEST_F(PrintingOAuth2ProfileAuthServersSyncBridgeTest,
       OnProfileAuthorizationServersUpdate) {
  CreateBridge();
  EXPECT_CALL(mock_observer_,
              OnProfileAuthorizationServersUpdate(std::set{uri_1u_, uri_2u_},
                                                  std::set<GURL>{}));
  DoInitialMerge({uri_1_, uri_2_});

  EXPECT_CALL(mock_observer_, OnProfileAuthorizationServersUpdate(
                                  std::set{uri_3u_}, std::set{uri_2u_}));
  DoApplyIncrementalSyncChanges(/*added=*/{uri_1_, uri_3_},
                                /*deleted=*/{uri_2_, uri_4_});
}

}  // namespace
}  // namespace ash::printing::oauth2