toolkit/components/osfile/tests/xpcshell/test_file_URL_conversion.js

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

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 file,
michael@0 3 * You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 function run_test() {
michael@0 6 Components.utils.import("resource://gre/modules/Services.jsm");
michael@0 7 Components.utils.import("resource://gre/modules/osfile.jsm");
michael@0 8 Components.utils.import("resource://gre/modules/FileUtils.jsm");
michael@0 9
michael@0 10 let isWindows = ("@mozilla.org/windows-registry-key;1" in Components.classes);
michael@0 11
michael@0 12 // Test cases for filePathToURI
michael@0 13 let paths = isWindows ? [
michael@0 14 'C:\\',
michael@0 15 'C:\\test',
michael@0 16 'C:\\test\\',
michael@0 17 'C:\\test%2f',
michael@0 18 'C:\\test\\test\\test',
michael@0 19 'C:\\test;+%',
michael@0 20 'C:\\test?action=index\\',
michael@0 21 'C:\\test\ test',
michael@0 22 '\\\\C:\\a\\b\\c',
michael@0 23 '\\\\Server\\a\\b\\c',
michael@0 24
michael@0 25 // note that per http://support.microsoft.com/kb/177506 (under more info),
michael@0 26 // the following characters are allowed on Windows:
michael@0 27 'C:\\char^',
michael@0 28 'C:\\char&',
michael@0 29 'C:\\char\'',
michael@0 30 'C:\\char@',
michael@0 31 'C:\\char{',
michael@0 32 'C:\\char}',
michael@0 33 'C:\\char[',
michael@0 34 'C:\\char]',
michael@0 35 'C:\\char,',
michael@0 36 'C:\\char$',
michael@0 37 'C:\\char=',
michael@0 38 'C:\\char!',
michael@0 39 'C:\\char-',
michael@0 40 'C:\\char#',
michael@0 41 'C:\\char(',
michael@0 42 'C:\\char)',
michael@0 43 'C:\\char%',
michael@0 44 'C:\\char.',
michael@0 45 'C:\\char+',
michael@0 46 'C:\\char~',
michael@0 47 'C:\\char_'
michael@0 48 ] : [
michael@0 49 '/',
michael@0 50 '/test',
michael@0 51 '/test/',
michael@0 52 '/test%2f',
michael@0 53 '/test/test/test',
michael@0 54 '/test;+%',
michael@0 55 '/test?action=index/',
michael@0 56 '/test\ test',
michael@0 57 '/punctuation/;,/?:@&=+$-_.!~*\'()"#',
michael@0 58 '/CasePreserving'
michael@0 59 ];
michael@0 60
michael@0 61 // some additional URIs to test, beyond those generated from paths
michael@0 62 let uris = isWindows ? [
michael@0 63 'file:///C:/test/',
michael@0 64 'file://localhost/C:/test',
michael@0 65 'file:///c:/test/test.txt',
michael@0 66 //'file:///C:/foo%2f', // trailing, encoded slash
michael@0 67 'file:///C:/%3f%3F',
michael@0 68 'file:///C:/%3b%3B',
michael@0 69 'file:///C:/%3c%3C', // not one of the special-cased ? or ;
michael@0 70 'file:///C:/%78', // 'x', not usually uri encoded
michael@0 71 'file:///C:/test#frag', // a fragment identifier
michael@0 72 'file:///C:/test?action=index' // an actual query component
michael@0 73 ] : [
michael@0 74 'file:///test/',
michael@0 75 'file://localhost/test',
michael@0 76 'file:///test/test.txt',
michael@0 77 'file:///foo%2f', // trailing, encoded slash
michael@0 78 'file:///%3f%3F',
michael@0 79 'file:///%3b%3B',
michael@0 80 'file:///%3c%3C', // not one of the special-cased ? or ;
michael@0 81 'file:///%78', // 'x', not usually uri encoded
michael@0 82 'file:///test#frag', // a fragment identifier
michael@0 83 'file:///test?action=index' // an actual query component
michael@0 84 ];
michael@0 85
michael@0 86 for (let path of paths) {
michael@0 87 // convert that to a uri using FileUtils and Services, which toFileURI is trying to model
michael@0 88 let file = FileUtils.File(path);
michael@0 89 let uri = Services.io.newFileURI(file).spec;
michael@0 90 do_check_eq(uri, OS.Path.toFileURI(path));
michael@0 91
michael@0 92 // keep the resulting URI to try the reverse, except for "C:\" for which the
michael@0 93 // behavior of nsIFileURL and OS.File is inconsistent
michael@0 94 if (path != "C:\\") {
michael@0 95 uris.push(uri);
michael@0 96 }
michael@0 97 }
michael@0 98
michael@0 99 for (let uri of uris) {
michael@0 100 // convert URIs to paths with nsIFileURI, which fromFileURI is trying to model
michael@0 101 let path = Services.io.newURI(uri, null, null).QueryInterface(Components.interfaces.nsIFileURL).file.path;
michael@0 102 do_check_eq(path, OS.Path.fromFileURI(uri));
michael@0 103 }
michael@0 104
michael@0 105 // check that non-file URLs aren't allowed
michael@0 106 let thrown = false;
michael@0 107 try {
michael@0 108 OS.Path.fromFileURI('http://test.com')
michael@0 109 } catch (e) {
michael@0 110 do_check_eq(e.message, "fromFileURI expects a file URI");
michael@0 111 thrown = true;
michael@0 112 }
michael@0 113 do_check_true(thrown);
michael@0 114 }

mercurial