michael@0: "use strict"; michael@0: michael@0: do_print("starting tests"); michael@0: michael@0: Components.utils.import("resource://gre/modules/osfile.jsm"); michael@0: Components.utils.import("resource://gre/modules/Task.jsm"); michael@0: michael@0: /** michael@0: * A test to check that the |append| mode flag is correctly implemented. michael@0: * (see bug 925865) michael@0: */ michael@0: michael@0: function setup_mode(mode) { michael@0: // Complete mode. michael@0: let realMode = { michael@0: read: true, michael@0: write: true michael@0: }; michael@0: for (let k in mode) { michael@0: realMode[k] = mode[k]; michael@0: } michael@0: return realMode; michael@0: } michael@0: michael@0: // Test append mode. michael@0: function test_append(mode) { michael@0: let path = OS.Path.join(OS.Constants.Path.tmpDir, michael@0: "test_osfile_async_append.tmp"); michael@0: michael@0: // Clear any left-over files from previous runs. michael@0: try { michael@0: yield OS.File.remove(path); michael@0: } catch (ex if ex.becauseNoSuchFile) { michael@0: // ignore michael@0: } michael@0: michael@0: try { michael@0: mode = setup_mode(mode); michael@0: mode.append = true; michael@0: if (mode.trunc) { michael@0: // Pre-fill file with some data to see if |trunc| actually works. michael@0: yield OS.File.writeAtomic(path, new Uint8Array(500)); michael@0: } michael@0: let file = yield OS.File.open(path, mode); michael@0: try { michael@0: yield file.write(new Uint8Array(1000)); michael@0: yield file.setPosition(0, OS.File.POS_START); michael@0: yield file.read(100); michael@0: // Should be at offset 100, length 1000 now. michael@0: yield file.write(new Uint8Array(100)); michael@0: // Should be at offset 1100, length 1100 now. michael@0: let stat = yield file.stat(); michael@0: do_check_eq(1100, stat.size); michael@0: } finally { michael@0: yield file.close(); michael@0: } michael@0: } catch(ex) { michael@0: try { michael@0: yield OS.File.remove(path); michael@0: } catch (ex if ex.becauseNoSuchFile) { michael@0: // ignore. michael@0: } michael@0: } michael@0: } michael@0: michael@0: // Test no-append mode. michael@0: function test_no_append(mode) { michael@0: let path = OS.Path.join(OS.Constants.Path.tmpDir, michael@0: "test_osfile_async_noappend.tmp"); michael@0: michael@0: // Clear any left-over files from previous runs. michael@0: try { michael@0: yield OS.File.remove(path); michael@0: } catch (ex if ex.becauseNoSuchFile) { michael@0: // ignore michael@0: } michael@0: michael@0: try { michael@0: mode = setup_mode(mode); michael@0: mode.append = false; michael@0: if (mode.trunc) { michael@0: // Pre-fill file with some data to see if |trunc| actually works. michael@0: yield OS.File.writeAtomic(path, new Uint8Array(500)); michael@0: } michael@0: let file = yield OS.File.open(path, mode); michael@0: try { michael@0: yield file.write(new Uint8Array(1000)); michael@0: yield file.setPosition(0, OS.File.POS_START); michael@0: yield file.read(100); michael@0: // Should be at offset 100, length 1000 now. michael@0: yield file.write(new Uint8Array(100)); michael@0: // Should be at offset 200, length 1000 now. michael@0: let stat = yield file.stat(); michael@0: do_check_eq(1000, stat.size); michael@0: } finally { michael@0: yield file.close(); michael@0: } michael@0: } finally { michael@0: try { michael@0: yield OS.File.remove(path); michael@0: } catch (ex if ex.becauseNoSuchFile) { michael@0: // ignore. michael@0: } michael@0: } michael@0: } michael@0: michael@0: let test_flags = [ michael@0: {}, michael@0: {create:true}, michael@0: {trunc:true} michael@0: ]; michael@0: function run_test() { michael@0: do_test_pending(); michael@0: michael@0: for (let t of test_flags) { michael@0: add_task(test_append.bind(null, t)); michael@0: add_task(test_no_append.bind(null, t)); michael@0: } michael@0: add_task(do_test_finished); michael@0: michael@0: run_next_test(); michael@0: }