-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathencrypt.test.ts
117 lines (112 loc) · 4.51 KB
/
encrypt.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import { beforeAll, describe, expect, test } from '@jest/globals';
import path from "path";
import { Vault } from '../src/Vault';
import { LocalStorageProvider } from '../src/providers/LocalStorageProvider';
import { EncryptedFile } from '../src/encrypted/EncryptedFile';
import { DirID } from '../src/types';
import { DecryptionError, DecryptionTarget } from '../src/Errors';
import crypto from 'node:crypto';
import { TargetFS } from './TargetFS';
import { EncryptedDir } from '../src/encrypted/EncryptedDir';
async function randomBuffer(size: number): Promise<Uint8Array>{
const arr = new Uint8Array(size);
for(let i = 0; i < size; i++) arr[i] = Math.floor(Math.random() * 256);
return arr;
}
describe('Test creating a vault', () => {
const provider = new LocalStorageProvider();
const dir = path.resolve(__dirname, 'encryptionTest');
beforeAll(async () => {
const testVaults = await provider.listItems(dir);
for(const v of testVaults) await provider.removeDir(v.fullName);
});
test('Try creating a vault', async () => {
await Vault.create(provider, dir, '12341234', {
create: {
name: 'encTest1'
}
});
await expect(Vault.open(provider, path.resolve(dir, 'encTest1'), '1234123', 'encTest1')).rejects.toThrowError(DecryptionError<DecryptionTarget.Vault>);
await expect(Vault.open(provider, path.resolve(dir, 'encTest1'), '12341234', 'encTest1')).resolves.not.toThrowError();
});
test('Try adding a file in root', async () => {
const v = await Vault.create(provider, dir, '12341234', {
create: {
name: 'encTest2'
}
});
const testFunction = async () => {
const limit = 20;
const buffers: Uint8Array[] = [];
const hashes: Uint8Array[] = [];
for(let i = 0; i < limit; i++) {
const b = await randomBuffer(Math.pow(2, i));
buffers.push(b);
hashes.push(crypto.createHash('sha256').update(b).digest());
await EncryptedFile.encrypt(v, `TestFile${i}.bin`, '' as DirID, b);
}
const items = await v.listItems('' as DirID);
for(let i = 0; i < limit; i++){
const item = items.find(it => it.decryptedName === `TestFile${i}.bin`);
if(!item || item.type === 'd') return false;
const fileContent = await item.decrypt();
const hash = crypto.createHash('sha256').update(fileContent.content).digest();
if(Buffer.compare(hash, hashes[i]) !== 0) return false;
}
return true;
}
await expect(testFunction()).resolves.toBe(true);
});
test('Create a random tree within a vault', async () => {
const sample = await TargetFS.create(provider, dir, 3, 32);
await expect(sample.verify()).resolves.toBe('Identical');
});
test('Create a random tree with very long names within a vault', async () => {
const sample = await TargetFS.create(provider, dir, 4, 777);
await expect(sample.verify()).resolves.toBe('Identical');
});
test('Create a random tree within a vault, and delete some folders at random', async () => {
const sample = await TargetFS.create(provider, dir, 5, 32);
const f = async () => {
for(const k in sample.tree){
// Open a directory with parent ID of dirId
const dirId = k as DirID;
for(const item of sample.tree[dirId].children){
if(item.type === 'f') continue;
// Randomly select a directory within that parent directory
if(Math.floor(Math.random() * 5) === 0){
sample.delFolder(dirId, item.id);
const items = await sample.vault.listItems(dirId);
const dir = items.find(i => i.decryptedName === item.name) as EncryptedDir;
if(dir) await dir.deleteDir();
else throw new Error(`Item not found: ${dirId}`);
}
}
}
return await sample.verify();
}
await expect(f()).resolves.toBe('Identical');
});
test('Create a random tree within a vault containing files and folders with long name, and delete some folders at random', async () => {
const sample = await TargetFS.create(provider, dir, 6, 888);
const f = async () => {
for(const k in sample.tree){
// Open a directory with parent ID of dirId
const dirId = k as DirID;
for(const item of sample.tree[dirId].children){
if(item.type === 'f') continue;
// Randomly select a directory within that parent directory
if(Math.floor(Math.random() * 5) === 0){
sample.delFolder(dirId, item.id);
const items = await sample.vault.listItems(dirId);
const dir = items.find(i => i.decryptedName === item.name) as EncryptedDir;
if(dir) await dir.deleteDir();
else throw new Error(`Item not found: ${dirId}`);
}
}
}
return await sample.verify();
}
await expect(f()).resolves.toBe('Identical');
});
});