|
| 1 | +// META: global=window,dedicatedworker,sharedworker |
| 2 | +// META: script=../resources/utils.js |
| 3 | + |
| 4 | +function integrity(desc, url, integrity, initRequestMode, shouldPass) { |
| 5 | + var fetchRequestInit = {'integrity': integrity} |
| 6 | + if (!!initRequestMode && initRequestMode !== "") { |
| 7 | + fetchRequestInit.mode = initRequestMode; |
| 8 | + } |
| 9 | + |
| 10 | + if (shouldPass) { |
| 11 | + promise_test(function(test) { |
| 12 | + return fetch(url, fetchRequestInit).then(function(resp) { |
| 13 | + if (initRequestMode !== "no-cors") { |
| 14 | + assert_equals(resp.status, 200, "Response's status is 200"); |
| 15 | + } else { |
| 16 | + assert_equals(resp.status, 0, "Opaque response's status is 0"); |
| 17 | + assert_equals(resp.type, "opaque"); |
| 18 | + } |
| 19 | + }); |
| 20 | + }, desc); |
| 21 | + } else { |
| 22 | + promise_test(function(test) { |
| 23 | + return promise_rejects_js(test, TypeError, fetch(url, fetchRequestInit)); |
| 24 | + }, desc); |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +const topSha256 = "sha256-KHIDZcXnR2oBHk9DrAA+5fFiR6JjudYjqoXtMR1zvzk="; |
| 29 | +const topSha384 = "sha384-MgZYnnAzPM/MjhqfOIMfQK5qcFvGZsGLzx4Phd7/A8fHTqqLqXqKo8cNzY3xEPTL"; |
| 30 | +const topSha512 = "sha512-D6yns0qxG0E7+TwkevZ4Jt5t7Iy3ugmAajG/dlf6Pado1JqTyneKXICDiqFIkLMRExgtvg8PlxbKTkYfRejSOg=="; |
| 31 | +const invalidSha256 = "sha256-dKUcPOn/AlUjWIwcHeHNqYXPlvyGiq+2dWOdFcE+24I="; |
| 32 | +const invalidSha512 = "sha512-oUceBRNxPxnY60g/VtPCj2syT4wo4EZh2CgYdWy9veW8+OsReTXoh7dizMGZafvx9+QhMS39L/gIkxnPIn41Zg=="; |
| 33 | + |
| 34 | +const path = dirname(location.pathname) + RESOURCES_DIR + "top.txt"; |
| 35 | +const url = path; |
| 36 | +const corsUrl = |
| 37 | + `http://{{host}}:{{ports[http][1]}}${path}?pipe=header(Access-Control-Allow-Origin,*)`; |
| 38 | +const corsUrl2 = `https://{{host}}:{{ports[https][0]}}${path}` |
| 39 | + |
| 40 | +integrity("Empty string integrity", url, "", /* initRequestMode */ undefined, |
| 41 | + /* shouldPass */ true); |
| 42 | +integrity("SHA-256 integrity", url, topSha256, /* initRequestMode */ undefined, |
| 43 | + /* shouldPass */ true); |
| 44 | +integrity("SHA-384 integrity", url, topSha384, /* initRequestMode */ undefined, |
| 45 | + /* shouldPass */ true); |
| 46 | +integrity("SHA-512 integrity", url, topSha512, /* initRequestMode */ undefined, |
| 47 | + /* shouldPass */ true); |
| 48 | +integrity("Invalid integrity", url, invalidSha256, |
| 49 | + /* initRequestMode */ undefined, /* shouldPass */ false); |
| 50 | +integrity("Multiple integrities: valid stronger than invalid", url, |
| 51 | + invalidSha256 + " " + topSha384, /* initRequestMode */ undefined, |
| 52 | + /* shouldPass */ true); |
| 53 | +integrity("Multiple integrities: invalid stronger than valid", |
| 54 | + url, invalidSha512 + " " + topSha384, /* initRequestMode */ undefined, |
| 55 | + /* shouldPass */ false); |
| 56 | +integrity("Multiple integrities: invalid as strong as valid", url, |
| 57 | + invalidSha512 + " " + topSha512, /* initRequestMode */ undefined, |
| 58 | + /* shouldPass */ true); |
| 59 | +integrity("Multiple integrities: both are valid", url, |
| 60 | + topSha384 + " " + topSha512, /* initRequestMode */ undefined, |
| 61 | + /* shouldPass */ true); |
| 62 | +integrity("Multiple integrities: both are invalid", url, |
| 63 | + invalidSha256 + " " + invalidSha512, /* initRequestMode */ undefined, |
| 64 | + /* shouldPass */ false); |
| 65 | +integrity("CORS empty integrity", corsUrl, "", /* initRequestMode */ undefined, |
| 66 | + /* shouldPass */ true); |
| 67 | +integrity("CORS SHA-512 integrity", corsUrl, topSha512, |
| 68 | + /* initRequestMode */ undefined, /* shouldPass */ true); |
| 69 | +integrity("CORS invalid integrity", corsUrl, invalidSha512, |
| 70 | + /* initRequestMode */ undefined, /* shouldPass */ false); |
| 71 | + |
| 72 | +integrity("Empty string integrity for opaque response", corsUrl2, "", |
| 73 | + /* initRequestMode */ "no-cors", /* shouldPass */ true); |
| 74 | +integrity("SHA-* integrity for opaque response", corsUrl2, topSha512, |
| 75 | + /* initRequestMode */ "no-cors", /* shouldPass */ false); |
| 76 | + |
| 77 | +done(); |
0 commit comments