File: search_engines_helper.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 (335 lines) | stat: -rw-r--r-- 11,608 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
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
// Copyright 2012 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/sync/test/integration/search_engines_helper.h"

#include <stddef.h>

#include <vector>

#include "base/functional/bind.h"
#include "base/hash/sha1.h"
#include "base/strings/strcat.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "base/time/time.h"
#include "base/uuid.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/search_engines/template_url_service_factory.h"
#include "chrome/browser/sync/test/integration/sync_datatype_helper.h"
#include "chrome/browser/sync/test/integration/sync_test.h"
#include "components/search_engines/template_url.h"
#include "components/sync/base/data_type.h"
#include "testing/gmock/include/gmock/gmock.h"

namespace {

using sync_datatype_helper::test;

using ::testing::Contains;
using ::testing::Key;
using ::testing::Not;

GUIDToTURLMap CreateGUIDToTURLMap(TemplateURLService* service) {
  GUIDToTURLMap map;
  for (TemplateURL* turl : service->GetTemplateURLs()) {
    EXPECT_THAT(map, Not(Contains(Key(turl->sync_guid()))))
        << "Found two template URLs with same GUID";
    map[turl->sync_guid()] = turl;
  }
  return map;
}

std::string GetTURLInfoString(const TemplateURL& turl) {
  return "TemplateURL: shortname: " + base::UTF16ToUTF8(turl.short_name()) +
         " keyword: " + base::UTF16ToUTF8(turl.keyword()) +
         " url: " + turl.url();
}

bool TURLsMatch(const TemplateURL& turl1, const TemplateURL& turl2) {
  bool result = (turl1.url() == turl2.url()) &&
                (turl1.keyword() == turl2.keyword()) &&
                (turl1.short_name() == turl2.short_name());

  // Print some useful debug info.
  if (!result) {
    DVLOG(1) << "TemplateURLs did not match: " << GetTURLInfoString(turl1)
             << " vs " << GetTURLInfoString(turl2);
  }

  return result;
}

bool ServicesMatch(int profile_a, int profile_b, std::ostream* os) {
  TemplateURLService* service_a =
      search_engines_helper::GetServiceForBrowserContext(profile_a);
  TemplateURLService* service_b =
      search_engines_helper::GetServiceForBrowserContext(profile_b);

  // Services that have synced should have identical TURLs, including the GUIDs.
  // Make sure we compare those fields in addition to the user-visible fields.
  GUIDToTURLMap a_turls = CreateGUIDToTURLMap(service_a);
  GUIDToTURLMap b_turls = CreateGUIDToTURLMap(service_b);

  if (a_turls.size() != b_turls.size()) {
    *os << "Service a and b do not match in size: " << a_turls.size() << " vs "
        << b_turls.size() << " respectively.";
    return false;
  }

  for (const auto& [guid, a_turl] : a_turls) {
    if (b_turls.find(guid) == b_turls.end()) {
      *os << "TURL GUID from a not found in b's TURLs: " << guid;
      return false;
    }
    if (!TURLsMatch(*b_turls[guid], *a_turl)) {
      return false;
    }
  }

  const TemplateURL* default_a = service_a->GetDefaultSearchProvider();
  const TemplateURL* default_b = service_b->GetDefaultSearchProvider();
  if (!TURLsMatch(*default_a, *default_b)) {
    *os << "Default search providers do not match: A's default: "
        << default_a->keyword() << " B's default: " << default_b->keyword();
    return false;
  } else {
    *os << "A had default with URL: " << default_a->url()
        << " and keyword: " << default_a->keyword();
  }

  return true;
}

}  // namespace

