|
| 1 | +import { MAX_KDF_PROMISE_CACHE_SIZE } from './constants'; |
1 | 2 | import encryption, { createSHA256Hash } from './encryption';
|
2 | 3 |
|
3 | 4 | describe('encryption tests', () => {
|
@@ -112,4 +113,157 @@ describe('encryption tests', () => {
|
112 | 113 | expect(result).toBe(false);
|
113 | 114 | });
|
114 | 115 | });
|
| 116 | + |
| 117 | + describe('Deferred Promise KDF Functionality', () => { |
| 118 | + it('should handle concurrent encryption operations with same password', async () => { |
| 119 | + const password = 'test-password-concurrent'; |
| 120 | + const plaintext = 'test-data'; |
| 121 | + |
| 122 | + // Start multiple concurrent encryption operations |
| 123 | + const promises = Array(3) |
| 124 | + .fill(0) |
| 125 | + .map(async (_, i) => { |
| 126 | + return encryption.encryptString(`${plaintext}-${i}`, password); |
| 127 | + }); |
| 128 | + |
| 129 | + const results = await Promise.all(promises); |
| 130 | + expect(results).toHaveLength(3); |
| 131 | + |
| 132 | + // Verify all results can be decrypted |
| 133 | + for (let i = 0; i < results.length; i++) { |
| 134 | + const decrypted = await encryption.decryptString(results[i], password); |
| 135 | + expect(decrypted).toBe(`${plaintext}-${i}`); |
| 136 | + } |
| 137 | + }); |
| 138 | + |
| 139 | + it('should handle concurrent encrypt/decrypt operations', async () => { |
| 140 | + const password = 'test-concurrent-mixed'; |
| 141 | + const testData = 'concurrent-test-data'; |
| 142 | + |
| 143 | + // First encrypt some data |
| 144 | + const encryptedData = await encryption.encryptString(testData, password); |
| 145 | + |
| 146 | + // Start concurrent operations |
| 147 | + const decryptPromises = Array(2) |
| 148 | + .fill(0) |
| 149 | + .map(() => { |
| 150 | + return encryption.decryptString(encryptedData, password); |
| 151 | + }); |
| 152 | + |
| 153 | + const encryptPromises = Array(2) |
| 154 | + .fill(0) |
| 155 | + .map((_, i) => { |
| 156 | + return encryption.encryptString(`new-data-${i}`, password); |
| 157 | + }); |
| 158 | + |
| 159 | + const allResults = await Promise.all([ |
| 160 | + ...decryptPromises, |
| 161 | + ...encryptPromises, |
| 162 | + ]); |
| 163 | + |
| 164 | + // Verify decrypt results |
| 165 | + expect(allResults[0]).toBe(testData); |
| 166 | + expect(allResults[1]).toBe(testData); |
| 167 | + |
| 168 | + // Verify encrypt results can be decrypted |
| 169 | + const newDecrypted0 = await encryption.decryptString( |
| 170 | + allResults[2], |
| 171 | + password, |
| 172 | + ); |
| 173 | + const newDecrypted1 = await encryption.decryptString( |
| 174 | + allResults[3], |
| 175 | + password, |
| 176 | + ); |
| 177 | + expect(newDecrypted0).toBe('new-data-0'); |
| 178 | + expect(newDecrypted1).toBe('new-data-1'); |
| 179 | + }); |
| 180 | + |
| 181 | + it('should handle different passwords concurrently', async () => { |
| 182 | + const password1 = 'password-one'; |
| 183 | + const password2 = 'password-two'; |
| 184 | + const testData = 'multi-password-test'; |
| 185 | + |
| 186 | + // Start concurrent operations with different passwords |
| 187 | + const promises = [ |
| 188 | + encryption.encryptString(testData, password1), |
| 189 | + encryption.encryptString(testData, password2), |
| 190 | + ]; |
| 191 | + |
| 192 | + const results = await Promise.all(promises); |
| 193 | + expect(results).toHaveLength(2); |
| 194 | + |
| 195 | + // Verify decryption with correct passwords |
| 196 | + const decrypted1 = await encryption.decryptString(results[0], password1); |
| 197 | + const decrypted2 = await encryption.decryptString(results[1], password2); |
| 198 | + |
| 199 | + expect(decrypted1).toBe(testData); |
| 200 | + expect(decrypted2).toBe(testData); |
| 201 | + |
| 202 | + // Cross-password decryption should fail |
| 203 | + await expect( |
| 204 | + encryption.decryptString(results[0], password2), |
| 205 | + ).rejects.toThrow( |
| 206 | + 'Unable to decrypt string - aes/gcm: invalid ghash tag', |
| 207 | + ); |
| 208 | + await expect( |
| 209 | + encryption.decryptString(results[1], password1), |
| 210 | + ).rejects.toThrow( |
| 211 | + 'Unable to decrypt string - aes/gcm: invalid ghash tag', |
| 212 | + ); |
| 213 | + }); |
| 214 | + |
| 215 | + it('should work correctly under concurrent load', async () => { |
| 216 | + const password = 'load-test-password'; |
| 217 | + const baseData = 'load-test-data'; |
| 218 | + |
| 219 | + // Create a larger number of concurrent operations |
| 220 | + const encryptPromises = Array(10) |
| 221 | + .fill(0) |
| 222 | + .map((_, i) => encryption.encryptString(`${baseData}-${i}`, password)); |
| 223 | + |
| 224 | + const results = await Promise.all(encryptPromises); |
| 225 | + expect(results).toHaveLength(10); |
| 226 | + |
| 227 | + // Verify all can be decrypted |
| 228 | + const decryptPromises = results.map((encrypted, i) => |
| 229 | + encryption.decryptString(encrypted, password).then((decrypted) => { |
| 230 | + expect(decrypted).toBe(`${baseData}-${i}`); |
| 231 | + return decrypted; |
| 232 | + }), |
| 233 | + ); |
| 234 | + |
| 235 | + await Promise.all(decryptPromises); |
| 236 | + }); |
| 237 | + |
| 238 | + it('should limit KDF promise cache size and remove oldest entries when limit is reached', async () => { |
| 239 | + // Create enough operations to exceed the actual cache limit |
| 240 | + const numOperations = MAX_KDF_PROMISE_CACHE_SIZE + 5; // 25 operations to exceed the limit |
| 241 | + |
| 242 | + const promises: Promise<string>[] = []; |
| 243 | + for (let i = 0; i < numOperations; i++) { |
| 244 | + // Use different passwords to create unique cache keys |
| 245 | + const uniquePassword = `cache-test-${i}`; |
| 246 | + promises.push(encryption.encryptString('test-data', uniquePassword)); |
| 247 | + } |
| 248 | + |
| 249 | + // All operations should complete successfully despite cache limit |
| 250 | + const results = await Promise.all(promises); |
| 251 | + expect(results).toHaveLength(numOperations); |
| 252 | + |
| 253 | + // Verify a sampling of results can be decrypted (testing all 25 would be slow) |
| 254 | + const sampleIndices = [ |
| 255 | + 0, |
| 256 | + Math.floor(MAX_KDF_PROMISE_CACHE_SIZE / 2), |
| 257 | + numOperations - 1, |
| 258 | + ]; // Test first, middle, and last |
| 259 | + for (const i of sampleIndices) { |
| 260 | + const uniquePassword = `cache-test-${i}`; |
| 261 | + const decrypted = await encryption.decryptString( |
| 262 | + results[i], |
| 263 | + uniquePassword, |
| 264 | + ); |
| 265 | + expect(decrypted).toBe('test-data'); |
| 266 | + } |
| 267 | + }, 30000); |
| 268 | + }); |
115 | 269 | });
|
0 commit comments