summaryrefslogtreecommitdiffstats
path: root/chromeos/network/onc/onc_translator_unittest.cc
blob: 7de265aee2c36749f435178fde6db3e3a54ad3d6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chromeos/network/onc/onc_translator.h"

#include <string>
#include <utility>

#include "base/memory/scoped_ptr.h"
#include "base/values.h"
#include "chromeos/network/onc/onc_signature.h"
#include "chromeos/network/onc/onc_test_utils.h"
#include "components/onc/onc_constants.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace chromeos {
namespace onc {

// First parameter: Filename of source ONC.
// Second parameter: Filename of expected translated Shill json.
class ONCTranslatorOncToShillTest
    : public ::testing::TestWithParam<std::pair<std::string, std::string> > {
};

// Test the translation from ONC to Shill json.
TEST_P(ONCTranslatorOncToShillTest, Translate) {
  std::string source_onc_filename = GetParam().first;
  scoped_ptr<const base::DictionaryValue> onc_network(
      test_utils::ReadTestDictionary(source_onc_filename));
  std::string result_shill_filename = GetParam().second;
  scoped_ptr<const base::DictionaryValue> expected_shill_network(
      test_utils::ReadTestDictionary(result_shill_filename));

  scoped_ptr<base::DictionaryValue> translation(TranslateONCObjectToShill(
      &kNetworkConfigurationSignature, *onc_network));

  EXPECT_TRUE(test_utils::Equals(expected_shill_network.get(),
                                 translation.get()));
}

// Test different network types, such that each ONC object type is tested at
// least once.
INSTANTIATE_TEST_CASE_P(
    ONCTranslatorOncToShillTest,
    ONCTranslatorOncToShillTest,
    ::testing::Values(
        std::make_pair("ethernet.onc", "shill_ethernet.json"),
        std::make_pair("ethernet_with_eap_and_cert_pems.onc",
                       "shill_ethernet_with_eap.json"),
        std::make_pair("valid_wifi_psk.onc", "shill_wifi_psk.json"),
        std::make_pair("wifi_clientcert_with_cert_pems.onc",
                       "shill_wifi_clientcert.json"),
        std::make_pair("wifi_wep_8021x_clientcert_with_cert_pems.onc",
                       "shill_wifi_wep_8021x_clientcert.json"),
        std::make_pair("valid_wifi_clientref.onc", "shill_wifi_clientref.json"),
        std::make_pair("valid_l2tpipsec.onc", "shill_l2tpipsec.json"),
        std::make_pair("wifi_dhcp.onc", "shill_wifi_dhcp.json"),
        std::make_pair("wifi_proxy.onc", "shill_wifi_proxy.json"),
        std::make_pair("wifi_proxy_pac.onc", "shill_wifi_proxy_pac.json"),
        std::make_pair("l2tpipsec_clientcert_with_cert_pems.onc",
                       "shill_l2tpipsec_clientcert.json"),
        std::make_pair("valid_openvpn_with_cert_pems.onc",
                       "shill_openvpn.json"),
        std::make_pair("openvpn_clientcert_with_cert_pems.onc",
                       "shill_openvpn_clientcert.json"),
        std::make_pair("cellular.onc", "shill_cellular.json"),
        std::make_pair("wimax.onc", "shill_wimax.json"),
        std::make_pair("third_party_vpn.onc", "shill_third_party_vpn.json")));

// First parameter: Filename of source Shill json.
// Second parameter: Filename of expected translated ONC network part.
//
// Note: This translation direction doesn't have to reconstruct all of the ONC
// fields, as Chrome doesn't need all of a Service's properties.
class ONCTranslatorShillToOncTest
    : public ::testing::TestWithParam<std::pair<std::string, std::string> > {
};

TEST_P(ONCTranslatorShillToOncTest, Translate) {
  std::string source_shill_filename = GetParam().first;
  scoped_ptr<const base::DictionaryValue> shill_network(
      test_utils::ReadTestDictionary(source_shill_filename));

  std::string result_onc_filename = GetParam().second;
  scoped_ptr<base::DictionaryValue> expected_onc_network(
      test_utils::ReadTestDictionary(result_onc_filename));

  scoped_ptr<base::DictionaryValue> translation(TranslateShillServiceToONCPart(
      *shill_network, ::onc::ONC_SOURCE_NONE, &kNetworkWithStateSignature,
      nullptr /* network_state */));

  EXPECT_TRUE(test_utils::Equals(expected_onc_network.get(),
                                 translation.get()));
}

INSTANTIATE_TEST_CASE_P(
    ONCTranslatorShillToOncTest,
    ONCTranslatorShillToOncTest,
    ::testing::Values(
        std::make_pair("shill_ethernet.json",
                       "translation_of_shill_ethernet.onc"),
        std::make_pair("shill_ethernet_with_eap.json",
                       "translation_of_shill_ethernet_with_eap.onc"),
        std::make_pair("shill_ethernet_with_ipconfig.json",
                       "translation_of_shill_ethernet_with_ipconfig.onc"),
        std::make_pair("shill_wifi_clientcert.json",
                       "translation_of_shill_wifi_clientcert.onc"),
        std::make_pair("shill_wifi_non_utf8_ssid.json",
                       "translation_of_shill_wifi_non_utf8_ssid.onc"),
        std::make_pair("shill_wifi_wep_8021x_clientcert.json",
                       "translation_of_shill_wifi_wep_8021x_clientcert.onc"),
        std::make_pair("shill_output_l2tpipsec.json",
                       "translation_of_shill_l2tpipsec.onc"),
        std::make_pair("shill_output_openvpn.json",
                       "translation_of_shill_openvpn.onc"),
        std::make_pair("shill_output_openvpn_with_errors.json",
                       "translation_of_shill_openvpn_with_errors.onc"),
        std::make_pair("shill_wifi_with_state.json",
                       "translation_of_shill_wifi_with_state.onc"),
        std::make_pair("shill_wifi_proxy.json",
                       "translation_of_shill_wifi_proxy.onc"),
        std::make_pair("shill_wifi_proxy_pac.json",
                       "translation_of_shill_wifi_proxy_pac.onc"),
        std::make_pair("shill_cellular_with_state.json",
                       "translation_of_shill_cellular_with_state.onc"),
        std::make_pair("shill_wimax_with_state.json",
                       "translation_of_shill_wimax_with_state.onc"),
        std::make_pair("shill_output_third_party_vpn.json",
                       "third_party_vpn.onc")));

}  // namespace onc
}  // namespace chromeos