| 1 |
// Copyright 2011 The Closure Library Authors. All Rights Reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS-IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
goog.provide('goog.storage.EncryptedStorageTest'); |
| 16 |
goog.setTestOnly('goog.storage.EncryptedStorageTest'); |
| 17 |
|
| 18 |
goog.require('goog.json'); |
| 19 |
goog.require('goog.storage.EncryptedStorage'); |
| 20 |
goog.require('goog.storage.ErrorCode'); |
| 21 |
goog.require('goog.storage.RichStorage'); |
| 22 |
goog.require('goog.storage.collectableStorageTester'); |
| 23 |
goog.require('goog.storage.storageTester'); |
| 24 |
goog.require('goog.testing.MockClock'); |
| 25 |
goog.require('goog.testing.PseudoRandom'); |
| 26 |
goog.require('goog.testing.jsunit'); |
| 27 |
goog.require('goog.testing.storage.FakeMechanism'); |
| 28 |
|
| 29 |
function getEncryptedWrapper(storage, key) { |
| 30 |
return JSON.parse(storage.mechanism.get(storage.hashKeyWithSecret_(key))); |
| 31 |
} |
| 32 |
|
| 33 |
function getEncryptedData(storage, key) { |
| 34 |
return getEncryptedWrapper(storage, key)[goog.storage.RichStorage.DATA_KEY]; |
| 35 |
} |
| 36 |
|
| 37 |
function decryptWrapper(storage, key, wrapper) { |
| 38 |
return JSON.parse(storage.decryptValue_( |
| 39 |
wrapper[goog.storage.EncryptedStorage.SALT_KEY], key, |
| 40 |
wrapper[goog.storage.RichStorage.DATA_KEY])); |
| 41 |
} |
| 42 |
|
| 43 |
function hammingDistance(a, b) { |
| 44 |
if (a.length != b.length) { |
| 45 |
throw new Error('Lengths must be the same for Hamming distance'); |
| 46 |
} |
| 47 |
var distance = 0; |
| 48 |
for (var i = 0; i < a.length; ++i) { |
| 49 |
if (a.charAt(i) != b.charAt(i)) { |
| 50 |
++distance; |
| 51 |
} |
| 52 |
} |
| 53 |
return distance; |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
function testBasicOperations() { |
| 58 |
var mechanism = new goog.testing.storage.FakeMechanism(); |
| 59 |
var storage = new goog.storage.EncryptedStorage(mechanism, 'secret'); |
| 60 |
goog.storage.storageTester.runBasicTests(storage); |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
function testExpiredKeyCollection() { |
| 65 |
var mechanism = new goog.testing.storage.FakeMechanism(); |
| 66 |
var clock = new goog.testing.MockClock(true); |
| 67 |
var storage = new goog.storage.EncryptedStorage(mechanism, 'secret'); |
| 68 |
|
| 69 |
goog.storage.collectableStorageTester.runBasicTests( |
| 70 |
mechanism, clock, storage); |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
function testEncryption() { |
| 75 |
var mechanism = new goog.testing.storage.FakeMechanism(); |
| 76 |
var clock = new goog.testing.MockClock(true); |
| 77 |
var storage = new goog.storage.EncryptedStorage(mechanism, 'secret'); |
| 78 |
var mallory = new goog.storage.EncryptedStorage(mechanism, 'guess'); |
| 79 |
|
| 80 |
// Simple Objects. |
| 81 |
storage.set('first', 'Hello world!'); |
| 82 |
storage.set('second', ['one', 'two', 'three'], 1000); |
| 83 |
storage.set('third', {'a': 97, 'b': 98}); |
| 84 |
|
| 85 |
// Wrong secret can't find keys. |
| 86 |
assertNull(mechanism.get('first')); |
| 87 |
assertNull(mechanism.get('second')); |
| 88 |
assertNull(mechanism.get('third')); |
| 89 |
assertUndefined(mallory.get('first')); |
| 90 |
assertUndefined(mallory.get('second')); |
| 91 |
assertUndefined(mallory.get('third')); |
| 92 |
|
| 93 |
// Wrong secret can't overwrite keys. |
| 94 |
mallory.set('first', 'Ho ho ho!'); |
| 95 |
assertObjectEquals('Ho ho ho!', mallory.get('first')); |
| 96 |
assertObjectEquals('Hello world!', storage.get('first')); |
| 97 |
mallory.remove('first'); |
| 98 |
|
| 99 |
// Correct key decrypts properly. |
| 100 |
assertObjectEquals('Hello world!', storage.get('first')); |
| 101 |
assertObjectEquals(['one', 'two', 'three'], storage.get('second')); |
| 102 |
assertObjectEquals({'a': 97, 'b': 98}, storage.get('third')); |
| 103 |
|
| 104 |
// Wrong secret can't decode values even if the key is revealed. |
| 105 |
var encryptedWrapper = getEncryptedWrapper(storage, 'first'); |
| 106 |
assertObjectEquals( |
| 107 |
'Hello world!', decryptWrapper(storage, 'first', encryptedWrapper)); |
| 108 |
assertThrows(function() { |
| 109 |
decryptWrapper(mallory, 'first', encryptedWrapper); |
| 110 |
}); |
| 111 |
|
| 112 |
// If the value is overwritten, it can't be decrypted. |
| 113 |
encryptedWrapper[goog.storage.RichStorage.DATA_KEY] = 'kaboom'; |
| 114 |
mechanism.set( |
| 115 |
storage.hashKeyWithSecret_('first'), |
| 116 |
goog.json.serialize(encryptedWrapper)); |
| 117 |
assertEquals( |
| 118 |
goog.storage.ErrorCode.DECRYPTION_ERROR, assertThrows(function() { |
| 119 |
storage.get('first'); |
| 120 |
})); |
| 121 |
|
| 122 |
// Test garbage collection. |
| 123 |
storage.collect(); |
| 124 |
assertNotNull(getEncryptedWrapper(storage, 'first')); |
| 125 |
assertObjectEquals(['one', 'two', 'three'], storage.get('second')); |
| 126 |
assertObjectEquals({'a': 97, 'b': 98}, storage.get('third')); |
| 127 |
clock.tick(2000); |
| 128 |
storage.collect(); |
| 129 |
assertNotNull(getEncryptedWrapper(storage, 'first')); |
| 130 |
assertUndefined(storage.get('second')); |
| 131 |
assertObjectEquals({'a': 97, 'b': 98}, storage.get('third')); |
| 132 |
mechanism.set(storage.hashKeyWithSecret_('first'), '"kaboom"'); |
| 133 |
storage.collect(); |
| 134 |
assertNotNull(getEncryptedWrapper(storage, 'first')); |
| 135 |
assertObjectEquals({'a': 97, 'b': 98}, storage.get('third')); |
| 136 |
storage.collect(true); |
| 137 |
assertUndefined(storage.get('first')); |
| 138 |
assertObjectEquals({'a': 97, 'b': 98}, storage.get('third')); |
| 139 |
|
| 140 |
// Clean up. |
| 141 |
storage.remove('third'); |
| 142 |
assertUndefined(storage.get('third')); |
| 143 |
clock.uninstall(); |
| 144 |
} |
| 145 |
|
| 146 |
function testSalting() { |
| 147 |
var mechanism = new goog.testing.storage.FakeMechanism(); |
| 148 |
var randomMock = new goog.testing.PseudoRandom(0, true); |
| 149 |
var storage = new goog.storage.EncryptedStorage(mechanism, 'secret'); |
| 150 |
|
| 151 |
// Same value under two different keys should appear very different, |
| 152 |
// even with the same salt. |
| 153 |
storage.set('one', 'Hello world!'); |
| 154 |
randomMock.seed(0); // Reset the generator so we get the same salt. |
| 155 |
storage.set('two', 'Hello world!'); |
| 156 |
var golden = getEncryptedData(storage, 'one'); |
| 157 |
assertRoughlyEquals( |
| 158 |
'Ciphertext did not change with keys', golden.length, |
| 159 |
hammingDistance(golden, getEncryptedData(storage, 'two')), 2); |
| 160 |
|
| 161 |
// Same key-value pair written second time should appear very different. |
| 162 |
storage.set('one', 'Hello world!'); |
| 163 |
assertRoughlyEquals( |
| 164 |
'Salting seems to have failed', golden.length, |
| 165 |
hammingDistance(golden, getEncryptedData(storage, 'one')), 2); |
| 166 |
|
| 167 |
// Clean up. |
| 168 |
storage.remove('1'); |
| 169 |
storage.remove('2'); |
| 170 |
randomMock.uninstall(); |
| 171 |
} |