Skip to content

Commit 0212765

Browse files
committed
fix: bug in encryption function, corrected tests
1 parent 3fb05c7 commit 0212765

File tree

5 files changed

+48
-19
lines changed

5 files changed

+48
-19
lines changed

src/core/encryption.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,11 @@ export async function encrypt(publicKeyString: string, plaintext: string) {
164164
for (let i = 0; i < data.length; i += MAX_CHUNK_SIZE) {
165165
const chunk = data.subarray(i, i + MAX_CHUNK_SIZE)
166166
const encryptedChunk = await encryptChunk(publicKey, chunk)
167-
const base64Chunk = encodeBase64(encryptedChunk)
168-
chunks.push(base64Chunk)
167+
const base64ChunkResult = encodeBase64(encryptedChunk)
168+
if (base64ChunkResult.err) {
169+
return base64ChunkResult
170+
}
171+
chunks.push(base64ChunkResult.val)
169172
}
170173

171174
return Ok(chunks.join(CHUNK_SEPARATOR))

src/core/errors.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,21 +80,21 @@ export const createHumanLog = createHumanLogs({
8080
},
8181
encrypt_failed_invalid_key_type: {
8282
template:
83-
'because encrypting failed due to invalid public key type. Expected "string", received "{publicKey}"',
83+
'because encrypting failed due to invalid public key type. Expected "string", received "{publicKey}".',
8484
params: {
8585
publicKey: ''
8686
}
8787
},
8888
decrypt_failed_invalid_ciphertext_type: {
8989
template:
90-
'because encrypting failed due to invalid ciphertext type. Expected "string", received "{ciphertext}".',
90+
'because decrypting failed due to invalid ciphertext type. Expected "string", received "{ciphertext}".',
9191
params: {
9292
ciphertext: ''
9393
}
9494
},
9595
decrypt_failed_invalid_key_type: {
9696
template:
97-
'because encrypting failed due to invalid private key type. Expected "string", received "{privateKey}"',
97+
'because decrypting failed due to invalid private key type. Expected "string", received "{privateKey}".',
9898
params: {
9999
privateKey: ''
100100
}

test/encryption.test.ts

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,27 @@ describe('encrypt', () => {
3333
expect(encrypted.slice(-2)).toBe('==')
3434
})
3535
it('throws for anything other than a string', async () => {
36-
const keyPair = await generateKeyPair()
36+
const keyPair = (await generateKeyPair()).unwrap()
3737
// @ts-expect-error
38-
await expect(encrypt(keyPair.publicKey, {})).rejects.toThrow()
38+
expect(toOneLine((await encrypt(keyPair.publicKey)).val.toString())).toBe(
39+
toOneLine(
40+
`because encrypting failed due to invalid plaintext type. Expected "string", received ",".`
41+
)
42+
)
3943
})
4044

4145
it('throws for invalid public key', async () => {
4246
// @ts-expect-error
43-
await expect(encrypt({}, 'Hello, World!')).rejects.toThrow()
44-
await expect(encrypt('invalid public key', 'Hello, World!')).rejects.toThrow()
47+
expect(toOneLine((await encrypt({}, 'Hello, World!')).val.toString())).toBe(
48+
toOneLine(
49+
`because encrypting failed due to invalid public key type. Expected "string", received "[object Object]".`
50+
)
51+
)
52+
expect(toOneLine((await encrypt('invalid public key', 'Hello, World!')).val.toString())).toBe(
53+
toOneLine(
54+
`because base64 decoding the value "," errored. Error: InvalidCharacterError: The string to be decoded is not correctly encoded.`
55+
)
56+
)
4557
})
4658

4759
it('encrypts large amount of text', async () => {
@@ -59,10 +71,10 @@ describe('decrypt', () => {
5971
const plaintext = 'Hello, World!'
6072
const keyPair = (await generateKeyPair()).unwrap()
6173
const encrypted = (await encrypt(keyPair.publicKey, plaintext)).unwrap()
62-
const decrypted = await decrypt(keyPair.privateKey, encrypted)
74+
const decrypted = await (await decrypt(keyPair.privateKey, encrypted)).unwrap()
6375
expect(decrypted).toBe(plaintext)
6476
})
65-
it.only('throws when decrypting with wrong private key', async () => {
77+
it('Returns Err result when decrypting with wrong private key', async () => {
6678
const plaintext = 'Hello, World!'
6779
const keyPair = (await generateKeyPair()).unwrap()
6880
const encrypted = (await encrypt(keyPair.publicKey, plaintext)).unwrap()
@@ -75,15 +87,27 @@ describe('decrypt', () => {
7587
// @ts-expect-error
7688
expect(toOneLine((await decrypt({}, encrypted)).val.toString())).toBe(
7789
toOneLine(
78-
`because encrypting failed due to invalid private key type. Expected "string", received "[object Object]"`
90+
`because decrypting failed due to invalid private key type. Expected "string", received "[object Object]".`
7991
)
8092
)
8193
})
82-
it('throws when input isnt a string', async () => {
94+
it('Returns Err result when input isnt a string', async () => {
8395
const keyPair = (await generateKeyPair()).unwrap()
96+
8497
// @ts-expect-error
85-
await expect(decrypt(keyPair.privateKey, {})).rejects.toThrow()
86-
await expect(decrypt(keyPair.privateKey, 'invalid ciphertext')).rejects.toThrow()
98+
expect(toOneLine((await decrypt(keyPair.privateKey, {})).val.toString())).toBe(
99+
toOneLine(
100+
`because decrypting failed due to invalid ciphertext type. Expected "string", received "[object Object]".`
101+
)
102+
)
103+
104+
expect(
105+
toOneLine((await decrypt(keyPair.privateKey, 'invalid ciphertext')).val.toString())
106+
).toBe(
107+
toOneLine(
108+
`because base64 decoding the value "invalid ciphertext" errored. Error: InvalidCharacterError: The string to be decoded is not correctly encoded.`
109+
)
110+
)
87111
})
88112

89113
it('decrypts large amount of text', async () => {

test/stringify.test.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,14 @@ it('removes cyclical dependencies', () => {
2929

3030
const stringifiedWithoutCircular = safeStringify(a).unwrap()
3131

32-
expect(stringifiedWithoutCircular).toEqual('{"json":{"b":2,"c":{"d":{"e":null}}},"meta":{}}')
32+
expect(stringifiedWithoutCircular).toEqual(
33+
'{"json":{"b":2,"c":{"d":{"e":"FLYTRAP_UNSERIALIZABLE_VALUE"}}}}'
34+
)
3335
expect(SuperJSON.parse(stringifiedWithoutCircular)).toEqual({
3436
b: 2,
3537
c: {
3638
d: {
37-
e: null
39+
e: FLYTRAP_UNSERIALIZABLE_VALUE
3840
}
3941
}
4042
})
@@ -222,7 +224,7 @@ it('processCaptures', () => {
222224
]) */
223225
})
224226

225-
describe.only('new stringify', () => {
227+
describe('new stringify', () => {
226228
// Circulars
227229
const a = {
228230
b: '23',

test/transform-uff.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ describe('functions', () => {
116116
}
117117
})
118118

119-
it.only('callee and accessorKey are correct', () => {
119+
it('callee and accessorKey are correct', () => {
120120
const accessorKeyFixtures = [
121121
/* {
122122
fixture: 'parseFloat',

0 commit comments

Comments
 (0)