-
Notifications
You must be signed in to change notification settings - Fork 9
/
test.js
45 lines (37 loc) · 1.13 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
var assert = require('assert');
var log = console.log;
var rbytes = require('./index');
var buf;
var octets = [0xa8, 0x75, 0xfe, 0xae, 0xc6, 0x7f, 0x00, 0x00, 0xa8, 0x75];
log("random bytes");
buf = rbytes.randomBytes(12);
assert.ok(Buffer.isBuffer(buf));
assert.equal(buf.length, 12);
log("buffer to hex");
buf = Buffer(octets);
assert.equal(buf.toHex(), "a875feaec67f0000a875");
log("write hex to buffer");
buf = Buffer(10);
assert.equal(buf.writeHex("a875feaec67f0000a875"), 10);
assert.equal(buf.length, 10);
for (var i=0; i<10; i++) {
assert.equal(buf[i], octets[i]);
}
log("add leading 0");
buf = Buffer(1)
assert.equal(buf.writeHex("f"), 1);
assert.equal(buf.toHex(), "0f");
log("too small");
buf = Buffer(1);
assert.throws(function() { buf.writeHex("ffff") },
Error, "Buffer too small");
buf = Buffer(10);
assert.throws(function() { buf.writeHex("a875feaec67f0000a8751") },
Error, "Buffer too small");
log("invalid hex");
buf = Buffer(10);
assert.throws(function() { buf.writeHex("gfhf") },
TypeError, "Invalid hex string");
buf = Buffer(1);
assert.throws(function() { buf.writeHex("g") },
TypeError, "Invalid hex string");