OLD | NEW |
(Empty) | |
| 1 /** |
| 2 * @fileoverview Test case for mixed-content in Web Platform Tests. |
| 3 * @author burnik@google.com (Kristijan Burnik) |
| 4 */ |
| 5 |
| 6 /** |
| 7 * MixedContentTestCase exercises all the tests for checking browser behavior |
| 8 * when resources regarded as mixed-content are requested. A single run covers |
| 9 * only a single scenario. |
| 10 * @param {object} scenario A JSON describing the test arrangement and |
| 11 * expectation(s). Refer to /mixed-content/spec.src.json for details. |
| 12 * @param {string} description The test scenario verbose description. |
| 13 * @param {SanityChecker} sanityChecker Instance of an object used to check the |
| 14 * running scenario. Useful in debug mode. See ./sanity-checker.js. |
| 15 * Run {@code ./tools/generate.py -h} for info on test generating modes. |
| 16 * @return {object} Object wrapping the start method used to run the test. |
| 17 */ |
| 18 function MixedContentTestCase(scenario, description, sanityChecker) { |
| 19 var httpProtocol = "http"; |
| 20 var httpsProtocol = "https"; |
| 21 var wsProtocol = "ws"; |
| 22 var wssProtocol = "wss"; |
| 23 |
| 24 var sameOriginHost = location.hostname; |
| 25 var crossOriginHost = "{{domains[www1]}}"; |
| 26 |
| 27 // These values can evaluate to either empty strings or a ":port" string. |
| 28 var httpPort = getNormalizedPort(parseInt("{{ports[http][0]}}", 10)); |
| 29 var httpsPort = getNormalizedPort(parseInt("{{ports[https][0]}}", 10)); |
| 30 var wsPort = getNormalizedPort(parseInt("{{ports[ws][0]}}", 10)); |
| 31 var wssPort = getNormalizedPort(parseInt("{{ports[wss][0]}}", 10)); |
| 32 |
| 33 var resourcePath = "/mixed-content/generic/expect.py"; |
| 34 var wsResourcePath = "/stash_responder"; |
| 35 |
| 36 // Map all endpoints to scenario for use in the test. |
| 37 var endpoint = { |
| 38 "same-origin": |
| 39 location.origin + resourcePath, |
| 40 "same-host-https": |
| 41 httpsProtocol + "://" + sameOriginHost + httpsPort + resourcePath, |
| 42 "same-host-http": |
| 43 httpProtocol + "://" + sameOriginHost + httpPort + resourcePath, |
| 44 "cross-origin-https": |
| 45 httpsProtocol + "://" + crossOriginHost + httpsPort + resourcePath, |
| 46 "cross-origin-http": |
| 47 httpProtocol + "://" + crossOriginHost + httpPort + resourcePath, |
| 48 "same-host-wss": |
| 49 wssProtocol + "://" + sameOriginHost + wssPort + wsResourcePath, |
| 50 "same-host-ws": |
| 51 wsProtocol + "://" + sameOriginHost + wsPort + wsResourcePath, |
| 52 "cross-origin-wss": |
| 53 wssProtocol + "://" + crossOriginHost + wssPort + wsResourcePath, |
| 54 "cross-origin-ws": |
| 55 wsProtocol + "://" + crossOriginHost + wsPort + wsResourcePath |
| 56 }; |
| 57 |
| 58 // Mapping all the resource requesting methods to the scenario. |
| 59 var resourceMap = { |
| 60 "a-tag": requestViaAnchor, |
| 61 "area-tag": requestViaArea, |
| 62 "fetch-request": requestViaFetch, |
| 63 "form-tag": requestViaForm, |
| 64 "iframe-tag": requestViaIframe, |
| 65 "img-tag": requestViaImage, |
| 66 "script-tag": requestViaScript, |
| 67 "worker-request": requestViaWorker, |
| 68 "xhr-request": requestViaXhr, |
| 69 "audio-tag": requestViaAudio, |
| 70 "video-tag": requestViaVideo, |
| 71 "picture-tag": requestViaPicture, |
| 72 "object-tag": requestViaObject, |
| 73 "link-css-tag": requestViaLinkStylesheet, |
| 74 "link-prefetch-tag": requestViaLinkPrefetch, |
| 75 "websocket-request": requestViaWebSocket |
| 76 }; |
| 77 |
| 78 sanityChecker.checkScenario(scenario, resourceMap); |
| 79 |
| 80 // Mapping all expected MIME types to the scenario. |
| 81 var contentType = { |
| 82 "a-tag": "text/html", |
| 83 "area-tag": "text/html", |
| 84 "fetch-request": "application/json", |
| 85 "form-tag": "text/html", |
| 86 "iframe-tag": "text/html", |
| 87 "img-tag": "image/png", |
| 88 "script-tag": "text/javascript", |
| 89 "worker-request": "application/javascript", |
| 90 "xhr-request": "application/json", |
| 91 "audio-tag": "audio/mpeg", |
| 92 "video-tag": "video/mp4", |
| 93 "picture-tag": "image/png", |
| 94 "object-tag": "text/html", |
| 95 "link-css-tag": "text/css", |
| 96 "link-prefetch-tag": "text/html", |
| 97 "websocket-request": "application/json" |
| 98 }; |
| 99 |
| 100 var mixed_content_test = async_test(description); |
| 101 |
| 102 function runTest() { |
| 103 sanityChecker.setFailTimeout(mixed_content_test); |
| 104 |
| 105 var key = guid(); |
| 106 var value = guid(); |
| 107 // We use the same path for both HTTP/S and WS/S stash requests. |
| 108 var stash_path = encodeURIComponent("/mixed-content"); |
| 109 var announceResourceRequestUrl = endpoint['same-origin'] + |
| 110 "?action=put&key=" + key + |
| 111 "&value=" + value + |
| 112 "&path=" + stash_path; |
| 113 var assertResourceRequestUrl = endpoint['same-origin'] + |
| 114 "?action=take&key=" + key + |
| 115 "&path=" + stash_path; |
| 116 var resourceRequestUrl = endpoint[scenario.origin] + "?redirection=" + |
| 117 scenario.redirection + "&action=purge&key=" + key + |
| 118 "&path=" + stash_path + "&content_type=" + |
| 119 contentType[scenario.subresource]; |
| 120 |
| 121 xhrRequest(announceResourceRequestUrl) |
| 122 .then(function(response) { |
| 123 // Send out the real resource request. |
| 124 // This should tear down the key if it's not blocked. |
| 125 return resourceMap[scenario.subresource](resourceRequestUrl); |
| 126 }) |
| 127 .then(function() { |
| 128 mixed_content_test.step(function() { |
| 129 assert_equals("allowed", scenario.expectation, |
| 130 "The triggered event should match '" + |
| 131 scenario.expectation + "'."); |
| 132 }, "Check if success event was triggered."); |
| 133 |
| 134 // Send request to check if the key has been torn down. |
| 135 return xhrRequest(assertResourceRequestUrl); |
| 136 }, function(error) { |
| 137 mixed_content_test.step(function() { |
| 138 assert_equals("blocked", scenario.expectation, |
| 139 "The triggered event should match '" + |
| 140 scenario.expectation + "'."); |
| 141 // TODO(kristijanburnik): param "error" can be an event or error. |
| 142 // Map assertion by resource. |
| 143 // e.g.: assert_equals(e.type, "error"); |
| 144 }, "Check if error event was triggered."); |
| 145 |
| 146 // When requestResource fails, we also check the key state. |
| 147 return xhrRequest(assertResourceRequestUrl); |
| 148 }) |
| 149 .then(function(response) { |
| 150 // Now check if the value has been torn down. If it's still there, |
| 151 // we have blocked the request to mixed-content. |
| 152 mixed_content_test.step(function() { |
| 153 assert_equals(response.status, scenario.expectation, |
| 154 "The resource request should be '" + scenario.expectation + |
| 155 "'."); |
| 156 }, "Check if request was sent."); |
| 157 mixed_content_test.done(); |
| 158 }); |
| 159 |
| 160 } // runTest |
| 161 |
| 162 return {start: runTest}; |
| 163 } // MixedContentTestCase |
OLD | NEW |