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

content / browser / web_package / signed_exchange_prologue_unittest.cc [blame]

// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "content/browser/web_package/signed_exchange_prologue.h"

#include "testing/gtest/include/gtest/gtest.h"

namespace content {
namespace signed_exchange_prologue {

TEST(SignedExchangePrologueTest, Parse2BytesEncodedLength) {
  constexpr struct {
    uint8_t bytes[2];
    size_t expected;
  } kTestCases[] = {
      {{0x00, 0x01}, 1u}, {{0xab, 0xcd}, 43981u},
  };

  int test_element_index = 0;
  for (const auto& test_case : kTestCases) {
    SCOPED_TRACE(testing::Message() << "testing case " << test_element_index++);
    EXPECT_EQ(Parse2BytesEncodedLength(test_case.bytes), test_case.expected);
  }
}

TEST(SignedExchangePrologueTest, Parse3BytesEncodedLength) {
  constexpr struct {
    uint8_t bytes[3];
    size_t expected;
  } kTestCases[] = {
      {{0x00, 0x00, 0x01}, 1u}, {{0x01, 0xe2, 0x40}, 123456u},
  };

  int test_element_index = 0;
  for (const auto& test_case : kTestCases) {
    SCOPED_TRACE(testing::Message() << "testing case " << test_element_index++);
    EXPECT_EQ(Parse3BytesEncodedLength(test_case.bytes), test_case.expected);
  }
}

TEST(SignedExchangePrologueTest, BeforeFallbackUrl_Success) {
  uint8_t bytes[] = {'s', 'x', 'g', '1', '-', 'b', '3', '\0', 0x12, 0x34};

  BeforeFallbackUrl before_fallback_url = BeforeFallbackUrl::Parse(
      base::make_span(bytes), nullptr /* devtools_proxy */);
  EXPECT_TRUE(before_fallback_url.is_valid());
  EXPECT_EQ(0x1234u, before_fallback_url.fallback_url_length());
}

TEST(SignedExchangePrologueTest, BeforeFallbackUrl_B2) {
  uint8_t bytes[] = {'s', 'x', 'g', '1', '-', 'b', '2', '\0', 0x12, 0x34};

  BeforeFallbackUrl before_fallback_url = BeforeFallbackUrl::Parse(
      base::make_span(bytes), nullptr /* devtools_proxy */);
  EXPECT_FALSE(before_fallback_url.is_valid());
  EXPECT_EQ(0x1234u, before_fallback_url.fallback_url_length());
}

TEST(SignedExchangePrologueTest, BeforeFallbackUrl_WrongMagic) {
  uint8_t bytes[] = {'s', 'x', 'g', '!', '-', 'b', '3', '\0', 0x12, 0x34};

  BeforeFallbackUrl before_fallback_url = BeforeFallbackUrl::Parse(
      base::make_span(bytes), nullptr /* devtools_proxy */);
  EXPECT_FALSE(before_fallback_url.is_valid());
  EXPECT_EQ(0x1234u, before_fallback_url.fallback_url_length());
}

TEST(SignedExchangePrologueTest, FallbackUrlAndAfter_Success) {
  uint8_t bytes[] = {'h', 't', 't',  'p',  's',  ':',  '/',  '/', 'e',
                     'x', 'a', 'm',  'p',  'l',  'e',  '.',  'c', 'o',
                     'm', '/', 0x00, 0x12, 0x34, 0x00, 0x23, 0x45};

  BeforeFallbackUrl before_fallback_url(true,
                                        sizeof("https://example.com/") - 1);
  EXPECT_TRUE(before_fallback_url.is_valid());

  FallbackUrlAndAfter fallback_url_and_after =
      FallbackUrlAndAfter::Parse(base::make_span(bytes), before_fallback_url,
                                 nullptr /* devtools_proxy */);

  EXPECT_TRUE(fallback_url_and_after.is_valid());
  EXPECT_EQ("https://example.com/",
            fallback_url_and_after.fallback_url().url.spec());
  EXPECT_EQ(0x1234u, fallback_url_and_after.signature_header_field_length());
  EXPECT_EQ(0x2345u, fallback_url_and_after.cbor_header_length());
}

TEST(SignedExchangePrologueTest, FallbackUrlAndAfter_NonHttpsUrl) {
  uint8_t bytes[] = {'h', 't',  't',  'p',  ':',  '/',  '/', 'e', 'x',
                     'a', 'm',  'p',  'l',  'e',  '.',  'c', 'o', 'm',
                     '/', 0x00, 0x12, 0x34, 0x00, 0x23, 0x45};

  BeforeFallbackUrl before_fallback_url(true,
                                        sizeof("http://example.com/") - 1);
  FallbackUrlAndAfter fallback_url_and_after =
      FallbackUrlAndAfter::Parse(base::make_span(bytes), before_fallback_url,
                                 nullptr /* devtools_proxy */);

  EXPECT_FALSE(fallback_url_and_after.is_valid());
  EXPECT_FALSE(fallback_url_and_after.fallback_url().url.is_valid());
}

TEST(SignedExchangePrologueTest, FallbackUrlAndAfter_UrlWithFragment) {
  uint8_t bytes[] = {'h', 't', 't', 'p', 's',  ':',  '/',  '/',  'e',  'x',
                     'a', 'm', 'p', 'l', 'e',  '.',  'c',  'o',  'm',  '/',
                     '#', 'f', 'o', 'o', 0x00, 0x12, 0x34, 0x00, 0x23, 0x45};

  BeforeFallbackUrl before_fallback_url(true,
                                        sizeof("https://example.com/#foo") - 1);
  FallbackUrlAndAfter fallback_url_and_after =
      FallbackUrlAndAfter::Parse(base::make_span(bytes), before_fallback_url,
                                 nullptr /* devtools_proxy */);

  EXPECT_FALSE(fallback_url_and_after.is_valid());
  EXPECT_FALSE(fallback_url_and_after.fallback_url().url.is_valid());
}

TEST(SignedExchangePrologueTest, FallbackUrlAndAfter_LongSignatureHeader) {
  uint8_t bytes[] = {'h', 't', 't',  'p',  's',  ':',  '/',  '/', 'e',
                     'x', 'a', 'm',  'p',  'l',  'e',  '.',  'c', 'o',
                     'm', '/', 0xff, 0x12, 0x34, 0x00, 0x23, 0x45};

  BeforeFallbackUrl before_fallback_url(true,
                                        sizeof("https://example.com/") - 1);
  FallbackUrlAndAfter fallback_url_and_after =
      FallbackUrlAndAfter::Parse(base::make_span(bytes), before_fallback_url,
                                 nullptr /* devtools_proxy */);

  EXPECT_FALSE(fallback_url_and_after.is_valid());
  EXPECT_EQ("https://example.com/",
            fallback_url_and_after.fallback_url().url.spec());
}

TEST(SignedExchangePrologueTest, FallbackUrlAndAfter_LongCBORHeader) {
  uint8_t bytes[] = {'h', 't', 't',  'p',  's',  ':',  '/',  '/', 'e',
                     'x', 'a', 'm',  'p',  'l',  'e',  '.',  'c', 'o',
                     'm', '/', 0x00, 0x12, 0x34, 0xff, 0x23, 0x45};

  BeforeFallbackUrl before_fallback_url(true,
                                        sizeof("https://example.com/") - 1);
  FallbackUrlAndAfter fallback_url_and_after =
      FallbackUrlAndAfter::Parse(base::make_span(bytes), before_fallback_url,
                                 nullptr /* devtools_proxy */);

  EXPECT_FALSE(fallback_url_and_after.is_valid());
  EXPECT_EQ("https://example.com/",
            fallback_url_and_after.fallback_url().url.spec());
}

}  // namespace signed_exchange_prologue
}  // namespace content