Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* Any copyright is dedicated to the Public Domain. |
michael@0 | 2 | * http://creativecommons.org/publicdomain/zero/1.0/ */ |
michael@0 | 3 | |
michael@0 | 4 | "use strict"; |
michael@0 | 5 | |
michael@0 | 6 | /** |
michael@0 | 7 | * A test to ensure that OS.File.setPermissions and |
michael@0 | 8 | * OS.File.prototype.setPermissions are all working correctly. |
michael@0 | 9 | * (see bug 1001849) |
michael@0 | 10 | * These functions are currently Unix-specific. The manifest skips |
michael@0 | 11 | * the test on Windows. |
michael@0 | 12 | */ |
michael@0 | 13 | |
michael@0 | 14 | /** |
michael@0 | 15 | * Helper function for test logging: prints a POSIX file permission mode as an |
michael@0 | 16 | * octal number, with a leading '0' per C (not JS) convention. When the |
michael@0 | 17 | * numeric value is 0777 or lower, it is padded on the left with zeroes to |
michael@0 | 18 | * four digits wide. |
michael@0 | 19 | * Sample outputs: 0022, 0644, 04755. |
michael@0 | 20 | */ |
michael@0 | 21 | function format_mode(mode) { |
michael@0 | 22 | if (mode <= 0o777) { |
michael@0 | 23 | return ("0000" + mode.toString(8)).slice(-4); |
michael@0 | 24 | } else { |
michael@0 | 25 | return "0" + mode.toString(8); |
michael@0 | 26 | } |
michael@0 | 27 | } |
michael@0 | 28 | |
michael@0 | 29 | /** |
michael@0 | 30 | * Use this function to compare two mode values; it prints both values as |
michael@0 | 31 | * octal numbers in the log. |
michael@0 | 32 | */ |
michael@0 | 33 | function do_check_modes_eq(left, right, text) { |
michael@0 | 34 | text = text + ": " + format_mode(left) + " === " + format_mode(right); |
michael@0 | 35 | do_report_result(left === right, text, Components.stack.caller, false); |
michael@0 | 36 | } |
michael@0 | 37 | |
michael@0 | 38 | const _umask = OS.Constants.Sys.umask; |
michael@0 | 39 | do_print("umask: " + format_mode(_umask)); |
michael@0 | 40 | |
michael@0 | 41 | /** |
michael@0 | 42 | * Compute the mode that a file should have after applying the umask, |
michael@0 | 43 | * whatever it happens to be. |
michael@0 | 44 | */ |
michael@0 | 45 | function apply_umask(mode) { |
michael@0 | 46 | return mode & ~_umask; |
michael@0 | 47 | } |
michael@0 | 48 | |
michael@0 | 49 | // Test application to paths. |
michael@0 | 50 | add_task(function*() { |
michael@0 | 51 | let path = OS.Path.join(OS.Constants.Path.tmpDir, |
michael@0 | 52 | "test_osfile_async_setPerms_nonproto.tmp"); |
michael@0 | 53 | yield OS.File.writeAtomic(path, new Uint8Array(1)); |
michael@0 | 54 | |
michael@0 | 55 | try { |
michael@0 | 56 | let stat; |
michael@0 | 57 | |
michael@0 | 58 | yield OS.File.setPermissions(path, {unixMode: 0o4777}); |
michael@0 | 59 | stat = yield OS.File.stat(path); |
michael@0 | 60 | do_check_modes_eq(stat.unixMode, 0o4777, |
michael@0 | 61 | "setPermissions(path, 04777)"); |
michael@0 | 62 | |
michael@0 | 63 | yield OS.File.setPermissions(path, {unixMode: 0o4777, |
michael@0 | 64 | unixHonorUmask: true}); |
michael@0 | 65 | stat = yield OS.File.stat(path); |
michael@0 | 66 | do_check_modes_eq(stat.unixMode, apply_umask(0o4777), |
michael@0 | 67 | "setPermissions(path, 04777&~umask)"); |
michael@0 | 68 | |
michael@0 | 69 | yield OS.File.setPermissions(path); |
michael@0 | 70 | stat = yield OS.File.stat(path); |
michael@0 | 71 | do_check_modes_eq(stat.unixMode, apply_umask(0o666), |
michael@0 | 72 | "setPermissions(path, {})"); |
michael@0 | 73 | |
michael@0 | 74 | yield OS.File.setPermissions(path, {unixMode: 0}); |
michael@0 | 75 | stat = yield OS.File.stat(path); |
michael@0 | 76 | do_check_modes_eq(stat.unixMode, 0, |
michael@0 | 77 | "setPermissions(path, 0000)"); |
michael@0 | 78 | |
michael@0 | 79 | } finally { |
michael@0 | 80 | yield OS.File.remove(path); |
michael@0 | 81 | } |
michael@0 | 82 | }); |
michael@0 | 83 | |
michael@0 | 84 | // Test application to open files. |
michael@0 | 85 | add_task(function*() { |
michael@0 | 86 | // First, create a file we can mess with. |
michael@0 | 87 | let path = OS.Path.join(OS.Constants.Path.tmpDir, |
michael@0 | 88 | "test_osfile_async_setDates_proto.tmp"); |
michael@0 | 89 | yield OS.File.writeAtomic(path, new Uint8Array(1)); |
michael@0 | 90 | |
michael@0 | 91 | try { |
michael@0 | 92 | let fd = yield OS.File.open(path, {write: true}); |
michael@0 | 93 | let stat; |
michael@0 | 94 | |
michael@0 | 95 | yield fd.setPermissions({unixMode: 0o4777}); |
michael@0 | 96 | stat = yield fd.stat(); |
michael@0 | 97 | do_check_modes_eq(stat.unixMode, 0o4777, |
michael@0 | 98 | "fd.setPermissions(04777)"); |
michael@0 | 99 | |
michael@0 | 100 | yield fd.setPermissions({unixMode: 0o4777, unixHonorUmask: true}); |
michael@0 | 101 | stat = yield fd.stat(); |
michael@0 | 102 | do_check_modes_eq(stat.unixMode, apply_umask(0o4777), |
michael@0 | 103 | "fd.setPermissions(04777&~umask)"); |
michael@0 | 104 | |
michael@0 | 105 | yield fd.setPermissions(); |
michael@0 | 106 | stat = yield fd.stat(); |
michael@0 | 107 | do_check_modes_eq(stat.unixMode, apply_umask(0o666), |
michael@0 | 108 | "fd.setPermissions({})"); |
michael@0 | 109 | |
michael@0 | 110 | yield fd.setPermissions({unixMode: 0}); |
michael@0 | 111 | stat = yield fd.stat(); |
michael@0 | 112 | do_check_modes_eq(stat.unixMode, 0, |
michael@0 | 113 | "fd.setPermissions(0000)"); |
michael@0 | 114 | |
michael@0 | 115 | yield fd.close(); |
michael@0 | 116 | } finally { |
michael@0 | 117 | yield OS.File.remove(path); |
michael@0 | 118 | } |
michael@0 | 119 | }); |
michael@0 | 120 | |
michael@0 | 121 | function run_test() { |
michael@0 | 122 | run_next_test(); |
michael@0 | 123 | } |