blob: 7862ead5dad53578d1653b20d09c0c4ee1e823a0 [file] [log] [blame]
Bartosz Fabianowskic4ee8bf2012-01-25 16:31:26 +01001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Julian Pastarmov2638fbc2011-07-20 14:30:46 +02002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include <base/file_path.h>
6#include <base/logging.h>
7#include <gtest/gtest.h>
8#include <openssl/ssl.h>
9#include <openssl/err.h>
10
11#include <chromeos/policy/libpolicy.h>
12#include <chromeos/policy/device_policy_impl.h>
13
14namespace policy {
15
Bartosz Fabianowskia6732cd2012-02-07 11:54:21 +010016static const char kPolicyFileAllSet[] =
17 "chromeos/policy/tests/whitelist/policy_all";
18static const char kPolicyFileNoneSet[] =
19 "chromeos/policy/tests/whitelist/policy_none";
Julian Pastarmov2638fbc2011-07-20 14:30:46 +020020static const char kKeyFile[] = "chromeos/policy/tests/whitelist/owner.key";
21
Bartosz Fabianowskia6732cd2012-02-07 11:54:21 +010022// This class mocks only the minimally needed functionionality to run tests
Julian Pastarmov2638fbc2011-07-20 14:30:46 +020023// that would otherwise fail because of hard restrictions like root file
Bartosz Fabianowskia6732cd2012-02-07 11:54:21 +010024// ownership. Otherwise, it preserves all the functionallity of the original
25// class.
Julian Pastarmov2638fbc2011-07-20 14:30:46 +020026class MockDevicePolicyImpl : public DevicePolicyImpl {
27 public:
28 MockDevicePolicyImpl(const FilePath& policy_path,
29 const FilePath& keyfile_path,
30 bool verify_files)
Bartosz Fabianowskia6732cd2012-02-07 11:54:21 +010031 : verify_files_(verify_files) {
Julian Pastarmov2638fbc2011-07-20 14:30:46 +020032 policy_path_ = policy_path;
33 keyfile_path_ = keyfile_path;
34 }
35
36 private:
37 // We don't care if files are owned by root for most tests.
38 virtual bool VerifyPolicyFiles() {
39 return !verify_files_ || DevicePolicyImpl::VerifyPolicyFiles();
40 }
41
42 bool verify_files_;
43};
44
Bartosz Fabianowskia6732cd2012-02-07 11:54:21 +010045// Test that a policy file can be verified and parsed correctly. The file
46// contains all possible fields, so reading should succeed for all.
47TEST(PolicyTest, DevicePolicyAllSetTest) {
48 FilePath policy_file(kPolicyFileAllSet);
Julian Pastarmov2638fbc2011-07-20 14:30:46 +020049 FilePath key_file(kKeyFile);
50 MockDevicePolicyImpl* device_policy =
51 new MockDevicePolicyImpl(policy_file, key_file, false);
52 PolicyProvider provider(device_policy);
53 provider.Reload();
54
Bartosz Fabianowskia6732cd2012-02-07 11:54:21 +010055 // Ensure we successfully loaded the device policy file.
Julian Pastarmov2638fbc2011-07-20 14:30:46 +020056 ASSERT_TRUE(provider.device_policy_is_loaded());
57
58 const DevicePolicy& policy = provider.GetDevicePolicy();
59
Bartosz Fabianowskia6732cd2012-02-07 11:54:21 +010060 // Check that we can read out all fields of the sample protobuf.
Julian Pastarmov2638fbc2011-07-20 14:30:46 +020061 int int_value = -1;
62 ASSERT_TRUE(policy.GetPolicyRefreshRate(&int_value));
63 ASSERT_EQ(100, int_value);
64
Bartosz Fabianowskia6732cd2012-02-07 11:54:21 +010065 std::vector<std::string> list_value;
66 ASSERT_TRUE(policy.GetUserWhitelist(&list_value));
67 ASSERT_EQ(3, list_value.size());
68 ASSERT_EQ("me@here.com", list_value[0]);
69 ASSERT_EQ("you@there.com", list_value[1]);
70 ASSERT_EQ("*@monsters.com", list_value[2]);
71
72 bool bool_value = true;
73 ASSERT_TRUE(policy.GetGuestModeEnabled(&bool_value));
74 ASSERT_FALSE(bool_value);
75
Julian Pastarmov2638fbc2011-07-20 14:30:46 +020076 bool_value = true;
77 ASSERT_TRUE(policy.GetCameraEnabled(&bool_value));
Bartosz Fabianowskic4ee8bf2012-01-25 16:31:26 +010078 ASSERT_FALSE(bool_value);
Julian Pastarmov2638fbc2011-07-20 14:30:46 +020079
80 bool_value = true;
81 ASSERT_TRUE(policy.GetShowUserNames(&bool_value));
Bartosz Fabianowskic4ee8bf2012-01-25 16:31:26 +010082 ASSERT_FALSE(bool_value);
Julian Pastarmov2638fbc2011-07-20 14:30:46 +020083
84 bool_value = true;
85 ASSERT_TRUE(policy.GetDataRoamingEnabled(&bool_value));
Bartosz Fabianowskic4ee8bf2012-01-25 16:31:26 +010086 ASSERT_FALSE(bool_value);
Julian Pastarmov2638fbc2011-07-20 14:30:46 +020087
88 bool_value = true;
89 ASSERT_TRUE(policy.GetAllowNewUsers(&bool_value));
Bartosz Fabianowskic4ee8bf2012-01-25 16:31:26 +010090 ASSERT_FALSE(bool_value);
Julian Pastarmov2638fbc2011-07-20 14:30:46 +020091
Bartosz Fabianowskia6732cd2012-02-07 11:54:21 +010092 bool_value = true;
93 ASSERT_TRUE(policy.GetMetricsEnabled(&bool_value));
94 ASSERT_FALSE(bool_value);
95
96 bool_value = true;
97 ASSERT_TRUE(policy.GetReportVersionInfo(&bool_value));
98 ASSERT_FALSE(bool_value);
99
100 bool_value = true;
101 ASSERT_TRUE(policy.GetReportActivityTimes(&bool_value));
102 ASSERT_FALSE(bool_value);
103
104 bool_value = true;
105 ASSERT_TRUE(policy.GetReportBootMode(&bool_value));
106 ASSERT_FALSE(bool_value);
107
108 bool_value = true;
109 ASSERT_TRUE(policy.GetEphemeralUsers(&bool_value));
110 ASSERT_FALSE(bool_value);
Julian Pastarmov2638fbc2011-07-20 14:30:46 +0200111
112 std::string string_value;
113 ASSERT_TRUE(policy.GetProxyMode(&string_value));
114 ASSERT_EQ("direct", string_value);
115
116 ASSERT_TRUE(policy.GetProxyServer(&string_value));
117 ASSERT_EQ("myproxy", string_value);
118
119 ASSERT_TRUE(policy.GetProxyPacUrl(&string_value));
120 ASSERT_EQ("http://mypac.pac", string_value);
121
122 ASSERT_TRUE(policy.GetProxyBypassList(&string_value));
123 ASSERT_EQ("a, b, c", string_value);
124
Bartosz Fabianowskia6732cd2012-02-07 11:54:21 +0100125 ASSERT_TRUE(policy.GetReleaseChannel(&string_value));
126 ASSERT_EQ("stable-channel", string_value);
127
128 ASSERT_TRUE(policy.GetOpenNetworkConfiguration(&string_value));
129 ASSERT_EQ("{}", string_value);
130
Bartosz Fabianowskic4ee8bf2012-01-25 16:31:26 +0100131 ASSERT_TRUE(policy.GetOwner(&string_value));
132 ASSERT_EQ("", string_value);
133
Bartosz Fabianowskia6732cd2012-02-07 11:54:21 +0100134 // Reloading the protobuf should succeed.
Julian Pastarmov2638fbc2011-07-20 14:30:46 +0200135 ASSERT_TRUE(provider.Reload());
136}
137
Bartosz Fabianowskia6732cd2012-02-07 11:54:21 +0100138// Test that a policy file can be verified and parsed correctly. The file
139// contains none of the possible fields, so reading should fail for all.
140TEST(PolicyTest, DevicePolicyNoneSetTest) {
141 FilePath policy_file(kPolicyFileNoneSet);
142 FilePath key_file(kKeyFile);
143 MockDevicePolicyImpl* device_policy =
144 new MockDevicePolicyImpl(policy_file, key_file, false);
145 PolicyProvider provider(device_policy);
146 provider.Reload();
147
148 // Ensure we successfully loaded the device policy file.
149 ASSERT_TRUE(provider.device_policy_is_loaded());
150
151 const DevicePolicy& policy = provider.GetDevicePolicy();
152
153 // Check that we cannot read any fields out of the sample protobuf.
154 int int_value;
155 std::vector<std::string> list_value;
156 bool bool_value;
157 std::string string_value;
158
159 ASSERT_FALSE(policy.GetPolicyRefreshRate(&int_value));
160 ASSERT_FALSE(policy.GetUserWhitelist(&list_value));
161 ASSERT_FALSE(policy.GetGuestModeEnabled(&bool_value));
162 ASSERT_FALSE(policy.GetCameraEnabled(&bool_value));
163 ASSERT_FALSE(policy.GetShowUserNames(&bool_value));
164 ASSERT_FALSE(policy.GetDataRoamingEnabled(&bool_value));
165 ASSERT_FALSE(policy.GetAllowNewUsers(&bool_value));
166 ASSERT_FALSE(policy.GetMetricsEnabled(&bool_value));
167 ASSERT_FALSE(policy.GetReportVersionInfo(&bool_value));
168 ASSERT_FALSE(policy.GetReportActivityTimes(&bool_value));
169 ASSERT_FALSE(policy.GetReportBootMode(&bool_value));
170 ASSERT_FALSE(policy.GetEphemeralUsers(&bool_value));
171 ASSERT_FALSE(policy.GetProxyMode(&string_value));
172 ASSERT_FALSE(policy.GetProxyServer(&string_value));
173 ASSERT_FALSE(policy.GetProxyPacUrl(&string_value));
174 ASSERT_FALSE(policy.GetProxyBypassList(&string_value));
175 ASSERT_FALSE(policy.GetReleaseChannel(&string_value));
176 ASSERT_FALSE(policy.GetOpenNetworkConfiguration(&string_value));
177}
178
Julian Pastarmov2638fbc2011-07-20 14:30:46 +0200179// Verify that the library will correctly recognize and signal missing files.
180TEST(PolicyTest, DevicePolicyFailure) {
181 LOG(INFO) << "Errors expected.";
182 // Try loading non-existing protobuf should fail.
183 FilePath non_existing("this_file_is_doof");
184 MockDevicePolicyImpl* device_policy =
185 new MockDevicePolicyImpl(non_existing, non_existing, true);
186 PolicyProvider provider(device_policy);
187 // Even after reload the policy should still be not loaded.
188 ASSERT_FALSE(provider.Reload());
189 ASSERT_FALSE(provider.device_policy_is_loaded());
190}
191
192} // namespace policy
193
194int main(int argc, char* argv[]) {
195 ::testing::InitGoogleTest(&argc, argv);
196 return RUN_ALL_TESTS();
197}