Skip to content

Commit

Permalink
Extract common tests for decoders
Browse files Browse the repository at this point in the history
  • Loading branch information
jmaxxz committed Mar 27, 2015
1 parent 6af6a51 commit 05ccfd2
Show file tree
Hide file tree
Showing 8 changed files with 172 additions and 179 deletions.
11 changes: 11 additions & 0 deletions spec/decode/decode.js
@@ -0,0 +1,11 @@
var should = require("should");
exports.shouldBehaveLikeADecoder = function(){
it("is a function", function(){
this.instance.decode.should.be.type("function");
});

it("returns the instance", function(){
var result = this.instance.decode(this.example, 0, this.example.length);
should(result).be.exactly(this.instance);
});
};
28 changes: 13 additions & 15 deletions spec/decode/icmp.spec.js
@@ -1,41 +1,39 @@
var Icmp = require("../../decode/icmp");
var util = require("../../util");
var shouldBehaveLikeADecoder = require("./decode").shouldBehaveLikeADecoder;
require("should");

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

describe("#decode()", function(){
it("is a function", function(){
instance.decode.should.be.type("function");
});
shouldBehaveLikeADecoder();

it("sets the #type to the ICMP type", function() {
instance.decode(exampleIcmp, 0);
instance.should.have.property("type", 1);
this.instance.decode(this.example, 0);
this.instance.should.have.property("type", 1);
});

it("sets the #code to the ICMP subtype", function() {
instance.decode(exampleIcmp, 0);
instance.should.have.property("code", 2);
this.instance.decode(this.example, 0);
this.instance.should.have.property("code", 2);
});

it("sets the #checksum to the decoded checksum for the header and data", function() {
instance.decode(exampleIcmp, 0);
instance.should.have.property("checksum", 772);
this.instance.decode(this.example, 0);
this.instance.should.have.property("checksum", 772);
});
});

describe("#toString()", function() {
var verifyToString = function verifyToString(type, code, result){
it("return \""+result+"\" for icmp of type="+type+" code="+code, function(){
instance = new Icmp();
instance.decode(new Buffer(util.int8_to_hex[type] + util.int8_to_hex[code] + "0000", "hex"), 0);
instance.toString().should.be.exactly(result);
this.instance = new Icmp();
this.instance.decode(new Buffer(util.int8_to_hex[type] + util.int8_to_hex[code] + "0000", "hex"), 0);
this.instance.toString().should.be.exactly(result);
});
};

Expand Down
52 changes: 25 additions & 27 deletions spec/decode/igmp.spec.js
@@ -1,37 +1,35 @@
var Igmp = require("../../decode/igmp");
var util = require("../../util");
var shouldBehaveLikeADecoder = require("./decode").shouldBehaveLikeADecoder;
require("should");

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

describe("#decode()", function(){
it("is a function", function(){
instance.decode.should.be.type("function");
});
shouldBehaveLikeADecoder();

it("sets the #type to the IGMP type", function() {
instance.decode(exampleIgmp, 0);
instance.should.have.property("type", 1);
this.instance.decode(this.example, 0);
this.instance.should.have.property("type", 1);
});

it("sets the #maxResponseTime", function() {
instance.decode(exampleIgmp, 0);
instance.should.have.property("maxResponseTime", 2);
this.instance.decode(this.example, 0);
this.instance.should.have.property("maxResponseTime", 2);
});

it("sets the #checksum", function() {
instance.decode(exampleIgmp, 0);
instance.should.have.property("checksum", 772);
this.instance.decode(this.example, 0);
this.instance.should.have.property("checksum", 772);
});

it("sets the #groupAddress", function() {
instance.decode(exampleIgmp, 0);
instance.groupAddress.should.have.property("addr", [5, 6, 7, 8]);
this.instance.decode(this.example, 0);
this.instance.groupAddress.should.have.property("addr", [5, 6, 7, 8]);
});

describe("when IGMP is type 0x11", function(){
Expand All @@ -41,8 +39,8 @@ describe("IGMP", function(){
});

it("sets #version to 3", function(){
instance.decode(packet, 0);
instance.should.have.property("version", 3);
this.instance.decode(packet, 0);
this.instance.should.have.property("version", 3);
});
});

Expand All @@ -53,8 +51,8 @@ describe("IGMP", function(){
});

it("sets #version to 1", function(){
instance.decode(packet, 0);
instance.should.have.property("version", 1);
this.instance.decode(packet, 0);
this.instance.should.have.property("version", 1);
});
});

Expand All @@ -65,8 +63,8 @@ describe("IGMP", function(){
});

it("sets #version to 2", function(){
instance.decode(packet, 0);
instance.should.have.property("version", 2);
this.instance.decode(packet, 0);
this.instance.should.have.property("version", 2);
});
});

Expand All @@ -77,8 +75,8 @@ describe("IGMP", function(){
});

it("sets #version to 2", function(){
instance.decode(packet, 0);
instance.should.have.property("version", 2);
this.instance.decode(packet, 0);
this.instance.should.have.property("version", 2);
});
});
describe("when IGMP is type 0x22", function(){
Expand All @@ -88,18 +86,18 @@ describe("IGMP", function(){
});

it("sets #version to 3", function(){
instance.decode(packet, 0);
instance.should.have.property("version", 3);
this.instance.decode(packet, 0);
this.instance.should.have.property("version", 3);
});
});
});

