File: quick_start_requests_unittest.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (69 lines) | stat: -rw-r--r-- 3,014 bytes parent folder | download | duplicates (7)
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
// Copyright 2023 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/quick_start/quick_start_requests.h"

#include "components/cbor/reader.h"
#include "crypto/hash.h"
#include "testing/gtest/include/gtest/gtest.h"

using ash::quick_start::requests::CBOREncodeGetAssertionRequest;
using ash::quick_start::requests::GenerateGetAssertionRequest;

namespace {
// Used as a dummy client data hash when constructing test requests.
const std::array<uint8_t, crypto::hash::kSha256Size> kTestClientDataHash = {
    0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a,
    0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15,
    0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
};
}  // namespace

class QuickStartRequestTest : public testing::Test {
 public:
  QuickStartRequestTest() = default;
  QuickStartRequestTest(QuickStartRequestTest&) = delete;
  QuickStartRequestTest& operator=(QuickStartRequestTest&) = delete;
  ~QuickStartRequestTest() override = default;

 protected:
  void SetUp() override {}
};

TEST_F(QuickStartRequestTest, GenerateGetAssertionRequest_ValidChallenge) {
  cbor::Value request = GenerateGetAssertionRequest(kTestClientDataHash);
  ASSERT_TRUE(request.is_map());
  const cbor::Value::MapValue& request_map = request.GetMap();
  // CBOR Index 0x01 stores the relying_party_id for the GetAssertionRequest.
  EXPECT_EQ(request_map.find(cbor::Value(0x01))->second.GetString(),
            "google.com");
  // CBOR Index 0x02 stores the client data hash.
  EXPECT_EQ(base::as_byte_span(
                request_map.find(cbor::Value(0x02))->second.GetBytestring()),
            kTestClientDataHash);
  // CBOR Index 0x05 stores the options for the GetAssertionRequest.
  const cbor::Value::MapValue& options_map =
      request_map.find(cbor::Value(0x05))->second.GetMap();
  // CBOR key "uv" stores the userVerification bit for the options.
  EXPECT_TRUE(options_map.find(cbor::Value("uv"))->second.GetBool());
  // CBOR key "up" stores the userPresence bit for the options.
  EXPECT_TRUE(options_map.find(cbor::Value("up"))->second.GetBool());
  EXPECT_GT(request_map.find(cbor::Value(0x02))->second.GetBytestring().size(),
            0UL);
}

TEST_F(QuickStartRequestTest, CBOREncodeGetAssertionRequest) {
  cbor::Value request = GenerateGetAssertionRequest(kTestClientDataHash);
  std::vector<uint8_t> cbor_encoded_request =
      CBOREncodeGetAssertionRequest(std::move(request));
  std::optional<cbor::Value> cbor;
  const base::span<const uint8_t> ctap_request_span =
      base::span(cbor_encoded_request);
  cbor = cbor::Reader::Read(ctap_request_span.subspan<1>());
  ASSERT_TRUE(cbor);
  ASSERT_TRUE(cbor->is_map());
  const cbor::Value::MapValue& cbor_map = cbor->GetMap();
  // CBOR Index 0x01 stores the relying_party_id for the GetAssertionRequest.
  EXPECT_EQ(cbor_map.find(cbor::Value(0x01))->second.GetString(), "google.com");
}