tls-test.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * nssocket-test.js : namespace socket unit test for TLS.
  3. *
  4. * (C) 2011, Charlie Robbins, Paolo Fragomeni, & the Contributors.
  5. *
  6. */
  7. var assert = require('assert'),
  8. fs = require('fs'),
  9. net = require('net'),
  10. path = require('path'),
  11. tls = require('tls'),
  12. vows = require('vows'),
  13. NsSocket = require('../lib/nssocket').NsSocket;
  14. var TLS_PORT = 50305,
  15. fixturesDir = path.join(__dirname, 'fixtures');
  16. var serverOpts = {
  17. key: fs.readFileSync(path.join(fixturesDir, 'ryans-key.pem')),
  18. cert: fs.readFileSync(path.join(fixturesDir, 'ryans-cert.pem')),
  19. ca: fs.readFileSync(path.join(fixturesDir, 'ryans-csr.pem'))
  20. };
  21. var tlsServer = tls.createServer(serverOpts),
  22. tlsOpt;
  23. tlsOpt = {
  24. type: 'tls',
  25. delimiter: '::'
  26. };
  27. tlsServer.listen(TLS_PORT);
  28. vows.describe('nssocket/tls').addBatch({
  29. "When using NsSocket with TLS": {
  30. topic: new NsSocket(tlsOpt),
  31. "should create a wrapped socket": function (outbound) {
  32. assert.instanceOf(outbound, NsSocket);
  33. },
  34. "should have the proper configuration settings": function (outbound) {
  35. assert.equal(outbound._type, tlsOpt.type);
  36. assert.equal(outbound._delimiter, tlsOpt.delimiter);
  37. },
  38. "the connect() method": {
  39. topic: function (outbound) {
  40. var that = this;
  41. tlsServer.on('secureConnection', this.callback.bind(null, null, outbound));
  42. outbound.connect(TLS_PORT);
  43. },
  44. "should actually connect": function (_, outbound, inbound) {
  45. assert.instanceOf(outbound, NsSocket);
  46. if (!inbound.authorized) {
  47. console.log('Certificate is not authorized: ' + inbound.authorizationError);
  48. }
  49. },
  50. "the on() method": {
  51. topic: function (outbound, inbound) {
  52. outbound.on(['data', 'here', 'is'], this.callback.bind(outbound, null));
  53. inbound.write(JSON.stringify(['here', 'is', 'something']) + '\n');
  54. },
  55. "should handle namespaced events": function (_, data) {
  56. assert.isString(data);
  57. assert.isArray(this.event);
  58. assert.lengthOf(this.event, 3);
  59. assert.equal(this.event[0], 'data');
  60. assert.equal(this.event[1], 'here');
  61. assert.equal(this.event[2], 'is');
  62. assert.equal(data, 'something');
  63. },
  64. "once idle": {
  65. topic: function (_, outbound, inbound) {
  66. outbound.once('idle', this.callback.bind(null, null, outbound, inbound));
  67. outbound.setIdle(100);
  68. },
  69. "it should emit `idle`": function (_, outbound, inbound) {
  70. assert.isNull(_);
  71. },
  72. "the send() method": {
  73. topic: function (outbound, inbound) {
  74. inbound.on('data', this.callback.bind(null, null, outbound, inbound));
  75. outbound.send(['hello','world'], { some: "json", data: 123 });
  76. },
  77. "we should see it on the other end": function (_, outbound, inbound, data) {
  78. assert.isObject(data);
  79. arr = JSON.parse(data.toString());
  80. assert.lengthOf(arr, 3);
  81. assert.equal(arr[0], 'hello');
  82. assert.equal(arr[1], 'world');
  83. assert.deepEqual(arr[2], { some: "json", data: 123 });
  84. },
  85. "the end() method": {
  86. topic: function (outbound, inbound) {
  87. outbound.on('close', this.callback.bind(null, null, outbound, inbound));
  88. inbound.end();
  89. },
  90. "should close without errors": function (_, _, _, err) {
  91. assert.isUndefined(err);
  92. }
  93. }
  94. }
  95. }
  96. }
  97. }
  98. }
  99. }).export(module);