| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/chromeos/cros/network_ui_data.h" | 5 #include "chromeos/network/network_ui_data.h" |
| 6 |
| 7 #include "base/values.h" |
| 8 #include "chromeos/network/onc/onc_test_utils.h" |
| 6 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
| 7 | 10 |
| 8 namespace chromeos { | 11 namespace chromeos { |
| 9 | 12 |
| 10 class NetworkUIDataTest : public testing::Test { | 13 TEST(NetworkUIDataTest, ONCSource) { |
| 11 protected: | |
| 12 NetworkUIDataTest() {} | |
| 13 virtual ~NetworkUIDataTest() {} | |
| 14 | |
| 15 void CheckProperty(const NetworkPropertyUIData& property, | |
| 16 const base::Value* expected_default_value, | |
| 17 bool expected_managed, | |
| 18 bool expected_recommended, | |
| 19 bool expected_editable) { | |
| 20 if (expected_default_value) { | |
| 21 EXPECT_TRUE(base::Value::Equals(expected_default_value, | |
| 22 property.default_value())); | |
| 23 } else { | |
| 24 EXPECT_FALSE(property.default_value()); | |
| 25 } | |
| 26 EXPECT_EQ(expected_managed, property.managed()); | |
| 27 EXPECT_EQ(expected_recommended, property.recommended()); | |
| 28 EXPECT_EQ(expected_editable, property.editable()); | |
| 29 } | |
| 30 }; | |
| 31 | |
| 32 TEST_F(NetworkUIDataTest, ONCSource) { | |
| 33 base::DictionaryValue ui_data_dict; | 14 base::DictionaryValue ui_data_dict; |
| 34 | 15 |
| 35 ui_data_dict.SetString(NetworkUIData::kKeyONCSource, "user_import"); | 16 ui_data_dict.SetString(NetworkUIData::kKeyONCSource, "user_import"); |
| 36 { | 17 { |
| 37 NetworkUIData ui_data(ui_data_dict); | 18 NetworkUIData ui_data(ui_data_dict); |
| 38 EXPECT_EQ(onc::ONC_SOURCE_USER_IMPORT, ui_data.onc_source()); | 19 EXPECT_EQ(onc::ONC_SOURCE_USER_IMPORT, ui_data.onc_source()); |
| 39 EXPECT_FALSE(ui_data.is_managed()); | 20 EXPECT_FALSE(ui_data.is_managed()); |
| 40 } | 21 } |
| 41 | 22 |
| 42 ui_data_dict.SetString(NetworkUIData::kKeyONCSource, "device_policy"); | 23 ui_data_dict.SetString(NetworkUIData::kKeyONCSource, "device_policy"); |
| 43 { | 24 { |
| 44 NetworkUIData ui_data(ui_data_dict); | 25 NetworkUIData ui_data(ui_data_dict); |
| 45 EXPECT_EQ(onc::ONC_SOURCE_DEVICE_POLICY, ui_data.onc_source()); | 26 EXPECT_EQ(onc::ONC_SOURCE_DEVICE_POLICY, ui_data.onc_source()); |
| 46 EXPECT_TRUE(ui_data.is_managed()); | 27 EXPECT_TRUE(ui_data.is_managed()); |
| 47 } | 28 } |
| 48 ui_data_dict.SetString(NetworkUIData::kKeyONCSource, "user_policy"); | 29 ui_data_dict.SetString(NetworkUIData::kKeyONCSource, "user_policy"); |
| 49 { | 30 { |
| 50 NetworkUIData ui_data(ui_data_dict); | 31 NetworkUIData ui_data(ui_data_dict); |
| 51 EXPECT_EQ(onc::ONC_SOURCE_USER_POLICY, ui_data.onc_source()); | 32 EXPECT_EQ(onc::ONC_SOURCE_USER_POLICY, ui_data.onc_source()); |
| 52 EXPECT_TRUE(ui_data.is_managed()); | 33 EXPECT_TRUE(ui_data.is_managed()); |
| 53 } | 34 } |
| 54 } | 35 } |
| 55 | 36 |
| 56 TEST_F(NetworkUIDataTest, CertificateType) { | 37 TEST(NetworkUIDataTest, CertificateType) { |
| 57 { | 38 { |
| 58 base::DictionaryValue ui_data_dict; | 39 base::DictionaryValue ui_data_dict; |
| 59 ui_data_dict.SetString(NetworkUIData::kKeyCertificateType, "none"); | 40 ui_data_dict.SetString(NetworkUIData::kKeyCertificateType, "none"); |
| 60 NetworkUIData ui_data(ui_data_dict); | 41 NetworkUIData ui_data(ui_data_dict); |
| 61 EXPECT_EQ(CLIENT_CERT_TYPE_NONE, ui_data.certificate_type()); | 42 EXPECT_EQ(CLIENT_CERT_TYPE_NONE, ui_data.certificate_type()); |
| 62 } | 43 } |
| 63 { | 44 { |
| 64 base::DictionaryValue ui_data_dict; | 45 base::DictionaryValue ui_data_dict; |
| 65 ui_data_dict.SetString(NetworkUIData::kKeyCertificateType, "ref"); | 46 ui_data_dict.SetString(NetworkUIData::kKeyCertificateType, "ref"); |
| 66 NetworkUIData ui_data(ui_data_dict); | 47 NetworkUIData ui_data(ui_data_dict); |
| 67 EXPECT_EQ(CLIENT_CERT_TYPE_REF, ui_data.certificate_type()); | 48 EXPECT_EQ(CLIENT_CERT_TYPE_REF, ui_data.certificate_type()); |
| 68 } | 49 } |
| 69 { | 50 { |
| 70 // for type pattern we need to have some kind of pattern | 51 // for type pattern we need to have some kind of pattern |
| 71 std::string organization("Little If Any, Inc."); | 52 std::string organization("Little If Any, Inc."); |
| 72 base::DictionaryValue ui_data_dict; | 53 base::DictionaryValue ui_data_dict; |
| 73 base::DictionaryValue* pattern_dict = new base::DictionaryValue; | 54 base::DictionaryValue* pattern_dict = new base::DictionaryValue; |
| 74 base::DictionaryValue* issuer_dict = new base::DictionaryValue; | 55 base::DictionaryValue* issuer_dict = new base::DictionaryValue; |
| 75 issuer_dict->SetString("Organization", organization); | 56 issuer_dict->SetString("Organization", organization); |
| 76 pattern_dict->Set("Issuer", issuer_dict); | 57 pattern_dict->Set("Issuer", issuer_dict); |
| 77 ui_data_dict.Set("certificate_pattern", pattern_dict); | 58 ui_data_dict.Set("certificate_pattern", pattern_dict); |
| 78 ui_data_dict.SetString(NetworkUIData::kKeyCertificateType, "pattern"); | 59 ui_data_dict.SetString(NetworkUIData::kKeyCertificateType, "pattern"); |
| 79 NetworkUIData ui_data(ui_data_dict); | 60 NetworkUIData ui_data(ui_data_dict); |
| 80 EXPECT_EQ(CLIENT_CERT_TYPE_PATTERN, ui_data.certificate_type()); | 61 EXPECT_EQ(CLIENT_CERT_TYPE_PATTERN, ui_data.certificate_type()); |
| 81 } | 62 } |
| 82 } | 63 } |
| 83 | 64 |
| 84 TEST_F(NetworkUIDataTest, CertificatePattern) { | 65 TEST(NetworkUIDataTest, CertificatePattern) { |
| 85 std::string organization("Little If Any, Inc."); | 66 std::string organization("Little If Any, Inc."); |
| 86 base::DictionaryValue ui_data_dict; | 67 base::DictionaryValue ui_data_dict; |
| 87 base::DictionaryValue* pattern_dict = new base::DictionaryValue; | 68 base::DictionaryValue* pattern_dict = new base::DictionaryValue; |
| 88 base::DictionaryValue* issuer_dict = new base::DictionaryValue; | 69 base::DictionaryValue* issuer_dict = new base::DictionaryValue; |
| 89 issuer_dict->SetString("Organization", organization); | 70 issuer_dict->SetString("Organization", organization); |
| 90 pattern_dict->Set("Issuer", issuer_dict); | 71 pattern_dict->Set("Issuer", issuer_dict); |
| 91 ui_data_dict.Set("certificate_pattern", pattern_dict); | 72 ui_data_dict.Set("certificate_pattern", pattern_dict); |
| 92 NetworkUIData ui_data(ui_data_dict); | 73 NetworkUIData ui_data(ui_data_dict); |
| 93 EXPECT_FALSE(ui_data.certificate_pattern().Empty()); | 74 EXPECT_FALSE(ui_data.certificate_pattern().Empty()); |
| 94 EXPECT_EQ(organization, | 75 EXPECT_EQ(organization, |
| 95 ui_data.certificate_pattern().issuer().organization()); | 76 ui_data.certificate_pattern().issuer().organization()); |
| 96 } | 77 } |
| 97 | 78 |
| 98 TEST_F(NetworkUIDataTest, PropertyInit) { | 79 class CreateUIDataTest |
| 99 NetworkPropertyUIData empty_prop; | 80 : public ::testing::TestWithParam<std::pair<std::string, std::string> > { |
| 100 CheckProperty(empty_prop, NULL, false, false, true); | 81 }; |
| 101 | 82 |
| 102 NetworkUIData empty_data; | 83 TEST_P(CreateUIDataTest, CreateUIDataFromONC) { |
| 103 NetworkPropertyUIData null_prop(empty_data); | 84 namespace test_utils = onc::test_utils; |
| 104 CheckProperty(null_prop, NULL, false, false, true); | 85 scoped_ptr<base::DictionaryValue> onc_network = |
| 86 test_utils::ReadTestDictionary(GetParam().first); |
| 105 | 87 |
| 106 base::DictionaryValue empty_dict; | 88 scoped_ptr<base::DictionaryValue> expected_uidata = |
| 107 NetworkUIData empty_data_2(empty_dict); | 89 test_utils::ReadTestDictionary(GetParam().second); |
| 108 NetworkPropertyUIData empty_dict_prop(empty_data_2); | |
| 109 CheckProperty(empty_dict_prop, NULL, false, false, true); | |
| 110 | 90 |
| 91 scoped_ptr<NetworkUIData> actual_uidata = |
| 92 CreateUIDataFromONC(onc::ONC_SOURCE_USER_POLICY, *onc_network); |
| 93 EXPECT_TRUE(actual_uidata != NULL); |
| 94 |
| 95 base::DictionaryValue actual_uidata_dict; |
| 96 actual_uidata->FillDictionary(&actual_uidata_dict); |
| 97 EXPECT_TRUE(test_utils::Equals(&actual_uidata_dict, expected_uidata.get())); |
| 111 } | 98 } |
| 112 | 99 |
| 113 TEST_F(NetworkUIDataTest, ParseOncProperty) { | 100 INSTANTIATE_TEST_CASE_P( |
| 114 base::DictionaryValue ui_data_dict; | 101 CreateUIDataTest, |
| 115 NetworkUIData ui_data; | 102 CreateUIDataTest, |
| 116 | 103 ::testing::Values( |
| 117 base::DictionaryValue onc; | 104 std::make_pair("valid_wifi_clientcert.onc", |
| 118 | 105 "uidata_for_wifi_clientcert.json"), |
| 119 base::StringValue val_a("a"); | 106 std::make_pair("valid_wifi_clientref.onc", |
| 120 base::StringValue val_b("b"); | 107 "uidata_for_wifi_clientref.json"), |
| 121 base::StringValue val_a_a("a_a"); | 108 std::make_pair("valid_wifi_psk.onc", |
| 122 base::StringValue val_a_b("a_b"); | 109 "uidata_for_wifi_psk.json"), |
| 123 | 110 std::make_pair("valid_openvpn_clientcert.onc", |
| 124 onc.Set("a", val_a.DeepCopy()); | 111 "uidata_for_openvpn_clientcert.json"), |
| 125 onc.Set("b", val_b.DeepCopy()); | 112 std::make_pair("valid_l2tpipsec_clientcert.onc", |
| 126 onc.Set("a.a", val_a_a.DeepCopy()); | 113 "uidata_for_l2tpipsec_clientcert.json"))); |
| 127 onc.Set("a.b", val_a_b.DeepCopy()); | |
| 128 base::ListValue recommended; | |
| 129 recommended.Append(new base::StringValue("b")); | |
| 130 recommended.Append(new base::StringValue("c")); | |
| 131 recommended.Append(new base::StringValue("a.a")); | |
| 132 onc.Set("Recommended", recommended.DeepCopy()); | |
| 133 onc.Set("a.Recommended", recommended.DeepCopy()); | |
| 134 | |
| 135 NetworkPropertyUIData prop; | |
| 136 | |
| 137 ui_data.set_onc_source(onc::ONC_SOURCE_USER_IMPORT); | |
| 138 ui_data.FillDictionary(&ui_data_dict); | |
| 139 | |
| 140 NetworkUIData empty_data; | |
| 141 prop.ParseOncProperty(empty_data, &onc, "a"); | |
| 142 CheckProperty(prop, NULL, false, false, true); | |
| 143 | |
| 144 prop.ParseOncProperty(ui_data, &onc, "a"); | |
| 145 CheckProperty(prop, NULL, false, false, true); | |
| 146 | |
| 147 prop.ParseOncProperty(ui_data, &onc, "a.b"); | |
| 148 CheckProperty(prop, NULL, false, false, true); | |
| 149 | |
| 150 prop.ParseOncProperty(ui_data, &onc, "c"); | |
| 151 CheckProperty(prop, NULL, false, false, true); | |
| 152 | |
| 153 ui_data.set_onc_source(onc::ONC_SOURCE_USER_POLICY); | |
| 154 ui_data.FillDictionary(&ui_data_dict); | |
| 155 | |
| 156 prop.ParseOncProperty(ui_data, &onc, "a"); | |
| 157 CheckProperty(prop, NULL, true, false, false); | |
| 158 | |
| 159 prop.ParseOncProperty(ui_data, &onc, "b"); | |
| 160 CheckProperty(prop, &val_b, false, true, true); | |
| 161 | |
| 162 prop.ParseOncProperty(ui_data, &onc, "c"); | |
| 163 CheckProperty(prop, NULL, false, false, true); | |
| 164 | |
| 165 prop.ParseOncProperty(ui_data, &onc, "d"); | |
| 166 CheckProperty(prop, NULL, true, false, false); | |
| 167 | |
| 168 prop.ParseOncProperty(ui_data, &onc, "a.a"); | |
| 169 CheckProperty(prop, NULL, true, false, false); | |
| 170 | |
| 171 prop.ParseOncProperty(ui_data, &onc, "a.b"); | |
| 172 CheckProperty(prop, &val_a_b, false, true, true); | |
| 173 | |
| 174 prop.ParseOncProperty(ui_data, &onc, "a.c"); | |
| 175 CheckProperty(prop, NULL, false, false, true); | |
| 176 | |
| 177 prop.ParseOncProperty(ui_data, &onc, "a.d"); | |
| 178 CheckProperty(prop, NULL, true, false, false); | |
| 179 | |
| 180 prop.ParseOncProperty(ui_data, NULL, "a.e"); | |
| 181 CheckProperty(prop, NULL, true, false, false); | |
| 182 } | |
| 183 | 114 |
| 184 } // namespace chromeos | 115 } // namespace chromeos |
| OLD | NEW |