11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ciconst { isWindows } = require('../common');
31cb0ef41Sopenharmony_ciconst assert = require('assert');
41cb0ef41Sopenharmony_ciconst url = require('url');
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_cifunction testInvalidArgs(...args) {
71cb0ef41Sopenharmony_ci  for (const arg of args) {
81cb0ef41Sopenharmony_ci    assert.throws(() => url.fileURLToPath(arg), {
91cb0ef41Sopenharmony_ci      code: 'ERR_INVALID_ARG_TYPE'
101cb0ef41Sopenharmony_ci    });
111cb0ef41Sopenharmony_ci  }
121cb0ef41Sopenharmony_ci}
131cb0ef41Sopenharmony_ci
141cb0ef41Sopenharmony_ci// Input must be string or URL
151cb0ef41Sopenharmony_citestInvalidArgs(null, undefined, 1, {}, true);
161cb0ef41Sopenharmony_ci
171cb0ef41Sopenharmony_ci// Input must be a file URL
181cb0ef41Sopenharmony_ciassert.throws(() => url.fileURLToPath('https://a/b/c'), {
191cb0ef41Sopenharmony_ci  code: 'ERR_INVALID_URL_SCHEME'
201cb0ef41Sopenharmony_ci});
211cb0ef41Sopenharmony_ci
221cb0ef41Sopenharmony_ci{
231cb0ef41Sopenharmony_ci  const withHost = new URL('file://host/a');
241cb0ef41Sopenharmony_ci
251cb0ef41Sopenharmony_ci  if (isWindows) {
261cb0ef41Sopenharmony_ci    assert.strictEqual(url.fileURLToPath(withHost), '\\\\host\\a');
271cb0ef41Sopenharmony_ci  } else {
281cb0ef41Sopenharmony_ci    assert.throws(() => url.fileURLToPath(withHost), {
291cb0ef41Sopenharmony_ci      code: 'ERR_INVALID_FILE_URL_HOST'
301cb0ef41Sopenharmony_ci    });
311cb0ef41Sopenharmony_ci  }
321cb0ef41Sopenharmony_ci}
331cb0ef41Sopenharmony_ci
341cb0ef41Sopenharmony_ci{
351cb0ef41Sopenharmony_ci  if (isWindows) {
361cb0ef41Sopenharmony_ci    assert.throws(() => url.fileURLToPath('file:///C:/a%2F/'), {
371cb0ef41Sopenharmony_ci      code: 'ERR_INVALID_FILE_URL_PATH'
381cb0ef41Sopenharmony_ci    });
391cb0ef41Sopenharmony_ci    assert.throws(() => url.fileURLToPath('file:///C:/a%5C/'), {
401cb0ef41Sopenharmony_ci      code: 'ERR_INVALID_FILE_URL_PATH'
411cb0ef41Sopenharmony_ci    });
421cb0ef41Sopenharmony_ci    assert.throws(() => url.fileURLToPath('file:///?:/'), {
431cb0ef41Sopenharmony_ci      code: 'ERR_INVALID_FILE_URL_PATH'
441cb0ef41Sopenharmony_ci    });
451cb0ef41Sopenharmony_ci  } else {
461cb0ef41Sopenharmony_ci    assert.throws(() => url.fileURLToPath('file:///a%2F/'), {
471cb0ef41Sopenharmony_ci      code: 'ERR_INVALID_FILE_URL_PATH'
481cb0ef41Sopenharmony_ci    });
491cb0ef41Sopenharmony_ci  }
501cb0ef41Sopenharmony_ci}
511cb0ef41Sopenharmony_ci
521cb0ef41Sopenharmony_ci{
531cb0ef41Sopenharmony_ci  let testCases;
541cb0ef41Sopenharmony_ci  if (isWindows) {
551cb0ef41Sopenharmony_ci    testCases = [
561cb0ef41Sopenharmony_ci      // Lowercase ascii alpha
571cb0ef41Sopenharmony_ci      { path: 'C:\\foo', fileURL: 'file:///C:/foo' },
581cb0ef41Sopenharmony_ci      // Uppercase ascii alpha
591cb0ef41Sopenharmony_ci      { path: 'C:\\FOO', fileURL: 'file:///C:/FOO' },
601cb0ef41Sopenharmony_ci      // dir
611cb0ef41Sopenharmony_ci      { path: 'C:\\dir\\foo', fileURL: 'file:///C:/dir/foo' },
621cb0ef41Sopenharmony_ci      // trailing separator
631cb0ef41Sopenharmony_ci      { path: 'C:\\dir\\', fileURL: 'file:///C:/dir/' },
641cb0ef41Sopenharmony_ci      // dot
651cb0ef41Sopenharmony_ci      { path: 'C:\\foo.mjs', fileURL: 'file:///C:/foo.mjs' },
661cb0ef41Sopenharmony_ci      // space
671cb0ef41Sopenharmony_ci      { path: 'C:\\foo bar', fileURL: 'file:///C:/foo%20bar' },
681cb0ef41Sopenharmony_ci      // question mark
691cb0ef41Sopenharmony_ci      { path: 'C:\\foo?bar', fileURL: 'file:///C:/foo%3Fbar' },
701cb0ef41Sopenharmony_ci      // number sign
711cb0ef41Sopenharmony_ci      { path: 'C:\\foo#bar', fileURL: 'file:///C:/foo%23bar' },
721cb0ef41Sopenharmony_ci      // ampersand
731cb0ef41Sopenharmony_ci      { path: 'C:\\foo&bar', fileURL: 'file:///C:/foo&bar' },
741cb0ef41Sopenharmony_ci      // equals
751cb0ef41Sopenharmony_ci      { path: 'C:\\foo=bar', fileURL: 'file:///C:/foo=bar' },
761cb0ef41Sopenharmony_ci      // colon
771cb0ef41Sopenharmony_ci      { path: 'C:\\foo:bar', fileURL: 'file:///C:/foo:bar' },
781cb0ef41Sopenharmony_ci      // semicolon
791cb0ef41Sopenharmony_ci      { path: 'C:\\foo;bar', fileURL: 'file:///C:/foo;bar' },
801cb0ef41Sopenharmony_ci      // percent
811cb0ef41Sopenharmony_ci      { path: 'C:\\foo%bar', fileURL: 'file:///C:/foo%25bar' },
821cb0ef41Sopenharmony_ci      // backslash
831cb0ef41Sopenharmony_ci      { path: 'C:\\foo\\bar', fileURL: 'file:///C:/foo/bar' },
841cb0ef41Sopenharmony_ci      // backspace
851cb0ef41Sopenharmony_ci      { path: 'C:\\foo\bbar', fileURL: 'file:///C:/foo%08bar' },
861cb0ef41Sopenharmony_ci      // tab
871cb0ef41Sopenharmony_ci      { path: 'C:\\foo\tbar', fileURL: 'file:///C:/foo%09bar' },
881cb0ef41Sopenharmony_ci      // newline
891cb0ef41Sopenharmony_ci      { path: 'C:\\foo\nbar', fileURL: 'file:///C:/foo%0Abar' },
901cb0ef41Sopenharmony_ci      // carriage return
911cb0ef41Sopenharmony_ci      { path: 'C:\\foo\rbar', fileURL: 'file:///C:/foo%0Dbar' },
921cb0ef41Sopenharmony_ci      // latin1
931cb0ef41Sopenharmony_ci      { path: 'C:\\fóóbàr', fileURL: 'file:///C:/f%C3%B3%C3%B3b%C3%A0r' },
941cb0ef41Sopenharmony_ci      // Euro sign (BMP code point)
951cb0ef41Sopenharmony_ci      { path: 'C:\\€', fileURL: 'file:///C:/%E2%82%AC' },
961cb0ef41Sopenharmony_ci      // Rocket emoji (non-BMP code point)
971cb0ef41Sopenharmony_ci      { path: 'C:\\�', fileURL: 'file:///C:/%F0%9F%9A%80' },
981cb0ef41Sopenharmony_ci      // UNC path (see https://docs.microsoft.com/en-us/archive/blogs/ie/file-uris-in-windows)
991cb0ef41Sopenharmony_ci      { path: '\\\\nas\\My Docs\\File.doc', fileURL: 'file://nas/My%20Docs/File.doc' },
1001cb0ef41Sopenharmony_ci    ];
1011cb0ef41Sopenharmony_ci  } else {
1021cb0ef41Sopenharmony_ci    testCases = [
1031cb0ef41Sopenharmony_ci      // Lowercase ascii alpha
1041cb0ef41Sopenharmony_ci      { path: '/foo', fileURL: 'file:///foo' },
1051cb0ef41Sopenharmony_ci      // Uppercase ascii alpha
1061cb0ef41Sopenharmony_ci      { path: '/FOO', fileURL: 'file:///FOO' },
1071cb0ef41Sopenharmony_ci      // dir
1081cb0ef41Sopenharmony_ci      { path: '/dir/foo', fileURL: 'file:///dir/foo' },
1091cb0ef41Sopenharmony_ci      // trailing separator
1101cb0ef41Sopenharmony_ci      { path: '/dir/', fileURL: 'file:///dir/' },
1111cb0ef41Sopenharmony_ci      // dot
1121cb0ef41Sopenharmony_ci      { path: '/foo.mjs', fileURL: 'file:///foo.mjs' },
1131cb0ef41Sopenharmony_ci      // space
1141cb0ef41Sopenharmony_ci      { path: '/foo bar', fileURL: 'file:///foo%20bar' },
1151cb0ef41Sopenharmony_ci      // question mark
1161cb0ef41Sopenharmony_ci      { path: '/foo?bar', fileURL: 'file:///foo%3Fbar' },
1171cb0ef41Sopenharmony_ci      // number sign
1181cb0ef41Sopenharmony_ci      { path: '/foo#bar', fileURL: 'file:///foo%23bar' },
1191cb0ef41Sopenharmony_ci      // ampersand
1201cb0ef41Sopenharmony_ci      { path: '/foo&bar', fileURL: 'file:///foo&bar' },
1211cb0ef41Sopenharmony_ci      // equals
1221cb0ef41Sopenharmony_ci      { path: '/foo=bar', fileURL: 'file:///foo=bar' },
1231cb0ef41Sopenharmony_ci      // colon
1241cb0ef41Sopenharmony_ci      { path: '/foo:bar', fileURL: 'file:///foo:bar' },
1251cb0ef41Sopenharmony_ci      // semicolon
1261cb0ef41Sopenharmony_ci      { path: '/foo;bar', fileURL: 'file:///foo;bar' },
1271cb0ef41Sopenharmony_ci      // percent
1281cb0ef41Sopenharmony_ci      { path: '/foo%bar', fileURL: 'file:///foo%25bar' },
1291cb0ef41Sopenharmony_ci      // backslash
1301cb0ef41Sopenharmony_ci      { path: '/foo\\bar', fileURL: 'file:///foo%5Cbar' },
1311cb0ef41Sopenharmony_ci      // backspace
1321cb0ef41Sopenharmony_ci      { path: '/foo\bbar', fileURL: 'file:///foo%08bar' },
1331cb0ef41Sopenharmony_ci      // tab
1341cb0ef41Sopenharmony_ci      { path: '/foo\tbar', fileURL: 'file:///foo%09bar' },
1351cb0ef41Sopenharmony_ci      // newline
1361cb0ef41Sopenharmony_ci      { path: '/foo\nbar', fileURL: 'file:///foo%0Abar' },
1371cb0ef41Sopenharmony_ci      // carriage return
1381cb0ef41Sopenharmony_ci      { path: '/foo\rbar', fileURL: 'file:///foo%0Dbar' },
1391cb0ef41Sopenharmony_ci      // latin1
1401cb0ef41Sopenharmony_ci      { path: '/fóóbàr', fileURL: 'file:///f%C3%B3%C3%B3b%C3%A0r' },
1411cb0ef41Sopenharmony_ci      // Euro sign (BMP code point)
1421cb0ef41Sopenharmony_ci      { path: '/€', fileURL: 'file:///%E2%82%AC' },
1431cb0ef41Sopenharmony_ci      // Rocket emoji (non-BMP code point)
1441cb0ef41Sopenharmony_ci      { path: '/�', fileURL: 'file:///%F0%9F%9A%80' },
1451cb0ef41Sopenharmony_ci    ];
1461cb0ef41Sopenharmony_ci  }
1471cb0ef41Sopenharmony_ci
1481cb0ef41Sopenharmony_ci  for (const { path, fileURL } of testCases) {
1491cb0ef41Sopenharmony_ci    const fromString = url.fileURLToPath(fileURL);
1501cb0ef41Sopenharmony_ci    assert.strictEqual(fromString, path);
1511cb0ef41Sopenharmony_ci    const fromURL = url.fileURLToPath(new URL(fileURL));
1521cb0ef41Sopenharmony_ci    assert.strictEqual(fromURL, path);
1531cb0ef41Sopenharmony_ci  }
1541cb0ef41Sopenharmony_ci}
155