node-dash-button
Advanced tools
Comparing version 0.2.0 to 0.2.5
@@ -65,2 +65,3 @@ // converts a string: "8f:3f:20:33:54:44" | ||
pcap_session.on('packet', function(raw_packet) { | ||
//console.log(raw_packet) | ||
var packet = pcap.decode.packet(raw_packet); //decodes the packet | ||
@@ -84,2 +85,4 @@ if(packet.payload.ethertype === 2054) { //ensures it is an arp packet | ||
if (process.env.NODE_ENV === 'test') { | ||
module.exports = { hex_to_int_array: hex_to_int_array, | ||
@@ -90,4 +93,5 @@ int_array_to_hex: int_array_to_hex, | ||
}; | ||
} else { | ||
module.exports = register; | ||
} |
{ | ||
"name": "node-dash-button", | ||
"version": "0.2.0", | ||
"version": "0.2.5", | ||
"description": "a module for integrating amazon dash button presses into a node project", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "mocha test/test.js" | ||
"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" | ||
}, | ||
@@ -30,3 +31,3 @@ "bin":{ | ||
"dependencies": { | ||
"pcap": "^2.0.1", | ||
"pcap": "git+https://github.com/mranney/node_pcap.git", | ||
"underscore": "^1.8.3" | ||
@@ -37,7 +38,10 @@ }, | ||
"mocha": "^2.3.0", | ||
"pcap": "^2.0.1", | ||
"should": "^7.1.0", | ||
"underscore": "^1.8.3" | ||
"sinon": "*", | ||
"mockery": "*", | ||
"ip":"*", | ||
"istanbul": "*", | ||
"coveralls": "*" | ||
} | ||
} |
![Blank Dash](http://i.imgur.com/PP0CJ3s.png?1) | ||
# node-dash-button | ||
[![gitter](https://img.shields.io/badge/GITTER-join%20chat-green.svg?style=flat-square)](https://gitter.im/hortinstein/node-dash-button) | ||
[![Travis-CI Build Status](https://travis-ci.org/hortinstein/node-dash-button.svg)](https://travis-ci.org/hortinstein/node-dash-button) [![Coverage Status](https://coveralls.io/repos/hortinstein/node-dash-button/badge.svg?branch=master&service=github)](https://coveralls.io/github/hortinstein/node-dash-button?branch=master) [![gitter](https://img.shields.io/badge/GITTER-join%20chat-green.svg?style=flat-square)](https://gitter.im/hortinstein/node-dash-button) | ||
Place it. *Hack it.* Press it. ~~Get it.~~ | ||
@@ -18,2 +18,3 @@ | ||
- [Example Projects](#example-projects) | ||
- [Running Tests](#running-tests) | ||
- [To do](#to-do) | ||
@@ -72,2 +73,17 @@ - [Contributions](#contributions) | ||
#### Running Tests: | ||
Due to the use of pcap permiscuous monitoring this was difficult to test in CI environments, so I ended up making two testing suites. One uses the live pcap library and does actual packet capturing/arp injections. The other uses [mockery](https://github.com/mfncooper/mockery) to fake pcap packets. I will have an upcoming blog post on how I did this, because it was interesting. | ||
To run a live test of the code (requiring root due to permiscuous access please run). | ||
``` | ||
sudo npm run-script livetest | ||
``` | ||
This will actually inject ARP packets to the network to run the tests to ensure detection. | ||
I wanted to use various CI tools that would not allow the pcap functions to work, so I ended up mocking their functions. To run the mock tests use: | ||
``` | ||
npm test | ||
``` | ||
#### Example Projects: | ||
@@ -84,3 +100,3 @@ I collected a few examples I found on github of how people are using this module, some projects are more mature than others | ||
#### To do | ||
- figure out how not to use root and fix CI | ||
- refactor | ||
@@ -87,0 +103,0 @@ #### Contributions |
135
test/test.js
process.env.NODE_ENV = 'test'; | ||
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'); | ||
var assert = require('assert'); | ||
var dash_button = require('../index.js'); | ||
var mockery = require('mockery'); // https://github.com/nathanmacinnes/injectr | ||
var events = require('events'); | ||
var hex = '8f:3f:20:33:54:44'; | ||
@@ -11,76 +13,95 @@ var hex2 = '8f:3f:20:33:54:43'; | ||
var int_array = []; | ||
var arp = require('arpjs'); | ||
var sendarp = function(){ | ||
arp.send({ | ||
'op': 'request', | ||
'src_ip': '10.105.50.100', | ||
'dst_ip': '10.105.50.1', | ||
'src_mac': hex, | ||
'dst_mac': 'ff:ff:ff:ff:ff:ff' | ||
}); | ||
//these are packets to pass into the mock pcap.decode | ||
var packet1 = { | ||
'packet_payload_ethertype': 2054, | ||
'packet_payload_payload_sender_ha_addr': hex, | ||
}; | ||
var sendarp2 = function(){ | ||
arp.send({ | ||
'op': 'request', | ||
'src_ip': '10.105.50.100', | ||
'dst_ip': '10.105.50.1', | ||
'src_mac': hex2, | ||
'dst_mac': 'ff:ff:ff:ff:ff:ff' | ||
}); | ||
var packet2 = { | ||
'packet_payload_ethertype': 2054, | ||
'packet_payload_payload_sender_ha_addr': hex2, | ||
}; | ||
var sendarp3 = function(){ | ||
arp.send({ | ||
'op': 'request', | ||
'src_ip': '10.105.50.100', | ||
'dst_ip': '10.105.50.1', | ||
'src_mac': hex3, | ||
'dst_mac': 'ff:ff:ff:ff:ff:ff' | ||
}); | ||
var packet3 = { | ||
'packet_payload_ethertype': 2054, | ||
'packet_payload_payload_sender_ha_addr': hex3, | ||
}; | ||
fake_session = new events.EventEmitter(); | ||
var mock_pcap = { | ||
createSession: function() { | ||
//console.log("sending reference to fake event emitter") | ||
return fake_session; | ||
}, | ||
test: function() { | ||
return 'inject works!'; | ||
}, | ||
decode: { | ||
packet: function(packet) { | ||
mock_packet = { | ||
"payload": { | ||
"ethertype": packet.packet_payload_ethertype, | ||
"payload": { | ||
"sender_ha": { | ||
"addr": dash_button.hex_to_int_array(packet.packet_payload_payload_sender_ha_addr) | ||
} | ||
} | ||
} | ||
} | ||
return mock_packet; | ||
} | ||
} | ||
}; | ||
startTests = function() { | ||
before(function(done) { | ||
done(); | ||
before(function() { | ||
mockery.enable({ | ||
warnOnReplace: false, | ||
warnOnUnregistered: false, | ||
useCleanCache: true | ||
}); | ||
mockery.registerMock('pcap', mock_pcap); // replace the module `request` with a stub object | ||
pcap = require('pcap'); | ||
dash_button = require('../index.js'); | ||
}); | ||
it('should correctly mock pcap functions for testing', function(done) { | ||
pcap.test().should.equal('inject works!'); | ||
done(); | ||
}); | ||
it('should correctly convert string hex to decimal array', function(done) { | ||
int_array = dash_button.hex_to_int_array(hex) | ||
done(); | ||
done(); | ||
}); | ||
it('should correctly convert a decimal array to string hex', function(done) { | ||
dash_button.int_array_to_hex(int_array).should.equal(hex); | ||
done(); | ||
done(); | ||
}); | ||
it('should recognize an arp request', function(done) { | ||
this.timeout(30000);//sometimes the detection takes a while | ||
setTimeout(sendarp, 2500); //giving pcap time to set up a listener | ||
dash_button.register(hex).on('detected', function(){ | ||
dash_button.register(hex).on('detected', function() { | ||
done(); | ||
}); | ||
}); | ||
fake_session.emit('packet', packet1) | ||
}); | ||
it('should not fire with more than 2 arp requests in 2 seconds', function(done) { | ||
this.timeout(30000);//sometimes the detection takes a while | ||
setInterval(sendarp, 250); //giving pcap time to set up a listener | ||
dash_button.register(hex).on('detected', function(){ | ||
done(); | ||
}); | ||
dash_button.register(hex2).on('detected', function() { | ||
setTimeout(function() { | ||
done() | ||
}, 50); | ||
//console.log("should only see this once") | ||
}); | ||
for(count = 0; count < 10; count++) { | ||
//console.log("firing packet!") | ||
fake_session.emit('packet', packet2) | ||
} | ||
}); | ||
two_tester = dash_button.register([hex2,hex3]); | ||
it('should recognize first of two arp requests', function(done) { | ||
this.timeout(30000);//sometimes the detection takes a while | ||
setInterval(sendarp2, 250); //giving pcap time to set up a listener | ||
two_tester.on('detected', function(mac_address){ | ||
if (mac_address === hex2) 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) | ||
}); | ||
it('should recognize second of two arp requests', function(done) { | ||
this.timeout(30000);//sometimes the detection takes a while | ||
setInterval(sendarp3, 250); //giving pcap time to set up a listener | ||
two_tester.on('detected', function(mac_address){ | ||
if (mac_address === hex3) done(); | ||
}); | ||
two_tester.on('detected', function(mac_address) { | ||
if(mac_address === hex3) done(); | ||
}); | ||
fake_session.emit('packet', packet3) | ||
}); | ||
} | ||
startTests(); | ||
startTests(); |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Git dependency
Supply chain riskContains a dependency which resolves to a remote git URL. Dependencies fetched from git URLs are not immutable can be used to inject untrusted code or reduce the likelihood of a reproducible install.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
95029
10
269
114
8
1
3
- Removednan@2.22.0(transitive)
- Removedpcap@2.1.0(transitive)