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
|
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/domain_reliability/header.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_split.h"
#include "base/strings/stringprintf.h"
#include "components/domain_reliability/config.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace domain_reliability {
namespace {
class DomainReliabilityHeaderTest : public testing::Test {
protected:
DomainReliabilityHeaderTest() {}
~DomainReliabilityHeaderTest() override {}
void Parse(std::string value) {
parsed_ = DomainReliabilityHeader::Parse(value);
}
const DomainReliabilityHeader* parsed() const { return parsed_.get(); }
private:
std::unique_ptr<DomainReliabilityHeader> parsed_;
};
bool CheckReportUris(
const char* pipe_separated_expected_report_uris,
const std::vector<std::unique_ptr<GURL>>& actual_report_uris) {
if (!pipe_separated_expected_report_uris)
return actual_report_uris.empty();
std::vector<std::string> expected_report_uri_strings =
SplitString(pipe_separated_expected_report_uris,
"|",
base::KEEP_WHITESPACE,
base::SPLIT_WANT_ALL);
if (expected_report_uri_strings.size() != actual_report_uris.size())
return false;
for (size_t i = 0; i < actual_report_uris.size(); ++i) {
if (actual_report_uris[i]->spec() != expected_report_uri_strings[i])
return false;
}
return true;
}
TEST_F(DomainReliabilityHeaderTest, SetConfig) {
static const struct {
const char* header;
const char* pipe_separated_report_uris;
int64_t max_age_in_seconds;
bool include_subdomains;
const char* description;
} test_cases[] = {
{ "report-uri=https://a/; max-age=5",
"https://a/", 5, false,
"register" },
{ "report-uri=\"https://a/\"; max-age=5",
"https://a/", 5, false,
"register with quoted report-uri" },
{ "report-uri=<https://a/>; max-age=5",
"https://a/", 5, false,
"register with bracketed report-uri" },
{ "report-uri=https://a/ https://b/; max-age=5",
"https://a/|https://b/", 5, false,
"register with two report-uris" },
{ "report-uri=https://a/; max-age=5; includeSubdomains",
"https://a/", 5, true,
"register with includeSubdomains" },
};
for (const auto& test_case : test_cases) {
std::string assert_prefix = base::StringPrintf(
"Valid set-config NEL header \"%s\" (%s) incorrectly parsed as ",
test_case.header,
test_case.description);
Parse(test_case.header);
EXPECT_FALSE(parsed()->IsParseError()) << assert_prefix << "invalid.";
EXPECT_FALSE(parsed()->IsClearConfig()) << assert_prefix << "clear-config.";
if (parsed()->IsParseError() || parsed()->IsClearConfig())
continue;
EXPECT_TRUE(parsed()->IsSetConfig());
std::string assert_message =
assert_prefix + "\"" + parsed()->ToString() + "\"";
EXPECT_TRUE(CheckReportUris(test_case.pipe_separated_report_uris,
parsed()->config().collectors))
<< assert_message;
EXPECT_EQ(test_case.max_age_in_seconds,
parsed()->max_age().InSeconds())
<< assert_message;
EXPECT_EQ(test_case.include_subdomains,
parsed()->config().include_subdomains)
<< assert_message;
}
}
TEST_F(DomainReliabilityHeaderTest, ClearConfig) {
static const struct {
const char* header;
const char* description;
} test_cases[] = {
{ "max-age=0", "unregister" },
{ "report-uri=https://a/; max-age=0", "unregister with report-uri" },
{ "max-age=0; includeSubdomains", "unregister with includeSubdomains" },
};
for (const auto& test_case : test_cases) {
std::string assert_prefix = base::StringPrintf(
"Valid clear-config NEL header \"%s\" (%s) incorrectly parsed as ",
test_case.header,
test_case.description);
Parse(test_case.header);
EXPECT_FALSE(parsed()->IsParseError()) << assert_prefix << "invalid.";
EXPECT_FALSE(parsed()->IsSetConfig()) << assert_prefix << "set-config.";
}
}
TEST_F(DomainReliabilityHeaderTest, Error) {
static const struct {
const char* header;
const char* description;
} test_cases[] = {
{ "", "empty" },
{ "max-age=5", "report-uri missing with non-zero max-age" },
{ "report-uri; max-age=5", "report-uri has no value" },
{ "report-uri=; max-age=5", "report-uri value is empty" },
{ "report-uri=http://a/; max-age=5", "report-uri is insecure" },
{ "report-uri=https://a/ http://b/; max-age=5",
"one report-uri of two is insecure" },
{ "report-uri=\"https://a/; max-age=5", "report-uri is unbalanced" },
{ "report-uri=https://a/\"; max-age=5", "report-uri is unbalanced" },
{ "report-uri=<https://a/; max-age=5", "report-uri is unbalanced" },
{ "report-uri=https://a/>; max-age=5", "report-uri is unbalanced" },
{ "report-uri=https://a/", "max-age is missing" },
{ "report-uri=https://a/; max-age", "max-age has no value" },
{ "report-uri=https://a/; max-age=", "max-age value is empty" },
{ "report-uri=https://a/; max-age=a", "max-age is entirely non-numeric" },
{ "report-uri=https://a/; max-age=5a", "max-age is partly non-numeric" },
{ "report-uri=https://a/; max-age=5 5", "max-age has multiple values" },
};
for (const auto& test_case : test_cases) {
Parse(test_case.header);
EXPECT_TRUE(parsed()->IsParseError())
<< "Invalid NEL header \"" << test_case.header << "\" ("
<< test_case.description << ") incorrectly parsed as valid.";
}
}
} // namespace
} // namespace domain_reliability
|