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