OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2013 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 "base/bind.h" | |
6 #include "base/message_loop.h" | |
7 #include "base/run_loop.h" | |
8 #include "chrome/browser/chromeos/attestation/attestation_policy_observer.h" | |
9 #include "chrome/browser/chromeos/settings/cros_settings.h" | |
10 #include "chrome/browser/chromeos/settings/cros_settings_names.h" | |
11 #include "chrome/browser/chromeos/settings/stub_cros_settings_provider.h" | |
12 #include "chrome/browser/policy/cloud/mock_cloud_policy_client.h" | |
13 #include "chromeos/attestation/mock_attestation_flow.h" | |
14 #include "chromeos/dbus/mock_cryptohome_client.h" | |
15 #include "testing/gtest/include/gtest/gtest.h" | |
16 | |
17 using testing::_; | |
18 using testing::Invoke; | |
19 using testing::StrictMock; | |
20 using testing::WithArgs; | |
21 | |
22 namespace chromeos { | |
23 namespace attestation { | |
24 | |
25 namespace { | |
26 | |
27 void DBusCallbackFalse(const BoolDBusMethodCallback& callback) { | |
28 MessageLoop::current()->PostTask( | |
29 FROM_HERE, base::Bind(callback, DBUS_METHOD_CALL_SUCCESS, false)); | |
30 } | |
31 | |
32 void DBusCallbackTrue(const BoolDBusMethodCallback& callback) { | |
33 MessageLoop::current()->PostTask( | |
34 FROM_HERE, base::Bind(callback, DBUS_METHOD_CALL_SUCCESS, true)); | |
35 } | |
36 | |
37 void DBusDataCallback(const CryptohomeClient::DataMethodCallback& callback) { | |
38 MessageLoop::current()->PostTask( | |
39 FROM_HERE, base::Bind(callback, DBUS_METHOD_CALL_SUCCESS, true, "fake")); | |
40 } | |
41 | |
42 void CertCallbackSuccess(const AttestationFlow::CertificateCallback& callback) { | |
43 MessageLoop::current()->PostTask( | |
44 FROM_HERE, base::Bind(callback, true, "fake_cert")); | |
45 } | |
46 | |
47 } // namespace | |
48 | |
49 class AttestationPolicyObserverTest : public ::testing::Test { | |
50 public: | |
51 AttestationPolicyObserverTest() { | |
52 // Remove the real DeviceSettingsProvider and replace it with a stub. | |
53 CrosSettings* cros_settings = CrosSettings::Get(); | |
54 device_settings_provider_ = | |
55 cros_settings->GetProvider(kDeviceAttestationEnabled); | |
56 cros_settings->RemoveSettingsProvider(device_settings_provider_); | |
57 cros_settings->AddSettingsProvider(&stub_settings_provider_); | |
Mattias Nissler (ping if slow)
2013/04/10 17:31:03
Ah, CrosSettings mocking... sucks. I wish we had a
dkrahn
2013/04/12 01:17:29
+1, I was surprised to see this but it gets the jo
| |
58 cros_settings->SetBoolean(kDeviceAttestationEnabled, true); | |
59 policy_client_.SetDMToken("fake_dm_token"); | |
60 observer_.set_cryptohome_client(&cryptohome_client_); | |
61 observer_.set_attestation_flow(&attestation_flow_); | |
62 } | |
63 | |
64 virtual ~AttestationPolicyObserverTest() { | |
65 // Restore the real DeviceSettingsProvider. | |
66 CrosSettings* cros_settings = CrosSettings::Get(); | |
67 cros_settings->RemoveSettingsProvider(&stub_settings_provider_); | |
68 cros_settings->AddSettingsProvider(device_settings_provider_); | |
69 } | |
70 | |
71 protected: | |
72 void Run() { | |
73 base::RunLoop run_loop; | |
74 run_loop.RunUntilIdle(); | |
Mattias Nissler (ping if slow)
2013/04/10 17:31:03
You can just do base::RunLoop().RunUntilIdle() in
dkrahn
2013/04/12 01:17:29
Done.
| |
75 } | |
76 | |
77 MessageLoop message_loop_; | |
78 AttestationPolicyObserver observer_; | |
79 CrosSettingsProvider* device_settings_provider_; | |
80 StubCrosSettingsProvider stub_settings_provider_; | |
81 StrictMock<MockCryptohomeClient> cryptohome_client_; | |
82 StrictMock<MockAttestationFlow> attestation_flow_; | |
83 StrictMock<policy::MockCloudPolicyClient> policy_client_; | |
84 }; | |
85 | |
86 TEST_F(AttestationPolicyObserverTest, FeatureDisabled) { | |
87 CrosSettings* cros_settings = CrosSettings::Get(); | |
88 cros_settings->SetBoolean(kDeviceAttestationEnabled, false); | |
89 observer_.Connect(&policy_client_); | |
90 Run(); | |
91 } | |
92 | |
93 TEST_F(AttestationPolicyObserverTest, UnregisteredPolicyClient) { | |
94 policy_client_.SetDMToken(""); | |
95 observer_.Connect(&policy_client_); | |
96 Run(); | |
97 } | |
98 | |
99 TEST_F(AttestationPolicyObserverTest, NewCertificate) { | |
100 EXPECT_CALL(cryptohome_client_, TpmAttestationDoesKeyExist(_, _, _)) | |
101 .WillOnce(WithArgs<2>(Invoke(DBusCallbackFalse))); | |
102 EXPECT_CALL(attestation_flow_, GetCertificate(_, _)) | |
103 .WillOnce(WithArgs<1>(Invoke(CertCallbackSuccess))); | |
104 observer_.Connect(&policy_client_); | |
105 Run(); | |
106 } | |
107 | |
108 TEST_F(AttestationPolicyObserverTest, KeyExists) { | |
109 EXPECT_CALL(cryptohome_client_, TpmAttestationDoesKeyExist(_, _, _)) | |
110 .WillOnce(WithArgs<2>(Invoke(DBusCallbackTrue))); | |
111 EXPECT_CALL(cryptohome_client_, TpmAttestationGetCertificate(_, _, _)) | |
112 .WillOnce(WithArgs<2>(Invoke(DBusDataCallback))); | |
113 observer_.Connect(&policy_client_); | |
114 Run(); | |
115 } | |
116 | |
117 } // namespace attestation | |
118 } // namespace chromeos | |
OLD | NEW |