Skip to content

Commit

Permalink
Add basic tests for IGMP packets
Browse files Browse the repository at this point in the history
  • Loading branch information
jmaxxz committed Mar 5, 2015
1 parent 7c9ea23 commit 31b7547
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 23 deletions.
34 changes: 20 additions & 14 deletions decode/igmp.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,44 @@
var IPV4Addr = require("./ipv4_addr");

function IGMP() {
this.type = null;
this.version = null;
this.max_response_time = null;
this.checksum = null;
this.group_address = null;
this.type = undefined;
this.version = undefined;
this.maxResponseTime = undefined;
this.checksum = undefined;
this.groupAddress = undefined;
}

var IPV4Addr = require("./ipv4_addr");

// http://en.wikipedia.org/wiki/Internet_Group_Management_Protocol
// This is an implementation of V3
// https://tools.ietf.org/html/rfc3376
IGMP.prototype.decode = function (raw_packet, offset) {
this.type = raw_packet[offset];
this.max_response_time = raw_packet[offset + 1];

// units are 1/10s
// if value < 128 this is an int, else it is a float
// right now we don't handle the float version
this.maxResponseTime = raw_packet[offset + 1];

this.checksum = raw_packet.readUInt16BE(offset + 2); // 2, 3
this.group_address = new IPV4Addr(raw_packet, offset + 4); // 4, 5, 6, 7
this.groupAddress = new IPV4Addr(raw_packet, offset + 4); // 4, 5, 6, 7

//Membership Query (0x11)
//Membership Report (IGMPv1: 0x12, IGMPv2: 0x16, IGMPv3: 0x22)
//Leave Group (0x17)
switch (this.type) {
case 0x11:
this.version = this.max_response_time > 0 ? 2 : 1;
this.version = 3;
break;
case 0x12:
this.version = 1;
break;
case 0x16:
this.version = 2;
break;
case 0x17:
this.version = 2;
break;
case 0x22:
this.version = 3;
break;
default:
break;
}

return this;
Expand Down
11 changes: 6 additions & 5 deletions decode/ipv4_addr.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
var dec = require("../util").int8_to_dec;

function IPv4Addr(raw_packet, offset) {
this.o1 = raw_packet[offset];
this.o2 = raw_packet[offset + 1];
this.o3 = raw_packet[offset + 2];
this.o4 = raw_packet[offset + 3];
this.addr = new Array(4);
this.addr[0] = raw_packet[offset];
this.addr[1] = raw_packet[offset + 1];
this.addr[2] = raw_packet[offset + 2];
this.addr[3] = raw_packet[offset + 3];
}

// Don't use Array.prototype.join here, because string concat is much faster
IPv4Addr.prototype.toString = function () {
return dec[this.o1] + "." + dec[this.o2] + "." + dec[this.o3] + "." + dec[this.o4];
return dec[this.addr[0]] + "." + dec[this.addr[1]] + "." + dec[this.addr[2]] + "." + dec[this.addr[3]];
};

module.exports = IPv4Addr;
57 changes: 57 additions & 0 deletions spec/decode/igmp.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
var Igmp = require("../../decode/igmp");
var util = require("../../util");
require("should");

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

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

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

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

it("sets the #checksum", function() {
instance.decode(exampleIgmp, 0);
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]);
});
});

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);
});
};

//verifyToString(type, string)
verifyToString(0x11, "Membership Query");
verifyToString(0x12, "Membership Report");
verifyToString(0x16, "Membership Report");
verifyToString(0x17, "Leave Group");
verifyToString(0x22, "Membership Report");

//Default handler
verifyToString(0x01, "type 1");
});
});
5 changes: 1 addition & 4 deletions spec/decode/ipv4_addr.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@ describe("IPv4Addr", function(){
it("decodes ip address", function() {
var instance = new IPv4Addr(exampleIp, 0);

instance.should.have.property("o1", 1);
instance.should.have.property("o2", 2);
instance.should.have.property("o3", 3);
instance.should.have.property("o4", 4);
instance.should.have.property("addr", [1, 2, 3, 4]);
});
});

Expand Down

0 comments on commit 31b7547

Please sign in to comment.