Skip to content

Commit

Permalink
Add spec for ICMP decoder
Browse files Browse the repository at this point in the history
  - Remove fallacious fields from ICMP class
  • Loading branch information
jmaxxz committed Mar 3, 2015
1 parent df10a14 commit 19c769f
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 10 deletions.
16 changes: 6 additions & 10 deletions decode/icmp.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
function ICMP() {
this.type = null;
this.code = null;
this.checksum = null;
this.id = null;
this.sequence = null;
this.type = undefined;
this.code = undefined;
this.checksum = undefined;
}

// http://en.wikipedia.org/wiki/Internet_Control_Message_Protocol
ICMP.prototype.decode = function (raw_packet, offset) {
this.type = raw_packet[offset];
this.code = raw_packet[offset + 1];
this.checksum = raw_packet.readUInt16BE(offset + 2); // 2, 3
this.id = raw_packet.readUInt16BE(offset + 4); // 4, 5
this.sequence = raw_packet.readUInt16BE(offset + 6); // 6, 7
this.type = raw_packet[offset++];
this.code = raw_packet[offset++];
this.checksum = raw_packet.readUInt16BE(offset); // 2, 3

return this;
};
Expand Down
31 changes: 31 additions & 0 deletions spec/decode/icmp.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
var Icmp = require("../../decode/icmp");
require("should");

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

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

it("sets the #type to the ICMP type", function() {
instance.decode(exampleIcmp, 0);
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);
});

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

0 comments on commit 19c769f

Please sign in to comment.