Bartosz Fabianowski | c4ee8bf | 2012-01-25 16:31:26 +0100 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Julian Pastarmov | 2638fbc | 2011-07-20 14:30:46 +0200 | [diff] [blame] | 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 <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 | |
| 14 | namespace policy { |
| 15 | |
Bartosz Fabianowski | a6732cd | 2012-02-07 11:54:21 +0100 | [diff] [blame^] | 16 | static const char kPolicyFileAllSet[] = |
| 17 | "chromeos/policy/tests/whitelist/policy_all"; |
| 18 | static const char kPolicyFileNoneSet[] = |
| 19 | "chromeos/policy/tests/whitelist/policy_none"; |
Julian Pastarmov | 2638fbc | 2011-07-20 14:30:46 +0200 | [diff] [blame] | 20 | static const char kKeyFile[] = "chromeos/policy/tests/whitelist/owner.key"; |
| 21 | |
Bartosz Fabianowski | a6732cd | 2012-02-07 11:54:21 +0100 | [diff] [blame^] | 22 | // This class mocks only the minimally needed functionionality to run tests |
Julian Pastarmov | 2638fbc | 2011-07-20 14:30:46 +0200 | [diff] [blame] | 23 | // that would otherwise fail because of hard restrictions like root file |
Bartosz Fabianowski | a6732cd | 2012-02-07 11:54:21 +0100 | [diff] [blame^] | 24 | // ownership. Otherwise, it preserves all the functionallity of the original |
| 25 | // class. |
Julian Pastarmov | 2638fbc | 2011-07-20 14:30:46 +0200 | [diff] [blame] | 26 | class MockDevicePolicyImpl : public DevicePolicyImpl { |
| 27 | public: |
| 28 | MockDevicePolicyImpl(const FilePath& policy_path, |
| 29 | const FilePath& keyfile_path, |
| 30 | bool verify_files) |
Bartosz Fabianowski | a6732cd | 2012-02-07 11:54:21 +0100 | [diff] [blame^] | 31 | : verify_files_(verify_files) { |
Julian Pastarmov | 2638fbc | 2011-07-20 14:30:46 +0200 | [diff] [blame] | 32 | 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 Fabianowski | a6732cd | 2012-02-07 11:54:21 +0100 | [diff] [blame^] | 45 | // Test that a policy file can be verified and parsed correctly. The file |
| 46 | // contains all possible fields, so reading should succeed for all. |
| 47 | TEST(PolicyTest, DevicePolicyAllSetTest) { |
| 48 | FilePath policy_file(kPolicyFileAllSet); |
Julian Pastarmov | 2638fbc | 2011-07-20 14:30:46 +0200 | [diff] [blame] | 49 | 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 Fabianowski | a6732cd | 2012-02-07 11:54:21 +0100 | [diff] [blame^] | 55 | // Ensure we successfully loaded the device policy file. |
Julian Pastarmov | 2638fbc | 2011-07-20 14:30:46 +0200 | [diff] [blame] | 56 | ASSERT_TRUE(provider.device_policy_is_loaded()); |
| 57 | |
| 58 | const DevicePolicy& policy = provider.GetDevicePolicy(); |
| 59 | |
Bartosz Fabianowski | a6732cd | 2012-02-07 11:54:21 +0100 | [diff] [blame^] | 60 | // Check that we can read out all fields of the sample protobuf. |
Julian Pastarmov | 2638fbc | 2011-07-20 14:30:46 +0200 | [diff] [blame] | 61 | int int_value = -1; |
| 62 | ASSERT_TRUE(policy.GetPolicyRefreshRate(&int_value)); |
| 63 | ASSERT_EQ(100, int_value); |
| 64 | |
Bartosz Fabianowski | a6732cd | 2012-02-07 11:54:21 +0100 | [diff] [blame^] | 65 | 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 Pastarmov | 2638fbc | 2011-07-20 14:30:46 +0200 | [diff] [blame] | 76 | bool_value = true; |
| 77 | ASSERT_TRUE(policy.GetCameraEnabled(&bool_value)); |
Bartosz Fabianowski | c4ee8bf | 2012-01-25 16:31:26 +0100 | [diff] [blame] | 78 | ASSERT_FALSE(bool_value); |
Julian Pastarmov | 2638fbc | 2011-07-20 14:30:46 +0200 | [diff] [blame] | 79 | |
| 80 | bool_value = true; |
| 81 | ASSERT_TRUE(policy.GetShowUserNames(&bool_value)); |
Bartosz Fabianowski | c4ee8bf | 2012-01-25 16:31:26 +0100 | [diff] [blame] | 82 | ASSERT_FALSE(bool_value); |
Julian Pastarmov | 2638fbc | 2011-07-20 14:30:46 +0200 | [diff] [blame] | 83 | |
| 84 | bool_value = true; |
| 85 | ASSERT_TRUE(policy.GetDataRoamingEnabled(&bool_value)); |
Bartosz Fabianowski | c4ee8bf | 2012-01-25 16:31:26 +0100 | [diff] [blame] | 86 | ASSERT_FALSE(bool_value); |
Julian Pastarmov | 2638fbc | 2011-07-20 14:30:46 +0200 | [diff] [blame] | 87 | |
| 88 | bool_value = true; |
| 89 | ASSERT_TRUE(policy.GetAllowNewUsers(&bool_value)); |
Bartosz Fabianowski | c4ee8bf | 2012-01-25 16:31:26 +0100 | [diff] [blame] | 90 | ASSERT_FALSE(bool_value); |
Julian Pastarmov | 2638fbc | 2011-07-20 14:30:46 +0200 | [diff] [blame] | 91 | |
Bartosz Fabianowski | a6732cd | 2012-02-07 11:54:21 +0100 | [diff] [blame^] | 92 | 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 Pastarmov | 2638fbc | 2011-07-20 14:30:46 +0200 | [diff] [blame] | 111 | |
| 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 Fabianowski | a6732cd | 2012-02-07 11:54:21 +0100 | [diff] [blame^] | 125 | 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 Fabianowski | c4ee8bf | 2012-01-25 16:31:26 +0100 | [diff] [blame] | 131 | ASSERT_TRUE(policy.GetOwner(&string_value)); |
| 132 | ASSERT_EQ("", string_value); |
| 133 | |
Bartosz Fabianowski | a6732cd | 2012-02-07 11:54:21 +0100 | [diff] [blame^] | 134 | // Reloading the protobuf should succeed. |
Julian Pastarmov | 2638fbc | 2011-07-20 14:30:46 +0200 | [diff] [blame] | 135 | ASSERT_TRUE(provider.Reload()); |
| 136 | } |
| 137 | |
Bartosz Fabianowski | a6732cd | 2012-02-07 11:54:21 +0100 | [diff] [blame^] | 138 | // 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. |
| 140 | TEST(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 Pastarmov | 2638fbc | 2011-07-20 14:30:46 +0200 | [diff] [blame] | 179 | // Verify that the library will correctly recognize and signal missing files. |
| 180 | TEST(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 | |
| 194 | int main(int argc, char* argv[]) { |
| 195 | ::testing::InitGoogleTest(&argc, argv); |
| 196 | return RUN_ALL_TESTS(); |
| 197 | } |