crypto.test.ts (2162B)
1 import { AEAD, PasswordHash } from "#core/domain/crypto.ts"; 2 import { assertEquals } from "$std/assert/assert_equals.ts"; 3 import { assertThrows } from "$std/assert/assert_throws.ts"; 4 import * as fc from "fast-check"; 5 import { encodeBase58 } from "$std/encoding/base58.ts"; 6 7 Deno.test({ 8 name: "unit password hash should verify password", 9 fn() { 10 fc.assert( 11 fc.property( 12 fc.string(), 13 (candidate) => PasswordHash.hash(candidate).verify(candidate), 14 ), 15 { numRuns: 5 }, 16 ); 17 }, 18 }); 19 20 Deno.test({ 21 name: "unit password hash should not verify any other string", 22 fn() { 23 fc.assert( 24 fc.property(fc.string(), fc.string(), (a, b) => { 25 fc.pre(a !== b); 26 return !PasswordHash.hash(a).verify(b); 27 }), 28 { numRuns: 5 }, 29 ); 30 }, 31 }); 32 33 Deno.test({ 34 name: "unit encryption should decrypt its encrypted value", 35 fn() { 36 const aead = new AEAD(); 37 fc.assert(fc.property(fc.string(), (value) => { 38 const act = aead.decrypt(aead.encrypt(value)); 39 assertEquals(act, value); 40 })); 41 }, 42 }); 43 44 Deno.test({ 45 name: "unit encryption shouldn't parse any value", 46 fn() { 47 const aead = new AEAD(); 48 fc.assert(fc.property(fc.string(), (value) => { 49 const act = () => aead.decrypt(encodeBase58(value)); 50 assertThrows(act); 51 })); 52 }, 53 }); 54 55 Deno.test({ 56 name: "unit encryption shouldn't parse same value in different context", 57 fn() { 58 const aead = new AEAD(); 59 fc.assert( 60 fc.property( 61 fc.string(), 62 fc.string(), 63 fc.string(), 64 (value, contextA, contextB) => { 65 fc.pre(contextA !== contextB); 66 const act = () => 67 aead.decrypt(aead.encrypt(value, contextA), contextB); 68 assertThrows(act); 69 }, 70 ), 71 ); 72 }, 73 }); 74 75 Deno.test({ 76 name: "unit encryption should parse same value in same context", 77 fn() { 78 const aead = new AEAD(); 79 fc.assert( 80 fc.property( 81 fc.string(), 82 fc.string(), 83 (value, context) => { 84 const act = aead.decrypt(aead.encrypt(value, context), context); 85 assertEquals(act, value); 86 }, 87 ), 88 ); 89 }, 90 });