deno.land / std@0.166.0 / node / _tools / test / parallel / test-buffer-write.js

test-buffer-write.js
نووسراو ببینە
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
// deno-fmt-ignore-file// deno-lint-ignore-file
// Copyright Joyent and Node contributors. All rights reserved. MIT license.// Taken from Node 18.12.0// This file is automatically generated by "node/_tools/setup.ts". Do not modify this file manually
'use strict';
const common = require('../common');const assert = require('assert');
[-1, 10].forEach((offset) => { assert.throws( () => Buffer.alloc(9).write('foo', offset), { code: 'ERR_OUT_OF_RANGE', name: 'RangeError', message: 'The value of "offset" is out of range. ' + `It must be >= 0 && <= 9. Received ${offset}` } );});
const resultMap = new Map([ ['utf8', Buffer.from([102, 111, 111, 0, 0, 0, 0, 0, 0])], ['ucs2', Buffer.from([102, 0, 111, 0, 111, 0, 0, 0, 0])], ['ascii', Buffer.from([102, 111, 111, 0, 0, 0, 0, 0, 0])], ['latin1', Buffer.from([102, 111, 111, 0, 0, 0, 0, 0, 0])], ['binary', Buffer.from([102, 111, 111, 0, 0, 0, 0, 0, 0])], ['utf16le', Buffer.from([102, 0, 111, 0, 111, 0, 0, 0, 0])], ['base64', Buffer.from([102, 111, 111, 0, 0, 0, 0, 0, 0])], ['base64url', Buffer.from([102, 111, 111, 0, 0, 0, 0, 0, 0])], ['hex', Buffer.from([102, 111, 111, 0, 0, 0, 0, 0, 0])],]);
// utf8, ucs2, ascii, latin1, utf16leconst encodings = ['utf8', 'utf-8', 'ucs2', 'ucs-2', 'ascii', 'latin1', 'binary', 'utf16le', 'utf-16le'];
encodings .reduce((es, e) => es.concat(e, e.toUpperCase()), []) .forEach((encoding) => { const buf = Buffer.alloc(9); const len = Buffer.byteLength('foo', encoding); assert.strictEqual(buf.write('foo', 0, len, encoding), len);
if (encoding.includes('-')) encoding = encoding.replace('-', '');
assert.deepStrictEqual(buf, resultMap.get(encoding.toLowerCase())); });
// base64['base64', 'BASE64', 'base64url', 'BASE64URL'].forEach((encoding) => { const buf = Buffer.alloc(9); const len = Buffer.byteLength('Zm9v', encoding);
assert.strictEqual(buf.write('Zm9v', 0, len, encoding), len); assert.deepStrictEqual(buf, resultMap.get(encoding.toLowerCase()));});
// hex['hex', 'HEX'].forEach((encoding) => { const buf = Buffer.alloc(9); const len = Buffer.byteLength('666f6f', encoding);
assert.strictEqual(buf.write('666f6f', 0, len, encoding), len); assert.deepStrictEqual(buf, resultMap.get(encoding.toLowerCase()));});
// Invalid encodingsfor (let i = 1; i < 10; i++) { const encoding = String(i).repeat(i); const error = common.expectsError({ code: 'ERR_UNKNOWN_ENCODING', name: 'TypeError', message: `Unknown encoding: ${encoding}` });
assert.ok(!Buffer.isEncoding(encoding)); assert.throws(() => Buffer.alloc(9).write('foo', encoding), error);}
// UCS-2 overflow CVE-2018-12115for (let i = 1; i < 4; i++) { // Allocate two Buffers sequentially off the pool. Run more than once in case // we hit the end of the pool and don't get sequential allocations const x = Buffer.allocUnsafe(4).fill(0); const y = Buffer.allocUnsafe(4).fill(1); // Should not write anything, pos 3 doesn't have enough room for a 16-bit char assert.strictEqual(x.write('ыыыыыы', 3, 'ucs2'), 0); // CVE-2018-12115 experienced via buffer overrun to next block in the pool assert.strictEqual(Buffer.compare(y, Buffer.alloc(4, 1)), 0);}
// Should not write any data when there is no space for 16-bit charsconst z = Buffer.alloc(4, 0);assert.strictEqual(z.write('\u0001', 3, 'ucs2'), 0);assert.strictEqual(Buffer.compare(z, Buffer.alloc(4, 0)), 0);// Make sure longer strings are written up to the buffer end.assert.strictEqual(z.write('abcd', 2), 2);assert.deepStrictEqual([...z], [0, 0, 0x61, 0x62]);
// Large overrun could corrupt the processassert.strictEqual(Buffer.alloc(4) .write('ыыыыыы'.repeat(100), 3, 'utf16le'), 0);
{ // .write() does not affect the byte after the written-to slice of the Buffer. // Refs: https://github.com/nodejs/node/issues/26422 const buf = Buffer.alloc(8); assert.strictEqual(buf.write('ыы', 1, 'utf16le'), 4); assert.deepStrictEqual([...buf], [0, 0x4b, 0x04, 0x4b, 0x04, 0, 0, 0]);}
std

Version Info

Tagged at
a year ago