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 "policy/policy_constants.h"
12 #include "testing/gtest/include/gtest/gtest.h"
14 namespace extensions
{
16 const char kTestPref
[] = "unit_test.test_pref";
18 TEST(NativeMessagingHostListPolicyHandlerTest
, CheckPolicySettings
) {
20 policy::PolicyMap policy_map
;
21 NativeMessagingHostListPolicyHandler
handler(
22 policy::key::kNativeMessagingBlacklist
, kTestPref
, true);
24 policy_map
.Set(policy::key::kNativeMessagingBlacklist
,
25 policy::POLICY_LEVEL_MANDATORY
,
26 policy::POLICY_SCOPE_USER
,
30 policy::PolicyErrorMap errors
;
31 EXPECT_TRUE(handler
.CheckPolicySettings(policy_map
, &errors
));
32 EXPECT_TRUE(errors
.empty());
35 list
.Append(new base::StringValue("test.a.b"));
36 policy_map
.Set(policy::key::kNativeMessagingBlacklist
,
37 policy::POLICY_LEVEL_MANDATORY
,
38 policy::POLICY_SCOPE_USER
,
42 policy::PolicyErrorMap errors
;
43 EXPECT_TRUE(handler
.CheckPolicySettings(policy_map
, &errors
));
44 EXPECT_TRUE(errors
.empty());
47 list
.Append(new base::StringValue("*"));
48 policy_map
.Set(policy::key::kNativeMessagingBlacklist
,
49 policy::POLICY_LEVEL_MANDATORY
,
50 policy::POLICY_SCOPE_USER
,
54 policy::PolicyErrorMap errors
;
55 EXPECT_TRUE(handler
.CheckPolicySettings(policy_map
, &errors
));
56 EXPECT_TRUE(errors
.empty());
59 list
.Append(new base::StringValue("invalid Name"));
60 policy_map
.Set(policy::key::kNativeMessagingBlacklist
,
61 policy::POLICY_LEVEL_MANDATORY
,
62 policy::POLICY_SCOPE_USER
,
66 policy::PolicyErrorMap errors
;
67 EXPECT_TRUE(handler
.CheckPolicySettings(policy_map
, &errors
));
68 EXPECT_FALSE(errors
.empty());
70 errors
.GetErrors(policy::key::kNativeMessagingBlacklist
).empty());
74 TEST(NativeMessagingHostListPolicyHandlerTest
, ApplyPolicySettings
) {
75 base::ListValue policy
;
76 base::ListValue expected
;
77 policy::PolicyMap policy_map
;
79 base::Value
* value
= NULL
;
80 NativeMessagingHostListPolicyHandler
handler(
81 policy::key::kNativeMessagingBlacklist
, kTestPref
, true);
83 policy
.Append(new base::StringValue("com.example.test"));
84 expected
.Append(new base::StringValue("com.example.test"));
86 policy_map
.Set(policy::key::kNativeMessagingBlacklist
,
87 policy::POLICY_LEVEL_MANDATORY
,
88 policy::POLICY_SCOPE_USER
,
91 handler
.ApplyPolicySettings(policy_map
, &prefs
);
92 EXPECT_TRUE(prefs
.GetValue(kTestPref
, &value
));
93 EXPECT_TRUE(base::Value::Equals(&expected
, value
));
95 policy
.Append(new base::StringValue("*"));
96 expected
.Append(new base::StringValue("*"));
98 policy_map
.Set(policy::key::kNativeMessagingBlacklist
,
99 policy::POLICY_LEVEL_MANDATORY
,
100 policy::POLICY_SCOPE_USER
,
103 handler
.ApplyPolicySettings(policy_map
, &prefs
);
104 EXPECT_TRUE(prefs
.GetValue(kTestPref
, &value
));
105 EXPECT_TRUE(base::Value::Equals(&expected
, value
));
107 policy
.Append(new base::StringValue("invalid Name"));
108 policy_map
.Set(policy::key::kNativeMessagingBlacklist
,
109 policy::POLICY_LEVEL_MANDATORY
,
110 policy::POLICY_SCOPE_USER
,
113 handler
.ApplyPolicySettings(policy_map
, &prefs
);
114 EXPECT_TRUE(prefs
.GetValue(kTestPref
, &value
));
115 EXPECT_TRUE(base::Value::Equals(&expected
, value
));
118 } // namespace extensions