File: session_chaps_client_unittest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (415 lines) | stat: -rw-r--r-- 17,929 bytes parent folder | download | duplicates (3)
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
409
410
411
412
413
414
415
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chromeos/ash/components/kcer/chaps/session_chaps_client.h"

#include <stdint.h>

#include "base/test/gmock_callback_support.h"
#include "base/test/task_environment.h"
#include "base/test/test_future.h"
#include "chromeos/ash/components/dbus/chaps/fake_chaps_client.h"
#include "chromeos/constants/pkcs11_definitions.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/cros_system_api/dbus/chaps/dbus-constants.h"

using ObjectHandle = kcer::SessionChapsClient::ObjectHandle;
using SessionId = kcer::SessionChapsClient::SessionId;
using SlotId = kcer::SessionChapsClient::SlotId;
using base::test::RunOnceCallback;
using base::test::RunOnceCallbackRepeatedly;
using testing::_;
using testing::Mock;

namespace kcer {
namespace {

class KcerSessionChapsClientTest : public testing::Test {
 public:
  void SetUp() override {
    if (ash::ChapsClient::Get()) {
      ash::ChapsClient::Shutdown();
    }
    ash::ChapsClient::InitializeFake();
    fake_chaps_client_ =
        static_cast<ash::FakeChapsClient*>(ash::ChapsClient::Get());
  }

  void TearDown() override {
    fake_chaps_client_ = nullptr;
    ash::ChapsClient::Shutdown();
  }

 protected:
  ObjectHandle CreateObject(SlotId slot_id,
                            const std::vector<uint8_t>& attributes) {
    base::test::TestFuture<ObjectHandle, uint32_t> create_waiter;
    client_.CreateObject(slot_id, attributes,
                         /*attempts_left=*/1, create_waiter.GetCallback());
    EXPECT_EQ(create_waiter.Get<uint32_t>(), chromeos::PKCS11_CKR_OK);
    return create_waiter.Get<ObjectHandle>();
  }

  base::test::SingleThreadTaskEnvironment task_environment_{
      base::test::TaskEnvironment::TimeSource::MOCK_TIME};

  raw_ptr<ash::FakeChapsClient> fake_chaps_client_ = nullptr;
  SessionChapsClientImpl client_;

  // In the real world the attributes are supposed to be a binary encoding of
  // the chaps::AttributeList proto message. With the current implementation of
  // FakeChapsClient just some binary buffer is good enough.
  const std::vector<uint8_t> kFakeAttrs{1, 1, 1};

