Home | History | Annotate | Download | only in onc
      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 "base/memory/scoped_ptr.h"
      8 #include "base/values.h"
      9 #include "chromeos/network/onc/onc_signature.h"
     10 #include "chromeos/network/onc/onc_test_utils.h"
     11 #include "components/onc/onc_constants.h"
     12 #include "testing/gtest/include/gtest/gtest.h"
     13 
     14 namespace chromeos {
     15 namespace onc {
     16 
     17 // First parameter: Filename of source ONC.
     18 // Second parameter: Filename of expected translated Shill json.
     19 class ONCTranslatorOncToShillTest
     20     : public ::testing::TestWithParam<std::pair<std::string, std::string> > {
     21 };
     22 
     23 // Test the translation from ONC to Shill json.
     24 TEST_P(ONCTranslatorOncToShillTest, Translate) {
     25   std::string source_onc_filename = GetParam().first;
     26   scoped_ptr<const base::DictionaryValue> onc_network(
     27       test_utils::ReadTestDictionary(source_onc_filename));
     28   std::string result_shill_filename = GetParam().second;
     29   scoped_ptr<const base::DictionaryValue> expected_shill_network(
     30       test_utils::ReadTestDictionary(result_shill_filename));
     31 
     32   scoped_ptr<base::DictionaryValue> translation(TranslateONCObjectToShill(
     33       &kNetworkConfigurationSignature, *onc_network));
     34 
     35   EXPECT_TRUE(test_utils::Equals(expected_shill_network.get(),
     36                                  translation.get()));
     37 }
     38 
     39 // Test different network types, such that each ONC object type is tested at
     40 // least once.
     41 INSTANTIATE_TEST_CASE_P(
     42     ONCTranslatorOncToShillTest,
     43     ONCTranslatorOncToShillTest,
     44     ::testing::Values(
     45         std::make_pair("ethernet.onc", "shill_ethernet.json"),
     46         std::make_pair("ethernet_with_eap_and_cert_pems.onc",
     47                        "shill_ethernet_with_eap.json"),
     48         std::make_pair("valid_wifi_psk.onc", "shill_wifi_psk.json"),
     49         std::make_pair("wifi_clientcert_with_cert_pems.onc",
     50                        "shill_wifi_clientcert.json"),
     51         std::make_pair("valid_wifi_clientref.onc", "shill_wifi_clientref.json"),
     52         std::make_pair("valid_l2tpipsec.onc", "shill_l2tpipsec.json"),
     53         std::make_pair("l2tpipsec_clientcert_with_cert_pems.onc",
     54                        "shill_l2tpipsec_clientcert.json"),
     55         std::make_pair("valid_openvpn_with_cert_pems.onc",
     56                        "shill_openvpn.json"),
     57         std::make_pair("openvpn_clientcert_with_cert_pems.onc",
     58                        "shill_openvpn_clientcert.json"),
     59         std::make_pair("cellular.onc", "shill_cellular.json"),
     60         std::make_pair("wimax.onc", "shill_wimax.json")));
     61 
     62 // First parameter: Filename of source Shill json.
     63 // Second parameter: Filename of expected translated ONC network part.
     64 //
     65 // Note: This translation direction doesn't have to reconstruct all of the ONC
     66 // fields, as Chrome doesn't need all of a Service's properties.
     67 class ONCTranslatorShillToOncTest
     68     : public ::testing::TestWithParam<std::pair<std::string, std::string> > {
     69 };
     70 
     71 TEST_P(ONCTranslatorShillToOncTest, Translate) {
     72   std::string source_shill_filename = GetParam().first;
     73   scoped_ptr<const base::DictionaryValue> shill_network(
     74       test_utils::ReadTestDictionary(source_shill_filename));
     75 
     76   std::string result_onc_filename = GetParam().second;
     77   scoped_ptr<base::DictionaryValue> expected_onc_network(
     78       test_utils::ReadTestDictionary(result_onc_filename));
     79 
     80   scoped_ptr<base::DictionaryValue> translation(TranslateShillServiceToONCPart(
     81       *shill_network, ::onc::ONC_SOURCE_NONE, &kNetworkWithStateSignature));
     82 
     83   EXPECT_TRUE(test_utils::Equals(expected_onc_network.get(),
     84                                  translation.get()));
     85 }
     86 
     87 INSTANTIATE_TEST_CASE_P(
     88     ONCTranslatorShillToOncTest,
     89     ONCTranslatorShillToOncTest,
     90     ::testing::Values(
     91         std::make_pair("shill_ethernet.json",
     92                        "translation_of_shill_ethernet.onc"),
     93         std::make_pair("shill_ethernet_with_eap.json",
     94                        "translation_of_shill_ethernet_with_eap.onc"),
     95         std::make_pair("shill_ethernet_with_ipconfig.json",
     96                        "translation_of_shill_ethernet_with_ipconfig.onc"),
     97         std::make_pair("shill_wifi_clientcert.json",
     98                        "translation_of_shill_wifi_clientcert.onc"),
     99         std::make_pair("shill_wifi_wpa1.json",
    100                        "translation_of_shill_wifi_wpa1.onc"),
    101         std::make_pair("shill_output_l2tpipsec.json",
    102                        "translation_of_shill_l2tpipsec.onc"),
    103         std::make_pair("shill_output_openvpn.json",
    104                        "translation_of_shill_openvpn.onc"),
    105         std::make_pair("shill_output_openvpn_with_errors.json",
    106                        "translation_of_shill_openvpn_with_errors.onc"),
    107         std::make_pair("shill_wifi_with_state.json",
    108                        "translation_of_shill_wifi_with_state.onc"),
    109         std::make_pair("shill_cellular_with_state.json",
    110                        "translation_of_shill_cellular_with_state.onc"),
    111         std::make_pair("shill_wimax_with_state.json",
    112                        "translation_of_shill_wimax_with_state.onc")));
    113 
    114 }  // namespace onc
    115 }  // namespace chromeos
    116