1 // Copyright (c) 2012 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 "chromeos/network/onc/onc_utils.h"
9 #include "base/json/json_file_value_serializer.h"
10 #include "base/logging.h"
11 #include "base/strings/string_number_conversions.h"
12 #include "base/values.h"
13 #include "chromeos/chromeos_test_utils.h"
14 #include "chromeos/network/network_ui_data.h"
15 #include "chromeos/network/onc/onc_signature.h"
16 #include "chromeos/network/onc/onc_test_utils.h"
17 #include "testing/gtest/include/gtest/gtest.h"
23 scoped_ptr
<base::Value
> ReadTestJson(const std::string
& filename
) {
24 base::Value
* result
= nullptr;
26 if (!test_utils::GetTestDataPath("network", filename
, &path
)) {
27 NOTREACHED() << "Unable to get test file path for: " << filename
;
28 return make_scoped_ptr(result
);
30 JSONFileValueDeserializer
deserializer(path
);
31 deserializer
.set_allow_trailing_comma(true);
32 std::string error_message
;
33 result
= deserializer
.Deserialize(nullptr, &error_message
);
34 CHECK(result
!= nullptr) << "Couldn't json-deserialize file: " << filename
35 << ": " << error_message
;
36 return make_scoped_ptr(result
);
43 TEST(ONCDecrypterTest
, BrokenEncryptionIterations
) {
44 scoped_ptr
<base::DictionaryValue
> encrypted_onc
=
45 test_utils::ReadTestDictionary("broken-encrypted-iterations.onc");
47 scoped_ptr
<base::DictionaryValue
> decrypted_onc
=
48 Decrypt("test0000", *encrypted_onc
);
50 EXPECT_EQ(NULL
, decrypted_onc
.get());
53 TEST(ONCDecrypterTest
, BrokenEncryptionZeroIterations
) {
54 scoped_ptr
<base::DictionaryValue
> encrypted_onc
=
55 test_utils::ReadTestDictionary("broken-encrypted-zero-iterations.onc");
58 scoped_ptr
<base::DictionaryValue
> decrypted_onc
=
59 Decrypt("test0000", *encrypted_onc
);
61 EXPECT_EQ(NULL
, decrypted_onc
.get());
64 TEST(ONCDecrypterTest
, LoadEncryptedOnc
) {
65 scoped_ptr
<base::DictionaryValue
> encrypted_onc
=
66 test_utils::ReadTestDictionary("encrypted.onc");
67 scoped_ptr
<base::DictionaryValue
> expected_decrypted_onc
=
68 test_utils::ReadTestDictionary("decrypted.onc");
71 scoped_ptr
<base::DictionaryValue
> actual_decrypted_onc
=
72 Decrypt("test0000", *encrypted_onc
);
74 base::DictionaryValue emptyDict
;
75 EXPECT_TRUE(test_utils::Equals(expected_decrypted_onc
.get(),
76 actual_decrypted_onc
.get()));
81 const char* kLoginId
= "hans";
82 const char* kLoginEmail
= "hans@my.domain.com";
84 class StringSubstitutionStub
: public StringSubstitution
{
86 StringSubstitutionStub() {}
87 bool GetSubstitute(const std::string
& placeholder
,
88 std::string
* substitute
) const override
{
89 if (placeholder
== ::onc::substitutes::kLoginIDField
)
90 *substitute
= kLoginId
;
91 else if (placeholder
==::onc::substitutes::kEmailField
)
92 *substitute
= kLoginEmail
;
98 DISALLOW_COPY_AND_ASSIGN(StringSubstitutionStub
);
103 TEST(ONCStringExpansion
, OpenVPN
) {
104 scoped_ptr
<base::DictionaryValue
> vpn_onc
=
105 test_utils::ReadTestDictionary("valid_openvpn.onc");
107 StringSubstitutionStub substitution
;
108 ExpandStringsInOncObject(kNetworkConfigurationSignature
, substitution
,
111 std::string actual_expanded
;
112 vpn_onc
->GetString("VPN.OpenVPN.Username", &actual_expanded
);
113 EXPECT_EQ(actual_expanded
, std::string("abc ") + kLoginEmail
+ " def");
116 TEST(ONCStringExpansion
, WiFi_EAP
) {
117 scoped_ptr
<base::DictionaryValue
> wifi_onc
=
118 test_utils::ReadTestDictionary("wifi_clientcert_with_cert_pems.onc");
120 StringSubstitutionStub substitution
;
121 ExpandStringsInOncObject(kNetworkConfigurationSignature
, substitution
,
124 std::string actual_expanded
;
125 wifi_onc
->GetString("WiFi.EAP.Identity", &actual_expanded
);
126 EXPECT_EQ(actual_expanded
, std::string("abc ") + kLoginId
+ "@my.domain.com");
129 TEST(ONCResolveServerCertRefs
, ResolveServerCertRefs
) {
130 scoped_ptr
<base::DictionaryValue
> test_cases
=
131 test_utils::ReadTestDictionary(
132 "network_configs_with_resolved_certs.json");
134 CertPEMsByGUIDMap certs
;
135 certs
["cert_google"] = "pem_google";
136 certs
["cert_webkit"] = "pem_webkit";
138 for (base::DictionaryValue::Iterator
it(*test_cases
);
139 !it
.IsAtEnd(); it
.Advance()) {
140 SCOPED_TRACE("Test case: " + it
.key());
142 const base::DictionaryValue
* test_case
= NULL
;
143 it
.value().GetAsDictionary(&test_case
);
145 const base::ListValue
* networks_with_cert_refs
= NULL
;
146 test_case
->GetList("WithCertRefs", &networks_with_cert_refs
);
148 const base::ListValue
* expected_resolved_onc
= NULL
;
149 test_case
->GetList("WithResolvedRefs", &expected_resolved_onc
);
151 bool expected_success
= (networks_with_cert_refs
->GetSize() ==
152 expected_resolved_onc
->GetSize());
154 scoped_ptr
<base::ListValue
> actual_resolved_onc(
155 networks_with_cert_refs
->DeepCopy());
157 bool success
= ResolveServerCertRefsInNetworks(certs
,
158 actual_resolved_onc
.get());
159 EXPECT_EQ(expected_success
, success
);
160 EXPECT_TRUE(test_utils::Equals(expected_resolved_onc
,
161 actual_resolved_onc
.get()));
165 TEST(ONCUtils
, ProxySettingsToProxyConfig
) {
166 scoped_ptr
<base::Value
> test_data(ReadTestJson("proxy_config.json"));
168 base::ListValue
* list_of_tests
;
169 test_data
->GetAsList(&list_of_tests
);
170 ASSERT_TRUE(list_of_tests
);
173 for (base::ListValue::iterator it
= list_of_tests
->begin();
174 it
!= list_of_tests
->end(); ++it
, ++index
) {
175 SCOPED_TRACE("Test case #" + base::IntToString(index
));
177 base::DictionaryValue
* test_case
;
178 (*it
)->GetAsDictionary(&test_case
);
180 base::DictionaryValue
* onc_proxy_settings
;
181 test_case
->GetDictionary("ONC_ProxySettings", &onc_proxy_settings
);
183 base::DictionaryValue
* expected_proxy_config
;
184 test_case
->GetDictionary("ProxyConfig", &expected_proxy_config
);
186 scoped_ptr
<base::DictionaryValue
> actual_proxy_config
=
187 ConvertOncProxySettingsToProxyConfig(*onc_proxy_settings
);
189 test_utils::Equals(expected_proxy_config
, actual_proxy_config
.get()));
193 TEST(ONCUtils
, ProxyConfigToOncProxySettings
) {
194 scoped_ptr
<base::Value
> test_data(ReadTestJson("proxy_config.json"));
196 base::ListValue
* list_of_tests
;
197 test_data
->GetAsList(&list_of_tests
);
198 ASSERT_TRUE(list_of_tests
);
201 for (base::ListValue::iterator it
= list_of_tests
->begin();
202 it
!= list_of_tests
->end(); ++it
, ++index
) {
203 SCOPED_TRACE("Test case #" + base::IntToString(index
));
205 base::DictionaryValue
* test_case
;
206 (*it
)->GetAsDictionary(&test_case
);
208 base::DictionaryValue
* shill_proxy_config
;
209 test_case
->GetDictionary("ProxyConfig", &shill_proxy_config
);
211 base::DictionaryValue
* onc_proxy_settings
;
212 test_case
->GetDictionary("ONC_ProxySettings", &onc_proxy_settings
);
214 scoped_ptr
<base::DictionaryValue
> actual_proxy_settings
=
215 ConvertProxyConfigToOncProxySettings(*shill_proxy_config
);
217 test_utils::Equals(onc_proxy_settings
, actual_proxy_settings
.get()));
222 } // namespace chromeos