1 // Copyright 2014 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.
5 #include "chrome/browser/sync/test/integration/wifi_credentials_helper.h"
7 #include "base/logging.h"
8 #include "base/strings/string_number_conversions.h"
9 #include "chrome/browser/profiles/profile.h"
10 #include "chrome/browser/sync/test/integration/sync_datatype_helper.h"
11 #include "chrome/browser/sync/test/integration/sync_test.h"
12 #include "components/wifi_sync/wifi_credential_syncable_service.h"
13 #include "components/wifi_sync/wifi_credential_syncable_service_factory.h"
15 #if defined(OS_CHROMEOS)
16 #include "chrome/browser/sync/test/integration/wifi_credentials_helper_chromeos.h"
19 using wifi_sync::WifiCredential
;
20 using wifi_sync::WifiCredentialSyncableService
;
21 using wifi_sync::WifiCredentialSyncableServiceFactory
;
22 using wifi_sync::WifiSecurityClass
;
23 using sync_datatype_helper::test
;
25 using WifiCredentialSet
= wifi_sync::WifiCredential::CredentialSet
;
27 namespace wifi_credentials_helper
{
31 void SetupClientForProfile(Profile
* profile
) {
32 #if defined(OS_CHROMEOS)
33 wifi_credentials_helper::chromeos::SetupClientForProfileChromeOs(profile
);
39 WifiCredentialSyncableService
* GetServiceForBrowserContext(
40 content::BrowserContext
* context
) {
41 return WifiCredentialSyncableServiceFactory::GetForBrowserContext(
45 WifiCredentialSyncableService
* GetServiceForProfile(int profile_index
) {
46 return GetServiceForBrowserContext(test()->GetProfile(profile_index
));
49 void AddWifiCredentialToProfile(
50 Profile
* profile
, const WifiCredential
& credential
) {
51 #if defined(OS_CHROMEOS)
52 wifi_credentials_helper::chromeos::AddWifiCredentialToProfileChromeOs(
59 bool CredentialsMatch(const WifiCredentialSet
& a_credentials
,
60 const WifiCredentialSet
& b_credentials
) {
61 if (a_credentials
.size() != b_credentials
.size()) {
62 LOG(ERROR
) << "CredentialSets a and b do not match in size: "
63 << a_credentials
.size()
64 << " vs " << b_credentials
.size() << " respectively.";
68 for (const WifiCredential
& credential
: a_credentials
) {
69 if (b_credentials
.find(credential
) == b_credentials
.end()) {
71 << "Network from a not found in b. "
73 << base::HexEncode(credential
.ssid().data(),
74 credential
.ssid().size()).c_str()
75 << " SecurityClass: " << credential
.security_class()
76 << " Passphrase: " << credential
.passphrase();
87 #if defined(OS_CHROMEOS)
88 wifi_credentials_helper::chromeos::SetUpChromeOs();
95 SetupClientForProfile(test()->verifier());
96 for (int i
= 0; i
< test()->num_clients(); ++i
)
97 SetupClientForProfile(test()->GetProfile(i
));
100 bool VerifierIsEmpty() {
101 return GetWifiCredentialsForProfile(test()->verifier()).empty();
104 bool ProfileMatchesVerifier(int profile_index
) {
105 WifiCredentialSet verifier_credentials
=
106 GetWifiCredentialsForProfile(test()->verifier());
107 WifiCredentialSet other_credentials
=
108 GetWifiCredentialsForProfile(test()->GetProfile(profile_index
));
109 return CredentialsMatch(verifier_credentials
, other_credentials
);
112 bool AllProfilesMatch() {
113 if (test()->use_verifier() && !ProfileMatchesVerifier(0)) {
114 LOG(ERROR
) << "Profile 0 does not match verifier.";
118 WifiCredentialSet profile0_credentials
=
119 GetWifiCredentialsForProfile(test()->GetProfile(0));
120 for (int i
= 1; i
< test()->num_clients(); ++i
) {
121 WifiCredentialSet other_profile_credentials
=
122 GetWifiCredentialsForProfile(test()->GetProfile(i
));
123 if (!CredentialsMatch(profile0_credentials
, other_profile_credentials
)) {
124 LOG(ERROR
) << "Profile " << i
<< " " << "does not match with profile 0.";
131 scoped_ptr
<WifiCredential
> MakeWifiCredential(const std::string
& ssid
,
132 WifiSecurityClass security_class
,
133 const std::string
& passphrase
) {
134 return WifiCredential::Create(WifiCredential::MakeSsidBytesForTest(ssid
),
139 void AddWifiCredential(int profile_index
,
140 const std::string
& sync_id
,
141 const WifiCredential
& credential
) {
142 AddWifiCredentialToProfile(test()->GetProfile(profile_index
), credential
);
143 if (test()->use_verifier())
144 AddWifiCredentialToProfile(test()->verifier(), credential
);
146 // TODO(quiche): Remove this, once we have plumbing to route
147 // NetworkConfigurationObserver events to
148 // WifiCredentialSyncableService instances.
149 GetServiceForProfile(profile_index
)
150 ->AddToSyncedNetworks(sync_id
, credential
);
153 WifiCredentialSet
GetWifiCredentialsForProfile(const Profile
* profile
) {
154 #if defined(OS_CHROMEOS)
155 return wifi_credentials_helper::chromeos::
156 GetWifiCredentialsForProfileChromeOs(profile
);
159 return WifiCredential::MakeSet();
163 } // namespace wifi_credentials_helper