namespace search_engines_helper {

TemplateURLService* GetServiceForBrowserContext(int profile_index) {
  return TemplateURLServiceFactory::GetForProfile(
      test()->GetProfile(profile_index));
}

TemplateURLService* GetVerifierService() {
  return TemplateURLServiceFactory::GetForProfile(test()->verifier());
}

bool ServiceMatchesVerifier(int profile_index) {
  TemplateURLService* verifier = GetVerifierService();
  TemplateURLService* other = GetServiceForBrowserContext(profile_index);

  TemplateURLService::TemplateURLVector verifier_turls =
      verifier->GetTemplateURLs();
  if (verifier_turls.size() != other->GetTemplateURLs().size()) {
    DVLOG(1) << "Verifier and other service have a different count of TURLs: "
             << verifier_turls.size() << " vs "
             << other->GetTemplateURLs().size() << " respectively.";
    return false;
  }

  for (TemplateURL* verifier_turl : verifier_turls) {
    const TemplateURL* other_turl =
        other->GetTemplateURLForKeyword(verifier_turl->keyword());

    if (!other_turl) {
      DVLOG(1) << "The other service did not contain a TURL with keyword: "
               << verifier_turl->keyword();
      return false;
    }
    if (!TURLsMatch(*verifier_turl, *other_turl)) {
      return false;
    }
  }

  return true;
}

bool AllServicesMatch() {
  return AllServicesMatch(&VLOG_STREAM(1));
}

bool AllServicesMatch(std::ostream* os) {
  // Use 0 as the baseline.
  if (test()->UseVerifier() && !ServiceMatchesVerifier(0)) {
    *os << "TemplateURLService 0 does not match verifier.";
    return false;
  }

  for (int it = 1; it < test()->num_clients(); ++it) {
    if (!ServicesMatch(0, it, os)) {
      *os << "TemplateURLService " << it << " does not match with "
          << "service 0.";
      return false;
    }
  }
  return true;
}

TemplateURLBuilder::TemplateURLBuilder(const std::string& keyword) {
  data_.SetShortName(base::UTF8ToUTF16(keyword));
  data_.SetKeyword(base::UTF8ToUTF16(keyword));
  data_.SetURL(base::StringPrintf("http://www.test-%s.com/", keyword.c_str()));
  data_.favicon_url = GURL("http://favicon.url");
  data_.safe_for_autoreplace = false;
  data_.is_active = TemplateURLData::ActiveStatus::kTrue;
  data_.date_created = base::Time::FromTimeT(100);
  data_.last_modified = base::Time::FromTimeT(100);
  data_.prepopulate_id = 999999;

  // Produce a GUID deterministically from |keyword|.
  std::string hex_encoded_hash =
      base::HexEncode(base::SHA1Hash(base::as_byte_span(keyword)));
  hex_encoded_hash.resize(12);
  data_.sync_guid =
      base::StrCat({"12345678-0000-4000-8000-", hex_encoded_hash});
  DCHECK(base::Uuid::ParseCaseInsensitive(data_.sync_guid).is_valid());
}

TemplateURLBuilder::~TemplateURLBuilder() = default;

std::unique_ptr<TemplateURL> TemplateURLBuilder::Build() {
  return std::make_unique<TemplateURL>(data_);
}

void AddSearchEngine(int profile_index, const std::string& keyword) {
  Profile* profile = test()->GetProfile(profile_index);
  TemplateURLBuilder builder(keyword);
  TemplateURLServiceFactory::GetForProfile(profile)->Add(builder.Build());
  if (test()->UseVerifier()) {
    GetVerifierService()->Add(builder.Build());
  }
}

void EditSearchEngine(int profile_index,
                      const std::string& keyword,
                      const std::u16string& short_name,
                      const std::string& new_keyword,
                      const std::string& url) {
  ASSERT_FALSE(url.empty());
  TemplateURLService* service = GetServiceForBrowserContext(profile_index);
  TemplateURL* turl =
      service->GetTemplateURLForKeyword(base::UTF8ToUTF16(keyword));
  EXPECT_TRUE(turl);
  ASSERT_FALSE(new_keyword.empty());
  service->ResetTemplateURL(turl, short_name, base::UTF8ToUTF16(new_keyword),
                            url);
  // Make sure we do the same on the verifier.
  if (test()->UseVerifier()) {
    TemplateURL* verifier_turl = GetVerifierService()->GetTemplateURLForKeyword(
        base::UTF8ToUTF16(keyword));
    EXPECT_TRUE(verifier_turl);
    GetVerifierService()->ResetTemplateURL(verifier_turl, short_name,
                                           base::UTF8ToUTF16(new_keyword), url);
  }
}

void DeleteSearchEngine(int profile_index, const std::string& keyword) {
  TemplateURLService* service = GetServiceForBrowserContext(profile_index);
  TemplateURL* turl =
      service->GetTemplateURLForKeyword(base::UTF8ToUTF16(keyword));
  EXPECT_TRUE(turl);
  service->Remove(turl);
  // Make sure we do the same on the verifier.
  if (test()->UseVerifier()) {
    TemplateURL* verifier_turl = GetVerifierService()->GetTemplateURLForKeyword(
        base::UTF8ToUTF16(keyword));
    EXPECT_TRUE(verifier_turl);
    GetVerifierService()->Remove(verifier_turl);
  }
}

void ChangeDefaultSearchProvider(int profile_index,
                                 const std::string& keyword) {
  TemplateURLService* service = GetServiceForBrowserContext(profile_index);
  TemplateURL* turl =
      service->GetTemplateURLForKeyword(base::UTF8ToUTF16(keyword));
  ASSERT_TRUE(turl);
  service->SetUserSelectedDefaultSearchProvider(turl);
  if (test()->UseVerifier()) {
    TemplateURL* verifier_turl = GetVerifierService()->GetTemplateURLForKeyword(
        base::UTF8ToUTF16(keyword));
    ASSERT_TRUE(verifier_turl);
    GetVerifierService()->SetUserSelectedDefaultSearchProvider(verifier_turl);
  }
}

bool HasSearchEngine(int profile_index, const std::string& keyword) {
  TemplateURLService* service = GetServiceForBrowserContext(profile_index);
  TemplateURL* turl =
      service->GetTemplateURLForKeyword(base::UTF8ToUTF16(keyword));
  return turl != nullptr;
}

std::string GetDefaultSearchEngineKeyword(int profile_index) {
  TemplateURLService* service = GetServiceForBrowserContext(profile_index);
  return base::UTF16ToUTF8(service->GetDefaultSearchProvider()->keyword());
}

bool HasSearchEngineInFakeServer(const std::string& keyword,
                                 fake_server::FakeServer* fake_server) {
  return GetSearchEngineInFakeServerWithKeyword(keyword, fake_server)
      .has_value();
}

std::optional<sync_pb::SearchEngineSpecifics>
GetSearchEngineInFakeServerWithKeyword(const std::string& keyword,
                                       fake_server::FakeServer* fake_server) {
  for (const sync_pb::SyncEntity& entity :
       fake_server->GetSyncEntitiesByDataType(syncer::SEARCH_ENGINES)) {
    if (entity.specifics().search_engine().keyword() == keyword) {
      return entity.specifics().search_engine();
    }
  }
  return std::nullopt;
}

SearchEnginesMatchChecker::SearchEnginesMatchChecker() {
  if (test()->UseVerifier()) {
    observations_.AddObservation(GetVerifierService());
  }

  for (int i = 0; i < test()->num_clients(); ++i) {
    observations_.AddObservation(GetServiceForBrowserContext(i));
  }
}

SearchEnginesMatchChecker::~SearchEnginesMatchChecker() = default;

bool SearchEnginesMatchChecker::IsExitConditionSatisfied(std::ostream* os) {
  return AllServicesMatch(os);
}

void SearchEnginesMatchChecker::OnTemplateURLServiceChanged() {
  CheckExitCondition();
}

HasSearchEngineChecker::HasSearchEngineChecker(int profile_index,
                                               const std::string& keyword)
    : service_(GetServiceForBrowserContext(profile_index)),
      keyword_(base::UTF8ToUTF16(keyword)) {
  observations_.AddObservation(service_.get());
}

HasSearchEngineChecker::~HasSearchEngineChecker() = default;

bool HasSearchEngineChecker::IsExitConditionSatisfied(std::ostream* os) {
  return service_->GetTemplateURLForKeyword(keyword_) != nullptr;
}

void HasSearchEngineChecker::OnTemplateURLServiceChanged() {
  CheckExitCondition();
}

FakeServerHasSearchEngineChecker::FakeServerHasSearchEngineChecker(
    const std::string& keyword)
    : keyword_(keyword) {}

bool FakeServerHasSearchEngineChecker::IsExitConditionSatisfied(
    std::ostream* os) {
  return HasSearchEngineInFakeServer(keyword_, fake_server());
}

}  // namespace search_engines_helper