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

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/components/osfile/tests/xpcshell/test_file_URL_conversion.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,114 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this file,
     1.6 + * You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +function run_test() {
     1.9 +  Components.utils.import("resource://gre/modules/Services.jsm");
    1.10 +  Components.utils.import("resource://gre/modules/osfile.jsm");
    1.11 +  Components.utils.import("resource://gre/modules/FileUtils.jsm");
    1.12 +
    1.13 +  let isWindows = ("@mozilla.org/windows-registry-key;1" in Components.classes);
    1.14 +
    1.15 +  // Test cases for filePathToURI
    1.16 +  let paths = isWindows ? [
    1.17 +    'C:\\',
    1.18 +    'C:\\test',
    1.19 +    'C:\\test\\',
    1.20 +    'C:\\test%2f',
    1.21 +    'C:\\test\\test\\test',
    1.22 +    'C:\\test;+%',
    1.23 +    'C:\\test?action=index\\',
    1.24 +    'C:\\test\ test',
    1.25 +    '\\\\C:\\a\\b\\c',
    1.26 +    '\\\\Server\\a\\b\\c',
    1.27 +
    1.28 +    // note that per http://support.microsoft.com/kb/177506 (under more info),
    1.29 +    // the following characters are allowed on Windows:
    1.30 +    'C:\\char^',
    1.31 +    'C:\\char&',
    1.32 +    'C:\\char\'',
    1.33 +    'C:\\char@',
    1.34 +    'C:\\char{',
    1.35 +    'C:\\char}',
    1.36 +    'C:\\char[',
    1.37 +    'C:\\char]',
    1.38 +    'C:\\char,',
    1.39 +    'C:\\char$',
    1.40 +    'C:\\char=',
    1.41 +    'C:\\char!',
    1.42 +    'C:\\char-',
    1.43 +    'C:\\char#',
    1.44 +    'C:\\char(',
    1.45 +    'C:\\char)',
    1.46 +    'C:\\char%',
    1.47 +    'C:\\char.',
    1.48 +    'C:\\char+',
    1.49 +    'C:\\char~',
    1.50 +    'C:\\char_'
    1.51 +  ] : [
    1.52 +    '/',
    1.53 +    '/test',
    1.54 +    '/test/',
    1.55 +    '/test%2f',
    1.56 +    '/test/test/test',
    1.57 +    '/test;+%',
    1.58 +    '/test?action=index/',
    1.59 +    '/test\ test',
    1.60 +    '/punctuation/;,/?:@&=+$-_.!~*\'()"#',
    1.61 +    '/CasePreserving'
    1.62 +  ];
    1.63 +
    1.64 +  // some additional URIs to test, beyond those generated from paths
    1.65 +  let uris = isWindows ? [
    1.66 +    'file:///C:/test/',
    1.67 +    'file://localhost/C:/test',
    1.68 +    'file:///c:/test/test.txt',
    1.69 +    //'file:///C:/foo%2f', // trailing, encoded slash
    1.70 +    'file:///C:/%3f%3F',
    1.71 +    'file:///C:/%3b%3B',
    1.72 +    'file:///C:/%3c%3C', // not one of the special-cased ? or ;
    1.73 +    'file:///C:/%78', // 'x', not usually uri encoded
    1.74 +    'file:///C:/test#frag', // a fragment identifier
    1.75 +    'file:///C:/test?action=index' // an actual query component
    1.76 +  ] : [
    1.77 +    'file:///test/',
    1.78 +    'file://localhost/test',
    1.79 +    'file:///test/test.txt',
    1.80 +    'file:///foo%2f', // trailing, encoded slash
    1.81 +    'file:///%3f%3F',
    1.82 +    'file:///%3b%3B',
    1.83 +    'file:///%3c%3C', // not one of the special-cased ? or ;
    1.84 +    'file:///%78', // 'x', not usually uri encoded
    1.85 +    'file:///test#frag', // a fragment identifier
    1.86 +    'file:///test?action=index' // an actual query component
    1.87 +  ];
    1.88 +
    1.89 +  for (let path of paths) {
    1.90 +    // convert that to a uri using FileUtils and Services, which toFileURI is trying to model
    1.91 +    let file = FileUtils.File(path);
    1.92 +    let uri = Services.io.newFileURI(file).spec;
    1.93 +    do_check_eq(uri, OS.Path.toFileURI(path));
    1.94 +
    1.95 +    // keep the resulting URI to try the reverse, except for "C:\" for which the
    1.96 +    // behavior of nsIFileURL and OS.File is inconsistent
    1.97 +    if (path != "C:\\") {
    1.98 +      uris.push(uri);
    1.99 +    }
   1.100 +  }
   1.101 +
   1.102 +  for (let uri of uris) {
   1.103 +    // convert URIs to paths with nsIFileURI, which fromFileURI is trying to model
   1.104 +    let path = Services.io.newURI(uri, null, null).QueryInterface(Components.interfaces.nsIFileURL).file.path;
   1.105 +    do_check_eq(path, OS.Path.fromFileURI(uri));
   1.106 +  }
   1.107 +
   1.108 +  // check that non-file URLs aren't allowed
   1.109 +  let thrown = false;
   1.110 +  try {
   1.111 +    OS.Path.fromFileURI('http://test.com')
   1.112 +  } catch (e) {
   1.113 +    do_check_eq(e.message, "fromFileURI expects a file URI");
   1.114 +    thrown = true;
   1.115 +  }
   1.116 +  do_check_true(thrown);
   1.117 +}

mercurial