1 // Copyright 2015 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 "components/wifi_sync/wifi_credential.h"
7 #include "base/logging.h"
8 #include "base/values.h"
9 #include "components/onc/onc_constants.h"
10 #include "testing/gtest/include/gtest/gtest.h"
16 const char kSsid
[] = "fake-ssid";
17 const char kSsidNonUtf8
[] = "\xc0";
18 const char kPassphraseWep
[] = "abcde";
19 const char kPassphraseWepNonUtf8
[] = "\xc0\xc0\xc0\xc0\xc0";
20 const char kPassphrasePsk
[] = "fake-psk-passphrase";
21 const char kPassphrase8021X
[] = "fake-8021X-passphrase";
23 WifiCredential
MakeCredential(const std::string
& ssid
,
24 WifiSecurityClass security_class
,
25 const std::string
& passphrase
) {
26 scoped_ptr
<WifiCredential
> credential
=
27 WifiCredential::Create(
28 WifiCredential::MakeSsidBytesForTest(ssid
),
35 bool TypeIsWifi(const base::DictionaryValue
& onc_properties
) {
36 std::string network_type
;
37 EXPECT_TRUE(onc_properties
.GetString(
38 onc::toplevel_config::kType
, &network_type
));
39 return network_type
== onc::network_type::kWiFi
;
42 std::string
GetSsid(const base::DictionaryValue
& onc_properties
) {
44 EXPECT_TRUE(onc_properties
.GetString(
45 onc::network_config::WifiProperty(onc::wifi::kSSID
), &ssid
));
49 std::string
GetOncSecurity(const base::DictionaryValue
& onc_properties
) {
50 std::string onc_security
;
51 EXPECT_TRUE(onc_properties
.GetString(
52 onc::network_config::WifiProperty(onc::wifi::kSecurity
), &onc_security
));
56 std::string
GetPassphrase(const base::DictionaryValue
& onc_properties
) {
57 std::string passphrase
;
58 EXPECT_TRUE(onc_properties
.GetString(
59 onc::network_config::WifiProperty(onc::wifi::kPassphrase
), &passphrase
));
65 TEST(WifiCredentialTest
, CreateWithSecurityClassInvalid
) {
66 scoped_ptr
<WifiCredential
> credential
=
67 WifiCredential::Create(
68 WifiCredential::MakeSsidBytesForTest(kSsid
),
69 SECURITY_CLASS_INVALID
,
71 EXPECT_FALSE(credential
);
74 TEST(WifiCredentialTest
, CreateWithPassphraseNonUtf8
) {
75 scoped_ptr
<WifiCredential
> credential
=
76 WifiCredential::Create(
77 WifiCredential::MakeSsidBytesForTest(kSsid
),
79 kPassphraseWepNonUtf8
);
80 EXPECT_FALSE(credential
);
83 TEST(WifiCredentialTest
, ToOncPropertiesSecurityNone
) {
84 const WifiCredential
credential(
85 MakeCredential(kSsid
, SECURITY_CLASS_NONE
, ""));
86 scoped_ptr
<base::DictionaryValue
> onc_properties
=
87 credential
.ToOncProperties();
88 ASSERT_TRUE(onc_properties
);
89 EXPECT_TRUE(TypeIsWifi(*onc_properties
));
90 EXPECT_EQ(kSsid
, GetSsid(*onc_properties
));
91 EXPECT_EQ(onc::wifi::kSecurityNone
, GetOncSecurity(*onc_properties
));
94 TEST(WifiCredentialTest
, ToOncPropertiesSecurityWep
) {
95 const WifiCredential
credential(
96 MakeCredential(kSsid
, SECURITY_CLASS_WEP
, kPassphraseWep
));
97 scoped_ptr
<base::DictionaryValue
> onc_properties
=
98 credential
.ToOncProperties();
99 ASSERT_TRUE(onc_properties
);
100 EXPECT_TRUE(TypeIsWifi(*onc_properties
));
101 EXPECT_EQ(kSsid
, GetSsid(*onc_properties
));
102 EXPECT_EQ(onc::wifi::kWEP_PSK
, GetOncSecurity(*onc_properties
));
103 EXPECT_EQ(kPassphraseWep
, GetPassphrase(*onc_properties
));
106 TEST(WifiCredentialTest
, ToOncPropertiesSecurityPsk
) {
107 const WifiCredential
credential(
108 MakeCredential(kSsid
, SECURITY_CLASS_PSK
, kPassphrasePsk
));
109 scoped_ptr
<base::DictionaryValue
> onc_properties
=
110 credential
.ToOncProperties();
111 ASSERT_TRUE(onc_properties
);
112 EXPECT_TRUE(TypeIsWifi(*onc_properties
));
113 EXPECT_EQ(kSsid
, GetSsid(*onc_properties
));
114 EXPECT_EQ(onc::wifi::kWPA_PSK
, GetOncSecurity(*onc_properties
));
115 EXPECT_EQ(kPassphrasePsk
, GetPassphrase(*onc_properties
));
118 TEST(WifiCredentialTest
, ToOncPropertiesSecurity8021X
) {
119 const WifiCredential
credential(
120 MakeCredential(kSsid
, SECURITY_CLASS_802_1X
, kPassphrase8021X
));
121 scoped_ptr
<base::DictionaryValue
> onc_properties
=
122 credential
.ToOncProperties();
123 ASSERT_TRUE(onc_properties
);
124 EXPECT_TRUE(TypeIsWifi(*onc_properties
));
125 EXPECT_EQ(kSsid
, GetSsid(*onc_properties
));
126 EXPECT_EQ(onc::wifi::kWPA_EAP
, GetOncSecurity(*onc_properties
));
127 EXPECT_EQ(kPassphrase8021X
, GetPassphrase(*onc_properties
));
130 // TODO(quiche): Update this test, once ONC suports non-UTF-8 SSIDs.
132 TEST(WifiCredentialTest
, ToOncPropertiesSsidNonUtf8
) {
133 const WifiCredential
credential(
134 MakeCredential(kSsidNonUtf8
, SECURITY_CLASS_NONE
, ""));
135 scoped_ptr
<base::DictionaryValue
> onc_properties
=
136 credential
.ToOncProperties();
137 EXPECT_FALSE(onc_properties
);
140 } // namespace wifi_sync