File: ecdsa_p256_public_key_unittest.cc

package info (click to toggle)
chromium 139.0.7258.138-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 6,120,676 kB
  • sloc: cpp: 35,100,869; 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 (55 lines) | stat: -rw-r--r-- 2,180 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
// 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 "components/web_package/signed_web_bundles/ecdsa_p256_public_key.h"

#include <array>

#include "base/test/gmock_expected_support.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace web_package {

namespace {

using base::test::ErrorIs;
using base::test::HasValue;
using testing::Eq;

// Valid ECDSA P-256 public key.
constexpr std::array<uint8_t, EcdsaP256PublicKey::kLength> kEcdsaP256PublicKey =
    {0x03, 0x42, 0x06, 0xc0, 0x35, 0xe5, 0x87, 0x9f, 0xdd, 0x31, 0x51,
     0x95, 0x44, 0xfd, 0x8d, 0x6c, 0x1b, 0xe9, 0x99, 0x11, 0xe8, 0x40,
     0x5b, 0xae, 0x6a, 0x36, 0x1b, 0xf5, 0x17, 0x12, 0xa1, 0x17, 0xe3};

// Invalid key that is one symbol shorter than necessary.
constexpr std::array<uint8_t, EcdsaP256PublicKey::kLength - 1>
    kInvalidShortPublicKey = {0x04, 0x42, 0x06, 0xc0, 0x35, 0xe5, 0x87, 0x9f,
                              0xdd, 0x31, 0x51, 0x95, 0x44, 0xfd, 0x8d, 0x6c,
                              0x1b, 0xe9, 0x99, 0x11, 0xe8, 0x40, 0x5b, 0xae,
                              0x6a, 0x36, 0x1b, 0xf5, 0x17, 0x12, 0xa1, 0x17};

// `kEcdsaP256PublicKey` with the first byte changed to 0x04 (invalid).
constexpr std::array<uint8_t, EcdsaP256PublicKey::kLength> kInvalidPublicKey = {
    0x04, 0x42, 0x06, 0xc0, 0x35, 0xe5, 0x87, 0x9f, 0xdd, 0x31, 0x51,
    0x95, 0x44, 0xfd, 0x8d, 0x6c, 0x1b, 0xe9, 0x99, 0x11, 0xe8, 0x40,
    0x5b, 0xae, 0x6a, 0x36, 0x1b, 0xf5, 0x17, 0x12, 0xa1, 0x17, 0xe3};

}  // namespace

TEST(EcdsaP256PublicKeyTest, Create) {
  EXPECT_THAT(EcdsaP256PublicKey::Create(kEcdsaP256PublicKey), HasValue());

  EXPECT_THAT(EcdsaP256PublicKey::Create(kInvalidShortPublicKey),
              ErrorIs(Eq("The ECDSA P-256 public key does not have the correct "
                         "length. Expected 33 bytes, but received 32 bytes.")));

  EXPECT_THAT(
      EcdsaP256PublicKey::Create(kInvalidPublicKey),
      ErrorIs(
          Eq("Unable to parse a valid ECDSA P-256 key from the given bytes.")));
}

}  // namespace web_package