summaryrefslogtreecommitdiff
path: root/tests/node_compat/test/parallel/test-buffer-isutf8.js
blob: 02f142db893b57e2d69fee00c262c13f39890219 (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
// deno-fmt-ignore-file
// deno-lint-ignore-file

// Copyright Joyent and Node contributors. All rights reserved. MIT license.
// Taken from Node 18.12.1
// This file is automatically generated by `tests/node_compat/runner/setup.ts`. Do not modify this file manually.

'use strict';

require('../common');
const assert = require('assert');
const { isUtf8, Buffer } = require('buffer');
const { TextEncoder } = require('util');

const encoder = new TextEncoder();

assert.strictEqual(isUtf8(encoder.encode('hello')), true);
assert.strictEqual(isUtf8(encoder.encode('ğ')), true);
assert.strictEqual(isUtf8(Buffer.from([])), true);

// Taken from test/fixtures/wpt/encoding/textdecoder-fatal.any.js
[
  [0xFF], // 'invalid code'
  [0xC0], // 'ends early'
  [0xE0], // 'ends early 2'
  [0xC0, 0x00], // 'invalid trail'
  [0xC0, 0xC0], // 'invalid trail 2'
  [0xE0, 0x00], // 'invalid trail 3'
  [0xE0, 0xC0], // 'invalid trail 4'
  [0xE0, 0x80, 0x00], // 'invalid trail 5'
  [0xE0, 0x80, 0xC0], // 'invalid trail 6'
  [0xFC, 0x80, 0x80, 0x80, 0x80, 0x80], // '> 0x10FFFF'
  [0xFE, 0x80, 0x80, 0x80, 0x80, 0x80], // 'obsolete lead byte'

  // Overlong encodings
  [0xC0, 0x80], // 'overlong U+0000 - 2 bytes'
  [0xE0, 0x80, 0x80], // 'overlong U+0000 - 3 bytes'
  [0xF0, 0x80, 0x80, 0x80], // 'overlong U+0000 - 4 bytes'
  [0xF8, 0x80, 0x80, 0x80, 0x80], // 'overlong U+0000 - 5 bytes'
  [0xFC, 0x80, 0x80, 0x80, 0x80, 0x80], // 'overlong U+0000 - 6 bytes'

  [0xC1, 0xBF], // 'overlong U+007F - 2 bytes'
  [0xE0, 0x81, 0xBF], // 'overlong U+007F - 3 bytes'
  [0xF0, 0x80, 0x81, 0xBF], // 'overlong U+007F - 4 bytes'
  [0xF8, 0x80, 0x80, 0x81, 0xBF], // 'overlong U+007F - 5 bytes'
  [0xFC, 0x80, 0x80, 0x80, 0x81, 0xBF], // 'overlong U+007F - 6 bytes'

  [0xE0, 0x9F, 0xBF], // 'overlong U+07FF - 3 bytes'
  [0xF0, 0x80, 0x9F, 0xBF], // 'overlong U+07FF - 4 bytes'
  [0xF8, 0x80, 0x80, 0x9F, 0xBF], // 'overlong U+07FF - 5 bytes'
  [0xFC, 0x80, 0x80, 0x80, 0x9F, 0xBF], // 'overlong U+07FF - 6 bytes'

  [0xF0, 0x8F, 0xBF, 0xBF], // 'overlong U+FFFF - 4 bytes'
  [0xF8, 0x80, 0x8F, 0xBF, 0xBF], // 'overlong U+FFFF - 5 bytes'
  [0xFC, 0x80, 0x80, 0x8F, 0xBF, 0xBF], // 'overlong U+FFFF - 6 bytes'

  [0xF8, 0x84, 0x8F, 0xBF, 0xBF], // 'overlong U+10FFFF - 5 bytes'
  [0xFC, 0x80, 0x84, 0x8F, 0xBF, 0xBF], // 'overlong U+10FFFF - 6 bytes'

  // UTF-16 surrogates encoded as code points in UTF-8
  [0xED, 0xA0, 0x80], // 'lead surrogate'
  [0xED, 0xB0, 0x80], // 'trail surrogate'
  [0xED, 0xA0, 0x80, 0xED, 0xB0, 0x80], // 'surrogate pair'
].forEach((input) => {
  assert.strictEqual(isUtf8(Buffer.from(input)), false);
});

[
  null,
  undefined,
  'hello',
  true,
  false,
].forEach((input) => {
  assert.throws(
    () => { isUtf8(input); },
    {
      code: 'ERR_INVALID_ARG_TYPE',
    },
  );
});

{
  // Test with detached array buffers
  const arrayBuffer = new ArrayBuffer(1024);
  structuredClone(arrayBuffer, { transfer: [arrayBuffer] });
  assert.throws(
    () => { isUtf8(arrayBuffer); },
    {
      code: 'ERR_INVALID_STATE'
    }
  );
}