1 // Copyright (c) 2012 The Chromium OS 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 "install_attributes/libinstallattributes.h"
6 
7 #include <string>
8 
9 #include <gtest/gtest.h>
10 
11 // Allows to override the install attributes path while preserving all the
12 // functionality of the original class.
13 class MockInstallAttributesReader : public InstallAttributesReader {
14  public:
SetPath(const std::string & filename)15   void SetPath(const std::string& filename) {
16     install_attributes_path_ = base::FilePath(filename);
17   }
GetAttributesCount() const18   size_t GetAttributesCount() const { return attributes_.size(); }
19 };
20 
TEST(InstallAttributesTest,ReadNonexistingAttributes)21 TEST(InstallAttributesTest, ReadNonexistingAttributes) {
22   MockInstallAttributesReader reader;
23   reader.SetPath("non-existing.pb");
24   ASSERT_FALSE(reader.IsLocked());
25   ASSERT_EQ(0, reader.GetAttributesCount());
26 }
27 
28 // corrupt.pb is an invalid proto.
TEST(InstallAttributesTest,ReadCorruptAttributes)29 TEST(InstallAttributesTest, ReadCorruptAttributes) {
30   MockInstallAttributesReader reader;
31   reader.SetPath("install_attributes/tests/corrupt.pb");
32   ASSERT_TRUE(reader.IsLocked());
33   ASSERT_EQ(0, reader.GetAttributesCount());
34 }
35 
36 // consumer.pb is a valid proto containing no attributes.
TEST(InstallAttributesTest,ReadEmptyAttributes)37 TEST(InstallAttributesTest, ReadEmptyAttributes) {
38   MockInstallAttributesReader reader;
39   reader.SetPath("install_attributes/tests/consumer.pb");
40   ASSERT_TRUE(reader.IsLocked());
41   ASSERT_EQ(0, reader.GetAttributesCount());
42 }
43 
44 // managed.pb is a valid proto containing the usual enterprise enrollment
45 // attributes.
TEST(InstallAttributesTest,ReadManagedAttributes)46 TEST(InstallAttributesTest, ReadManagedAttributes) {
47   MockInstallAttributesReader reader;
48   reader.SetPath("install_attributes/tests/managed.pb");
49   ASSERT_TRUE(reader.IsLocked());
50   ASSERT_EQ(std::string(), reader.GetAttribute("non-existing"));
51   ASSERT_EQ("enterprise", reader.GetAttribute("enterprise.mode"));
52 }
53 
54 // Going from non-existing attributes file to existing attributes file must
55 // work, i.e. the non-existence of the attributes file must not be cached.
TEST(InstallAttributesTest,ProgressionFromNonExistingToManaged)56 TEST(InstallAttributesTest, ProgressionFromNonExistingToManaged) {
57   MockInstallAttributesReader reader;
58   reader.SetPath("non-existing.pb");
59   ASSERT_FALSE(reader.IsLocked());
60   ASSERT_EQ(0, reader.GetAttributesCount());
61 
62   reader.SetPath("install_attributes/tests/managed.pb");
63   ASSERT_TRUE(reader.IsLocked());
64   ASSERT_EQ("enterprise", reader.GetAttribute("enterprise.mode"));
65 }
66 
67 // Going from empty attributes file to non-empty attributes file must not work,
68 // i.e. the non-existence of the attributes must be cached.
TEST(InstallAttributesTest,NoProgressionFromEmptyToManaged)69 TEST(InstallAttributesTest, NoProgressionFromEmptyToManaged) {
70   MockInstallAttributesReader reader;
71   reader.SetPath("install_attributes/tests/consumer.pb");
72   ASSERT_TRUE(reader.IsLocked());
73   ASSERT_EQ(0, reader.GetAttributesCount());
74 
75   reader.SetPath("install_attributes/tests/managed.pb");
76   ASSERT_TRUE(reader.IsLocked());
77   ASSERT_EQ(std::string(), reader.GetAttribute("enterprise.mode"));
78 }
79