[go: nahoru, domu]

blob: 7ed8776f34497d2a745a6b02b842e2ea68b418f1 [file] [log] [blame]
Nan Lin6774c0c2023-11-30 12:17:551// Copyright 2023 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 "components/attribution_reporting/aggregatable_trigger_config.h"
6
Arthur Sonzognic571efb2024-01-26 20:26:187#include <optional>
Nan Lin6774c0c2023-11-30 12:17:558#include <string>
9
10#include "base/test/gmock_expected_support.h"
11#include "base/test/scoped_feature_list.h"
12#include "base/test/values_test_util.h"
13#include "base/values.h"
14#include "components/attribution_reporting/features.h"
15#include "components/attribution_reporting/source_registration_time_config.mojom.h"
16#include "components/attribution_reporting/test_utils.h"
17#include "components/attribution_reporting/trigger_registration_error.mojom.h"
18#include "testing/gmock/include/gmock/gmock.h"
19#include "testing/gtest/include/gtest/gtest.h"
Nan Lin6774c0c2023-11-30 12:17:5520
21namespace attribution_reporting {
22namespace {
23
24using ::attribution_reporting::mojom::SourceRegistrationTimeConfig;
25using ::attribution_reporting::mojom::TriggerRegistrationError;
26using ::base::test::ErrorIs;
27using ::base::test::ValueIs;
28using ::testing::Optional;
29using ::testing::Property;
30
31TEST(AggregatableTriggerConfigTest, ParseAggregatableSourceRegistrationTime) {
32 const struct {
33 const char* desc;
34 const char* json;
35 ::testing::Matcher<
36 base::expected<AggregatableTriggerConfig, TriggerRegistrationError>>
37 matches;
38 } kTestCases[] = {
39 {
40 "empty",
41 R"json({})json",
42 ValueIs(AggregatableTriggerConfig()),
43 },
44 {
45 "aggregatable_source_registration_time_include",
46 R"json({"aggregatable_source_registration_time":"include"})json",
47 ValueIs(Property(
48 &AggregatableTriggerConfig::source_registration_time_config,
49 SourceRegistrationTimeConfig::kInclude)),
50 },
51 {
52 "aggregatable_source_registration_time_exclude",
53 R"json({"aggregatable_source_registration_time":"exclude"})json",
54 ValueIs(Property(
55 &AggregatableTriggerConfig::source_registration_time_config,
56 SourceRegistrationTimeConfig::kExclude)),
57 },
58 {
59 "aggregatable_source_registration_time_wrong_type",
60 R"json({"aggregatable_source_registration_time":123})json",
61 ErrorIs(TriggerRegistrationError::
62 kAggregatableSourceRegistrationTimeWrongType),
63 },
64 {
65 "aggregatable_source_registration_time_invalid_value",
66 R"json({"aggregatable_source_registration_time":"unknown"})json",
67 ErrorIs(TriggerRegistrationError::
68 kAggregatableSourceRegistrationTimeUnknownValue),
69 },
70 };
71
72 for (const auto& test_case : kTestCases) {
73 SCOPED_TRACE(test_case.desc);
74
75 base::Value::Dict input = base::test::ParseJsonDict(test_case.json);
76
77 EXPECT_THAT(AggregatableTriggerConfig::Parse(input), test_case.matches);
78 }
79}
80
81TEST(AggregatableTriggerConfigTest, ParseTriggerContextId) {
82 const struct {
83 const char* desc;
84 const char* json;
85 ::testing::Matcher<
86 base::expected<AggregatableTriggerConfig, TriggerRegistrationError>>
87 enabled_matches;
88 ::testing::Matcher<
89 base::expected<AggregatableTriggerConfig, TriggerRegistrationError>>
90 disabled_matches;
91 } kTestCases[] = {
92 {
93 "empty",
94 R"json({})json",
95 ValueIs(AggregatableTriggerConfig()),
96 ValueIs(AggregatableTriggerConfig()),
97 },
98 {
99 "trigger_context_id_valid",
100 R"json({"trigger_context_id":"123"})json",
101 ValueIs(Property(&AggregatableTriggerConfig::trigger_context_id,
102 Optional(std::string("123")))),
103 ValueIs(AggregatableTriggerConfig()),
104 },
105 {
106 "trigger_context_id_wrong_type",
107 R"json({"trigger_context_id":123})json",
108 ErrorIs(TriggerRegistrationError::kTriggerContextIdInvalidValue),
109 ValueIs(AggregatableTriggerConfig()),
110 },
111 {
112 "trigger_context_id_invalid_value",
113 R"json({"trigger_context_id":""})json",
114 ErrorIs(TriggerRegistrationError::kTriggerContextIdInvalidValue),
115 ValueIs(AggregatableTriggerConfig()),
116 },
117 {
118 "trigger_context_id_disallowed",
119 R"json({
120 "aggregatable_source_registration_time":"include",
121 "trigger_context_id":"123"
122 })json",
123 ErrorIs(TriggerRegistrationError::
124 kTriggerContextIdInvalidSourceRegistrationTimeConfig),
125 ValueIs(*AggregatableTriggerConfig::Create(
126 SourceRegistrationTimeConfig::kInclude,
Arthur Sonzognic571efb2024-01-26 20:26:18127 /*trigger_context_id=*/std::nullopt)),
Nan Lin6774c0c2023-11-30 12:17:55128 },
129 };
130
131 for (const auto& test_case : kTestCases) {
132 SCOPED_TRACE(test_case.desc);
133
134 base::Value::Dict input = base::test::ParseJsonDict(test_case.json);
135
136 {
137 SCOPED_TRACE("disabled");
138
139 base::test::ScopedFeatureList scoped_feature_list;
140 scoped_feature_list.InitAndDisableFeature(
141 features::kAttributionReportingTriggerContextId);
142
143 EXPECT_THAT(AggregatableTriggerConfig::Parse(input),
144 test_case.disabled_matches);
145 }
146
147 {
148 SCOPED_TRACE("enabled");
149
150 base::test::ScopedFeatureList scoped_feature_list(
151 features::kAttributionReportingTriggerContextId);
152
153 EXPECT_THAT(AggregatableTriggerConfig::Parse(input),
154 test_case.enabled_matches);
155 }
156 }
157}
158
159TEST(AggregatableTriggerConfigTest, Create) {
160 const struct {
161 const char* desc;
162 SourceRegistrationTimeConfig source_registration_time_config;
Arthur Sonzognic571efb2024-01-26 20:26:18163 std::optional<std::string> trigger_context_id;
164 std::optional<AggregatableTriggerConfig> expected;
Nan Lin6774c0c2023-11-30 12:17:55165 } kTestCases[] = {
166 {
167 "valid_exclude_source_registration_time_with_trigger_context_id",
168 SourceRegistrationTimeConfig::kExclude,
169 "123",
170 *AggregatableTriggerConfig::Create(
171 SourceRegistrationTimeConfig::kExclude, "123"),
172 },
173 {
174 "valid_exclude_source_registration_time_without_trigger_context_id",
175 SourceRegistrationTimeConfig::kExclude,
Arthur Sonzognic571efb2024-01-26 20:26:18176 std::nullopt,
Nan Lin6774c0c2023-11-30 12:17:55177 AggregatableTriggerConfig(),
178 },
179 {
180 "valid_include_source_registration_time_without_trigger_context_id",
181 SourceRegistrationTimeConfig::kInclude,
Arthur Sonzognic571efb2024-01-26 20:26:18182 std::nullopt,
Nan Lin6774c0c2023-11-30 12:17:55183 *AggregatableTriggerConfig::Create(
Arthur Sonzognic571efb2024-01-26 20:26:18184 SourceRegistrationTimeConfig::kInclude, std::nullopt),
Nan Lin6774c0c2023-11-30 12:17:55185 },
186 {
187 "trigger_context_id_empty",
188 SourceRegistrationTimeConfig::kExclude,
189 "",
Arthur Sonzognic571efb2024-01-26 20:26:18190 std::nullopt,
Nan Lin6774c0c2023-11-30 12:17:55191 },
192 {
193 "trigger_context_id_too_long",
194 SourceRegistrationTimeConfig::kExclude,
195 std::string(65, 'a'),
Arthur Sonzognic571efb2024-01-26 20:26:18196 std::nullopt,
Nan Lin6774c0c2023-11-30 12:17:55197 },
198 {
199 "trigger_context_id_disallowed",
200 SourceRegistrationTimeConfig::kInclude,
201 "123",
Arthur Sonzognic571efb2024-01-26 20:26:18202 std::nullopt,
Nan Lin6774c0c2023-11-30 12:17:55203 },
204 };
205
206 for (const auto& test_case : kTestCases) {
207 SCOPED_TRACE(test_case.desc);
208
209 EXPECT_EQ(AggregatableTriggerConfig::Create(
210 test_case.source_registration_time_config,
211 test_case.trigger_context_id),
212 test_case.expected);
213 }
214}
215
216TEST(AggregatableTriggerConfigTest, Parse_TriggerContextIdLength) {
217 base::test::ScopedFeatureList scoped_feature_list(
218 features::kAttributionReportingTriggerContextId);
219
220 constexpr char kTriggerContextId[] = "trigger_context_id";
221
222 base::Value::Dict dict;
223 dict.Set(kTriggerContextId, std::string(64, 'a'));
224 EXPECT_THAT(AggregatableTriggerConfig::Parse(dict),
225 ValueIs(Property(&AggregatableTriggerConfig::trigger_context_id,
226 Optional(std::string(64, 'a')))));
227
228 dict.Set(kTriggerContextId, std::string(65, 'a'));
229 EXPECT_THAT(AggregatableTriggerConfig::Parse(dict),
230 ErrorIs(TriggerRegistrationError::kTriggerContextIdInvalidValue));
231}
232
233TEST(AggregatableTriggerConfigTest, Serialize) {
234 const struct {
235 AggregatableTriggerConfig input;
236 const char* expected_json;
237 } kTestCases[] = {
238 {
239 AggregatableTriggerConfig(),
240 R"json({
241 "aggregatable_source_registration_time":"exclude"
242 })json",
243 },
244 {
245 *AggregatableTriggerConfig::Create(
246 SourceRegistrationTimeConfig::kInclude,
Arthur Sonzognic571efb2024-01-26 20:26:18247 /*trigger_context_id=*/std::nullopt),
Nan Lin6774c0c2023-11-30 12:17:55248 R"json({
249 "aggregatable_source_registration_time":"include"
250 })json",
251 },
252 {
253 *AggregatableTriggerConfig::Create(
254 SourceRegistrationTimeConfig::kExclude,
255 /*trigger_context_id=*/"123"),
256 R"json({
257 "aggregatable_source_registration_time":"exclude",
258 "trigger_context_id":"123"
259 })json",
260 },
261 };
262
263 for (const auto& test_case : kTestCases) {
264 SCOPED_TRACE(test_case.input);
265
266 base::Value::Dict dict;
267 test_case.input.Serialize(dict);
268 EXPECT_THAT(dict, base::test::IsJson(test_case.expected_json));
269 }
270}
271
272} // namespace
273} // namespace attribution_reporting