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
26 using testing::_;
27 using testing::Invoke;
28 using testing::StrictMock;
29 using testing::WithArgs;
30
31 namespace tpm_manager {
32
33 class TpmNvramDBusProxyTest : public testing::Test {
34 public:
35 ~TpmNvramDBusProxyTest() override = default;
SetUp()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
TEST_F(TpmNvramDBusProxyTest,DefineSpace)47 TEST_F(TpmNvramDBusProxyTest, DefineSpace) {
48 uint32_t nvram_index = 5;
49 size_t nvram_size = 32;
50 auto fake_dbus_call = [nvram_index, nvram_size](
51 dbus::MethodCall* method_call,
52 const dbus::MockObjectProxy::ResponseCallback& response_callback) {
53 // Verify request protobuf.
54 dbus::MessageReader reader(method_call);
55 DefineSpaceRequest 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_size());
60 EXPECT_EQ(nvram_size, request.size());
61 // Create reply protobuf.
62 auto response = dbus::Response::CreateEmpty();
63 dbus::MessageWriter writer(response.get());
64 DefineSpaceReply reply;
65 reply.set_result(NVRAM_RESULT_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 DefineSpaceReply& reply) {
74 callback_count++;
75 EXPECT_EQ(NVRAM_RESULT_SUCCESS, reply.result());
76 };
77 DefineSpaceRequest request;
78 request.set_index(nvram_index);
79 request.set_size(nvram_size);
80 proxy_.DefineSpace(request, base::Bind(callback));
81 EXPECT_EQ(1, callback_count);
82 }
83
TEST_F(TpmNvramDBusProxyTest,DestroySpaceRequest)84 TEST_F(TpmNvramDBusProxyTest, DestroySpaceRequest) {
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 DestroySpaceRequest 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 DestroySpaceReply reply;
99 reply.set_result(NVRAM_RESULT_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 DestroySpaceReply& reply) {
108 callback_count++;
109 EXPECT_EQ(NVRAM_RESULT_SUCCESS, reply.result());
110 };
111 DestroySpaceRequest request;
112 request.set_index(nvram_index);
113 proxy_.DestroySpace(request, base::Bind(callback));
114 EXPECT_EQ(1, callback_count);
115 }
TEST_F(TpmNvramDBusProxyTest,WriteSpace)116 TEST_F(TpmNvramDBusProxyTest, WriteSpace) {
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 WriteSpaceRequest 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 WriteSpaceReply reply;
134 reply.set_result(NVRAM_RESULT_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 WriteSpaceReply& reply) {
143 callback_count++;
144 EXPECT_EQ(NVRAM_RESULT_SUCCESS, reply.result());
145 };
146 WriteSpaceRequest request;
147 request.set_index(nvram_index);
148 request.set_data(nvram_data);
149 proxy_.WriteSpace(request, base::Bind(callback));
150 EXPECT_EQ(1, callback_count);
151 }
152
TEST_F(TpmNvramDBusProxyTest,ReadSpace)153 TEST_F(TpmNvramDBusProxyTest, ReadSpace) {
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 ReadSpaceRequest 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 ReadSpaceReply reply;
169 reply.set_result(NVRAM_RESULT_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 ReadSpaceReply& reply) {
179 callback_count++;
180 EXPECT_EQ(NVRAM_RESULT_SUCCESS, reply.result());
181 EXPECT_TRUE(reply.has_data());
182 EXPECT_EQ(nvram_data, reply.data());
183 };
184 ReadSpaceRequest request;
185 request.set_index(nvram_index);
186 proxy_.ReadSpace(request, base::Bind(callback));
187 EXPECT_EQ(1, callback_count);
188 }
189
TEST_F(TpmNvramDBusProxyTest,LockSpace)190 TEST_F(TpmNvramDBusProxyTest, LockSpace) {
191 uint32_t nvram_index = 5;
192 auto fake_dbus_call = [nvram_index](
193 dbus::MethodCall* method_call,
194 const dbus::MockObjectProxy::ResponseCallback& response_callback) {
195 // Verify request protobuf.
196 dbus::MessageReader reader(method_call);
197 LockSpaceRequest request;
198 EXPECT_TRUE(reader.PopArrayOfBytesAsProto(&request));
199 EXPECT_TRUE(request.has_index());
200 EXPECT_EQ(nvram_index, request.index());
201 // Create reply protobuf.
202 auto response = dbus::Response::CreateEmpty();
203 dbus::MessageWriter writer(response.get());
204 LockSpaceReply reply;
205 reply.set_result(NVRAM_RESULT_SUCCESS);
206 writer.AppendProtoAsArrayOfBytes(reply);
207 response_callback.Run(response.release());
208 };
209 EXPECT_CALL(*mock_object_proxy_, CallMethodWithErrorCallback(_, _, _, _))
210 .WillOnce(WithArgs<0, 2>(Invoke(fake_dbus_call)));
211 // Set expectations on the outputs.
212 int callback_count = 0;
213 auto callback = [&callback_count](const LockSpaceReply& reply) {
214 callback_count++;
215 EXPECT_EQ(NVRAM_RESULT_SUCCESS, reply.result());
216 };
217 LockSpaceRequest request;
218 request.set_index(nvram_index);
219 proxy_.LockSpace(request, base::Bind(callback));
220 EXPECT_EQ(1, callback_count);
221 }
222
TEST_F(TpmNvramDBusProxyTest,ListSpaces)223 TEST_F(TpmNvramDBusProxyTest, ListSpaces) {
224 constexpr uint32_t nvram_index_list[] = {3, 4, 5};
225 auto fake_dbus_call = [nvram_index_list](
226 dbus::MethodCall* method_call,
227 const dbus::MockObjectProxy::ResponseCallback& response_callback) {
228 // Verify request protobuf.
229 dbus::MessageReader reader(method_call);
230 ListSpacesRequest request;
231 EXPECT_TRUE(reader.PopArrayOfBytesAsProto(&request));
232 // Create reply protobuf.
233 auto response = dbus::Response::CreateEmpty();
234 dbus::MessageWriter writer(response.get());
235 ListSpacesReply reply;
236 reply.set_result(NVRAM_RESULT_SUCCESS);
237 for (auto index : nvram_index_list) {
238 reply.add_index_list(index);
239 }
240 writer.AppendProtoAsArrayOfBytes(reply);
241 response_callback.Run(response.release());
242 };
243 EXPECT_CALL(*mock_object_proxy_, CallMethodWithErrorCallback(_, _, _, _))
244 .WillOnce(WithArgs<0, 2>(Invoke(fake_dbus_call)));
245 // Set expectations on the outputs.
246 int callback_count = 0;
247 auto callback = [&callback_count,
248 nvram_index_list](const ListSpacesReply& reply) {
249 callback_count++;
250 EXPECT_EQ(NVRAM_RESULT_SUCCESS, reply.result());
251 EXPECT_EQ(arraysize(nvram_index_list), reply.index_list_size());
252 for (size_t i = 0; i < 3; i++) {
253 EXPECT_EQ(nvram_index_list[i], reply.index_list(i));
254 }
255 };
256 ListSpacesRequest request;
257 proxy_.ListSpaces(request, base::Bind(callback));
258 EXPECT_EQ(1, callback_count);
259 }
260
TEST_F(TpmNvramDBusProxyTest,GetSpaceInfo)261 TEST_F(TpmNvramDBusProxyTest, GetSpaceInfo) {
262 uint32_t nvram_index = 5;
263 size_t nvram_size = 32;
264 auto fake_dbus_call = [nvram_index, nvram_size](
265 dbus::MethodCall* method_call,
266 const dbus::MockObjectProxy::ResponseCallback& response_callback) {
267 // Verify request protobuf.
268 dbus::MessageReader reader(method_call);
269 GetSpaceInfoRequest request;
270 EXPECT_TRUE(reader.PopArrayOfBytesAsProto(&request));
271 EXPECT_TRUE(request.has_index());
272 EXPECT_EQ(nvram_index, request.index());
273 // Create reply protobuf.
274 auto response = dbus::Response::CreateEmpty();
275 dbus::MessageWriter writer(response.get());
276 GetSpaceInfoReply reply;
277 reply.set_result(NVRAM_RESULT_SUCCESS);
278 reply.set_size(nvram_size);
279 writer.AppendProtoAsArrayOfBytes(reply);
280 response_callback.Run(response.release());
281 };
282 EXPECT_CALL(*mock_object_proxy_, CallMethodWithErrorCallback(_, _, _, _))
283 .WillOnce(WithArgs<0, 2>(Invoke(fake_dbus_call)));
284 // Set expectations on the outputs.
285 int callback_count = 0;
286 auto callback = [&callback_count,
287 nvram_size](const GetSpaceInfoReply& reply) {
288 callback_count++;
289 EXPECT_EQ(NVRAM_RESULT_SUCCESS, reply.result());
290 EXPECT_TRUE(reply.has_size());
291 EXPECT_EQ(nvram_size, reply.size());
292 };
293 GetSpaceInfoRequest request;
294 request.set_index(nvram_index);
295 proxy_.GetSpaceInfo(request, base::Bind(callback));
296 EXPECT_EQ(1, callback_count);
297 }
298
299 } // namespace tpm_manager
300