summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/fs/FileSystemSyncAccessHandle-read-write.https.worker.js
blob: 1c8fda94bbeb09bec8f395364bcea3a6343c3a3d (plain)
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
importScripts("/resources/testharness.js");
importScripts('resources/sync-access-handle-test.js');

'use strict';

sync_access_handle_test((t, handle) => {
  const readBuffer = new Uint8Array(24);
  const readBytes = handle.read(readBuffer, {at: 0});
  assert_equals(0, readBytes, 'Check that no bytes were read');
}, 'Test reading an empty file through a sync access handle.');

sync_access_handle_test((t, handle) => {
  const readBuffer = new ArrayBuffer(0);
  const readBytes = handle.read(readBuffer, {at: 0});
  assert_equals(0, readBytes, 'Check that no bytes were read');
}, 'Test using an empty ArrayBuffer.');

sync_access_handle_test((t, handle) => {
  const readBuffer = new ArrayBuffer(24);
  const readBytes = handle.read(readBuffer, {at: 0});
  assert_equals(0, readBytes, 'Check that no bytes were read');
}, 'Test using an ArrayBuffer.');

sync_access_handle_test((t, handle) => {
  if (!('TextEncoder' in self)) {
    return;
  }

  const decoder = new TextDecoder();

  const text = 'Hello Storage Foundation';
  const writeBuffer = new TextEncoder().encode(text);
  const writtenBytes = handle.write(writeBuffer, {at: 0});
  assert_equals(
      writeBuffer.byteLength, writtenBytes,
      'Check that all bytes were written.');
  let readBuffer = new Uint8Array(writtenBytes);
  let readBytes = handle.read(readBuffer, {at: 0});
  assert_equals(writtenBytes, readBytes, 'Check that all bytes were read');
  assert_equals(
      text, decoder.decode(readBuffer),
      'Check that the written bytes and the read bytes match');

  // Test a read of less bytes than available.
  const expected = 'Storage';
  readBuffer = new Uint8Array(expected.length);
  readBytes = handle.read(readBuffer, {at: text.indexOf(expected)});
  assert_equals(readBuffer.length, readBytes, 'Check that all bytes were read');
  const actual = decoder.decode(readBuffer);
  assert_equals(
      expected, actual,
      'Partial read returned unexpected contents');
}, 'Test writing and reading through a sync access handle.');

sync_access_handle_test((t, handle) => {
  if (!('TextEncoder' in self)) {
    return;
  }

  const encoder = new TextEncoder();
  const decoder = new TextDecoder();

  for (text of ['Hello', 'Longer Text']) {
    const writeBuffer = encoder.encode(text);
    const writtenBytes = handle.write(writeBuffer, {at: 0});
    assert_equals(
        writeBuffer.byteLength, writtenBytes,
        'Check that all bytes were written.');
    const readBuffer = new Uint8Array(writtenBytes);
    const readBytes = handle.read(readBuffer, {at: 0});
    assert_equals(writtenBytes, readBytes, 'Check that all bytes were read');
    assert_equals(
        text, decoder.decode(readBuffer),
        'Check that the written bytes and the read bytes match');
  }
}, 'Test second write that is bigger than the first write');

sync_access_handle_test((t, handle) => {
  if (!('TextEncoder' in self)) {
    return;
  }

  const encoder = new TextEncoder();
  const decoder = new TextDecoder();

  for (tuple
           of [{input: 'Hello World', expected: 'Hello World'},
               {input: 'foobar', expected: 'foobarWorld'}]) {
    const text = tuple.input;
    const expected = tuple.expected;
    const writeBuffer = encoder.encode(text);
    const writtenBytes = handle.write(writeBuffer, {at: 0});
    assert_equals(
        writeBuffer.byteLength, writtenBytes,
        'Check that all bytes were written.');
    const readBuffer = new Uint8Array(expected.length);
    const readBytes = handle.read(readBuffer, {at: 0});
    assert_equals(expected.length, readBytes, 'Check that all bytes were read');
    assert_equals(
        expected, decoder.decode(readBuffer),
        'Check that the written bytes and the read bytes match');
  }
}, 'Test second write that is smaller than the first write');

