-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(background): write storage tests
- Loading branch information
Showing
6 changed files
with
118 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
})); | ||
}); | ||
}); | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,5 +9,5 @@ | |
"@/*": ["./src/*"] | ||
} | ||
}, | ||
"include": ["src"] | ||
"include": ["src", "test"] | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.