-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathunimatrix.test.ts
393 lines (356 loc) · 12.3 KB
/
unimatrix.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
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
import { JSONStringify, UnimatrixDB } from '../src/common';
import {
genDataKey,
onData,
getData,
setData,
//unimatrix-sync
cardano,
} from '../src/index'
import { UnimatrixData, UnimatrixDecryptFn, UnimatrixEncryptFn, UnimatrixValidatorMap } from '../src/unimatrix';
import Gun ,{IGunInstance} from 'gun';
import sha512 from 'crypto-js/sha512'; // lib is using native crypto API nowdays
import aes from 'crypto-js/aes'; // lib is using native crypto API nowdays
import Utf8 from 'crypto-js/enc-utf8'; // lib is using native crypto API nowdays
//const crypto = require('crypto');
import {describe, expect, test,beforeAll,afterAll, jest} from '@jest/globals'
import { cleanDb, getRandomInt,testDb, timeout } from './common';
const testEncryptionFn:UnimatrixEncryptFn=(args)=>{
const json =JSONStringify(args.store.file);
const idHash =sha512(args.id).toString();
const publicDataHex=Buffer.from(
JSONStringify({updatedAt:args.store.updatedAt})
).toString('hex');
const password =`${idHash}:${publicDataHex}:${args.validator}:${args.path.join('/')}`;
const ciphertext = aes.encrypt(json, password).toString();
//using native - deprecated
// const cipher = crypto.createCipher('aes-256-cbc', password);
// let ciphertext = cipher.update(json, 'utf8', 'hex');
// ciphertext += cipher.final('hex');
const file=`${publicDataHex}:${ciphertext}`;
return file;
}
const testDecryptionFn:UnimatrixDecryptFn=(args)=>{
const idHash =sha512(args.id).toString();
const [publicDataHex,ciphertext]=(args.store||"").split(':');
const password =`${idHash}:${publicDataHex}:${args.validator}:${args.path.join('/')}`;
const bytes = aes.decrypt(ciphertext, password);
const file = JSON.parse(bytes.toString(Utf8));
//using native - deprecated
// const decipher = crypto.createDecipher('aes-256-cbc', password);
// let decrypted = decipher.update(ciphertext, 'hex', 'utf8');
// decrypted += decipher.final('utf8');
// const file=JSON.parse(decrypted);
const {updatedAt}=JSON.parse(Buffer.from(publicDataHex,"hex").toString())||{};
return {file,updatedAt};
}
const testValidatorDefinitions:UnimatrixValidatorMap={
'foo.testValidatorFunc1':(args)=>{
return "not implemented"
},
'foo.bar.baz.testValidatorFunc1':(args)=>{
const [foo,bar,privateData1,privateData2,baz,privateData3] = args.path||[];
if(foo!=='foo')
return "foo error";
if(bar!=='bar')
return "bar error";
if(baz!=='baz')
return "baz error";
if(!privateData1.includes('OnlyWeKnowAbout'))
return "privateData1 error";
if(!privateData2.includes('OnlyWeKnowAbout'))
return "privateData2 error";
if(!privateData3.includes('OnlyWeKnowAbout'))
return "privateData3 error";
return true;
},
'foo.bar.baz.testValidatorFunc2':(args)=>{
return "not implemented"
},
}
const testCase1={
id:'secretIdOnlyWeKnowAbout1234',
validator:'foo.bar.baz.testValidatorFunc1',
path:[
'foo',
'bar',
'dataOnlyWeKnowAbout',
'moreDataOnlyWeKnowAbout',
'baz',
'evenMoreDataOnlyWeKnowAbout'
],
store:{
file:{
data:{
arbitrary:{data:{here:getRandomInt(0,999999)}},
msg25:"🚀 One small step for man, one giant leap for mankind.",
},
error:undefined,
},
updatedAt:Date.now(),
},
dataKey:{
key: '25c1a1fce7c5c83122bdda4c19d84df9d3834f8237033a45c265c7374f1d7d1e6cddb24b01702704aec35ac68677ad0288bc3812e4d793305ee7a8c3c429e583',
path: 'secretIdOnlyWeKnowAbout1234:foo.bar.baz.testValidatorFunc1:foo/bar/dataOnlyWeKnowAbout/moreDataOnlyWeKnowAbout/baz/evenMoreDataOnlyWeKnowAbout'
},
validation:true,
encryptedStore:'U2FsdGVkX1/8PZR8MCX5bGp4PGBotq+ZFXpHpmAeEsW+3EVT+eoArN/HFRszcxIq52EiBw0gh8bPrAI8a0F0tNIr9AKn3BI3AdYFck2mLsElNc2HcXKQ+lGETfrXN8jO7vhXMYN6qVhTLYRi3QaQJZmd0wl0z/5lZ//QXr6xVnE=',
}
const testCase2={
...testCase1,
path:[
'foo',
'bar',
'dataOnlyWeKnowAbout',
'moreDataOnlyWeKnowAbout',
'baz',
'jeezIDontKnowThisData'
],
validation:"privateData3 error",
}
const testCase3={
...testCase1,
id:'iDoNotKnowTheRightSecretId',
}
const testCase4={...testCase1};
testCase4.store.file.data.arbitrary.data.here=getRandomInt(0,999999);
describe('generate hashes', () => {
test('upon data', () => {
const testCase=testCase1;
const _dataKey=genDataKey({
id:testCase.id,
validator:testCase.validator,
path:testCase.path,
});
//console.dir({dataKey});
expect(_dataKey).toEqual(testCase.dataKey);
//expect(_dataKey.path).toBe(testCase.dataKey.path);
});
});
describe('validate', () => {
test('right data parameters', () => {
const testCase=testCase1;
const validatorFn=testValidatorDefinitions[testCase.validator];
const validation=validatorFn({
id:testCase.id,
validator:testCase.validator,
path:testCase.path,
store:testCase.store,
// file:testCase.file,
// updatedAt:testCase.updatedAt,
})
expect(validation).toBe(testCase.validation)
})
test('wrong data parameters', () => {
const testCase=testCase2;
const validatorFn=testValidatorDefinitions[testCase.validator];
const validation=validatorFn({
id:testCase.id,
validator:testCase.validator,
path:testCase.path,
store:testCase.store,
// file:testCase.file,
// updatedAt:testCase.updatedAt,
})
expect(validation).toBe(testCase.validation)
});
});
describe('encrypt and decrypt data ', () => {
test('using crypto-js/aes for testing', () => {
const testCase=testCase1;
const encryptedStore=testEncryptionFn({
id:testCase.id,
validator:testCase.validator,
path:testCase.path,
store:testCase.store,
// file:testCase.file,
// updatedAt:testCase.updatedAt,
});
const decryptedStore=testDecryptionFn({
id:testCase.id,
validator:testCase.validator,
path:testCase.path,
store:encryptedStore,
//file:encryptedStore,
});
expect(decryptedStore).toEqual(testCase.store)
});
});
// async tests ahead...
jest.setTimeout(timeout);
describe('with connection to Unimatrix', () => {
beforeAll(async () => {
await cleanDb();
});
describe('set and get data', () => {
test('using right secrets', async ()=> {
const testCase=testCase1;
//lets patch test case for concurrent processes running these tests across the globe
testCase.id=`${testCase.id}_${String(getRandomInt(0,999999999))}_${String(Date.now())}`;
let db=testDb();
const setResponse=await setData({
db,
checkByFetching:false,
id:testCase.id,
validator:testCase.validator,
validators:testValidatorDefinitions,
path:testCase.path,
store:testCase.store,
//file:testCase.file,
//updatedAt:testCase.updatedAt,
encryptData:testEncryptionFn,
decryptData:testDecryptionFn,
});
// console.dir({setResponse})
const getOkResponse=await getData({
db,
id:testCase.id,
validator:testCase.validator,
validators:testValidatorDefinitions,
path:testCase.path,
timeout:1000*3,
encryptData:testEncryptionFn,
decryptData:testDecryptionFn,
});
// console.dir({getOkResponse})
expect(getOkResponse).toEqual(testCase.store);
//(db as any)=undefined;
})
test('using wrong secrets', async ()=> {
const testCase=testCase1;
//lets patch test case for concurrent processes running these tests across the globe
testCase.id=`${testCase.id}_${String(getRandomInt(0,999999999))}_${String(Date.now())}`;
let db=testDb();
const setResponse=await setData({
db,
checkByFetching:false,
id:testCase.id,
validator:testCase.validator,
validators:testValidatorDefinitions,
path:testCase.path,
store:testCase.store,
//file:testCase.file,
//updatedAt:testCase.updatedAt,
encryptData:testEncryptionFn,
decryptData:testDecryptionFn,
});
// console.dir({setResponse})
const getFailResponse=await getData({
db,
id:testCase3.id,
validator:testCase3.validator,
validators:testValidatorDefinitions,
path:testCase3.path,
timeout:1000*3,
encryptData:testEncryptionFn,
decryptData:testDecryptionFn,
});
// console.dir({getFailResponse})
expect(getFailResponse).toBeUndefined();
//(db as any)=undefined;
});
});
describe('set and listen for data', () => {
test('using right secrets',(done)=> {
const testCase=testCase4;
//lets patch test case for concurrent processes running these tests across the globe
testCase.id=`${testCase.id}_${String(getRandomInt(0,999999999))}_${String(Date.now())}`;
let db=testDb();
const setResponse=setData({
db,
checkByFetching:false,
id:testCase.id,
validator:testCase.validator,
validators:testValidatorDefinitions,
path:testCase.path,
store:testCase.store,
//file:testCase.file,
//updatedAt:testCase.updatedAt,
encryptData:testEncryptionFn,
decryptData:testDecryptionFn,
}).then(()=>{
onData({
db,
id:testCase.id,
validator:testCase.validator,
validators:testValidatorDefinitions,
path:testCase.path,
timeout:1000*3,
encryptData:testEncryptionFn,
decryptData:testDecryptionFn,
on:({store,validationError,timeoutError,node,stop})=>{
//These can get triggered, even several times, by other testing instances, so better simplify test:
// if(validationError){
// stop();
// return done("ValidationError: "+validationError)
// }
// if(store?.file?.error){
// stop();
// return done("UserDefinedError: "+store?.file?.error);
// }
if(timeoutError){
stop();
expect(timeoutError).toBe(true);
return done("TimeoutError: timeout listening for data");
}
if(store){
stop();
expect(store).toEqual(testCase.store);
done();
}
}
});
});
});
test('using wrong secrets',(done)=> {
const testCase=testCase4;
//lets patch test case for concurrent processes running these tests across the globe
testCase.id=`${testCase.id}_${String(getRandomInt(0,999999999))}_${String(Date.now())}`;
let db=testDb();
const setResponse=setData({
db,
checkByFetching:false,
id:testCase.id,
validator:testCase.validator,
validators:testValidatorDefinitions,
path:testCase.path,
store:testCase.store,
// file:testCase.file,
// updatedAt:testCase.updatedAt,
encryptData:testEncryptionFn,
decryptData:testDecryptionFn,
}).then(()=>{
onData({
db,
id:testCase3.id,
validator:testCase.validator,
validators:testValidatorDefinitions,
path:testCase.path,
timeout:1000*3,
encryptData:testEncryptionFn,
decryptData:testDecryptionFn,
on:({store,validationError,timeoutError,node,stop})=>{
//These can get triggered, even several times, by other testing instances, so better simplify test:
// if(validationError){
// stop();
// return done("ValidationError: "+validationError)
// }
// if(store?.file?.error){
// stop();
// return done("UserDefinedError: "+store?.file?.error);
// }
// if(store){
// stop();
// expect(store).toBeUndefined();
// }
if(timeoutError){
stop();
expect(timeoutError).toBe(true);
return done(); // we are expecting this error to get triggered after wrongly-listening for the right data
}
}
});
});
});
});
});