sync_access_handle_test((t, handle) => {
  const expected = 17;
  const writeBuffer = new Uint8Array(1);
  writeBuffer[0] = expected;
  const offset = 5;
  const writtenBytes = handle.write(writeBuffer, {at: offset});
  assert_equals(
      writeBuffer.byteLength, writtenBytes,
      'Check that all bytes were written.');
  const fileLength = writeBuffer.byteLength + offset;
  const readBuffer = new Uint8Array(fileLength);
  const readBytes = handle.read(readBuffer, {at: 0});
  assert_equals(fileLength, readBytes, 'Check that all bytes were read');
  for (let i = 0; i < offset; ++i) {
    assert_equals(
        readBuffer[i], 0,
        `Gaps in the file should be filled with 0, but got ${readBuffer[i]}.`);
  }

  assert_equals(
      readBuffer[offset], expected,
      'Gaps in the file should be filled with 0.');
}, 'Test initial write with an offset');

sync_access_handle_test((t, handle) => {
  if (!('TextEncoder' in self)) {
    return;
  }

  const encoder = new TextEncoder();
  const decoder = new TextDecoder();

  for (tuple
           of [{input: 'Hello World', expected: 'Hello World', offset: 0},
               {input: 'foobar', expected: 'Hello foobar', offset: 6}]) {
    const text = tuple.input;
    const expected = tuple.expected;
    const offset = tuple.offset;
    const writeBuffer = encoder.encode(text);
    const writtenBytes = handle.write(writeBuffer, {at: offset});
    assert_equals(
        writeBuffer.byteLength, writtenBytes,
        'Check that all bytes were written.');
    const readBuffer = new Uint8Array(expected.length);
    const readBytes = handle.read(readBuffer, {at: 0});
    assert_equals(expected.length, readBytes, 'Check that all bytes were read');
    const actual = decoder.decode(readBuffer);
    assert_equals(
        expected, actual,
        'Check content read from the handle');
  }
}, 'Test overwriting the file at an offset');

sync_access_handle_test((t, handle) => {
  if (!('TextEncoder' in self)) {
    return;
  }

  const decoder = new TextDecoder();

  const text = 'Hello Storage Foundation';
  const writeBuffer = new TextEncoder().encode(text);
  const writtenBytes = handle.write(writeBuffer, {at: 0});
  assert_equals(
      writeBuffer.byteLength, writtenBytes,
      'Check that all bytes were written.');
  const bufferLength = text.length;
  for (tuple
           of [{offset: 0, expected: text},
               {offset: 6, expected: text.substring(6)}]) {
    const offset = tuple.offset;
    const expected = tuple.expected;

    const readBuffer = new Uint8Array(bufferLength);
    const readBytes = handle.read(readBuffer, {at: offset});
    assert_equals(expected.length, readBytes, 'Check that all bytes were read');
    const actual = decoder.decode(readBuffer);
    assert_true(
        actual.startsWith(expected),
        `Expected to read ${expected} but the actual value was ${actual}.`);
  }

  const readBuffer = new Uint8Array(bufferLength);
  // Offset is greater than the file length.
  const readBytes = handle.read(readBuffer, {at: bufferLength + 1});
  assert_equals(0, readBytes, 'Check that no bytes were read');
  for (let i = 0; i < readBuffer.byteLength; ++i) {
    assert_equals(0, readBuffer[i], 'Check that the read buffer is unchanged.');
  }
}, 'Test read at an offset');

sync_access_handle_test((t, handle) => {
  if (!('TextEncoder' in self)) {
    return;
  }

  const expected = 'Hello Storage Foundation';
  const writeBuffer = new TextEncoder().encode(expected);
  const writtenBytes = handle.write(writeBuffer, {at: 0});
  assert_equals(
      writeBuffer.byteLength, writtenBytes,
      'Check that all bytes were written.');

  const bufferLength = expected.length;
  const readBuffer = new Uint8Array(expected.length);
  // No options parameter provided, should read at offset 0.
  const readBytes = handle.read(readBuffer, {at: 0});
  assert_equals(expected.length, readBytes, 'Check that all bytes were read');
  const actual = new TextDecoder().decode(readBuffer);
  assert_equals(
      expected, actual,
      `Expected to read ${expected} but the actual value was ${actual}.`);
}, 'Test read with default options');

