aboutsummaryrefslogtreecommitdiffstats
path: root/test/integration/lib/encryptor-test.js
blob: 897d22740f8b665421000e5cd29d0b72f77d477d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
var encryptor = require('../../../app/scripts/lib/encryptor')

QUnit.module('encryptor')

QUnit.test('encryptor:serializeBufferForStorage', function (assert) {
  assert.expect(1)
  var buf = new Buffer(2)
  buf[0] = 16
  buf[1] = 1

  var output = encryptor.serializeBufferForStorage(buf)

  var expect = '0x1001'
  assert.equal(expect, output)
})

QUnit.test('encryptor:serializeBufferFromStorage', function (assert) {
  assert.expect(2)
  var input = '0x1001'
  var output = encryptor.serializeBufferFromStorage(input)

  assert.equal(output[0], 16)
  assert.equal(output[1], 1)
})

QUnit.test('encryptor:encrypt & decrypt', function(assert) {
  var done = assert.async();
  var password, data, encrypted

  password = 'a sample passw0rd'
  data = { foo: 'data to encrypt' }

  encryptor.encrypt(password, data)
  .then(function(encryptedStr) {
    assert.equal(typeof encryptedStr, 'string', 'returns a string')
    return encryptor.decrypt(password, encryptedStr)
  })
  .then(function (decryptedObj) {
    assert.deepEqual(decryptedObj, data, 'decrypted what was encrypted')
    done()
  })
  .catch(function(reason) {
    assert.ifError(reason, 'threw an error')
    done(reason)
  })

})

QUnit.test('encryptor:encrypt & decrypt with wrong password', function(assert) {
  var done = assert.async();
  var password, data, encrypted, wrongPassword

  password = 'a sample passw0rd'
  wrongPassword = 'a wrong password'
  data = { foo: 'data to encrypt' }

  encryptor.encrypt(password, data)
  .then(function(encryptedStr) {
    assert.equal(typeof encryptedStr, 'string', 'returns a string')
    return encryptor.decrypt(wrongPassword, encryptedStr)
  })
  .then(function (decryptedObj) {
    assert.equal(!decryptedObj, true, 'Wrong password should not decrypt')
    done()
  })
  .catch(function(reason) {
    done()
  })
})