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
|
// 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/aggregatable_dedup_key.h"
#include "base/test/gmock_expected_support.h"
#include "base/test/values_test_util.h"
#include "base/time/time.h"
#include "base/types/expected.h"
#include "base/values.h"
#include "components/attribution_reporting/filters.h"
#include "components/attribution_reporting/test_utils.h"
#include "components/attribution_reporting/trigger_registration_error.mojom.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace attribution_reporting {
namespace {
using ::attribution_reporting::mojom::TriggerRegistrationError;
using ::base::test::ErrorIs;
using ::base::test::ValueIs;
using ::testing::AllOf;
using ::testing::Field;
TEST(AggregatableDedupKeyTest, FromJSON) {
const struct {
const char* description;
const char* json;
::testing::Matcher<
base::expected<AggregatableDedupKey, TriggerRegistrationError>>
matches;
} kTestCases[] = {
{
"empty",
R"json({})json",
ValueIs(AllOf(Field(&AggregatableDedupKey::dedup_key, std::nullopt),
Field(&AggregatableDedupKey::filters, FilterPair()))),
},
{
"dedup_key_valid",
R"json({"deduplication_key":"3"})json",
ValueIs(Field(&AggregatableDedupKey::dedup_key, 3)),
},
{
"dedup_key_wrong_type",
R"json({"deduplication_key":123})json",
ErrorIs(TriggerRegistrationError::kAggregatableDedupKeyValueInvalid),
},
{
"dedup_key_invalid",
R"json({"deduplication_key":"abc"})json",
ErrorIs(TriggerRegistrationError::kAggregatableDedupKeyValueInvalid),
},
{
"filters_valid",
R"json({"filters":{"a":["b"], "_lookback_window": 1}})json",
ValueIs(Field(&AggregatableDedupKey::filters,
FilterPair(/*positive=*/{*FilterConfig::Create(
{{{"a", {"b"}}}},
/*lookback_window=*/base::Seconds(1))},
/*negative=*/FiltersDisjunction()))),
},
{
"filters_wrong_type",
R"json({"filters":123})json",
ErrorIs(TriggerRegistrationError::kFiltersWrongType),
},
{
"not_filters_valid",
R"json({"not_filters":{"a":["b"], "_lookback_window": 1}})json",
ValueIs(Field(&AggregatableDedupKey::filters,
FilterPair(
/*positive=*/FiltersDisjunction(),
/*negative=*/{*FilterConfig::Create(
{{{"a", {"b"}}}},
/*lookback_window=*/base::Seconds(1))}))),
},
{
"not_filters_wrong_type",
R"json({"not_filters":123})json",
ErrorIs(TriggerRegistrationError::kFiltersWrongType),
},
};
for (const auto& test_case : kTestCases) {
SCOPED_TRACE(test_case.description);
base::Value value = base::test::ParseJson(test_case.json);
EXPECT_THAT(AggregatableDedupKey::FromJSON(value), test_case.matches);
}
}
TEST(AggregatableDedupKeyTest, ToJson) {
const struct {
AggregatableDedupKey input;
const char* expected_json;
} kTestCases[] = {
{
AggregatableDedupKey(),
R"json({})json",
},
{
AggregatableDedupKey(
/*dedup_key=*/3,
FilterPair(/*positive=*/{*FilterConfig::Create({{"a", {}}})},
/*negative=*/{*FilterConfig::Create({{"b", {}}})})),
R"json({
"deduplication_key": "3",
"filters": [{"a": []}],
"not_filters": [{"b": []}]
})json",
},
};
for (const auto& test_case : kTestCases) {
EXPECT_THAT(test_case.input.ToJson(),
base::test::IsJson(test_case.expected_json));
}
}
} // namespace
} // namespace attribution_reporting
|