-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdecrypt.test.ts
139 lines (126 loc) · 5.58 KB
/
decrypt.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import { describe, expect, test } from '@jest/globals';
import path from "path";
import { Vault } from '../src/Vault';
import { LocalStorageProvider } from '../src/providers/LocalStorageProvider';
import { DirID, ItemPath } from '../src/types';
import { EncryptedFile } from '../src/encrypted/EncryptedFile';
import { InvalidSignatureError } from '../src/Errors';
async function decrypt(provider: LocalStorageProvider, password: string, vaultNumber: number, options?: {
vaultFile?: ItemPath
}): Promise<Vault>{
const v = await Vault.open(
provider,
path.resolve(__dirname, 'decryptionTest', `vault${vaultNumber}`),
password,
'Test Vault',
options
? options
: {
vaultFile: vaultNumber === 1
? path.resolve(__dirname, 'decryptionTest', 'vault1', 'vault-valid.cryptomator') as ItemPath
: path.resolve(__dirname, 'decryptionTest', `vault${vaultNumber}`, 'vault.cryptomator') as ItemPath
});
return v;
}
async function saveDecrypted(provider: LocalStorageProvider, file: EncryptedFile) {
const decrypted = await file.decryptAsString();
const testFileName = new Date().getTime() + decrypted.title.slice(-20);
console.log('Output generated: ' + testFileName)
const decryptedFile = path.resolve(__dirname, 'output', testFileName);
await provider.writeFile(decryptedFile, decrypted.content);
return decryptedFile;
}
describe('Test opening an existing vault', () => {
const provider = new LocalStorageProvider();
test('Wrong password should throw an error', async () => {
await expect(decrypt(provider, 'qq11@11', 1)).rejects.toThrowError();
await expect(decrypt(provider, '', 1)).rejects.toThrowError();
await expect(decrypt(provider, 'qq11@@@11', 1)).rejects.toThrowError();
});
test('Vault opening should fail if vault.cryptomator is invalid', async () => {
await expect(decrypt(provider, 'qq11@@11', 1, {vaultFile: path.resolve(__dirname, 'decryptionTest', 'vault1', 'vault-corrupted.cryptomator') as ItemPath})).rejects.toThrowError(InvalidSignatureError);
});
test('Try opening a vault with a correct password', async () => {
await expect(decrypt(provider, 'qq11@@11', 1)).resolves.not.toThrowError();
});
test('Try listing encrypted items in root', async () => {
const vault = await decrypt(provider, 'qq11@@11', 1);
await expect(vault.listEncrypted('' as DirID)).resolves.not.toThrowError();
});
test('Try decrypting names of items in root', async () => {
const vault = await decrypt(provider, 'qq11@@11', 1);
const f = async () => {
const count = [0, 0];
const items = await vault.listItems('' as DirID);
for(const i of items){
if(i.type === 'd') count[0]++;
else if(i.type === 'f') count[1]++;
}
return count;
}
await expect(f()).resolves.toStrictEqual([4, 1]);
});
test('Try decrypting names of items in root using root dir object', async () => {
const vault = await decrypt(provider, 'qq11@@11', 1);
const f = async () => {
const count = [0, 0];
const items = await (await vault.getRootDir()).listItems();
for(const i of items){
if(i.type === 'd') count[0]++;
else if(i.type === 'f') count[1]++;
}
return count;
}
await expect(f()).resolves.toStrictEqual([4, 1]);
});
test('Try getting directory ID of folders in root', async () => {
const vault = await decrypt(provider, 'qq11@@11', 1);
const items = await vault.listItems('' as DirID);
const folderNames: Promise<DirID>[] = [];
for(const item of items){
if(item.type === 'd') folderNames.push(item.getDirId());
}
await expect(Promise.all(folderNames)).resolves.not.toThrowError();
});
test('Try decrypting header of a file', async () => {
const vault = await decrypt(provider, 'qq11@@11', 1);
const items = await vault.listItems('' as DirID);
const firstFile = items.find(i => i.type === 'f');
const pendingContentKey = (firstFile! as EncryptedFile).decryptHeader();
await expect(pendingContentKey).resolves.not.toThrowError();
});
test('Try decrypting a file', async () => {
const vault = await decrypt(provider, 'qq11@@11', 1);
const items = await vault.listItems('' as DirID);
const firstFile = items.find(i => i.decryptedName === 'WELCOME.rtf') as EncryptedFile;
const decryptedFile = await saveDecrypted(provider, firstFile);
const read = async () => {
const str = await provider.readFileString(decryptedFile);
return str.includes('Cryptomator');
}
await expect(read()).resolves.toBe(true);
});
test('Try listing items with long names', async () => {
const vault = await decrypt(provider, '12341234', 2);
const f = async () => {
const items = await vault.listItems('' as DirID);
const longItems = items.filter(i => i.decryptedName.length > 220);
const foundDir = longItems.some(i => i.decryptedName.length > 220 && i.decryptedName.includes('A'.repeat(220)))
const foundFile = longItems.some(i => i.decryptedName.length > 220 && i.decryptedName.includes('B'.repeat(220)) && i.decryptedName.endsWith('.txt'));
return foundDir && foundFile;
}
await expect(f()).resolves.toBe(true);
});
test('Try decrypting file with long name', async () => {
const vault = await decrypt(provider, '12341234', 2);
const items = await vault.listItems('' as DirID);
const longItems = items.filter(i => i.decryptedName.length > 220);
const foundFile = longItems.find(i => i.decryptedName.length > 220 && i.decryptedName.includes('B'.repeat(220)) && i.decryptedName.endsWith('.txt')) as EncryptedFile;
const decryptedFile = await saveDecrypted(provider, foundFile);
const read = async () => {
const str = await provider.readFileString(decryptedFile);
return str.includes('Hello world');
}
await expect(read()).resolves.toBe(true);
});
});