|
| 1 | +// META: script=/resources/testdriver.js |
| 2 | +// META: script=/resources/testdriver-vendor.js |
| 3 | +// META: script=/common/utils.js |
| 4 | +// META: script=resources/fledge-util.sub.js |
| 5 | +// META: script=/common/subset-tests.js |
| 6 | +// META: timeout=long |
| 7 | +// META: variant=?1-5 |
| 8 | +// META: variant=?6-10 |
| 9 | +// META: variant=?11-15 |
| 10 | + |
| 11 | +'use strict;' |
| 12 | + |
| 13 | +// These tests cover encodeUtf8 and decodeUtf8. |
| 14 | + |
| 15 | +const helpers = ` |
| 16 | + function assertEq(l, r, label) { |
| 17 | + if (l !== r) |
| 18 | + throw 'Mismatch ' + label; |
| 19 | + } |
| 20 | +
|
| 21 | + function assertByteArray(result, expect) { |
| 22 | + if (!(result instanceof Uint8Array)) { |
| 23 | + throw 'Not a Uint8Array!'; |
| 24 | + } |
| 25 | + assertEq(result.length, expect.length, 'length'); |
| 26 | + for (var i = 0; i < result.length; ++i) { |
| 27 | + assertEq(result[i], expect[i], i); |
| 28 | + } |
| 29 | + } |
| 30 | +
|
| 31 | + function assertString(result, expect) { |
| 32 | + if (typeof result !== 'string') { |
| 33 | + throw 'Not a string'; |
| 34 | + } |
| 35 | + assertEq(result.length, expect.length, 'length'); |
| 36 | + for (var i = 0; i < result.length; ++i) { |
| 37 | + assertEq(result.charCodeAt(i), expect.charCodeAt(i), i); |
| 38 | + } |
| 39 | + } |
| 40 | +` |
| 41 | + |
| 42 | +async function testConversion(test, conversionBody) { |
| 43 | + const uuid = generateUuid(test); |
| 44 | + let sellerReportURL = createSellerReportURL(uuid); |
| 45 | + let bidderReportURL = createBidderReportURL(uuid); |
| 46 | + |
| 47 | + let fullBody = ` |
| 48 | + ${helpers} |
| 49 | + ${conversionBody} |
| 50 | + `; |
| 51 | + |
| 52 | + let biddingLogicURL = createBiddingScriptURL({ |
| 53 | + generateBid: fullBody, |
| 54 | + reportWin: fullBody + `sendReportTo('${bidderReportURL}')` |
| 55 | + }); |
| 56 | + |
| 57 | + let decisionLogicURL = createDecisionScriptURL(uuid, { |
| 58 | + scoreAd: fullBody, |
| 59 | + reportResult: fullBody + `sendReportTo('${sellerReportURL}')` |
| 60 | + }); |
| 61 | + |
| 62 | + await joinInterestGroup(test, uuid, {biddingLogicURL: biddingLogicURL}); |
| 63 | + await runBasicFledgeAuctionAndNavigate( |
| 64 | + test, uuid, {decisionLogicURL: decisionLogicURL}); |
| 65 | + await waitForObservedRequests(uuid, [sellerReportURL, bidderReportURL]); |
| 66 | +} |
| 67 | + |
| 68 | +async function testConversionException(test, conversionBody) { |
| 69 | + const uuid = generateUuid(test); |
| 70 | + let sellerReportURL = createSellerReportURL(uuid); |
| 71 | + let bidderReportURL = createBidderReportURL(uuid); |
| 72 | + |
| 73 | + let fullBody = ` |
| 74 | + ${helpers} |
| 75 | + try { |
| 76 | + ${conversionBody}; |
| 77 | + return -1; |
| 78 | + } catch (e) { |
| 79 | + } |
| 80 | + `; |
| 81 | + |
| 82 | + let biddingLogicURL = createBiddingScriptURL({ |
| 83 | + generateBid: fullBody, |
| 84 | + reportWin: fullBody + `sendReportTo('${bidderReportURL}')` |
| 85 | + }); |
| 86 | + |
| 87 | + let decisionLogicURL = createDecisionScriptURL(uuid, { |
| 88 | + scoreAd: fullBody, |
| 89 | + reportResult: fullBody + `sendReportTo('${sellerReportURL}')` |
| 90 | + }); |
| 91 | + |
| 92 | + await joinInterestGroup(test, uuid, {biddingLogicURL: biddingLogicURL}); |
| 93 | + await runBasicFledgeAuctionAndNavigate( |
| 94 | + test, uuid, {decisionLogicURL: decisionLogicURL}); |
| 95 | + await waitForObservedRequests(uuid, [sellerReportURL, bidderReportURL]); |
| 96 | +} |
| 97 | + |
| 98 | +subsetTest(promise_test, async test => { |
| 99 | + await testConversion( |
| 100 | + test, `let result = browserSignals.encodeUtf8('ABC\u0490'); |
| 101 | + assertByteArray(result, [65, 66, 67, 0xD2, 0x90])`); |
| 102 | +}, 'encodeUtf8 - basic'); |
| 103 | + |
| 104 | +subsetTest(promise_test, async test => { |
| 105 | + await testConversion( |
| 106 | + test, `let result = browserSignals.encodeUtf8('A\uD800C'); |
| 107 | + assertByteArray(result, [65, 0xEF, 0xBF, 0xBD, 67])`); |
| 108 | +}, 'encodeUtf8 - mismatched surrogate gets replaced'); |
| 109 | + |
| 110 | +subsetTest(promise_test, async test => { |
| 111 | + await testConversion( |
| 112 | + test, `let result = browserSignals.encodeUtf8('A\uD83D\uDE02C'); |
| 113 | + assertByteArray(result, [65, 0xF0, 0x9F, 0x98, 0x82, 67])`); |
| 114 | +}, 'encodeUtf8 - surrogate pair combined'); |
| 115 | + |
| 116 | +subsetTest(promise_test, async test => { |
| 117 | + const conversionBody = ` |
| 118 | + let obj = { |
| 119 | + toString: () => "ABC" |
| 120 | + }; |
| 121 | + let result = browserSignals.encodeUtf8(obj); |
| 122 | + assertByteArray(result, [65, 66, 67]) |
| 123 | + `; |
| 124 | + await testConversion(test, conversionBody); |
| 125 | +}, 'encodeUtf8 - custom string conversion'); |
| 126 | + |
| 127 | +subsetTest(promise_test, async test => { |
| 128 | + const conversionBody = ` |
| 129 | + let result = browserSignals.encodeUtf8(); |
| 130 | + `; |
| 131 | + await testConversionException(test, conversionBody); |
| 132 | +}, 'encodeUtf8 - not enough arguments'); |
| 133 | + |
| 134 | +subsetTest(promise_test, async test => { |
| 135 | + const conversionBody = ` |
| 136 | + let obj = { |
| 137 | + toString: () => { throw 'no go' } |
| 138 | + }; |
| 139 | + let result = browserSignals.encodeUtf8(obj); |
| 140 | + `; |
| 141 | + await testConversionException(test, conversionBody); |
| 142 | +}, 'encodeUtf8 - custom string conversion failure'); |
| 143 | + |
| 144 | +subsetTest(promise_test, async test => { |
| 145 | + const conversionBody = ` |
| 146 | + let input = new Uint8Array([65, 66, 0xD2, 0x90, 67]); |
| 147 | + let result = browserSignals.decodeUtf8(input); |
| 148 | + assertString(result, 'AB\u0490C'); |
| 149 | + `; |
| 150 | + await testConversion(test, conversionBody); |
| 151 | +}, 'decodeUtf8 - basic'); |
| 152 | + |
| 153 | +subsetTest(promise_test, async test => { |
| 154 | + const conversionBody = ` |
| 155 | + let input = new Uint8Array([65, 32, 0xD2]); |
| 156 | + let result = browserSignals.decodeUtf8(input); |
| 157 | + if (result.indexOf('\uFFFD') === -1) |
| 158 | + throw 'Should have replacement character'; |
| 159 | + `; |
| 160 | + await testConversion(test, conversionBody); |
| 161 | +}, 'decodeUtf8 - broken utf-8'); |
| 162 | + |
| 163 | +subsetTest(promise_test, async test => { |
| 164 | + const conversionBody = ` |
| 165 | + let input = new Uint8Array([65, 32, 0xED, 0xA0, 0x80, 66]); |
| 166 | + let result = browserSignals.decodeUtf8(input); |
| 167 | + if (result.indexOf('\uFFFD') === -1) |
| 168 | + throw 'Should have replacement character'; |
| 169 | + `; |
| 170 | + await testConversion(test, conversionBody); |
| 171 | +}, 'decodeUtf8 - mismatched surrogate'); |
| 172 | + |
| 173 | +subsetTest(promise_test, async test => { |
| 174 | + const conversionBody = ` |
| 175 | + let input = new Uint8Array([65, 0xF0, 0x9F, 0x98, 0x82, 67]); |
| 176 | + let result = browserSignals.decodeUtf8(input); |
| 177 | + assertString(result, 'A\uD83D\uDE02C'); |
| 178 | + `; |
| 179 | + await testConversion(test, conversionBody); |
| 180 | +}, 'decodeUtf8 - non-BMP character'); |
| 181 | + |
| 182 | +subsetTest(promise_test, async test => { |
| 183 | + const conversionBody = ` |
| 184 | + let buffer = new ArrayBuffer(8); |
| 185 | + let fullView = new Uint8Array(buffer); |
| 186 | + for (let i = 0; i < fullView.length; ++i) |
| 187 | + fullView[i] = 65 + i; |
| 188 | + let partialView = new Uint8Array(buffer, 2, 3); |
| 189 | + assertString(browserSignals.decodeUtf8(fullView), |
| 190 | + 'ABCDEFGH'); |
| 191 | + assertString(browserSignals.decodeUtf8(partialView), |
| 192 | + 'CDE'); |
| 193 | + `; |
| 194 | + await testConversion(test, conversionBody); |
| 195 | +}, 'decodeUtf8 - proper Uint8Array handling'); |
| 196 | + |
| 197 | +subsetTest(promise_test, async test => { |
| 198 | + const conversionBody = ` |
| 199 | + let result = browserSignals.decodeUtf8(); |
| 200 | + `; |
| 201 | + await testConversionException(test, conversionBody); |
| 202 | +}, 'decodeUtf8 - not enough arguments'); |
| 203 | + |
| 204 | +subsetTest(promise_test, async test => { |
| 205 | + const conversionBody = ` |
| 206 | + let result = browserSignals.decodeUtf8([65, 32, 66]); |
| 207 | + `; |
| 208 | + await testConversionException(test, conversionBody); |
| 209 | +}, 'decodeUtf8 - wrong type'); |
0 commit comments