Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 <!DOCTYPE html> | |
| 2 <html> | |
| 3 <head> | |
| 4 <title>Tests integrity enforcement on fetch()</title> | |
| 5 <script src="/resources/testharness.js"></script> | |
| 6 <script src="/resources/testharnessreport.js"></script> | |
| 7 </head> | |
| 8 <body> | |
| 9 <script> | |
| 10 var SRITest = function(pass, name, src, integrity, expectedValue) { | |
| 11 this.pass = pass; | |
| 12 this.name = name; | |
| 13 this.src = src; | |
| 14 this.integrity = integrity; | |
| 15 this.expectedValue = expectedValue; | |
| 16 } | |
| 17 SRITest.prototype.execute = function() { | |
| 18 var test = async_test(this.name); | |
| 19 var pass = this.pass; | |
| 20 var integrity = this.integrity; | |
| 21 var expectedValue = this.expectedValue; | |
| 22 var options = {}; | |
| 23 if (integrity !== '') { | |
| 24 options.integrity = integrity; | |
| 25 } | |
| 26 fetch(this.src, options) | |
| 27 .then(test.step_func(function(response) { | |
|
yhirano
2015/08/19 10:42:36
You can use promise_test. Then you don't have to i
jww
2015/08/19 16:43:40
Done.
| |
| 28 if (pass) { | |
| 29 if (expectedValue) { | |
| 30 consume(response.body.getReader(), test, expectedValue, ''); | |
|
yhirano
2015/08/19 10:42:36
You can use response.text().
jww
2015/08/19 16:43:40
Done.
| |
| 31 } else { | |
| 32 test.done(); | |
| 33 } | |
| 34 } else { | |
| 35 assert_unreached("Response on bad fetch."); | |
| 36 } | |
| 37 })) | |
| 38 .catch(test.step_func(function(error) { | |
| 39 if (pass) { | |
| 40 assert_unreached("Network error on good fetch."); | |
| 41 } else { | |
| 42 test.done(); | |
| 43 } | |
| 44 })); | |
| 45 } | |
| 46 | |
| 47 function consume(reader, test, expectedValue, actualValue) { | |
| 48 return reader.read().then(function(result) { | |
| 49 if (result.done) { | |
| 50 assert_equals(actualValue, expectedValue, "Value consumed must match hashed value."); | |
| 51 test.done(); | |
| 52 return; | |
| 53 } | |
| 54 actualValue = actualValue + String.fromCharCode.apply(null, result.value ); | |
| 55 return consume(reader, test, expectedValue, actualValue); | |
| 56 }); | |
| 57 } | |
| 58 | |
| 59 new SRITest(true, 'No integrity', 'call-success.js', '', 'success();\n').execute (); | |
| 60 new SRITest(true, 'Good integrity', 'call-success.js', 'sha256-B0/62fJSJFrdjEFR9 ba04m/D+LHQ+zG6PGcaR0Trpxg=', 'success();\n').execute(); | |
| 61 new SRITest(false, 'Bad integrity', 'call-success.js', 'sha256-deadbeef').execut e(); | |
| 62 new SRITest(false, 'Bad integrity and an img', '/resources/square100.png', 'sha2 56-B0/62fJSJFrdjEFR9ba04m/D+LHQ+zG6PGcaR0Trpxg=').execute(); | |
| 63 new SRITest(true, 'Good integrity and an img', '/resources/square100.png', 'sha2 56-xZjdcorjj+TiKGteFFcrNbdqrDns2iVURBpGpAwp12k=').execute(); | |
| 64 </script> | |
| 65 </body> | |
| 66 </html> | |
| OLD | NEW |