1// Copyright Joyent, Inc. and other Node contributors. 2// 3// Permission is hereby granted, free of charge, to any person obtaining a 4// copy of this software and associated documentation files (the 5// "Software"), to deal in the Software without restriction, including 6// without limitation the rights to use, copy, modify, merge, publish, 7// distribute, sublicense, and/or sell copies of the Software, and to permit 8// persons to whom the Software is furnished to do so, subject to the 9// following conditions: 10// 11// The above copyright notice and this permission notice shall be included 12// in all copies or substantial portions of the Software. 13// 14// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 15// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN 17// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 18// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 19// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 20// USE OR OTHER DEALINGS IN THE SOFTWARE. 21 22'use strict'; 23const common = require('../common'); 24const fixtures = require('../common/fixtures'); 25if (!common.canCreateSymLink()) 26 common.skip('insufficient privileges'); 27 28const assert = require('assert'); 29const path = require('path'); 30const fs = require('fs'); 31 32let linkTime; 33let fileTime; 34 35const tmpdir = require('../common/tmpdir'); 36tmpdir.refresh(); 37 38// Test creating and reading symbolic link 39const linkData = fixtures.path('/cycles/root.js'); 40const linkPath = path.join(tmpdir.path, 'symlink1.js'); 41 42fs.symlink(linkData, linkPath, common.mustCall(function(err) { 43 assert.ifError(err); 44 45 fs.lstat(linkPath, common.mustCall(function(err, stats) { 46 assert.ifError(err); 47 linkTime = stats.mtime.getTime(); 48 })); 49 50 fs.stat(linkPath, common.mustCall(function(err, stats) { 51 assert.ifError(err); 52 fileTime = stats.mtime.getTime(); 53 })); 54 55 fs.readlink(linkPath, common.mustCall(function(err, destination) { 56 assert.ifError(err); 57 assert.strictEqual(destination, linkData); 58 })); 59})); 60 61// Test invalid symlink 62{ 63 const linkData = fixtures.path('/not/exists/file'); 64 const linkPath = path.join(tmpdir.path, 'symlink2.js'); 65 66 fs.symlink(linkData, linkPath, common.mustCall(function(err) { 67 assert.ifError(err); 68 69 assert(!fs.existsSync(linkPath)); 70 })); 71} 72 73[false, 1, {}, [], null, undefined].forEach((input) => { 74 const errObj = { 75 code: 'ERR_INVALID_ARG_TYPE', 76 name: 'TypeError', 77 message: /target|path/ 78 }; 79 assert.throws(() => fs.symlink(input, '', common.mustNotCall()), errObj); 80 assert.throws(() => fs.symlinkSync(input, ''), errObj); 81 82 assert.throws(() => fs.symlink('', input, common.mustNotCall()), errObj); 83 assert.throws(() => fs.symlinkSync('', input), errObj); 84}); 85 86const errObj = { 87 code: 'ERR_FS_INVALID_SYMLINK_TYPE', 88 name: 'Error', 89 message: 90 'Symlink type must be one of "dir", "file", or "junction". Received ""' 91}; 92assert.throws(() => fs.symlink('', '', '', common.mustNotCall()), errObj); 93assert.throws(() => fs.symlinkSync('', '', ''), errObj); 94 95process.on('exit', function() { 96 assert.notStrictEqual(linkTime, fileTime); 97}); 98