uriloader/exthandler/tests/unit/test_punycodeURIs.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 // Encoded test URI to work on all platforms/independent of file encoding
michael@0 6 const kTestURI = "http://\u65e5\u672c\u8a93.jp/";
michael@0 7 const kExpectedURI = "http://xn--wgv71a309e.jp/";
michael@0 8 const kOutputFile = "result.txt";
michael@0 9
michael@0 10 // Try several times in case the box we're running on is slow.
michael@0 11 const kMaxCheckExistAttempts = 30; // seconds
michael@0 12 var gCheckExistsAttempts = 0;
michael@0 13
michael@0 14 const tempDir = do_get_tempdir();
michael@0 15
michael@0 16 function checkFile() {
michael@0 17 // This is where we expect the output
michael@0 18 var tempFile = tempDir.clone();
michael@0 19 tempFile.append(kOutputFile);
michael@0 20
michael@0 21 if (!tempFile.exists()) {
michael@0 22 if (gCheckExistsAttempts >= kMaxCheckExistAttempts) {
michael@0 23 do_throw("Expected File " + tempFile.path + " does not exist after " +
michael@0 24 kMaxCheckExistAttempts + " seconds");
michael@0 25 }
michael@0 26 else {
michael@0 27 ++gCheckExistsAttempts;
michael@0 28 // Wait a bit longer then try again
michael@0 29 do_timeout(1000, checkFile);
michael@0 30 return;
michael@0 31 }
michael@0 32 }
michael@0 33
michael@0 34 // Now read it
michael@0 35 var fstream =
michael@0 36 Components.classes["@mozilla.org/network/file-input-stream;1"]
michael@0 37 .createInstance(Components.interfaces.nsIFileInputStream);
michael@0 38 var sstream =
michael@0 39 Components.classes["@mozilla.org/scriptableinputstream;1"]
michael@0 40 .createInstance(Components.interfaces.nsIScriptableInputStream);
michael@0 41 fstream.init(tempFile, -1, 0, 0);
michael@0 42 sstream.init(fstream);
michael@0 43
michael@0 44 // Read the first line only as that's the one we expect WriteArguments
michael@0 45 // to be writing the argument to.
michael@0 46 var data = sstream.read(4096);
michael@0 47
michael@0 48 sstream.close();
michael@0 49 fstream.close();
michael@0 50
michael@0 51 // Now remove the old file
michael@0 52 tempFile.remove(false);
michael@0 53
michael@0 54 // This currently fails on Mac with an argument like -psn_0_nnnnnn
michael@0 55 // This seems to be to do with how the executable is called, but I couldn't
michael@0 56 // find a way around it.
michael@0 57 // Additionally the lack of OS detection in xpcshell tests sucks, so we'll
michael@0 58 // have to check for the argument mac gives us.
michael@0 59 if (data.substring(0, 7) != "-psn_0_")
michael@0 60 do_check_eq(data, kExpectedURI);
michael@0 61
michael@0 62 do_test_finished();
michael@0 63 }
michael@0 64
michael@0 65 function run_test() {
michael@0 66 var isOSX = ("nsILocalFileMac" in Components.interfaces);
michael@0 67 if (isOSX) {
michael@0 68 dump("INFO | test_punycodeURIs.js | Skipping test on mac, bug 599475")
michael@0 69 return;
michael@0 70 }
michael@0 71
michael@0 72 // set up the uri to test with
michael@0 73 var ioService =
michael@0 74 Components.classes["@mozilla.org/network/io-service;1"]
michael@0 75 .getService(Components.interfaces.nsIIOService);
michael@0 76
michael@0 77 // set up the local handler object
michael@0 78 var localHandler =
michael@0 79 Components.classes["@mozilla.org/uriloader/local-handler-app;1"]
michael@0 80 .createInstance(Components.interfaces.nsILocalHandlerApp);
michael@0 81 localHandler.name = "Test Local Handler App";
michael@0 82
michael@0 83 // WriteArgument will just dump its arguments to a file for us.
michael@0 84 var processDir = do_get_cwd();
michael@0 85 var exe = processDir.clone();
michael@0 86 exe.append("WriteArgument");
michael@0 87
michael@0 88 if (!exe.exists()) {
michael@0 89 // Maybe we are on windows
michael@0 90 exe.leafName = "WriteArgument.exe";
michael@0 91 if (!exe.exists())
michael@0 92 do_throw("Could not locate the WriteArgument tests executable\n");
michael@0 93 }
michael@0 94
michael@0 95 var outFile = tempDir.clone();
michael@0 96 outFile.append(kOutputFile);
michael@0 97
michael@0 98 // Set an environment variable for WriteArgument to pick up
michael@0 99 var envSvc =
michael@0 100 Components.classes["@mozilla.org/process/environment;1"]
michael@0 101 .getService(Components.interfaces.nsIEnvironment);
michael@0 102
michael@0 103 // The Write Argument file needs to know where its libraries are, so
michael@0 104 // just force the path variable
michael@0 105 // For mac
michael@0 106 var greDir = HandlerServiceTest._dirSvc.get("GreD", Components.interfaces.nsIFile);
michael@0 107
michael@0 108 envSvc.set("DYLD_LIBRARY_PATH", greDir.path);
michael@0 109 // For Linux
michael@0 110 envSvc.set("LD_LIBRARY_PATH", greDir.path);
michael@0 111 //XXX: handle windows
michael@0 112
michael@0 113 // Now tell it where we want the file.
michael@0 114 envSvc.set("WRITE_ARGUMENT_FILE", outFile.path);
michael@0 115
michael@0 116 var uri = ioService.newURI(kTestURI, null, null);
michael@0 117
michael@0 118 // Just check we've got these matching, if we haven't there's a problem
michael@0 119 // with ascii spec or our test case.
michael@0 120 do_check_eq(uri.asciiSpec, kExpectedURI);
michael@0 121
michael@0 122 localHandler.executable = exe;
michael@0 123 localHandler.launchWithURI(uri);
michael@0 124
michael@0 125 do_test_pending();
michael@0 126 do_timeout(1000, checkFile);
michael@0 127 }

mercurial