Skip to content

Commit

Permalink
ip: add BufferWriter support.
Browse files Browse the repository at this point in the history
ip: minor fix to 16 byte ipv4 writer.
pkg: add bufio as a dev dependency for testing.
test: add tests for binet.read/binet.write and
binet.readBR/binet.writeBW
  • Loading branch information
nodech committed Dec 28, 2021
1 parent f7af201 commit 3fdd678
Show file tree
Hide file tree
Showing 5 changed files with 418 additions and 59 deletions.
15 changes: 13 additions & 2 deletions lib/ip.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ binet.write = function write(dst, str, off, size) {
off += 10;
dst[off++] = 0xff;
dst[off++] = 0xff;
return off;
return off + 4;
}

if (inet.pton6(str, dst, off) >= 0)
Expand All @@ -233,7 +233,18 @@ binet.write = function write(dst, str, off, size) {

binet.writeBW = function writeBW(bw, str, size) {
assert(bw && typeof bw === 'object');
bw.offset = binet.write(bw.data, str, bw.offset, size);

// StaticWriter
if (bw.data) {
bw.offset = binet.write(bw.data, str, bw.offset, size);
return bw;
}

const buf = Buffer.alloc(size);
const off = binet.write(buf, str, 0, size);
// these must always match.
assert(off === size);
bw.writeBytes(buf);
return bw;
};

Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
"bsert": "~0.0.10"
},
"devDependencies": {
"bmocha": "^2.1.0"
"bmocha": "^2.1.0",
"bufio": "^1.0.7"
},
"engines": {
"node": ">=8.0.0"
Expand Down
6 changes: 4 additions & 2 deletions test/data/inet-vectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

const vectors = exports;

// [bits, offset, expected buffer, from string]
vectors.PTON4 = [
[4 * 8, 0, Buffer.from('01020304', 'hex'), '1.2.3.4'],
[4 * 8, 0, Buffer.from('81010101', 'hex'), '129.1.1.1'],
Expand All @@ -34,7 +35,7 @@ vectors.PTON4 = [
[4 * 8, 0, Buffer.from('11223340', 'hex'), '0x1122334']
];

// returned code, buffer size, offset, string
// [code, buffer size, offset, string]
vectors.PTON4_MALFORMED = [
[-1, 4, 0, ''],
[-1, 4, 0, ' '],
Expand Down Expand Up @@ -72,6 +73,7 @@ vectors.PTON4_MALFORMED = [
[-1, 4, 0, '255.255.255.255.100']
];

// [bits, offset, expected buffer, from string]
vectors.PTON6 = [
[16 * 8, 0, Buffer.alloc(16), '::'],
[16 * 8, 0, Buffer.from('00000000000000000000000000000001', 'hex'), '::1'],
Expand All @@ -86,7 +88,7 @@ vectors.PTON6 = [
'1234:1234:1234:1234:1234:1234:1234:1234']
];

// returned code, buffer size, offset, string
// [code, buffer size, offset, string]
vectors.PTON6_MALFORMED = [
[-1, 16, 0, '12:34::ff/240'],
[-1, 16, 0, '12:34::ff/02'],
Expand Down
223 changes: 223 additions & 0 deletions test/data/read-write-vectors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,223 @@
'use strict';

/*
* Some extra vectors for the options and etc.
*/

const vectors = exports;

// NOTE: str needs to be normalized.
vectors.READ = [
{
off: 0,
size: 16,
buf: Buffer.from('00000000000000000000000000000001', 'hex'),
str: '::1',
err: null
},
{
off: null,
size: null,
buf: Buffer.from('00000000000000000000000000000002', 'hex'),
str: '::2',
err: null
},
{
off: 0,
size: 16,
buf: Buffer.from('fd87d87eeb43ffffffffffffffffffff', 'hex'),
str: '7777777777777777.onion',
err: null
},

// some errors
{
off: 10,
size: 16,
buf: Buffer.alloc(25),
str: '',
err: 'Out of bounds read.'
},
{
off: 0,
size: 32,
buf: Buffer.alloc(32),
str: '',
err: 'Invalid IP address.'
}
];

// Writes with SIZE: 4
vectors.WRITE_4 = [
{
str: '4.3.2.1',
off: 0,
size: 4,
foff: 4,
buf: Buffer.from('04030201', 'hex'),
err: null
},
{
str: '4.3.2.1',
off: null,
size: 4,
foff: 4,
buf: Buffer.from('04030201', 'hex'),
err: null
},
{
str: '4.3.2.1',
off: 0,
size: 4,
foff: 4,
buf: Buffer.from('0403020100000000', 'hex'),
err: null
},
{
str: '4.3.2.1',
off: 4,
size: 4,
foff: 8,
buf: Buffer.from('0000000004030201', 'hex'),
err: null
},
// mapped 4
{
str: '::ffff:0403:0201',
off: 0,
size: 4,
foff: 4,
buf: Buffer.from('04030201', 'hex'),
err: null
},

// Errors
// Not Mapped IPv4
{
str: '::0403:0201',
off: 0,
size: 4,
foff: 0,
buf: Buffer.from('00000000000000000000000000000000', 'hex'),
err: 'Out of bounds write.'
},
{
str: ' ',
off: 0,
size: 4,
foff: 0,
buf: Buffer.from('00000000000000000000000000000000', 'hex'),
err: 'Invalid IPv4 address.'
}
];

vectors.WRITE_6 = [
// size 16
{
str: '::1',
off: 0,
size: 16,
foff: 16,
buf: Buffer.from('00000000000000000000000000000001', 'hex'),
err: null
},
{
str: '::2',
off: null,
size: null,
foff: 16,
buf: Buffer.from('00000000000000000000000000000002', 'hex'),
err: null
},
{
str: 'ffff::eeee',
off: 16,
size: 16,
foff: 32,
buf: Buffer.from('00000000000000000000000000000000'
+ 'ffff000000000000000000000000eeee', 'hex'),
err: null
},
{
str: 'ffff::eeee',
off: 8,
size: 16,
foff: 24,
buf: Buffer.from('0000000000000000'
+ 'ffff000000000000000000000000eeee'
+ '0000000000000000', 'hex'),
err: null
},
{
str: '4.3.2.1',
off: 0,
size: 16,
foff: 16,
buf: Buffer.from('00000000000000000000ffff04030201', 'hex'),
err: null
},
{
str: '4.3.2.1',
off: 8,
size: 16,
foff: 24,
buf: Buffer.from('0000000000000000'
+ '00000000000000000000ffff04030201'
+ '0000000000000000', 'hex'),
err: null
},

// Errors
// size: 16
{
off: 10,
size: 16,
foff: 0,
buf: Buffer.alloc(25),
str: '',
err: 'Out of bounds write.'
},
{
str: ' ',
off: 0,
size: 16,
foff: 0,
buf: Buffer.alloc(32),
err: 'Invalid IPv6 address.'
},

// Incorrect size
{
off: 0,
size: 32,
foff: 0,
buf: Buffer.alloc(40),
str: '',
err: 'Invalid IP address.'
}
];

vectors.WRITE_ONION = [
{
off: 0,
size: 16,
foff: 16,
buf: Buffer.from('fd87d87eeb43ffffffffffffffffffff', 'hex'),
str: '7777777777777777.onion',
err: null
},
{
off: 0,
size: 16,
foff: 16,
buf: Buffer.from('fd87d87eeb4300000000000000000000', 'hex'),
str: 'aaaaaaaaaaaaaaaa.onion',
err: null
}
];

vectors.WRITE = [
...vectors.WRITE_4,
...vectors.WRITE_6,
...vectors.WRITE_ONION
];
Loading

0 comments on commit 3fdd678

Please sign in to comment.