-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathblock.test.js
More file actions
115 lines (107 loc) · 3.08 KB
/
block.test.js
File metadata and controls
115 lines (107 loc) · 3.08 KB
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
const Crypto = require('node:crypto');
const Block = require('./common/block');
const Util = require('../common/common-util');
const {
connectUser,
getRandomKeys,
getBlockPath
} = require('./common/utils.js');
const keys = getRandomKeys();
const cryptKey = Crypto.randomBytes(32);
const blockKeys = {
sign: keys,
symmetric: cryptKey
};
const randomString = Crypto.randomBytes(16).toString('hex');
const log = (require.main === module) ? console.log : function () {};
log('Block path', getBlockPath(keys.edPublic));
const writeBlock = args => {
return new Promise((resolve, reject) => {
Block.writeLoginBlock({
blockKeys,
content: {
randomString,
test: true
},
}, (e, res) => {
if (e) {
console.error(e, res);
return reject(e);
}
resolve(args);
});
});
};
const checkBlock = args => {
return new Promise((resolve, reject) => {
const url = Block.getBlockUrl(blockKeys);
log('Block URL:', url);
Util.getBlock(url, {}, (err, res) => {
if (err) { return reject(err); }
res.arrayBuffer().then(arraybuffer => {
const block = new Uint8Array(arraybuffer);
let decryptedBlock = Block.decrypt(block, blockKeys);
if (!decryptedBlock) {
return reject('DECRYPTION_ERROR');
}
if (decryptedBlock.randomString === randomString
&& decryptedBlock.test) {
return resolve(args);
}
reject('INVALID_BLOCK_CONTENT');
});
});
});
};
const removeBlock = args => {
return new Promise((resolve, reject) => {
Block.removeLoginBlock({
blockKeys,
reason: 'INTEGRATION_TEST'
}, (e, res) => {
if (e) {
console.error(e, res);
return reject(e);
}
resolve(args);
});
});
};
const checkRemovedBlock = args => {
return new Promise((resolve, reject) => {
const url = Block.getBlockUrl(blockKeys);
Util.getBlock(url, {}, (e, res) => {
if (e === 404 && res?.reason === 'INTEGRATION_TEST') {
return resolve(args);
}
console.log(e, res);
reject('BLOCK_DELETION_ERROR');
});
});
};
const initUser = () => {
return new Promise((resolve, reject) => {
connectUser(0)
.then(writeBlock)
.then(checkBlock)
.then(removeBlock)
.then(checkRemovedBlock)
.then(() => {
resolve();
}).catch(e => {
console.error(e);
reject(e);
});
});
};
initUser()
.then(() => {
console.log('BLOCK: success');
if (require.main === module) { process.exit(0); }
global?.onTestEnd?.(true);
}).catch(e => {
console.log('BLOCK: failure');
console.log(e);
if (require.main === module) { process.exit(1); }
global?.onTestEnd?.(false);
});