test_safeoutputstream_append.js (1286B)
1 /* atomic-file-output-stream and safe-file-output-stream should throw and 2 * exception if PR_APPEND is explicity specified without PR_TRUNCATE. */ 3 "use strict"; 4 5 const PR_WRONLY = 0x02; 6 const PR_CREATE_FILE = 0x08; 7 const PR_APPEND = 0x10; 8 const PR_TRUNCATE = 0x20; 9 10 function check_flag(file, contractID, flags, throws) { 11 let stream = Cc[contractID].createInstance(Ci.nsIFileOutputStream); 12 13 if (throws) { 14 /* NS_ERROR_INVALID_ARG is reported as NS_ERROR_ILLEGAL_VALUE, since they 15 * are same value. */ 16 Assert.throws( 17 () => stream.init(file, flags, 0o644, 0), 18 /NS_ERROR_ILLEGAL_VALUE/ 19 ); 20 } else { 21 stream.init(file, flags, 0o644, 0); 22 stream.close(); 23 } 24 } 25 26 function run_test() { 27 let filename = "test.txt"; 28 let file = Services.dirsvc.get("TmpD", Ci.nsIFile); 29 file.append(filename); 30 31 let tests = [ 32 [PR_WRONLY | PR_CREATE_FILE | PR_APPEND | PR_TRUNCATE, false], 33 [PR_WRONLY | PR_CREATE_FILE | PR_TRUNCATE, false], 34 [PR_WRONLY | PR_CREATE_FILE | PR_APPEND, true], 35 [-1, false], 36 ]; 37 for (let contractID of [ 38 "@mozilla.org/network/atomic-file-output-stream;1", 39 "@mozilla.org/network/safe-file-output-stream;1", 40 ]) { 41 for (let [flags, throws] of tests) { 42 check_flag(file, contractID, flags, throws); 43 } 44 } 45 }