bitmessage-js/tests/functional.js

145 lines
3.9 KiB
JavaScript
Raw Normal View History

2015-02-05 20:55:36 +01:00
var expect = require("chai").expect;
2015-02-02 16:30:05 +01:00
var bitmessage = require("../lib");
var structs = bitmessage.structs;
var message = structs.message;
2015-02-06 10:47:40 +01:00
var WsTransport = require("../lib/net/ws");
2015-02-02 16:30:05 +01:00
2015-02-05 19:38:36 +01:00
var TcpTransport, tcp;
2015-02-02 16:30:05 +01:00
if (!process.browser) {
2015-02-06 10:47:40 +01:00
TcpTransport = require("../lib/net/tcp");
2015-02-02 16:30:05 +01:00
describe("TCP transport", function() {
2015-02-05 19:38:36 +01:00
before(function(done) {
tcp = new TcpTransport();
tcp.on("error", function(err) {
2015-02-09 22:56:55 +01:00
console.log("TCP transport error: " + err);
2015-02-05 19:38:36 +01:00
});
2015-02-09 22:56:55 +01:00
tcp.on("warning", function(warn) {
console.log("TCP transport warning: " + warn);
});
// Wait some time for the server.
setTimeout(done, 300);
2015-02-05 19:38:36 +01:00
});
2015-02-05 20:55:36 +01:00
it("should return nothing on bootstrap by default", function() {
return tcp.bootstrap().then(function(nodes) {
expect(nodes).to.be.empty;
});
});
it("should resolve DNS seeds on bootstrap", function() {
2015-02-06 19:10:50 +01:00
this.timeout(10000);
2015-02-05 20:55:36 +01:00
var tcp2 = new TcpTransport({
dnsSeeds: [["bootstrap8444.bitmessage.org", 8444]],
});
return tcp2.bootstrap().then(function(nodes) {
expect(nodes).to.be.not.empty;
expect(nodes[0][1]).to.be.equal(8444);
});
});
it("should return hardcoded seeds on bootstrap", function() {
2015-02-06 19:10:50 +01:00
this.timeout(10000);
2015-02-05 20:55:36 +01:00
var tcp3 = new TcpTransport({
seeds: [["1.1.1.1", 8080]],
dnsSeeds: [["bootstrap8444.bitmessage.org", 8444]],
});
return tcp3.bootstrap().then(function(nodes) {
expect(nodes).to.have.length.at.least(2);
expect(nodes[0][1]).to.be.equal(8444);
expect(nodes[nodes.length - 1][0]).to.equal("1.1.1.1");
expect(nodes[nodes.length - 1][1]).to.equal(8080);
});
});
2015-02-05 19:38:36 +01:00
it("should allow to interconnect two nodes", function(done) {
tcp.connect(22333, "127.0.0.1");
2015-02-09 15:38:10 +01:00
tcp.once("open", function() {
2015-02-05 19:38:36 +01:00
done();
});
});
2015-02-09 22:56:55 +01:00
it("should automatically establish connection", function(done) {
2015-02-09 15:38:10 +01:00
tcp.once("established", function() {
done();
});
});
it("should allow to communicate", function(done) {
tcp.on("message", function cb(command, payload) {
if (command === "echo-res") {
expect(payload.toString()).to.equal("test");
tcp.removeListener("message", cb);
done();
}
});
tcp.send("echo-req", Buffer("test"));
});
2015-02-05 19:38:36 +01:00
it("should allow to close connection", function(done) {
tcp.close();
2015-02-09 15:38:10 +01:00
tcp.once("close", function() {
2015-02-05 19:38:36 +01:00
done();
});
});
2015-02-02 16:30:05 +01:00
});
}
describe("WebSocket transport", function() {
2015-02-09 22:56:55 +01:00
var ws;
before(function(done) {
ws = new WsTransport();
ws.on("error", function(err) {
console.log("WebSocket transport error: " + err);
});
ws.on("warning", function(warn) {
console.log("WebSocket transport warning: " + warn);
});
// Wait some time for the server.
setTimeout(done, 300);
});
it("should return hardcoded seeds on bootstrap", function() {
var ws2 = new WsTransport({seeds: [["ws.example.com", 8080]]});
return ws2.bootstrap().then(function(nodes) {
expect(nodes).to.have.length(1);
expect(nodes[0][0]).to.be.equal("ws.example.com");
expect(nodes[0][1]).to.be.equal(8080);
});
});
it("should allow to interconnect two nodes", function(done) {
ws.connect("ws://127.0.0.1:22334");
ws.once("open", function() {
done();
});
});
it("should automatically establish connection", function(done) {
ws.once("established", function() {
done();
});
});
it("should allow to communicate", function(done) {
ws.on("message", function cb(command, payload) {
if (command === "echo-res") {
expect(payload.toString()).to.equal("test");
ws.removeListener("message", cb);
done();
}
});
ws.send("echo-req", Buffer("test"));
});
it("should allow to close connection", function(done) {
ws.close();
ws.once("close", function() {
done();
});
});
2015-02-02 16:30:05 +01:00
});