michael@0: /* Any copyright is dedicated to the Public Domain. michael@0: * http://creativecommons.org/publicdomain/zero/1.0/ */ michael@0: michael@0: "use strict"; michael@0: michael@0: /** michael@0: * A test to ensure that OS.File.setPermissions and michael@0: * OS.File.prototype.setPermissions are all working correctly. michael@0: * (see bug 1001849) michael@0: * These functions are currently Unix-specific. The manifest skips michael@0: * the test on Windows. michael@0: */ michael@0: michael@0: /** michael@0: * Helper function for test logging: prints a POSIX file permission mode as an michael@0: * octal number, with a leading '0' per C (not JS) convention. When the michael@0: * numeric value is 0777 or lower, it is padded on the left with zeroes to michael@0: * four digits wide. michael@0: * Sample outputs: 0022, 0644, 04755. michael@0: */ michael@0: function format_mode(mode) { michael@0: if (mode <= 0o777) { michael@0: return ("0000" + mode.toString(8)).slice(-4); michael@0: } else { michael@0: return "0" + mode.toString(8); michael@0: } michael@0: } michael@0: michael@0: /** michael@0: * Use this function to compare two mode values; it prints both values as michael@0: * octal numbers in the log. michael@0: */ michael@0: function do_check_modes_eq(left, right, text) { michael@0: text = text + ": " + format_mode(left) + " === " + format_mode(right); michael@0: do_report_result(left === right, text, Components.stack.caller, false); michael@0: } michael@0: michael@0: const _umask = OS.Constants.Sys.umask; michael@0: do_print("umask: " + format_mode(_umask)); michael@0: michael@0: /** michael@0: * Compute the mode that a file should have after applying the umask, michael@0: * whatever it happens to be. michael@0: */ michael@0: function apply_umask(mode) { michael@0: return mode & ~_umask; michael@0: } michael@0: michael@0: // Test application to paths. michael@0: add_task(function*() { michael@0: let path = OS.Path.join(OS.Constants.Path.tmpDir, michael@0: "test_osfile_async_setPerms_nonproto.tmp"); michael@0: yield OS.File.writeAtomic(path, new Uint8Array(1)); michael@0: michael@0: try { michael@0: let stat; michael@0: michael@0: yield OS.File.setPermissions(path, {unixMode: 0o4777}); michael@0: stat = yield OS.File.stat(path); michael@0: do_check_modes_eq(stat.unixMode, 0o4777, michael@0: "setPermissions(path, 04777)"); michael@0: michael@0: yield OS.File.setPermissions(path, {unixMode: 0o4777, michael@0: unixHonorUmask: true}); michael@0: stat = yield OS.File.stat(path); michael@0: do_check_modes_eq(stat.unixMode, apply_umask(0o4777), michael@0: "setPermissions(path, 04777&~umask)"); michael@0: michael@0: yield OS.File.setPermissions(path); michael@0: stat = yield OS.File.stat(path); michael@0: do_check_modes_eq(stat.unixMode, apply_umask(0o666), michael@0: "setPermissions(path, {})"); michael@0: michael@0: yield OS.File.setPermissions(path, {unixMode: 0}); michael@0: stat = yield OS.File.stat(path); michael@0: do_check_modes_eq(stat.unixMode, 0, michael@0: "setPermissions(path, 0000)"); michael@0: michael@0: } finally { michael@0: yield OS.File.remove(path); michael@0: } michael@0: }); michael@0: michael@0: // Test application to open files. michael@0: add_task(function*() { michael@0: // First, create a file we can mess with. michael@0: let path = OS.Path.join(OS.Constants.Path.tmpDir, michael@0: "test_osfile_async_setDates_proto.tmp"); michael@0: yield OS.File.writeAtomic(path, new Uint8Array(1)); michael@0: michael@0: try { michael@0: let fd = yield OS.File.open(path, {write: true}); michael@0: let stat; michael@0: michael@0: yield fd.setPermissions({unixMode: 0o4777}); michael@0: stat = yield fd.stat(); michael@0: do_check_modes_eq(stat.unixMode, 0o4777, michael@0: "fd.setPermissions(04777)"); michael@0: michael@0: yield fd.setPermissions({unixMode: 0o4777, unixHonorUmask: true}); michael@0: stat = yield fd.stat(); michael@0: do_check_modes_eq(stat.unixMode, apply_umask(0o4777), michael@0: "fd.setPermissions(04777&~umask)"); michael@0: michael@0: yield fd.setPermissions(); michael@0: stat = yield fd.stat(); michael@0: do_check_modes_eq(stat.unixMode, apply_umask(0o666), michael@0: "fd.setPermissions({})"); michael@0: michael@0: yield fd.setPermissions({unixMode: 0}); michael@0: stat = yield fd.stat(); michael@0: do_check_modes_eq(stat.unixMode, 0, michael@0: "fd.setPermissions(0000)"); michael@0: michael@0: yield fd.close(); michael@0: } finally { michael@0: yield OS.File.remove(path); michael@0: } michael@0: }); michael@0: michael@0: function run_test() { michael@0: run_next_test(); michael@0: }