-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
210 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
/*! | ||
* Some of the tests cases are based on c-ares: | ||
* Copyright (c) 2007-2018, Daniel Stenberg (MIT License) | ||
* https://github.com/c-ares/c-ares | ||
* https://github.com/c-ares/c-ares/blob/master/test/ares-test-internal.cc | ||
*/ | ||
'use strict'; | ||
|
||
const vectors = exports; | ||
|
||
vectors.PTON4 = [ | ||
[4 * 8, 0, Buffer.from('01020304', 'hex'), '1.2.3.4'], | ||
[4 * 8, 0, Buffer.from('81010101', 'hex'), '129.1.1.1'], | ||
[4 * 8, 0, Buffer.from('C0010101', 'hex'), '192.1.1.1'], | ||
[4 * 8, 0, Buffer.from('E0010101', 'hex'), '224.1.1.1'], | ||
[4 * 8, 0, Buffer.from('E1010101', 'hex'), '225.1.1.1'], | ||
[4, 0, Buffer.from('E0000000', 'hex'), '224'], | ||
[4 * 8, 0, Buffer.from('FD000000', 'hex'), '253'], | ||
[4 * 8, 0, Buffer.from('F0010101', 'hex'), '240.1.1.1'], | ||
[4 * 8, 0, Buffer.from('02030405', 'hex'), '02.3.4.5'], | ||
[3 * 8, 0, Buffer.from('01020304', 'hex'), '1.2.3.4/24'], | ||
[3 * 8, 0, Buffer.from('01020300', 'hex'), '1.2.3/24'], | ||
[2 * 8, 0, Buffer.from('a0000000', 'hex'), '0xa'], | ||
[2 * 8, 0, Buffer.from('a0000000', 'hex'), '0Xa'], | ||
[1 * 8, 0, Buffer.from('01020000', 'hex'), '1.2/8'], | ||
[2 * 8, 0, Buffer.from('01020000', 'hex'), '0x0102/16'], | ||
[4 * 8, 0, Buffer.from('02030405', 'hex'), '02.3.4.5'], | ||
[0, 0, Buffer.from('02030405', 'hex'), '2.3.4.5/000'], | ||
|
||
// Hex 0, onstants are allowed. | ||
[4 * 8, 0, Buffer.from('01020304', 'hex'), '0x01020304'], | ||
[4 * 8, 0, Buffer.from('0a0b0c0d', 'hex'), '0x0a0b0c0d'], | ||
[4 * 8, 0, Buffer.from('0A0B0C0D', 'hex'), '0x0A0B0C0D'], | ||
[4 * 8, 0, Buffer.from('11223340', 'hex'), '0x1122334'] | ||
]; | ||
|
||
// returned code, buffer size, offset, string | ||
vectors.PTON4_MALFORMED = [ | ||
[-1, 4, 0, ''], | ||
[-1, 4, 0, ' '], | ||
[-1, 4, 0, '0x'], | ||
[-1, 4, 0, '0x '], | ||
[-1, 4, 0, 'x0'], | ||
[-1, 4, 0, '0xXYZZY'], | ||
[-1, 4, 0, 'xyzzy'], | ||
[-1, 4, 0, '257.2.3.4'], | ||
[-1, 4, 0, '002.3.4.x'], | ||
[-1, 4, 0, '00.3.4.x'], | ||
[-1, 4, 0, '2.3.4.x'], | ||
[-2, 4, 0, '2.3.4.5.6'], | ||
[-2, 4, 0, '2.3.4.5.6/12'], | ||
[-1, 4, 0, '2.3.4:5'], | ||
[-1, 4, 0, '2.3.4.5/120'], | ||
[-1, 4, 0, '2.3.4.5/1x'], | ||
[-1, 4, 0, '2.3.4.5/x'], | ||
[-1, 4, 0, '0x0xyz'], | ||
[-1, 4, 0, '0x0102030405'], | ||
[-1, 4, 0, '0x010203045'], | ||
|
||
// No room, no room. | ||
// TODO: Should this be -2 instead? | ||
[-1, 3, 0, '1.2.3.4'], | ||
[-1, 2, 0, '0x01020304'], | ||
[-1, 0, 0, '0x01020304'], | ||
[-1, 0, 0, '0x0a0b0c0d'], | ||
[-1, 0, 0, '0x0xyz'], | ||
[-1, 3, 0, '0x1122334'], | ||
[-1, 3, 0, '253'], | ||
|
||
// binet tests | ||
[-1, 4, 1, '0x01020304'], | ||
[-1, 4, 0, '255.255.255.255.100'] | ||
]; | ||
|
||
vectors.PTON6 = [ | ||
[16 * 8, 0, Buffer.alloc(16), '::'], | ||
[16 * 8, 0, Buffer.from('00000000000000000000000000000001', 'hex'), '::1'], | ||
[16 * 8, 0, Buffer.from('12345678000000000000000000000000', 'hex'), '1234:5678::'], | ||
[16 * 8, 0, Buffer.from('001200340000000000000000000000ff', 'hex'), '12:34::ff'], | ||
[16 * 8, 0, Buffer.from('00120034000000000000ffff01020304', 'hex'), '12:34::ffff:1.2.3.4'], | ||
[23 * 1, 0, Buffer.from('00120000000000000000000000000000', 'hex'), '12:34::ffff:1.2.3.4/23'], | ||
[3 * 8, 0, Buffer.from('00120000000000000000000000000000', 'hex'), '12:34::ff/24'], | ||
[0 * 8, 0, Buffer.from('00000000000000000000000000000000', 'hex'), '12:34::ff/0'], | ||
[16 * 8, 0, Buffer.from('00120034000000000000ffff00020000', 'hex'), '12:34::ffff:0.2'], | ||
[16 * 8, 0, Buffer.from('12341234123412341234123412341234', 'hex'), | ||
'1234:1234:1234:1234:1234:1234:1234:1234'] | ||
]; | ||
|
||
// returned code, buffer size, offset, string | ||
vectors.PTON6_MALFORMED = [ | ||
[-1, 16, 0, '12:34::ff/240'], | ||
[-1, 16, 0, '12:34::ff/02'], | ||
[-1, 16, 0, '12:34::ff/2y'], | ||
[-1, 16, 0, '12:34::ff/y'], | ||
[-1, 16, 0, '12:34::ff/'], | ||
[-1, 16, 0, ''], | ||
[-1, 16, 0, ':x'], | ||
[-1, 16, 0, ':'], | ||
[-1, 16, 0, ': :1234'], | ||
[-1, 16, 0, '::12345'], | ||
[-1, 16, 0, '1234::2345:3456::0011'], | ||
// TODO: Check if this should fail. | ||
// [-1, 16, 0, '1234:1234:1234:1234:1234:1234:1234:1234:'], | ||
[-1, 16, 0, '1234:1234:1234:1234:1234:1234:1234:1234::'], | ||
[-1, 16, 0, '1234:1234:1234:1234:1234:1234:1234:1.2.3.4'], | ||
[-1, 16, 0, ':1234:1234:1234:1234:1234:1234:1234:1234'], | ||
[-1, 16, 0, ':1234:1234:1234:1234:1234:1234:1234:1234:'], | ||
[-1, 16, 0, '1234:1234:1234:1234:1234:1234:1234:1234:5678'], | ||
[-1, 16, 0, '1234:1234:1234:1234:1234:1234:1234:1234:5678:5678:5678'], | ||
[-1, 16, 0, '12:34::ffff:257.2.3.4'], | ||
[-1, 16, 0, '12:34::ffff:002.2.3.4'], | ||
[-1, 16, 0, '12:34::ffff:1.2.3.4.5.6'], | ||
[-1, 16, 0, '12:34::ffff:1.2.3.4.5'], | ||
[-1, 16, 0, '12:34::ffff:1.2.3.z'], | ||
[-1, 16, 0, '12:34::ffff:1.2.3001.4'], | ||
[-1, 16, 0, '12:34::ffff:1.2.3..4'], | ||
[-1, 16, 0, '12:34::ffff:1.2.3.'], | ||
[-1, 16, 0, '1234:1234:1234:1234:1234:1234:1234:1234:5678:5678'], | ||
|
||
[-2, 15, 0, '12:34::ff'] | ||
]; |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
'use strict'; | ||
|
||
const assert = require('bsert'); | ||
const inet = require('../lib/inet'); | ||
|
||
const inetVectors = require('./data/inet-vectors'); | ||
|
||
describe('inet', function() { | ||
it('should convert using pton4', () => { | ||
for (const [ebits, off, eraw, str] of inetVectors.PTON4) { | ||
const raw = Buffer.alloc(4); | ||
const rawPton = Buffer.alloc(4); | ||
const resBits = inet.pton4(str, raw, off); | ||
const resPton = inet.pton(4, str, rawPton, off); | ||
|
||
assert.bufferEqual(raw, eraw, | ||
`Failed to convert ${str}.`); | ||
assert.bufferEqual(rawPton, eraw, | ||
`Failed to convert ${str}.`); | ||
assert.strictEqual(resBits, ebits, | ||
`Incorrect bits for ${str}.`); | ||
assert.strictEqual(resPton, ebits, | ||
`Incorrect bits for ${str}.`); | ||
} | ||
}); | ||
|
||
it('should fail converting using pton4', () => { | ||
for (const [code, size, off, str] of inetVectors.PTON4_MALFORMED) { | ||
const raw = Buffer.alloc(size); | ||
const rawPton = Buffer.alloc(size); | ||
const res = inet.pton4(str, raw, off); | ||
const resPton = inet.pton(4, str, rawPton, off); | ||
|
||
assert.strictEqual(res, code, `${str} should fail.`); | ||
assert.strictEqual(resPton, code, `${str} should fail.`); | ||
} | ||
}); | ||
|
||
it('should convert using pton6', () => { | ||
for (const [ebits, off, eraw, str] of inetVectors.PTON6) { | ||
const raw = Buffer.alloc(16); | ||
const rawPton = Buffer.alloc(16); | ||
const res = inet.pton6(str, raw, off); | ||
const resPton = inet.pton(6, str, rawPton, off); | ||
|
||
assert.bufferEqual(raw, eraw, | ||
`Failed to convert ${str}.`); | ||
assert.bufferEqual(rawPton, eraw, | ||
`Failed to convert ${str}.`); | ||
assert.strictEqual(res, ebits, | ||
`Incorrect bits for ${str}.`); | ||
assert.strictEqual(resPton, ebits, | ||
`Incorrect bits for ${str}.`); | ||
} | ||
}); | ||
|
||
it('should fail converting using pton6', () => { | ||
for (const [code, size, off, str] of inetVectors.PTON6_MALFORMED) { | ||
const raw = Buffer.alloc(size); | ||
const rawPton = Buffer.alloc(size); | ||
const res = inet.pton6(str, raw, off); | ||
const resPton = inet.pton6(str, rawPton, off); | ||
|
||
assert.strictEqual(res, code, `${str} should fail.`); | ||
assert.strictEqual(resPton, code, `${str} should fail.`); | ||
} | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters