Skip to content

Commit

Permalink
Add tests for ipv6 decode function
Browse files Browse the repository at this point in the history
  -  follow javascript camel casing convention
     for oo style apis on ipv6 object
  • Loading branch information
jmaxxz committed Mar 10, 2015
1 parent 8609a20 commit fe514b1
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 10 deletions.
24 changes: 14 additions & 10 deletions decode/ipv6.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,24 +49,28 @@ IPv6Header.prototype.decode = function (raw_packet, next_header, ip, offset) {
};

function IPv6() {

this.version = undefined;
this.trafficClass = undefined;
this.flowLabel = undefined;
this.payloadLength = undefined;
this.nextHeader = undefined;
this.hopLimit = undefined;
this.saddr = undefined;
this.daddr = undefined;
}

IPv6.prototype.decode = function (raw_packet, offset) {

// http://en.wikipedia.org/wiki/IPv6
this.version = (raw_packet[offset] & 240) >> 4; // first 4 bits
this.traffic_class = ((raw_packet[offset] & 15) << 4) + ((raw_packet[offset+1] & 240) >> 4);
this.flow_label = ((raw_packet[offset + 1] & 15) << 16) +
this.version = ((raw_packet[offset] & 0xf0) >> 4); // first 4 bits
this.trafficClass = ((raw_packet[offset] & 0x0f) << 4) | ((raw_packet[offset+1] & 0xf0) >> 4);
this.flowLabel = ((raw_packet[offset + 1] & 0x0f) << 16) +
(raw_packet[offset + 2] << 8) +
raw_packet[offset + 3];
this.payload_length = raw_packet.readUInt16BE(offset+4, true);
this.total_length = this.payload_length + 40;
this.next_header = raw_packet[offset+6];
this.hop_limit = raw_packet[offset+7];
this.payloadLength = raw_packet.readUInt16BE(offset+4, true);
this.nextHeader = raw_packet[offset+6];
this.hopLimit = raw_packet[offset+7];
this.saddr = new IPv6Addr().decode(raw_packet, offset+8);
this.daddr = new IPv6Addr().decode(raw_packet, offset+24);
this.header_bytes = 40;

new IPv6Header().decode(raw_packet, this.next_header, this, offset+40);
return this;
Expand Down
73 changes: 73 additions & 0 deletions spec/decode/ipv6.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
var IPv6 = require("../../decode/ipv6");
require("should");

describe("IPv6", function(){
var example, instance;
beforeEach(function () {
example = new Buffer("61298765" + // version=6, trafficClass=0x12, labelflow=0,
"0000" + // payloadLength =0
"3b" + // No next header
"00" + // hopLimit=0
"fe80000000000000708dfe834114a512" + // src address
"2001000041379e508000f12ab9c82815", // dest address
"hex");
instance = new IPv6();
});

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

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

it("sets #trafficClass to the trafficClass decoded from packet", function(){
instance.decode(example, 0);
instance.should.have.property("trafficClass", 0x12);
});

it("sets #flowLabel to the flow label decoded from packet", function(){
instance.decode(example, 0);
instance.should.have.property("flowLabel", 0x98765);
});

it("sets #flowLabel to the flow label decoded from packet", function(){
instance.decode(example, 0);
instance.should.have.property("payloadLength", 0);
});

it("sets #nextHeader to the next header the protocol id of the payload", function(){
instance.decode(example, 0);
instance.should.have.property("nextHeader", 59);
});

it("sets #hopLimit to the hop limit from the packet", function(){
instance.decode(example, 0);
instance.should.have.property("hopLimit", 0);
});

it("sets #saddr to the senders address", function(){
instance.decode(example, 0);
instance.saddr.should.have.property("addr", [0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x70, 0x8d, 0xfe, 0x83, 0x41, 0x14, 0xa5, 0x12]);
});

it("sets #daddr to the destination address", function(){
instance.decode(example, 0);
instance.daddr.should.have.property("addr", [0x20, 0x01, 0x00, 0x00, 0x41, 0x37, 0x9e, 0x50, 0x80, 0x00, 0xf1, 0x2a, 0xb9, 0xc8, 0x28, 0x15]);
});
});

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

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

0 comments on commit fe514b1

Please sign in to comment.