• 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  ['Int8', 'Int16BE', 'Int16LE', 'Int32BE', 'Int32LE'].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 signed integers
48{
49  const data = Buffer.from([0x23, 0xab, 0x7c, 0xef]);
50
51  assert.strictEqual(data.readInt8(0), 0x23);
52
53  data[0] = 0xff;
54  assert.strictEqual(data.readInt8(0), -1);
55
56  data[0] = 0x87;
57  assert.strictEqual(data.readInt8(0), -121);
58  assert.strictEqual(data.readInt8(1), -85);
59  assert.strictEqual(data.readInt8(2), 124);
60  assert.strictEqual(data.readInt8(3), -17);
61}
62
63// Test 16 bit integers
64{
65  const buffer = Buffer.from([0x16, 0x79, 0x65, 0x6e, 0x69, 0x78]);
66
67  assert.strictEqual(buffer.readInt16BE(0), 0x1679);
68  assert.strictEqual(buffer.readInt16LE(0), 0x7916);
69
70  buffer[0] = 0xff;
71  buffer[1] = 0x80;
72  assert.strictEqual(buffer.readInt16BE(0), -128);
73  assert.strictEqual(buffer.readInt16LE(0), -32513);
74
75  buffer[0] = 0x77;
76  buffer[1] = 0x65;
77  assert.strictEqual(buffer.readInt16BE(0), 0x7765);
78  assert.strictEqual(buffer.readInt16BE(1), 0x6565);
79  assert.strictEqual(buffer.readInt16BE(2), 0x656e);
80  assert.strictEqual(buffer.readInt16BE(3), 0x6e69);
81  assert.strictEqual(buffer.readInt16BE(4), 0x6978);
82  assert.strictEqual(buffer.readInt16LE(0), 0x6577);
83  assert.strictEqual(buffer.readInt16LE(1), 0x6565);
84  assert.strictEqual(buffer.readInt16LE(2), 0x6e65);
85  assert.strictEqual(buffer.readInt16LE(3), 0x696e);
86  assert.strictEqual(buffer.readInt16LE(4), 0x7869);
87}
88
89// Test 32 bit integers
90{
91  const buffer = Buffer.from([0x43, 0x53, 0x16, 0x79, 0x36, 0x17]);
92
93  assert.strictEqual(buffer.readInt32BE(0), 0x43531679);
94  assert.strictEqual(buffer.readInt32LE(0), 0x79165343);
95
96  buffer[0] = 0xff;
97  buffer[1] = 0xfe;
98  buffer[2] = 0xef;
99  buffer[3] = 0xfa;
100  assert.strictEqual(buffer.readInt32BE(0), -69638);
101  assert.strictEqual(buffer.readInt32LE(0), -84934913);
102
103  buffer[0] = 0x42;
104  buffer[1] = 0xc3;
105  buffer[2] = 0x95;
106  buffer[3] = 0xa9;
107  assert.strictEqual(buffer.readInt32BE(0), 0x42c395a9);
108  assert.strictEqual(buffer.readInt32BE(1), -1013601994);
109  assert.strictEqual(buffer.readInt32BE(2), -1784072681);
110  assert.strictEqual(buffer.readInt32LE(0), -1449802942);
111  assert.strictEqual(buffer.readInt32LE(1), 917083587);
112  assert.strictEqual(buffer.readInt32LE(2), 389458325);
113}
114
115// Test Int
116{
117  const buffer = Buffer.from([0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08]);
118
119  assert.strictEqual(buffer.readIntLE(0, 1), 0x01);
120  assert.strictEqual(buffer.readIntBE(0, 1), 0x01);
121  assert.strictEqual(buffer.readIntLE(0, 3), 0x030201);
122  assert.strictEqual(buffer.readIntBE(0, 3), 0x010203);
123  assert.strictEqual(buffer.readIntLE(0, 5), 0x0504030201);
124  assert.strictEqual(buffer.readIntBE(0, 5), 0x0102030405);
125  assert.strictEqual(buffer.readIntLE(0, 6), 0x060504030201);
126  assert.strictEqual(buffer.readIntBE(0, 6), 0x010203040506);
127  assert.strictEqual(buffer.readIntLE(1, 6), 0x070605040302);
128  assert.strictEqual(buffer.readIntBE(1, 6), 0x020304050607);
129  assert.strictEqual(buffer.readIntLE(2, 6), 0x080706050403);
130  assert.strictEqual(buffer.readIntBE(2, 6), 0x030405060708);
131
132  // Check byteLength.
133  ['readIntBE', 'readIntLE'].forEach((fn) => {
134    ['', '0', null, {}, [], () => {}, true, false, undefined].forEach((len) => {
135      assert.throws(
136        () => buffer[fn](0, len),
137        { code: 'ERR_INVALID_ARG_TYPE' });
138    });
139
140    [Infinity, -1].forEach((byteLength) => {
141      assert.throws(
142        () => buffer[fn](0, byteLength),
143        {
144          code: 'ERR_OUT_OF_RANGE',
145          message: 'The value of "byteLength" is out of range. ' +
146                   `It must be >= 1 and <= 6. Received ${byteLength}`
147        });
148    });
149
150    [NaN, 1.01].forEach((byteLength) => {
151      assert.throws(
152        () => buffer[fn](0, byteLength),
153        {
154          code: 'ERR_OUT_OF_RANGE',
155          name: 'RangeError',
156          message: 'The value of "byteLength" is out of range. ' +
157                   `It must be an integer. Received ${byteLength}`
158        });
159    });
160  });
161
162  // Test 1 to 6 bytes.
163  for (let i = 1; i <= 6; i++) {
164    ['readIntBE', 'readIntLE'].forEach((fn) => {
165      ['', '0', null, {}, [], () => {}, true, false, undefined].forEach((o) => {
166        assert.throws(
167          () => buffer[fn](o, i),
168          {
169            code: 'ERR_INVALID_ARG_TYPE',
170            name: 'TypeError'
171          });
172      });
173
174      [Infinity, -1, -4294967295].forEach((offset) => {
175        assert.throws(
176          () => buffer[fn](offset, i),
177          {
178            code: 'ERR_OUT_OF_RANGE',
179            name: 'RangeError',
180            message: 'The value of "offset" is out of range. ' +
181                     `It must be >= 0 and <= ${8 - i}. Received ${offset}`
182          });
183      });
184
185      [NaN, 1.01].forEach((offset) => {
186        assert.throws(
187          () => buffer[fn](offset, i),
188          {
189            code: 'ERR_OUT_OF_RANGE',
190            name: 'RangeError',
191            message: 'The value of "offset" is out of range. ' +
192                     `It must be an integer. Received ${offset}`
193          });
194      });
195    });
196  }
197}
198