describe("#toString()", function() {
var verifyToString = function verifyToString(type, result){
it("return \""+result+"\" for igmp of type="+type, function(){
instance = new Igmp();
instance.decode(new Buffer(util.int8_to_hex[type] + "000000000000", "hex"), 0);
instance.toString().should.be.exactly(result);
this.instance = new Igmp();
this.instance.decode(new Buffer(util.int8_to_hex[type] + "000000000000", "hex"), 0);
this.instance.toString().should.be.exactly(result);
});
};

Expand Down
73 changes: 33 additions & 40 deletions spec/decode/ipv4.spec.js
@@ -1,82 +1,75 @@
var IPv4 = require("../../decode/ipv4");
var shouldBehaveLikeADecoder = require("./decode").shouldBehaveLikeADecoder;
require("should");

describe("IPv4", function(){
var example, instance;
beforeEach(function () {
example = new Buffer("46c000200000400001021274c0a82101effffffa94040000" + //header
this.example = new Buffer("46c000200000400001021274c0a82101effffffa94040000" + //header
"1600fa04effffffa" + //igmpv2
"00000000", //checksum
"hex");
instance = new IPv4();
this.instance = new IPv4();
});

describe("#decode", function(){
it("is a function", function(){
instance.decode.should.be.type("function");
});
shouldBehaveLikeADecoder();

it("sets #version to 4", function() { //After all this is ip "v4"
instance.decode(example, 0);
instance.should.have.property("version", 4);
this.instance.decode(this.example, 0);
this.instance.should.have.property("version", 4);
});

it("sets #headerLength to the header length", function() { //After all this is ip "v4"
instance.decode(example, 0);
instance.should.have.property("headerLength", 24);
this.instance.decode(this.example, 0);
this.instance.should.have.property("headerLength", 24);
});

it("sets #diffserv to the differentied services field", function() { //After all this is ip "v4"
instance.decode(example, 0);
instance.should.have.property("diffserv", 0xc0);
this.instance.decode(this.example, 0);
this.instance.should.have.property("diffserv", 0xc0);
});

it("sets #flags to a decoded version of the ip flags", function() {
instance.decode(example, 0);
instance.flags.should.be.property("reserved", false);
instance.flags.should.be.property("doNotFragment", true);
instance.flags.should.be.property("moreFragments", false);
this.instance.decode(this.example, 0);
this.instance.flags.should.be.property("reserved", false);
this.instance.flags.should.be.property("doNotFragment", true);
this.instance.flags.should.be.property("moreFragments", false);
});

it("sets #fragmentOffset to the fragmentation offset", function(){
instance.decode(example, 0);
instance.should.have.property("fragmentOffset", 0);
this.instance.decode(this.example, 0);
this.instance.should.have.property("fragmentOffset", 0);
});

it("sets #ttl to the time to live", function(){
instance.decode(example, 0);
instance.should.have.property("ttl", 1);
this.instance.decode(this.example, 0);
this.instance.should.have.property("ttl", 1);
});

it("sets #protocol to the time to live", function(){
instance.decode(example, 0);
instance.should.have.property("protocol", 2);
this.instance.decode(this.example, 0);
this.instance.should.have.property("protocol", 2);
});

it("sets #headerChecksum to the checksum of the header", function(){
instance.decode(example, 0);
instance.should.have.property("headerChecksum", 0x1274);
this.instance.decode(this.example, 0);
this.instance.should.have.property("headerChecksum", 0x1274);
});

it("sets #saddr to the senders address", function(){
instance.decode(example, 0);
instance.saddr.should.have.property("addr", [192, 168, 33, 1]);
this.instance.decode(this.example, 0);
this.instance.saddr.should.have.property("addr", [192, 168, 33, 1]);
});

it("sets #daddr to the destination address", function(){
instance.decode(example, 0);
instance.daddr.should.have.property("addr", [239, 255, 255, 250]);
this.instance.decode(this.example, 0);
this.instance.daddr.should.have.property("addr", [239, 255, 255, 250]);
});
});

describe("#toString()", function(){
var instance;
beforeEach(function(){
instance = new IPv4();
});

it("is a function", function(){
instance.toString.should.be.type("function");
this.instance.toString.should.be.type("function");
});

it("returns a value like \"192.168.33.1 -> 239.255.255.250 IGMP Membership Report\" when no flags are set", function(){
Expand All @@ -85,23 +78,23 @@ describe("IPv4", function(){
"00000000", //checksum
"hex");

instance.decode(noflags, 0);
this.instance.decode(noflags, 0);

instance.toString().should.be.exactly("192.168.33.1 -> 239.255.255.250 IGMP Membership Report");
this.instance.toString().should.be.exactly("192.168.33.1 -> 239.255.255.250 IGMP Membership Report");
});

it("returns a value like \"192.168.33.1 -> 239.255.255.250 flags [d] IGMP Membership Report\" when flags are set", function() {
instance.decode(example, 0);
instance.toString().should.be.exactly("192.168.33.1 -> 239.255.255.250 flags [d] IGMP Membership Report");
this.instance.decode(this.example, 0);
this.instance.toString().should.be.exactly("192.168.33.1 -> 239.255.255.250 flags [d] IGMP Membership Report");
});

it("returns a value like \"192.168.33.1 -> 239.255.255.250 flags [d] proto 255 undefined\" when the protocol is not support by node_pcap", function() {
var unknownProtocol = new Buffer("46c000200000400001ff1274c0a82101effffffa94040000" + //header
"00000000", //checksum
"hex");

instance.decode(unknownProtocol, 0);
instance.toString().should.be.exactly("192.168.33.1 -> 239.255.255.250 flags [d] proto 255 undefined");
this.instance.decode(unknownProtocol, 0);
this.instance.toString().should.be.exactly("192.168.33.1 -> 239.255.255.250 flags [d] proto 255 undefined");
});
});
});

0 comments on commit 05ccfd2

Please sign in to comment.