Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(31)

Side by Side Diff: chrome/browser/chromeos/attestation/attestation_policy_observer_unittest.cc

Issue 12556004: Created AttestationPolicyObserver. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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_);
58 cros_settings->SetBoolean(kDeviceAttestationEnabled, true);
59 policy_client_.SetDMToken("fake_dm_token");
60 }
61
62 virtual ~AttestationPolicyObserverTest() {
63 // Restore the real DeviceSettingsProvider.
64 CrosSettings* cros_settings = CrosSettings::Get();
65 cros_settings->RemoveSettingsProvider(&stub_settings_provider_);
66 cros_settings->AddSettingsProvider(device_settings_provider_);
67 }
68
69 protected:
70 void Run() {
71 observer_.reset(new AttestationPolicyObserver(&policy_client_,
Mattias Nissler (ping if slow) 2013/04/12 12:51:43 nit: There's no need to make observer_ a class mem
dkrahn 2013/04/12 21:12:20 Done.
72 &cryptohome_client_,
73 &attestation_flow_));
74 base::RunLoop().RunUntilIdle();
75 }
76
77 MessageLoop message_loop_;
78 scoped_ptr<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 Run();
90 }
91
92 TEST_F(AttestationPolicyObserverTest, UnregisteredPolicyClient) {
93 policy_client_.SetDMToken("");
94 Run();
95 }
96
97 TEST_F(AttestationPolicyObserverTest, NewCertificate) {
98 EXPECT_CALL(cryptohome_client_, TpmAttestationDoesKeyExist(_, _, _))
99 .WillOnce(WithArgs<2>(Invoke(DBusCallbackFalse)));
100 EXPECT_CALL(attestation_flow_, GetCertificate(_, _))
101 .WillOnce(WithArgs<1>(Invoke(CertCallbackSuccess)));
102 Run();
103 }
104
105 TEST_F(AttestationPolicyObserverTest, KeyExists) {
106 EXPECT_CALL(cryptohome_client_, TpmAttestationDoesKeyExist(_, _, _))
107 .WillOnce(WithArgs<2>(Invoke(DBusCallbackTrue)));
108 EXPECT_CALL(cryptohome_client_, TpmAttestationGetCertificate(_, _, _))
109 .WillOnce(WithArgs<2>(Invoke(DBusDataCallback)));
110 Run();
111 }
112
113 } // namespace attestation
114 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698