aboutsummaryrefslogtreecommitdiff
path: root/install_attributes/tests/libinstallattributes_unittest.cc
diff options
context:
space:
mode:
Diffstat (limited to 'install_attributes/tests/libinstallattributes_unittest.cc')
-rw-r--r--install_attributes/tests/libinstallattributes_unittest.cc83
1 files changed, 83 insertions, 0 deletions
diff --git a/install_attributes/tests/libinstallattributes_unittest.cc b/install_attributes/tests/libinstallattributes_unittest.cc
new file mode 100644
index 0000000..45ff827
--- /dev/null
+++ b/install_attributes/tests/libinstallattributes_unittest.cc
@@ -0,0 +1,83 @@
+// Copyright (c) 2012 The Chromium OS 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 "install_attributes/libinstallattributes.h"
+
+#include <string>
+
+#include <gtest/gtest.h>
+
+// Allows to override the install attributes path while preserving all the
+// functionality of the original class.
+class MockInstallAttributesReader : public InstallAttributesReader {
+ public:
+ void SetPath(const std::string& filename) {
+ install_attributes_path_ = base::FilePath(filename);
+ }
+ size_t GetAttributesCount() const { return attributes_.size(); }
+};
+
+TEST(InstallAttributesTest, ReadNonexistingAttributes) {
+ MockInstallAttributesReader reader;
+ reader.SetPath("non-existing.pb");
+ ASSERT_FALSE(reader.IsLocked());
+ ASSERT_EQ(0, reader.GetAttributesCount());
+}
+
+// corrupt.pb is an invalid proto.
+TEST(InstallAttributesTest, ReadCorruptAttributes) {
+ MockInstallAttributesReader reader;
+ reader.SetPath("install_attributes/tests/corrupt.pb");
+ ASSERT_TRUE(reader.IsLocked());
+ ASSERT_EQ(0, reader.GetAttributesCount());
+}
+
+// consumer.pb is a valid proto containing no attributes.
+TEST(InstallAttributesTest, ReadEmptyAttributes) {
+ MockInstallAttributesReader reader;
+ reader.SetPath("install_attributes/tests/consumer.pb");
+ ASSERT_TRUE(reader.IsLocked());
+ ASSERT_EQ(0, reader.GetAttributesCount());
+}
+
+// managed.pb is a valid proto containing the usual enterprise enrollment
+// attributes.
+TEST(InstallAttributesTest, ReadManagedAttributes) {
+ MockInstallAttributesReader reader;
+ reader.SetPath("install_attributes/tests/managed.pb");
+ ASSERT_TRUE(reader.IsLocked());
+ ASSERT_EQ(std::string(), reader.GetAttribute("non-existing"));
+ ASSERT_EQ("enterprise", reader.GetAttribute("enterprise.mode"));
+}
+
+// Going from non-existing attributes file to existing attributes file must
+// work, i.e. the non-existence of the attributes file must not be cached.
+TEST(InstallAttributesTest, ProgressionFromNonExistingToManaged) {
+ MockInstallAttributesReader reader;
+ reader.SetPath("non-existing.pb");
+ ASSERT_FALSE(reader.IsLocked());
+ ASSERT_EQ(0, reader.GetAttributesCount());
+
+ reader.SetPath("install_attributes/tests/managed.pb");
+ ASSERT_TRUE(reader.IsLocked());
+ ASSERT_EQ("enterprise", reader.GetAttribute("enterprise.mode"));
+}
+
+// Going from empty attributes file to non-empty attributes file must not work,
+// i.e. the non-existence of the attributes must be cached.
+TEST(InstallAttributesTest, NoProgressionFromEmptyToManaged) {
+ MockInstallAttributesReader reader;
+ reader.SetPath("install_attributes/tests/consumer.pb");
+ ASSERT_TRUE(reader.IsLocked());
+ ASSERT_EQ(0, reader.GetAttributesCount());
+
+ reader.SetPath("install_attributes/tests/managed.pb");
+ ASSERT_TRUE(reader.IsLocked());
+ ASSERT_EQ(std::string(), reader.GetAttribute("enterprise.mode"));
+}
+
+int main(int argc, char* argv[]) {
+ ::testing::InitGoogleTest(&argc, argv);
+ return RUN_ALL_TESTS();
+}