-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvirtio_9p.js
executable file
·1841 lines (1667 loc) · 66.6 KB
/
virtio_9p.js
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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env node
"use strict";
process.on("unhandledRejection", exn => { throw exn; });
const TEST_RELEASE_BUILD = +process.env.TEST_RELEASE_BUILD;
var V86 = require(`../../build/${TEST_RELEASE_BUILD ? "libv86" : "libv86-debug"}.js`).V86;
const fs = require("fs");
const testfsjson = require("./testfs.json");
const SHOW_LOGS = false;
const STOP_ON_FIRST_FAILURE = false;
function log_pass(msg, ...args)
{
console.log(`\x1b[92m[+] ${msg}\x1b[0m`, ...args);
}
function log_warn(msg, ...args)
{
console.error(`\x1b[93m[!] ${msg}\x1b[0m`, ...args);
}
function log_fail(msg, ...args)
{
console.error(`\x1b[91m[-] ${msg}\x1b[0m`, ...args);
}
function assert_equal(actual, expected, message)
{
if(actual !== expected)
{
log_warn("Failed assert equal (Test: %s). %s", tests[test_num].name, message || "");
log_warn("Expected:\n" + expected);
log_warn("Actual:\n" + actual);
test_fail();
}
}
function assert_not_equal(actual, expected, message)
{
if(actual === expected)
{
log_warn("Failed assert not equal (Test: %s). %s", tests[test_num].name, message || "");
log_warn("Expected something different than:\n" + expected);
test_fail();
}
}
// Random printable characters.
const test_file = new Uint8Array(512).map(v => 0x20 + Math.random() * 0x5e);
const test_file_string = Buffer.from(test_file).toString();
const test_file_small = new Uint8Array(16).map(v => 0x20 + Math.random() * 0x5e);
const test_file_small_string = Buffer.from(test_file_small).toString();
const tests =
[
{
name: "API SearchPath",
timeout: 60,
mounts:
[
{ path: "/x/fs2" },
],
start: () =>
{
emulator.serial0_send("mkdir -p /mnt/a/b/c\n");
emulator.serial0_send("touch /mnt/a/b/c/file1\n");
emulator.serial0_send("touch /mnt/file2\n");
emulator.serial0_send("mkdir -p /mnt/x/fs2/y/z\n");
emulator.serial0_send("echo done-searchpath\n");
},
end_trigger: "done-searchpath",
end: (capture, done) =>
{
const root1 = emulator.fs9p.SearchPath("");
assert_equal(root1.id, 0, "root1 id");
assert_equal(root1.parentid, -1, "root1 parentid");
const root2 = emulator.fs9p.SearchPath("/");
assert_equal(root2.id, 0, "root2 / id");
assert_equal(root2.parentid, -1, "root2 / parentid");
const notfound1 = emulator.fs9p.SearchPath("c");
assert_equal(notfound1.id, -1, "notfound1 c id");
assert_equal(notfound1.parentid, 0, "notfound1 c parentid");
const notfound2 = emulator.fs9p.SearchPath("c/d");
assert_equal(notfound2.id, -1, "notfound2 c/d id");
assert_equal(notfound2.parentid, -1, "notfound2 c/d parentid");
const notfound3 = emulator.fs9p.SearchPath("a/d");
assert_equal(notfound3.id, -1, "notfound3 a/d id");
assert_not_equal(notfound3.parentid, -1, "notfound3 a/d parent id");
const idx_a = notfound3.parentid;
const notfound4 = emulator.fs9p.SearchPath("a/d/e");
assert_equal(notfound4.id, -1, "notfound4 a/d/e id");
assert_equal(notfound4.parentid, -1, "notfound4 a/d/e parentid");
const dir1 = emulator.fs9p.SearchPath("a");
assert_equal(dir1.id, idx_a, "dir1 a id");
assert_equal(dir1.parentid, 0, "dir1 a parentid");
const dir2 = emulator.fs9p.SearchPath("a/b/c");
assert_not_equal(dir2.id, -1, "dir2 a/b/c id");
assert_not_equal(dir2.parentid, -1, "dir2 a/b/c parentid");
const idx_b = dir2.parentid;
const idx_c = dir2.id;
const file1 = emulator.fs9p.SearchPath("a/b/c/file1");
assert_not_equal(file1.id, -1, "file1 a/b/c/file1 id");
assert_equal(file1.parentid, idx_c, "file1 a/b/c/file1 parentid");
const file2 = emulator.fs9p.SearchPath("file2");
assert_not_equal(file2.id, -1, "file2 id");
assert_equal(file2.parentid, 0, "file2 parentid");
const fwdpath1 = emulator.fs9p.SearchPath("x/fs2");
assert_equal(fwdpath1.forward_path, null, "fwdpath1 x/fs2");
const fwdpath2 = emulator.fs9p.SearchPath("x/fs2/y");
assert_equal(fwdpath2.forward_path, "/y", "fwdpath2 x/fs2/y");
const fwdpath3 = emulator.fs9p.SearchPath("x/fs2/y/z");
assert_equal(fwdpath3.forward_path, "/y/z", "fwdpath3 x/fs2/y/z");
const fwdpath4 = emulator.fs9p.SearchPath("x/fs2/nonexistent");
assert_equal(fwdpath4.forward_path, "/nonexistent", "fwdpath4 x/fs2/nonexistent");
done();
},
},
{
name: "Read Existing",
timeout: 60,
start: () =>
{
emulator.serial0_send("cp /etc/profile /mnt/read-existing\n");
emulator.serial0_send("echo start-capture; cat /etc/profile; echo done-read-existing\n");
},
capture_trigger: "start-capture",
end_trigger: "done-read-existing",
end: async (capture, done) =>
{
const data = await emulator.read_file("read-existing");
assert_equal(capture, Buffer.from(data).toString());
done();
},
},
{
name: "Read New",
timeout: 60,
start: () =>
{
emulator.serial0_send("dd if=/dev/zero of=/mnt/read-new bs=1k count=512\n");
emulator.serial0_send("echo done-read-new\n");
},
end_trigger: "done-read-new",
end: async (capture, done) =>
{
const data = await emulator.read_file("read-new");
assert_equal(data.length, 512 * 1024);
if(data.find(v => v !== 0))
{
log_warn("Fail: Incorrect data. Expected all zeros.");
test_fail();
}
done();
},
},
{
name: "Read Async",
use_fsjson: true,
timeout: 60,
start: () =>
{
emulator.serial0_send("echo start-capture;");
// "foo" is from ./testfs/foo
emulator.serial0_send("cat /mnt/foo;");
emulator.serial0_send("echo done-read-async\n");
},
capture_trigger: "start-capture",
end_trigger: "done-read-async",
end: async (capture, done) =>
{
assert_equal(capture, "bar\n");
const data = await emulator.read_file("foo");
assert_equal(Buffer.from(data).toString(), "bar\n");
done();
},
},
{
name: "Write New",
timeout: 60,
files:
[
{
file: "write-new",
data: test_file,
},
],
start: () =>
{
emulator.serial0_send("echo start-capture; cat /mnt/write-new; echo; echo done-write-new\n");
},
capture_trigger: "start-capture",
end_trigger: "done-write-new",
end: (capture, done) =>
{
// Handle word wrapping.
const lines = capture.split("\n");
let pos = 0;
for(const line of lines)
{
assert_equal(line, test_file_string.slice(pos, line.length));
pos += line.length;
}
done();
},
},
{
name: "New file time",
timeout: 10,
start: () =>
{
emulator.serial0_send("echo start-capture; echo foo > /mnt/bar; ls -l --full-time --color=never /mnt/bar; echo; echo done-write-new\n");
},
capture_trigger: "start-capture",
end_trigger: "done-write-new",
end: (capture, done) =>
{
const outputs = capture.split("\n").map(output => output.split(/\s+/));
// atime: Should be fresh
const [year, month, day] = outputs[0][5].split("-");
assert_not_equal(year, "1970");
done();
},
},
{
name: "Move",
timeout: 60,
files:
[
{
file: "test-file",
data: test_file,
},
],
start: () =>
{
emulator.serial0_send("echo start-capture;");
emulator.serial0_send("cat /mnt/test-file;");
emulator.serial0_send("find /mnt;");
// Rename. Verify updated directory.
emulator.serial0_send("mv /mnt/test-file /mnt/renamed;");
emulator.serial0_send("cat /mnt/renamed;");
emulator.serial0_send("find /mnt;");
// Move between folders. Verify directories.
emulator.serial0_send("mkdir /mnt/somedir;");
emulator.serial0_send("mv /mnt/renamed /mnt/somedir/file;");
emulator.serial0_send("cat /mnt/somedir/file;");
emulator.serial0_send("find /mnt;");
// Rename folder.
emulator.serial0_send("mv /mnt/somedir /mnt/otherdir;");
emulator.serial0_send("cat /mnt/otherdir/file;");
emulator.serial0_send("find /mnt;");
// Move folder.
emulator.serial0_send("mkdir /mnt/thirddir;");
emulator.serial0_send("mv /mnt/otherdir /mnt/thirddir;");
emulator.serial0_send("cat /mnt/thirddir/otherdir/file;");
emulator.serial0_send("find /mnt;");
// Move folder outside /mnt. Should be removed from 9p filesystem.
emulator.serial0_send("mv /mnt/thirddir/otherdir /root/movedoutside;");
emulator.serial0_send("cat /root/movedoutside/file;");
emulator.serial0_send("find /mnt;");
// Cleanup.
emulator.serial0_send("rm -rf /root/movedoutside;");
emulator.serial0_send("echo done-move\n");
},
capture_trigger: "start-capture",
end_trigger: "done-move",
end: (capture, done) =>
{
assert_equal(capture,
test_file_string +
"/mnt\n" +
"/mnt/test-file\n" +
test_file_string +
"/mnt\n" +
"/mnt/renamed\n" +
test_file_string +
"/mnt\n" +
"/mnt/somedir\n" +
"/mnt/somedir/file\n" +
test_file_string +
"/mnt\n" +
"/mnt/otherdir\n" +
"/mnt/otherdir/file\n" +
test_file_string +
"/mnt\n" +
"/mnt/thirddir\n" +
"/mnt/thirddir/otherdir\n" +
"/mnt/thirddir/otherdir/file\n" +
test_file_string +
"/mnt\n" +
"/mnt/thirddir\n");
done();
},
},
{
name: "Unlink",
timeout: 60,
files:
[
{
file: "existing-file",
data: test_file,
},
],
start: () =>
{
emulator.serial0_send("touch /mnt/new-file\n");
emulator.serial0_send("mkdir /mnt/new-dir\n");
emulator.serial0_send("touch /mnt/new-dir/file\n");
emulator.serial0_send("echo start-capture;");
emulator.serial0_send("rm /mnt/new-file;");
emulator.serial0_send("test ! -e /mnt/new-file && echo new-file-unlinked;");
emulator.serial0_send("cat /mnt/new-file 2>/dev/null || echo read-failed;");
emulator.serial0_send("rm /mnt/existing-file;");
emulator.serial0_send("test ! -e /mnt/existing-file && echo existing-file-unlinked;");
emulator.serial0_send("cat /mnt/existing-file 2>/dev/null || echo read-failed;");
emulator.serial0_send("rmdir /mnt/new-dir 2>/dev/null || echo rmdir-failed;");
emulator.serial0_send("test -e /mnt/new-dir && echo new-dir-exist;");
emulator.serial0_send("rm /mnt/new-dir/file;");
emulator.serial0_send("rmdir /mnt/new-dir;");
emulator.serial0_send("test ! -e /mnt/new-dir/file && echo new-dir-file-unlinked;");
emulator.serial0_send("test ! -e /mnt/new-dir && echo new-dir-unlinked;");
emulator.serial0_send("ls /mnt/new-dir 2>/dev/null || echo read-failed;");
emulator.serial0_send("echo done-unlink\n");
},
capture_trigger: "start-capture",
end_trigger: "done-unlink",
end: (capture, done) =>
{
assert_equal(capture,
"new-file-unlinked\n" +
"read-failed\n" +
"existing-file-unlinked\n" +
"read-failed\n" +
"rmdir-failed\n" +
"new-dir-exist\n" +
"new-dir-file-unlinked\n" +
"new-dir-unlinked\n" +
"read-failed\n");
done();
},
},
{
name: "Hard Links",
timeout: 60,
files:
[
{
file: "target",
data: test_file_small,
},
],
start: () =>
{
// Helper that prints filename followed by nlinks.
emulator.serial0_send("nlinks() {\n");
emulator.serial0_send(` ls -dli $@ | awk '{ print "'$@' "$3 }'\n`);
emulator.serial0_send("}\n");
// Check nlinks before mkdir.
emulator.serial0_send("nlinks /mnt | tee -a /mnt/target\n");
emulator.serial0_send("mkdir /mnt/dir\n");
emulator.serial0_send("echo other > /mnt/target2\n");
// Check nlinks after mkdir.
emulator.serial0_send("nlinks /mnt | tee -a /mnt/target\n");
emulator.serial0_send("nlinks /mnt/dir | tee -a /mnt/target\n");
emulator.serial0_send("nlinks /mnt/target | tee -a /mnt/target\n");
// Create hard links.
emulator.serial0_send("ln /mnt/target /mnt/link1\n");
emulator.serial0_send("ln /mnt/link1 /mnt/dir/link2\n");
emulator.serial0_send("ln /mnt/dir/link2 /mnt/dir/link3\n");
emulator.serial0_send("ln /mnt/target2 /mnt/link-other\n");
// Test inode numbers.
emulator.serial0_send("{ test /mnt/target -ef /mnt/link1 && \n");
emulator.serial0_send(" test /mnt/link1 -ef /mnt/dir/link2 && \n");
emulator.serial0_send(" test /mnt/target -ef /mnt/dir/link3 && \n");
emulator.serial0_send(" echo same inode | tee -a /mnt/target; }\n");
emulator.serial0_send("{ test /mnt/link-other -ef /mnt/dir/link3 || \n");
emulator.serial0_send(" echo different inode | tee -a /mnt/link1; }\n");
// Check nlinks after hard links.
emulator.serial0_send("nlinks /mnt | tee -a /mnt/dir/link2\n");
emulator.serial0_send("nlinks /mnt/dir | tee -a /mnt/dir/link2\n");
emulator.serial0_send("nlinks /mnt/target | tee -a /mnt/dir/link2\n");
emulator.serial0_send("nlinks /mnt/dir/link2 | tee -a /mnt/dir/link2\n");
emulator.serial0_send("nlinks /mnt/target2 | tee -a /mnt/dir/link2\n");
emulator.serial0_send("nlinks /mnt/link-other | tee -a /mnt/dir/link2\n");
// Movement and unlink.
emulator.serial0_send("mv /mnt/link1 /mnt/link1-renamed\n");
emulator.serial0_send("echo renamed | tee -a /mnt/link1-renamed\n");
emulator.serial0_send("mv /mnt/dir/link2 /mnt/link2-moved\n");
emulator.serial0_send("echo moved | tee -a /mnt/link2-moved\n");
emulator.serial0_send("rm /mnt/target\n");
emulator.serial0_send("echo unlinked original | tee -a /mnt/dir/link3\n");
// Test inode numbers after movement and unlinking.
emulator.serial0_send("{ test /mnt/link1-renamed -ef /mnt/link2-moved && \n");
emulator.serial0_send(" test /mnt/link2-moved -ef /mnt/dir/link3 && \n");
emulator.serial0_send(" echo same inode after mv | tee -a /mnt/link1-renamed; }\n");
// Check nlinks after movement and unlinking.
emulator.serial0_send("nlinks /mnt | tee -a /mnt/link2-moved\n");
emulator.serial0_send("nlinks /mnt/dir | tee -a /mnt/link2-moved\n");
emulator.serial0_send("nlinks /mnt/link1-renamed | tee -a /mnt/link2-moved\n");
emulator.serial0_send("echo start-capture;\\\n");
// Unlink the rest and output the above messages.
emulator.serial0_send("rm /mnt/link1-renamed;\\\n");
emulator.serial0_send("echo unlinked link1 >> /mnt/link2-moved;\\\n");
emulator.serial0_send("nlinks /mnt/link2-moved >> /mnt/link2-moved;\\\n");
emulator.serial0_send("rm /mnt/link2-moved;\\\n");
emulator.serial0_send("echo unlinked link2 >> /mnt/dir/link3;\\\n");
emulator.serial0_send("nlinks /mnt/dir/link3 >> /mnt/dir/link3;\\\n");
emulator.serial0_send("cat /mnt/dir/link3;\\\n");
emulator.serial0_send("rm /mnt/dir/link3;\\\n");
// Verify nlinks of directories after unlinking hardlinks.
emulator.serial0_send("nlinks /mnt;\\\n");
emulator.serial0_send("nlinks /mnt/dir;\\\n");
// Verify nlinks of root directory after subdirectory is unlinked.
emulator.serial0_send("rmdir /mnt/dir;\\\n");
emulator.serial0_send("nlinks /mnt;\\\n");
emulator.serial0_send("echo done-hard-links\n");
},
capture_trigger: "start-capture",
end_trigger: "done-hard-links",
end: (capture, done) =>
{
assert_equal(capture,
test_file_small_string +
"/mnt 2\n" +
"/mnt 3\n" +
"/mnt/dir 2\n" +
"/mnt/target 1\n" +
"same inode\n" +
"different inode\n" +
"/mnt 3\n" +
"/mnt/dir 2\n" +
"/mnt/target 4\n" +
"/mnt/dir/link2 4\n" +
"/mnt/target2 2\n" +
"/mnt/link-other 2\n" +
"renamed\n" +
"moved\n" +
"unlinked original\n" +
"same inode after mv\n" +
"/mnt 3\n" +
"/mnt/dir 2\n" +
"/mnt/link1-renamed 3\n" +
"unlinked link1\n" +
"/mnt/link2-moved 2\n" +
"unlinked link2\n" +
"/mnt/dir/link3 1\n" +
"/mnt 3\n" +
"/mnt/dir 2\n" +
"/mnt 2\n");
done();
},
},
{
name: "Symlinks",
timeout: 60,
files:
[
{
file: "target",
data: test_file_small,
},
],
start: () =>
{
emulator.serial0_send("echo otherdata > /mnt/target2\n");
emulator.serial0_send("ln -s /mnt/target /mnt/symlink\n");
emulator.serial0_send("echo appended >> /mnt/symlink\n");
emulator.serial0_send("echo start-capture;");
// Should output same file data.
emulator.serial0_send("cat /mnt/target;");
emulator.serial0_send("cat /mnt/symlink;");
// Swap target with the other file.
emulator.serial0_send("rm /mnt/target;");
emulator.serial0_send("mv /mnt/target2 /mnt/target;");
// Symlink should now read from that file.
emulator.serial0_send("cat /mnt/symlink;");
emulator.serial0_send("rm /mnt/target;");
emulator.serial0_send("rm /mnt/symlink;");
emulator.serial0_send("echo done-symlinks\n");
},
capture_trigger: "start-capture",
end_trigger: "done-symlinks",
end: (capture, done) =>
{
assert_equal(capture,
test_file_small_string + "appended\n" +
test_file_small_string + "appended\n" +
"otherdata\n");
done();
},
},
{
name: "Mknod - fifo",
timeout: 60,
start: () =>
{
emulator.serial0_send("mkfifo /mnt/testfifo\n");
emulator.serial0_send('(cat /mnt/testfifo > /mnt/testfifo-output;echo "\ndone-fifo") &\n');
emulator.serial0_send("echo fifomessage > /mnt/testfifo\n");
},
end_trigger: "done-fifo",
end: async (capture, done) =>
{
const data = await emulator.read_file("testfifo-output");
assert_equal(Buffer.from(data).toString(), "fifomessage\n");
done();
},
},
{
name: "Readlink",
timeout: 60,
start: () =>
{
emulator.serial0_send("touch /mnt/target\n");
emulator.serial0_send("ln -s /mnt/target /mnt/link\n");
emulator.serial0_send("echo start-capture;");
emulator.serial0_send("readlink /mnt/link;");
emulator.serial0_send("echo done-readlink\n");
},
capture_trigger: "start-capture",
end_trigger: "done-readlink",
end: (capture, done) =>
{
assert_equal(capture, "/mnt/target\n");
done();
},
},
{
name: "Mkdir",
timeout: 60,
start: () =>
{
emulator.serial0_send("echo notfoobar > /mnt/e-file\n");
emulator.serial0_send("mkdir /mnt/a-dir\n");
emulator.serial0_send("mkdir /mnt/a-dir/b-dir\n");
emulator.serial0_send("mkdir /mnt/a-dir/c-dir\n");
emulator.serial0_send("touch /mnt/a-dir/d-file\n");
emulator.serial0_send("echo mkdirfoobar > /mnt/a-dir/e-file\n");
emulator.serial0_send("echo done-mkdir\n");
},
end_trigger: "done-mkdir",
end: async (capture, done) =>
{
const data = await emulator.read_file("a-dir/e-file");
assert_equal(Buffer.from(data).toString(), "mkdirfoobar\n");
done();
},
},
{
name: "Walk",
timeout: 60,
start: () =>
{
emulator.serial0_send("mkdir -p /mnt/walk/a/aa/aaa/aaaa\n");
emulator.serial0_send("mkdir -p /mnt/walk/a/aa/aaa/aaaa\n");
emulator.serial0_send("mkdir -p /mnt/walk/b/ba\n");
emulator.serial0_send("mkdir -p /mnt/walk/a/aa/aab\n");
emulator.serial0_send("mkdir -p /mnt/walk/a/aa/aac\n");
emulator.serial0_send("touch /mnt/walk/a/aa/aab/aabfile\n");
emulator.serial0_send("touch /mnt/walk/b/bfile\n");
emulator.serial0_send("echo start-capture;");
emulator.serial0_send("find /mnt/walk | sort;"); // order agnostic
emulator.serial0_send("echo done-walk\n");
},
capture_trigger: "start-capture",
end_trigger: "done-walk",
end: (capture, done) =>
{
const actual = capture;
const expected =
"/mnt/walk\n" +
"/mnt/walk/a\n" +
"/mnt/walk/a/aa\n" +
"/mnt/walk/a/aa/aaa\n" +
"/mnt/walk/a/aa/aaa/aaaa\n" +
"/mnt/walk/a/aa/aab\n" +
"/mnt/walk/a/aa/aab/aabfile\n" +
"/mnt/walk/a/aa/aac\n" +
"/mnt/walk/b\n" +
"/mnt/walk/b/ba\n" +
"/mnt/walk/b/bfile\n";
assert_equal(actual, expected);
done();
},
},
{
name: "Statfs",
timeout: 60,
allow_failure: true,
start: () =>
{
emulator.serial0_send("echo start-capture;");
emulator.serial0_send("touch /mnt/file;");
emulator.serial0_send("df -PTk /mnt | tail -n 1;");
// Grow file and verify space usage.
emulator.serial0_send("dd if=/dev/zero of=/mnt/file bs=1k count=4 status=none;");
emulator.serial0_send("df -PTk /mnt | tail -n 1;");
// Shrink file and verify space usage.
emulator.serial0_send("truncate -s 0 /mnt/file;");
emulator.serial0_send("df -PTk /mnt | tail -n 1;");
emulator.serial0_send("echo done-statfs\n");
},
capture_trigger: "start-capture",
end_trigger: "done-statfs",
end: (capture, done) =>
{
const outputs = capture.split("\n").map(output => output.split(/\s+/));
if(outputs.length < 3)
{
log_warn("Wrong format: %s", capture);
test_fail();
done();
return;
}
const before = outputs[0];
const after_add = outputs[1];
const after_rm = outputs[2];
// mount tag
assert_equal(before[0], "host9p");
// fs type
assert_equal(before[1], "9p");
// total size in 1024 blocks
assert_equal(after_add[2], before[2]);
assert_equal(after_rm[2], before[2]);
// used size in 1024 blocks
assert_equal(+after_add[3], (+before[3]) + 4);
assert_equal(after_rm[3], before[3]);
// free size in 1024 blocks
assert_equal(+after_add[4], (+before[4]) - 4);
assert_equal(after_rm[4], before[4]);
// Entry [5] is percentage used.
// mount path
assert_equal(before[6], "/mnt");
done();
},
},
{
name: "File Attributes",
timeout: 60,
start: () =>
{
emulator.serial0_send("echo start-capture;");
emulator.serial0_send("dd if=/dev/zero of=/mnt/file bs=1 count=137 status=none;");
emulator.serial0_send("touch -t 200002022222 /mnt/file;");
emulator.serial0_send("chmod =rw /mnt/file;");
emulator.serial0_send("ls -l --full-time --color=never /mnt/file;");
emulator.serial0_send("chmod +x /mnt/file;");
emulator.serial0_send("chmod -w /mnt/file;");
emulator.serial0_send("ln /mnt/file /mnt/file-link;");
emulator.serial0_send("ls -l --full-time --color=never /mnt/file;");
emulator.serial0_send("chmod -x /mnt/file;");
emulator.serial0_send("truncate -s 100 /mnt/file;");
emulator.serial0_send("touch -t 201011220344 /mnt/file;");
emulator.serial0_send("rm /mnt/file-link;");
emulator.serial0_send("ls -l --full-time --color=never /mnt/file;");
emulator.serial0_send("echo done-file-attr\n");
},
capture_trigger: "start-capture",
end_trigger: "done-file-attr",
end: (capture, done) =>
{
const outputs = capture.split("\n").map(output => output.split(/\s+/));
if(outputs.length < 3)
{
log_warn("Wrong format (expected 3 rows): %s", capture);
test_fail();
done();
return;
}
// mode
assert_equal(outputs[0][0], "-rw-r--r--");
// nlinks
assert_equal(outputs[0][1], "1");
// user
assert_equal(outputs[0][2], "root");
// group
assert_equal(outputs[0][3], "root");
// size
assert_equal(outputs[0][4], "137");
// atime
assert_equal(outputs[0][5], "2000-02-02");
assert_equal(outputs[0][6], "22:22:00");
assert_equal(outputs[0][7], "+0000");
// pathname
assert_equal(outputs[0][8], "/mnt/file");
// mode
assert_equal(outputs[1][0], "-r-xr-xr-x");
// nlinks
assert_equal(outputs[1][1], "2");
// user
assert_equal(outputs[1][2], "root");
// group
assert_equal(outputs[1][3], "root");
// size
assert_equal(outputs[1][4], "137");
// atime
assert_equal(outputs[1][5], "2000-02-02");
assert_equal(outputs[1][6], "22:22:00");
assert_equal(outputs[1][7], "+0000");
// pathname
assert_equal(outputs[1][8], "/mnt/file");
// mode
assert_equal(outputs[2][0], "-r--r--r--");
// nlinks
assert_equal(outputs[2][1], "1");
// user
assert_equal(outputs[2][2], "root");
// group
assert_equal(outputs[2][3], "root");
// size
assert_equal(outputs[2][4], "100");
// atime
assert_equal(outputs[2][5], "2010-11-22");
assert_equal(outputs[2][6], "03:44:00");
assert_equal(outputs[2][7], "+0000");
// pathname
assert_equal(outputs[2][8], "/mnt/file");
done();
},
},
{
name: "Xattrwalk and Listxattr",
timeout: 60,
allow_failure: true,
start: () =>
{
emulator.serial0_send("echo originalvalue > /mnt/file\n");
emulator.serial0_send("echo start-capture;");
emulator.serial0_send('setfattr --name=user.attr1 --value="val1" /mnt/file;');
emulator.serial0_send('setfattr --name=user.attr2 --value="val2" /mnt/file;');
emulator.serial0_send('setfattr --name=user.mime_type --value="text/plain" /mnt/file;');
emulator.serial0_send('setfattr --name=user.nested.attr --value="foobar" /mnt/file;');
// Unrecognized attribute name under other namespaces should be allowed.
emulator.serial0_send('setfattr --name=security.not_an_attr --value="val3" /mnt/file;');
// Remove the caps attribute we've automatically put in. Tested later.
emulator.serial0_send('setfattr --remove=security.capability /mnt/file;');
emulator.serial0_send("getfattr --encoding=text --absolute-names --dump /mnt/file | sort;");
emulator.serial0_send("getfattr --encoding=text --absolute-names --name=user.nested.attr /mnt/file;");
emulator.serial0_send("getfattr --encoding=text --absolute-names --name=security.not_an_attr /mnt/file;");
emulator.serial0_send("getfattr --encoding=text --absolute-names --name=user.attr2 /mnt/file;");
emulator.serial0_send("echo done-listxattr\n");
},
capture_trigger: "start-capture",
end_trigger: "done-listxattr",
end: (capture, done) =>
{
assert_equal(capture,
"# file: /mnt/file\n" +
'security.not_an_attr="val3"\n' +
'user.attr1="val1"\n' +
'user.attr2="val2"\n' +
'user.mime_type="text/plain"\n' +
'user.nested.attr="foobar"\n' +
"\n" +
"# file: /mnt/file\n" +
'user.nested.attr="foobar"\n' +
"\n" +
"# file: /mnt/file\n" +
'security.not_an_attr="val3"\n' +
"\n" +
"# file: /mnt/file\n" +
'user.attr2="val2"\n');
done();
},
},
{
name: "Xattrcreate",
timeout: 60,
allow_failure: true,
start: () =>
{
emulator.serial0_send("echo originalvalue > /mnt/file\n");
// Remove the caps attribute we've automatically put in. Tested later.
emulator.serial0_send('setfattr --remove=security.capability /mnt/file\n');
emulator.serial0_send("echo start-capture;");
// Creation of new xattr using xattrcreate.
emulator.serial0_send("setfattr --name=user.foo --value=bar /mnt/file;");
// File contents should not be overriden.
emulator.serial0_send("cat /mnt/file;");
emulator.serial0_send("getfattr --encoding=hex --absolute-names --name=user.foo /mnt/file;");
// Overwriting of xattr using xattrcreate.
emulator.serial0_send("setfattr --name=user.foo --value=baz /mnt/file;");
// File contents should not be overriden.
emulator.serial0_send("cat /mnt/file;");
emulator.serial0_send("getfattr --encoding=hex --absolute-names --name=user.foo /mnt/file;");
emulator.serial0_send("echo done-xattrcreate\n");
},
capture_trigger: "start-capture",
end_trigger: "done-xattrcreate",
end: (capture, done) =>
{
assert_equal(capture,
"originalvalue\n" +
"# file: /mnt/file\n" +
'user.foo="bar"\n' +
"\n" +
"originalvalue\n" +
"# file: /mnt/file\n" +
'user.foo="baz"\n' +
"\n");
done();
},
},
{
name: "Report All Security Capabilities",
timeout: 60,
allow_failure: true,
start: () =>
{
emulator.serial0_send("touch /mnt/file\n");
emulator.serial0_send("echo start-capture;");
emulator.serial0_send("getfattr --encoding=hex --absolute-names --name=security.capability /mnt/file;");
emulator.serial0_send("echo done-xattr\n");
},
capture_trigger: "start-capture",
end_trigger: "done-xattr",
end: (capture, done) =>
{
assert_equal(capture,
"# file: /mnt/file\n" +
"security.capability=0x" +
// magic and revision number
"00000002" +
// lower permitted
"ffffffff" +
// lower inheritable
"ffffffff" +
// higher permitted
"3f000000" +
// higher inheritable
"3f000000" +
"\n\n");
done();
},
},
{
name: "File Locks",
timeout: 60,
start: () =>
{
emulator.serial0_send("touch /mnt/file\n");
emulator.serial0_send("touch /mnt/logs\n");
emulator.serial0_send("mkfifo /mnt/fifo1\n");
emulator.serial0_send("mkfifo /mnt/fifo2\n");
emulator.serial0_send("flock -s /mnt/file -c 'cat /mnt/fifo1 >> /mnt/file' &\n");
emulator.serial0_send("flock -s /mnt/file -c 'echo lock-shared-2 >> /mnt/file' \n");
emulator.serial0_send("flock -xn /mnt/file -c 'echo lock unblocked! >> /mnt/logs' \n");
emulator.serial0_send("echo lock-shared-1 > /mnt/fifo1\n");
emulator.serial0_send("flock -x /mnt/file -c 'cat /mnt/fifo1 >> /mnt/file' &\n");
emulator.serial0_send("flock -x /mnt/file -c 'echo lock-exclusive-2 >> /mnt/file' &\n");
emulator.serial0_send("flock -sn /mnt/file -c 'echo lock unblocked! >> /mnt/logs' \n");
emulator.serial0_send("echo lock-exclusive-1 > /mnt/fifo1\n");
emulator.serial0_send("flock -sn /mnt/file -c 'cat /mnt/fifo1 >> /mnt/file' &\n");
emulator.serial0_send("flock -s /mnt/file -c 'cat /mnt/fifo2 >> /mnt/file' &\n");
emulator.serial0_send("flock -x /mnt/file -c 'echo lock-exclusive-3 >> /mnt/file' &\n");
emulator.serial0_send("echo lock-shared-4 > /mnt/fifo2\n");
emulator.serial0_send("echo lock-shared-3 > /mnt/fifo1\n");
emulator.serial0_send("echo start-capture;\\\n");
emulator.serial0_send("cat /mnt/file;\\\n");
emulator.serial0_send("cat /mnt/logs;\\\n");
emulator.serial0_send("echo done-locks\n");
},
capture_trigger: "start-capture",
end_trigger: "done-locks",
end: (capture, done) =>
{
assert_equal(capture,
"lock-shared-2\n" +
"lock-shared-1\n" +
"lock-exclusive-1\n" +
"lock-exclusive-2\n" +
"lock-shared-4\n" +
"lock-shared-3\n" +
"lock-exclusive-3\n");
const idx = emulator.fs9p.Search(0, "file");
const P9_LOCK_TYPE_RDLCK = 0;
const P9_LOCK_TYPE_WRLCK = 1;
const P9_LOCK_TYPE_UNLCK = 2;
const P9_LOCK_SUCCESS = 0;
const P9_LOCK_BLOCKED = 1;
const CLIENT_ID = "under test";
function test_getlock(num, type, pos, proc_id, locked)
{
const lock = emulator.fs9p.DescribeLock(type, pos, 1, proc_id, CLIENT_ID);
const ret = emulator.fs9p.GetLock(idx, lock, 0);
assert_equal(ret !== null, locked,
`getlock ${num}: type=${type}, pos=${pos}, proc_id=${proc_id}. Wrong state:`);
}
function test_lock(num, type, start, length, proc_id, status, lock_state)
{
console.log(` Lock ${num}: type=${type}, start=${start}, length=${length} ` +
` proc_id=${proc_id}, expected state=${lock_state}`);
const lock = emulator.fs9p.DescribeLock(type, start, length, proc_id, CLIENT_ID);
assert_equal(emulator.fs9p.Lock(idx, lock, 0), status, "Wrong status:");
for(const [i, state] of [...lock_state].entries())
{
switch(state)
{
case "1":
test_getlock(num, P9_LOCK_TYPE_WRLCK, i, 1, false);
test_getlock(num, P9_LOCK_TYPE_RDLCK, i, 2, false);
test_getlock(num, P9_LOCK_TYPE_WRLCK, i, 2, true);
break;
case "2":
test_getlock(num, P9_LOCK_TYPE_WRLCK, i, 2, false);
test_getlock(num, P9_LOCK_TYPE_RDLCK, i, 1, false);
test_getlock(num, P9_LOCK_TYPE_WRLCK, i, 1, true);
break;