Skip to content

Commit

Permalink
test(background): write storage tests
Browse files Browse the repository at this point in the history
  • Loading branch information
moreal committed May 29, 2024
1 parent 8f278a5 commit 199fdf1
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 76 deletions.
1 change: 1 addition & 0 deletions background/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"@vue/test-utils": "^1.3.0",
"eslint": "^7.32.0",
"eslint-plugin-vue": "^8.7.1",
"fast-check": "^3.19.0",
"typescript": "^5.4.5",
"vite": "^5.2.8",
"vitest": "^1.6.0",
Expand Down
17 changes: 17 additions & 0 deletions background/test/storage/backend/memory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { IStorageBackend } from "@/storage/backend/index.js";

export class InMemoryStorageBackend implements IStorageBackend {
private readonly map: Map<string, any> = new Map();

constructor() {
this.map = new Map();
}

get<T>(key: string): Promise<T> {
return this.map.get(key);
}

async set<T>(key: string, value: T): Promise<void> {
this.map.set(key, value);
}
}
75 changes: 0 additions & 75 deletions background/test/storage/storage.test.js

This file was deleted.

83 changes: 83 additions & 0 deletions background/test/storage/storage.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import { Storage } from "@/storage/index.js"
import { InMemoryStorageBackend } from "./backend/memory.js"
import { describe, expect, beforeEach, test } from "vitest";
import { IStorageBackend } from "@/storage/backend/common.js";
import fc from "fast-check";
import aes256 from "@/utils/aes256.js";

// TODO: Check lazy passpharse.
const passphrase = 'Storage Passphrase'

describe("Storage", () => {
let backend!: IStorageBackend;
let storage!: Storage;

beforeEach(() => {
backend = new InMemoryStorageBackend();
storage = new Storage(passphrase, backend);
});

describe('secureGet', () => {
test('should fail if the data is not secured', async () => {
fc.assert(fc.asyncProperty(fc.string(), fc.string(), async (key, value) => {
const v = await aes256.encrypt(JSON.stringify(value), passphrase);
await backend.set(`${key}-no-secure`, JSON.stringify({ v: v, }))
await backend.set(`${key}-secure-false`, JSON.stringify({ secure: false, v: v }))

await expect(async () => await storage.secureGet(`${key}-no-secure`)).rejects.toThrowError("SecureGet has accessed to not secured data");
await expect(async () => await storage.secureGet(`${key}-secure-false`)).rejects.toThrowError("SecureGet has accessed to not secured data");
}));
});

test('should success if the data is secured', async () => {
fc.assert(fc.asyncProperty(fc.string(), fc.string(), async (key, value) => {
const v = await aes256.encrypt(JSON.stringify(value), passphrase);
await backend.set(key, JSON.stringify({ secure: true, v: v, }))

await expect(storage.secureGet(key)).resolves.toEqual(value);
}));
});
});

describe('get', () => {
test('should fail if the data is secured', async () => {
fc.assert(fc.asyncProperty(fc.string(), fc.string(), async (key, value) => {
const v = await aes256.encrypt(JSON.stringify(value), passphrase);
await backend.set(key, JSON.stringify({ secure: true, v: v }))

await expect(async () => await storage.secureGet(key)).rejects.toThrowError("Can not access secure data");
}));
});

test('should success if the data is not secured', async () => {
fc.assert(fc.asyncProperty(fc.string(), fc.string(), async (key, value) => {
await backend.set(key, JSON.stringify({ v: value, }));
await expect(storage.get(key)).resolves.toEqual(value);
}));
});
});

describe('set', () => {
test('should store non-secured data', async () => {
fc.assert(fc.asyncProperty(fc.string(), fc.string(), async (key, value) => {
await storage.set(key, value);

const raw = JSON.parse(await backend.get(key));
expect(!!raw.secure).toEqual(false);
expect(raw.v).toEqual(value);
}));
});
});

describe('secureSet', () => {
test('should store secured data', async () => {
fc.assert(fc.asyncProperty(fc.string(), fc.string(), async (key, value) => {
await storage.secureSet(key, value);

const raw = JSON.parse(await backend.get(key));
expect(raw.secure).toEqual(true);
expect(raw.v).not.toEqual(value);
}));
});
});
})
2 changes: 1 addition & 1 deletion background/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@
"@/*": ["./src/*"]
}
},
"include": ["src"]
"include": ["src", "test"]
}
16 changes: 16 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 199fdf1

Please sign in to comment.