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
3 // found in the LICENSE file.
5 #include "chrome/browser/extensions/api/messaging/native_messaging_policy_handler.h"
7 #include "base/prefs/pref_value_map.h"
8 #include "chrome/browser/extensions/policy_handlers.h"
9 #include "components/policy/core/browser/policy_error_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"
13 #include "testing/gtest/include/gtest/gtest.h"
15 namespace extensions
{
17 const char kTestPref
[] = "unit_test.test_pref";
19 TEST(NativeMessagingHostListPolicyHandlerTest
, CheckPolicySettings
) {
21 policy::PolicyMap policy_map
;
22 NativeMessagingHostListPolicyHandler
handler(
23 policy::key::kNativeMessagingBlacklist
, kTestPref
, true);
25 policy_map
.Set(policy::key::kNativeMessagingBlacklist
,
26 policy::POLICY_LEVEL_MANDATORY
,
27 policy::POLICY_SCOPE_USER
,
28 policy::POLICY_SOURCE_CLOUD
,
32 policy::PolicyErrorMap errors
;
33 EXPECT_TRUE(handler
.CheckPolicySettings(policy_map
, &errors
));
34 EXPECT_TRUE(errors
.empty());
37 list
.Append(new base::StringValue("test.a.b"));
38 policy_map
.Set(policy::key::kNativeMessagingBlacklist
,
39 policy::POLICY_LEVEL_MANDATORY
,
40 policy::POLICY_SCOPE_USER
,
41 policy::POLICY_SOURCE_CLOUD
,
45 policy::PolicyErrorMap errors
;
46 EXPECT_TRUE(handler
.CheckPolicySettings(policy_map
, &errors
));
47 EXPECT_TRUE(errors
.empty());
50 list
.Append(new base::StringValue("*"));
51 policy_map
.Set(policy::key::kNativeMessagingBlacklist
,
52 policy::POLICY_LEVEL_MANDATORY
,
53 policy::POLICY_SCOPE_USER
,
54 policy::POLICY_SOURCE_CLOUD
,
58 policy::PolicyErrorMap errors
;
59 EXPECT_TRUE(handler
.CheckPolicySettings(policy_map
, &errors
));
60 EXPECT_TRUE(errors
.empty());
63 list
.Append(new base::StringValue("invalid Name"));
64 policy_map
.Set(policy::key::kNativeMessagingBlacklist
,
65 policy::POLICY_LEVEL_MANDATORY
,
66 policy::POLICY_SCOPE_USER
,
67 policy::POLICY_SOURCE_CLOUD
,
71 policy::PolicyErrorMap errors
;
72 EXPECT_TRUE(handler
.CheckPolicySettings(policy_map
, &errors
));
73 EXPECT_FALSE(errors
.empty());
75 errors
.GetErrors(policy::key::kNativeMessagingBlacklist
).empty());
79 TEST(NativeMessagingHostListPolicyHandlerTest
, ApplyPolicySettings
) {
80 base::ListValue policy
;
81 base::ListValue expected
;
82 policy::PolicyMap policy_map
;
84 base::Value
* value
= NULL
;
85 NativeMessagingHostListPolicyHandler
handler(
86 policy::key::kNativeMessagingBlacklist
, kTestPref
, true);
88 policy
.Append(new base::StringValue("com.example.test"));
89 expected
.Append(new base::StringValue("com.example.test"));
91 policy_map
.Set(policy::key::kNativeMessagingBlacklist
,
92 policy::POLICY_LEVEL_MANDATORY
,
93 policy::POLICY_SCOPE_USER
,
94 policy::POLICY_SOURCE_CLOUD
,
97 handler
.ApplyPolicySettings(policy_map
, &prefs
);
98 EXPECT_TRUE(prefs
.GetValue(kTestPref
, &value
));
99 EXPECT_TRUE(base::Value::Equals(&expected
, value
));
101 policy
.Append(new base::StringValue("*"));
102 expected
.Append(new base::StringValue("*"));
104 policy_map
.Set(policy::key::kNativeMessagingBlacklist
,
105 policy::POLICY_LEVEL_MANDATORY
,
106 policy::POLICY_SCOPE_USER
,
107 policy::POLICY_SOURCE_CLOUD
,
110 handler
.ApplyPolicySettings(policy_map
, &prefs
);
111 EXPECT_TRUE(prefs
.GetValue(kTestPref
, &value
));
112 EXPECT_TRUE(base::Value::Equals(&expected
, value
));
114 policy
.Append(new base::StringValue("invalid Name"));
115 policy_map
.Set(policy::key::kNativeMessagingBlacklist
,
116 policy::POLICY_LEVEL_MANDATORY
,
117 policy::POLICY_SCOPE_USER
,
118 policy::POLICY_SOURCE_CLOUD
,
121 handler
.ApplyPolicySettings(policy_map
, &prefs
);
122 EXPECT_TRUE(prefs
.GetValue(kTestPref
, &value
));
123 EXPECT_TRUE(base::Value::Equals(&expected
, value
));
126 } // namespace extensions