UNPKG

22.2 kBJavaScriptView Raw
1'use strict'
2
3import {
4 createDatastore,
5 deleteDatastore,
6 getDatastore,
7 datastoreMkdir,
8 datastoreRmdir,
9 datastoreListdir,
10 datastoreGetFile,
11 datastorePutFile,
12 datastoreDeleteFile,
13 datastoreStat,
14 getOrCreateDatastore
15} from './testlib';
16
17import {
18 datastoreGetId,
19 decodePrivateKey,
20} from '../../../lib/';
21
22import {
23 makeInodeHeaderBlob
24} from '../../../lib/';
25
26import {
27 getCoreSession,
28 makeAuthRequest
29} from "blockstack";
30
31const assert = require('assert');
32const bitcoinjs = require('bitcoinjs-lib');
33const http = require('http');
34const jsontokens = require('jsontokens');
35const BigInteger = require('bigi');
36const Promise = require('promise');
37
38var args = process.argv.slice(2);
39var command = null;
40if( args.length == 0 ) {
41 command = "unittest";
42}
43else {
44 command = args[0];
45}
46
47var res = null;
48
49function dir_expect(dir, names) {
50 for (var name of names) {
51 if( !Object.keys(dir['children']).includes(name) ) {
52 return false;
53 }
54 }
55 return true;
56}
57
58function dir_absent(ds_str, dir_path) {
59 return datastoreListdir(ds_str, dir_path).then(
60 (idata) => {
61 console.log(`listdir ${dir_path} got result: ${JSON.stringify(idata)}`);
62 return false;
63 },
64 (error) => {
65 console.log(`listdir ${dir_path} failed`);
66 console.log(error);
67 console.log(JSON.stringify(error));
68 return true;
69 });
70}
71
72function stat_dir(ds_str, dir_path, expect_error) {
73 return datastoreStat(ds_str, dir_path).then(
74 (inode) => {
75 console.log(`stat dir ${dir_path} got result: ${JSON.stringify(inode)}`);
76 assert(inode);
77 assert(!inode.error);
78 assert(!inode.errno);
79 if( inode.type != 2 ) {
80 console.log(inode);
81 return false;
82 }
83
84 return true;
85 },
86 (error) => {
87 console.log(`stat ${dir_path} failed`);
88 console.log(error);
89 console.log(JSON.stringify(error));
90 if (expect_error) {
91 return true;
92 }
93 else {
94 return false;
95 }
96 });
97}
98
99function stat_file(ds_str, file_path, expect_error) {
100 return datastoreStat(ds_str, file_path).then(
101 (inode) => {
102 console.log(`stat file ${file_path} got result: ${JSON.stringify(inode)}`);
103 assert(inode);
104 assert(!inode.error);
105 assert(!inode.errno);
106 if( inode.type != 1 ) {
107 console.log(inode);
108 return false;
109 }
110
111 return true;
112 },
113 (error) => {
114 console.log(`stat ${file_path} failed`);
115 console.log(error);
116 console.log(JSON.stringify(error));
117
118 if (expect_error) {
119 return true;
120 }
121 else {
122 return false;
123 }
124 });
125}
126
127
128function file_expect(ds_str, file_path, content) {
129 return datastoreGetFile(ds_str, file_path).then(
130 (idata) => {
131 console.log(`getfile ${file_path} got result: ${JSON.stringify(idata)}`);
132 if( idata.error || !idata ) {
133 if( expect_error ) {
134 return true;
135 }
136 else {
137 console.log(idata.error);
138 return false;
139 }
140 }
141
142 if( idata != content ) {
143 console.log(`expected ${content}; got ${idata}`);
144 }
145
146 return true;
147 },
148 (error) => {
149 console.log(`getfile ${file_path} failed`);
150 console.log(error);
151 console.log(JSON.stringify(error));
152 return false;
153 });
154}
155
156
157function file_absent(ds_str, file_path) {
158 return datastoreGetFile(ds_str, file_path).then(
159 (idata) => {
160 console.log(`getfile ${file_path} got result: ${JSON.stringify(idata)}`);
161 if (idata) {
162 return false;
163 }
164 else {
165 return true;
166 }
167 },
168 (error) => {
169 console.log(`getfile ${file_path} failed`);
170 console.log(error);
171 console.log(JSON.stringify(error));
172 return false;
173 });
174}
175
176
177function http_request(options) {
178
179 var p = new Promise(function(resolve, reject) {
180 http.request(options, function(response) {
181 var strbuf = [];
182 response.on('data', function(chunk) {
183 strbuf.push(chunk);
184 });
185
186 response.on('end', function() {
187 if( response.statusCode != 200 ) {
188 return reject("HTTP Status " + response.statusCode);
189 }
190
191 var str = Buffer.concat(strbuf).toString();
192 var resp = JSON.parse(str);
193 str = null;
194 strbuf = null;
195
196 resolve(resp);
197 });
198
199 response.on('error', function() {
200 reject(resp);
201 });
202 }).end();
203 });
204 return p;
205}
206
207function node_ping(host, port) {
208 var options = {
209 'method': 'GET',
210 'host': host,
211 'port': port,
212 'path': '/v1/node/ping',
213 };
214
215 return http_request(options);
216}
217
218
219if( command == 'createDatastore' ) {
220 assert(args.length >= 5);
221 res = createDatastore(args[1], args[2], args[3], args[4], args[5])
222}
223else if( command == 'deleteDatastore') {
224 assert(args.length >= 2);
225 res = deleteDatastore(args[1]);
226}
227else if( command == 'getDatastore') {
228 assert(args.length >= 5);
229 res = getDatastore(args[1], args[2], args[3], args[4]);
230}
231else if( command == 'mkdir' ) {
232 assert(args.length >= 3);
233 res = datastoreMkdir(args[1], args[2], args[3], args[4]);
234}
235else if( command == 'rmdir' ) {
236 assert(args.length >= 3);
237 res = datastoreRmdir(args[1], args[2], args[3], args[4]);
238}
239else if( command == 'listdir' ) {
240 assert(args.length >= 3 );
241 res = datastoreListdir(args[1], args[2], args[3], args[4]);
242}
243else if( command == 'getfile' ) {
244 assert(args.length >= 3);
245 res = datastoreGetFile(args[1], args[2], args[3], args[4]);
246}
247else if( command == 'putfile' ) {
248 assert(args.length >= 4);
249 res = datastorePutFile(args[1], args[2], args[3], args[4], args[5]);
250}
251else if( command == 'deletefile' ) {
252 assert(args.length >= 3);
253 res = datastoreDeleteFile(args[1], args[2]);
254}
255else if( command == 'stat' ) {
256 assert(args.length >= 3 );
257 res = datastoreStat(args[1], args[2]);
258}
259else if( command == 'unittest' ) {
260 var hdr = makeInodeHeaderBlob("1BjnYXfXbh84Xrc24zM1GFvCrXenp8AqUZ", 2, "1BjnYXfXbh84Xrc24zM1GFvCrXenp8AqUZ", "86ce29a7-0714-4136-bfbc-d48f2e55afd4", "9ceb6a079746a67defdadd7ad19a4c9e070a7e5dd2d41df9fc6e3d289e8e49c4", "c429b777-c7b9-4e07-99ba-7cdf98a283c3", 1);
261
262 var api_password = "blockstack_integration_test_api_password";
263 var device_id = 'c429b777-c7b9-4e07-99ba-7cdf98a283c3';
264 var datastore_privkey = bitcoinjs.ECPair.makeRandom();
265 var datastore_privkey_hex = datastore_privkey.d.toBuffer().toString('hex');
266 var datastore_pubkey_hex = datastore_privkey.getPublicKeyBuffer().toString('hex');
267 var datastore_id = datastoreGetId(datastore_pubkey_hex);
268 var res = null;
269 var datastore = null;
270 var datastore_str = null;
271 var session_token = null;
272
273 console.log(`private key is ${datastore_privkey_hex}`);
274 console.log(`public key is ${datastore_pubkey_hex}`);
275 console.log("begin ping");
276
277 node_ping('localhost', 16268)
278 .then((res) => {
279
280 console.log(`ping result: ${JSON.stringify(res)}`);
281
282 var auth_request = makeAuthRequest(datastore_privkey_hex, "https://www.foo.com", "https://www.foo.com/manifest.json", "https://www.foo.com/login", ['store_read', 'store_write', 'store_admin']);
283 return getCoreSession('localhost', 16268, api_password, datastore_privkey_hex, "judecn.id", auth_request);
284
285 }, (error) => {console.log(JSON.stringify(error)); process.exit(1);})
286 .then((token_res) => {
287
288 console.log(`session result: ${JSON.stringify(token_res)}`);
289 session_token = token_res;
290 if( !session_token ) {
291 console.log("failed to authenticate");
292 process.exit(1);
293 }
294
295 return getOrCreateDatastore({'local': 1}, session_token, datastore_privkey_hex);
296
297 }, (error) => {console.log("get session token failed:"); console.log(error); console.log(JSON.stringify(error)); process.exit(1);})
298 .then((res) => {
299
300 console.log(`getOrCreateDatastore (create) result: ${JSON.stringify(res)}`);
301 if( res.error ) {
302 console.log(res);
303 process.exit(1);
304 }
305
306 // make sure it's idempotent
307 return getOrCreateDatastore({'local': 1}, session_token, datastore_privkey_hex);
308
309 }, (error) => {console.log("getOrCreateDatastore (create) failed:"); console.log(error); console.log(JSON.stringify(error)); process.exit(1);})
310 .then((res) => {
311
312 console.log(`getOrCreateDatastore (get) result: ${JSON.stringify(res)}`);
313 if( res.error ) {
314 console.log(res);
315 console.log("exiting");
316 process.exit(1);
317 }
318
319 datastore = res.datastore;
320 datastore_str = JSON.stringify(res);
321
322 return datastoreMkdir(datastore_str, '/dir1');
323
324 }, (error) => {console.log("getOrCreateDatastore (get) failed:"); console.log(error); console.log(JSON.stringify(error)); process.exit(1);})
325 .then((res) => {
326
327 console.log(`datastoreMkdir result: ${JSON.stringify(res)}`);
328 if( res.error ) {
329 console.log(res);
330 console.log(JSON.stringify(res.error));
331 console.log("exiting");
332 process.exit(1);
333 }
334
335 return datastoreMkdir(datastore_str, '/dir1/dir2');
336
337 }, (error) => {console.log("mkdir /dir1 failed:"); console.log(error); console.log(JSON.stringify(error)); process.exit(1);})
338 .then((res) => {
339
340 console.log(`datastoreMkdir result: ${JSON.stringify(res)}`);
341 if( res.error ) {
342 console.log(res);
343 process.exit(1);
344 }
345
346 return datastorePutFile(datastore_str, '/file1', "hello world");
347
348 }, (error) => {console.log("mkdir /dir1/dir2 failed:"); console.log(error); console.log(JSON.stringify(error)); process.exit(1);})
349 .then((res) => {
350
351 console.log(`datastorePutFile result: ${JSON.stringify(res)}`);
352 if( res.error ) {
353 console.log(res);
354 process.exit(1);
355 }
356
357 return datastorePutFile(datastore_str, '/dir1/file2', "hello world 2");
358
359 }, (error) => {console.log("putfile /file1 failed:"); console.log(error); console.log(JSON.stringify(error)); process.exit(1);})
360 .then((res) => {
361
362 console.log(`datastorePutFile result: ${JSON.stringify(res)}`);
363 if( res.error ) {
364 console.log(res);
365 process.exit(1);
366 }
367
368 return datastorePutFile(datastore_str, '/dir1/dir2/file3', 'hello world 3');
369
370 }, (error) => {console.log("putfile /dir1/file2 failed:"); console.log(error); console.log(JSON.stringify(error)); process.exit(1);})
371 .then((res) => {
372
373 console.log(`datastorePutFile result: ${JSON.stringify(res)}`);
374 if( res.error ) {
375 console.log(res);
376 process.exit(1);
377 }
378
379 return datastoreListdir(datastore_str, '/');
380
381 }, (error) => {console.log("putfile /dir1/dir2/file3 failed:"); console.log(error); console.log(JSON.stringify(error)); process.exit(1);})
382 .then((res) => {
383
384 console.log(`datastoreListdir result: ${JSON.stringify(res)}`);
385 if( !res || res.error) {
386 console.log(res);
387 process.exit(1);
388 }
389
390 if( !dir_expect(res, ['dir1', 'file1']) ) {
391 console.log("Missing dir1 or file1");
392 console.log(res);
393 process.exit(1);
394 }
395
396 return datastoreListdir(datastore_str, '/dir1');
397
398 }, (error) => {console.log("listdir / failed:"); console.log(error); console.log(JSON.stringify(error)); process.exit(1);})
399 .then((res) => {
400
401 console.log(`datastoreListdir result: ${JSON.stringify(res)}`);
402 if( !res || res.error) {
403 console.log(res);
404 process.exit(1);
405 }
406
407 if( !dir_expect(res, ['dir2', 'file2']) ) {
408 console.log("Missing dir2 or file2");
409 console.log(res);
410 process.exit(1);
411 }
412
413 return stat_dir(datastore_str, '/');
414
415 }, (error) => {console.log("listdir /dir1 failed:"); console.log(error); console.log(JSON.stringify(error)); process.exit(1);})
416 .then((res) => {
417
418 console.log(`stat_dir result: ${JSON.stringify(res)}`);
419 if( !res ) {
420 process.exit(1);
421 }
422 return stat_dir(datastore_str, '/dir1');
423
424 }, (error) => {console.log("stat dir / failed:"); console.log(error); console.log(JSON.stringify(error)); process.exit(1);})
425 .then((res) => {
426
427 console.log(`stat_dir result: ${JSON.stringify(res)}`);
428 if( !res ) {
429 process.exit(1);
430 }
431 return stat_dir(datastore_str, '/dir1/dir2');
432
433 }, (error) => {console.log("stat dir /dir1 failed:"); console.log(error); console.log(JSON.stringify(error)); process.exit(1);})
434 .then((res) => {
435
436 console.log(`stat_dir result: ${JSON.stringify(res)}`);
437 if( !res ) {
438 process.exit(1);
439 }
440 return stat_file(datastore_str, '/file1');
441
442 }, (error) => {console.log("stat dir /dir1/dir2 failed:"); console.log(error); console.log(JSON.stringify(error)); process.exit(1);})
443 .then((res) => {
444
445 console.log(`stat_file result: ${JSON.stringify(res)}`);
446 if( !res ) {
447 process.exit(1);
448 }
449 return stat_file(datastore_str, '/dir1/file2');
450
451 }, (error) => {console.log("stat file /file1 failed:"); console.log(error); console.log(JSON.stringify(error)); process.exit(1);})
452 .then((res) => {
453
454 console.log(`stat_file result: ${JSON.stringify(res)}`);
455 if( !res ) {
456 process.exit(1);
457 }
458 return stat_file(datastore_str, '/dir1/dir2/file3');
459
460 }, (error) => {console.log("stat file /dir1/file2 failed:"); console.log(error); console.log(JSON.stringify(error)); process.exit(1);})
461 .then((res) => {
462
463 console.log(`stat_file result: ${JSON.stringify(res)}`);
464 if( !res ) {
465 process.exit(1);
466 }
467 return file_expect(datastore_str, '/file1', 'hello world');
468
469 }, (error) => {console.log("stat file /dir1/dir2/file3 failed:"); console.log(error); console.log(JSON.stringify(error)); process.exit(1);})
470 .then((res) => {
471
472 console.log(`file_expect result: ${JSON.stringify(res)}`);
473 if( !res ) {
474 process.exit(1);
475 }
476 return file_expect(datastore_str, '/dir1/file2', 'hello world 2');
477
478 }, (error) => {console.log("get file /file1 failed:"); console.log(error); console.log(JSON.stringify(error)); process.exit(1);})
479 .then((res) => {
480
481 console.log(`file_expect result: ${JSON.stringify(res)}`);
482 if( !res ) {
483 process.exit(1);
484 }
485 return file_expect(datastore_str, '/dir1/dir2/file3', 'hello world 3');
486
487 }, (error) => {console.log("get file /dir1/file2 failed:"); console.log(error); console.log(JSON.stringify(error)); process.exit(1);})
488 .then((res) => {
489
490 console.log(`file_expect result: ${JSON.stringify(res)}`);
491 if( !res || res.error) {
492 process.exit(1);
493 }
494 return datastoreDeleteFile(datastore_str, '/file1');
495
496 }, (error) => {console.log("get file /dir1/dir2/file3 failed:"); console.log(error); console.log(JSON.stringify(error)); process.exit(1);})
497 .then((res) => {
498
499 console.log(`deletefile result: ${JSON.stringify(res)}`);
500 if( !res || res.error) {
501 process.exit(1);
502 }
503 return datastoreDeleteFile(datastore_str, '/dir1/file2');
504
505 }, (error) => {console.log("delete file /file1 failed:"); console.log(error); console.log(JSON.stringify(error)); process.exit(1);})
506 .then((res) => {
507
508 console.log(`deletefile result: ${JSON.stringify(res)}`);
509 if( !res || res.error ) {
510 process.exit(1);
511 }
512 return datastoreDeleteFile(datastore_str, '/dir1/dir2/file3');
513
514 }, (error) => {console.log("delete file /dir1/file2 failed:"); console.log(error); console.log(JSON.stringify(error)); process.exit(1);})
515 .then((res) => {
516
517 console.log(`deletefile result: ${JSON.stringify(res)}`);
518 if( !res || res.error ) {
519 process.exit(1);
520 }
521 return stat_file(datastore_str, '/file1', true);
522
523 }, (error) => {console.log("delete file /dir1/dir2/file3 failed:"); console.log(error); console.log(JSON.stringify(error)); process.exit(1);})
524 .then((res) => {
525
526 console.log(`stat_file result (expect failure): ${JSON.stringify(res)}`);
527 if( !res || res.error) {
528 process.exit(1);
529 }
530 return stat_file(datastore_str, '/dir1/file2', true);
531
532 }, (error) => {console.log("stat /file1 failed:"); console.log(error); console.log(JSON.stringify(error)); process.exit(1);})
533 .then((res) => {
534
535 console.log(`stat_file result (expect failure): ${JSON.stringify(res)}`);
536 if( !res || res.error ) {
537 process.exit(1);
538 }
539 return stat_file(datastore_str, '/dir1/dir2/file3', true);
540
541 }, (error) => {console.log("stat file /dir1/file2 failed:"); console.log(error); console.log(JSON.stringify(error)); process.exit(1);})
542 .then((res) => {
543
544 console.log(`stat_file result: ${JSON.stringify(res)}`);
545 if( !res || res.error ) {
546 process.exit(1);
547 }
548 return file_absent(datastore_str, '/file1');
549
550 }, (error) => {console.log("stat file /dir1/dir2/file3 failed:"); console.log(error); console.log(JSON.stringify(error)); process.exit(1);})
551 .then((res) => {
552
553 console.log(`file_absent result (expect failure): ${JSON.stringify(res)}`);
554 if( !res || res.error) {
555 process.exit(1);
556 }
557 return file_absent(datastore_str, '/dir1/file2');
558
559 }, (error) => {console.log("getFile /dir1/file2 failed:"); console.log(error); console.log(JSON.stringify(error)); process.exit(1);})
560 .then((res) => {
561
562 console.log(`file_absent result (expect failure): ${JSON.stringify(res)}`);
563 if( !res || res.error ) {
564 process.exit(1);
565 }
566 return file_absent(datastore_str, '/dir1/dir2/file3', true);
567
568 }, (error) => {console.log("getFile /dir1/file2 failed:"); console.log(error); console.log(JSON.stringify(error)); process.exit(1);})
569 .then((res) => {
570
571 console.log(`file_absent result (expect failure): ${JSON.stringify(res)}`);
572 if( !res || res.error ) {
573 process.exit(1);
574 }
575 return datastoreRmdir(datastore_str, '/dir1/dir2');
576
577 }, (error) => {console.log("getFile /dir1/dir2/file3 failed:"); console.log(error); console.log(JSON.stringify(error)); process.exit(1);})
578 .then((res) => {
579
580 console.log(`rmdir result: ${JSON.stringify(res)}`);
581 if( !res || res.error ) {
582 process.exit(1);
583 }
584 return datastoreRmdir(datastore_str, '/dir1');
585
586 }, (error) => {console.log("rmdir /dir1/dir2 failed:"); console.log(error); console.log(JSON.stringify(error)); process.exit(1);})
587 .then((res) => {
588
589 console.log(`rmdir result: ${JSON.stringify(res)}`);
590 if( res.error ) {
591 console.log(res);
592 process.exit(1);
593 }
594
595 return dir_absent(datastore_str, '/dir1/dir2');
596
597 }, (error) => {console.log("rmdir /dir1 failed:"); console.log(error); console.log(JSON.stringify(error)); process.exit(1);})
598 .then((res) => {
599
600 console.log(`dir_absent result: ${JSON.stringify(res)}`);
601 if( !res || res.error) {
602 console.log(res);
603 process.exit(1);
604 }
605
606 return dir_absent(datastore_str, '/dir1');
607
608 }, (error) => {console.log("listdir /dir1/dir2 failed:"); console.log(error); console.log(JSON.stringify(error)); process.exit(1);})
609 .then((res) => {
610
611 console.log(`dir_absent result: ${JSON.stringify(res)}`);
612 if( !res || res.error) {
613 console.log(res);
614 process.exit(1);
615 }
616
617 return stat_dir(datastore_str, '/dir1', true);
618
619 }, (error) => {console.log("listdir /dir1 failed:"); console.log(error); console.log(JSON.stringify(error)); process.exit(1);})
620 .then((res) => {
621
622 console.log(`stat_dir result: ${JSON.stringify(res)}`);
623 if( !res || res.error ) {
624 process.exit(1);
625 }
626 return stat_dir(datastore_str, '/dir1/dir2', true);
627
628 }, (error) => {console.log("stat dir /dir1 failed:"); console.log(error); console.log(JSON.stringify(error)); process.exit(1);})
629 .then((res) => {
630
631 console.log(`stat_dir result: ${JSON.stringify(res)}`);
632 if( !res || res.error ) {
633 process.exit(1);
634 }
635
636 return deleteDatastore(datastore_str);
637 }, (error) => {console.log("stat dir /dir1/dir2 failed:"); console.log(error); console.log(JSON.stringify(error)); process.exit(1);})
638 .then((res) => {
639
640 console.log(`delete datastore result: ${JSON.stringify(res)}`);
641 if( !res ) {
642 process.exit(1);
643 }
644 process.exit(0);
645 }, (error) => {console.log("delete datastore failed:"); console.log(error); console.log(JSON.stringify(error)); process.exit(1);});
646}
647else {
648 console.log("No command given");
649 console.log(`args = ${args}`);
650 console.log(`command = ${command}`);
651 assert(0);
652}