Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(767)

Side by Side Diff: chrome/browser/chromeos/network_settings/onc_validator_unittest.cc

Issue 10944009: Implementation of ONC signature, validator and normalizer. (Closed) Base URL: http://git.chromium.org/chromium/src.git@gperffix
Patch Set: Completed validator for complete ONC. Integrated into OncNetworkParser. Created 8 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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 "chrome/browser/chromeos/network_settings/onc_validator.h"
6
7 #include <string>
8
9 #include "base/memory/scoped_ptr.h"
10 #include "base/values.h"
11 #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_test_utils.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15
16 namespace chromeos {
17 namespace onc {
18
19 // This test case is about validating valid ONC objects.
20 TEST(ONCValidatorValidTest, ValidPolicyOnc) {
21 scoped_ptr<Validator> validator(Validator::CreateStrictValidator(true));
22 scoped_ptr<const base::DictionaryValue> network;
23 scoped_ptr<base::DictionaryValue> repaired;
24
25 network = test_utils::ReadTestDictionary("policy.onc");
26 repaired = validator->ValidateAndRepairObject(
27 &kNetworkConfigurationSignature,
28 *network);
29 EXPECT_TRUE(test_utils::Equals(network.get(), repaired.get()));
30
31 network = test_utils::ReadTestDictionary("valid.onc");
32 repaired = validator->ValidateAndRepairObject(
33 &kNetworkConfigurationSignature,
34 *network);
35 EXPECT_TRUE(test_utils::Equals(network.get(), repaired.get()));
36 }
37
38 // Validate invalid ONC objects and check the resulting repaired object. This
39 // test fixture loads a test json file into |invalid_| containing several test
40 // objects which can be accessed by their path. The test boolean parameter
41 // determines wether to use the strict or the liberal validator.
42 class ONCValidatorInvalidTest : public ::testing::TestWithParam<bool> {
43 public:
44 // Validate the entry at |path_to_object| with the given
45 // |signature|. Depending on |managed_onc| the object is interpreted as a
46 // managed onc (with recommended fields) or not. The resulting repaired object
47 // must match the entry at |path_to_repaired| if the liberal validator is
48 // used.
49 void ValidateInvalid(const std::string& path_to_object,
50 const std::string& path_to_repaired,
51 const OncValueSignature* signature,
52 bool managed_onc) {
53 scoped_ptr<Validator> validator;
54 if (GetParam())
55 validator = Validator::CreateStrictValidator(managed_onc);
56 else
57 validator = Validator::CreateLiberalValidator(managed_onc);
58
59 const base::DictionaryValue* object = NULL;
60 ASSERT_TRUE(invalid_->GetDictionary(path_to_object, &object));
61
62 scoped_ptr<base::DictionaryValue> actual_repaired =
63 validator->ValidateAndRepairObject(signature, *object);
64 if (GetParam() || path_to_repaired == "") {
65 EXPECT_EQ(NULL, actual_repaired.get());
66 } else {
67 const base::DictionaryValue* expected_repaired = NULL;
68 invalid_->GetDictionary(path_to_repaired, &expected_repaired);
69 EXPECT_TRUE(test_utils::Equals(expected_repaired, actual_repaired.get()));
70 }
71 }
72
73 virtual void SetUp() {
74 invalid_ = test_utils::ReadTestDictionary("invalid.json");
75 }
76
77 scoped_ptr<const base::DictionaryValue> invalid_;
78 };
79
80 TEST_P(ONCValidatorInvalidTest, UnknownFieldName) {
81 ValidateInvalid("network-unknown-fieldname",
82 "network-repaired",
83 &kNetworkConfigurationSignature, false);
84 ValidateInvalid("managed-network-unknown-fieldname",
85 "managed-network-repaired",
86 &kNetworkConfigurationSignature, true);
87 }
88
89 TEST_P(ONCValidatorInvalidTest, UnknownRecommendedFieldName) {
90 ValidateInvalid("managed-network-unknown-recommended",
91 "managed-network-repaired",
92 &kNetworkConfigurationSignature, true);
93 }
94
95 TEST_P(ONCValidatorInvalidTest, DictionaryRecommended) {
96 ValidateInvalid("managed-network-dict-recommended",
97 "managed-network-repaired",
98 &kNetworkConfigurationSignature, true);
99 }
100
101 TEST_P(ONCValidatorInvalidTest, MissingRequiredField) {
102 ValidateInvalid("network-missing-required",
103 "network-missing-required",
104 &kNetworkConfigurationSignature, false);
105 ValidateInvalid("managed-network-missing-required",
106 "managed-network-missing-required",
107 &kNetworkConfigurationSignature, true);
108 }
109
110 TEST_P(ONCValidatorInvalidTest, RecommendedInUnmanaged) {
111 ValidateInvalid("network-illegal-recommended",
112 "network-repaired",
113 &kNetworkConfigurationSignature, false);
114 }
115
116 INSTANTIATE_TEST_CASE_P(ONCValidatorInvalidTest,
117 ONCValidatorInvalidTest,
118 ::testing::Bool());
Mattias Nissler (ping if slow) 2012/11/02 10:10:00 newline
pneubeck (no reviews) 2012/11/05 12:04:48 Done.
119 } // namespace onc
120 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698