1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/components/osfile/tests/xpcshell/test_osfile_async_append.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,122 @@ 1.4 +"use strict"; 1.5 + 1.6 +do_print("starting tests"); 1.7 + 1.8 +Components.utils.import("resource://gre/modules/osfile.jsm"); 1.9 +Components.utils.import("resource://gre/modules/Task.jsm"); 1.10 + 1.11 +/** 1.12 + * A test to check that the |append| mode flag is correctly implemented. 1.13 + * (see bug 925865) 1.14 + */ 1.15 + 1.16 +function setup_mode(mode) { 1.17 + // Complete mode. 1.18 + let realMode = { 1.19 + read: true, 1.20 + write: true 1.21 + }; 1.22 + for (let k in mode) { 1.23 + realMode[k] = mode[k]; 1.24 + } 1.25 + return realMode; 1.26 +} 1.27 + 1.28 +// Test append mode. 1.29 +function test_append(mode) { 1.30 + let path = OS.Path.join(OS.Constants.Path.tmpDir, 1.31 + "test_osfile_async_append.tmp"); 1.32 + 1.33 + // Clear any left-over files from previous runs. 1.34 + try { 1.35 + yield OS.File.remove(path); 1.36 + } catch (ex if ex.becauseNoSuchFile) { 1.37 + // ignore 1.38 + } 1.39 + 1.40 + try { 1.41 + mode = setup_mode(mode); 1.42 + mode.append = true; 1.43 + if (mode.trunc) { 1.44 + // Pre-fill file with some data to see if |trunc| actually works. 1.45 + yield OS.File.writeAtomic(path, new Uint8Array(500)); 1.46 + } 1.47 + let file = yield OS.File.open(path, mode); 1.48 + try { 1.49 + yield file.write(new Uint8Array(1000)); 1.50 + yield file.setPosition(0, OS.File.POS_START); 1.51 + yield file.read(100); 1.52 + // Should be at offset 100, length 1000 now. 1.53 + yield file.write(new Uint8Array(100)); 1.54 + // Should be at offset 1100, length 1100 now. 1.55 + let stat = yield file.stat(); 1.56 + do_check_eq(1100, stat.size); 1.57 + } finally { 1.58 + yield file.close(); 1.59 + } 1.60 + } catch(ex) { 1.61 + try { 1.62 + yield OS.File.remove(path); 1.63 + } catch (ex if ex.becauseNoSuchFile) { 1.64 + // ignore. 1.65 + } 1.66 + } 1.67 +} 1.68 + 1.69 +// Test no-append mode. 1.70 +function test_no_append(mode) { 1.71 + let path = OS.Path.join(OS.Constants.Path.tmpDir, 1.72 + "test_osfile_async_noappend.tmp"); 1.73 + 1.74 + // Clear any left-over files from previous runs. 1.75 + try { 1.76 + yield OS.File.remove(path); 1.77 + } catch (ex if ex.becauseNoSuchFile) { 1.78 + // ignore 1.79 + } 1.80 + 1.81 + try { 1.82 + mode = setup_mode(mode); 1.83 + mode.append = false; 1.84 + if (mode.trunc) { 1.85 + // Pre-fill file with some data to see if |trunc| actually works. 1.86 + yield OS.File.writeAtomic(path, new Uint8Array(500)); 1.87 + } 1.88 + let file = yield OS.File.open(path, mode); 1.89 + try { 1.90 + yield file.write(new Uint8Array(1000)); 1.91 + yield file.setPosition(0, OS.File.POS_START); 1.92 + yield file.read(100); 1.93 + // Should be at offset 100, length 1000 now. 1.94 + yield file.write(new Uint8Array(100)); 1.95 + // Should be at offset 200, length 1000 now. 1.96 + let stat = yield file.stat(); 1.97 + do_check_eq(1000, stat.size); 1.98 + } finally { 1.99 + yield file.close(); 1.100 + } 1.101 + } finally { 1.102 + try { 1.103 + yield OS.File.remove(path); 1.104 + } catch (ex if ex.becauseNoSuchFile) { 1.105 + // ignore. 1.106 + } 1.107 + } 1.108 +} 1.109 + 1.110 +let test_flags = [ 1.111 + {}, 1.112 + {create:true}, 1.113 + {trunc:true} 1.114 +]; 1.115 +function run_test() { 1.116 + do_test_pending(); 1.117 + 1.118 + for (let t of test_flags) { 1.119 + add_task(test_append.bind(null, t)); 1.120 + add_task(test_no_append.bind(null, t)); 1.121 + } 1.122 + add_task(do_test_finished); 1.123 + 1.124 + run_next_test(); 1.125 +}