• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2017 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "net/tools/transport_security_state_generator/spki_hash.h"
6 #include "base/strings/string_number_conversions.h"
7 #include "testing/gmock/include/gmock/gmock.h"
8 #include "testing/gtest/include/gtest/gtest.h"
9 
10 namespace net::transport_security_state {
11 
12 namespace {
13 
TEST(SPKIHashTest,FromString)14 TEST(SPKIHashTest, FromString) {
15   SPKIHash hash;
16 
17   // Valid SHA256.
18   EXPECT_TRUE(
19       hash.FromString("sha256/1111111111111111111111111111111111111111111="));
20   std::vector<uint8_t> hash_vector(hash.data(), hash.data() + hash.size());
21   EXPECT_THAT(
22       hash_vector,
23       testing::ElementsAreArray(
24           {0xD7, 0x5D, 0x75, 0xD7, 0x5D, 0x75, 0xD7, 0x5D, 0x75, 0xD7, 0x5D,
25            0x75, 0xD7, 0x5D, 0x75, 0xD7, 0x5D, 0x75, 0xD7, 0x5D, 0x75, 0xD7,
26            0x5D, 0x75, 0xD7, 0x5D, 0x75, 0xD7, 0x5D, 0x75, 0xD7, 0x5D}));
27 
28   SPKIHash hash2;
29   EXPECT_TRUE(
30       hash2.FromString("sha256/4osU79hfY3P2+WJGlT2mxmSL+5FIwLEVxTQcavyBNgQ="));
31   std::vector<uint8_t> hash_vector2(hash2.data(), hash2.data() + hash2.size());
32   EXPECT_THAT(
33       hash_vector2,
34       testing::ElementsAreArray(
35           {0xE2, 0x8B, 0x14, 0xEF, 0xD8, 0x5F, 0x63, 0x73, 0xF6, 0xF9, 0x62,
36            0x46, 0x95, 0x3D, 0XA6, 0xC6, 0x64, 0x8B, 0xFB, 0x91, 0x48, 0xC0,
37            0xB1, 0x15, 0xC5, 0x34, 0x1C, 0x6A, 0xFC, 0x81, 0x36, 0x04}));
38 
39   SPKIHash hash3;
40 
41   // Valid SHA1 should be rejected.
42   EXPECT_FALSE(hash3.FromString("sha1/111111111111111111111111111="));
43   EXPECT_FALSE(hash3.FromString("sha1/gzF+YoVCU9bXeDGQ7JGQVumRueM="));
44 
45   // SHA1 disguised as SHA256.
46   EXPECT_FALSE(hash3.FromString("sha256/111111111111111111111111111="));
47 
48   // SHA512 disguised as SHA256.
49   EXPECT_FALSE(
50       hash3.FromString("sha256/ns3smS51SK/4P7uSVhSlCIMNAxkD+r6C/ZZA/"
51                        "07vac0uyMdRS4jKfqlvk3XxLFP1v5aMIxM5cdTM7FHNwxagQg=="));
52 
53   // Invalid BASE64.
54   EXPECT_FALSE(hash3.FromString("sha256/hsts-preload"));
55   EXPECT_FALSE(hash3.FromString("sha256/1. 2. 3. security!="));
56 }
57 
58 }  // namespace
59 
60 }  // namespace net::transport_security_state
61