summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/fledge/tentative/join-leave-ad-interest-group-in-fenced-frame.https.window.js
blob: e6836ab2f45a873a3d5bd3de9f74c92424782bec (plain)
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
// META: script=/resources/testdriver.js
// META: script=/common/utils.js
// META: script=resources/fledge-util.sub.js
// META: script=/common/subset-tests.js
// META: timeout=long
// META: variant=?1-4
// META: variant=?5-8
// META: variant=?9-last

"use strict;"

// These are separate from the other join-leave tests because these all create
// and navigate fenced frames, which is much slower than just joining/leaving
// interest groups, and running the occasional auction. Most tests use a
// buyer with an origin of OTHER_ORIGIN1, so it has a distinct origin from the
// seller and publisher.

// Creates a tracker URL that's requested when a call succeeds in a fenced
// frame.
function createSuccessURL(uuid, origin = document.location.origin) {
  return createTrackerURL(origin, uuid, "track_get", "success");
}

// Creates a tracker URL that's requested when a call fails in a fenced frame, with
// the expected exception.
function createExceptionURL(uuid, origin = document.location.origin) {
  return createTrackerURL(origin, uuid, "track_get", "exception");
}

// Creates a tracker URL that's requested when joinAdInterestGroup() or
// leaveAdInterestGroup() fails with an exception other than the one that's
// expected.
function createBadExceptionURL(uuid, origin = document.location.origin) {
  return createTrackerURL(origin, uuid, "track_get", "bad_exception");
}

// Creates render URL that calls "navigator.leaveAdInterestGroup()" when
// loaded, with no arguments. It then fetches a URL depending on whether it
// threw an exception. No exception should ever be thrown when this is run
// in an ad URL, so only fetch the "bad exception" URL on error.
function createNoArgsTryLeaveRenderURL(uuid, origin = document.location.origin) {
  return createRenderURL(
    uuid,
    `async function TryLeave() {
       try {
         await navigator.leaveAdInterestGroup();
         await fetch("${createSuccessURL(uuid, origin)}");
       } catch (e) {
         await fetch("${createBadExceptionURL(uuid, origin)}");
       }
     }

     TryLeave();`,
    /*signalsParams=*/null,
    origin);
}

subsetTest(promise_test, async test => {
  const uuid = generateUuid(test);

  // Interest group that an ad fenced frame attempts to join. The join should
  // fail.
  let interestGroupJoinedInFrame = createInterestGroupForOrigin(
      uuid, document.location.origin, {name: 'group2'});

  // Create a render URL that tries to join "interestGroupJoinedInFrame".
  const renderURL = createRenderURL(
    uuid,
    `async function TryJoin() {
       try {
         await navigator.joinAdInterestGroup(
             ${JSON.stringify(interestGroupJoinedInFrame)});
         await fetch("${createSuccessURL(uuid)}");
       } catch (e) {
         if (e instanceof DOMException && e.name === "NotAllowedError") {
           await fetch("${createExceptionURL(uuid)}");
         } else {
           await fetch("${createBadExceptionURL(uuid)}");
         }
       }
     }

     TryJoin();`);

  await joinInterestGroup(test, uuid, {ads: [{ renderURL: renderURL}]});

  await runBasicFledgeAuctionAndNavigate(test, uuid);

  // This should wait until the leave call has thrown an exception.
  await waitForObservedRequests(
      uuid,
      [createBidderReportURL(uuid), createSellerReportURL(uuid), createExceptionURL(uuid)]);

  // Leave the initial interest group.
  await leaveInterestGroup();

  // Check the interest group was not successfully joined in the fenced frame
  // by running an auction, to make sure the thrown exception accurately
  // indicates the group wasn't joined.
  await runBasicFledgeTestExpectingNoWinner(test, uuid);
}, 'joinAdInterestGroup() in ad fenced frame.');

subsetTest(promise_test, async test => {
  const uuid = generateUuid(test);

  // Create a render URL that tries to leave the default test interest group by
  // name. Even a though a render URL can leave its own interest group by using
  // the 0-argument version of leaveAdInterestGroup(), it can't leave its own
  // interest group by using the 1-argument version, so this should fail.
  const renderURL = createRenderURL(
      uuid,
      `async function TryLeave() {
         try {
           await navigator.leaveAdInterestGroup(
               {owner: "${window.location.origin}", name: "${DEFAULT_INTEREST_GROUP_NAME}"});
           await fetch("${createSuccessURL(uuid)}");
         } catch (e) {
           if (e instanceof DOMException && e.name === "NotAllowedError") {
             await fetch("${createExceptionURL(uuid)}");
           } else {
             await fetch("${createBadExceptionURL(uuid)}");
           }
         }
       }

       TryLeave();`);

  await joinInterestGroup(
      test, uuid,
      {ads: [{ renderURL: renderURL}]});

  await runBasicFledgeAuctionAndNavigate(test, uuid);

  // This should wait until the leave call has thrown an exception.
  await waitForObservedRequests(
      uuid,
      [createBidderReportURL(uuid), createSellerReportURL(uuid), createExceptionURL(uuid)]);

  // Check the interest group was not left.
  await runBasicFledgeTestExpectingWinner(test, uuid);
}, 'leaveAdInterestGroup() in ad fenced frame, specify an interest group.');

