summaryrefslogtreecommitdiffstats
path: root/testing/xpcshell/node-ws/test/validation.test.js
blob: 5718b12f02d990fec656bc62c46ea2fd32bc03b7 (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
'use strict';

const assert = require('assert');

const { isValidUTF8 } = require('../lib/validation');

describe('extension', () => {
  describe('isValidUTF8', () => {
    it('returns false if it finds invalid bytes', () => {
      assert.strictEqual(isValidUTF8(Buffer.from([0xf8])), false);
    });

    it('returns false for overlong encodings', () => {
      assert.strictEqual(isValidUTF8(Buffer.from([0xc0, 0xa0])), false);
      assert.strictEqual(isValidUTF8(Buffer.from([0xe0, 0x80, 0xa0])), false);
      assert.strictEqual(
        isValidUTF8(Buffer.from([0xf0, 0x80, 0x80, 0xa0])),
        false
      );
    });

    it('returns false for code points in the range U+D800 - U+DFFF', () => {
      for (let i = 0xa0; i < 0xc0; i++) {
        for (let j = 0x80; j < 0xc0; j++) {
          assert.strictEqual(isValidUTF8(Buffer.from([0xed, i, j])), false);
        }
      }
    });

    it('returns false for code points greater than U+10FFFF', () => {
      assert.strictEqual(
        isValidUTF8(Buffer.from([0xf4, 0x90, 0x80, 0x80])),
        false
      );
      assert.strictEqual(
        isValidUTF8(Buffer.from([0xf5, 0x80, 0x80, 0x80])),
        false
      );
    });

    it('returns true for a well-formed UTF-8 byte sequence', () => {
      // prettier-ignore
      const buf = Buffer.from([
        0xe2, 0x82, 0xAC, // €
        0xf0, 0x90, 0x8c, 0x88, // 𐍈
        0x24 // $
      ]);

      assert.strictEqual(isValidUTF8(buf), true);
    });
  });
});