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/network_settings/onc_validator.h" | 5 #include "chrome/browser/chromeos/network_settings/onc_validator.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
| 9 #include "base/logging.h" |
9 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
10 #include "base/values.h" | 11 #include "base/values.h" |
11 #include "chrome/browser/chromeos/cros/onc_constants.h" | 12 #include "chrome/browser/chromeos/cros/onc_constants.h" |
12 #include "chrome/browser/chromeos/network_settings/onc_signature.h" | 13 #include "chrome/browser/chromeos/network_settings/onc_signature.h" |
13 #include "chrome/browser/chromeos/network_settings/onc_test_utils.h" | 14 #include "chrome/browser/chromeos/network_settings/onc_test_utils.h" |
| 15 #include "chrome/browser/chromeos/network_settings/onc_utils.h" |
14 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
15 | 17 |
16 namespace chromeos { | 18 namespace chromeos { |
17 namespace onc { | 19 namespace onc { |
18 namespace { | 20 namespace { |
19 | 21 |
20 // Create a strict validator that complains about every error. | 22 // Create a strict validator that complains about every error. |
21 scoped_ptr<Validator> CreateStrictValidator(bool managed_onc) { | 23 scoped_ptr<Validator> CreateStrictValidator(bool managed_onc) { |
22 return make_scoped_ptr(new Validator(true, true, true, managed_onc)); | 24 return make_scoped_ptr(new Validator(true, true, true, managed_onc)); |
23 } | 25 } |
24 | 26 |
25 // Create a liberal validator that ignores or repairs non-critical errors. | 27 // Create a liberal validator that ignores or repairs non-critical errors. |
26 scoped_ptr<Validator> CreateLiberalValidator(bool managed_onc) { | 28 scoped_ptr<Validator> CreateLiberalValidator(bool managed_onc) { |
27 return make_scoped_ptr(new Validator(false, false, false, managed_onc)); | 29 return make_scoped_ptr(new Validator(false, false, false, managed_onc)); |
28 } | 30 } |
29 } // namespace | 31 } // namespace |
30 | 32 |
| 33 TEST(ONCValidatorValidTest, EmptyUnencryptedConfiguration) { |
| 34 scoped_ptr<Validator> validator(CreateStrictValidator(true)); |
| 35 std::string error; |
| 36 scoped_ptr<const base::DictionaryValue> original( |
| 37 ReadDictionaryFromJson(kEmptyUnencryptedConfiguration, &error)); |
| 38 |
| 39 std::string messages; |
| 40 scoped_ptr<const base::DictionaryValue> repaired( |
| 41 validator->ValidateAndRepairObject(&kToplevelConfigurationSignature, |
| 42 *original, &messages)); |
| 43 EXPECT_TRUE(messages.empty()); |
| 44 EXPECT_TRUE(test_utils::Equals(original.get(), repaired.get())) << messages; |
| 45 } |
| 46 |
31 // This test case is about validating valid ONC objects. | 47 // This test case is about validating valid ONC objects. |
32 TEST(ONCValidatorValidTest, ValidPolicyOnc) { | 48 class ONCValidatorValidTest |
| 49 : public ::testing::TestWithParam< |
| 50 std::pair<std::string, |
| 51 const onc::OncValueSignature*> > { |
| 52 protected: |
| 53 std::string GetFilename() const { |
| 54 return GetParam().first; |
| 55 } |
| 56 |
| 57 const onc::OncValueSignature* GetSignature() const { |
| 58 return GetParam().second; |
| 59 } |
| 60 }; |
| 61 |
| 62 TEST_P(ONCValidatorValidTest, ValidPolicyOnc) { |
33 scoped_ptr<Validator> validator(CreateStrictValidator(true)); | 63 scoped_ptr<Validator> validator(CreateStrictValidator(true)); |
34 scoped_ptr<const base::DictionaryValue> network; | 64 scoped_ptr<const base::DictionaryValue> original( |
35 scoped_ptr<base::DictionaryValue> repaired; | 65 test_utils::ReadTestDictionary(GetFilename())); |
36 | 66 |
37 network = test_utils::ReadTestDictionary("policy.onc"); | 67 std::string messages; |
38 repaired = validator->ValidateAndRepairObject( | 68 scoped_ptr<const base::DictionaryValue> repaired( |
39 &kNetworkConfigurationSignature, | 69 validator->ValidateAndRepairObject(GetSignature(), *original, &messages)); |
40 *network); | 70 EXPECT_TRUE(messages.empty()); |
41 EXPECT_TRUE(test_utils::Equals(network.get(), repaired.get())); | 71 EXPECT_TRUE(test_utils::Equals(original.get(), repaired.get())) << messages; |
| 72 } |
42 | 73 |
43 network = test_utils::ReadTestDictionary("valid.onc"); | 74 INSTANTIATE_TEST_CASE_P( |
44 repaired = validator->ValidateAndRepairObject( | 75 ONCValidatorValidTest, |
45 &kNetworkConfigurationSignature, | 76 ONCValidatorValidTest, |
46 *network); | 77 ::testing::Values(std::make_pair("managed_vpn.onc", |
47 EXPECT_TRUE(test_utils::Equals(network.get(), repaired.get())); | 78 &kNetworkConfigurationSignature), |
48 } | 79 std::make_pair("managed_toplevel.onc", |
| 80 &kToplevelConfigurationSignature), |
| 81 std::make_pair("managed_ethernet.onc", |
| 82 &kNetworkConfigurationSignature), |
| 83 // Ignore recommended arrays in unmanaged ONC: |
| 84 std::make_pair("recommended_in_unmanaged.onc", |
| 85 &kNetworkConfigurationSignature))); |
49 | 86 |
50 // Validate invalid ONC objects and check the resulting repaired object. This | 87 // Validate invalid ONC objects and check the resulting repaired object. This |
51 // test fixture loads a test json file into |invalid_| containing several test | 88 // test fixture loads a test json file into |invalid_| containing several test |
52 // objects which can be accessed by their path. The test boolean parameter | 89 // objects which can be accessed by their path. The test boolean parameter |
53 // determines wether to use the strict or the liberal validator. | 90 // determines wether to use the strict or the liberal validator. |
54 class ONCValidatorInvalidTest : public ::testing::TestWithParam<bool> { | 91 class ONCValidatorInvalidTest : public ::testing::TestWithParam<bool> { |
55 public: | 92 public: |
56 // Validate the entry at |path_to_object| with the given | 93 // Validate the entry at |path_to_object| with the given |
57 // |signature|. Depending on |managed_onc| the object is interpreted as a | 94 // |signature|. Depending on |managed_onc| the object is interpreted as a |
58 // managed onc (with recommended fields) or not. The resulting repaired object | 95 // managed onc (with recommended fields) or not. The resulting repaired object |
59 // must match the entry at |path_to_repaired| if the liberal validator is | 96 // must match the entry at |path_to_repaired| if the liberal validator is |
60 // used. | 97 // used. |
61 void ValidateInvalid(const std::string& path_to_object, | 98 void ValidateInvalid(const std::string& path_to_object, |
62 const std::string& path_to_repaired, | 99 const std::string& path_to_repaired, |
63 const OncValueSignature* signature, | 100 const OncValueSignature* signature, |
64 bool managed_onc) { | 101 bool managed_onc) { |
65 scoped_ptr<Validator> validator; | 102 scoped_ptr<Validator> validator; |
66 if (GetParam()) | 103 if (GetParam()) |
67 validator = CreateStrictValidator(managed_onc); | 104 validator = CreateStrictValidator(managed_onc); |
68 else | 105 else |
69 validator = CreateLiberalValidator(managed_onc); | 106 validator = CreateLiberalValidator(managed_onc); |
70 | 107 |
71 const base::DictionaryValue* object = NULL; | 108 const base::DictionaryValue* object = NULL; |
72 ASSERT_TRUE(invalid_->GetDictionary(path_to_object, &object)); | 109 ASSERT_TRUE(invalid_->GetDictionary(path_to_object, &object)); |
73 | 110 |
| 111 std::string messages; |
74 scoped_ptr<base::DictionaryValue> actual_repaired = | 112 scoped_ptr<base::DictionaryValue> actual_repaired = |
75 validator->ValidateAndRepairObject(signature, *object); | 113 validator->ValidateAndRepairObject(signature, *object, &messages); |
| 114 EXPECT_FALSE(messages.empty()); |
| 115 DVLOG(1) << messages; |
76 if (GetParam() || path_to_repaired == "") { | 116 if (GetParam() || path_to_repaired == "") { |
77 EXPECT_EQ(NULL, actual_repaired.get()); | 117 EXPECT_EQ(NULL, actual_repaired.get()); |
78 } else { | 118 } else { |
79 const base::DictionaryValue* expected_repaired = NULL; | 119 const base::DictionaryValue* expected_repaired = NULL; |
80 invalid_->GetDictionary(path_to_repaired, &expected_repaired); | 120 invalid_->GetDictionary(path_to_repaired, &expected_repaired); |
81 EXPECT_TRUE(test_utils::Equals(expected_repaired, actual_repaired.get())); | 121 EXPECT_TRUE(test_utils::Equals(expected_repaired, actual_repaired.get())); |
82 } | 122 } |
83 } | 123 } |
84 | 124 |
85 virtual void SetUp() { | 125 virtual void SetUp() { |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
134 "network-repaired", | 174 "network-repaired", |
135 &kNetworkConfigurationSignature, false); | 175 &kNetworkConfigurationSignature, false); |
136 } | 176 } |
137 | 177 |
138 INSTANTIATE_TEST_CASE_P(ONCValidatorInvalidTest, | 178 INSTANTIATE_TEST_CASE_P(ONCValidatorInvalidTest, |
139 ONCValidatorInvalidTest, | 179 ONCValidatorInvalidTest, |
140 ::testing::Bool()); | 180 ::testing::Bool()); |
141 | 181 |
142 } // namespace onc | 182 } // namespace onc |
143 } // namespace chromeos | 183 } // namespace chromeos |
OLD | NEW |