Permalink
Browse files

Add support for specifying if connection should be encrypted.

  • Loading branch information...
1 parent 0ac3ba0 commit 97a6361c26cfad773384fa5888dd5071ccc38a03 @pekim pekim committed Jul 15, 2012
Showing with 24 additions and 12 deletions.
  1. +2 −1 src/connection.coffee
  2. +10 −6 src/prelogin-payload.coffee
  3. +12 −5 test/unit/prelogin-payload-test.coffee
View
3 src/connection.coffee
@@ -142,6 +142,7 @@ class Connection extends EventEmitter
@config.options.packetSize ||= DEFAULT_PACKET_SIZE
@config.options.tdsVersion ||= DEFAULT_TDS_VERSION
@config.options.isolationLevel ||= ISOLATION_LEVEL.READ_UNCOMMITTED
+ @config.options.encrypt ||= false
if !@config.options.port && !@config.options.instanceName
@config.options.port = DEFAULT_PORT
@@ -308,7 +309,7 @@ class Connection extends EventEmitter
@transitionTo(@STATE.FINAL)
sendPreLogin: ->
- payload = new PreloginPayload()
+ payload = new PreloginPayload({encrypt: @config.options.encrypt})
@messageIo.sendMessage(TYPE.PRELOGIN, payload.data)
@debug.payload(->
payload.toString(' ')
View
16 src/prelogin-payload.coffee
@@ -36,10 +36,11 @@ for name, value of MARS
s2.2.6.4
###
class PreloginPayload
- constructor: (buffer) ->
- if buffer instanceof Buffer
- @data = buffer
+ constructor: (bufferOrOptions) ->
+ if bufferOrOptions instanceof Buffer
+ @data = bufferOrOptions
else
+ @options = bufferOrOptions || {}
@createOptions()
@extractOptions()
@@ -82,7 +83,10 @@ class PreloginPayload
createEncryptionOption: () ->
buffer = new WritableTrackingBuffer(optionBufferSize)
- buffer.writeUInt8(ENCRYPT.NOT_SUP)
+ if @options.encrypt
+ buffer.writeUInt8(ENCRYPT.ON)
+ else
+ buffer.writeUInt8(ENCRYPT.NOT_SUP)
token: TOKEN.ENCRYPTION
data: buffer.data
@@ -162,11 +166,11 @@ class PreloginPayload
@version.patch,
@version.trivial,
@version.subbuild,
- if @encryption then @encryption else 0,
+ if @encryption then @encryption else 0,
if @encryptionString then @encryptionString else 0,
if @instance then @instance else 0,
if @threadId then @threadId else 0,
- if @mars then @mars else 0,
+ if @mars then @mars else 0,
if @marsString then @marsString else 0
)
View
17 test/unit/prelogin-payload-test.coffee
@@ -1,28 +1,35 @@
PreloginPayload = require('../../src/prelogin-payload')
-exports.createFromScratch = (test) ->
+exports.noEncrypt = (test) ->
payload = new PreloginPayload()
- assertPayload(test, payload)
+ assertPayload(test, payload, 'NOT_SUP')
+
+ test.done()
+
+exports.encrypt = (test) ->
+ payload = new PreloginPayload({encrypt: true})
+
+ assertPayload(test, payload, 'ON')
test.done()
exports.createFromBuffer = (test) ->
payload = new PreloginPayload()
new PreloginPayload(payload.data)
- assertPayload(test, payload)
+ assertPayload(test, payload, 'NOT_SUP')
test.done()
-assertPayload = (test, payload) ->
+assertPayload = (test, payload, encryptionString) ->
test.strictEqual(payload.version.major, 0)
test.strictEqual(payload.version.minor, 0)
test.strictEqual(payload.version.patch, 0)
test.strictEqual(payload.version.trivial, 1)
test.strictEqual(payload.version.subbuild, 1)
- test.strictEqual(payload.encryptionString, 'NOT_SUP')
+ test.strictEqual(payload.encryptionString, encryptionString)
test.strictEqual(payload.instance, 0)
test.strictEqual(payload.threadId, 0)
test.strictEqual(payload.marsString, 'OFF')

0 comments on commit 97a6361

Please sign in to comment.