-
Notifications
You must be signed in to change notification settings - Fork 0
/
crypto_util_proxy_unittest.cc
408 lines (378 loc) · 15.5 KB
/
crypto_util_proxy_unittest.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
//
// Copyright (C) 2013 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#include "shill/crypto_util_proxy.h"
#include <algorithm>
#include <string>
#include <vector>
#include <base/callback.h>
#include <gtest/gtest.h>
#include "shill/callbacks.h"
#include "shill/mock_crypto_util_proxy.h"
#include "shill/mock_event_dispatcher.h"
#include "shill/mock_file_io.h"
#include "shill/mock_process_manager.h"
using base::Bind;
using std::min;
using std::string;
using std::vector;
using testing::AnyOf;
using testing::DoAll;
using testing::InSequence;
using testing::Invoke;
using testing::Mock;
using testing::NotNull;
using testing::Return;
using testing::StrEq;
using testing::WithoutArgs;
using testing::_;
namespace shill {
namespace {
const char kTestBSSID[] = "00:11:22:33:44:55";
const char kTestCertificate[] = "testcertgoeshere";
const char kTestData[] = "thisisthetestdata";
const char kTestDestinationUDN[] = "TEST1234-5678-ABCD";
const char kTestNonce[] = "abort abort abort";
const char kTestPublicKey[] = "YWJvcnQgYWJvcnQgYWJvcnQK";
const char kTestSerializedCommandMessage[] =
"Since we're not testing protocol buffer seriallization, and no data "
"actually makes it to a shim, we're safe to write whatever we want here.";
const char kTestSerializedCommandResponse[] =
"Similarly, we never ask a protocol buffer to deserialize this string.";
const char kTestSignedData[] = "Ynl0ZXMgYnl0ZXMgYnl0ZXMK";
const int kTestStdinFd = 9111;
const int kTestStdoutFd = 9119;
const pid_t kTestShimPid = 989898;
} // namespace
MATCHER_P(ErrorIsOfType, error_type, "") {
if (error_type != arg.type()) {
return false;
}
return true;
}
class CryptoUtilProxyTest : public testing::Test {
public:
CryptoUtilProxyTest()
: crypto_util_proxy_(&dispatcher_) {
test_ssid_.push_back(78);
test_ssid_.push_back(69);
test_ssid_.push_back(80);
test_ssid_.push_back(84);
test_ssid_.push_back(85);
test_ssid_.push_back(78);
test_ssid_.push_back(69);
}
virtual void SetUp() {
crypto_util_proxy_.process_manager_ = &process_manager_;
crypto_util_proxy_.file_io_ = &file_io_;
}
virtual void TearDown() {
// Note that |crypto_util_proxy_| needs its process manager reference in
// order not to segfault when it tries to kill any outstanding shims on
// shutdown. Thus we don't clear out those fields here, and we make sure
// to declare the proxy after mocks it consumes.
}
// TODO(quiche): Consider refactoring
// HandleStartInMinijailWithPipes, HandleStopProcess, and
// HandleUpdateExitCallback into a FakeProcessManager. b/24210150
pid_t HandleStartInMinijailWithPipes(
const tracked_objects::Location& /* spawn_source */,
const base::FilePath& /* program */,
vector<string> /* program_args */,
const std::string& /* run_as_user */,
const std::string& /* run_as_group */,
uint64_t /* capabilities_mask */,
const base::Callback<void(int)>& exit_callback,
int* stdin,
int* stdout,
int* /* stderr */) {
exit_callback_ = exit_callback;
*stdin = kTestStdinFd;
*stdout = kTestStdoutFd;
return kTestShimPid;
}
void StartAndCheckShim(const std::string& command,
const std::string& shim_stdin) {
InSequence seq;
// Delegate the start call to the real implementation just for this test.
EXPECT_CALL(crypto_util_proxy_, StartShimForCommand(_, _, _))
.WillOnce(Invoke(&crypto_util_proxy_,
&MockCryptoUtilProxy::RealStartShimForCommand));
// All shims should be spawned in a Minijail.
EXPECT_CALL(
process_manager_,
StartProcessInMinijailWithPipes(
_, // caller location
base::FilePath(CryptoUtilProxy::kCryptoUtilShimPath),
AnyOf(
vector<string>{CryptoUtilProxy::kCommandVerify},
vector<string>{CryptoUtilProxy::kCommandEncrypt}),
"shill-crypto",
"shill-crypto",
0, // no capabilities required
_, // exit_callback
NotNull(), // stdin
NotNull(), // stdout
nullptr)) // stderr
.WillOnce(Invoke(this,
&CryptoUtilProxyTest::HandleStartInMinijailWithPipes));
// We should always schedule a shim timeout callback.
EXPECT_CALL(dispatcher_, PostDelayedTask(_, _));
// We don't allow file I/O to block.
EXPECT_CALL(file_io_,
SetFdNonBlocking(kTestStdinFd))
.WillOnce(Return(0));
EXPECT_CALL(file_io_,
SetFdNonBlocking(kTestStdoutFd))
.WillOnce(Return(0));
// We instead do file I/O through async callbacks registered with the event
// dispatcher.
EXPECT_CALL(dispatcher_, CreateInputHandler(_, _, _)).Times(1);
EXPECT_CALL(dispatcher_, CreateReadyHandler(_, _, _)).Times(1);
// The shim is left in flight, not killed.
EXPECT_CALL(process_manager_, StopProcess(_)).Times(0);
crypto_util_proxy_.StartShimForCommand(
command, shim_stdin,
Bind(&MockCryptoUtilProxy::TestResultHandlerCallback,
crypto_util_proxy_.base::SupportsWeakPtr<MockCryptoUtilProxy>::
AsWeakPtr()));
EXPECT_EQ(shim_stdin, crypto_util_proxy_.input_buffer_);
EXPECT_TRUE(crypto_util_proxy_.output_buffer_.empty());
EXPECT_EQ(crypto_util_proxy_.shim_pid_, kTestShimPid);
Mock::VerifyAndClearExpectations(&crypto_util_proxy_);
Mock::VerifyAndClearExpectations(&dispatcher_);
Mock::VerifyAndClearExpectations(&process_manager_);
}
void ExpectCleanup(const Error& expected_result) {
if (crypto_util_proxy_.shim_stdin_ > -1) {
EXPECT_CALL(file_io_,
Close(crypto_util_proxy_.shim_stdin_)).Times(1);
}
if (crypto_util_proxy_.shim_stdout_ > -1) {
EXPECT_CALL(file_io_,
Close(crypto_util_proxy_.shim_stdout_)).Times(1);
}
if (crypto_util_proxy_.shim_pid_) {
EXPECT_CALL(process_manager_, UpdateExitCallback(_, _))
.Times(1)
.WillOnce(Invoke(this,
&CryptoUtilProxyTest::HandleUpdateExitCallback));
EXPECT_CALL(process_manager_, StopProcess(crypto_util_proxy_.shim_pid_))
.Times(1)
.WillOnce(Invoke(this,
&CryptoUtilProxyTest::HandleStopProcess));
}
}
void AssertShimDead() {
EXPECT_FALSE(crypto_util_proxy_.shim_pid_);
}
bool HandleUpdateExitCallback(pid_t /*pid*/,
const base::Callback<void(int)>& new_callback) {
exit_callback_ = new_callback;
return true;
}
bool HandleStopProcess(pid_t /*pid*/) {
const int kExitStatus = -1;
// NB: in the real world, this ordering is not guaranteed. That
// is, StopProcess() might return before executing the callback.
exit_callback_.Run(kExitStatus);
return true;
}
void StopAndCheckShim(const Error& error) {
ExpectCleanup(error);
crypto_util_proxy_.CleanupShim(error);
crypto_util_proxy_.OnShimDeath(-1);
EXPECT_EQ(crypto_util_proxy_.shim_pid_, 0);
Mock::VerifyAndClearExpectations(&process_manager_);
}
protected:
MockProcessManager process_manager_;
MockEventDispatcher dispatcher_;
MockFileIO file_io_;
MockCryptoUtilProxy crypto_util_proxy_;
std::vector<uint8_t> test_ssid_;
base::Callback<void(int)> exit_callback_;
};
TEST_F(CryptoUtilProxyTest, BasicAPIUsage) {
{
InSequence seq;
// Delegate the API call to the real implementation for this test.
EXPECT_CALL(crypto_util_proxy_,
VerifyDestination(_, _, _, _, _, _, _, _, _))
.WillOnce(Invoke(&crypto_util_proxy_,
&MockCryptoUtilProxy::RealVerifyDestination));
// API calls are just thin wrappers that write up a message to a shim, then
// send it via StartShimForCommand. Expect that a shim will be started in
// response to the API being called.
EXPECT_CALL(crypto_util_proxy_,
StartShimForCommand(CryptoUtilProxy::kCommandVerify, _, _))
.WillOnce(Return(true));
ResultBoolCallback result_callback =
Bind(&MockCryptoUtilProxy::TestResultBoolCallback,
crypto_util_proxy_.
base::SupportsWeakPtr<MockCryptoUtilProxy>::AsWeakPtr());
Error error;
EXPECT_TRUE(crypto_util_proxy_.VerifyDestination(kTestCertificate,
kTestPublicKey,
kTestNonce,
kTestSignedData,
kTestDestinationUDN,
test_ssid_,
kTestBSSID,
result_callback,
&error));
EXPECT_TRUE(error.IsSuccess());
}
{
// And very similarly...
InSequence seq;
EXPECT_CALL(crypto_util_proxy_, EncryptData(_, _, _, _))
.WillOnce(Invoke(&crypto_util_proxy_,
&MockCryptoUtilProxy::RealEncryptData));
EXPECT_CALL(crypto_util_proxy_,
StartShimForCommand(CryptoUtilProxy::kCommandEncrypt, _, _))
.WillOnce(Return(true));
ResultStringCallback result_callback =
Bind(&MockCryptoUtilProxy::TestResultStringCallback,
crypto_util_proxy_.
base::SupportsWeakPtr<MockCryptoUtilProxy>::AsWeakPtr());
Error error;
// Normally, we couldn't have these two operations run successfully without
// finishing the first one, since only one shim can be in flight at a time.
// However, this works because we didn't actually start a shim, we just
// trapped the call in our mock.
EXPECT_TRUE(crypto_util_proxy_.EncryptData(kTestPublicKey, kTestData,
result_callback, &error));
EXPECT_TRUE(error.IsSuccess());
}
}
TEST_F(CryptoUtilProxyTest, ShimCleanedBeforeCallback) {
// Some operations, like VerifyAndEncryptData in the manager, chain two
// shim operations together. Make sure that we don't call back with results
// before the shim state is clean.
{
StartAndCheckShim(CryptoUtilProxy::kCommandEncrypt,
kTestSerializedCommandMessage);
Error e(Error::kOperationFailed);
ExpectCleanup(e);
EXPECT_CALL(crypto_util_proxy_,
TestResultHandlerCallback(
StrEq(""), ErrorIsOfType(Error::kOperationFailed)))
.Times(1)
.WillOnce(WithoutArgs(Invoke(this,
&CryptoUtilProxyTest::AssertShimDead)));
crypto_util_proxy_.HandleShimError(e);
}
{
StartAndCheckShim(CryptoUtilProxy::kCommandEncrypt,
kTestSerializedCommandMessage);
EXPECT_CALL(crypto_util_proxy_,
TestResultHandlerCallback(
StrEq(""), ErrorIsOfType(Error::kSuccess)))
.Times(1)
.WillOnce(WithoutArgs(Invoke(this,
&CryptoUtilProxyTest::AssertShimDead)));
ExpectCleanup(Error(Error::kSuccess));
InputData data;
data.buf = nullptr;
data.len = 0;
crypto_util_proxy_.HandleShimOutput(&data);
}
}
// Verify that even when we have errors, we'll call the result handler.
// Ultimately, this is supposed to make sure that we always return something to
// our callers over DBus.
TEST_F(CryptoUtilProxyTest, FailuresReturnValues) {
StartAndCheckShim(CryptoUtilProxy::kCommandEncrypt,
kTestSerializedCommandMessage);
EXPECT_CALL(crypto_util_proxy_, TestResultHandlerCallback(
StrEq(""), ErrorIsOfType(Error::kOperationFailed))).Times(1);
Error e(Error::kOperationFailed);
ExpectCleanup(e);
crypto_util_proxy_.HandleShimError(e);
}
TEST_F(CryptoUtilProxyTest, TimeoutsTriggerFailure) {
StartAndCheckShim(CryptoUtilProxy::kCommandEncrypt,
kTestSerializedCommandMessage);
EXPECT_CALL(crypto_util_proxy_, TestResultHandlerCallback(
StrEq(""), ErrorIsOfType(Error::kOperationTimeout))).Times(1);
ExpectCleanup(Error(Error::kOperationTimeout));
// This timeout is scheduled by StartShimForCommand.
crypto_util_proxy_.HandleShimTimeout();
}
TEST_F(CryptoUtilProxyTest, OnlyOneInstanceInFlightAtATime) {
StartAndCheckShim(CryptoUtilProxy::kCommandEncrypt,
kTestSerializedCommandMessage);
// Can't start things twice.
EXPECT_FALSE(crypto_util_proxy_.RealStartShimForCommand(
CryptoUtilProxy::kCommandEncrypt, kTestSerializedCommandMessage,
Bind(&MockCryptoUtilProxy::TestResultHandlerCallback,
crypto_util_proxy_.
base::SupportsWeakPtr<MockCryptoUtilProxy>::AsWeakPtr())));
// But if some error (or completion) caused us to clean up the shim...
StopAndCheckShim(Error(Error::kSuccess));
// Then we could start the shim again.
StartAndCheckShim(CryptoUtilProxy::kCommandEncrypt,
kTestSerializedCommandMessage);
// Clean up after ourselves.
StopAndCheckShim(Error(Error::kOperationFailed));
}
// This test walks the CryptoUtilProxy through the life time of a shim by
// simulating the API call, file I/O operations, and the final handler on shim
// completion.
TEST_F(CryptoUtilProxyTest, ShimLifeTime) {
const int kBytesAtATime = 10;
StartAndCheckShim(CryptoUtilProxy::kCommandEncrypt,
kTestSerializedCommandMessage);
// Emulate the operating system pulling bytes through the pipe, and the event
// loop notifying us that the file descriptor is ready.
int bytes_left = strlen(kTestSerializedCommandMessage);
while (bytes_left > 0) {
int bytes_written = min(kBytesAtATime, bytes_left);
EXPECT_CALL(file_io_, Write(kTestStdinFd, _, bytes_left))
.Times(1).WillOnce(Return(bytes_written));
bytes_left -= bytes_written;
if (bytes_left < 1) {
EXPECT_CALL(file_io_, Close(kTestStdinFd));
}
crypto_util_proxy_.HandleShimStdinReady(crypto_util_proxy_.shim_stdin_);
Mock::VerifyAndClearExpectations(&crypto_util_proxy_);
}
// At this point, the shim goes off and does terribly complex crypto stuff,
// before responding with a string of bytes over stdout. Emulate the shim
// and the event loop to push those bytes back.
const int response_length = bytes_left =
strlen(kTestSerializedCommandResponse);
InputData data;
while (bytes_left > 0) {
int bytes_written = min(kBytesAtATime, bytes_left);
data.len = bytes_written;
data.buf = reinterpret_cast<unsigned char*>(const_cast<char*>(
kTestSerializedCommandResponse + response_length - bytes_left));
bytes_left -= bytes_written;
crypto_util_proxy_.HandleShimOutput(&data);
}
// Write 0 bytes in to signify the end of the stream. This should in turn
// cause our callback to be called.
data.len = 0;
data.buf = nullptr;
EXPECT_CALL(
crypto_util_proxy_,
TestResultHandlerCallback(string(kTestSerializedCommandResponse),
ErrorIsOfType(Error::kSuccess))).Times(1);
ExpectCleanup(Error(Error::kSuccess));
crypto_util_proxy_.HandleShimOutput(&data);
}
} // namespace shill