11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ciconst common = require('../common');
41cb0ef41Sopenharmony_ciconst fixtures = require('../common/fixtures');
51cb0ef41Sopenharmony_ciconst assert = require('assert');
61cb0ef41Sopenharmony_ciconst fs = require('fs');
71cb0ef41Sopenharmony_ciconst os = require('os');
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ciconst url = fixtures.fileURL('a.js');
101cb0ef41Sopenharmony_ci
111cb0ef41Sopenharmony_ciassert(url instanceof URL);
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_ci// Check that we can pass in a URL object successfully
141cb0ef41Sopenharmony_cifs.readFile(url, common.mustSucceed((data) => {
151cb0ef41Sopenharmony_ci  assert(Buffer.isBuffer(data));
161cb0ef41Sopenharmony_ci}));
171cb0ef41Sopenharmony_ci
181cb0ef41Sopenharmony_ci// Check that using a non file:// URL reports an error
191cb0ef41Sopenharmony_ciconst httpUrl = new URL('http://example.org');
201cb0ef41Sopenharmony_ci
211cb0ef41Sopenharmony_ciassert.throws(
221cb0ef41Sopenharmony_ci  () => {
231cb0ef41Sopenharmony_ci    fs.readFile(httpUrl, common.mustNotCall());
241cb0ef41Sopenharmony_ci  },
251cb0ef41Sopenharmony_ci  {
261cb0ef41Sopenharmony_ci    code: 'ERR_INVALID_URL_SCHEME',
271cb0ef41Sopenharmony_ci    name: 'TypeError',
281cb0ef41Sopenharmony_ci    message: 'The URL must be of scheme file'
291cb0ef41Sopenharmony_ci  });
301cb0ef41Sopenharmony_ci
311cb0ef41Sopenharmony_ci// pct-encoded characters in the path will be decoded and checked
321cb0ef41Sopenharmony_ciif (common.isWindows) {
331cb0ef41Sopenharmony_ci  // Encoded back and forward slashes are not permitted on windows
341cb0ef41Sopenharmony_ci  ['%2f', '%2F', '%5c', '%5C'].forEach((i) => {
351cb0ef41Sopenharmony_ci    assert.throws(
361cb0ef41Sopenharmony_ci      () => {
371cb0ef41Sopenharmony_ci        fs.readFile(new URL(`file:///c:/tmp/${i}`), common.mustNotCall());
381cb0ef41Sopenharmony_ci      },
391cb0ef41Sopenharmony_ci      {
401cb0ef41Sopenharmony_ci        code: 'ERR_INVALID_FILE_URL_PATH',
411cb0ef41Sopenharmony_ci        name: 'TypeError',
421cb0ef41Sopenharmony_ci        message: 'File URL path must not include encoded \\ or / characters'
431cb0ef41Sopenharmony_ci      }
441cb0ef41Sopenharmony_ci    );
451cb0ef41Sopenharmony_ci  });
461cb0ef41Sopenharmony_ci  assert.throws(
471cb0ef41Sopenharmony_ci    () => {
481cb0ef41Sopenharmony_ci      fs.readFile(new URL('file:///c:/tmp/%00test'), common.mustNotCall());
491cb0ef41Sopenharmony_ci    },
501cb0ef41Sopenharmony_ci    {
511cb0ef41Sopenharmony_ci      code: 'ERR_INVALID_ARG_VALUE',
521cb0ef41Sopenharmony_ci      name: 'TypeError',
531cb0ef41Sopenharmony_ci      message: 'The argument \'path\' must be a string or Uint8Array without ' +
541cb0ef41Sopenharmony_ci               "null bytes. Received 'c:\\\\tmp\\\\\\x00test'"
551cb0ef41Sopenharmony_ci    }
561cb0ef41Sopenharmony_ci  );
571cb0ef41Sopenharmony_ci} else {
581cb0ef41Sopenharmony_ci  // Encoded forward slashes are not permitted on other platforms
591cb0ef41Sopenharmony_ci  ['%2f', '%2F'].forEach((i) => {
601cb0ef41Sopenharmony_ci    assert.throws(
611cb0ef41Sopenharmony_ci      () => {
621cb0ef41Sopenharmony_ci        fs.readFile(new URL(`file:///c:/tmp/${i}`), common.mustNotCall());
631cb0ef41Sopenharmony_ci      },
641cb0ef41Sopenharmony_ci      {
651cb0ef41Sopenharmony_ci        code: 'ERR_INVALID_FILE_URL_PATH',
661cb0ef41Sopenharmony_ci        name: 'TypeError',
671cb0ef41Sopenharmony_ci        message: 'File URL path must not include encoded / characters'
681cb0ef41Sopenharmony_ci      });
691cb0ef41Sopenharmony_ci  });
701cb0ef41Sopenharmony_ci  assert.throws(
711cb0ef41Sopenharmony_ci    () => {
721cb0ef41Sopenharmony_ci      fs.readFile(new URL('file://hostname/a/b/c'), common.mustNotCall());
731cb0ef41Sopenharmony_ci    },
741cb0ef41Sopenharmony_ci    {
751cb0ef41Sopenharmony_ci      code: 'ERR_INVALID_FILE_URL_HOST',
761cb0ef41Sopenharmony_ci      name: 'TypeError',
771cb0ef41Sopenharmony_ci      message: `File URL host must be "localhost" or empty on ${os.platform()}`
781cb0ef41Sopenharmony_ci    }
791cb0ef41Sopenharmony_ci  );
801cb0ef41Sopenharmony_ci  assert.throws(
811cb0ef41Sopenharmony_ci    () => {
821cb0ef41Sopenharmony_ci      fs.readFile(new URL('file:///tmp/%00test'), common.mustNotCall());
831cb0ef41Sopenharmony_ci    },
841cb0ef41Sopenharmony_ci    {
851cb0ef41Sopenharmony_ci      code: 'ERR_INVALID_ARG_VALUE',
861cb0ef41Sopenharmony_ci      name: 'TypeError',
871cb0ef41Sopenharmony_ci      message: "The argument 'path' must be a string or Uint8Array without " +
881cb0ef41Sopenharmony_ci               "null bytes. Received '/tmp/\\x00test'"
891cb0ef41Sopenharmony_ci    }
901cb0ef41Sopenharmony_ci  );
911cb0ef41Sopenharmony_ci}
92