OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012 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/policy/network_configuration_updater.h" | |
6 | |
7 #include "base/memory/scoped_ptr.h" | |
8 #include "base/message_loop.h" | |
9 #include "base/run_loop.h" | |
10 #include "chrome/browser/chromeos/cros/mock_network_library.h" | |
11 #include "chrome/browser/policy/mock_configuration_policy_provider.h" | |
12 #include "chrome/browser/policy/policy_map.h" | |
13 #include "chrome/browser/policy/policy_service_impl.h" | |
14 #include "chromeos/network/onc/onc_constants.h" | |
15 #include "chromeos/network/onc/onc_utils.h" | |
16 #include "policy/policy_constants.h" | |
17 #include "testing/gmock/include/gmock/gmock.h" | |
18 #include "testing/gtest/include/gtest/gtest.h" | |
19 | |
20 using testing::AtLeast; | |
21 using testing::Mock; | |
22 using testing::Ne; | |
23 using testing::Return; | |
24 using testing::_; | |
25 | |
26 namespace policy { | |
27 | |
28 static const char kFakeONC[] = "{ \"GUID\": \"1234\" }"; | |
29 | |
30 class NetworkConfigurationUpdaterTest | |
31 : public testing::TestWithParam<const char*>{ | |
32 protected: | |
33 virtual void SetUp() OVERRIDE { | |
34 EXPECT_CALL(provider_, IsInitializationComplete(_)) | |
35 .WillRepeatedly(Return(true)); | |
36 provider_.Init(); | |
37 PolicyServiceImpl::Providers providers; | |
38 providers.push_back(&provider_); | |
39 policy_service_.reset(new PolicyServiceImpl(providers)); | |
40 } | |
41 | |
42 virtual void TearDown() OVERRIDE { | |
43 provider_.Shutdown(); | |
44 } | |
45 | |
46 void UpdateProviderPolicy(const PolicyMap& policy) { | |
47 provider_.UpdateChromePolicy(policy); | |
48 base::RunLoop loop; | |
49 loop.RunUntilIdle(); | |
50 } | |
51 | |
52 // Maps configuration policy name to corresponding ONC source. | |
53 static chromeos::onc::ONCSource NameToONCSource( | |
54 const std::string& name) { | |
55 if (name == key::kDeviceOpenNetworkConfiguration) | |
56 return chromeos::onc::ONC_SOURCE_DEVICE_POLICY; | |
57 if (name == key::kOpenNetworkConfiguration) | |
58 return chromeos::onc::ONC_SOURCE_USER_POLICY; | |
59 return chromeos::onc::ONC_SOURCE_NONE; | |
60 } | |
61 | |
62 chromeos::MockNetworkLibrary network_library_; | |
63 MockConfigurationPolicyProvider provider_; | |
64 scoped_ptr<PolicyServiceImpl> policy_service_; | |
65 MessageLoop loop_; | |
66 }; | |
67 | |
68 TEST_P(NetworkConfigurationUpdaterTest, InitialUpdates) { | |
69 PolicyMap policy; | |
70 policy.Set(GetParam(), POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | |
71 Value::CreateStringValue(kFakeONC)); | |
72 UpdateProviderPolicy(policy); | |
73 | |
74 EXPECT_CALL(network_library_, AddNetworkProfileObserver(_)); | |
75 | |
76 // Initially, only the device policy is applied. The user policy is only | |
77 // applied after the user profile was initialized. | |
78 const char* device_onc = GetParam() == key::kDeviceOpenNetworkConfiguration ? | |
79 kFakeONC : chromeos::onc::kEmptyUnencryptedConfiguration; | |
80 EXPECT_CALL(network_library_, LoadOncNetworks( | |
81 device_onc, "", chromeos::onc::ONC_SOURCE_DEVICE_POLICY, _)); | |
82 | |
83 { | |
84 NetworkConfigurationUpdater updater(policy_service_.get(), | |
85 &network_library_); | |
86 Mock::VerifyAndClearExpectations(&network_library_); | |
87 | |
88 // After the user policy is initialized, we always push both policies to the | |
89 // NetworkLibrary. | |
90 EXPECT_CALL(network_library_, LoadOncNetworks( | |
91 kFakeONC, "", NameToONCSource(GetParam()), _)); | |
92 EXPECT_CALL(network_library_, LoadOncNetworks( | |
93 chromeos::onc::kEmptyUnencryptedConfiguration, | |
94 "", | |
95 Ne(NameToONCSource(GetParam())), | |
96 _)); | |
97 | |
98 EXPECT_CALL(network_library_, RemoveNetworkProfileObserver(_)); | |
99 | |
100 updater.OnUserPolicyInitialized(); | |
101 } | |
102 Mock::VerifyAndClearExpectations(&network_library_); | |
103 } | |
104 | |
105 TEST_P(NetworkConfigurationUpdaterTest, AllowWebTrust) { | |
106 { | |
107 EXPECT_CALL(network_library_, AddNetworkProfileObserver(_)); | |
108 | |
109 // Initially web trust is disabled. | |
110 EXPECT_CALL(network_library_, LoadOncNetworks(_, _, _, false)) | |
111 .Times(AtLeast(0)); | |
112 NetworkConfigurationUpdater updater(policy_service_.get(), | |
113 &network_library_); | |
114 updater.OnUserPolicyInitialized(); | |
115 Mock::VerifyAndClearExpectations(&network_library_); | |
116 | |
117 // Web trust should be forwarded to LoadOncNetworks. | |
118 EXPECT_CALL(network_library_, LoadOncNetworks(_, _, _, true)) | |
119 .Times(AtLeast(0)); | |
120 | |
121 updater.set_allow_web_trust(true); | |
122 | |
123 PolicyMap policy; | |
124 policy.Set(GetParam(), POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | |
125 Value::CreateStringValue(kFakeONC)); | |
126 UpdateProviderPolicy(policy); | |
127 Mock::VerifyAndClearExpectations(&network_library_); | |
128 | |
129 EXPECT_CALL(network_library_, RemoveNetworkProfileObserver(_)); | |
130 } | |
131 Mock::VerifyAndClearExpectations(&network_library_); | |
132 } | |
133 | |
134 TEST_P(NetworkConfigurationUpdaterTest, PolicyChange) { | |
135 { | |
136 EXPECT_CALL(network_library_, AddNetworkProfileObserver(_)); | |
137 | |
138 // Ignore the initial updates. | |
139 EXPECT_CALL(network_library_, LoadOncNetworks(_, _, _, _)) | |
140 .Times(AtLeast(0)); | |
141 NetworkConfigurationUpdater updater(policy_service_.get(), | |
142 &network_library_); | |
143 updater.OnUserPolicyInitialized(); | |
144 Mock::VerifyAndClearExpectations(&network_library_); | |
145 | |
146 // We should update if policy changes. | |
147 EXPECT_CALL(network_library_, LoadOncNetworks( | |
148 kFakeONC, "", NameToONCSource(GetParam()), _)); | |
149 | |
150 // In the current implementation, we always apply both policies. | |
151 EXPECT_CALL(network_library_, LoadOncNetworks( | |
152 chromeos::onc::kEmptyUnencryptedConfiguration, | |
153 "", | |
154 Ne(NameToONCSource(GetParam())), | |
155 _)); | |
156 | |
157 PolicyMap policy; | |
158 policy.Set(GetParam(), POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | |
159 Value::CreateStringValue(kFakeONC)); | |
160 UpdateProviderPolicy(policy); | |
161 Mock::VerifyAndClearExpectations(&network_library_); | |
162 | |
163 // Another update is expected if the policy goes away. In the current | |
164 // implementation, we always apply both policies. | |
165 EXPECT_CALL(network_library_, LoadOncNetworks( | |
166 chromeos::onc::kEmptyUnencryptedConfiguration, "", | |
167 chromeos::onc::ONC_SOURCE_DEVICE_POLICY, _)); | |
168 | |
169 EXPECT_CALL(network_library_, LoadOncNetworks( | |
170 chromeos::onc::kEmptyUnencryptedConfiguration, "", | |
171 chromeos::onc::ONC_SOURCE_USER_POLICY, _)); | |
172 | |
173 EXPECT_CALL(network_library_, RemoveNetworkProfileObserver(_)); | |
174 | |
175 policy.Erase(GetParam()); | |
176 UpdateProviderPolicy(policy); | |
177 } | |
178 Mock::VerifyAndClearExpectations(&network_library_); | |
179 } | |
180 | |
181 INSTANTIATE_TEST_CASE_P( | |
182 NetworkConfigurationUpdaterTestInstance, | |
183 NetworkConfigurationUpdaterTest, | |
184 testing::Values(key::kDeviceOpenNetworkConfiguration, | |
185 key::kOpenNetworkConfiguration)); | |
186 | |
187 } // namespace policy | |
OLD | NEW |