michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this file, michael@0: * You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: function run_test() { michael@0: Components.utils.import("resource://gre/modules/Services.jsm"); michael@0: Components.utils.import("resource://gre/modules/osfile.jsm"); michael@0: Components.utils.import("resource://gre/modules/FileUtils.jsm"); michael@0: michael@0: let isWindows = ("@mozilla.org/windows-registry-key;1" in Components.classes); michael@0: michael@0: // Test cases for filePathToURI michael@0: let paths = isWindows ? [ michael@0: 'C:\\', michael@0: 'C:\\test', michael@0: 'C:\\test\\', michael@0: 'C:\\test%2f', michael@0: 'C:\\test\\test\\test', michael@0: 'C:\\test;+%', michael@0: 'C:\\test?action=index\\', michael@0: 'C:\\test\ test', michael@0: '\\\\C:\\a\\b\\c', michael@0: '\\\\Server\\a\\b\\c', michael@0: michael@0: // note that per http://support.microsoft.com/kb/177506 (under more info), michael@0: // the following characters are allowed on Windows: michael@0: 'C:\\char^', michael@0: 'C:\\char&', michael@0: 'C:\\char\'', michael@0: 'C:\\char@', michael@0: 'C:\\char{', michael@0: 'C:\\char}', michael@0: 'C:\\char[', michael@0: 'C:\\char]', michael@0: 'C:\\char,', michael@0: 'C:\\char$', michael@0: 'C:\\char=', michael@0: 'C:\\char!', michael@0: 'C:\\char-', michael@0: 'C:\\char#', michael@0: 'C:\\char(', michael@0: 'C:\\char)', michael@0: 'C:\\char%', michael@0: 'C:\\char.', michael@0: 'C:\\char+', michael@0: 'C:\\char~', michael@0: 'C:\\char_' michael@0: ] : [ michael@0: '/', michael@0: '/test', michael@0: '/test/', michael@0: '/test%2f', michael@0: '/test/test/test', michael@0: '/test;+%', michael@0: '/test?action=index/', michael@0: '/test\ test', michael@0: '/punctuation/;,/?:@&=+$-_.!~*\'()"#', michael@0: '/CasePreserving' michael@0: ]; michael@0: michael@0: // some additional URIs to test, beyond those generated from paths michael@0: let uris = isWindows ? [ michael@0: 'file:///C:/test/', michael@0: 'file://localhost/C:/test', michael@0: 'file:///c:/test/test.txt', michael@0: //'file:///C:/foo%2f', // trailing, encoded slash michael@0: 'file:///C:/%3f%3F', michael@0: 'file:///C:/%3b%3B', michael@0: 'file:///C:/%3c%3C', // not one of the special-cased ? or ; michael@0: 'file:///C:/%78', // 'x', not usually uri encoded michael@0: 'file:///C:/test#frag', // a fragment identifier michael@0: 'file:///C:/test?action=index' // an actual query component michael@0: ] : [ michael@0: 'file:///test/', michael@0: 'file://localhost/test', michael@0: 'file:///test/test.txt', michael@0: 'file:///foo%2f', // trailing, encoded slash michael@0: 'file:///%3f%3F', michael@0: 'file:///%3b%3B', michael@0: 'file:///%3c%3C', // not one of the special-cased ? or ; michael@0: 'file:///%78', // 'x', not usually uri encoded michael@0: 'file:///test#frag', // a fragment identifier michael@0: 'file:///test?action=index' // an actual query component michael@0: ]; michael@0: michael@0: for (let path of paths) { michael@0: // convert that to a uri using FileUtils and Services, which toFileURI is trying to model michael@0: let file = FileUtils.File(path); michael@0: let uri = Services.io.newFileURI(file).spec; michael@0: do_check_eq(uri, OS.Path.toFileURI(path)); michael@0: michael@0: // keep the resulting URI to try the reverse, except for "C:\" for which the michael@0: // behavior of nsIFileURL and OS.File is inconsistent michael@0: if (path != "C:\\") { michael@0: uris.push(uri); michael@0: } michael@0: } michael@0: michael@0: for (let uri of uris) { michael@0: // convert URIs to paths with nsIFileURI, which fromFileURI is trying to model michael@0: let path = Services.io.newURI(uri, null, null).QueryInterface(Components.interfaces.nsIFileURL).file.path; michael@0: do_check_eq(path, OS.Path.fromFileURI(uri)); michael@0: } michael@0: michael@0: // check that non-file URLs aren't allowed michael@0: let thrown = false; michael@0: try { michael@0: OS.Path.fromFileURI('http://test.com') michael@0: } catch (e) { michael@0: do_check_eq(e.message, "fromFileURI expects a file URI"); michael@0: thrown = true; michael@0: } michael@0: do_check_true(thrown); michael@0: }