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

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 "content/public/test/test_browser_thread.h"
16 #include "testing/gtest/include/gtest/gtest.h"
17
18 using testing::_;
19 using testing::Invoke;
20 using testing::StrictMock;
21 using testing::WithArgs;
22
23 namespace chromeos {
24 namespace attestation {
25
26 namespace {
27
28 void DBusCallbackFalse(const BoolDBusMethodCallback& callback) {
29 MessageLoop::current()->PostTask(
30 FROM_HERE, base::Bind(callback, DBUS_METHOD_CALL_SUCCESS, false));
31 }
32
33 void DBusCallbackTrue(const BoolDBusMethodCallback& callback) {
34 MessageLoop::current()->PostTask(
35 FROM_HERE, base::Bind(callback, DBUS_METHOD_CALL_SUCCESS, true));
36 }
37
38 void DBusDataCallback(const CryptohomeClient::DataMethodCallback& callback) {
39 MessageLoop::current()->PostTask(
40 FROM_HERE, base::Bind(callback, DBUS_METHOD_CALL_SUCCESS, true, "fake"));
41 }
42
43 void CertCallbackSuccess(const AttestationFlow::CertificateCallback& callback) {
44 MessageLoop::current()->PostTask(
45 FROM_HERE, base::Bind(callback, true, "fake_cert"));
46 }
47
48 } // namespace
49
50 class AttestationPolicyObserverTest : public ::testing::Test {
51 public:
52 AttestationPolicyObserverTest()
53 : message_loop_(MessageLoop::TYPE_UI),
54 ui_thread_(content::BrowserThread::UI, &message_loop_) {
55 // Remove the real DeviceSettingsProvider and replace it with a stub.
56 CrosSettings* cros_settings = CrosSettings::Get();
57 device_settings_provider_ =
58 cros_settings->GetProvider(kDeviceAttestationEnabled);
59 cros_settings->RemoveSettingsProvider(device_settings_provider_);
60 cros_settings->AddSettingsProvider(&stub_settings_provider_);
61 cros_settings->SetBoolean(kDeviceAttestationEnabled, true);
62 policy_client_.SetDMToken("fake_dm_token");
63 }
64
65 virtual ~AttestationPolicyObserverTest() {
66 // Restore the real DeviceSettingsProvider.
67 CrosSettings* cros_settings = CrosSettings::Get();
68 cros_settings->RemoveSettingsProvider(&stub_settings_provider_);
69 cros_settings->AddSettingsProvider(device_settings_provider_);
70 }
71
72 protected:
73 void Run() {
74 AttestationPolicyObserver observer(&policy_client_,
75 &cryptohome_client_,
76 &attestation_flow_);
77 base::RunLoop().RunUntilIdle();
78 }
79
80 MessageLoop message_loop_;
81 content::TestBrowserThread ui_thread_;
82 ScopedTestCrosSettings test_cros_settings_;
83 CrosSettingsProvider* device_settings_provider_;
84 StubCrosSettingsProvider stub_settings_provider_;
85 StrictMock<MockCryptohomeClient> cryptohome_client_;
86 StrictMock<MockAttestationFlow> attestation_flow_;
87 StrictMock<policy::MockCloudPolicyClient> policy_client_;
88 };
89
90 TEST_F(AttestationPolicyObserverTest, FeatureDisabled) {
91 CrosSettings* cros_settings = CrosSettings::Get();
92 cros_settings->SetBoolean(kDeviceAttestationEnabled, false);
93 Run();
94 }
95
96 TEST_F(AttestationPolicyObserverTest, UnregisteredPolicyClient) {
97 policy_client_.SetDMToken("");
98 Run();
99 }
100
101 TEST_F(AttestationPolicyObserverTest, NewCertificate) {
102 EXPECT_CALL(cryptohome_client_, TpmAttestationDoesKeyExist(_, _, _))
103 .WillOnce(WithArgs<2>(Invoke(DBusCallbackFalse)));
104 EXPECT_CALL(attestation_flow_, GetCertificate(_, _))
105 .WillOnce(WithArgs<1>(Invoke(CertCallbackSuccess)));
106 Run();
107 }
108
109 TEST_F(AttestationPolicyObserverTest, KeyExists) {
110 EXPECT_CALL(cryptohome_client_, TpmAttestationDoesKeyExist(_, _, _))
111 .WillOnce(WithArgs<2>(Invoke(DBusCallbackTrue)));
112 EXPECT_CALL(cryptohome_client_, TpmAttestationGetCertificate(_, _, _))
113 .WillOnce(WithArgs<2>(Invoke(DBusDataCallback)));
114 Run();
115 }
116
117 } // namespace attestation
118 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698