Skip to content

Commit

Permalink
Add tests for Udp.decode
Browse files Browse the repository at this point in the history
  • Loading branch information
jmaxxz committed Apr 2, 2015
1 parent a10b480 commit 3cdf014
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 5 deletions.
10 changes: 5 additions & 5 deletions decode/udp.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ var DNS = require("./dns");

function UDP(emitter) {
this.emitter = emitter;
this.sport = null;
this.dport = null;
this.length = null;
this.checksum = null;
this.data = null;
this.sport = undefined;
this.dport = undefined;
this.length = undefined;
this.checksum = undefined;
this.data = undefined;
}

// http://en.wikipedia.org/wiki/User_Datagram_Protocol
Expand Down
51 changes: 51 additions & 0 deletions spec/decode/udp.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
var Udp = require("../../decode/udp");
var events = require("events");
var shouldBehaveLikeADecoder = require("./decode").shouldBehaveLikeADecoder;
require("should");

describe("Udp", function(){
beforeEach(function () {
this.eventEmitter = new events.EventEmitter();
this.instance = new Udp(this.eventEmitter);
this.example = new Buffer("04d2" + // source port 1234
"04d3" + // dst port 1235
"0009" + // length
"df03" + // checksum (this on is bad)
"30", "hex");
});

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

it("sets #sport to the source port", function(){
this.instance.decode(this.example, 0);
this.instance.should.have.property("sport", 1234);
});

it("sets #dport to the destination port", function(){
this.instance.decode(this.example, 0);
this.instance.should.have.property("dport", 1235);
});

it("sets #length to the length of the payload", function(){
this.instance.decode(this.example, 0);
this.instance.should.have.property("length", 9);
});

it("sets #data to the payload", function(){
this.instance.decode(this.example, 0);
this.instance.should.have.property("data", new Buffer("30", "hex"));
});

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

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

0 comments on commit 3cdf014

Please sign in to comment.