diff --git a/index.js b/index.js index c7eec7d..d987868 100644 --- a/index.js +++ b/index.js @@ -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 @@ -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"= @@ -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'); @@ -36,14 +37,11 @@ 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) { @@ -51,7 +49,7 @@ var register = function(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({ diff --git a/package.json b/package.json index 179627b..b8442ab 100644 --- a/package.json +++ b/package.json @@ -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": { @@ -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" } - } diff --git a/test/test-live.js b/test/test-live.js index d7c3024..115fd84 100644 --- a/test/test-live.js +++ b/test/test-live.js @@ -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'; @@ -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) { @@ -80,6 +80,6 @@ startTests = function() { if (mac_address === hex3) done(); }); }); -} +}; startTests(); diff --git a/test/test.js b/test/test.js index 1d72c88..b2952bc 100644 --- a/test/test.js +++ b/test/test.js @@ -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'); @@ -34,7 +34,7 @@ var mock_pcap = { }, decode: { packet: function(packet) { - mock_packet = { + var mock_packet = { "payload": { "ethertype": packet.packet_payload_ethertype, "payload": { @@ -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({ @@ -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) { @@ -75,18 +77,18 @@ 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) { @@ -94,13 +96,23 @@ startTests = function() { 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(); \ No newline at end of file