deno.land / std@0.224.0 / crypto / unstable_keystack_test.ts
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
import { assert, assertEquals, assertThrows } from "../assert/mod.ts";
import { KeyStack } from "./unstable_keystack.ts";
Deno.test({ name: "KeyStack() throws on empty keys", fn() { assertThrows( () => new KeyStack([]), TypeError, "keys must contain at least one value", ); },});
Deno.test({ name: "keyStack.sign() handles single key", async fn() { const keys = ["hello"]; const keyStack = new KeyStack(keys); const actual = await keyStack.sign("world"); const expected = "8ayXAutfryPKKRpNxG3t3u4qeMza8KQSvtdxTP_7HMQ"; assertEquals(actual, expected); },});
Deno.test({ name: "keyStack.sign() handles two keys, first key used", async fn() { const keys = ["hello", "world"]; const keyStack = new KeyStack(keys); const actual = await keyStack.sign("world"); const expected = "8ayXAutfryPKKRpNxG3t3u4qeMza8KQSvtdxTP_7HMQ"; assertEquals(actual, expected); },});
Deno.test({ name: "keyStack.verify() handles single key", async fn() { const keys = ["hello"]; const keyStack = new KeyStack(keys); const digest = await keyStack.sign("world"); assert(await keyStack.verify("world", digest)); },});
Deno.test({ name: "keyStack.verify() handles single key verify invalid", async fn() { const keys = ["hello"]; const keyStack = new KeyStack(keys); const digest = await keyStack.sign("world"); assert(!await keyStack.verify("worlds", digest)); },});
Deno.test({ name: "keyStack.verify() handles two keys", async fn() { const keys = ["hello", "world"]; const keyStack = new KeyStack(keys); const digest = await keyStack.sign("world"); assert(await keyStack.verify("world", digest)); },});
Deno.test({ name: "keyStack.verify() handles unshift key", async fn() { const keys = ["hello"]; const keyStack = new KeyStack(keys); const digest = await keyStack.sign("world"); keys.unshift("world"); assertEquals(keys, ["world", "hello"]); assert(await keyStack.verify("world", digest)); },});
Deno.test({ name: "keyStack.verify() handles shift key", async fn() { const keys = ["hello", "world"]; const keyStack = new KeyStack(keys); const digest = await keyStack.sign("world"); assertEquals(keys.shift(), "hello"); assertEquals(keys, ["world"]); assert(!await keyStack.verify("world", digest)); },});
Deno.test({ name: "keyStack.indexOf() handles single key", async fn() { const keys = ["hello"]; const keyStack = new KeyStack(keys); assertEquals( await keyStack.indexOf( "world", "8ayXAutfryPKKRpNxG3t3u4qeMza8KQSvtdxTP_7HMQ", ), 0, ); },});
Deno.test({ name: "keyStack.indexOf() handles two keys index 0", async fn() { const keys = ["hello", "world"]; const keyStack = new KeyStack(keys); assertEquals( await keyStack.indexOf( "world", "8ayXAutfryPKKRpNxG3t3u4qeMza8KQSvtdxTP_7HMQ", ), 0, ); },});
Deno.test({ name: "keyStack.indexOf() handles two keys index 1", async fn() { const keys = ["world", "hello"]; const keyStack = new KeyStack(keys); assertEquals( await keyStack.indexOf( "world", "8ayXAutfryPKKRpNxG3t3u4qeMza8KQSvtdxTP_7HMQ", ), 1, ); },});
Deno.test({ name: "keyStack.indexOf() handles two keys not found", async fn() { const keys = ["world", "hello"]; const keyStack = new KeyStack(keys); assertEquals( await keyStack.indexOf( "hello", "8ayXAutfryPKKRpNxG3t3u4qeMza8KQSvtdxTP_7HMQ", ), -1, ); },});
Deno.test({ name: "keyStack.verify() handles number array key", async fn() { const keys = [[212, 213]]; const keyStack = new KeyStack(keys); assert(await keyStack.verify("hello", await keyStack.sign("hello"))); },});
Deno.test({ name: "keyStack.verify() handles Uint8Array key", async fn() { const keys = [new Uint8Array([212, 213])]; const keyStack = new KeyStack(keys); assert(await keyStack.verify("hello", await keyStack.sign("hello"))); },});
Deno.test({ name: "verify() handles ArrayBuffer key", async fn() { const key = new ArrayBuffer(2); const dataView = new DataView(key); dataView.setInt8(0, 212); dataView.setInt8(1, 213); const keys = [key]; const keyStack = new KeyStack(keys); assert(await keyStack.verify("hello", await keyStack.sign("hello"))); },});
Deno.test({ name: "verify() handles number array data", async fn() { const keys = [[212, 213]]; const keyStack = new KeyStack(keys); assert(await keyStack.verify([212, 213], await keyStack.sign([212, 213]))); },});
Deno.test({ name: "verify() handles Uint8Array data", async fn() { const keys = [[212, 213]]; const keyStack = new KeyStack(keys); assert( await keyStack.verify( new Uint8Array([212, 213]), await keyStack.sign(new Uint8Array([212, 213])), ), ); },});
Deno.test({ name: "verify() handles ArrayBuffer data", async fn() { const keys = [[212, 213]]; const keyStack = new KeyStack(keys); const data1 = new ArrayBuffer(2); const dataView1 = new DataView(data1); dataView1.setInt8(0, 212); dataView1.setInt8(1, 213); const data2 = new ArrayBuffer(2); const dataView2 = new DataView(data2); dataView2.setInt8(0, 212); dataView2.setInt8(1, 213); assert(await keyStack.verify(data2, await keyStack.sign(data1))); },});
Deno.test({ name: "verify() handles user iterable keys", async fn() { const keys = new Set(["hello", "world"]); const keyStack = new KeyStack(keys); const actual = await keyStack.sign("world"); const expected = "8ayXAutfryPKKRpNxG3t3u4qeMza8KQSvtdxTP_7HMQ"; assertEquals(actual, expected); },});
Deno.test({ name: "KeyStack() handles inspection in Deno", fn() { assertEquals( Deno.inspect(new KeyStack(["abcdef"])), `KeyStack { length: 1 }`, ); },});
Deno.test({ name: "KeyStack() handles inspection in Node", async fn() { const { inspect } = await import("node:util");
assertEquals( inspect(new KeyStack(["abcdef"])), `KeyStack { length: 1 }`, ); },});
Version Info