  // In the current implementation of FakeChapsClient the query doesn't matter
  // and it will return all attributes anyway.
  const std::vector<uint8_t> kFakeQuery{2, 2, 2};
};

TEST_F(KcerSessionChapsClientTest, CreateAndFetchObject) {
  // Test that CreateObject() can create an object.
  base::test::TestFuture<ObjectHandle, uint32_t> create_waiter;
  client_.CreateObject(SlotId(1), /*attributes=*/kFakeAttrs,
                       /*attempts_left=*/1, create_waiter.GetCallback());
  EXPECT_EQ(create_waiter.Get<uint32_t>(), chromeos::PKCS11_CKR_OK);
  EXPECT_NE(create_waiter.Get<ObjectHandle>(), ObjectHandle(0));
  ObjectHandle handle = create_waiter.Get<ObjectHandle>();

  // Test that GetAttributeValue() can retrieve attributes from an object and
  // that CreateObject() did forward the attributes correctly.
  base::test::TestFuture<std::vector<uint8_t>, uint32_t> attr_waiter;
  client_.GetAttributeValue(SlotId(1), handle, kFakeQuery,
                            /*attempts_left=*/1, attr_waiter.GetCallback());
  EXPECT_EQ(attr_waiter.Get<uint32_t>(), chromeos::PKCS11_CKR_OK);
  EXPECT_EQ(attr_waiter.Get<std::vector<uint8_t>>(), kFakeAttrs);
}

TEST_F(KcerSessionChapsClientTest, CreateFindDestroyObject) {
  // Create a test object.
  ObjectHandle handle = CreateObject(SlotId(1), kFakeAttrs);

  // Test that FindObjects() can correctly find the created object.
  base::test::TestFuture<std::vector<ObjectHandle>, uint32_t> find_waiter_1;
  client_.FindObjects(SlotId(1), kFakeQuery,
                      /*attempts_left=*/1, find_waiter_1.GetCallback());
  EXPECT_EQ(find_waiter_1.Get<uint32_t>(), chromeos::PKCS11_CKR_OK);
  EXPECT_EQ(find_waiter_1.Get<std::vector<ObjectHandle>>(),
            std::vector<ObjectHandle>{handle});

  // Test DestroyObject() can successfully  delete the object.
  base::test::TestFuture<uint32_t> destroy_waiter;
  client_.DestroyObject(SlotId(1), handle,
                        /*attempts_left=*/1, destroy_waiter.GetCallback());
  EXPECT_EQ(destroy_waiter.Get<uint32_t>(), chromeos::PKCS11_CKR_OK);

  // Test that FindObjects() doesn't find the deleted object.
  base::test::TestFuture<std::vector<ObjectHandle>, uint32_t> find_waiter_2;
  client_.FindObjects(SlotId(1), kFakeQuery,
                      /*attempts_left=*/1, find_waiter_2.GetCallback());
  EXPECT_EQ(find_waiter_2.Get<uint32_t>(), chromeos::PKCS11_CKR_OK);
  EXPECT_EQ(find_waiter_2.Get<std::vector<ObjectHandle>>(),
            std::vector<ObjectHandle>());
}

// Test that GetAttributeValue correctly forwards the arguments to the mojo
// layer and the result back from it.
TEST_F(KcerSessionChapsClientTest, SetGetAttributeValue) {
  // Create a test object.
  ObjectHandle handle = CreateObject(SlotId(1), kFakeAttrs);

  // Create different fake attributes.
  const std::vector<uint8_t> kFakeAttrs2{9, 9, 9, 9};
  ASSERT_NE(kFakeAttrs, kFakeAttrs2);

  // Test that SetAttributeValue() can successfully set attributes on an object.
  base::test::TestFuture<uint32_t> set_waiter;
  client_.SetAttributeValue(SlotId(1), handle, kFakeAttrs2,
                            /*attempts_left=*/1, set_waiter.GetCallback());
  EXPECT_EQ(set_waiter.Get<uint32_t>(), chromeos::PKCS11_CKR_OK);

  // Test that GetAttributeValue() can retrieve attributes from an object and
  // that SetAttributeValue() successfully overwritten the old attributes.
  base::test::TestFuture<std::vector<uint8_t>, uint32_t> get_waiter;
  client_.GetAttributeValue(SlotId(1), handle, kFakeQuery,
                            /*attempts_left=*/1, get_waiter.GetCallback());
  EXPECT_EQ(get_waiter.Get<uint32_t>(), chromeos::PKCS11_CKR_OK);
  EXPECT_EQ(get_waiter.Get<std::vector<uint8_t>>(), kFakeAttrs2);
}

TEST_F(KcerSessionChapsClientTest, Sign) {
  constexpr uint64_t kFakeMechanismType = 11;
  const std::vector<uint8_t> kFakeMechanismParams = {2, 2, 2};
  const std::vector<uint8_t> kFakeData = {4, 4, 4};
  // In the current implementation of FakeChapsClient, it just returns the
  // original data as signature.
  const std::vector<uint8_t>& kExpectedSignature = kFakeData;

  // In the real world Sign should be called with a handle of a key, but the
  // current implementation of FakeChapsClient only checks that the object
  // exists.
  ObjectHandle handle = CreateObject(SlotId(1), kFakeAttrs);

  // Test that Sign() can be called successfully and that it forwards the data
  // and the signature back correctly.
  base::test::TestFuture<std::vector<uint8_t>, uint32_t> waiter;
  client_.Sign(SlotId(1), kFakeMechanismType, kFakeMechanismParams, handle,
               kFakeData,
               /*attempts_left=*/1, waiter.GetCallback());
  EXPECT_EQ(waiter.Get<std::vector<uint8_t>>(), kExpectedSignature);
  EXPECT_EQ(waiter.Get<uint32_t>(), chromeos::PKCS11_CKR_OK);
}

TEST_F(KcerSessionChapsClientTest, GenerateAndGetKeyPair) {
  constexpr uint64_t kFakeMechanismType = 11;
  const std::vector<uint8_t> kFakeMechanismParams = {2, 2, 2};
  const std::vector<uint8_t> kFakePublicAttrs = {3, 3, 3};
  const std::vector<uint8_t> kFakePrivateAttrs = {4, 4, 4};

  // Test that GenerateKeyPair() can be successfully called.
  base::test::TestFuture<ObjectHandle, ObjectHandle, uint32_t> waiter;
  client_.GenerateKeyPair(SlotId(1), kFakeMechanismType, kFakeMechanismParams,
                          kFakePublicAttrs, kFakePrivateAttrs,
                          /*attempts_left=*/1, waiter.GetCallback());
  EXPECT_EQ(waiter.Get<uint32_t>(), chromeos::PKCS11_CKR_OK);
  ObjectHandle pub_key_handle = waiter.Get<0>();
  ObjectHandle priv_key_handle = waiter.Get<1>();
  EXPECT_NE(pub_key_handle, ObjectHandle(0));
  EXPECT_NE(priv_key_handle, ObjectHandle(0));
  EXPECT_NE(pub_key_handle, priv_key_handle);

  // Test that GenerateKeyPair() forwarded the key attributes correctly. Note
  // that in the real world the attributes given to GenerateKeyPair() are more
  // like command arguments and not the actual attributes for the generated
  // keys.

  base::test::TestFuture<std::vector<uint8_t>, uint32_t> get_waiter_1;
  client_.GetAttributeValue(SlotId(1), pub_key_handle, kFakeQuery,
                            /*attempts_left=*/1, get_waiter_1.GetCallback());
  EXPECT_EQ(get_waiter_1.Get<uint32_t>(), chromeos::PKCS11_CKR_OK);
  EXPECT_EQ(get_waiter_1.Get<std::vector<uint8_t>>(), kFakePublicAttrs);

  base::test::TestFuture<std::vector<uint8_t>, uint32_t> get_waiter_2;
  client_.GetAttributeValue(SlotId(1), priv_key_handle, kFakeQuery,
                            /*attempts_left=*/1, get_waiter_2.GetCallback());
  EXPECT_EQ(get_waiter_2.Get<uint32_t>(), chromeos::PKCS11_CKR_OK);
  EXPECT_EQ(get_waiter_2.Get<std::vector<uint8_t>>(), kFakePrivateAttrs);
}

// Test that all methods fail if they need to open a session and fail to do so.
TEST_F(KcerSessionChapsClientTest, CannotOpenSessionThenAllMethodsFail) {
  fake_chaps_client_->SimulateOpenSessionError(
      chromeos::PKCS11_CKR_GENERAL_ERROR);

  // GetMechanismList() doesn't require a session, so it wouldn't fail and is
  // not called in the test.

  {
    base::test::TestFuture<ObjectHandle, uint32_t> waiter;
    client_.CreateObject(SlotId(1), /*attributes=*/{},
                         /*attempts_left=*/1, waiter.GetCallback());
    EXPECT_NE(waiter.Get<uint32_t>(), chromeos::PKCS11_CKR_OK);
  }

  {
    base::test::TestFuture<uint32_t> waiter;
    client_.DestroyObject(SlotId(1), ObjectHandle(0), /*attempts_left=*/1,
                          waiter.GetCallback());
    EXPECT_NE(waiter.Get<uint32_t>(), chromeos::PKCS11_CKR_OK);
  }

  {
    base::test::TestFuture<std::vector<uint8_t>, uint32_t> waiter;
    client_.GetAttributeValue(SlotId(1), ObjectHandle(1),
                              /*attributes_query=*/{}, /*attempts_left=*/1,
                              waiter.GetCallback());
    EXPECT_NE(waiter.Get<uint32_t>(), chromeos::PKCS11_CKR_OK);
  }

  {
    base::test::TestFuture<uint32_t> waiter;
    client_.SetAttributeValue(SlotId(1), ObjectHandle(1),
                              /*attributes=*/{},
                              /*attempts_left=*/1, waiter.GetCallback());
    EXPECT_NE(waiter.Get<uint32_t>(), chromeos::PKCS11_CKR_OK);
  }

  {
    base::test::TestFuture<std::vector<ObjectHandle>, uint32_t> waiter;
    client_.FindObjects(SlotId(1),
                        /*attributes=*/{},
                        /*attempts_left=*/1, waiter.GetCallback());
    EXPECT_NE(waiter.Get<uint32_t>(), chromeos::PKCS11_CKR_OK);
  }

  {
    base::test::TestFuture<std::vector<uint8_t>, uint32_t> waiter;
    client_.Sign(SlotId(1), /*mechanism_type=*/0,
                 /*mechanism_parameter=*/{}, /*key_handle=*/ObjectHandle(0),
                 /*data=*/{}, /*attempts_left=*/1, waiter.GetCallback());
    EXPECT_NE(waiter.Get<uint32_t>(), chromeos::PKCS11_CKR_OK);
  }

  {
    base::test::TestFuture<ObjectHandle, ObjectHandle, uint32_t> waiter;
    client_.GenerateKeyPair(SlotId(1), /*mechanism_type=*/0,
                            /*mechanism_parameter=*/{},
                            /*public_attributes=*/{},
                            /*private_attributes=*/{}, /*attempts_left=*/1,
                            waiter.GetCallback());
    EXPECT_NE(waiter.Get<uint32_t>(), chromeos::PKCS11_CKR_OK);
  }
}

// Test that the first call to each slot opens a session that can be reused by
// all the other methods.
TEST_F(KcerSessionChapsClientTest, AllMethodsDontReopenSession) {
  // Call a method on two different slots that should open new sessions for
  // them. Also save created objects to use them for other methods.
  std::vector<ObjectHandle> objects_handles(2);
  for (uint64_t slot_id = 0; slot_id < 2; ++slot_id) {
    base::test::TestFuture<ObjectHandle, uint32_t> waiter;
    client_.CreateObject(SlotId(slot_id), /*attributes=*/{},
                         /*attempts_left=*/1, waiter.GetCallback());
    EXPECT_EQ(waiter.Get<uint32_t>(), chromeos::PKCS11_CKR_OK);
    objects_handles[slot_id] = waiter.Get<ObjectHandle>();
  }

  // Prevent any new sessions from being opened, as covered by the
  // CannotOpenSessionThenAllMethodsFail test, the methods will fail if they
  // attempt to open new session. They are expected to reuse the existing
  // session.
  fake_chaps_client_->SimulateOpenSessionError(
      chromeos::PKCS11_CKR_GENERAL_ERROR);

  for (uint64_t slot_id = 0; slot_id < 2; ++slot_id) {
    base::test::TestFuture<ObjectHandle, uint32_t> waiter;
    client_.CreateObject(SlotId(slot_id), /*attributes=*/{},
                         /*attempts_left=*/1, waiter.GetCallback());
    EXPECT_EQ(waiter.Get<uint32_t>(), chromeos::PKCS11_CKR_OK);
  }

  for (uint64_t slot_id = 0; slot_id < 2; ++slot_id) {
    base::test::TestFuture<std::vector<uint8_t>, uint32_t> waiter;
    client_.GetAttributeValue(SlotId(slot_id), objects_handles[slot_id],
                              /*attributes_query=*/{}, /*attempts_left=*/1,
                              waiter.GetCallback());
    EXPECT_EQ(waiter.Get<uint32_t>(), chromeos::PKCS11_CKR_OK);
  }

  for (uint64_t slot_id = 0; slot_id < 2; ++slot_id) {
    base::test::TestFuture<uint32_t> waiter;
    client_.SetAttributeValue(SlotId(slot_id), objects_handles[slot_id],
                              /*attributes=*/{},
                              /*attempts_left=*/1, waiter.GetCallback());
    EXPECT_EQ(waiter.Get<uint32_t>(), chromeos::PKCS11_CKR_OK);
  }

  for (uint64_t slot_id = 0; slot_id < 2; ++slot_id) {
    base::test::TestFuture<std::vector<ObjectHandle>, uint32_t> waiter;
    client_.FindObjects(SlotId(slot_id),
                        /*attributes=*/{},
                        /*attempts_left=*/1, waiter.GetCallback());
    EXPECT_EQ(waiter.Get<uint32_t>(), chromeos::PKCS11_CKR_OK);
  }

  for (uint64_t slot_id = 0; slot_id < 2; ++slot_id) {
    base::test::TestFuture<std::vector<uint8_t>, uint32_t> waiter;
    client_.Sign(SlotId(slot_id), /*mechanism_type=*/0,
                 /*mechanism_parameter=*/{},
                 /*key_handle=*/objects_handles[slot_id],
                 /*data=*/{}, /*attempts_left=*/1, waiter.GetCallback());
    EXPECT_EQ(waiter.Get<uint32_t>(), chromeos::PKCS11_CKR_OK);
  }

  for (uint64_t slot_id = 0; slot_id < 2; ++slot_id) {
    base::test::TestFuture<ObjectHandle, ObjectHandle, uint32_t> waiter;
    client_.GenerateKeyPair(
        SlotId(slot_id), /*mechanism_type=*/0,
        /*mechanism_parameter=*/{}, /*public_attributes=*/{},
        /*private_attributes=*/{}, /*attempts_left=*/1, waiter.GetCallback());
    EXPECT_EQ(waiter.Get<uint32_t>(), chromeos::PKCS11_CKR_OK);
  }

  for (uint64_t slot_id = 0; slot_id < 2; ++slot_id) {
    base::test::TestFuture<uint32_t> waiter;
    client_.DestroyObject(SlotId(slot_id), objects_handles[slot_id],
                          /*attempts_left=*/1, waiter.GetCallback());
    EXPECT_EQ(waiter.Get<uint32_t>(), chromeos::PKCS11_CKR_OK);
  }
}

// Test that all methods try to open a session multiple times when there's no
// open session at the beginning of each method.
TEST_F(KcerSessionChapsClientTest, AllMethodsTryReopenSession) {
  constexpr int kAttempts = 5;
  fake_chaps_client_->SimulateOpenSessionError(
      chromeos::PKCS11_CKR_GENERAL_ERROR);

  EXPECT_EQ(fake_chaps_client_->GetAndResetOpenSessionCounter(), 0);

  {
    base::test::TestFuture<ObjectHandle, uint32_t> waiter;
    client_.CreateObject(SlotId(1), /*attributes=*/{}, kAttempts,
                         waiter.GetCallback());
    EXPECT_TRUE(waiter.Wait());
    EXPECT_EQ(fake_chaps_client_->GetAndResetOpenSessionCounter(), kAttempts);
  }

  {
    base::test::TestFuture<uint32_t> waiter;
    client_.DestroyObject(SlotId(1), ObjectHandle(1), kAttempts,
                          waiter.GetCallback());
    EXPECT_TRUE(waiter.Wait());
    EXPECT_EQ(fake_chaps_client_->GetAndResetOpenSessionCounter(), kAttempts);
  }

  {
    base::test::TestFuture<std::vector<uint8_t>, uint32_t> waiter;
    client_.GetAttributeValue(SlotId(1), ObjectHandle(1),
                              /*attributes_query=*/{}, kAttempts,
                              waiter.GetCallback());
    EXPECT_TRUE(waiter.Wait());
    EXPECT_EQ(fake_chaps_client_->GetAndResetOpenSessionCounter(), kAttempts);
  }

  {
    base::test::TestFuture<uint32_t> waiter;
    client_.SetAttributeValue(SlotId(1), ObjectHandle(1),
                              /*attributes=*/{}, kAttempts,
                              waiter.GetCallback());
    EXPECT_TRUE(waiter.Wait());
    EXPECT_EQ(fake_chaps_client_->GetAndResetOpenSessionCounter(), kAttempts);
  }

  {
    base::test::TestFuture<std::vector<ObjectHandle>, uint32_t> waiter;
    client_.FindObjects(SlotId(1),
                        /*attributes=*/{}, kAttempts, waiter.GetCallback());
    EXPECT_TRUE(waiter.Wait());
    EXPECT_EQ(fake_chaps_client_->GetAndResetOpenSessionCounter(), kAttempts);
  }

  {
    base::test::TestFuture<std::vector<uint8_t>, uint32_t> waiter;
    client_.Sign(SlotId(1), /*mechanism_type=*/0,
                 /*mechanism_parameter=*/{}, /*key_handle=*/ObjectHandle(0),
                 /*data=*/{}, kAttempts, waiter.GetCallback());
    EXPECT_TRUE(waiter.Wait());
    EXPECT_EQ(fake_chaps_client_->GetAndResetOpenSessionCounter(), kAttempts);
  }

  {
    base::test::TestFuture<ObjectHandle, ObjectHandle, uint32_t> waiter;
    client_.GenerateKeyPair(
        SlotId(1), /*mechanism_type=*/0,
        /*mechanism_parameter=*/{}, /*public_attributes=*/{},
        /*private_attributes=*/{}, kAttempts, waiter.GetCallback());
    EXPECT_TRUE(waiter.Wait());
    EXPECT_EQ(fake_chaps_client_->GetAndResetOpenSessionCounter(), kAttempts);
  }
}

}  // namespace
}  // namespace kcer