-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrpc.test.js
More file actions
273 lines (254 loc) · 7.71 KB
/
rpc.test.js
File metadata and controls
273 lines (254 loc) · 7.71 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
const Crypto = require('node:crypto');
const Util = require('../common/common-util');
const padId = Crypto.randomBytes(16).toString('hex');
const hk = '0123456789abcdef';
const {
connectUser,
createAnonRpc, createUserRpc,
getRandomKeys, getRandomMsg,
getChannelPath
} = require('./common/utils.js');
console.log('rpc', getChannelPath(padId));
const Env = {};
const isClearedEvt = Util.mkEvent(true);
const sendMsg = wc => {
Env.messages ||= [];
const msg = getRandomMsg();
Env.messages.push(msg);
return wc.bcast(msg);
};
const sendMessages = wc => {
const send = () => { return sendMsg(wc); };
return send().then(send).then(send).then(send).then(send);
};
const initPad = (network) => {
const txid = Crypto.randomBytes(4).toString('hex');
const {edPrivate, edPublic} = getRandomKeys();
Env.keys = { edPublic, edPrivate };
return new Promise((resolve, reject) => {
network.on('message', (msg, sender) => {
if (!Env.wc) { return; }
const parsed = JSON.parse(msg);
if (sender !== hk) { return; }
if (parsed?.error === "EDELETED" &&
parsed?.message === "TEST_RPC" &&
parsed?.channel === padId) {
Env.isDeleted = true;
return;
}
if (parsed?.error === "ECLEARED" &&
parsed?.channel === padId) {
Env.isCleared = true;
isClearedEvt.fire();
return;
}
if (parsed?.state === 1 && parsed?.channel === padId) {
return sendMessages(Env.wc).then(() => {
resolve({network});
Env.wc.leave();
}).catch(reject);
}
});
network.join(padId).then(wc => {
Env.wc = wc;
const msg = ['GET_HISTORY', padId, {
txid, metadata: {
owners: [Env.keys.edPublic],
restricted: true,
allowed: []
}
}];
network.sendto(hk, JSON.stringify(msg));
}).catch(e => {
reject(e);
});
});
};
const checkAnon = (args) => {
const {rpc, network} = args;
Env.anonRpc = rpc;
return new Promise((resolve, reject) => {
rpc.send("GET_FILE_SIZE", padId, (e, data) => {
if (e) { return void reject(e); }
const size = data[0];
if (size !== 1150) { // 5 messages, metadata + data
console.error(size);
reject('INVALID_SIZE');
}
resolve({
network,
keys: Env.keys
});
});
});
};
const checkUser = (args) => {
const {rpc} = args;
Env.ownerRpc = rpc;
return Promise.resolve(args);
};
const checkAccess = (args) => {
return new Promise((resolve, reject) => {
connectUser(1).then(network => {
network.join(padId).then(() => {
reject('ACCESS_NOT_REJECTED');
}).catch(e => {
if (e.type !== "ERESTRICTED") {
console.error("UNEXPECTED ERROR", e);
return reject("INVALID_ERROR");
}
resolve(args);
});
}).catch(reject);
});
};
const checkAllowed = (args) => {
const {network} = args;
return new Promise((resolve, reject) => {
network.join(padId).then((wc) => {
resolve(args);
Env.wc = wc;
}).catch(e => {
console.error("UNEXPECTED ERROR", e);
return reject("INVALID_ERROR");
});
});
};
const checkHistoryAccess = (args) => {
const txid = Crypto.randomBytes(4).toString('hex');
return new Promise((resolve, reject) => {
connectUser(2).then(network => {
const msg = ['GET_HISTORY', padId, {
txid
}];
network.sendto(hk, JSON.stringify(msg)).then(() => {
reject('HISTORY_NOT_REJECTED');
}).catch(e => {
if (e.type !== "ERESTRICTED") {
console.error("UNEXPECTED ERROR", e);
return reject("INVALID_ERROR");
}
resolve(args);
});
}).catch(reject);
});
};
const trimPad = (args) => {
return new Promise((resolve, reject) => {
// Remove the first 2 messages
const hash = Env.messages[2].slice(0,64);
Env.ownerRpc.send('TRIM_HISTORY', {
channel: padId,
hash
}, (e) => {
if (e) { return reject(e); }
resolve(args);
});
});
};
const checkTrim = (args) => {
return new Promise((resolve, reject) => {
Env.anonRpc.send("GET_FILE_SIZE", padId, (e, data) => {
if (e) { return void reject(e); }
const size = data[0];
if (size !== 754) { // 3 messages + metadata
console.error(size);
reject('INVALID_SIZE');
}
resolve(args);
});
});
};
const clearPad = (args) => {
return new Promise((resolve, reject) => {
Env.ownerRpc.send('CLEAR_OWNED_CHANNEL', padId, (e) => {
if (e) { return reject(e); }
const to = setTimeout(() => {
if (!Env.isCleared) {
return reject("MISSING_ECLEARED_MESSAGE");
}
}, 1000);
isClearedEvt.reg(() => {
clearTimeout(to);
resolve(args);
});
});
});
};
const checkClear = (args) => {
return new Promise((resolve, reject) => {
Env.anonRpc.send("GET_FILE_SIZE", padId, (e, data) => {
if (e) { return void reject(e); }
const size = data[0];
if (size !== 160) { // cleared channel + metadata
console.error(size);
reject('INVALID_SIZE');
}
resolve(args);
});
});
};
const removePad = (args) => {
return new Promise((resolve, reject) => {
Env.ownerRpc.send('REMOVE_OWNED_CHANNEL', {
channel: padId,
reason: 'TEST_RPC'
}, (e) => {
if (e) { return reject(e); }
if (!Env.isDeleted) {
return reject("MISSING_EDELETED_MESSAGE");
}
resolve(args);
});
});
};
const checkRemoved = (args) => {
return new Promise((resolve, reject) => {
Env.anonRpc.send("IS_NEW_CHANNEL", padId, (e, data) => {
if (e) { return void reject(e); }
const value = data[0];
if (!value.isNew) { return reject('DELETION_ERROR'); }
if (value.reason !== "TEST_RPC") {
console.error(value);
reject('INVALID_REASON');
}
resolve(args);
});
});
};
const initUser = () => {
return new Promise((resolve, reject) => {
connectUser(0)
.then(initPad)
.then(createAnonRpc)
.then(checkAnon)
.then(createUserRpc)
.then(checkUser)
.then(checkAccess)
.then(checkAllowed)
.then(checkHistoryAccess)
.then(trimPad)
.then(checkTrim)
.then(clearPad)
.then(checkClear)
.then(removePad)
.then(checkRemoved)
.then(() => {
resolve();
}).catch(e => {
console.error(e);
reject(e);
});
});
};
initUser()
.then(() => {
console.log('RPC: success');
if (require.main === module) { process.exit(0); }
global?.onTestEnd?.(true);
}).catch(e => {
console.log('RPC: failure');
console.log(e);
if (require.main === module) { process.exit(1); }
global?.onTestEnd?.(false);
});