1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
|
"use strict";
do_print("starting tests");
Components.utils.import("resource://gre/modules/osfile.jsm");
Components.utils.import("resource://gre/modules/Task.jsm");
/**
* A test to check that the |append| mode flag is correctly implemented.
* (see bug 925865)
*/
function setup_mode(mode) {
// Complete mode.
let realMode = {
read: true,
write: true
};
for (let k in mode) {
realMode[k] = mode[k];
}
return realMode;
}
// Test append mode.
function test_append(mode) {
let path = OS.Path.join(OS.Constants.Path.tmpDir,
"test_osfile_async_append.tmp");
// Clear any left-over files from previous runs.
try {
yield OS.File.remove(path);
} catch (ex if ex.becauseNoSuchFile) {
// ignore
}
try {
mode = setup_mode(mode);
mode.append = true;
if (mode.trunc) {
// Pre-fill file with some data to see if |trunc| actually works.
yield OS.File.writeAtomic(path, new Uint8Array(500));
}
let file = yield OS.File.open(path, mode);
try {
yield file.write(new Uint8Array(1000));
yield file.setPosition(0, OS.File.POS_START);
yield file.read(100);
// Should be at offset 100, length 1000 now.
yield file.write(new Uint8Array(100));
// Should be at offset 1100, length 1100 now.
let stat = yield file.stat();
do_check_eq(1100, stat.size);
} finally {
yield file.close();
}
} catch(ex) {
try {
yield OS.File.remove(path);
} catch (ex if ex.becauseNoSuchFile) {
// ignore.
}
}
}
// Test no-append mode.
function test_no_append(mode) {
let path = OS.Path.join(OS.Constants.Path.tmpDir,
"test_osfile_async_noappend.tmp");
// Clear any left-over files from previous runs.
try {
yield OS.File.remove(path);
} catch (ex if ex.becauseNoSuchFile) {
// ignore
}
try {
mode = setup_mode(mode);
mode.append = false;
if (mode.trunc) {
// Pre-fill file with some data to see if |trunc| actually works.
yield OS.File.writeAtomic(path, new Uint8Array(500));
}
let file = yield OS.File.open(path, mode);
try {
yield file.write(new Uint8Array(1000));
yield file.setPosition(0, OS.File.POS_START);
yield file.read(100);
// Should be at offset 100, length 1000 now.
yield file.write(new Uint8Array(100));
// Should be at offset 200, length 1000 now.
let stat = yield file.stat();
do_check_eq(1000, stat.size);
} finally {
yield file.close();
}
} finally {
try {
yield OS.File.remove(path);
} catch (ex if ex.becauseNoSuchFile) {
// ignore.
}
}
}
var test_flags = [
{},
{create:true},
{trunc:true}
];
function run_test() {
do_test_pending();
for (let t of test_flags) {
add_task(test_append.bind(null, t));
add_task(test_no_append.bind(null, t));
}
add_task(do_test_finished);
run_next_test();
}
|