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