subsetTest(promise_test, async test => {
  const uuid = generateUuid(test);

  const bidder_origin = OTHER_ORIGIN1;
  const render_url_origin = window.location.origin;

  await joinCrossOriginInterestGroup(
      test, uuid, bidder_origin,
      {ads: [{ renderURL: createNoArgsTryLeaveRenderURL(uuid, render_url_origin) }]});

  await runBasicFledgeAuctionAndNavigate(test, uuid, {interestGroupBuyers : [bidder_origin]});

  // Leaving the interest group should claim to succeed, to avoid leaking
  // whether or not the buyer was same-origin or to the fenced frame.
  await waitForObservedRequests(
      uuid,
      [ createBidderReportURL(uuid), createSellerReportURL(uuid),
        createSuccessURL(uuid, render_url_origin)]);

  // Check the interest group was not actually left.
  await runBasicFledgeTestExpectingWinner(test, uuid, {interestGroupBuyers : [bidder_origin]});
}, 'leaveAdInterestGroup() in non-buyer origin ad fenced frame, no parameters.');

subsetTest(promise_test, async test => {
  const uuid = generateUuid(test);

  const bidder_origin = OTHER_ORIGIN1;
  const render_url_origin = OTHER_ORIGIN1;

  // Use a different origin for the buyer, to make sure that's the origin
  // that matters.
  await joinCrossOriginInterestGroup(
      test, uuid, bidder_origin,
      {ads: [{ renderURL: createNoArgsTryLeaveRenderURL(uuid, render_url_origin) }]});

  await runBasicFledgeAuctionAndNavigate(test, uuid, {interestGroupBuyers : [bidder_origin]});

  // This should wait until the leave call has completed.
  await waitForObservedRequests(
      uuid,
      [ createBidderReportURL(uuid), createSellerReportURL(uuid),
        createSuccessURL(uuid, render_url_origin)]);

  // Check the interest group was actually left.
  await runBasicFledgeTestExpectingNoWinner(test, uuid, {interestGroupBuyers : [bidder_origin]});
}, 'leaveAdInterestGroup() in buyer origin ad fenced frame, no parameters.');

subsetTest(promise_test, async test => {
  const uuid = generateUuid(test);

  const bidder_origin = OTHER_ORIGIN1;
  const render_url_origin = OTHER_ORIGIN1;
  const iframe_origin = OTHER_ORIGIN1;

  // Create a render URL which, in an iframe, loads the common "try leave"
  // render URL from the buyer's origin (which isn't technically being used as
  // a render URL, in this case).
  const renderURL = createRenderURL(
      uuid,
      `let iframe = document.createElement("iframe");
       iframe.permissions = "join-ad-interest-group";
       iframe.src = "${createNoArgsTryLeaveRenderURL(uuid, iframe_origin)}";
       document.body.appendChild(iframe);`,
      /*signalsParams=*/null,
      render_url_origin);

  await joinCrossOriginInterestGroup(
      test, uuid, bidder_origin,
      {ads: [{ renderURL: renderURL }]});

  await runBasicFledgeAuctionAndNavigate(test, uuid, {interestGroupBuyers : [bidder_origin]});

  // This should wait until the leave call has completed.
  await waitForObservedRequests(
      uuid,
      [ createBidderReportURL(uuid), createSellerReportURL(uuid),
        createSuccessURL(uuid, iframe_origin)]);

  // Check the interest group was actually left.
  await runBasicFledgeTestExpectingNoWinner(test, uuid, {interestGroupBuyers : [bidder_origin]});
}, 'leaveAdInterestGroup() in same-origin iframe inside buyer origin ad fenced frame, no parameters.');

subsetTest(promise_test, async test => {
  const uuid = generateUuid(test);

  const bidder_origin = OTHER_ORIGIN1;
  const render_url_origin = OTHER_ORIGIN1;
  const iframe_origin = document.location.origin;

  // Create a render URL which, in an iframe, loads the common "try leave"
  // render URL from an origin other than the buyer's origin.
  const renderURL = createRenderURL(
      uuid,
      `let iframe = document.createElement("iframe");
       iframe.permissions = "join-ad-interest-group";
       iframe.src = "${createNoArgsTryLeaveRenderURL(uuid, iframe_origin)}";
       document.body.appendChild(iframe);`,
      /*signalsParams=*/null,
      render_url_origin);

  await joinCrossOriginInterestGroup(
      test, uuid, bidder_origin,
      {ads: [{ renderURL: renderURL }]});

  await runBasicFledgeAuctionAndNavigate(test, uuid, {interestGroupBuyers : [bidder_origin]});

  // Leaving the interest group should claim to succeed, to avoid leaking
  // whether or not the buyer was same-origin or to the iframe.
  await waitForObservedRequests(
      uuid,
      [ createBidderReportURL(uuid), createSellerReportURL(uuid),
        createSuccessURL(uuid, iframe_origin)]);

  // Check the interest group was not actually left.
  await runBasicFledgeTestExpectingWinner(test, uuid, {interestGroupBuyers : [bidder_origin]});
}, 'leaveAdInterestGroup() in cross-origin iframe inside buyer origin ad fenced frame, no parameters.');

