diff --git a/packages/vc-test-suite/__tests__/storage.helper.test.ts b/packages/vc-test-suite/__tests__/storage.helper.test.ts index bc01dcad..720550d8 100644 --- a/packages/vc-test-suite/__tests__/storage.helper.test.ts +++ b/packages/vc-test-suite/__tests__/storage.helper.test.ts @@ -23,8 +23,9 @@ describe('decryptData', () => { tag: 'mockTag', }; const mockKey = 'mockKey'; - const mockDecryptedBase64 = 'ZGVjcnlwdGVkIGRhdGE='; // base64 for "decrypted data" - const mockDecrypted = 'decrypted data'; + const mockDecryptedJSON = '{"key": "value"}'; + const mockDecryptedObject = { key: 'value' }; + const mockDecryptedNonJSON = 'non-JSON string'; beforeEach(() => { jest.clearAllMocks(); @@ -34,48 +35,37 @@ describe('decryptData', () => { jest.restoreAllMocks(); }); - it('should return decrypted data when given valid input', () => { + it('should return parsed JSON object when decrypted data is valid JSON', () => { (crypto.createDecipheriv as jest.Mock).mockReturnValue({ setAuthTag: jest.fn(), - update: jest.fn().mockReturnValue(mockDecryptedBase64), + update: jest.fn().mockReturnValue(mockDecryptedJSON), final: jest.fn().mockReturnValue(''), }); const result = decryptData(mockEncryptedData, mockKey); - expect(result).toBe(mockDecrypted); + expect(result).toEqual(mockDecryptedObject); }); - it('should throw an error if decryption fails', () => { + it('should return decrypted string when decrypted data is not valid JSON', () => { (crypto.createDecipheriv as jest.Mock).mockReturnValue({ setAuthTag: jest.fn(), - update: jest.fn(), - final: jest.fn().mockImplementation(() => { - throw new Error('Decryption failed'); - }), + update: jest.fn().mockReturnValue(mockDecryptedNonJSON), + final: jest.fn().mockReturnValue(''), }); - expect(() => decryptData(mockEncryptedData, mockKey)).toThrow('Decryption failed'); + const result = decryptData(mockEncryptedData, mockKey); + expect(result).toBe(mockDecryptedNonJSON); }); - it('should return the original string for non-base64 decryption result', () => { - const nonBase64String = 'not a base64 string'; + it('should throw an error if decryption fails', () => { (crypto.createDecipheriv as jest.Mock).mockReturnValue({ setAuthTag: jest.fn(), - update: jest.fn().mockReturnValue(nonBase64String), - final: jest.fn().mockReturnValue(''), - }); - - const originalBufferFrom = Buffer.from; - jest.spyOn(Buffer, 'from').mockImplementation((input: any, encoding?: string) => { - if (input === nonBase64String && encoding === 'base64') { - throw new Error('Invalid base64 string'); - } - return originalBufferFrom(input); + update: jest.fn(), + final: jest.fn().mockImplementation(() => { + throw new Error('Decryption failed'); + }), }); - const result = decryptData(mockEncryptedData, mockKey); - - expect(result).toBe(nonBase64String); - expect(Buffer.from).toHaveBeenCalledWith(nonBase64String, 'base64'); + expect(() => decryptData(mockEncryptedData, mockKey)).toThrow('Decryption failed'); }); }); diff --git a/packages/vc-test-suite/tests/Storage/helper.ts b/packages/vc-test-suite/tests/Storage/helper.ts index 22d93c6e..5a544999 100644 --- a/packages/vc-test-suite/tests/Storage/helper.ts +++ b/packages/vc-test-suite/tests/Storage/helper.ts @@ -21,15 +21,14 @@ interface EncryptedData { * @param {string} key - Decryption key in hexadecimal format * @returns {string} - Decrypted data */ -export function decryptData({ cipherText, iv, tag }: EncryptedData, key: string): string { +export function decryptData({ cipherText, iv, tag }: EncryptedData, key: string): Record | string { const decipher = crypto.createDecipheriv('aes-256-gcm', Buffer.from(key, 'hex'), Buffer.from(iv, 'base64')); decipher.setAuthTag(Buffer.from(tag, 'base64')); let decrypted = decipher.update(cipherText, 'base64', 'utf8'); decrypted += decipher.final('utf8'); - // Try to decode the base64 string, if it fails, return the original string try { - return Buffer.from(decrypted, 'base64').toString('utf8'); + return JSON.parse(decrypted); } catch (error) { return decrypted; } diff --git a/packages/vc-test-suite/tests/Storage/storage.test.ts b/packages/vc-test-suite/tests/Storage/storage.test.ts index aab60efb..02396f1b 100644 --- a/packages/vc-test-suite/tests/Storage/storage.test.ts +++ b/packages/vc-test-suite/tests/Storage/storage.test.ts @@ -153,17 +153,15 @@ describe('Storage Service', function () { expect(fetchResponse.data).to.have.property('iv'); expect(fetchResponse.data).to.have.property('tag'); expect(fetchResponse.data).to.have.property('type'); - expect(fetchResponse.data.type).to.equal('AES-GCM'); - - const decryptedData = JSON.parse( - decryptData( - { - cipherText: fetchResponse.data.cipherText, - iv: fetchResponse.data.iv, - tag: fetchResponse.data.tag, - }, - response.data.key, - ), + expect(fetchResponse.data.type).to.equal('aes-256-gcm'); + + const decryptedData = decryptData( + { + cipherText: fetchResponse.data.cipherText, + iv: fetchResponse.data.iv, + tag: fetchResponse.data.tag, + }, + response.data.key, ); expect(decryptedData).to.deep.equal(payload.data); @@ -199,17 +197,15 @@ describe('Storage Service', function () { expect(fetchResponse.data).to.have.property('iv'); expect(fetchResponse.data).to.have.property('tag'); expect(fetchResponse.data).to.have.property('type'); - expect(fetchResponse.data.type).to.equal('AES-GCM'); - - const decryptedData = JSON.parse( - decryptData( - { - cipherText: fetchResponse.data.cipherText, - iv: fetchResponse.data.iv, - tag: fetchResponse.data.tag, - }, - storeResponse.data.key, - ), + expect(fetchResponse.data.type).to.equal('aes-256-gcm'); + + const decryptedData = decryptData( + { + cipherText: fetchResponse.data.cipherText, + iv: fetchResponse.data.iv, + tag: fetchResponse.data.tag, + }, + storeResponse.data.key, ); expect(decryptedData).to.deep.equal(payload.data); @@ -270,17 +266,15 @@ describe('Storage Service', function () { expect(fetchResponse.data).to.have.property('iv'); expect(fetchResponse.data).to.have.property('tag'); expect(fetchResponse.data).to.have.property('type'); - expect(fetchResponse.data.type).to.equal('AES-GCM'); - - const decryptedData = JSON.parse( - decryptData( - { - cipherText: fetchResponse.data.cipherText, - iv: fetchResponse.data.iv, - tag: fetchResponse.data.tag, - }, - response1.data.key, - ), + expect(fetchResponse.data.type).to.equal('aes-256-gcm'); + + const decryptedData = decryptData( + { + cipherText: fetchResponse.data.cipherText, + iv: fetchResponse.data.iv, + tag: fetchResponse.data.tag, + }, + response1.data.key, ); expect(decryptedData).to.deep.equal(payload1.data);