sync_access_handle_test((t, handle) => {
  if (!('TextEncoder' in self)) {
    return;
  }

  const expected = 'Hello Storage Foundation';
  const writeBuffer = new TextEncoder().encode(expected);
  // No options parameter provided, should write at offset 0.
  const writtenBytes = handle.write(writeBuffer);
  assert_equals(
      writeBuffer.byteLength, writtenBytes,
      'Check that all bytes were written.');

  const bufferLength = expected.length;
  const readBuffer = new Uint8Array(expected.length);
  const readBytes = handle.read(readBuffer, {at: 0});
  assert_equals(expected.length, readBytes, 'Check that all bytes were read');
  const actual = new TextDecoder().decode(readBuffer);
  assert_equals(
      expected, actual,
      `Expected to read ${expected} but the actual value was ${actual}.`);
}, 'Test write with default options');

sync_access_handle_test((t, handle) => {
  const readBuffer = new Uint8Array(24);
  assert_throws_js(TypeError, () => handle.read(readBuffer, {at: -1}));
}, 'Test reading at a negative offset fails.');

sync_access_handle_test((t, handle) => {
  const text = 'foobar';
  const writeBuffer = new TextEncoder().encode(text);
  assert_throws_js(TypeError, () => handle.write(writeBuffer, {at: -1}));

  const readBuffer = new Uint8Array(24);
  const readBytes = handle.read(readBuffer, {at: 0});

  assert_equals(0, readBytes, 'Check that no bytes were written');
}, 'Test writing at a negative offset fails.');

sync_access_handle_test((t, handle) => {
  if (!('TextEncoder' in self)) {
    return;
  }

  const encoder = new TextEncoder();
  const decoder = new TextDecoder();

  let writeBuffer = encoder.encode("Hello ");
  let writtenBytes = handle.write(writeBuffer);
  writeBuffer = encoder.encode("World");
  writtenBytes += handle.write(writeBuffer);
  let readBuffer = new Uint8Array(256);
  let readBytes = handle.read(readBuffer, {at: 0});
  assert_equals(readBytes, "Hello World".length, 'Check that all bytes were read');
  let actual = decoder.decode(readBuffer).substring(0, readBytes);
  assert_equals(
    actual, "Hello World",
    'Check content read from the handle');

  readBuffer = new Uint8Array(5);
  readBytes = handle.read(readBuffer, {at: 0});
  assert_equals(readBytes, 5, 'Check that all bytes were read');
  actual = decoder.decode(readBuffer).substring(0, readBytes);
  assert_equals(
    actual, "Hello",
    'Check content read from the handle');

  readBuffer = new Uint8Array(256);
  readBytes = handle.read(readBuffer);
  assert_equals(readBytes, "Hello World".length - 5, 'Check that all bytes were read');
  actual = decoder.decode(readBuffer).substring(0, readBytes);
  assert_equals(
    actual, " World",
    'Check content read from the handle');

  readBuffer = new Uint8Array(5);
  readBytes = handle.read(readBuffer, {at: 0});
  assert_equals(readBytes, 5, 'Check that all bytes were read');
  actual = decoder.decode(readBuffer);
  assert_equals(
    actual, "Hello",
    'Check content read from the handle');
  writeBuffer = encoder.encode(" X");
  writtenBytes = handle.write(writeBuffer);
  assert_equals(writtenBytes, 2, 'Check overwrite length');

  readBuffer = new Uint8Array(256);
  readBytes = handle.read(readBuffer, {at: 0});
  assert_equals(readBytes, "Hello Xorld".length, 'Check that all bytes were read');
  actual = decoder.decode(readBuffer).substring(0, readBytes);
  assert_equals(
    actual, "Hello Xorld",
    'Check content read from the handle');
}, 'Test reading and writing a file using the cursor');

done();