forked from web-platform-tests/wpt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreal-time-reporting.https.window.js
274 lines (247 loc) · 10.4 KB
/
real-time-reporting.https.window.js
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
// META: script=/resources/testdriver.js
// META: script=/resources/testdriver-vendor.js
// META: script=/common/utils.js
// META: script=resources/fledge-util.sub.js
// META: script=third_party/cbor-js/cbor.js
// META: script=/common/subset-tests.js
// META: timeout=long
// META: variant=?1-5
// META: variant=?6-last
'use strict;'
// The tests in this file focus on real time reporting.
// Creates a bidding script located on "origin". The generateBid() method calls
// real time reporting API and returns a bid of "bid".
// The reportWin() method is empty.
function createBiddingScriptURLForRealTimeReporting(origin = null, bid = 1) {
return createBiddingScriptURL({
origin: origin ? origin : new URL(BASE_URL).origin,
bid: bid,
allowComponentAuction: true,
generateBid: `
realTimeReporting.contributeToHistogram({ bucket: 20, priorityWeight: 1});`
});
}
// Creates a decision script that calls real time reporting API.
// The reportResult() method is empty.
function createDecisionScriptURLForRealTimeReporting(uuid) {
return createDecisionScriptURL(uuid, {
scoreAd: `
realTimeReporting.contributeToHistogram({ bucket: 200, priorityWeight: 1});`
});
}
// Delay method that waits for prescribed number of milliseconds.
const delay = ms => new Promise(resolve => step_timeout(resolve, ms));
// Polls the given `origin` to retrieve reports sent there. Once the reports are
// received, returns the list of reports. Returns null if the timeout is reached
// before a report is available.
const pollReports = async (origin, wait_for = 1, timeout = 5000 /*ms*/) => {
const path = '/.well-known/interest-group/real-time-report';
let startTime = performance.now();
let payloads = [];
while (performance.now() - startTime < timeout) {
const resp = await fetch(new URL(path, origin));
const payload = await resp.arrayBuffer();
if (payload.byteLength > 0) {
payloads = payloads.concat(payload);
}
if (payloads.length >= wait_for) {
return payloads;
}
await delay(/*ms=*/ 100);
}
if (payloads.length > 0) {
return payloads;
}
return null;
};
// Verifies that `reports` has 1 report in cbor, which has the expected three
// fields.
// `version` should be 1.
// `histogram` and `platformHistogram` should be objects that pass
// verifyHistogram().
const verifyReports = (reports) => {
assert_equals(reports.length, 1);
const report = CBOR.decode(reports[0]);
assert_own_property(report, 'version');
assert_equals(report.version, 1);
assert_own_property(report, 'histogram');
verifyHistogram(report.histogram, 128, 1024);
assert_own_property(report, 'platformHistogram');
verifyHistogram(report.platformHistogram, 1, 4);
assert_equals(Object.keys(report).length, 3);
};
// Verifies that a `histogram` has two fields: "buckets" and "length", where
// "buckets" field is a Uint8Array of `bucketSize`, and "length" field equals to
// `length`.
const verifyHistogram = (histogram, bucketSize, length) => {
assert_own_property(histogram, 'buckets');
assert_own_property(histogram, 'length');
assert_equals(Object.keys(histogram).length, 2);
assert_true(histogram.buckets instanceof Uint8Array);
assert_equals(histogram.buckets.length, bucketSize);
assert_equals(histogram.length, length);
};
const resetWptServer = () => Promise.all([
resetReports('/.well-known/interest-group/real-time-report'),
]);
// Method to clear the stash. Takes the URL as parameter.
const resetReports = url => {
// The view of the stash is path-specific
// (https://web-platform-tests.org/tools/wptserve/docs/stash.html), therefore
// the origin doesn't need to be specified.
url = `${url}?clear_stash=true`;
const options = {
method: 'POST',
};
return fetch(url, options);
};
subsetTest(promise_test, async test => {
const uuid = generateUuid(test);
await resetWptServer();
await joinCrossOriginInterestGroup(test, uuid, OTHER_ORIGIN1, {
biddingLogicURL: createBiddingScriptURLForRealTimeReporting(OTHER_ORIGIN1)
});
await runBasicFledgeAuctionAndNavigate(test, uuid, {
decisionLogicURL: createDecisionScriptURLForRealTimeReporting(uuid),
interestGroupBuyers: [OTHER_ORIGIN1],
sellerRealTimeReportingConfig: {type: 'default-local-reporting'},
perBuyerRealTimeReportingConfig:
{[OTHER_ORIGIN1]: {type: 'default-local-reporting'}}
});
const sellerReports = await pollReports(location.origin);
verifyReports(sellerReports);
const buyerReports = await pollReports(OTHER_ORIGIN1);
verifyReports(buyerReports);
}, 'Real time reporting different buyer and seller both opted-in and called api.');
subsetTest(promise_test, async test => {
const uuid = generateUuid(test);
await resetWptServer();
await joinCrossOriginInterestGroup(test, uuid, OTHER_ORIGIN1, {
biddingLogicURL: createBiddingScriptURLForRealTimeReporting(OTHER_ORIGIN1)
});
await runBasicFledgeAuctionAndNavigate(test, uuid, {
decisionLogicURL: createDecisionScriptURLForRealTimeReporting(uuid),
interestGroupBuyers: [OTHER_ORIGIN1],
perBuyerRealTimeReportingConfig:
{[OTHER_ORIGIN1]: {type: 'default-local-reporting'}}
});
const buyerReports = await pollReports(OTHER_ORIGIN1);
verifyReports(buyerReports);
// Seller called the RTR API, but didn't opt-in.
const sellerReports =
await pollReports(location.origin, /*wait_for=*/ 1, /*timeout=*/ 1000);
assert_equals(sellerReports, null);
}, 'Real time reporting buyer opted-in but not seller.');
subsetTest(promise_test, async test => {
const uuid = generateUuid(test);
await resetWptServer();
await joinCrossOriginInterestGroup(test, uuid, OTHER_ORIGIN1, {
biddingLogicURL: createBiddingScriptURLForRealTimeReporting(OTHER_ORIGIN1)
});
await runBasicFledgeAuctionAndNavigate(test, uuid, {
decisionLogicURL: createDecisionScriptURLForRealTimeReporting(uuid),
interestGroupBuyers: [OTHER_ORIGIN1],
sellerRealTimeReportingConfig: {type: 'default-local-reporting'}
});
const sellerReports = await pollReports(location.origin);
verifyReports(sellerReports);
// Buyer called the RTR API, but didn't opt-in.
const buyerReports =
await pollReports(OTHER_ORIGIN1, /*wait_for=*/ 1, /*timeout=*/ 1000);
assert_equals(buyerReports, null);
}, 'Real time reporting seller opted-in but not buyer.');
subsetTest(promise_test, async test => {
const uuid = generateUuid(test);
await resetWptServer();
await joinCrossOriginInterestGroup(
test, uuid, OTHER_ORIGIN1,
{biddingLogicURL: createBiddingScriptURL({origin: OTHER_ORIGIN1})});
await runBasicFledgeAuctionAndNavigate(test, uuid, {
decisionLogicURL: createDecisionScriptURL(uuid),
interestGroupBuyers: [OTHER_ORIGIN1],
sellerRealTimeReportingConfig: {type: 'default-local-reporting'},
perBuyerRealTimeReportingConfig:
{[OTHER_ORIGIN1]: {type: 'default-local-reporting'}}
});
const sellerReports = await pollReports(location.origin);
verifyReports(sellerReports);
const buyerReports = await pollReports(OTHER_ORIGIN1);
verifyReports(buyerReports);
}, 'Real time reporting different buyer and seller both opted-in but did not call api.');
subsetTest(promise_test, async test => {
const uuid = generateUuid(test);
await resetWptServer();
await joinCrossOriginInterestGroup(test, uuid, OTHER_ORIGIN1, {
biddingLogicURL: createBiddingScriptURLForRealTimeReporting(OTHER_ORIGIN1)
});
await runBasicFledgeAuctionAndNavigate(test, uuid, {
decisionLogicURL: createDecisionScriptURLForRealTimeReporting(uuid),
interestGroupBuyers: [OTHER_ORIGIN1]
});
const sellerReports = await pollReports(location.origin);
assert_equals(sellerReports, null);
const buyerReports =
await pollReports(OTHER_ORIGIN1, /*wait_for=*/ 1, /*timeout=*/ 1000);
assert_equals(buyerReports, null);
}, 'Real time reporting both called api but did not opt-in.');
subsetTest(promise_test, async test => {
const uuid = generateUuid(test);
await resetWptServer();
await joinInterestGroup(
test, uuid,
{biddingLogicURL: createBiddingScriptURLForRealTimeReporting()});
const origin = location.origin;
await runBasicFledgeAuctionAndNavigate(test, uuid, {
decisionLogicURL: createDecisionScriptURLForRealTimeReporting(uuid),
sellerRealTimeReportingConfig: {type: 'default-local-reporting'},
perBuyerRealTimeReportingConfig:
{[origin]: {type: 'default-local-reporting'}}
});
const reports = await pollReports(origin);
verifyReports(reports);
}, 'Real time reporting buyer and seller same origin.');
subsetTest(promise_test, async test => {
const uuid = generateUuid(test);
await resetWptServer();
await joinCrossOriginInterestGroup(test, uuid, OTHER_ORIGIN1, {
biddingLogicURL: createBiddingScriptURLForRealTimeReporting(OTHER_ORIGIN1)
});
await joinCrossOriginInterestGroup(test, uuid, OTHER_ORIGIN2, {
biddingLogicURL:
createBiddingScriptURLForRealTimeReporting(OTHER_ORIGIN2, /*bid=*/ 100)
});
await runBasicFledgeAuctionAndNavigate(test, uuid, {
decisionLogicURL: createDecisionScriptURLForRealTimeReporting(uuid),
interestGroupBuyers: [OTHER_ORIGIN1, OTHER_ORIGIN2],
perBuyerRealTimeReportingConfig: {
[OTHER_ORIGIN1]: {type: 'default-local-reporting'},
[OTHER_ORIGIN2]: {type: 'default-local-reporting'}
}
});
const reports1 = await pollReports(OTHER_ORIGIN1);
verifyReports(reports1);
const reports2 = await pollReports(OTHER_ORIGIN2);
verifyReports(reports2);
}, 'Real time reporting both winning and losing buyers opted-in.');
subsetTest(promise_test, async test => {
const uuid = generateUuid(test);
await resetWptServer();
await joinCrossOriginInterestGroup(test, uuid, OTHER_ORIGIN1, {
biddingLogicURL: createBiddingScriptURLForRealTimeReporting(OTHER_ORIGIN1)
});
await joinCrossOriginInterestGroup(test, uuid, OTHER_ORIGIN2, {
biddingLogicURL:
createBiddingScriptURLForRealTimeReporting(OTHER_ORIGIN2, /*bid=*/ 100)
});
await runBasicFledgeAuctionAndNavigate(test, uuid, {
decisionLogicURL: createDecisionScriptURLForRealTimeReporting(uuid),
interestGroupBuyers: [OTHER_ORIGIN1, OTHER_ORIGIN2],
perBuyerRealTimeReportingConfig:
{[OTHER_ORIGIN1]: {type: 'default-local-reporting'}}
});
const reports1 = await pollReports(OTHER_ORIGIN1);
verifyReports(reports1);
const reports2 =
await pollReports(OTHER_ORIGIN2, /*wait_for=*/ 1, /*timeout=*/ 1000);
assert_equals(reports2, null);
}, 'Real time reporting one buyer opted-in but not the other.');