tpm_ownership_dbus_proxy_test.cc revision 9cbe78de6c99f9e31643eb32b6ad4fbe75fd30ba
1//
2// Copyright (C) 2015 The Android Open Source Project
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8//      http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15//
16
17#include <string>
18
19#include <brillo/bind_lambda.h>
20#include <dbus/mock_object_proxy.h>
21#include <gmock/gmock.h>
22#include <gtest/gtest.h>
23
24#include "tpm_manager/client/tpm_ownership_dbus_proxy.h"
25
26using testing::_;
27using testing::Invoke;
28using testing::StrictMock;
29using testing::WithArgs;
30
31namespace tpm_manager {
32
33class TpmOwnershipDBusProxyTest : public testing::Test {
34 public:
35  ~TpmOwnershipDBusProxyTest() override = default;
36  void SetUp() override {
37    mock_object_proxy_ = new StrictMock<dbus::MockObjectProxy>(
38        nullptr, "", dbus::ObjectPath(""));
39    proxy_.set_object_proxy(mock_object_proxy_.get());
40  }
41
42 protected:
43  scoped_refptr<StrictMock<dbus::MockObjectProxy>> mock_object_proxy_;
44  TpmOwnershipDBusProxy proxy_;
45};
46
47TEST_F(TpmOwnershipDBusProxyTest, GetTpmStatus) {
48  auto fake_dbus_call = [](
49      dbus::MethodCall* method_call,
50      const dbus::MockObjectProxy::ResponseCallback& response_callback) {
51    // Verify request protobuf.
52    dbus::MessageReader reader(method_call);
53    GetTpmStatusRequest request;
54    EXPECT_TRUE(reader.PopArrayOfBytesAsProto(&request));
55    // Create reply protobuf.
56    auto response = dbus::Response::CreateEmpty();
57    dbus::MessageWriter writer(response.get());
58    GetTpmStatusReply reply;
59    reply.set_status(STATUS_SUCCESS);
60    reply.set_enabled(true);
61    reply.set_owned(true);
62    reply.set_dictionary_attack_counter(3);
63    reply.set_dictionary_attack_threshold(4);
64    reply.set_dictionary_attack_lockout_in_effect(true);
65    reply.set_dictionary_attack_lockout_seconds_remaining(5);
66    writer.AppendProtoAsArrayOfBytes(reply);
67    response_callback.Run(response.release());
68  };
69  EXPECT_CALL(*mock_object_proxy_, CallMethodWithErrorCallback(_, _, _, _))
70      .WillOnce(WithArgs<0, 2>(Invoke(fake_dbus_call)));
71
72  // Set expectations on the outputs.
73  int callback_count = 0;
74  auto callback = [&callback_count](const GetTpmStatusReply& reply) {
75    callback_count++;
76    EXPECT_EQ(STATUS_SUCCESS, reply.status());
77    EXPECT_TRUE(reply.enabled());
78    EXPECT_TRUE(reply.owned());
79    EXPECT_EQ(3, reply.dictionary_attack_counter());
80    EXPECT_EQ(4, reply.dictionary_attack_threshold());
81    EXPECT_TRUE(reply.dictionary_attack_lockout_in_effect());
82    EXPECT_EQ(5, reply.dictionary_attack_lockout_seconds_remaining());
83  };
84  GetTpmStatusRequest request;
85  proxy_.GetTpmStatus(request, base::Bind(callback));
86  EXPECT_EQ(1, callback_count);
87}
88
89TEST_F(TpmOwnershipDBusProxyTest, TakeOwnership) {
90  auto fake_dbus_call = [](
91      dbus::MethodCall* method_call,
92      const dbus::MockObjectProxy::ResponseCallback& response_callback) {
93    // Verify request protobuf.
94    dbus::MessageReader reader(method_call);
95    TakeOwnershipRequest request;
96    EXPECT_TRUE(reader.PopArrayOfBytesAsProto(&request));
97    // Create reply protobuf.
98    auto response = dbus::Response::CreateEmpty();
99    dbus::MessageWriter writer(response.get());
100    TakeOwnershipReply reply;
101    reply.set_status(STATUS_SUCCESS);
102    writer.AppendProtoAsArrayOfBytes(reply);
103    response_callback.Run(response.release());
104  };
105  EXPECT_CALL(*mock_object_proxy_, CallMethodWithErrorCallback(_, _, _, _))
106      .WillOnce(WithArgs<0, 2>(Invoke(fake_dbus_call)));
107
108  // Set expectations on the outputs.
109  int callback_count = 0;
110  auto callback = [&callback_count](const TakeOwnershipReply& reply) {
111    callback_count++;
112    EXPECT_EQ(STATUS_SUCCESS, reply.status());
113  };
114  TakeOwnershipRequest request;
115  proxy_.TakeOwnership(request, base::Bind(callback));
116  EXPECT_EQ(1, callback_count);
117}
118
119TEST_F(TpmOwnershipDBusProxyTest, RemoveOwnerDependency) {
120  const std::string owner_dependency("owner");
121  auto fake_dbus_call = [&owner_dependency](
122      dbus::MethodCall* method_call,
123      const dbus::MockObjectProxy::ResponseCallback& response_callback) {
124    // Verify request protobuf.
125    dbus::MessageReader reader(method_call);
126    RemoveOwnerDependencyRequest request;
127    EXPECT_TRUE(reader.PopArrayOfBytesAsProto(&request));
128    EXPECT_TRUE(request.has_owner_dependency());
129    EXPECT_EQ(owner_dependency, request.owner_dependency());
130    // Create reply protobuf.
131    auto response = dbus::Response::CreateEmpty();
132    dbus::MessageWriter writer(response.get());
133    RemoveOwnerDependencyReply reply;
134    reply.set_status(STATUS_SUCCESS);
135    writer.AppendProtoAsArrayOfBytes(reply);
136    response_callback.Run(response.release());
137  };
138  EXPECT_CALL(*mock_object_proxy_, CallMethodWithErrorCallback(_, _, _, _))
139      .WillOnce(WithArgs<0, 2>(Invoke(fake_dbus_call)));
140
141  // Set expectations on the outputs.
142  int callback_count = 0;
143  auto callback = [&callback_count](const RemoveOwnerDependencyReply& reply) {
144    callback_count++;
145    EXPECT_EQ(STATUS_SUCCESS, reply.status());
146  };
147  RemoveOwnerDependencyRequest request;
148  request.set_owner_dependency(owner_dependency);
149  proxy_.RemoveOwnerDependency(request, base::Bind(callback));
150  EXPECT_EQ(1, callback_count);
151}
152
153}  // namespace tpm_manager
154