Skip to content
Browse files

Add tests for zmq 4.x security mechanisms

  • Loading branch information...
1 parent bfa32e8 commit efbdf4567abd7f0e83c1a36b6c6d277a170b4e2b @msealand msealand committed Feb 10, 2014
Showing with 184 additions and 0 deletions.
  1. +52 −0 test/test.socket.zap.curve.js
  2. +41 −0 test/test.socket.zap.null.js
  3. +45 −0 test/test.socket.zap.plain.js
  4. +46 −0 test/zap.js
View
52 test/test.socket.zap.curve.js
@@ -0,0 +1,52 @@
+var should = require('should')
+ , semver = require('semver')
+ , zmq = require('../');
+
+if (semver.gte(zmq.version, '4.0.0')) {
+ var zap = require('./zap')
+ , port = 'tcp://127.0.0.1:12347'
+ , zapSocket = zap.start()
+ , rep = zmq.socket('rep')
+ , req = zmq.socket('req');
+
+ var serverPublicKey = new Buffer('7f188e5244b02bf497b86de417515cf4d4053ce4eb977aee91a55354655ec33a', 'hex')
+ , serverPrivateKey = new Buffer('1f5d3873472f95e11f4723d858aaf0919ab1fb402cb3097742c606e61dd0d7d8', 'hex')
+ , clientPublicKey = new Buffer('ea1cc8bd7c8af65497d43fc21dbec6560c5e7b61bcfdcbd2b0dfacf0b4c38d45', 'hex')
+ , clientPrivateKey = new Buffer('83f99afacfab052406e5f421612568034e85f4c8182a1c92671e83dca669d31d', 'hex');
+
+ rep.on('message', function(msg){
+ msg.should.be.an.instanceof(Buffer);
+ msg.toString().should.equal('hello');
+ rep.send('world');
+ });
+
+ rep.zap_domain = "test";
+ rep.curve_server = 1;
+ rep.curve_secretkey = serverPrivateKey;
+ rep.mechanism.should.eql(2);
+
+ var timeout = setTimeout(function() {
+ req.close();
+ rep.close();
+ zapSocket.close();
+ throw new Error("Request timed out");
+ }, 1000);
+
+ rep.bind(port, function(){
+ req.curve_serverkey = serverPublicKey;
+ req.curve_publickey = clientPublicKey;
+ req.curve_secretkey = clientPrivateKey;
+ req.mechanism.should.eql(2);
+
+ req.connect(port);
+ req.send('hello');
+ req.on('message', function(msg){
+ msg.should.be.an.instanceof(Buffer);
+ msg.toString().should.equal('world');
+ clearTimeout(timeout);
+ req.close();
+ rep.close();
+ zapSocket.close();
+ });
+ });
+}
View
41 test/test.socket.zap.null.js
@@ -0,0 +1,41 @@
+var should = require('should')
+ , semver = require('semver')
+ , zmq = require('../');
+
+if (semver.gte(zmq.version, '4.0.0')) {
+ var zap = require('./zap')
+ , port = 'tcp://127.0.0.1:12345'
+ , zapSocket = zap.start()
+ , rep = zmq.socket('rep')
+ , req = zmq.socket('req');
+
+ rep.on('message', function(msg){
+ msg.should.be.an.instanceof(Buffer);
+ msg.toString().should.equal('hello');
+ rep.send('world');
+ });
+
+ rep.zap_domain = "test";
+ rep.mechanism.should.eql(0);
+
+ var timeout = setTimeout(function() {
+ req.close();
+ rep.close();
+ zapSocket.close();
+ throw new Error("Request timed out");
+ }, 1000);
+
+ rep.bind(port, function(){
+ req.mechanism.should.eql(0);
+ req.connect(port);
+ req.send('hello');
+ req.on('message', function(msg){
+ msg.should.be.an.instanceof(Buffer);
+ msg.toString().should.equal('world');
+ clearTimeout(timeout);
+ req.close();
+ rep.close();
+ zapSocket.close();
+ });
+ });
+}
View
45 test/test.socket.zap.plain.js
@@ -0,0 +1,45 @@
+var should = require('should')
+ , semver = require('semver')
+ , zmq = require('../');
+
+if (semver.gte(zmq.version, '4.0.0')) {
+ var zap = require('./zap')
+ , port = 'tcp://127.0.0.1:12346'
+ , zapSocket = zap.start()
+ , rep = zmq.socket('rep')
+ , req = zmq.socket('req');
+
+ rep.on('message', function(msg){
+ msg.should.be.an.instanceof(Buffer);
+ msg.toString().should.equal('hello');
+ rep.send('world');
+ });
+
+ rep.zap_domain = "test";
+ rep.plain_server = 1;
+ rep.mechanism.should.eql(1);
+
+ var timeout = setTimeout(function() {
+ req.close();
+ rep.close();
+ zapSocket.close();
+ throw new Error("Request timed out");
+ }, 1000);
+
+ rep.bind(port, function(){
+ req.plain_username = "user";
+ req.plain_password = "pass";
+ req.mechanism.should.eql(1);
+
+ req.connect(port);
+ req.send('hello');
+ req.on('message', function(msg){
+ msg.should.be.an.instanceof(Buffer);
+ msg.toString().should.equal('world');
+ clearTimeout(timeout);
+ req.close();
+ rep.close();
+ zapSocket.close();
+ });
+ });
+}
View
46 test/zap.js
@@ -0,0 +1,46 @@
+// This is mainly for testing that the security mechanisms themselves are working
+// not the ZAP protocol itself. As long as the request is valid, this will
+// authenticate it.
+
+var zmq = require('../');
+
+module.exports.start = function() {
+ var zap = zmq.socket('router');
+ zap.on('message', function() {
+ var data = Array.prototype.slice.call(arguments);
+
+ if (!data || !data.length) throw new Error("Invalid ZAP request");
+
+ var returnPath = [],
+ frame = data.shift();
+ while (frame && (frame.length != 0)) {
+ returnPath.push(frame);
+ frame = data.shift();
+ }
+ returnPath.push(frame);
+
+ if (data.length < 6) throw new Error("Invalid ZAP request");
+
+ var zapReq = {
+ version: data.shift(),
+ requestId: data.shift(),
+ domain: new Buffer(data.shift()).toString('utf8'),
+ address: new Buffer(data.shift()).toString('utf8'),
+ identity: new Buffer(data.shift()).toString('utf8'),
+ mechanism: new Buffer(data.shift()).toString('utf8'),
+ credentials: data.slice(0)
+ };
+
+ zap.send(returnPath.concat([
+ zapReq.version,
+ zapReq.requestId,
+ new Buffer("200", "utf8"),
+ new Buffer("OK", "utf8"),
+ new Buffer(0),
+ new Buffer(0)
+ ]));
+ });
+
+ zap.bindSync("inproc://zeromq.zap.01");
+ return zap;
+}

0 comments on commit efbdf45

Please sign in to comment.
Something went wrong with that request. Please try again.