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.
4 
5 #include "chromeos/network/onc/onc_translator.h"
6 
7 #include <memory>
8 #include <string>
9 #include <utility>
10 
11 #include "base/values.h"
12 #include "chromeos/network/onc/onc_signature.h"
13 #include "chromeos/network/onc/onc_test_utils.h"
14 #include "components/onc/onc_constants.h"
15 #include "testing/gtest/include/gtest/gtest.h"
16 
17 namespace chromeos {
18 namespace onc {
19 
20 // First parameter: Filename of source ONC.
21 // Second parameter: Filename of expected translated Shill json.
22 class ONCTranslatorOncToShillTest
23     : public ::testing::TestWithParam<std::pair<std::string, std::string>> {};
24 
25 // Test the translation from ONC to Shill json.
TEST_P(ONCTranslatorOncToShillTest,Translate)26 TEST_P(ONCTranslatorOncToShillTest, Translate) {
27   std::string source_onc_filename = GetParam().first;
28   std::unique_ptr<const base::DictionaryValue> onc_network(
29       test_utils::ReadTestDictionary(source_onc_filename));
30   std::string result_shill_filename = GetParam().second;
31   std::unique_ptr<const base::DictionaryValue> expected_shill_network(
32       test_utils::ReadTestDictionary(result_shill_filename));
33 
34   std::unique_ptr<base::DictionaryValue> translation(
35       TranslateONCObjectToShill(&kNetworkConfigurationSignature, *onc_network));
36 
37   EXPECT_TRUE(test_utils::Equals(expected_shill_network.get(),
38                                  translation.get()));
39 }
40 
41 // Test different network types, such that each ONC object type is tested at
42 // least once.
43 INSTANTIATE_TEST_SUITE_P(
44     ONCTranslatorOncToShillTest,
45     ONCTranslatorOncToShillTest,
46     ::testing::Values(
47         std::make_pair("ethernet.onc", "shill_ethernet.json"),
48         std::make_pair("ethernet_with_eap_and_cert_pems.onc",
49                        "shill_ethernet_with_eap.json"),
50         std::make_pair("valid_wifi_psk.onc", "shill_wifi_psk.json"),
51         std::make_pair("wifi_clientcert_with_cert_pems.onc",
52                        "shill_wifi_clientcert.json"),
53         std::make_pair("wifi_wep_8021x_clientcert_with_cert_pems.onc",
54                        "shill_wifi_wep_8021x_clientcert.json"),
55         std::make_pair("valid_wifi_clientref.onc", "shill_wifi_clientref.json"),
56         std::make_pair("valid_l2tpipsec.onc", "shill_l2tpipsec.json"),
57         std::make_pair("wifi_dhcp.onc", "shill_wifi_dhcp.json"),
58         std::make_pair("wifi_eap_tls.onc", "shill_wifi_eap_tls.json"),
59         std::make_pair("wifi_eap_ttls.onc", "shill_wifi_eap_ttls.json"),
60         std::make_pair("wifi_proxy.onc", "shill_wifi_proxy.json"),
61         std::make_pair("wifi_proxy_pac.onc", "shill_wifi_proxy_pac.json"),
62         std::make_pair("l2tpipsec_clientcert_with_cert_pems.onc",
63                        "shill_l2tpipsec_clientcert.json"),
64         std::make_pair("valid_openvpn_with_cert_pems.onc",
65                        "shill_openvpn.json"),
66         std::make_pair("openvpn_clientcert_with_cert_pems.onc",
67                        "shill_openvpn_clientcert.json"),
68         std::make_pair("openvpn_clientcert_pkcs11.onc",
69                        "shill_openvpn_clientcert_pkcs11.json"),
70         std::make_pair("openvpn_compression_algorithm_lzo.onc",
71                        "shill_openvpn_compression_algorithm_lzo.json"),
72         std::make_pair("openvpn_compression_algorithm_none.onc",
73                        "shill_openvpn_compression_algorithm_none.json"),
74         std::make_pair("vpn_ipsec_clientcert_pkcs11.onc",
75                        "shill_vpn_ipsec_clientcert_pkcs11.json"),
76         std::make_pair("cellular.onc", "shill_cellular.json"),
77         // WiMAX is deprecated, but we need to ensure older ONC configurations
78         // are handled gracefully.
79         std::make_pair("wimax.onc", "shill_wimax.json"),
80         std::make_pair("third_party_vpn.onc", "shill_third_party_vpn.json"),
81         std::make_pair("arc_vpn.onc", "shill_arc_vpn.json"),
82         std::make_pair("wifi_eap_ttls_with_password_variable.onc",
83                        "shill_wifi_eap_ttls_with_password_variable.json"),
84         std::make_pair("wifi_eap_ttls_with_hardcoded_password.onc",
85                        "shill_wifi_eap_ttls_with_hardcoded_password.json")));
86 
87 // First parameter: Filename of source Shill json.
88 // Second parameter: Filename of expected translated ONC network part.
89 //
90 // Note: This translation direction doesn't have to reconstruct all of the ONC
91 // fields, as Chrome doesn't need all of a Service's properties.
92 class ONCTranslatorShillToOncTest
93     : public ::testing::TestWithParam<std::pair<std::string, std::string>> {};
94 
TEST_P(ONCTranslatorShillToOncTest,Translate)95 TEST_P(ONCTranslatorShillToOncTest, Translate) {
96   std::string source_shill_filename = GetParam().first;
97   std::unique_ptr<const base::DictionaryValue> shill_network(
98       test_utils::ReadTestDictionary(source_shill_filename));
99 
100   std::string result_onc_filename = GetParam().second;
101   std::unique_ptr<base::DictionaryValue> expected_onc_network(
102       test_utils::ReadTestDictionary(result_onc_filename));
103 
104   std::unique_ptr<base::DictionaryValue> translation(
105       TranslateShillServiceToONCPart(*shill_network, ::onc::ONC_SOURCE_NONE,
106                                      &kNetworkWithStateSignature,
107                                      nullptr /* network_state */));
108 
109   EXPECT_TRUE(test_utils::Equals(expected_onc_network.get(),
110                                  translation.get()));
111 }
112 
113 INSTANTIATE_TEST_SUITE_P(
114     ONCTranslatorShillToOncTest,
115     ONCTranslatorShillToOncTest,
116     ::testing::Values(
117         std::make_pair("shill_ethernet.json",
118                        "translation_of_shill_ethernet.onc"),
119         std::make_pair("shill_ethernet_with_eap.json",
120                        "translation_of_shill_ethernet_with_eap.onc"),
121         std::make_pair("shill_ethernet_with_ipconfig.json",
122                        "translation_of_shill_ethernet_with_ipconfig.onc"),
123         std::make_pair("shill_wifi_clientcert.json",
124                        "translation_of_shill_wifi_clientcert.onc"),
125         std::make_pair("shill_wifi_non_utf8_ssid.json",
126                        "translation_of_shill_wifi_non_utf8_ssid.onc"),
127         std::make_pair("shill_wifi_wep_8021x_clientcert.json",
128                        "translation_of_shill_wifi_wep_8021x_clientcert.onc"),
129         std::make_pair("shill_output_l2tpipsec.json",
130                        "translation_of_shill_l2tpipsec.onc"),
131         std::make_pair("shill_output_openvpn.json",
132                        "translation_of_shill_openvpn.onc"),
133         std::make_pair("shill_output_openvpn_with_errors.json",
134                        "translation_of_shill_openvpn_with_errors.onc"),
135         std::make_pair("shill_output_openvpn_clientcert.json",
136                        "translation_of_shill_openvpn_clientcert.onc"),
137         std::make_pair(
138             "shill_output_openvpn_compression_algorithm_lz4_v2.json",
139             "translation_of_shill_openvpn_compression_algorithm_lz4_v2.onc"),
140         std::make_pair("shill_output_vpn_ipsec.json",
141                        "translation_of_shill_vpn_ipsec.onc"),
142         std::make_pair("shill_tether.json", "tether.onc"),
143         std::make_pair("shill_wifi_with_state.json",
144                        "translation_of_shill_wifi_with_state.onc"),
145         std::make_pair("shill_wifi_eap_tls.json",
146                        "translation_of_shill_wifi_eap_tls.onc"),
147         std::make_pair("shill_wifi_eap_ttls.json",
148                        "translation_of_shill_wifi_eap_ttls.onc"),
149         std::make_pair("shill_wifi_proxy.json",
150                        "translation_of_shill_wifi_proxy.onc"),
151         std::make_pair("shill_wifi_proxy_pac.json",
152                        "translation_of_shill_wifi_proxy_pac.onc"),
153         std::make_pair("shill_cellular_with_state.json",
154                        "translation_of_shill_cellular_with_state.onc"),
155         std::make_pair("shill_output_third_party_vpn.json",
156                        "translation_of_shill_output_third_party_vpn.onc"),
157         std::make_pair(
158             "shill_wifi_eap_ttls_with_password_variable.json",
159             "translation_of_shill_wifi_eap_ttls_with_password_variable.onc"),
160         std::make_pair(
161             "shill_wifi_eap_ttls_with_hardcoded_password.json",
162             "translation_of_shill_wifi_eap_ttls_with_hardcoded_password.onc")));
163 
164 }  // namespace onc
165 }  // namespace chromeos
166