2019-01-01 19:58:40 -05:00
|
|
|
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
2019-03-06 20:48:46 -05:00
|
|
|
import { testPerm, assertEquals } from "./test_util.ts";
|
2018-10-26 16:01:45 -04:00
|
|
|
|
2019-03-06 16:54:58 -05:00
|
|
|
const isNotWindows = Deno.build.os !== "win";
|
2018-10-26 16:01:45 -04:00
|
|
|
|
2019-02-08 15:59:38 -05:00
|
|
|
testPerm({ read: true, write: true }, function chmodSyncSuccess() {
|
2018-10-26 16:01:45 -04:00
|
|
|
const enc = new TextEncoder();
|
|
|
|
const data = enc.encode("Hello");
|
2019-02-12 10:08:56 -05:00
|
|
|
const tempDir = Deno.makeTempDirSync();
|
2018-10-26 16:01:45 -04:00
|
|
|
const filename = tempDir + "/test.txt";
|
2019-02-12 10:08:56 -05:00
|
|
|
Deno.writeFileSync(filename, data, { perm: 0o666 });
|
2018-10-26 16:01:45 -04:00
|
|
|
|
|
|
|
// On windows no effect, but should not crash
|
2019-02-12 10:08:56 -05:00
|
|
|
Deno.chmodSync(filename, 0o777);
|
2018-10-26 16:01:45 -04:00
|
|
|
|
|
|
|
// Check success when not on windows
|
|
|
|
if (isNotWindows) {
|
2019-02-12 10:08:56 -05:00
|
|
|
const fileInfo = Deno.statSync(filename);
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(fileInfo.mode & 0o777, 0o777);
|
2018-10-26 16:01:45 -04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Check symlink when not on windows
|
|
|
|
if (isNotWindows) {
|
2019-02-08 15:59:38 -05:00
|
|
|
testPerm({ read: true, write: true }, function chmodSyncSymlinkSuccess() {
|
2018-10-26 16:01:45 -04:00
|
|
|
const enc = new TextEncoder();
|
|
|
|
const data = enc.encode("Hello");
|
2019-02-12 10:08:56 -05:00
|
|
|
const tempDir = Deno.makeTempDirSync();
|
2018-10-26 16:01:45 -04:00
|
|
|
|
|
|
|
const filename = tempDir + "/test.txt";
|
2019-02-12 10:08:56 -05:00
|
|
|
Deno.writeFileSync(filename, data, { perm: 0o666 });
|
2018-10-26 16:01:45 -04:00
|
|
|
const symlinkName = tempDir + "/test_symlink.txt";
|
2019-02-12 10:08:56 -05:00
|
|
|
Deno.symlinkSync(filename, symlinkName);
|
2018-10-26 16:01:45 -04:00
|
|
|
|
2019-02-12 10:08:56 -05:00
|
|
|
let symlinkInfo = Deno.lstatSync(symlinkName);
|
2018-10-27 10:33:01 -04:00
|
|
|
const symlinkMode = symlinkInfo.mode & 0o777; // platform dependent
|
2018-10-26 16:01:45 -04:00
|
|
|
|
2019-02-12 10:08:56 -05:00
|
|
|
Deno.chmodSync(symlinkName, 0o777);
|
2018-10-26 16:01:45 -04:00
|
|
|
|
|
|
|
// Change actual file mode, not symlink
|
2019-02-12 10:08:56 -05:00
|
|
|
const fileInfo = Deno.statSync(filename);
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(fileInfo.mode & 0o777, 0o777);
|
2019-02-12 10:08:56 -05:00
|
|
|
symlinkInfo = Deno.lstatSync(symlinkName);
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(symlinkInfo.mode & 0o777, symlinkMode);
|
2018-10-26 16:01:45 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
testPerm({ write: true }, function chmodSyncFailure() {
|
|
|
|
let err;
|
|
|
|
try {
|
|
|
|
const filename = "/badfile.txt";
|
2019-02-12 10:08:56 -05:00
|
|
|
Deno.chmodSync(filename, 0o777);
|
2018-10-26 16:01:45 -04:00
|
|
|
} catch (e) {
|
|
|
|
err = e;
|
|
|
|
}
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(err.kind, Deno.ErrorKind.NotFound);
|
|
|
|
assertEquals(err.name, "NotFound");
|
2018-10-26 16:01:45 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
testPerm({ write: false }, function chmodSyncPerm() {
|
|
|
|
let err;
|
|
|
|
try {
|
2019-02-12 10:08:56 -05:00
|
|
|
Deno.chmodSync("/somefile.txt", 0o777);
|
2018-10-26 16:01:45 -04:00
|
|
|
} catch (e) {
|
|
|
|
err = e;
|
|
|
|
}
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(err.kind, Deno.ErrorKind.PermissionDenied);
|
|
|
|
assertEquals(err.name, "PermissionDenied");
|
2018-10-26 16:01:45 -04:00
|
|
|
});
|
|
|
|
|
2019-02-08 15:59:38 -05:00
|
|
|
testPerm({ read: true, write: true }, async function chmodSuccess() {
|
2018-10-26 16:01:45 -04:00
|
|
|
const enc = new TextEncoder();
|
|
|
|
const data = enc.encode("Hello");
|
2019-02-12 10:08:56 -05:00
|
|
|
const tempDir = Deno.makeTempDirSync();
|
2018-10-26 16:01:45 -04:00
|
|
|
const filename = tempDir + "/test.txt";
|
2019-02-12 10:08:56 -05:00
|
|
|
Deno.writeFileSync(filename, data, { perm: 0o666 });
|
2018-10-26 16:01:45 -04:00
|
|
|
|
|
|
|
// On windows no effect, but should not crash
|
2019-02-12 10:08:56 -05:00
|
|
|
await Deno.chmod(filename, 0o777);
|
2018-10-26 16:01:45 -04:00
|
|
|
|
|
|
|
// Check success when not on windows
|
|
|
|
if (isNotWindows) {
|
2019-02-12 10:08:56 -05:00
|
|
|
const fileInfo = Deno.statSync(filename);
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(fileInfo.mode & 0o777, 0o777);
|
2018-10-26 16:01:45 -04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Check symlink when not on windows
|
|
|
|
if (isNotWindows) {
|
2019-02-08 15:59:38 -05:00
|
|
|
testPerm({ read: true, write: true }, async function chmodSymlinkSuccess() {
|
2018-10-26 16:01:45 -04:00
|
|
|
const enc = new TextEncoder();
|
|
|
|
const data = enc.encode("Hello");
|
2019-02-12 10:08:56 -05:00
|
|
|
const tempDir = Deno.makeTempDirSync();
|
2018-10-26 16:01:45 -04:00
|
|
|
|
|
|
|
const filename = tempDir + "/test.txt";
|
2019-02-12 10:08:56 -05:00
|
|
|
Deno.writeFileSync(filename, data, { perm: 0o666 });
|
2018-10-26 16:01:45 -04:00
|
|
|
const symlinkName = tempDir + "/test_symlink.txt";
|
2019-02-12 10:08:56 -05:00
|
|
|
Deno.symlinkSync(filename, symlinkName);
|
2018-10-26 16:01:45 -04:00
|
|
|
|
2019-02-12 10:08:56 -05:00
|
|
|
let symlinkInfo = Deno.lstatSync(symlinkName);
|
2018-10-27 10:33:01 -04:00
|
|
|
const symlinkMode = symlinkInfo.mode & 0o777; // platform dependent
|
2018-10-26 16:01:45 -04:00
|
|
|
|
2019-02-12 10:08:56 -05:00
|
|
|
await Deno.chmod(symlinkName, 0o777);
|
2018-10-26 16:01:45 -04:00
|
|
|
|
|
|
|
// Just change actual file mode, not symlink
|
2019-02-12 10:08:56 -05:00
|
|
|
const fileInfo = Deno.statSync(filename);
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(fileInfo.mode & 0o777, 0o777);
|
2019-02-12 10:08:56 -05:00
|
|
|
symlinkInfo = Deno.lstatSync(symlinkName);
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(symlinkInfo.mode & 0o777, symlinkMode);
|
2018-10-26 16:01:45 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
testPerm({ write: true }, async function chmodFailure() {
|
|
|
|
let err;
|
|
|
|
try {
|
|
|
|
const filename = "/badfile.txt";
|
2019-02-12 10:08:56 -05:00
|
|
|
await Deno.chmod(filename, 0o777);
|
2018-10-26 16:01:45 -04:00
|
|
|
} catch (e) {
|
|
|
|
err = e;
|
|
|
|
}
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(err.kind, Deno.ErrorKind.NotFound);
|
|
|
|
assertEquals(err.name, "NotFound");
|
2018-10-26 16:01:45 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
testPerm({ write: false }, async function chmodPerm() {
|
|
|
|
let err;
|
|
|
|
try {
|
2019-02-12 10:08:56 -05:00
|
|
|
await Deno.chmod("/somefile.txt", 0o777);
|
2018-10-26 16:01:45 -04:00
|
|
|
} catch (e) {
|
|
|
|
err = e;
|
|
|
|
}
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(err.kind, Deno.ErrorKind.PermissionDenied);
|
|
|
|
assertEquals(err.name, "PermissionDenied");
|
2018-10-26 16:01:45 -04:00
|
|
|
});
|