OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/extensions/api/messaging/native_messaging_policy_handle
r.h" | 5 #include "chrome/browser/extensions/api/messaging/native_messaging_policy_handle
r.h" |
6 | 6 |
7 #include "base/prefs/pref_value_map.h" | 7 #include "base/prefs/pref_value_map.h" |
8 #include "chrome/browser/extensions/policy_handlers.h" | 8 #include "chrome/browser/extensions/policy_handlers.h" |
9 #include "components/policy/core/browser/policy_error_map.h" | 9 #include "components/policy/core/browser/policy_error_map.h" |
10 #include "components/policy/core/common/policy_map.h" | 10 #include "components/policy/core/common/policy_map.h" |
11 #include "components/policy/core/common/policy_types.h" | |
12 #include "policy/policy_constants.h" | 11 #include "policy/policy_constants.h" |
13 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
14 | 13 |
15 namespace extensions { | 14 namespace extensions { |
16 | 15 |
17 const char kTestPref[] = "unit_test.test_pref"; | 16 const char kTestPref[] = "unit_test.test_pref"; |
18 | 17 |
19 TEST(NativeMessagingHostListPolicyHandlerTest, CheckPolicySettings) { | 18 TEST(NativeMessagingHostListPolicyHandlerTest, CheckPolicySettings) { |
20 base::ListValue list; | 19 base::ListValue list; |
21 policy::PolicyMap policy_map; | 20 policy::PolicyMap policy_map; |
22 NativeMessagingHostListPolicyHandler handler( | 21 NativeMessagingHostListPolicyHandler handler( |
23 policy::key::kNativeMessagingBlacklist, kTestPref, true); | 22 policy::key::kNativeMessagingBlacklist, kTestPref, true); |
24 | 23 |
25 policy_map.Set(policy::key::kNativeMessagingBlacklist, | 24 policy_map.Set(policy::key::kNativeMessagingBlacklist, |
26 policy::POLICY_LEVEL_MANDATORY, | 25 policy::POLICY_LEVEL_MANDATORY, |
27 policy::POLICY_SCOPE_USER, | 26 policy::POLICY_SCOPE_USER, |
28 policy::POLICY_SOURCE_CLOUD, | |
29 list.DeepCopy(), | 27 list.DeepCopy(), |
30 NULL); | 28 NULL); |
31 { | 29 { |
32 policy::PolicyErrorMap errors; | 30 policy::PolicyErrorMap errors; |
33 EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors)); | 31 EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors)); |
34 EXPECT_TRUE(errors.empty()); | 32 EXPECT_TRUE(errors.empty()); |
35 } | 33 } |
36 | 34 |
37 list.Append(new base::StringValue("test.a.b")); | 35 list.Append(new base::StringValue("test.a.b")); |
38 policy_map.Set(policy::key::kNativeMessagingBlacklist, | 36 policy_map.Set(policy::key::kNativeMessagingBlacklist, |
39 policy::POLICY_LEVEL_MANDATORY, | 37 policy::POLICY_LEVEL_MANDATORY, |
40 policy::POLICY_SCOPE_USER, | 38 policy::POLICY_SCOPE_USER, |
41 policy::POLICY_SOURCE_CLOUD, | |
42 list.DeepCopy(), | 39 list.DeepCopy(), |
43 NULL); | 40 NULL); |
44 { | 41 { |
45 policy::PolicyErrorMap errors; | 42 policy::PolicyErrorMap errors; |
46 EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors)); | 43 EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors)); |
47 EXPECT_TRUE(errors.empty()); | 44 EXPECT_TRUE(errors.empty()); |
48 } | 45 } |
49 | 46 |
50 list.Append(new base::StringValue("*")); | 47 list.Append(new base::StringValue("*")); |
51 policy_map.Set(policy::key::kNativeMessagingBlacklist, | 48 policy_map.Set(policy::key::kNativeMessagingBlacklist, |
52 policy::POLICY_LEVEL_MANDATORY, | 49 policy::POLICY_LEVEL_MANDATORY, |
53 policy::POLICY_SCOPE_USER, | 50 policy::POLICY_SCOPE_USER, |
54 policy::POLICY_SOURCE_CLOUD, | |
55 list.DeepCopy(), | 51 list.DeepCopy(), |
56 NULL); | 52 NULL); |
57 { | 53 { |
58 policy::PolicyErrorMap errors; | 54 policy::PolicyErrorMap errors; |
59 EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors)); | 55 EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors)); |
60 EXPECT_TRUE(errors.empty()); | 56 EXPECT_TRUE(errors.empty()); |
61 } | 57 } |
62 | 58 |
63 list.Append(new base::StringValue("invalid Name")); | 59 list.Append(new base::StringValue("invalid Name")); |
64 policy_map.Set(policy::key::kNativeMessagingBlacklist, | 60 policy_map.Set(policy::key::kNativeMessagingBlacklist, |
65 policy::POLICY_LEVEL_MANDATORY, | 61 policy::POLICY_LEVEL_MANDATORY, |
66 policy::POLICY_SCOPE_USER, | 62 policy::POLICY_SCOPE_USER, |
67 policy::POLICY_SOURCE_CLOUD, | |
68 list.DeepCopy(), | 63 list.DeepCopy(), |
69 NULL); | 64 NULL); |
70 { | 65 { |
71 policy::PolicyErrorMap errors; | 66 policy::PolicyErrorMap errors; |
72 EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors)); | 67 EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors)); |
73 EXPECT_FALSE(errors.empty()); | 68 EXPECT_FALSE(errors.empty()); |
74 EXPECT_FALSE( | 69 EXPECT_FALSE( |
75 errors.GetErrors(policy::key::kNativeMessagingBlacklist).empty()); | 70 errors.GetErrors(policy::key::kNativeMessagingBlacklist).empty()); |
76 } | 71 } |
77 } | 72 } |
78 | 73 |
79 TEST(NativeMessagingHostListPolicyHandlerTest, ApplyPolicySettings) { | 74 TEST(NativeMessagingHostListPolicyHandlerTest, ApplyPolicySettings) { |
80 base::ListValue policy; | 75 base::ListValue policy; |
81 base::ListValue expected; | 76 base::ListValue expected; |
82 policy::PolicyMap policy_map; | 77 policy::PolicyMap policy_map; |
83 PrefValueMap prefs; | 78 PrefValueMap prefs; |
84 base::Value* value = NULL; | 79 base::Value* value = NULL; |
85 NativeMessagingHostListPolicyHandler handler( | 80 NativeMessagingHostListPolicyHandler handler( |
86 policy::key::kNativeMessagingBlacklist, kTestPref, true); | 81 policy::key::kNativeMessagingBlacklist, kTestPref, true); |
87 | 82 |
88 policy.Append(new base::StringValue("com.example.test")); | 83 policy.Append(new base::StringValue("com.example.test")); |
89 expected.Append(new base::StringValue("com.example.test")); | 84 expected.Append(new base::StringValue("com.example.test")); |
90 | 85 |
91 policy_map.Set(policy::key::kNativeMessagingBlacklist, | 86 policy_map.Set(policy::key::kNativeMessagingBlacklist, |
92 policy::POLICY_LEVEL_MANDATORY, | 87 policy::POLICY_LEVEL_MANDATORY, |
93 policy::POLICY_SCOPE_USER, | 88 policy::POLICY_SCOPE_USER, |
94 policy::POLICY_SOURCE_CLOUD, | |
95 policy.DeepCopy(), | 89 policy.DeepCopy(), |
96 NULL); | 90 NULL); |
97 handler.ApplyPolicySettings(policy_map, &prefs); | 91 handler.ApplyPolicySettings(policy_map, &prefs); |
98 EXPECT_TRUE(prefs.GetValue(kTestPref, &value)); | 92 EXPECT_TRUE(prefs.GetValue(kTestPref, &value)); |
99 EXPECT_TRUE(base::Value::Equals(&expected, value)); | 93 EXPECT_TRUE(base::Value::Equals(&expected, value)); |
100 | 94 |
101 policy.Append(new base::StringValue("*")); | 95 policy.Append(new base::StringValue("*")); |
102 expected.Append(new base::StringValue("*")); | 96 expected.Append(new base::StringValue("*")); |
103 | 97 |
104 policy_map.Set(policy::key::kNativeMessagingBlacklist, | 98 policy_map.Set(policy::key::kNativeMessagingBlacklist, |
105 policy::POLICY_LEVEL_MANDATORY, | 99 policy::POLICY_LEVEL_MANDATORY, |
106 policy::POLICY_SCOPE_USER, | 100 policy::POLICY_SCOPE_USER, |
107 policy::POLICY_SOURCE_CLOUD, | |
108 policy.DeepCopy(), | 101 policy.DeepCopy(), |
109 NULL); | 102 NULL); |
110 handler.ApplyPolicySettings(policy_map, &prefs); | 103 handler.ApplyPolicySettings(policy_map, &prefs); |
111 EXPECT_TRUE(prefs.GetValue(kTestPref, &value)); | 104 EXPECT_TRUE(prefs.GetValue(kTestPref, &value)); |
112 EXPECT_TRUE(base::Value::Equals(&expected, value)); | 105 EXPECT_TRUE(base::Value::Equals(&expected, value)); |
113 | 106 |
114 policy.Append(new base::StringValue("invalid Name")); | 107 policy.Append(new base::StringValue("invalid Name")); |
115 policy_map.Set(policy::key::kNativeMessagingBlacklist, | 108 policy_map.Set(policy::key::kNativeMessagingBlacklist, |
116 policy::POLICY_LEVEL_MANDATORY, | 109 policy::POLICY_LEVEL_MANDATORY, |
117 policy::POLICY_SCOPE_USER, | 110 policy::POLICY_SCOPE_USER, |
118 policy::POLICY_SOURCE_CLOUD, | |
119 policy.DeepCopy(), | 111 policy.DeepCopy(), |
120 NULL); | 112 NULL); |
121 handler.ApplyPolicySettings(policy_map, &prefs); | 113 handler.ApplyPolicySettings(policy_map, &prefs); |
122 EXPECT_TRUE(prefs.GetValue(kTestPref, &value)); | 114 EXPECT_TRUE(prefs.GetValue(kTestPref, &value)); |
123 EXPECT_TRUE(base::Value::Equals(&expected, value)); | 115 EXPECT_TRUE(base::Value::Equals(&expected, value)); |
124 } | 116 } |
125 | 117 |
126 } // namespace extensions | 118 } // namespace extensions |
OLD | NEW |