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
|
// 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 "components/attribution_reporting/destination_set.h"
#include "base/test/gmock_expected_support.h"
#include "base/test/values_test_util.h"
#include "base/types/expected.h"
#include "base/values.h"
#include "components/attribution_reporting/source_registration_error.mojom.h"
#include "components/attribution_reporting/test_utils.h"
#include "net/base/schemeful_site.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace attribution_reporting {
namespace {
using ::attribution_reporting::mojom::SourceRegistrationError;
using ::base::test::ErrorIs;
using ::base::test::ValueIs;
using ::testing::ElementsAre;
using ::testing::Property;
TEST(DestinationSetTest, Parse) {
EXPECT_THAT(DestinationSet::FromJSON(nullptr),
ErrorIs(SourceRegistrationError::kDestinationMissing));
const struct {
const char* desc;
const char* json;
::testing::Matcher<base::expected<DestinationSet, SourceRegistrationError>>
matches;
} kTestCases[] = {
{
"empty",
R"json(null)json",
ErrorIs(SourceRegistrationError::kDestinationWrongType),
},
{
"destination_wrong_type",
R"json(0)json",
ErrorIs(SourceRegistrationError::kDestinationWrongType),
},
{
"destination_untrustworthy",
R"json("http://d.example")json",
ErrorIs(SourceRegistrationError::kDestinationUntrustworthy),
},
{
"basic_destination",
R"json("https://d.example")json",
ValueIs(Property(&DestinationSet::destinations,
ElementsAre(net::SchemefulSite::Deserialize(
"https://d.example")))),
},
{
"destination_list_empty",
R"json([])json",
ErrorIs(SourceRegistrationError::kDestinationWrongType),
},
{
"destination_in_list_wrong_type",
R"json([0])json",
ErrorIs(SourceRegistrationError::kDestinationWrongType),
},
{
"destination_in_list_untrustworthy",
R"json(["http://d.example"])json",
ErrorIs(SourceRegistrationError::kDestinationListUntrustworthy),
},
{
"multiple_destinations",
R"json([
"https://d.example",
"https://e.example",
"https://f.example"
])json",
ValueIs(Property(
&DestinationSet::destinations,
ElementsAre(
net::SchemefulSite::Deserialize("https://d.example"),
net::SchemefulSite::Deserialize("https://e.example"),
net::SchemefulSite::Deserialize("https://f.example")))),
},
{
"too_many_destinations",
R"json([
"https://d.example",
"https://e.example",
"https://f.example",
"https://g.example"
])json",
ErrorIs(SourceRegistrationError::kDestinationWrongType),
},
{
"size_check_after_transformation_and_deduplication",
R"json([
"https://d1.example",
"https://d2.example",
"https://d3.example/a",
"https://d3.example/b"
])json",
ValueIs(Property(
&DestinationSet::destinations,
ElementsAre(
net::SchemefulSite::Deserialize("https://d1.example"),
net::SchemefulSite::Deserialize("https://d2.example"),
net::SchemefulSite::Deserialize("https://d3.example")))),
},
};
for (const auto& test_case : kTestCases) {
SCOPED_TRACE(test_case.desc);
const base::Value value = base::test::ParseJson(test_case.json);
EXPECT_THAT(DestinationSet::FromJSON(&value), test_case.matches);
}
}
TEST(DestinationSetTest, ToJson) {
const struct {
DestinationSet input;
const char* expected_json;
} kTestCases[] = {
{
*DestinationSet::Create(
{net::SchemefulSite::Deserialize("https://d.example")}),
R"json("https://d.example")json",
},
{
*DestinationSet::Create(
{net::SchemefulSite::Deserialize("https://d.example"),
net::SchemefulSite::Deserialize("https://e.example"),
net::SchemefulSite::Deserialize("https://f.example")}),
R"json(
["https://d.example","https://e.example","https://f.example"]
)json",
},
};
for (const auto& test_case : kTestCases) {
EXPECT_THAT(test_case.input.ToJson(),
base::test::IsJson(test_case.expected_json));
}
}
} // namespace
} // namespace attribution_reporting
|