• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2
3require('../common');
4const assert = require('assert');
5
6// Test OOB
7{
8  const buffer = Buffer.alloc(4);
9
10  ['UInt8', 'UInt16BE', 'UInt16LE', 'UInt32BE', 'UInt32LE'].forEach((fn) => {
11
12    // Verify that default offset works fine.
13    buffer[`read${fn}`](undefined);
14    buffer[`read${fn}`]();
15
16    ['', '0', null, {}, [], () => {}, true, false].forEach((o) => {
17      assert.throws(
18        () => buffer[`read${fn}`](o),
19        {
20          code: 'ERR_INVALID_ARG_TYPE',
21          name: 'TypeError'
22        });
23    });
24
25    [Infinity, -1, -4294967295].forEach((offset) => {
26      assert.throws(
27        () => buffer[`read${fn}`](offset),
28        {
29          code: 'ERR_OUT_OF_RANGE',
30          name: 'RangeError'
31        });
32    });
33
34    [NaN, 1.01].forEach((offset) => {
35      assert.throws(
36        () => buffer[`read${fn}`](offset),
37        {
38          code: 'ERR_OUT_OF_RANGE',
39          name: 'RangeError',
40          message: 'The value of "offset" is out of range. ' +
41                   `It must be an integer. Received ${offset}`
42        });
43    });
44  });
45}
46
47// Test 8 bit unsigned integers
48{
49  const data = Buffer.from([0xff, 0x2a, 0x2a, 0x2a]);
50  assert.strictEqual(data.readUInt8(0), 255);
51  assert.strictEqual(data.readUInt8(1), 42);
52  assert.strictEqual(data.readUInt8(2), 42);
53  assert.strictEqual(data.readUInt8(3), 42);
54}
55
56// Test 16 bit unsigned integers
57{
58  const data = Buffer.from([0x00, 0x2a, 0x42, 0x3f]);
59  assert.strictEqual(data.readUInt16BE(0), 0x2a);
60  assert.strictEqual(data.readUInt16BE(1), 0x2a42);
61  assert.strictEqual(data.readUInt16BE(2), 0x423f);
62  assert.strictEqual(data.readUInt16LE(0), 0x2a00);
63  assert.strictEqual(data.readUInt16LE(1), 0x422a);
64  assert.strictEqual(data.readUInt16LE(2), 0x3f42);
65
66  data[0] = 0xfe;
67  data[1] = 0xfe;
68  assert.strictEqual(data.readUInt16BE(0), 0xfefe);
69  assert.strictEqual(data.readUInt16LE(0), 0xfefe);
70}
71
72// Test 32 bit unsigned integers
73{
74  const data = Buffer.from([0x32, 0x65, 0x42, 0x56, 0x23, 0xff]);
75  assert.strictEqual(data.readUInt32BE(0), 0x32654256);
76  assert.strictEqual(data.readUInt32BE(1), 0x65425623);
77  assert.strictEqual(data.readUInt32BE(2), 0x425623ff);
78  assert.strictEqual(data.readUInt32LE(0), 0x56426532);
79  assert.strictEqual(data.readUInt32LE(1), 0x23564265);
80  assert.strictEqual(data.readUInt32LE(2), 0xff235642);
81}
82
83// Test UInt
84{
85  const buffer = Buffer.from([0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08]);
86
87  assert.strictEqual(buffer.readUIntLE(0, 1), 0x01);
88  assert.strictEqual(buffer.readUIntBE(0, 1), 0x01);
89  assert.strictEqual(buffer.readUIntLE(0, 3), 0x030201);
90  assert.strictEqual(buffer.readUIntBE(0, 3), 0x010203);
91  assert.strictEqual(buffer.readUIntLE(0, 5), 0x0504030201);
92  assert.strictEqual(buffer.readUIntBE(0, 5), 0x0102030405);
93  assert.strictEqual(buffer.readUIntLE(0, 6), 0x060504030201);
94  assert.strictEqual(buffer.readUIntBE(0, 6), 0x010203040506);
95  assert.strictEqual(buffer.readUIntLE(1, 6), 0x070605040302);
96  assert.strictEqual(buffer.readUIntBE(1, 6), 0x020304050607);
97  assert.strictEqual(buffer.readUIntLE(2, 6), 0x080706050403);
98  assert.strictEqual(buffer.readUIntBE(2, 6), 0x030405060708);
99
100  // Check byteLength.
101  ['readUIntBE', 'readUIntLE'].forEach((fn) => {
102    ['', '0', null, {}, [], () => {}, true, false, undefined].forEach((len) => {
103      assert.throws(
104        () => buffer[fn](0, len),
105        { code: 'ERR_INVALID_ARG_TYPE' });
106    });
107
108    [Infinity, -1].forEach((byteLength) => {
109      assert.throws(
110        () => buffer[fn](0, byteLength),
111        {
112          code: 'ERR_OUT_OF_RANGE',
113          message: 'The value of "byteLength" is out of range. ' +
114                   `It must be >= 1 and <= 6. Received ${byteLength}`
115        });
116    });
117
118    [NaN, 1.01].forEach((byteLength) => {
119      assert.throws(
120        () => buffer[fn](0, byteLength),
121        {
122          code: 'ERR_OUT_OF_RANGE',
123          name: 'RangeError',
124          message: 'The value of "byteLength" is out of range. ' +
125                   `It must be an integer. Received ${byteLength}`
126        });
127    });
128  });
129
130  // Test 1 to 6 bytes.
131  for (let i = 1; i <= 6; i++) {
132    ['readUIntBE', 'readUIntLE'].forEach((fn) => {
133      ['', '0', null, {}, [], () => {}, true, false, undefined].forEach((o) => {
134        assert.throws(
135          () => buffer[fn](o, i),
136          {
137            code: 'ERR_INVALID_ARG_TYPE',
138            name: 'TypeError'
139          });
140      });
141
142      [Infinity, -1, -4294967295].forEach((offset) => {
143        assert.throws(
144          () => buffer[fn](offset, i),
145          {
146            code: 'ERR_OUT_OF_RANGE',
147            name: 'RangeError',
148            message: 'The value of "offset" is out of range. ' +
149                     `It must be >= 0 and <= ${8 - i}. Received ${offset}`
150          });
151      });
152
153      [NaN, 1.01].forEach((offset) => {
154        assert.throws(
155          () => buffer[fn](offset, i),
156          {
157            code: 'ERR_OUT_OF_RANGE',
158            name: 'RangeError',
159            message: 'The value of "offset" is out of range. ' +
160                     `It must be an integer. Received ${offset}`
161          });
162      });
163    });
164  }
165}
166