Skip to content

Commit

Permalink
Merge pull request #12 from hortinstein/cleaning
Browse files Browse the repository at this point in the history
Cleaning
  • Loading branch information
hortinstein committed Oct 17, 2015
2 parents 4d65353 + 06a1f34 commit facd600
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 33 deletions.
22 changes: 10 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
"use strict";

// converts a string: "8f:3f:20:33:54:44"
// to a numeric array: [ 143, 63, 32, 51, 84, 68 ]
// for comparison
Expand All @@ -9,8 +11,7 @@ var hex_to_int_array = function(hex){
}
//console.log(hex,int_array)
return int_array;

}
};

// converts a numeric array: [ 143, 63, 32, 51, 84, 68 ]
// to a string: "8f:3f:20:33:54:44"=
Expand All @@ -19,12 +20,12 @@ var int_array_to_hex = function (int_array) {
var hex = "";
for (var i in int_array){
var h = int_array[i].toString(16); // converting to hex
if (h.length < 2) h = '0' + h; //adding a 0 for non 2 digit numbers
if (i !== int_array.length) hex+=":"; //adding a : for all but the last group
if (h.length < 2) {h = '0' + h}; //adding a 0 for non 2 digit numbers
if (i !== int_array.length) {hex+=":"}; //adding a : for all but the last group
hex += h;
}
return hex.slice(1);//slice is to get rid of the leading :
}
};


var pcap = require('pcap');
Expand All @@ -36,22 +37,19 @@ var create_session = function () {
var session = pcap.createSession();
} catch (err) {
console.error(err);
if (err == "Error: pcap_findalldevs didn't find any devs") {
console.log("Failed to create pcap session: couldn't find devices to listen on.\n" +
"Try running with elevated privileges via 'sudo'");
}
process.exit(1);
console.error("Failed to create pcap session: couldn't find devices to listen on.\n" + "Try running with elevated privileges via 'sudo'");
throw new Error('Error: No devices to listen');
}
return session;
}
};

//Function to register the node button
var register = function(mac_addresses) {
if (Array.isArray(mac_addresses)){
//console.log("array detected")
} else {
//console.log("single element detected")
mac_addresses = [mac_addresses]//cast to array
mac_addresses = [mac_addresses];//cast to array
}
var pcap_session = create_session();
var readStream = new stream.Readable({
Expand Down
12 changes: 5 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"livetest": "istanbul cover ./node_modules/mocha/bin/_mocha test/test-live.js --report lcovonly -- -R spec && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js && rm -rf ./coverage",
"test": "istanbul cover ./node_modules/mocha/bin/_mocha test/test.js --report lcovonly -- -R spec && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js && rm -rf ./coverage"
},
"bin":{
"bin": {
"findbutton": "./bin/findbutton"
},
"repository": {
Expand All @@ -33,13 +33,11 @@
},
"devDependencies": {
"arpjs": "^1.0.0",
"coveralls": "^2.11.4",
"istanbul": "0.3.22",
"mocha": "^2.3.0",
"mockery": "^1.4.0",
"should": "^7.1.0",
"sinon": "*",
"mockery": "*",
"ip":"*",
"istanbul": "*",
"coveralls": "*"
"sinon": "^1.17.1"
}

}
6 changes: 3 additions & 3 deletions test/test-live.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ process.env.NODE_ENV = 'test';
var should = require('should');
var assert = require('assert');
var dash_button = require('../index.js');
var pcap = require('pcap')
var pcap = require('pcap');
var hex = '8f:3f:20:33:54:44';
var hex2 = '8f:3f:20:33:54:43';
var hex3 = '8f:3f:20:33:54:42';
Expand Down Expand Up @@ -43,7 +43,7 @@ startTests = function() {
done();
});
it('should correctly convert string hex to decimal array', function(done) {
int_array = dash_button.hex_to_int_array(hex)
int_array = dash_button.hex_to_int_array(hex);
done();
});
it('should correctly convert a decimal array to string hex', function(done) {
Expand Down Expand Up @@ -80,6 +80,6 @@ startTests = function() {
if (mac_address === hex3) done();
});
});
}
};
startTests();

34 changes: 23 additions & 11 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
process.env.NODE_ENV = 'test';
require('buffer')
require('buffer');
///http://bulkan-evcimen.com/using_mockery_to_mock_modules_nodejs.html
//this should be an effective way to mock functions
var should = require('should');
Expand Down Expand Up @@ -34,7 +34,7 @@ var mock_pcap = {
},
decode: {
packet: function(packet) {
mock_packet = {
var mock_packet = {
"payload": {
"ethertype": packet.packet_payload_ethertype,
"payload": {
Expand All @@ -43,11 +43,13 @@ var mock_pcap = {
}
}
}
}
};
return mock_packet;
}
}
};
var pcap = ""; //for linting purps
var dash_button = ""; //for linting purps
startTests = function() {
before(function() {
mockery.enable({
Expand All @@ -64,7 +66,7 @@ startTests = function() {
done();
});
it('should correctly convert string hex to decimal array', function(done) {
int_array = dash_button.hex_to_int_array(hex)
int_array = dash_button.hex_to_int_array(hex);
done();
});
it('should correctly convert a decimal array to string hex', function(done) {
Expand All @@ -75,32 +77,42 @@ startTests = function() {
dash_button.register(hex).on('detected', function() {
done();
});
fake_session.emit('packet', packet1)
fake_session.emit('packet', packet1);
});
it('should not fire with more than 2 arp requests in 2 seconds', function(done) {
dash_button.register(hex2).on('detected', function() {
setTimeout(function() {
done()
done();
}, 50);
//console.log("should only see this once")
});
for(count = 0; count < 10; count++) {
//console.log("firing packet!")
fake_session.emit('packet', packet2)
//console.log("firing packet!");
fake_session.emit('packet', packet2);
}
});
it('should recognize first of two arp requests', function(done) {
two_tester = dash_button.register([hex2, hex3]);
two_tester.on('detected', function(mac_address) {
if(mac_address === hex2) done();
});
fake_session.emit('packet', packet2)
fake_session.emit('packet', packet2);
});
it('should recognize second of two arp requests', function(done) {
two_tester.on('detected', function(mac_address) {
if(mac_address === hex3) done();
});
fake_session.emit('packet', packet3)
fake_session.emit('packet', packet3);
});
}
it('should throw an error if no interfaces are available', function(done) {
mock_pcap.createSession = function() {
throw new Error("Error: pcap_findalldevs didn't find any devs");
};
try {
dash_button.register('bullshit');
} catch(err) {
done()
}
});
};
startTests();

0 comments on commit facd600

Please sign in to comment.