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_nvram_dbus_proxy.h"
25
26using testing::_;
27using testing::Invoke;
28using testing::StrictMock;
29using testing::WithArgs;
30
31namespace tpm_manager {
32
33class TpmNvramDBusProxyTest : public testing::Test {
34 public:
35  ~TpmNvramDBusProxyTest() 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  TpmNvramDBusProxy proxy_;
45};
46
47TEST_F(TpmNvramDBusProxyTest, DefineNvram) {
48  uint32_t nvram_index = 5;
49  size_t nvram_length = 32;
50  auto fake_dbus_call = [nvram_index, nvram_length](
51      dbus::MethodCall* method_call,
52      const dbus::MockObjectProxy::ResponseCallback& response_callback) {
53    // Verify request protobuf.
54    dbus::MessageReader reader(method_call);
55    DefineNvramRequest request;
56    EXPECT_TRUE(reader.PopArrayOfBytesAsProto(&request));
57    EXPECT_TRUE(request.has_index());
58    EXPECT_EQ(nvram_index, request.index());
59    EXPECT_TRUE(request.has_length());
60    EXPECT_EQ(nvram_length, request.length());
61    // Create reply protobuf.
62    auto response = dbus::Response::CreateEmpty();
63    dbus::MessageWriter writer(response.get());
64    DefineNvramReply reply;
65    reply.set_status(STATUS_SUCCESS);
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  // Set expectations on the outputs.
72  int callback_count = 0;
73  auto callback = [&callback_count](const DefineNvramReply& reply) {
74    callback_count++;
75    EXPECT_EQ(STATUS_SUCCESS, reply.status());
76  };
77  DefineNvramRequest request;
78  request.set_index(nvram_index);
79  request.set_length(nvram_length);
80  proxy_.DefineNvram(request, base::Bind(callback));
81  EXPECT_EQ(1, callback_count);
82}
83
84TEST_F(TpmNvramDBusProxyTest, DestroyNvram) {
85  uint32_t nvram_index = 5;
86  auto fake_dbus_call = [nvram_index](
87      dbus::MethodCall* method_call,
88      const dbus::MockObjectProxy::ResponseCallback& response_callback) {
89    // Verify request protobuf.
90    dbus::MessageReader reader(method_call);
91    DestroyNvramRequest request;
92    EXPECT_TRUE(reader.PopArrayOfBytesAsProto(&request));
93    EXPECT_TRUE(request.has_index());
94    EXPECT_EQ(nvram_index, request.index());
95    // Create reply protobuf.
96    auto response = dbus::Response::CreateEmpty();
97    dbus::MessageWriter writer(response.get());
98    DestroyNvramReply reply;
99    reply.set_status(STATUS_SUCCESS);
100    writer.AppendProtoAsArrayOfBytes(reply);
101    response_callback.Run(response.release());
102  };
103  EXPECT_CALL(*mock_object_proxy_, CallMethodWithErrorCallback(_, _, _, _))
104      .WillOnce(WithArgs<0, 2>(Invoke(fake_dbus_call)));
105  // Set expectations on the outputs.
106  int callback_count = 0;
107  auto callback = [&callback_count](const DestroyNvramReply& reply) {
108    callback_count++;
109    EXPECT_EQ(STATUS_SUCCESS, reply.status());
110  };
111  DestroyNvramRequest request;
112  request.set_index(nvram_index);
113  proxy_.DestroyNvram(request, base::Bind(callback));
114  EXPECT_EQ(1, callback_count);
115}
116TEST_F(TpmNvramDBusProxyTest, WriteNvram) {
117  uint32_t nvram_index = 5;
118  std::string nvram_data("nvram_data");
119  auto fake_dbus_call = [nvram_index, nvram_data](
120      dbus::MethodCall* method_call,
121      const dbus::MockObjectProxy::ResponseCallback& response_callback) {
122    // Verify request protobuf.
123    dbus::MessageReader reader(method_call);
124    WriteNvramRequest request;
125    EXPECT_TRUE(reader.PopArrayOfBytesAsProto(&request));
126    EXPECT_TRUE(request.has_index());
127    EXPECT_EQ(nvram_index, request.index());
128    EXPECT_TRUE(request.has_data());
129    EXPECT_EQ(nvram_data, request.data());
130    // Create reply protobuf.
131    auto response = dbus::Response::CreateEmpty();
132    dbus::MessageWriter writer(response.get());
133    WriteNvramReply 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  // Set expectations on the outputs.
141  int callback_count = 0;
142  auto callback = [&callback_count](const WriteNvramReply& reply) {
143    callback_count++;
144    EXPECT_EQ(STATUS_SUCCESS, reply.status());
145  };
146  WriteNvramRequest request;
147  request.set_index(nvram_index);
148  request.set_data(nvram_data);
149  proxy_.WriteNvram(request, base::Bind(callback));
150  EXPECT_EQ(1, callback_count);
151}
152
153TEST_F(TpmNvramDBusProxyTest, ReadNvram) {
154  uint32_t nvram_index = 5;
155  std::string nvram_data("nvram_data");
156  auto fake_dbus_call = [nvram_index, nvram_data](
157      dbus::MethodCall* method_call,
158      const dbus::MockObjectProxy::ResponseCallback& response_callback) {
159    // Verify request protobuf.
160    dbus::MessageReader reader(method_call);
161    ReadNvramRequest request;
162    EXPECT_TRUE(reader.PopArrayOfBytesAsProto(&request));
163    EXPECT_TRUE(request.has_index());
164    EXPECT_EQ(nvram_index, request.index());
165    // Create reply protobuf.
166    auto response = dbus::Response::CreateEmpty();
167    dbus::MessageWriter writer(response.get());
168    ReadNvramReply reply;
169    reply.set_status(STATUS_SUCCESS);
170    reply.set_data(nvram_data);
171    writer.AppendProtoAsArrayOfBytes(reply);
172    response_callback.Run(response.release());
173  };
174  EXPECT_CALL(*mock_object_proxy_, CallMethodWithErrorCallback(_, _, _, _))
175      .WillOnce(WithArgs<0, 2>(Invoke(fake_dbus_call)));
176  // Set expectations on the outputs.
177  int callback_count = 0;
178  auto callback = [&callback_count, nvram_data](const ReadNvramReply& reply) {
179    callback_count++;
180    EXPECT_EQ(STATUS_SUCCESS, reply.status());
181    EXPECT_TRUE(reply.has_data());
182    EXPECT_EQ(nvram_data, reply.data());
183  };
184  ReadNvramRequest request;
185  request.set_index(nvram_index);
186  proxy_.ReadNvram(request, base::Bind(callback));
187  EXPECT_EQ(1, callback_count);
188}
189
190TEST_F(TpmNvramDBusProxyTest, IsNvramDefined) {
191  uint32_t nvram_index = 5;
192  bool nvram_defined = true;
193  auto fake_dbus_call = [nvram_index, nvram_defined](
194      dbus::MethodCall* method_call,
195      const dbus::MockObjectProxy::ResponseCallback& response_callback) {
196    // Verify request protobuf.
197    dbus::MessageReader reader(method_call);
198    IsNvramDefinedRequest request;
199    EXPECT_TRUE(reader.PopArrayOfBytesAsProto(&request));
200    EXPECT_TRUE(request.has_index());
201    EXPECT_EQ(nvram_index, request.index());
202    // Create reply protobuf.
203    auto response = dbus::Response::CreateEmpty();
204    dbus::MessageWriter writer(response.get());
205    IsNvramDefinedReply reply;
206    reply.set_status(STATUS_SUCCESS);
207    reply.set_is_defined(nvram_defined);
208    writer.AppendProtoAsArrayOfBytes(reply);
209    response_callback.Run(response.release());
210  };
211  EXPECT_CALL(*mock_object_proxy_, CallMethodWithErrorCallback(_, _, _, _))
212      .WillOnce(WithArgs<0, 2>(Invoke(fake_dbus_call)));
213  // Set expectations on the outputs.
214  int callback_count = 0;
215  auto callback = [&callback_count, nvram_defined](
216      const IsNvramDefinedReply& reply) {
217    callback_count++;
218    EXPECT_EQ(STATUS_SUCCESS, reply.status());
219    EXPECT_TRUE(reply.has_is_defined());
220    EXPECT_EQ(nvram_defined, reply.is_defined());
221  };
222  IsNvramDefinedRequest request;
223  request.set_index(nvram_index);
224  proxy_.IsNvramDefined(request, base::Bind(callback));
225  EXPECT_EQ(1, callback_count);
226}
227
228TEST_F(TpmNvramDBusProxyTest, IsNvramLocked) {
229  uint32_t nvram_index = 5;
230  bool nvram_locked = true;
231  auto fake_dbus_call = [nvram_index, nvram_locked](
232      dbus::MethodCall* method_call,
233      const dbus::MockObjectProxy::ResponseCallback& response_callback) {
234    // Verify request protobuf.
235    dbus::MessageReader reader(method_call);
236    IsNvramLockedRequest request;
237    EXPECT_TRUE(reader.PopArrayOfBytesAsProto(&request));
238    EXPECT_TRUE(request.has_index());
239    EXPECT_EQ(nvram_index, request.index());
240    // Create reply protobuf.
241    auto response = dbus::Response::CreateEmpty();
242    dbus::MessageWriter writer(response.get());
243    IsNvramLockedReply reply;
244    reply.set_status(STATUS_SUCCESS);
245    reply.set_is_locked(nvram_locked);
246    writer.AppendProtoAsArrayOfBytes(reply);
247    response_callback.Run(response.release());
248  };
249  EXPECT_CALL(*mock_object_proxy_, CallMethodWithErrorCallback(_, _, _, _))
250      .WillOnce(WithArgs<0, 2>(Invoke(fake_dbus_call)));
251  // Set expectations on the outputs.
252  int callback_count = 0;
253  auto callback = [&callback_count, nvram_locked](
254      const IsNvramLockedReply& reply) {
255    callback_count++;
256    EXPECT_EQ(STATUS_SUCCESS, reply.status());
257    EXPECT_TRUE(reply.has_is_locked());
258    EXPECT_EQ(nvram_locked, reply.is_locked());
259  };
260  IsNvramLockedRequest request;
261  request.set_index(nvram_index);
262  proxy_.IsNvramLocked(request, base::Bind(callback));
263  EXPECT_EQ(1, callback_count);
264}
265
266TEST_F(TpmNvramDBusProxyTest, GetNvramSize) {
267  uint32_t nvram_index = 5;
268  size_t nvram_size = 32;
269  auto fake_dbus_call = [nvram_index, nvram_size](
270      dbus::MethodCall* method_call,
271      const dbus::MockObjectProxy::ResponseCallback& response_callback) {
272    // Verify request protobuf.
273    dbus::MessageReader reader(method_call);
274    GetNvramSizeRequest request;
275    EXPECT_TRUE(reader.PopArrayOfBytesAsProto(&request));
276    EXPECT_TRUE(request.has_index());
277    EXPECT_EQ(nvram_index, request.index());
278    // Create reply protobuf.
279    auto response = dbus::Response::CreateEmpty();
280    dbus::MessageWriter writer(response.get());
281    GetNvramSizeReply reply;
282    reply.set_status(STATUS_SUCCESS);
283    reply.set_size(nvram_size);
284    writer.AppendProtoAsArrayOfBytes(reply);
285    response_callback.Run(response.release());
286  };
287  EXPECT_CALL(*mock_object_proxy_, CallMethodWithErrorCallback(_, _, _, _))
288      .WillOnce(WithArgs<0, 2>(Invoke(fake_dbus_call)));
289  // Set expectations on the outputs.
290  int callback_count = 0;
291  auto callback = [&callback_count, nvram_size](
292      const GetNvramSizeReply& reply) {
293    callback_count++;
294    EXPECT_EQ(STATUS_SUCCESS, reply.status());
295    EXPECT_TRUE(reply.has_size());
296    EXPECT_EQ(nvram_size, reply.size());
297  };
298  GetNvramSizeRequest request;
299  request.set_index(nvram_index);
300  proxy_.GetNvramSize(request, base::Bind(callback));
301  EXPECT_EQ(1, callback_count);
302}
303
304}  // namespace tpm_manager
305