OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/chromeos/login/signed_settings_helper.h" | 5 #include "chrome/browser/chromeos/login/signed_settings_helper.h" |
6 | 6 |
| 7 #include "base/bind.h" |
7 #include "base/message_loop.h" | 8 #include "base/message_loop.h" |
8 #include "chrome/browser/chromeos/cros/cros_library.h" | 9 #include "chrome/browser/chromeos/cros/cros_library.h" |
9 #include "chrome/browser/chromeos/cros_settings_names.h" | 10 #include "chrome/browser/chromeos/cros_settings_names.h" |
10 #include "chrome/browser/chromeos/dbus/dbus_thread_manager.h" | 11 #include "chrome/browser/chromeos/dbus/dbus_thread_manager.h" |
| 12 #include "chrome/browser/chromeos/dbus/mock_dbus_thread_manager.h" |
| 13 #include "chrome/browser/chromeos/dbus/mock_session_manager_client.h" |
| 14 #include "chrome/browser/chromeos/login/mock_owner_key_utils.h" |
11 #include "chrome/browser/chromeos/login/mock_ownership_service.h" | 15 #include "chrome/browser/chromeos/login/mock_ownership_service.h" |
12 #include "chrome/browser/chromeos/login/owner_manager.h" | 16 #include "chrome/browser/chromeos/login/owner_manager.h" |
13 #include "chrome/browser/chromeos/login/signed_settings.h" | 17 #include "chrome/browser/chromeos/login/signed_settings.h" |
14 #include "chrome/browser/policy/proto/chrome_device_policy.pb.h" | 18 #include "chrome/browser/policy/proto/chrome_device_policy.pb.h" |
15 #include "chrome/browser/policy/proto/device_management_backend.pb.h" | 19 #include "chrome/browser/policy/proto/device_management_backend.pb.h" |
16 #include "content/test/test_browser_thread.h" | 20 #include "content/test/test_browser_thread.h" |
17 #include "testing/gmock/include/gmock/gmock.h" | 21 #include "testing/gmock/include/gmock/gmock.h" |
18 #include "testing/gtest/include/gtest/gtest.h" | 22 #include "testing/gtest/include/gtest/gtest.h" |
19 | 23 |
20 using content::BrowserThread; | |
21 using ::testing::_; | 24 using ::testing::_; |
22 using ::testing::A; | 25 using ::testing::A; |
23 using ::testing::AtLeast; | |
24 using ::testing::InSequence; | 26 using ::testing::InSequence; |
25 using ::testing::Invoke; | 27 using ::testing::Invoke; |
26 using ::testing::Return; | |
27 using ::testing::ReturnRef; | |
28 using ::testing::SaveArg; | |
29 using ::testing::WithArg; | 28 using ::testing::WithArg; |
30 | 29 |
31 namespace em = enterprise_management; | 30 namespace em = enterprise_management; |
32 namespace chromeos { | 31 namespace chromeos { |
33 | 32 |
34 class MockSignedSettingsHelperCallback : public SignedSettingsHelper::Callback { | 33 ACTION_P(Retrieve, policy_blob) { arg0.Run(policy_blob); } |
35 public: | 34 ACTION_P(Store, success) { arg1.Run(success); } |
36 virtual ~MockSignedSettingsHelperCallback() {} | |
37 | |
38 MOCK_METHOD3(OnStorePropertyCompleted, void( | |
39 SignedSettings::ReturnCode code, | |
40 const std::string& name, | |
41 const base::Value& value)); | |
42 MOCK_METHOD3(OnRetrievePropertyCompleted, void( | |
43 SignedSettings::ReturnCode code, | |
44 const std::string& name, | |
45 const base::Value* value)); | |
46 }; | |
47 | 35 |
48 class SignedSettingsHelperTest : public testing::Test, | 36 class SignedSettingsHelperTest : public testing::Test, |
49 public SignedSettingsHelper::TestDelegate { | 37 public SignedSettingsHelper::TestDelegate { |
50 public: | 38 public: |
51 SignedSettingsHelperTest() | 39 SignedSettingsHelperTest() |
52 : fake_email_("fakey@example.com"), | 40 : message_loop_(MessageLoop::TYPE_UI), |
53 fake_prop_(kReleaseChannel), | 41 ui_thread_(content::BrowserThread::UI, &message_loop_), |
54 fake_value_("false"), | 42 file_thread_(content::BrowserThread::FILE, &message_loop_), |
55 message_loop_(MessageLoop::TYPE_UI), | 43 pending_ops_(0), |
56 ui_thread_(BrowserThread::UI, &message_loop_), | 44 mock_dbus_thread_manager_(new MockDBusThreadManager) { |
57 file_thread_(BrowserThread::FILE), | |
58 pending_ops_(0) { | |
59 } | 45 } |
60 | 46 |
61 virtual void SetUp() { | 47 virtual void SetUp() { |
62 file_thread_.Start(); | |
63 SignedSettingsHelper::Get()->set_test_delegate(this); | 48 SignedSettingsHelper::Get()->set_test_delegate(this); |
64 DBusThreadManager::Initialize(); | 49 DBusThreadManager::InitializeForTesting(mock_dbus_thread_manager_); |
| 50 |
| 51 fake_policy_data_ = BuildPolicyData(); |
| 52 std::string data_serialized = fake_policy_data_.SerializeAsString(); |
| 53 std::string serialized_policy_; |
| 54 fake_policy_ = BuildProto(data_serialized, |
| 55 std::string("false"), |
| 56 &serialized_policy_); |
| 57 |
| 58 MockSessionManagerClient* client = |
| 59 mock_dbus_thread_manager_->mock_session_manager_client(); |
| 60 // Make sure the mocked out class calls back to notify success on store and |
| 61 // retrieve ops. |
| 62 EXPECT_CALL(*client, StorePolicy(_, _)) |
| 63 .WillRepeatedly(Store(true)); |
| 64 EXPECT_CALL(*client, RetrievePolicy(_)) |
| 65 .WillRepeatedly(Retrieve(serialized_policy_)); |
| 66 |
| 67 EXPECT_CALL(m_, StartSigningAttempt(_, A<OwnerManager::Delegate*>())) |
| 68 .WillRepeatedly(WithArg<1>( |
| 69 Invoke(&SignedSettingsHelperTest::OnKeyOpComplete))); |
| 70 EXPECT_CALL(m_, StartVerifyAttempt(_, _, A<OwnerManager::Delegate*>())) |
| 71 .WillRepeatedly(WithArg<2>( |
| 72 Invoke(&SignedSettingsHelperTest::OnKeyOpComplete))); |
65 } | 73 } |
66 | 74 |
67 virtual void TearDown() { | 75 virtual void TearDown() { |
68 DBusThreadManager::Shutdown(); | 76 DBusThreadManager::Shutdown(); |
69 SignedSettingsHelper::Get()->set_test_delegate(NULL); | 77 SignedSettingsHelper::Get()->set_test_delegate(NULL); |
70 } | 78 } |
71 | 79 |
72 virtual void OnOpCreated(SignedSettings* op) { | 80 virtual void OnOpCreated(SignedSettings* op) { |
73 // Use MockOwnershipService for all SignedSettings op. | 81 // Use MockOwnershipService for all SignedSettings op. |
74 op->set_service(&m_); | 82 op->set_service(&m_); |
75 } | 83 } |
76 | 84 |
77 virtual void OnOpStarted(SignedSettings* op) { | 85 virtual void OnOpStarted(SignedSettings* op) { |
78 } | 86 } |
79 | 87 |
80 virtual void OnOpCompleted(SignedSettings* op) { | 88 virtual void OnOpCompleted(SignedSettings* op) { |
81 --pending_ops_; | 89 --pending_ops_; |
82 if (!pending_ops_) | |
83 MessageLoop::current()->Quit(); | |
84 } | 90 } |
85 | 91 |
86 static void OnKeyOpComplete(OwnerManager::Delegate* op) { | 92 static void OnKeyOpComplete(OwnerManager::Delegate* op) { |
87 op->OnKeyOpComplete(OwnerManager::SUCCESS, std::vector<uint8>()); | 93 op->OnKeyOpComplete(OwnerManager::SUCCESS, std::vector<uint8>()); |
88 } | 94 } |
89 | 95 |
90 em::PolicyData BuildPolicyData() { | 96 em::PolicyData BuildPolicyData() { |
91 em::PolicyData to_return; | 97 em::PolicyData to_return; |
92 em::ChromeDeviceSettingsProto pol; | 98 em::ChromeDeviceSettingsProto pol; |
93 to_return.set_policy_type(SignedSettings::kDevicePolicyType); | 99 to_return.set_policy_type(chromeos::kDevicePolicyType); |
94 to_return.set_policy_value(pol.SerializeAsString()); | 100 to_return.set_policy_value(pol.SerializeAsString()); |
95 return to_return; | 101 return to_return; |
96 } | 102 } |
97 | 103 |
98 const std::string fake_email_; | 104 em::PolicyFetchResponse BuildProto(const std::string& data, |
99 const std::string fake_prop_; | 105 const std::string& sig, |
100 const base::StringValue fake_value_; | 106 std::string* out_serialized) { |
| 107 em::PolicyFetchResponse fake_policy; |
| 108 if (!data.empty()) |
| 109 fake_policy.set_policy_data(data); |
| 110 if (!sig.empty()) |
| 111 fake_policy.set_policy_data_signature(sig); |
| 112 EXPECT_TRUE(fake_policy.SerializeToString(out_serialized)); |
| 113 return fake_policy; |
| 114 } |
| 115 |
| 116 em::PolicyData fake_policy_data_; |
| 117 em::PolicyFetchResponse fake_policy_; |
| 118 std::string serialized_policy_; |
101 MockOwnershipService m_; | 119 MockOwnershipService m_; |
102 | 120 |
103 MessageLoop message_loop_; | 121 MessageLoop message_loop_; |
104 content::TestBrowserThread ui_thread_; | 122 content::TestBrowserThread ui_thread_; |
105 content::TestBrowserThread file_thread_; | 123 content::TestBrowserThread file_thread_; |
106 | 124 |
107 int pending_ops_; | 125 int pending_ops_; |
108 | 126 |
| 127 MockDBusThreadManager* mock_dbus_thread_manager_; |
| 128 |
109 ScopedStubCrosEnabler stub_cros_enabler_; | 129 ScopedStubCrosEnabler stub_cros_enabler_; |
110 }; | 130 }; |
111 | 131 |
| 132 class SignedSettingsCallbacks { |
| 133 public: |
| 134 virtual ~SignedSettingsCallbacks() {} |
| 135 // Callback of StorePolicyOp. |
| 136 virtual void OnStorePolicyCompleted(SignedSettings::ReturnCode code) = 0; |
| 137 // Callback of RetrievePolicyOp. |
| 138 virtual void OnRetrievePolicyCompleted( |
| 139 SignedSettings::ReturnCode code, |
| 140 const em::PolicyFetchResponse& policy) = 0; |
| 141 }; |
| 142 |
| 143 class MockSignedSettingsCallbacks |
| 144 : public SignedSettingsCallbacks, |
| 145 public base::SupportsWeakPtr<MockSignedSettingsCallbacks> { |
| 146 public: |
| 147 virtual ~MockSignedSettingsCallbacks() {} |
| 148 |
| 149 MOCK_METHOD1(OnStorePolicyCompleted, void(SignedSettings::ReturnCode)); |
| 150 MOCK_METHOD2(OnRetrievePolicyCompleted, void(SignedSettings::ReturnCode, |
| 151 const em::PolicyFetchResponse&)); |
| 152 }; |
| 153 |
112 TEST_F(SignedSettingsHelperTest, SerializedOps) { | 154 TEST_F(SignedSettingsHelperTest, SerializedOps) { |
113 MockSignedSettingsHelperCallback cb; | 155 MockSignedSettingsCallbacks cb; |
114 | |
115 EXPECT_CALL(m_, GetStatus(_)) | |
116 .Times(2) | |
117 .WillRepeatedly(Return(OwnershipService::OWNERSHIP_TAKEN)); | |
118 EXPECT_CALL(m_, has_cached_policy()) | |
119 .Times(2) | |
120 .WillRepeatedly(Return(true)); | |
121 em::PolicyData fake_pol = BuildPolicyData(); | |
122 EXPECT_CALL(m_, cached_policy()) | |
123 .Times(2) | |
124 .WillRepeatedly(ReturnRef(fake_pol)); | |
125 EXPECT_CALL(m_, set_cached_policy(A<const em::PolicyData&>())) | |
126 .Times(1) | |
127 .WillRepeatedly(SaveArg<0>(&fake_pol)); | |
128 | 156 |
129 InSequence s; | 157 InSequence s; |
130 EXPECT_CALL(m_, StartSigningAttempt(_, A<OwnerManager::Delegate*>())) | 158 EXPECT_CALL(cb, OnStorePolicyCompleted(SignedSettings::SUCCESS)) |
131 .WillOnce(WithArg<1>(Invoke(&SignedSettingsHelperTest::OnKeyOpComplete))); | |
132 EXPECT_CALL(cb, OnStorePropertyCompleted(SignedSettings::SUCCESS, _, _)) | |
133 .Times(1); | 159 .Times(1); |
134 | 160 EXPECT_CALL(cb, OnRetrievePolicyCompleted(SignedSettings::SUCCESS, _)) |
135 EXPECT_CALL(cb, OnRetrievePropertyCompleted(SignedSettings::SUCCESS, _, _)) | |
136 .Times(1); | 161 .Times(1); |
137 | 162 |
138 | 163 |
139 pending_ops_ = 2; | 164 pending_ops_ = 2; |
140 SignedSettingsHelper::Get()->StartStorePropertyOp(fake_prop_, fake_value_, | 165 SignedSettingsHelper::Get()->StartStorePolicyOp( |
141 &cb); | 166 fake_policy_, |
142 SignedSettingsHelper::Get()->StartRetrieveProperty(fake_prop_, &cb); | 167 base::Bind(&MockSignedSettingsCallbacks::OnStorePolicyCompleted, |
| 168 base::Unretained(&cb))); |
| 169 SignedSettingsHelper::Get()->StartRetrievePolicyOp( |
| 170 base::Bind(&MockSignedSettingsCallbacks::OnRetrievePolicyCompleted, |
| 171 base::Unretained(&cb))); |
143 | 172 |
144 message_loop_.Run(); | 173 message_loop_.RunAllPending(); |
| 174 ASSERT_EQ(0, pending_ops_); |
145 } | 175 } |
146 | 176 |
147 TEST_F(SignedSettingsHelperTest, CanceledOps) { | 177 TEST_F(SignedSettingsHelperTest, CanceledOps) { |
148 MockSignedSettingsHelperCallback cb; | 178 MockSignedSettingsCallbacks cb; |
149 | |
150 EXPECT_CALL(m_, GetStatus(_)) | |
151 .Times(3) | |
152 .WillRepeatedly(Return(OwnershipService::OWNERSHIP_TAKEN)); | |
153 EXPECT_CALL(m_, has_cached_policy()) | |
154 .Times(3) | |
155 .WillRepeatedly(Return(true)); | |
156 em::PolicyData fake_pol = BuildPolicyData(); | |
157 EXPECT_CALL(m_, cached_policy()) | |
158 .Times(3) | |
159 .WillRepeatedly(ReturnRef(fake_pol)); | |
160 EXPECT_CALL(m_, set_cached_policy(A<const em::PolicyData&>())) | |
161 .Times(1) | |
162 .WillRepeatedly(SaveArg<0>(&fake_pol)); | |
163 | 179 |
164 InSequence s; | 180 InSequence s; |
165 | 181 EXPECT_CALL(cb, OnStorePolicyCompleted(SignedSettings::SUCCESS)) |
166 // RetrievePropertyOp for cb_to_be_canceled still gets executed but callback | |
167 // does not happen. | |
168 EXPECT_CALL(m_, StartSigningAttempt(_, A<OwnerManager::Delegate*>())) | |
169 .WillOnce(WithArg<1>(Invoke(&SignedSettingsHelperTest::OnKeyOpComplete))); | |
170 EXPECT_CALL(cb, OnStorePropertyCompleted(SignedSettings::SUCCESS, _, _)) | |
171 .Times(1); | 182 .Times(1); |
172 EXPECT_CALL(cb, OnRetrievePropertyCompleted(SignedSettings::SUCCESS, _, _)) | 183 EXPECT_CALL(cb, OnRetrievePolicyCompleted(SignedSettings::SUCCESS, _)) |
173 .Times(1); | 184 .Times(1); |
174 | 185 |
175 pending_ops_ = 3; | 186 pending_ops_ = 3; |
176 MockSignedSettingsHelperCallback cb_to_be_canceled; | |
177 SignedSettingsHelper::Get()->StartRetrieveProperty(fake_prop_, | |
178 &cb_to_be_canceled); | |
179 SignedSettingsHelper::Get()->CancelCallback(&cb_to_be_canceled); | |
180 | 187 |
181 SignedSettingsHelper::Get()->StartStorePropertyOp(fake_prop_, fake_value_, | 188 { |
182 &cb); | 189 // This op will be deleted and never will be executed (expect only one call |
183 SignedSettingsHelper::Get()->StartRetrieveProperty(fake_prop_, &cb); | 190 // to OnRetrievePolicyCompleted above). However the OpComplete callback will |
| 191 // be still called, therefore we expect three pending ops. |
| 192 MockSignedSettingsCallbacks cb_to_be_deleted; |
| 193 SignedSettingsHelper::Get()->StartRetrievePolicyOp( |
| 194 base::Bind(&MockSignedSettingsCallbacks::OnRetrievePolicyCompleted, |
| 195 cb_to_be_deleted.AsWeakPtr())); |
| 196 } |
| 197 SignedSettingsHelper::Get()->StartStorePolicyOp( |
| 198 fake_policy_, |
| 199 base::Bind(&MockSignedSettingsCallbacks::OnStorePolicyCompleted, |
| 200 base::Unretained(&cb))); |
| 201 SignedSettingsHelper::Get()->StartRetrievePolicyOp( |
| 202 base::Bind(&MockSignedSettingsCallbacks::OnRetrievePolicyCompleted, |
| 203 base::Unretained(&cb))); |
184 | 204 |
185 message_loop_.Run(); | 205 message_loop_.RunAllPending(); |
| 206 ASSERT_EQ(0, pending_ops_); |
186 } | 207 } |
187 | 208 |
188 } // namespace chromeos | 209 } // namespace chromeos |
OLD | NEW |