2014-12-13 19:56:14 +01:00
|
|
|
var expect = require("chai").expect;
|
2014-12-27 16:17:41 +01:00
|
|
|
var allTests = typeof window === "undefined" ?
|
|
|
|
!!process.env.ALL_TESTS :
|
|
|
|
window.ALL_TESTS;
|
|
|
|
|
2015-01-11 14:59:43 +01:00
|
|
|
var bufferEqual = require("buffer-equal");
|
2014-12-18 17:47:18 +01:00
|
|
|
var bitmessage = require("./lib");
|
2015-01-11 16:27:14 +01:00
|
|
|
var bmcrypto = require("./lib/crypto");
|
2015-01-05 00:40:52 +01:00
|
|
|
var structs = bitmessage.structs;
|
|
|
|
var message = structs.message;
|
|
|
|
var var_int = structs.var_int;
|
|
|
|
var var_str = structs.var_str;
|
|
|
|
var var_int_list = structs.var_int_list;
|
2015-01-06 00:32:10 +01:00
|
|
|
var net_addr = structs.net_addr;
|
2015-01-15 22:00:27 +01:00
|
|
|
var inv_vect = structs.inv_vect;
|
2015-01-11 14:59:43 +01:00
|
|
|
var encrypted = structs.encrypted;
|
2015-01-05 00:40:52 +01:00
|
|
|
var messageEncodings = structs.messageEncodings;
|
|
|
|
var serviceFeatures = structs.serviceFeatures;
|
|
|
|
var pubkeyFeatures = structs.pubkeyFeatures;
|
2015-01-15 18:13:02 +01:00
|
|
|
var messages = bitmessage.messages;
|
|
|
|
var version = messages.version;
|
2015-01-15 19:11:33 +01:00
|
|
|
var addr = messages.addr;
|
2015-01-15 22:00:27 +01:00
|
|
|
var inv = messages.inv;
|
2015-01-02 22:24:36 +01:00
|
|
|
var WIF = bitmessage.WIF;
|
2015-01-07 22:28:13 +01:00
|
|
|
var POW = bitmessage.POW;
|
2015-01-02 22:24:36 +01:00
|
|
|
var Address = bitmessage.Address;
|
2015-01-11 18:24:39 +01:00
|
|
|
var UserAgent = bitmessage.UserAgent;
|
2015-01-02 22:24:36 +01:00
|
|
|
|
|
|
|
describe("Crypto", function() {
|
|
|
|
it("should implement SHA-512 hash", function() {
|
2015-01-03 15:52:27 +01:00
|
|
|
expect(bmcrypto.sha512(Buffer("test")).toString("hex")).to.equal("ee26b0dd4af7e749aa1a8ee3c10ae9923f618980772e473f8819a5d4940e0db27ac185f8a0e1d5f84f88bc887fd67b143732c304cc5fa9ad8e6f57f50028a8ff");
|
2015-01-02 22:24:36 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should implement SHA-256 hash", function() {
|
2015-01-03 15:52:27 +01:00
|
|
|
expect(bmcrypto.sha256(Buffer("test")).toString("hex")).to.equal("9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08");
|
2015-01-02 22:24:36 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should implement RIPEMD-160 hash", function() {
|
2015-01-03 15:52:27 +01:00
|
|
|
expect(bmcrypto.ripemd160(Buffer("test")).toString("hex")).to.equal("5e52fee47e6b070565f74372468cdc699de89107");
|
2015-01-02 22:24:36 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should implement cryptographically secure PRNG", function() {
|
|
|
|
var size = 100;
|
|
|
|
var rnd = bmcrypto.randomBytes(size);
|
|
|
|
expect(Buffer.isBuffer(rnd)).to.be.true;
|
|
|
|
expect(rnd.length).to.equal(size);
|
|
|
|
// Very simple statistical test.
|
|
|
|
var bytes = {};
|
|
|
|
var sum = 0;
|
|
|
|
var value;
|
|
|
|
for (var i = 0; i < size; i++) {
|
|
|
|
value = rnd[i];
|
|
|
|
sum += value;
|
|
|
|
if (!bytes[value]) {
|
|
|
|
bytes[value] = 0;
|
|
|
|
}
|
|
|
|
bytes[value]++;
|
|
|
|
expect(bytes[value]).to.be.below(7);
|
|
|
|
}
|
|
|
|
// Ideal sum = (255 / 2) * size = 12750
|
|
|
|
expect(sum).to.be.above(10000);
|
|
|
|
expect(sum).to.be.below(15000);
|
|
|
|
});
|
|
|
|
});
|
2014-12-13 19:56:14 +01:00
|
|
|
|
2015-01-02 22:24:36 +01:00
|
|
|
describe("Common structures", function() {
|
2015-01-04 21:22:14 +01:00
|
|
|
describe("message", function() {
|
|
|
|
it("should decode", function() {
|
|
|
|
var res;
|
|
|
|
res = message.decode(Buffer("e9beb4d97465737400000000000000000000000770b33ce97061796c6f6164", "hex"));
|
|
|
|
expect(res.command).to.equal("test");
|
|
|
|
expect(res.payload.toString()).to.equal("payload");
|
|
|
|
expect(res.length).to.equal(31);
|
|
|
|
expect(res.rest.toString("hex")).to.equal("");
|
2015-01-08 03:00:19 +01:00
|
|
|
|
|
|
|
res = message.decode(Buffer("e9beb4d90000000000000000000000000000000770b33ce97061796c6f6164", "hex"));
|
|
|
|
expect(res.command).to.equal("");
|
|
|
|
});
|
|
|
|
|
2015-01-15 18:13:02 +01:00
|
|
|
it("should throw when decoding message with truncated payload", function() {
|
|
|
|
expect(message.decode.bind(null, Buffer("e9beb4d97465737400000000000000000000000770b33ce97061796c6f61", "hex"))).to.throw(Error);
|
|
|
|
});
|
|
|
|
|
2015-01-08 03:00:19 +01:00
|
|
|
it("should encode", function() {
|
|
|
|
expect(message.encode({command: "test", payload: Buffer("payload")}).toString("hex")).to.equal("e9beb4d97465737400000000000000000000000770b33ce97061796c6f6164");
|
2015-01-04 21:22:14 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-01-01 20:26:06 +01:00
|
|
|
describe("var_int", function() {
|
|
|
|
it("should decode", function() {
|
|
|
|
var res;
|
|
|
|
expect(var_int.decode.bind(null, Buffer([]))).to.throw(Error);
|
|
|
|
expect(var_int.decode.bind(null, Buffer("fd00", "hex"))).to.throw(Error);
|
2015-01-02 21:28:45 +01:00
|
|
|
expect(var_int.decode.bind(null, Buffer("ff004170706e9b0368", "hex"))).to.throw(Error);
|
2014-12-18 17:47:18 +01:00
|
|
|
|
2015-01-01 20:26:06 +01:00
|
|
|
res = var_int.decode(Buffer([123]));
|
|
|
|
expect(res.value).to.equal(123);
|
|
|
|
expect(res.length).to.equal(1);
|
|
|
|
expect(res.rest.toString("hex")).to.equal("")
|
2014-12-18 17:47:18 +01:00
|
|
|
|
2015-01-01 20:26:06 +01:00
|
|
|
res = var_int.decode(Buffer("fd123456", "hex"));
|
|
|
|
expect(res.value).to.equal(0x1234);
|
|
|
|
expect(res.length).to.equal(3);
|
|
|
|
expect(res.rest.toString("hex")).to.equal("56");
|
2014-12-18 17:47:18 +01:00
|
|
|
|
2015-01-01 20:26:06 +01:00
|
|
|
res = var_int.decode(Buffer("fe1234567890", "hex"));
|
|
|
|
expect(res.value).to.equal(0x12345678);
|
|
|
|
expect(res.length).to.equal(5);
|
|
|
|
expect(res.rest.toString("hex")).to.equal("90");
|
2014-12-18 17:47:18 +01:00
|
|
|
|
2015-01-01 20:26:06 +01:00
|
|
|
res = var_int.decode(Buffer("ff0000001234567890", "hex"));
|
2015-01-02 21:28:45 +01:00
|
|
|
expect(res.value).to.equal(0x1234567890);
|
2015-01-01 20:26:06 +01:00
|
|
|
expect(res.length).to.equal(9);
|
|
|
|
expect(res.rest.length).to.equal(0);
|
|
|
|
});
|
2014-12-18 18:16:21 +01:00
|
|
|
|
2015-01-01 20:26:06 +01:00
|
|
|
it("should check for lowest length on decode", function() {
|
|
|
|
expect(var_int.decode.bind(null, Buffer("fd00fc", "hex"))).to.throw(Error);
|
|
|
|
expect(var_int.decode.bind(null, Buffer("fe0000ffff", "hex"))).to.throw(Error);
|
|
|
|
expect(var_int.decode.bind(null, Buffer("ff00000000ffffffff", "hex"))).to.throw(Error);
|
|
|
|
});
|
2014-12-27 12:38:58 +01:00
|
|
|
|
2015-01-01 20:26:06 +01:00
|
|
|
it("should encode", function() {
|
|
|
|
expect(var_int.encode(123).toString("hex")).to.equal("7b");
|
|
|
|
expect(var_int.encode(0x1234).toString("hex")).to.equal("fd1234");
|
|
|
|
expect(var_int.encode(0x12345678).toString("hex")).to.equal("fe12345678");
|
|
|
|
expect(var_int.encode(0x1234567890).toString("hex")).to.equal("ff0000001234567890");
|
|
|
|
expect(var_int.encode(Buffer("1234567890", "hex")).toString("hex")).to.equal("ff0000001234567890");
|
|
|
|
expect(var_int.encode.bind(null, -123)).to.throw(Error);
|
2015-01-02 21:28:45 +01:00
|
|
|
expect(var_int.encode.bind(null, 0x4170706e9b0368)).to.throw(Error);
|
2015-01-01 20:26:06 +01:00
|
|
|
expect(var_int.encode.bind(null, Buffer("123456789012345678", "hex"))).to.throw(Error);
|
|
|
|
expect(var_int.encode.bind(null, "test")).to.throw(Error);
|
|
|
|
});
|
2014-12-27 12:38:58 +01:00
|
|
|
});
|
2015-01-01 21:00:33 +01:00
|
|
|
|
|
|
|
describe("var_str", function() {
|
|
|
|
it("should decode", function() {
|
|
|
|
var res;
|
2015-01-02 15:27:03 +01:00
|
|
|
res = var_str.decode(Buffer("00", "hex"));
|
|
|
|
expect(res.str).to.equal("");
|
|
|
|
expect(res.length).to.equal(1);
|
|
|
|
expect(res.rest.toString("hex")).to.equal("");
|
|
|
|
|
2015-01-01 21:00:33 +01:00
|
|
|
res = var_str.decode(Buffer("0474657374", "hex"));
|
|
|
|
expect(res.str).to.equal("test");
|
|
|
|
expect(res.length).to.equal(5);
|
|
|
|
expect(res.rest.toString("hex")).to.equal("");
|
|
|
|
|
2015-01-02 15:27:03 +01:00
|
|
|
res = var_str.decode(Buffer("0474657374ffffff", "hex"));
|
|
|
|
expect(res.str).to.equal("test");
|
|
|
|
expect(res.length).to.equal(5);
|
|
|
|
expect(res.rest.toString("hex")).to.equal("ffffff");
|
2015-01-11 16:27:14 +01:00
|
|
|
|
|
|
|
// Truncated input.
|
|
|
|
expect(var_str.decode.bind(null, Buffer("04746573", "hex"))).to.throw(Error);
|
2015-01-01 21:00:33 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should encode", function() {
|
|
|
|
expect(var_str.encode("test").toString("hex")).to.equal("0474657374");
|
|
|
|
expect(var_str.encode("").toString("hex")).to.equal("00");
|
|
|
|
});
|
|
|
|
});
|
2015-01-02 15:27:03 +01:00
|
|
|
|
|
|
|
describe("var_int_list", function() {
|
|
|
|
it("should decode", function() {
|
|
|
|
var res;
|
|
|
|
res = var_int_list.decode(Buffer("00", "hex"));
|
|
|
|
expect(res.list).to.deep.equal([]);
|
|
|
|
expect(res.length).to.equal(1);
|
|
|
|
expect(res.rest.toString("hex")).to.equal("");
|
|
|
|
|
|
|
|
res = var_int_list.decode(Buffer("0501fd0400ff0004000000000000fd9c40fe000186a0", "hex"));
|
|
|
|
expect(res.length).to.equal(22);
|
|
|
|
expect(res.list.length).to.equal(5);
|
|
|
|
expect(res.list[0]).to.equal(1);
|
|
|
|
expect(res.list[1]).to.equal(1024);
|
|
|
|
expect(res.list[2] == 1125899906842624).to.be.true;
|
|
|
|
expect(res.list[3]).to.equal(40000);
|
|
|
|
expect(res.list[4]).to.equal(100000);
|
|
|
|
expect(res.rest.toString("hex")).to.equal("");
|
|
|
|
|
|
|
|
res = var_int_list.decode(Buffer("0501fd0400ff0004000000000000fd9c40fe000186a0ffffff", "hex"));
|
|
|
|
expect(res.length).to.equal(22);
|
|
|
|
expect(res.list.length).to.equal(5);
|
|
|
|
expect(res.rest.toString("hex")).to.equal("ffffff");
|
2015-01-11 16:27:14 +01:00
|
|
|
|
|
|
|
// Truncated input.
|
|
|
|
expect(var_int_list.decode.bind(null, Buffer("0501fd0400ff0004000000000000fd9c40fe000186", "hex"))).to.throw(Error);
|
2015-01-02 15:27:03 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should encode", function() {
|
|
|
|
expect(var_int_list.encode([]).toString("hex")).to.equal("00");
|
|
|
|
expect(var_int_list.encode([1, 1024, 1125899906842624, 40000, 100000]).toString("hex")).to.equal("0501fd0400ff0004000000000000fd9c40fe000186a0");
|
|
|
|
});
|
|
|
|
});
|
2015-01-05 00:40:52 +01:00
|
|
|
|
2015-01-06 00:32:10 +01:00
|
|
|
// FIXME(Kagami): Add more tests for inet_pton, inet_ntop; add more
|
|
|
|
// fail tests.
|
|
|
|
describe("net_addr", function() {
|
|
|
|
it("should decode", function() {
|
|
|
|
var res;
|
|
|
|
res = net_addr.decode(Buffer("0000000054aaf6c000000001000000000000000100000000000000000000ffff7f00000120fc", "hex"));
|
|
|
|
expect(res.time.getTime()).to.equal(1420490432000);
|
|
|
|
expect(res.stream).to.equal(1);
|
|
|
|
expect(res.services).to.have.members([serviceFeatures.NODE_NETWORK]);
|
|
|
|
expect(res.host).to.equal("127.0.0.1");
|
|
|
|
expect(res.port).to.equal(8444);
|
|
|
|
|
|
|
|
expect(net_addr.decode.bind(null, Buffer("000000000000000100000000000000000000ffff7f00000120fc", "hex"))).to.throw(Error);;
|
|
|
|
|
|
|
|
res = net_addr.decode(Buffer("000000000000000100000000000000000000ffff7f00000120fc", "hex"), {short: true});
|
|
|
|
expect(res.services).to.have.members([serviceFeatures.NODE_NETWORK]);
|
|
|
|
expect(res.host).to.equal("127.0.0.1");
|
|
|
|
expect(res.port).to.equal(8444);
|
|
|
|
|
|
|
|
res = net_addr.decode(Buffer("000000000000000100000000000000000000000000000001fde8", "hex"), {short: true});
|
|
|
|
expect(res.services).to.have.members([serviceFeatures.NODE_NETWORK]);
|
|
|
|
expect(res.host).to.equal("0:0:0:0:0:0:0:1");
|
|
|
|
expect(res.port).to.equal(65000);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should encode", function() {
|
|
|
|
var time = new Date(1420490432000);
|
|
|
|
expect(net_addr.encode({time: time, stream: 1, services: [serviceFeatures.NODE_NETWORK], host: "127.0.0.1", port: 8444}).toString("hex")).to.equal("0000000054aaf6c000000001000000000000000100000000000000000000ffff7f00000120fc");
|
|
|
|
expect(net_addr.encode({short: true, services: [serviceFeatures.NODE_NETWORK], host: "127.0.0.1", port: 8444}).toString("hex")).to.equal("000000000000000100000000000000000000ffff7f00000120fc");
|
2015-01-06 00:34:15 +01:00
|
|
|
expect(net_addr.encode({short: true, host: "::1", port: 65000}).toString("hex")).to.equal("000000000000000100000000000000000000000000000001fde8");
|
2015-01-06 00:32:10 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-01-15 22:00:27 +01:00
|
|
|
describe("inv_vect", function() {
|
|
|
|
it("should encode", function() {
|
|
|
|
expect(inv_vect.encode("test").toString("hex")).to.equal("faadcaf60afd35dfcdb5e9ea0d0a0531f6338c62187cff37a1efe11f1d41a348");
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("encrypted", function() {
|
2015-01-11 14:59:43 +01:00
|
|
|
it("should encode and decode", function() {
|
|
|
|
var iv = Buffer(16);
|
|
|
|
var ephemPublicKey = Buffer(65);
|
|
|
|
ephemPublicKey[0] = 0x04;
|
2015-01-14 00:36:07 +01:00
|
|
|
var ciphertext = Buffer("test");
|
2015-01-11 14:59:43 +01:00
|
|
|
var mac = Buffer(32);
|
|
|
|
var inopts = {
|
|
|
|
iv: iv,
|
|
|
|
ephemPublicKey: ephemPublicKey,
|
2015-01-14 00:36:07 +01:00
|
|
|
ciphertext: ciphertext,
|
2015-01-11 14:59:43 +01:00
|
|
|
mac: mac,
|
|
|
|
};
|
|
|
|
|
|
|
|
var encoded = encrypted.encode(inopts);
|
|
|
|
expect(encoded.length).to.equal(122);
|
|
|
|
var outopts = encrypted.decode(encoded);
|
|
|
|
expect(bufferEqual(iv, outopts.iv)).to.be.true;
|
|
|
|
expect(bufferEqual(ephemPublicKey, outopts.ephemPublicKey)).to.be.true;
|
2015-01-14 00:36:07 +01:00
|
|
|
expect(ciphertext.toString()).to.equal("test");
|
2015-01-11 14:59:43 +01:00
|
|
|
expect(bufferEqual(mac, outopts.mac)).to.be.true;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-01-15 22:00:27 +01:00
|
|
|
describe("message encodings", function() {
|
2015-01-05 00:40:52 +01:00
|
|
|
it("should decode", function() {
|
|
|
|
expect(messageEncodings.decode(Buffer([2])).value).to.equal(messageEncodings.SIMPLE);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should encode", function() {
|
|
|
|
expect(messageEncodings.encode(messageEncodings.SIMPLE).toString("hex")).to.equal("02");
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-01-15 22:00:27 +01:00
|
|
|
describe("service features", function() {
|
2015-01-05 00:40:52 +01:00
|
|
|
it("should decode", function() {
|
|
|
|
expect(serviceFeatures.decode(Buffer("0000000000000001", "hex"))).to.have.members([serviceFeatures.NODE_NETWORK]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should encode", function() {
|
|
|
|
expect(serviceFeatures.encode([serviceFeatures.NODE_NETWORK]).toString("hex")).to.equal("0000000000000001");
|
2015-01-15 15:46:58 +01:00
|
|
|
expect(serviceFeatures.encode(serviceFeatures.NODE_NETWORK).toString("hex")).to.equal("0000000000000001");
|
2015-01-05 00:40:52 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-01-15 22:00:27 +01:00
|
|
|
describe("pubkey features", function() {
|
2015-01-05 00:40:52 +01:00
|
|
|
it("should decode", function() {
|
|
|
|
expect(pubkeyFeatures.decode(Buffer("c0000000", "hex"))).to.have.members([pubkeyFeatures.DOES_ACK, pubkeyFeatures.INCLUDE_DESTINATION]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should encode", function() {
|
|
|
|
expect(pubkeyFeatures.encode([pubkeyFeatures.INCLUDE_DESTINATION, pubkeyFeatures.DOES_ACK]).toString("hex")).to.equal("c0000000");
|
2015-01-15 15:46:58 +01:00
|
|
|
expect(pubkeyFeatures.encode(pubkeyFeatures.INCLUDE_DESTINATION).toString("hex")).to.equal("40000000");
|
2015-01-05 00:40:52 +01:00
|
|
|
});
|
|
|
|
});
|
2014-12-18 17:47:18 +01:00
|
|
|
});
|
|
|
|
|
2015-01-15 18:13:02 +01:00
|
|
|
describe("Message types", function() {
|
|
|
|
describe("version", function() {
|
|
|
|
it("should encode and decode", function() {
|
|
|
|
var res = version.decode(version.encode({
|
|
|
|
remoteHost: "1.2.3.4",
|
|
|
|
remotePort: 48444,
|
|
|
|
port: 8444,
|
|
|
|
}));
|
|
|
|
expect(res.version).to.equal(3);
|
|
|
|
expect(res.services).to.deep.equal([serviceFeatures.NODE_NETWORK]);
|
|
|
|
expect(res.time).to.be.instanceof(Date);
|
|
|
|
expect(res.remoteHost).to.equal("1.2.3.4");
|
|
|
|
expect(res.remotePort).to.equal(48444);
|
|
|
|
expect(res.port).to.equal(8444);
|
|
|
|
expect(bufferEqual(res.nonce, version.NONCE)).to.be.true;
|
|
|
|
expect(res.software).to.deep.equal(UserAgent.SELF);
|
|
|
|
expect(res.streamNumbers).to.deep.equal([1]);
|
|
|
|
expect(res.length).to.equal(101);
|
|
|
|
});
|
|
|
|
});
|
2015-01-15 19:11:33 +01:00
|
|
|
|
|
|
|
describe("addr", function() {
|
|
|
|
it("should encode and decode", function() {
|
|
|
|
var res = addr.decode(addr.encode([]));
|
|
|
|
expect(res.length).to.equal(1);
|
|
|
|
expect(res.addrs).to.deep.equal([]);
|
|
|
|
|
|
|
|
res = addr.decode(addr.encode([
|
|
|
|
{host: "1.2.3.4", port: 8444},
|
|
|
|
{host: "ff::1", port: 18444},
|
|
|
|
]));
|
|
|
|
expect(res.length).to.equal(77);
|
|
|
|
expect(res.addrs.length).to.equal(2);
|
|
|
|
expect(res.addrs[0].host).to.equal("1.2.3.4");
|
|
|
|
expect(res.addrs[0].port).to.equal(8444);
|
|
|
|
expect(res.addrs[1].host).to.equal("ff:0:0:0:0:0:0:1");
|
|
|
|
expect(res.addrs[1].port).to.equal(18444);
|
|
|
|
});
|
2015-01-15 21:19:52 +01:00
|
|
|
|
|
|
|
it("shouldn't encode/decode more than 1000 entires", function() {
|
|
|
|
expect(addr.encode.bind(null, Array(2000))).to.throw(/too many/i);
|
|
|
|
expect(addr.decode.bind(null, var_int.encode(2000))).to.throw(/too many/i);
|
|
|
|
});
|
2015-01-15 19:11:33 +01:00
|
|
|
});
|
2015-01-15 22:00:27 +01:00
|
|
|
|
|
|
|
describe("inv", function() {
|
|
|
|
it("should encode and decode", function() {
|
|
|
|
var vect1 = inv_vect.encode(Buffer("test"));
|
|
|
|
var vect2 = inv_vect.encode(Buffer("test2"));
|
|
|
|
var inventory = [vect1, vect2];
|
|
|
|
var res = inv.decode(inv.encode(inventory));
|
|
|
|
expect(res.inventory.length).to.equal(2);
|
|
|
|
expect(bufferEqual(res.inventory[0], vect1)).to.be.true;
|
|
|
|
expect(bufferEqual(res.inventory[1], vect2)).to.be.true;
|
|
|
|
expect(res.length).to.equal(65);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("shouldn't encode/decode more than 1000 entires", function() {
|
|
|
|
expect(inv.encode.bind(null, Array(60000))).to.throw(/too many/i);
|
|
|
|
expect(inv.decode.bind(null, var_int.encode(60000))).to.throw(/too many/i);
|
|
|
|
});
|
|
|
|
});
|
2015-01-15 18:13:02 +01:00
|
|
|
});
|
|
|
|
|
2014-12-29 20:59:18 +01:00
|
|
|
describe("WIF", function() {
|
|
|
|
var wifSign = "5JgQ79vTBusc61xYPtUEHYQ38AXKdDZgQ5rFp7Cbb4ZjXUKFZEV";
|
|
|
|
var wifEnc = "5K2aL8cnsEWHwHfHnUrPo8QdYyRfoYUBmhAnWY5GTpDLbeyusnE";
|
|
|
|
var signPrivateKey = Buffer("71c95d26c716a5e85e9af9efe26fb5f744dc98005a13d05d23ee92c77e038d9f", "hex");
|
|
|
|
var encPrivateKey = Buffer("9f9969c93c2d186787a7653f70e49be34c03c4a853e6ad0c867db0946bc433c6", "hex");
|
|
|
|
|
|
|
|
it("should decode", function() {
|
2015-01-03 15:52:27 +01:00
|
|
|
var key1 = WIF.decode(wifSign);
|
|
|
|
expect(Buffer.isBuffer(key1)).to.be.true;
|
|
|
|
expect(key1.length).to.equal(32);
|
|
|
|
expect(key1.toString("hex")).to.equal(signPrivateKey.toString("hex"));
|
|
|
|
var key2 = WIF.decode(wifEnc);
|
|
|
|
expect(Buffer.isBuffer(key2)).to.be.true;
|
|
|
|
expect(key2.length).to.equal(32);
|
|
|
|
expect(key2.toString("hex")).to.equal(encPrivateKey.toString("hex"));
|
|
|
|
var addrStr = Address({signPrivateKey: key1, encPrivateKey: key2}).encode();
|
|
|
|
expect(addrStr).to.equal("BM-2cTux3PGRqHTEH6wyUP2sWeT4LrsGgy63z");
|
2014-12-29 20:59:18 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should encode", function() {
|
2015-01-03 15:52:27 +01:00
|
|
|
var wif1 = WIF.encode(signPrivateKey);
|
|
|
|
expect(wif1).to.equal(wifSign);
|
|
|
|
var wif2 = WIF.encode(encPrivateKey);
|
|
|
|
expect(wif2).to.equal(wifEnc);
|
2014-12-29 20:59:18 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-01-07 22:28:13 +01:00
|
|
|
describe("POW", function() {
|
|
|
|
it("should calculate target", function() {
|
|
|
|
expect(POW.getTarget({ttl: 2418984, payloadLength: 628, nonceTrialsPerByte: 1000, payloadLengthExtraBytes: 1000})).to.equal(297422593171);
|
|
|
|
expect(POW.getTarget({ttl: 86400, payloadLength: 628})).to.equal(4864647698763);
|
|
|
|
});
|
2015-01-08 14:47:29 +01:00
|
|
|
|
|
|
|
it("should check a POW", function() {
|
|
|
|
expect(POW.check({nonce: 21997550, target: 297422593171, initialHash: Buffer("8ff2d685db89a0af2e3dbfd3f700ae96ef4d9a1eac72fd778bbb368c7510cddda349e03207e1c4965bd95c6f7265e8f1a481a08afab3874eaafb9ade09a10880", "hex")})).to.be.true;
|
|
|
|
expect(POW.check({nonce: 3122437, target: 4864647698763, initialHash: Buffer("8ff2d685db89a0af2e3dbfd3f700ae96ef4d9a1eac72fd778bbb368c7510cddda349e03207e1c4965bd95c6f7265e8f1a481a08afab3874eaafb9ade09a10880", "hex")})).to.be.true;
|
|
|
|
expect(POW.check({nonce: 3122436, target: 4864647698763, initialHash: Buffer("8ff2d685db89a0af2e3dbfd3f700ae96ef4d9a1eac72fd778bbb368c7510cddda349e03207e1c4965bd95c6f7265e8f1a481a08afab3874eaafb9ade09a10880", "hex")})).to.be.false;
|
|
|
|
});
|
2015-01-09 14:12:23 +01:00
|
|
|
|
2015-01-12 19:27:14 +01:00
|
|
|
it("should reject promise on bad POW arguments", function(done) {
|
|
|
|
POW.doAsync({target: 123, initialHash: {}}).catch(function() {
|
|
|
|
POW.doAsync({target: 123, initialHash: Buffer("test")}).catch(function() {
|
|
|
|
POW.doAsync({poolSize: -1, target: 123, initialHash: Buffer(64)})
|
|
|
|
.catch(function() {
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2015-01-11 16:27:14 +01:00
|
|
|
});
|
|
|
|
|
2015-01-09 22:36:42 +01:00
|
|
|
if (allTests) {
|
2015-01-09 14:12:23 +01:00
|
|
|
it("should do a POW", function() {
|
2015-01-10 14:00:08 +01:00
|
|
|
this.timeout(300000);
|
2015-01-10 23:44:09 +01:00
|
|
|
var target = typeof window === "undefined" ? 297422593171 : 10693764680411;
|
2015-01-12 19:46:36 +01:00
|
|
|
var initialHash = Buffer("8ff2d685db89a0af2e3dbfd3f700ae96ef4d9a1eac72fd778bbb368c7510cddda349e03207e1c4965bd95c6f7265e8f1a481a08afab3874eaafb9ade09a10880", "hex");
|
|
|
|
return POW.doAsync({target: target, initialHash: initialHash})
|
|
|
|
.then(function(nonce) {
|
|
|
|
// FIXME(Kagami): Chromium behaves very strangely on Travis CI:
|
|
|
|
// computed nonces may vary in a big range (since target is
|
|
|
|
// simple, there are a lot of valid nonces). Probably because
|
|
|
|
// some spawned web workers get blocked for some reason.
|
|
|
|
if (typeof window === "undefined") {
|
|
|
|
expect(nonce).to.equal(21997550);
|
|
|
|
}
|
|
|
|
expect(POW.check({
|
|
|
|
nonce: nonce,
|
|
|
|
target: target,
|
|
|
|
initialHash: initialHash,
|
|
|
|
})).to.be.true;
|
|
|
|
});
|
2015-01-09 14:12:23 +01:00
|
|
|
});
|
|
|
|
}
|
2015-01-07 22:28:13 +01:00
|
|
|
});
|
|
|
|
|
2015-01-06 12:06:15 +01:00
|
|
|
describe("High-level classes", function() {
|
|
|
|
// FIXME(Kagami): Add more fail tests.
|
|
|
|
describe("Address", function() {
|
|
|
|
it("should decode Bitmessage address", function() {
|
|
|
|
var addr = Address.decode("BM-2cTux3PGRqHTEH6wyUP2sWeT4LrsGgy63z")
|
|
|
|
expect(addr.version).to.equal(4);
|
|
|
|
expect(addr.stream).to.equal(1);
|
|
|
|
expect(addr.ripe.toString("hex")).to.equal("003ab6655de4bd8c603eba9b00dd5970725fdd56");
|
|
|
|
});
|
2014-12-27 16:17:41 +01:00
|
|
|
|
2015-01-06 12:06:15 +01:00
|
|
|
it("should decode Bitmessage address badly formatted", function() {
|
|
|
|
var addr = Address.decode(" 2cTux3PGRqHTEH6wyUP2sWeT4LrsGgy63z ")
|
|
|
|
expect(addr.version).to.equal(4);
|
|
|
|
expect(addr.stream).to.equal(1);
|
|
|
|
expect(addr.ripe.toString("hex")).to.equal("003ab6655de4bd8c603eba9b00dd5970725fdd56");
|
|
|
|
});
|
2014-12-27 16:17:41 +01:00
|
|
|
|
2015-01-06 12:06:15 +01:00
|
|
|
it("should allow to generate new Bitmessage address", function() {
|
2015-01-10 14:00:08 +01:00
|
|
|
this.timeout(60000);
|
2015-01-06 12:06:15 +01:00
|
|
|
var addr = Address.fromRandom();
|
|
|
|
expect(addr.version).to.equal(4);
|
|
|
|
expect(addr.stream).to.equal(1);
|
|
|
|
expect(addr.signPrivateKey.length).to.equal(32);
|
|
|
|
expect(addr.encPrivateKey.length).to.equal(32);
|
|
|
|
var str = addr.encode();
|
|
|
|
expect(str.slice(0, 3)).to.equal("BM-");
|
|
|
|
var addr2 = Address.decode(str);
|
|
|
|
expect(addr2.version).to.equal(4);
|
|
|
|
expect(addr2.stream).to.equal(1);
|
|
|
|
expect(addr2.ripe.length).to.equal(20);
|
|
|
|
expect(addr2.ripe[0]).to.equal(0);
|
2014-12-27 16:17:41 +01:00
|
|
|
});
|
2015-01-06 12:06:15 +01:00
|
|
|
|
2015-01-07 22:28:13 +01:00
|
|
|
// FIXME(Kagami): Don't run it in browser currently because it's
|
|
|
|
// very slow. This need to be fixed.
|
|
|
|
if (allTests && typeof window === "undefined") {
|
2015-01-06 12:06:15 +01:00
|
|
|
it("should allow to generate shorter address", function() {
|
2015-01-10 14:00:08 +01:00
|
|
|
this.timeout(300000);
|
2015-01-06 12:06:15 +01:00
|
|
|
var addr = Address.fromRandom({ripelen: 18});
|
|
|
|
var ripe = addr.getRipe({short: true});
|
|
|
|
expect(ripe.length).to.be.at.most(18);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2015-01-11 18:24:39 +01:00
|
|
|
|
2015-01-15 22:00:27 +01:00
|
|
|
describe("UserAgent", function() {
|
2015-01-11 18:24:39 +01:00
|
|
|
var pybm = {name: "PyBitmessage", version: "0.4.4"};
|
|
|
|
var bnode = {name: "bitchan-node", version: "0.0.1"};
|
|
|
|
var bweb = {name: "bitchan-web"};
|
|
|
|
|
|
|
|
it("should decode", function() {
|
|
|
|
var ua = var_str.encode("/cBitmessage:0.2(iPad; U; CPU OS 3_2_1)/AndroidBuild:0.8/");
|
|
|
|
var res = UserAgent.decode(ua);
|
|
|
|
expect(res.software).to.deep.equal([
|
|
|
|
{name: "cBitmessage", version: "0.2", comments: "iPad; U; CPU OS 3_2_1"},
|
|
|
|
{name: "AndroidBuild", version: "0.8"},
|
|
|
|
]);
|
|
|
|
expect(res.length).to.equal(58);
|
|
|
|
expect(res.rest.toString("hex")).to.equal("");
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should encode", function() {
|
|
|
|
var ua = UserAgent.encode([pybm]);
|
|
|
|
expect(var_str.decode(ua).str).to.equal("/PyBitmessage:0.4.4/");
|
|
|
|
var res = UserAgent.decode(ua);
|
|
|
|
expect(res.software).to.deep.equal([pybm]);
|
|
|
|
expect(res.length).to.equal(21);
|
|
|
|
expect(res.rest.toString("hex")).to.equal("");
|
2015-01-11 18:28:12 +01:00
|
|
|
|
|
|
|
ua = UserAgent.encode([{name: "test", "comments": "linux"}]);
|
|
|
|
expect(var_str.decode(ua).str).to.equal("/test:0.0.0(linux)/");
|
2015-01-11 18:24:39 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should encode bitmessage's user agent", function() {
|
|
|
|
var res = UserAgent.decode(UserAgent.encodeSelf())
|
|
|
|
var software = res.software;
|
|
|
|
expect(software[0].name).to.equal("bitmessage");
|
|
|
|
expect(software[0]).to.have.property("version");
|
|
|
|
|
|
|
|
res = UserAgent.decode(UserAgent.encodeSelfWith([bnode, bweb]));
|
|
|
|
software = res.software;
|
|
|
|
expect(software[0].name).to.equal("bitmessage");
|
|
|
|
expect(software[1]).to.deep.equal(bnode);
|
|
|
|
expect(software[2].name).to.equal(bweb.name);
|
|
|
|
expect(software[2].version).to.equal("0.0.0");
|
|
|
|
});
|
2015-01-15 15:28:52 +01:00
|
|
|
|
|
|
|
it("should accept just object or string(s) on encode", function() {
|
|
|
|
var enc1 = UserAgent.encode({name: "test", version: "0.0.1"});
|
|
|
|
var enc2 = UserAgent.encode("test:0.0.1");
|
|
|
|
var res = [{name: "test", version: "0.0.1"}];
|
|
|
|
expect(UserAgent.decode(enc1).software).to.deep.equal(res);
|
|
|
|
expect(UserAgent.decode(enc2).software).to.deep.equal(res);
|
|
|
|
var enc3 = UserAgent.encodeSelfWith("test:0.0.1");
|
|
|
|
var software = UserAgent.decode(enc3).software;
|
|
|
|
expect(software[0].name).to.equal("bitmessage");
|
|
|
|
expect(software[1]).to.deep.equal(res[0]);
|
|
|
|
});
|
2015-01-11 18:24:39 +01:00
|
|
|
});
|
2014-12-13 19:56:14 +01:00
|
|
|
});
|