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

Unified 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: Addressed comments (formatting, sorting). Minor change in policy.onc. Created 8 years, 2 months 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 side-by-side diff with in-line comments
Download patch
Index: chrome/browser/chromeos/network_settings/onc_validator_unittest.cc
diff --git a/chrome/browser/chromeos/network_settings/onc_validator_unittest.cc b/chrome/browser/chromeos/network_settings/onc_validator_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..19eae8252f52e350f1aa15b1d0d318aa6a60077f
--- /dev/null
+++ b/chrome/browser/chromeos/network_settings/onc_validator_unittest.cc
@@ -0,0 +1,124 @@
+// 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 "chrome/browser/chromeos/network_settings/onc_validator.h"
+
+#include <string>
+
+#include "base/memory/scoped_ptr.h"
+#include "base/values.h"
+#include "chrome/browser/chromeos/cros/onc_constants.h"
+#include "chrome/browser/chromeos/network_settings/onc_signature.h"
+#include "chrome/browser/chromeos/network_settings/onc_test_utils.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+#include <iostream>
+
+namespace chromeos {
+namespace onc {
+
+// This test case is about validating valid ONC objects.
+TEST(ONCValidatorValidTest, ValidPolicyOnc) {
+ scoped_ptr<Validator> validator(Validator::CreateStrictValidator(true));
+ scoped_ptr<const base::DictionaryValue> network;
+ scoped_ptr<base::DictionaryValue> repaired;
+
+ network = test_utils::ReadTestDictionary("policy.onc");
+ repaired = validator->ValidateAndRepairObject(
+ &network_configuration_signature,
+ *network);
+ EXPECT_TRUE(repaired.get() != NULL && network->Equals(repaired.get()));
+
+ network = test_utils::ReadTestDictionary("valid.onc");
+ repaired = validator->ValidateAndRepairObject(
+ &network_configuration_signature,
+ *network);
+ EXPECT_TRUE(repaired.get() != NULL && network->Equals(repaired.get()));
+}
+
+// Validate invalid ONC objects and check the resulting repaired object. This
+// test fixture loads a test json file into |invalid_| containing several test
+// objects which can be accessed by their path. The test boolean parameter
+// determines wether to use the strict or the liberal validator.
+class ONCValidatorInvalidTest : public ::testing::TestWithParam<bool> {
+ public:
+ // Validate the entry at |path_to_object| with the given
+ // |signature|. Depending on |managed_onc| the object is interpreted as a
+ // managed onc (with recommended fields) or not. The resulting repaired object
+ // must match the entry at |path_to_repaired| if the liberal validator is
+ // used.
+ void ValidateInvalid(const std::string& path_to_object,
+ const std::string& path_to_repaired,
+ const OncValueSignature* signature,
+ bool managed_onc) {
+ scoped_ptr<Validator> validator;
+ if (GetParam())
+ validator = Validator::CreateStrictValidator(managed_onc);
+ else
+ validator = Validator::CreateLiberalValidator(managed_onc);
+
+ const base::DictionaryValue* object = NULL;
+ ASSERT_TRUE(invalid_->GetDictionary(path_to_object, &object));
+
+ scoped_ptr<base::DictionaryValue> actual_repaired =
+ validator->ValidateAndRepairObject(signature, *object);
+ if (GetParam() || path_to_repaired == "") {
+ EXPECT_EQ(NULL, actual_repaired.get());
+ } else {
+ const base::DictionaryValue* expected_repaired = NULL;
+ invalid_->GetDictionary(path_to_repaired, &expected_repaired);
+ ASSERT_TRUE(expected_repaired != NULL);
+ EXPECT_TRUE(actual_repaired.get() &&
+ actual_repaired->Equals(expected_repaired));
+ }
+ }
+
+ virtual void SetUp() {
+ invalid_ = test_utils::ReadTestDictionary("invalid.onc");
+ }
+
+ scoped_ptr<const base::DictionaryValue> invalid_;
+};
+
+TEST_P(ONCValidatorInvalidTest, UnknownFieldName) {
+ ValidateInvalid("network-unknown-fieldname",
+ "network-repaired",
+ &network_configuration_signature, false);
+ ValidateInvalid("managed-network-unknown-fieldname",
+ "managed-network-repaired",
+ &network_configuration_signature, true);
+}
+
+TEST_P(ONCValidatorInvalidTest, UnknownRecommendedFieldName) {
+ ValidateInvalid("managed-network-unknown-recommended",
+ "managed-network-repaired",
+ &network_configuration_signature, true);
+}
+
+TEST_P(ONCValidatorInvalidTest, DictionaryRecommended) {
+ ValidateInvalid("managed-network-dict-recommended",
+ "managed-network-repaired",
+ &network_configuration_signature, true);
+}
+
+TEST_P(ONCValidatorInvalidTest, MissingRequiredField) {
+ ValidateInvalid("network-missing-required",
+ "network-missing-required",
+ &network_configuration_signature, false);
+ ValidateInvalid("managed-network-missing-required",
+ "managed-network-missing-required",
+ &network_configuration_signature, true);
+}
+
+TEST_P(ONCValidatorInvalidTest, RecommendedInUnmanaged) {
+ ValidateInvalid("network-illegal-recommended",
+ "network-repaired",
+ &network_configuration_signature, false);
+}
+
+INSTANTIATE_TEST_CASE_P(ONCValidatorInvalidTest,
+ ONCValidatorInvalidTest,
+ ::testing::Bool());
+} // namespace onc
+} // namespace chromeos

Powered by Google App Engine
This is Rietveld 408576698