subsetTest(promise_test, async test => {
  const uuid = generateUuid(test);

  const bidder_origin = OTHER_ORIGIN1;
  const render_url_origin = document.location.origin;
  const iframe_origin = document.location.origin;

  // Create a render URL which, in an iframe, loads the common "try leave"
  // render URL from an origin other than the buyer's origin (which isn't
  // technically being used as a render URL, in this case).
  const renderURL = createRenderURL(
      uuid,
      `let iframe = document.createElement("iframe");
       iframe.permissions = "join-ad-interest-group";
       iframe.src = "${createNoArgsTryLeaveRenderURL(uuid, iframe_origin)}";
       document.body.appendChild(iframe);`,
      /*signalsParams=*/null,
      render_url_origin);

  await joinCrossOriginInterestGroup(
      test, uuid, bidder_origin,
      {ads: [{ renderURL: renderURL }]});

  await runBasicFledgeAuctionAndNavigate(test, uuid, {interestGroupBuyers : [bidder_origin]});

  // Leaving the interest group should claim to succeed, to avoid leaking
  // whether or not the buyer was same-origin or to the fenced frame.
  await waitForObservedRequests(
      uuid,
      [ createBidderReportURL(uuid), createSellerReportURL(uuid),
        createSuccessURL(uuid, iframe_origin)]);

  // Check the interest group was not actually left.
  await runBasicFledgeTestExpectingWinner(test, uuid, {interestGroupBuyers : [bidder_origin]});
}, 'leaveAdInterestGroup() in same-origin iframe inside non-buyer origin ad fenced frame, no parameters.');

subsetTest(promise_test, async test => {
  const uuid = generateUuid(test);

  const bidder_origin = OTHER_ORIGIN1;
  const render_url_origin = document.location.origin;
  const iframe_origin = OTHER_ORIGIN1;

  // Create a render URL which, in an iframe, loads the common "try leave"
  // render URL from the buyer's origin (which isn't technically being used as
  // a render URL, in this case).
  const renderURL = createRenderURL(
      uuid,
      `let iframe = document.createElement("iframe");
       iframe.permissions = "join-ad-interest-group";
       iframe.src = "${createNoArgsTryLeaveRenderURL(uuid, iframe_origin)}";
       document.body.appendChild(iframe);`,
      /*signalsParams=*/null,
      render_url_origin);

  await joinCrossOriginInterestGroup(
      test, uuid, bidder_origin,
      {ads: [{ renderURL: renderURL }]});

  await runBasicFledgeAuctionAndNavigate(test, uuid, {interestGroupBuyers : [bidder_origin]});
  // Leaving the interest group should succeed.
  await waitForObservedRequests(
      uuid,
      [ createBidderReportURL(uuid), createSellerReportURL(uuid),
        createSuccessURL(uuid, iframe_origin)]);

  // Check the interest group was left.
  await runBasicFledgeTestExpectingNoWinner(test, uuid, {interestGroupBuyers : [bidder_origin]});
}, 'leaveAdInterestGroup() in cross-origin buyer iframe inside non-buyer origin ad fenced frame, no parameters.');

subsetTest(promise_test, async test => {
  const uuid = generateUuid(test);

  // Render URL that loads the first ad component in a nested fenced frame.
  let loadFirstComponentAdURL =
      createRenderURL(
        uuid,
        `let fencedFrame = document.createElement("fencedframe");
         fencedFrame.mode = "opaque-ads";
         fencedFrame.config = window.fence.getNestedConfigs()[0];
         document.body.appendChild(fencedFrame);`,
        /*signalsParams=*/null,
        OTHER_ORIGIN1);

  await joinInterestGroup(
      test, uuid,
      // Interest group that makes a bid with a component ad. The render URL
      // will open the component ad in a fenced frame, and the component ad
      // URL is the common URL that tries to leave the ad's current interest
      // group, reporting the result to a tracker URL.
      { biddingLogicURL: createBiddingScriptURL(
          { generateBid: `return {
                            bid: 1,
                            render: interestGroup.ads[0].renderURL,
                            adComponents: [interestGroup.adComponents[0].renderURL]
                          };` }),
        ads: [{ renderURL: loadFirstComponentAdURL }],
        adComponents: [{ renderURL: createNoArgsTryLeaveRenderURL(uuid) }]});

  await runBasicFledgeAuctionAndNavigate(test, uuid);

  // Leaving the interest group should claim to succeed, to avoid leaking
  // whether or not the buyer was same-origin or to the fenced frame.
  await waitForObservedRequests(
      uuid,
      [createSellerReportURL(uuid), createSuccessURL(uuid)]);

  // Check the interest group was left.
  await runBasicFledgeTestExpectingNoWinner(test, uuid);
}, 'leaveAdInterestGroup() in component ad fenced frame, no parameters.');