summaryrefslogtreecommitdiffstats
path: root/remote/cdp/test/browser/io/browser_read.js
blob: 1692e45e90178768fd95cfcefaa0a1b42cde4b6c (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
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

add_task(async function seekByOffsets({ client }) {
  const { IO } = client;
  const contents = "Lorem ipsum";
  const { handle } = await registerFileStream(contents);

  for (const offset of [0, 5, 10, 100, 0, -1]) {
    const result = await IO.read({ handle, offset });
    ok(result.base64Encoded, `Data for offset ${offset} is base64 encoded`);
    ok(result.eof, `All data has been read for offset ${offset}`);
    is(
      atob(result.data),
      contents.substring(offset >= 0 ? offset : 0),
      `Found expected data for offset ${offset}`
    );
  }
});

add_task(async function remembersOffsetAfterRead({ client }) {
  const { IO } = client;
  const contents = "Lorem ipsum";
  const { handle } = await registerFileStream(contents);

  let expectedOffset = 0;
  const size = 3;
  do {
    const result = await IO.read({ handle, size });
    is(
      atob(result.data),
      contents.substring(expectedOffset, expectedOffset + size),
      `Found expected data for expectedOffset ${expectedOffset}`
    );
    ok(
      result.base64Encoded,
      `Data up to expected offset ${expectedOffset} is base64 encoded`
    );

    is(
      result.eof,
      expectedOffset + size >= contents.length,
      `All data has been read up to expected offset ${expectedOffset}`
    );

    expectedOffset = Math.min(expectedOffset + size, contents.length);
  } while (expectedOffset < contents.length);
});

add_task(async function readBySize({ client }) {
  const { IO } = client;
  const contents = "Lorem ipsum";
  const { handle } = await registerFileStream(contents);

  for (const size of [0, 5, 10, 100, 0, -1]) {
    const result = await IO.read({ handle, offset: 0, size });
    ok(result.base64Encoded, `Data for size ${size} is base64 encoded`);
    is(
      result.eof,
      size >= contents.length,
      `All data has been read for size ${size}`
    );
    is(
      atob(result.data),
      contents.substring(0, size),
      `Found expected data for size ${size}`
    );
  }
});

add_task(async function readAfterClose({ client }) {
  const { IO } = client;
  const contents = "Lorem ipsum";
  const { handle } = await registerFileStream(contents);

  await IO.close({ handle });

  try {
    await IO.read({ handle });
    ok(false, "Read shouldn't pass");
  } catch (e) {
    ok(
      e.message.startsWith(`Invalid stream handle`),
      "Error contains expected message"
    );
  }
});

add_task(async function unknownHandle({ client }) {
  const { IO } = client;
  const handle = "1000000";

  try {
    await IO.read({ handle });
    ok(false, "Read shouldn't pass");
  } catch (e) {
    ok(
      e.message.startsWith(`Invalid stream handle`),
      "Error contains expected message"
    );
  }
});

add_task(async function invalidHandleTypes({ client }) {
  const { IO } = client;
  for (const handle of [null, true, 1, [], {}]) {
    try {
      await IO.read({ handle });
      ok(false, "Read shouldn't pass");
    } catch (e) {
      ok(
        e.message.startsWith(`handle: string value expected`),
        "Error contains expected message"
      );
    }
  }
});

add_task(async function invalidOffsetTypes({ client }) {
  const { IO } = client;
  const contents = "Lorem ipsum";
  const { handle } = await registerFileStream(contents);

  for (const offset of [null, true, "1", [], {}]) {
    try {
      await IO.read({ handle, offset });
      ok(false, "Read shouldn't pass");
    } catch (e) {
      ok(
        e.message.startsWith(`offset: integer value expected`),
        "Error contains expected message"
      );
    }
  }
});

add_task(async function invalidSizeTypes({ client }) {
  const { IO } = client;
  const contents = "Lorem ipsum";
  const { handle } = await registerFileStream(contents);

  for (const size of [null, true, "1", [], {}]) {
    try {
      await IO.read({ handle, size });
      ok(false, "Read shouldn't pass");
    } catch (e) {
      ok(
        e.message.startsWith(`size: integer value expected`),
        "Error contains expected message"
      );
    }
  }
});