Skip to content

Commit

Permalink
test: port c-ares inet tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
nodech committed Dec 27, 2021
1 parent d05072d commit f7af201
Show file tree
Hide file tree
Showing 5 changed files with 210 additions and 1 deletion.
20 changes: 20 additions & 0 deletions lib/inet.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,16 @@ const TABLE = new Int8Array([
* Presentation to Network
*/

/**
* Convert IPv4 network string to network format.
* @param {String} src - IP String
* @param {Buffer} dst - buffer where to decode to
* @param {Number} off - from where to start.
* @returns {Number} - number of bits, either imputed classfully or specified
* with /CIDR, or -1 if some failure occurred (check errno). ENOENT means it
* was not an IPv4 network specification.
*/

function pton4(src, dst, off) {
if (dst == null)
dst = null;
Expand Down Expand Up @@ -256,6 +266,16 @@ function pton4(src, dst, off) {
return bits;
}

/**
* Convert IPv6 network string to network format.
* @param {String} src - IP String
* @param {Buffer} dst - buffer where to decode to
* @param {Number} off - from where to start.
* @returns {Number} - number of bits, either imputed classfully or specified
* with /CIDR, or -1 if some failure occurred (check errno). ENOENT means it
* was not an IPv6 network specification.
*/

function pton6(src, dst, off) {
if (dst == null)
dst = null;
Expand Down
121 changes: 121 additions & 0 deletions test/data/inet-vectors.js
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.
68 changes: 68 additions & 0 deletions test/inet-test.js
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.`);
}
});
});
2 changes: 1 addition & 1 deletion test/binet-test.js → test/ip-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const assert = require('assert');
const binet = require('../lib/binet');
const vectors = require('./data/vectors');
const vectors = require('./data/ip-vectors');

const allVectors = vectors.ALL.reduce((p, c) => add(p, c));

Expand Down

0 comments on commit f7af201

Please sign in to comment.