Skip to content

Commit

Permalink
Add tests for event raising code
Browse files Browse the repository at this point in the history
  • Loading branch information
jmaxxz committed Mar 28, 2015
1 parent 5021b40 commit f59fc37
Show file tree
Hide file tree
Showing 15 changed files with 63 additions and 28 deletions.
3 changes: 3 additions & 0 deletions decode/arp.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ Arp.prototype.decode = function (raw_packet, offset) {
return this;
};

Arp.prototype.decoderName = "arp";
Arp.prototype.eventsOnDecode = true;

Arp.prototype.toString = function () {
var ret = "";
if (this.operation === 1) {
Expand Down
3 changes: 3 additions & 0 deletions decode/icmp.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ ICMP.prototype.decode = function (raw_packet, offset) {
return this;
};

ICMP.prototype.decoderName = "icmp";
ICMP.prototype.eventsOnDecode = true;

ICMP.prototype.toString = function () {
var ret = "";

Expand Down
3 changes: 3 additions & 0 deletions decode/igmp.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ IGMP.prototype.decode = function (raw_packet, offset) {
return this;
};

IGMP.prototype.decoderName = "igmp";
IGMP.prototype.eventsOnDecode = true;

IGMP.prototype.toString = function () {
var ret;

Expand Down
3 changes: 3 additions & 0 deletions decode/ipv4.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ IPv4.prototype.decode = function (raw_packet, offset) {
return this;
};

IPv4.prototype.decoderName = "ipv4";
IPv4.prototype.eventsOnDecode = true;

IPv4.prototype.toString = function () {
var ret = this.saddr + " -> " + this.daddr + " ";
var flags = this.flags.toString();
Expand Down
3 changes: 3 additions & 0 deletions decode/ipv6.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ IPv6.prototype.decode = function (raw_packet, offset) {
return this;
};

IPv6.prototype.decoderName = "ipv6";
IPv6.prototype.eventsOnDecode = true;

IPv6.prototype.toString = function () {
var ret = this.saddr + " -> " + this.daddr + " ";

Expand Down
3 changes: 3 additions & 0 deletions decode/llc_packet.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,7 @@ LogicalLinkControl.prototype.decode = function (raw_packet, offset) {
return this;
};

LogicalLinkControl.prototype.decoderName = "llc";
LogicalLinkControl.prototype.eventsOnDecode = true;

module.exports = LogicalLinkControl;
2 changes: 2 additions & 0 deletions decode/tcp.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,8 @@ function TCP(emitter) {
// for (var i = orig_offset; i < orig_offset + len ; i++) {
// console.log((i - orig_offset) + " / " + i + ": " + raw_packet[i] + " " + String.fromCharCode(raw_packet[i]));
// }
TCP.prototype.decoderName = "tcp";
TCP.prototype.eventsOnDecode = true;

// http://en.wikipedia.org/wiki/Transmission_Control_Protocol
TCP.prototype.decode = function (raw_packet, offset, len) {
Expand Down
3 changes: 3 additions & 0 deletions decode/udp.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ UDP.prototype.decode = function (raw_packet, offset) {
return this;
};

UDP.prototype.decoderName = "udp";
UDP.prototype.eventsOnDecode = true;

UDP.prototype.toString = function () {
var ret = "UDP " + this.sport + "->" + this.dport + " len " + this.length;
if (this.sport === 53 || this.dport === 53) {
Expand Down
18 changes: 17 additions & 1 deletion spec/decode/decode.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var should = require("should");
exports.shouldBehaveLikeADecoder = function(){
var sinon = require("sinon");
exports.shouldBehaveLikeADecoder = function(decoderName, raisesEvent){
it("is a function", function(){
this.instance.decode.should.be.type("function");
});
Expand All @@ -8,4 +9,19 @@ exports.shouldBehaveLikeADecoder = function(){
var result = this.instance.decode(this.example, 0, this.example.length);
should(result).be.exactly(this.instance);
});

if(raisesEvent) {
it("raises a " + decoderName + " event on decode", function() {
// This is a bit of a special case so we need
// to rewire some of the variables used in
// other tests.
var eventHandler = sinon.spy();
this.eventEmitter.on(this.instance.decoderName, eventHandler);

// Decode
this.instance.decode(this.example, 0, this.example.length);

eventHandler.callCount.should.be.exactly(1);
});
}
};
6 changes: 4 additions & 2 deletions spec/decode/icmp.spec.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
var Icmp = require("../../decode/icmp");
var util = require("../../util");
var events = require("events");
var shouldBehaveLikeADecoder = require("./decode").shouldBehaveLikeADecoder;
require("should");

describe("ICMP", function(){
beforeEach(function () {
this.example = new Buffer("01020304", "hex");
this.instance = new Icmp();
this.eventEmitter = new events.EventEmitter();
this.instance = new Icmp(this.eventEmitter);
});

describe("#decode()", function(){
shouldBehaveLikeADecoder();
shouldBehaveLikeADecoder("icmp", true);

it("sets the #type to the ICMP type", function() {
this.instance.decode(this.example, 0);
Expand Down
6 changes: 4 additions & 2 deletions spec/decode/igmp.spec.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
var Igmp = require("../../decode/igmp");
var util = require("../../util");
var events = require("events");
var shouldBehaveLikeADecoder = require("./decode").shouldBehaveLikeADecoder;
require("should");

describe("IGMP", function(){
beforeEach(function () {
this.example = new Buffer("0102030405060708", "hex");
this.instance = new Igmp();
this.eventEmitter = new events.EventEmitter();
this.instance = new Igmp(this.eventEmitter);
});

describe("#decode()", function(){
shouldBehaveLikeADecoder();
shouldBehaveLikeADecoder("igmp", true);

it("sets the #type to the IGMP type", function() {
this.instance.decode(this.example, 0);
Expand Down
6 changes: 4 additions & 2 deletions spec/decode/ipv4.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var IPv4 = require("../../decode/ipv4");
var shouldBehaveLikeADecoder = require("./decode").shouldBehaveLikeADecoder;
var events = require("events");
require("should");

describe("IPv4", function(){
Expand All @@ -8,11 +9,12 @@ describe("IPv4", function(){
"1600fa04effffffa" + //igmpv2
"00000000", //checksum
"hex");
this.instance = new IPv4();
this.eventEmitter = new events.EventEmitter();
this.instance = new IPv4(this.eventEmitter);
});

describe("#decode", function(){
shouldBehaveLikeADecoder();
shouldBehaveLikeADecoder("ipv4", true);

it("sets #version to 4", function() { //After all this is ip "v4"
this.instance.decode(this.example, 0);
Expand Down
6 changes: 4 additions & 2 deletions spec/decode/ipv6.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
var IPv6 = require("../../decode/ipv6");
var NoNext = require("../../decode/ipv6headers/no_next");
var shouldBehaveLikeADecoder = require("./decode").shouldBehaveLikeADecoder;
var events = require("events");
require("should");

describe("IPv6", function(){
Expand All @@ -12,11 +13,12 @@ describe("IPv6", function(){
"fe80000000000000708dfe834114a512" + // src address
"2001000041379e508000f12ab9c82815", // dest address
"hex");
this.instance = new IPv6();
this.eventEmitter = new events.EventEmitter();
this.instance = new IPv6(this.eventEmitter);
});

describe("#decode", function(){
shouldBehaveLikeADecoder();
shouldBehaveLikeADecoder("ipv6", true);

it("sets #version to 6", function() { //After all this is ip "v6"
this.instance.decode(this.example, 0);
Expand Down
6 changes: 4 additions & 2 deletions spec/decode/llc_packet.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var LogicalLinkControl = require("../../decode/llc_packet");
var IPv4 = require("../../decode/ipv4");
var events = require("events");
var shouldBehaveLikeADecoder = require("./decode").shouldBehaveLikeADecoder;
var should = require("should");

Expand All @@ -9,11 +10,12 @@ describe("LogicalLinkControl", function(){
"46c000200000400001021274c0a82101effffffa94040000" + //ipv4 payload
"1600fa04effffffa" + //igmpv2
"00000000", "hex");
this.instance = new LogicalLinkControl();
this.eventEmitter = new events.EventEmitter();
this.instance = new LogicalLinkControl(this.eventEmitter);
});

describe("#decode", function(){
shouldBehaveLikeADecoder();
shouldBehaveLikeADecoder("llc", true);

it("sets #dsap to the destination service access point", function(){
this.instance.decode(this.example, 0);
Expand Down
20 changes: 3 additions & 17 deletions spec/decode/tcp.spec.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,17 @@
var TCP = require("../../decode/tcp");
var events = require("events");
var sinon = require("sinon");
var shouldBehaveLikeADecoder = require("./decode").shouldBehaveLikeADecoder;
require("should");

describe("TCP", function(){
beforeEach(function () {
this.example = new Buffer("b5dd00500aaf604e0000000060c2102044b2000002040218", "hex");
this.instance = new TCP();
this.eventEmitter = new events.EventEmitter();
this.instance = new TCP(this.eventEmitter);
});

describe("#decode()", function(){
it("raises a tcp event on decode", function() {
// This is a bit of a special case so we need
// to rewire some of the variables used in
// other tests.
var tcpHandler = sinon.spy();
var eventEmitter = new events.EventEmitter();
eventEmitter.on("tcp", tcpHandler);

// Decode
this.instance = new TCP(eventEmitter).decode(this.example, 0, 24);

tcpHandler.callCount.should.be.exactly(1);
});

shouldBehaveLikeADecoder();
shouldBehaveLikeADecoder("tcp", true);

it("sets #sport to the source port", function() {
this.instance.decode(this.example, 0);
Expand Down

0 comments on commit f59fc37

Please sign in to comment.