File: interest_group_test_utils.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (276 lines) | stat: -rw-r--r-- 12,312 bytes parent folder | download | duplicates (6)
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "third_party/blink/public/common/interest_group/test/interest_group_test_utils.h"

#include <stddef.h>

#include <optional>
#include <string_view>
#include <vector>

#include "base/check.h"
#include "base/containers/flat_map.h"
#include "base/strings/strcat.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/common/interest_group/ad_display_size.h"
#include "third_party/blink/public/common/interest_group/interest_group.h"

namespace blink {

namespace {

// Macros are used to keep the field names in the failure output of EXPECT_EQ().
// These should only be used to implement InterestGroupCompare(), and #undef'd
// after.

// Compare `actual` and `expected`, either expecting equality, or non-equality,
// depending on the value of `expect_equals` passed to InterestGroupCompare().
#define IG_COMPARE(actual, expected) \
  if (expect_equals) {               \
    EXPECT_EQ(actual, expected);     \
  } else {                           \
    if (actual != expected) {        \
      found_unequal = true;          \
    }                                \
  }

// Vectors and maps are a special case -- a parameter `func` is used to compare
// individual elements of the std::vector. IG_COMPARE_MAP() supports
// base::flat_map. std::optional-wrapped vectors and maps are also allowed.
//
// NOTE: Do **NOT** pass a lambda literal directly as `func`, as commas in the
// lambda definition may get mishandled by the  preprocessor, and lines get
// concatenated (making debugging harder). Instead, assign the lambda to a
// variable using "auto", then pass that.
#define IG_COMPARE_VEC(actual, expected, func)                              \
  IgCompareVecInternal(#actual, #expected, actual, expected, expect_equals, \
                       found_unequal, func)

#define IG_COMPARE_MAP(actual, expected, func)                              \
  IgCompareMapInternal(#actual, #expected, actual, expected, expect_equals, \
                       found_unequal, func)

// NOTE: Template template parameters could have been used here to match any
// list-like or map-like type, but the downside is they add complexity and can
// overmatch against non-desired types. Since we only use std::vector and
// base::flat_map, it makes sense to just manually implement those types. C++
// concepts might make it easier to be more general here in the future.

// Helper for IG_COMPARE_VEC() -- do not call directly.
//
// Handles plain std::vector instances *not* wrapped in std::optional.
template <typename T, typename Func>
void IgCompareVecInternal(std::string_view a_name,
                          std::string_view b_name,
                          const std::vector<T>& actual,
                          const std::vector<T>& expected,
                          const bool expect_equals,
                          bool& found_unequal,
                          Func f) {
  SCOPED_TRACE(base::StrCat({a_name, " and ", b_name}));
  IG_COMPARE(actual.size(), expected.size());
  if (actual.size() == expected.size()) {
    for (size_t i = 0; i < actual.size(); i++) {
      SCOPED_TRACE(i);
      f(actual[i], expected[i]);
    }
  }
}

// Helper for IG_COMPARE_VEC() -- do not call directly.
//
// Handles plain std::vector instances that *are* wrapped in std::optional.
template <typename T, typename Func>
void IgCompareVecInternal(std::string_view a_name,
                          std::string_view b_name,
                          const std::optional<std::vector<T>>& actual,
                          const std::optional<std::vector<T>>& expected,
                          const bool expect_equals,
                          bool& found_unequal,
                          Func f) {
  SCOPED_TRACE(base::StrCat({a_name, " and ", b_name}));
  IG_COMPARE(actual.has_value(), expected.has_value());
  if (actual && expected) {
    IgCompareVecInternal(a_name, b_name, *actual, *expected, expect_equals,
                         found_unequal, f);
  }
}

// Helper for IG_COMPARE_MAP() -- do not call directly.
//
// Handles plain base::flat_map instances *not* wrapped in std::optional.
template <typename K, typename V, typename Func>
void IgCompareMapInternal(std::string_view a_name,
                          std::string_view b_name,
                          const base::flat_map<K, V>& actual,
                          const base::flat_map<K, V>& expected,
                          const bool expect_equals,
                          bool& found_unequal,
                          Func f) {
  SCOPED_TRACE(base::StrCat({a_name, " and ", b_name}));
  IG_COMPARE(actual.size(), expected.size());
  if (actual.size() == expected.size()) {
    // NOTE: The correctness of this loop construction depends on the fact that
    // base::flat_map stores elements in sorted key order, so if `actual` and
    // `expected` are equal, their keys will have the same iteration order.
    size_t i = 0;
    for (auto a_it = actual.begin(), b_it = expected.begin();
         a_it != actual.end(); a_it++, b_it++, i++) {
      SCOPED_TRACE(i);
      CHECK(b_it != expected.end());
      // Since interest groups must be representable in JSON (for interest group
      // updates), key types must be strings in JSON. In C++, they are typically
      // either std::string, or url::Origin -- both of which support
      // operator==() and operator<<(). So, it's not necessary to have a
      // separate function passed in for comparing key types.
      IG_COMPARE(a_it->first, b_it->first);
      f(a_it->second, b_it->second);
    }
  }
}

// Helper for IG_COMPARE_MAP() -- do not call directly.
//
// Handles plain base::flat_map instances that *are* wrapped in std::optional.
template <typename K, typename V, typename Func>
void IgCompareMapInternal(std::string_view a_name,
                          std::string_view b_name,
                          const std::optional<base::flat_map<K, V>>& actual,
                          const std::optional<base::flat_map<K, V>>& expected,
                          const bool expect_equals,
                          bool& found_unequal,
                          Func f) {
  SCOPED_TRACE(base::StrCat({a_name, " and ", b_name}));
  IG_COMPARE(actual.has_value(), expected.has_value());
  if (actual && expected) {
    IgCompareMapInternal(a_name, b_name, *actual, *expected, expect_equals,
                         found_unequal, f);
  }
}

// Compares all fields and subfields of blink::InterestGroup using the
// IG_COMPARE*() macros implemented above.
//
// Used to implement IgExpectEqualsForTesting() and
// IgExpectNotEqualsForTesting().
//
// Technically `expected` is `not_expected` in the IgExpectNotEqualsForTesting()
// case, but only the `found_unequal` expectation can fail in that case. For the
// IgExpectEqualsForTesting() case, the name `expected` is appropriate for error
// messages.
void InterestGroupCompare(const blink::InterestGroup& actual,
                          const blink::InterestGroup& expected,
                          bool expect_equals) {
  bool found_unequal = false;

  IG_COMPARE(actual.expiry, expected.expiry);
  IG_COMPARE(actual.owner, expected.owner);
  IG_COMPARE(actual.name, expected.name);
  IG_COMPARE(actual.priority, expected.priority);
  IG_COMPARE(actual.enable_bidding_signals_prioritization,
             expected.enable_bidding_signals_prioritization);
  auto compare_doubles = [&](double actual, double expected) {
    IG_COMPARE(actual, expected);
  };
  IG_COMPARE_MAP(actual.priority_vector, expected.priority_vector,
                 compare_doubles);
  IG_COMPARE_MAP(actual.priority_signals_overrides,
                 expected.priority_signals_overrides, compare_doubles);
  auto compare_seller_capabilities = [&](SellerCapabilitiesType actual,
                                         SellerCapabilitiesType expected) {
    IG_COMPARE(actual, expected);
  };
  IG_COMPARE_MAP(actual.seller_capabilities, expected.seller_capabilities,
                 compare_seller_capabilities);
  IG_COMPARE(actual.all_sellers_capabilities,
             expected.all_sellers_capabilities);
  IG_COMPARE(actual.execution_mode, expected.execution_mode);
  IG_COMPARE(actual.bidding_url, expected.bidding_url);
  IG_COMPARE(actual.bidding_wasm_helper_url, expected.bidding_wasm_helper_url);
  IG_COMPARE(actual.update_url, expected.update_url);
  IG_COMPARE(actual.trusted_bidding_signals_url,
             expected.trusted_bidding_signals_url);
  auto compare_strings = [&](const std::string& actual,
                             const std::string& expected) {
    IG_COMPARE(actual, expected);
  };
  IG_COMPARE_VEC(actual.trusted_bidding_signals_keys,
                 expected.trusted_bidding_signals_keys, compare_strings);
  IG_COMPARE(actual.trusted_bidding_signals_slot_size_mode,
             expected.trusted_bidding_signals_slot_size_mode);
  IG_COMPARE(actual.max_trusted_bidding_signals_url_length,
             expected.max_trusted_bidding_signals_url_length);
  IG_COMPARE(actual.trusted_bidding_signals_coordinator,
             expected.trusted_bidding_signals_coordinator);
  auto compare_origins = [&](const url::Origin& actual,
                             const url::Origin& expected) {
    IG_COMPARE(actual, expected);
  };
  IG_COMPARE_VEC(actual.view_and_click_counts_providers,
                 expected.view_and_click_counts_providers, compare_origins);
  IG_COMPARE(actual.user_bidding_signals, expected.user_bidding_signals);
  auto compare_ads = [&](const blink::InterestGroup::Ad& actual,
                         const blink::InterestGroup::Ad& expected) {
    IG_COMPARE(actual.render_url(), expected.render_url());
    IG_COMPARE(actual.size_group, expected.size_group);
    IG_COMPARE(actual.metadata, expected.metadata);
    IG_COMPARE(actual.buyer_reporting_id, expected.buyer_reporting_id);
    IG_COMPARE(actual.buyer_and_seller_reporting_id,
               expected.buyer_and_seller_reporting_id);
    IG_COMPARE_VEC(actual.selectable_buyer_and_seller_reporting_ids,
                   expected.selectable_buyer_and_seller_reporting_ids,
                   compare_strings);
    IG_COMPARE(actual.ad_render_id, expected.ad_render_id);

    IG_COMPARE_VEC(actual.allowed_reporting_origins,
                   expected.allowed_reporting_origins, compare_origins);
    IG_COMPARE(actual.creative_scanning_metadata,
               expected.creative_scanning_metadata);
  };
  IG_COMPARE_VEC(actual.ads, expected.ads, compare_ads);
  IG_COMPARE_VEC(actual.ad_components, expected.ad_components, compare_ads);
  auto compare_ad_sizes = [&](const blink::AdSize& actual,
                              const blink::AdSize& expected) {
    IG_COMPARE(actual.width, expected.width);
    IG_COMPARE(actual.width_units, expected.width_units);
    IG_COMPARE(actual.height, expected.height);
    IG_COMPARE(actual.height_units, expected.height_units);
  };
  IG_COMPARE_MAP(actual.ad_sizes, expected.ad_sizes, compare_ad_sizes);
  auto compare_vectors_of_strings =
      [&](const std::vector<std::string>& actual,
          const std::vector<std::string>& expected) {
        IG_COMPARE_VEC(actual, expected, compare_strings);
      };
  IG_COMPARE_MAP(actual.size_groups, expected.size_groups,
                 compare_vectors_of_strings);
  IG_COMPARE(actual.auction_server_request_flags,
             expected.auction_server_request_flags);
  IG_COMPARE(actual.additional_bid_key, expected.additional_bid_key);
  IG_COMPARE(actual.aggregation_coordinator_origin,
             expected.aggregation_coordinator_origin);

  if (!expect_equals) {
    EXPECT_TRUE(found_unequal);
  }
}

#undef IG_COMPARE_MAP
#undef IG_COMPARE_VEC
#undef IG_COMPARE

}  // namespace

void IgExpectEqualsForTesting(const blink::InterestGroup& actual,
                              const blink::InterestGroup& expected) {
  InterestGroupCompare(actual, expected, /*expect_equals=*/true);
}

void IgExpectNotEqualsForTesting(const blink::InterestGroup& actual,
                                 const blink::InterestGroup& not_expected) {
  InterestGroupCompare(actual, not_expected, /*expect_equals=*/false);
}

}  // namespace blink