123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758 |
- #!/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 assert_equal(actual, expected, message)
- {
- if(actual !== expected)
- {
- console.warn("Failed assert equal (Test: %s). %s", tests[test_num].name, message || "");
- console.warn("Expected:\n" + expected);
- console.warn("Actual:\n" + actual);
- test_fail();
- }
- }
- function assert_not_equal(actual, expected, message)
- {
- if(actual === expected)
- {
- console.warn("Failed assert not equal (Test: %s). %s", tests[test_num].name, message || "");
- console.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))
- {
- console.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)
- {
- console.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)
- {
- console.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;
- case "3":
- test_getlock(num, P9_LOCK_TYPE_RDLCK, i, 1, false);
- test_getlock(num, P9_LOCK_TYPE_WRLCK, i, 1, true);
- test_getlock(num, P9_LOCK_TYPE_RDLCK, i, 2, false);
- test_getlock(num, P9_LOCK_TYPE_WRLCK, i, 2, true);
- break;
- case "e":
- test_getlock(num, P9_LOCK_TYPE_RDLCK, i, 1, false);
- test_getlock(num, P9_LOCK_TYPE_RDLCK, i, 2, true);
- break;
- case "E":
- test_getlock(num, P9_LOCK_TYPE_RDLCK, i, 1, true);
- test_getlock(num, P9_LOCK_TYPE_RDLCK, i, 2, false);
- break;
- case "-":
- test_getlock(num, P9_LOCK_TYPE_WRLCK, i, 1, false);
- test_getlock(num, P9_LOCK_TYPE_WRLCK, i, 2, false);
- break;
- }
- }
- }
- // Key:
- // 1/2/3 = shared lock by process 1/2/both
- // e/E = exclusive lock by process 1/2
- // - = no locks
- const I = Infinity;
- test_lock(0, P9_LOCK_TYPE_RDLCK, 0, 1, 1, P9_LOCK_SUCCESS, "1-------"); // First lock.
- test_lock(1, P9_LOCK_TYPE_RDLCK, 0, 2, 1, P9_LOCK_SUCCESS, "11------"); // Replace.
- test_lock(2, P9_LOCK_TYPE_RDLCK, 1, 1, 2, P9_LOCK_SUCCESS, "13------");
- test_lock(3, P9_LOCK_TYPE_RDLCK, 2, 2, 1, P9_LOCK_SUCCESS, "1311----"); // Skip. Merge before.
- test_lock(4, P9_LOCK_TYPE_WRLCK, 0, 1, 1, P9_LOCK_SUCCESS, "e311----"); // Shrink left.
- test_lock(5, P9_LOCK_TYPE_WRLCK, 1, 1, 1, P9_LOCK_BLOCKED, "e311----");
- test_lock(6, P9_LOCK_TYPE_UNLCK, 0, 4, 1, P9_LOCK_SUCCESS, "-2------"); // Delete.
- test_lock(7, P9_LOCK_TYPE_WRLCK, 1, 2, 1, P9_LOCK_BLOCKED, "-2------");
- test_lock(8, P9_LOCK_TYPE_UNLCK, 1, 3, 2, P9_LOCK_SUCCESS, "--------"); // Delete.
- test_lock(9, P9_LOCK_TYPE_WRLCK, 1, 1, 1, P9_LOCK_SUCCESS, "-e------");
- test_lock(10, P9_LOCK_TYPE_RDLCK, 3, 3, 1, P9_LOCK_SUCCESS, "-e-111--"); // Skip.
- test_lock(11, P9_LOCK_TYPE_RDLCK, 2, 1, 2, P9_LOCK_SUCCESS, "-e2111--"); // Skip past.
- test_lock(12, P9_LOCK_TYPE_UNLCK, 2, 1, 2, P9_LOCK_SUCCESS, "-e-111--"); // Delete.
- test_lock(13, P9_LOCK_TYPE_WRLCK, 0, 1, 1, P9_LOCK_SUCCESS, "ee-111--");
- test_lock(14, P9_LOCK_TYPE_WRLCK, 1, 4, 1, P9_LOCK_SUCCESS, "eeeee1--"); // Merge before. Shrink both ways.
- test_lock(15, P9_LOCK_TYPE_WRLCK, 1, 2, 2, P9_LOCK_BLOCKED, "eeeee1--");
- test_lock(16, P9_LOCK_TYPE_RDLCK, 4, 5, 2, P9_LOCK_BLOCKED, "eeeee1--");
- test_lock(17, P9_LOCK_TYPE_RDLCK, 5, I, 2, P9_LOCK_SUCCESS, "eeeee322");
- test_lock(18, P9_LOCK_TYPE_UNLCK, 0, I, 1, P9_LOCK_SUCCESS, "-----222"); // Replace.
- test_lock(19, P9_LOCK_TYPE_RDLCK, 4, I, 2, P9_LOCK_SUCCESS, "----2222"); // Replace.
- test_lock(20, P9_LOCK_TYPE_WRLCK, 2, I, 2, P9_LOCK_SUCCESS, "--EEEEEE"); // Replace.
- test_lock(21, P9_LOCK_TYPE_WRLCK, 0, 1, 2, P9_LOCK_SUCCESS, "E-EEEEEE");
- test_lock(22, P9_LOCK_TYPE_WRLCK, 1, 3, 2, P9_LOCK_SUCCESS, "EEEEEEEE"); // Merge both. Shrink left.
- test_lock(23, P9_LOCK_TYPE_RDLCK, 3, 4, 2, P9_LOCK_SUCCESS, "EEE2222E"); // Split.
- test_lock(24, P9_LOCK_TYPE_RDLCK, 1, 2, 2, P9_LOCK_SUCCESS, "E222222E"); // Merge after. Shrink right.
- test_lock(25, P9_LOCK_TYPE_RDLCK, 2, 3, 2, P9_LOCK_SUCCESS, "E222222E"); // No-op.
- done();
- },
- },
- {
- name: "Stress Files",
- timeout: 360,
- start: () =>
- {
- emulator.serial0_send("mkdir /mnt/stress-files\n");
- emulator.serial0_send('cat << "EOF" | sh\n');
- // Create files.
- // Ensure directory inode data exceeds maximum message size for 9p.
- emulator.serial0_send("for f in $(seq -w 0 999)\n");
- emulator.serial0_send("do\n");
- emulator.serial0_send(' echo "$f" > "/mnt/stress-files/file-$f"\n');
- emulator.serial0_send("done\n");
- emulator.serial0_send("echo start-capture\n");
- // Read some of them.
- emulator.serial0_send("for f in $(seq -w 0 31 999)\n");
- emulator.serial0_send("do\n");
- emulator.serial0_send(' cat "/mnt/stress-files/file-$f"\n');
- emulator.serial0_send("done\n");
- // Walk.
- emulator.serial0_send("find /mnt/stress-files | sort\n");
- // Delete and verify.
- // Using glob checks readdir.
- emulator.serial0_send("rm /mnt/stress-files/file-*\n");
- emulator.serial0_send('test -z "$(ls /mnt/stress-files)" && echo delete-success\n');
- emulator.serial0_send("echo done-stress-files\n");
- emulator.serial0_send("EOF\n");
- },
- capture_trigger: "start-capture",
- end_trigger: "done-stress-files",
- end: (capture, done) =>
- {
- let expected = "";
- for(let i = 0; i < 1000; i += 31)
- {
- expected += i.toString().padStart(3, "0") + "\n";
- }
- expected += "/mnt/stress-files\n";
- for(let i = 0; i < 1000; i ++)
- {
- expected += "/mnt/stress-files/file-" + i.toString().padStart(3, "0") + "\n";
- }
- expected += "delete-success\n";
- assert_equal(capture, expected);
- done();
- },
- },
- {
- name: "Stress Directories",
- timeout: 360,
- start: () =>
- {
- emulator.serial0_send('cat << "EOF" | sh\n');
- emulator.serial0_send("p=/mnt/stress-dirs\n");
- emulator.serial0_send('mkdir "$p"\n');
- // Create deep folder structure
- emulator.serial0_send("for i in $(seq 0 99)\n");
- emulator.serial0_send("do\n");
- emulator.serial0_send(' p="$p/$i"\n');
- emulator.serial0_send(' mkdir "$p"\n');
- emulator.serial0_send(' echo "$i" > "$p/file"\n');
- emulator.serial0_send("done\n");
- // Try accessing deep files
- emulator.serial0_send("p=/mnt/stress-dirs\n");
- emulator.serial0_send("echo start-capture\n");
- // Skip first 80 - otherwise too slow
- emulator.serial0_send("for i in $(seq 0 79)\n");
- emulator.serial0_send("do\n");
- emulator.serial0_send(' p="$p/$i"\n');
- emulator.serial0_send("done\n");
- emulator.serial0_send("for i in $(seq 80 99)\n");
- emulator.serial0_send("do\n");
- emulator.serial0_send(' p="$p/$i"\n');
- emulator.serial0_send(' cat "$p/file"\n');
- emulator.serial0_send("done\n");
- // Delete and verify
- emulator.serial0_send("rm -rf /mnt/stress-dirs/0\n");
- emulator.serial0_send('test -z "$(ls /mnt/stress-dirs)" && echo delete-success\n');
- emulator.serial0_send("echo done-stress-dirs\n");
- emulator.serial0_send("EOF\n");
- },
- capture_trigger: "start-capture",
- end_trigger: "done-stress-dirs",
- end: (capture, done) =>
- {
- const outputs = capture.split("\n");
- for(let i = 0; i < 20; i++)
- {
- assert_equal(outputs[i], `${i + 80}`);
- }
- assert_equal(outputs[20], "delete-success");
- done();
- },
- },
- {
- name: "Read Past Available",
- timeout: 60,
- start: () =>
- {
- emulator.serial0_send("echo a > /mnt/small-file\n");
- emulator.serial0_send("echo start-capture;");
- // Reading from offsets > size of file should not read anything.
- emulator.serial0_send("dd if=/mnt/small-file bs=1 count=1 skip=10;");
- emulator.serial0_send("dd if=/mnt/small-file bs=1 count=1 skip=100;");
- emulator.serial0_send("dd if=/mnt/small-file bs=1 count=1 skip=1000;");
- emulator.serial0_send("echo done-read-exceed\n");
- },
- capture_trigger: "start-capture",
- end_trigger: "done-read-exceed",
- end: (capture, done) =>
- {
- const outputs = capture.split("\n");
- assert_equal(outputs[0], "0+0 records in");
- assert_equal(outputs[1], "0+0 records out");
- assert_equal(outputs[2], "0+0 records in");
- assert_equal(outputs[3], "0+0 records out");
- assert_equal(outputs[4], "0+0 records in");
- assert_equal(outputs[5], "0+0 records out");
- done();
- },
- },
- {
- name: "Read Mounted",
- timeout: 60,
- mounts:
- [
- { path: "/a/b/fs2", baseurl: __dirname + "/testfs/", basefs: testfsjson },
- ],
- start: () =>
- {
- emulator.serial0_send("echo start-capture;");
- emulator.serial0_send("cat /mnt/a/b/fs2/foo;");
- emulator.serial0_send("cat /mnt/a/b/fs2/dir/bar;");
- emulator.serial0_send("echo done-read-mounted\n");
- },
- capture_trigger: "start-capture",
- end_trigger: "done-read-mounted",
- end: async (capture, done) =>
- {
- assert_equal(capture, "bar\nfoobaz\n");
- const data = await emulator.read_file("/a/b/fs2/dir/bar");
- assert_equal(Buffer.from(data).toString(), "foobaz\n");
- done();
- },
- },
- {
- name: "Write Mounted",
- timeout: 60,
- mounts:
- [
- { path: "/a/b/fs2" },
- ],
- files:
- [
- {
- file: "/a/b/fs2/write-new-host",
- data: test_file,
- },
- ],
- start: () =>
- {
- emulator.serial0_send("mkdir /mnt/a/b/fs2/c\n");
- emulator.serial0_send("echo foobar > /mnt/a/b/fs2/c/write-new-guest\n");
- emulator.serial0_send("echo start-capture;");
- emulator.serial0_send("cat /mnt/a/b/fs2/c/write-new-guest;");
- emulator.serial0_send("cat /mnt/a/b/fs2/write-new-host; echo;");
- emulator.serial0_send("echo done-write-mounted\n");
- },
- capture_trigger: "start-capture",
- end_trigger: "done-write-mounted",
- end: async (capture, done) =>
- {
- const lines = capture.split("\n");
- assert_equal(lines.shift(), "foobar");
- let pos = 0;
- for(const line of lines)
- {
- assert_equal(line, test_file_string.slice(pos, line.length));
- pos += line.length;
- }
- const data = await emulator.read_file("a/b/fs2/c/write-new-guest");
- assert_equal(Buffer.from(data).toString(), "foobar\n");
- done();
- },
- },
- {
- name: "Walk Mounted",
- timeout: 180,
- mounts:
- [
- { path: "/a/fs2" },
- { path: "/fs3" },
- { path: "/fs3/fs4" },
- ],
- start: () =>
- {
- emulator.serial0_send("echo start-capture;");
- emulator.serial0_send("mkdir -p /mnt/a/fs2/aa/aaa/aaaa;");
- emulator.serial0_send("mkdir -p /mnt/a/fs2/aa/aab;");
- emulator.serial0_send("mkdir -p /mnt/a/fs2/ab/aba;");
- emulator.serial0_send("touch /mnt/a/fs2/ab/aba/abafile;");
- emulator.serial0_send("mkdir -p /mnt/a/fs2/ab/abb;");
- emulator.serial0_send("mkdir -p /mnt/fs3/a/aa/aaa;");
- emulator.serial0_send("mkdir -p /mnt/fs3/a/ab/aba;");
- emulator.serial0_send("touch /mnt/fs3/a/afile;");
- emulator.serial0_send("mkdir -p /mnt/fs3/b;");
- emulator.serial0_send("mkdir -p /mnt/fs3/fs4/a/aa/aaa;");
- emulator.serial0_send("mkdir -p /mnt/fs3/fs4/a/ab/;");
- emulator.serial0_send("mkdir -p /mnt/fs3/fs4/a/ac/aca;");
- emulator.serial0_send("touch /mnt/fs3/fs4/a/ac/aca/acafile;");
- emulator.serial0_send("find /mnt | sort;"); // order agnostic
- emulator.serial0_send("echo done-walk-mounted\n");
- },
- capture_trigger: "start-capture",
- end_trigger: "done-walk-mounted",
- end: (capture, done) =>
- {
- const lines = capture.split("\n");
- const expected_lines =
- [
- "/mnt",
- "/mnt/a",
- "/mnt/a/fs2",
- "/mnt/a/fs2/aa",
- "/mnt/a/fs2/aa/aaa",
- "/mnt/a/fs2/aa/aaa/aaaa",
- "/mnt/a/fs2/aa/aab",
- "/mnt/a/fs2/ab",
- "/mnt/a/fs2/ab/aba",
- "/mnt/a/fs2/ab/aba/abafile",
- "/mnt/a/fs2/ab/abb",
- "/mnt/fs3",
- "/mnt/fs3/a",
- "/mnt/fs3/a/aa",
- "/mnt/fs3/a/aa/aaa",
- "/mnt/fs3/a/ab",
- "/mnt/fs3/a/ab/aba",
- "/mnt/fs3/a/afile",
- "/mnt/fs3/b",
- "/mnt/fs3/fs4",
- "/mnt/fs3/fs4/a",
- "/mnt/fs3/fs4/a/aa",
- "/mnt/fs3/fs4/a/aa/aaa",
- "/mnt/fs3/fs4/a/ab",
- "/mnt/fs3/fs4/a/ac",
- "/mnt/fs3/fs4/a/ac/aca",
- "/mnt/fs3/fs4/a/ac/aca/acafile",
- ];
- for(const expected of expected_lines)
- {
- assert_equal(lines.shift(), expected);
- }
- done();
- },
- },
- {
- name: "Move Mounted",
- timeout: 60,
- mounts:
- [
- { path: "/a/b/fs2" },
- { path: "/fs3" },
- { path: "/fs3/fs4" },
- { path: "/fs3/fs4/fs5" },
- ],
- start: () =>
- {
- emulator.serial0_send("echo foobar > /mnt/fs3/file\n");
- emulator.serial0_send("mkdir /mnt/a/b/fs2/dir\n");
- emulator.serial0_send("mkdir /mnt/fs3/fs4/fs5/otherdir\n");
- emulator.serial0_send("echo contents > /mnt/a/b/fs2/dir/child\n");
- // Using tail -f to keep 'file' open for modification in bg while it is being moved.
- // Using fifo to send data from fg job to bg job to write to file.
- emulator.serial0_send("mkfifo /mnt/fs3/fifo\n");
- emulator.serial0_send("mkfifo /mnt/fs3/fifo_intermediate\n");
- emulator.serial0_send("tail -f /mnt/fs3/fifo > /mnt/fs3/fifo_intermediate &\n");
- emulator.serial0_send('echo "$!" > /mnt/tailpid\n');
- emulator.serial0_send('{ sed "/EOF/q" < /mnt/fs3/fifo_intermediate && kill "$(cat /mnt/tailpid)"; } >> /mnt/fs3/file &\n');
- emulator.serial0_send("echo start-capture; \\\n");
- emulator.serial0_send("echo untouched > /mnt/fs3/fifo; \\\n");
- // File from forwarder to non-forwarder. Divert forwarder file.
- emulator.serial0_send("{ mv /mnt/fs3/file /mnt/file1 &&");
- emulator.serial0_send(" echo file jump to root > /mnt/fs3/fifo; }; \\\n");
- // File from non-forwarder to forwarder. Divert non-forwarder file.
- emulator.serial0_send("{ mv /mnt/file1 /mnt/fs3/file2 &&");
- emulator.serial0_send(" echo file jump filesystems > /mnt/fs3/fifo; }; \\\n");
- // File rename within the same foreign filesystem. Divert non-forwarder file.
- emulator.serial0_send("{ mv /mnt/fs3/file2 /mnt/fs3/file3 && ");
- emulator.serial0_send(" echo file renamed > /mnt/fs3/fifo; }; \\\n");
- // File from forwarder to forwarder under directory. Divert forwarder file.
- emulator.serial0_send("{ mv /mnt/fs3/file3 /mnt/a/b/fs2/dir/file4 && ");
- emulator.serial0_send(" echo file move to dir > /mnt/fs3/fifo; }; \\\n");
- // Directory from forwarder to forwarder.
- emulator.serial0_send("{ mv /mnt/a/b/fs2/dir /mnt/fs3/fs4/fs5/dir1 && ");
- emulator.serial0_send(" echo dir jump filesystems > /mnt/fs3/fifo; }; \\\n");
- // Moving mountpoint across filesystems.
- emulator.serial0_send("{ mv /mnt/fs3/fs4 /mnt/a/b/fs2/fs4 2>/dev/null || ");
- emulator.serial0_send(" echo move mount point across - fails > /mnt/fs3/fifo; }; \\\n");
- emulator.serial0_send("{ mv /mnt/fs3/fs4/fs5 /mnt/fs5 2>/dev/null || ");
- emulator.serial0_send(" echo move mount point upwards - fails > /mnt/fs3/fifo; }; \\\n");
- // Directory move within the same foreign filesystem.
- emulator.serial0_send("{ mv /mnt/fs3/fs4/fs5/dir1 /mnt/fs3/fs4/fs5/otherdir/dir2 && ");
- emulator.serial0_send(" echo dir move > /mnt/fs3/fifo; }; \\\n");
- // Directory from forwarder to non-forwarder. Divert forwarder directory.
- emulator.serial0_send("{ mv /mnt/fs3/fs4/fs5/otherdir/dir2 /mnt/dir3 && ");
- emulator.serial0_send(" echo dir jump to root > /mnt/fs3/fifo; }; \\\n");
- // Directory from non-forwarder to forwarder. Divert non-forwarder directory.
- emulator.serial0_send("{ mv /mnt/dir3 /mnt/fs3/fs4/dir4 && ");
- emulator.serial0_send(" echo dir jump back > /mnt/fs3/fifo; }; \\\n");
- // Moving empty file (treated differently when rewriting data.
- emulator.serial0_send("touch /mnt/a/b/fs2/emptyfile; \\\n");
- emulator.serial0_send("{ mv /mnt/a/b/fs2/emptyfile /mnt/fs3/fs4/dir4/emptyfile && ");
- emulator.serial0_send(" echo move empty file > /mnt/fs3/fifo; }; \\\n");
- emulator.serial0_send("cat /mnt/fs3/fs4/dir4/emptyfile; \\\n");
- emulator.serial0_send('printf "EOF\\n\\n" > /mnt/fs3/fifo & wait "$(cat /mnt/tailpid)" 2>/dev/null; \\\n');
- emulator.serial0_send("cat /mnt/fs3/fs4/dir4/file4; \\\n");
- emulator.serial0_send("cat /mnt/fs3/fs4/dir4/child; \\\n");
- emulator.serial0_send("find /mnt | sort; \\\n");
- emulator.serial0_send("echo done-move-mounted\n");
- },
- capture_trigger: "start-capture",
- end_trigger: "done-move-mounted",
- end: (capture, done) =>
- {
- assert_equal(capture,
- "foobar\n" +
- "untouched\n" +
- "file jump to root\n" +
- "file jump filesystems\n" +
- "file renamed\n" +
- "file move to dir\n" +
- "dir jump filesystems\n" +
- "move mount point across - fails\n" +
- "move mount point upwards - fails\n" +
- "dir move\n" +
- "dir jump to root\n" +
- "dir jump back\n" +
- "move empty file\n" +
- "EOF\n" +
- "contents\n" +
- "/mnt\n" +
- "/mnt/a\n" +
- "/mnt/a/b\n" +
- "/mnt/a/b/fs2\n" +
- "/mnt/fs3\n" +
- "/mnt/fs3/fifo\n" +
- "/mnt/fs3/fifo_intermediate\n" +
- "/mnt/fs3/fs4\n" +
- "/mnt/fs3/fs4/dir4\n" +
- "/mnt/fs3/fs4/dir4/child\n" +
- "/mnt/fs3/fs4/dir4/emptyfile\n" +
- "/mnt/fs3/fs4/dir4/file4\n" +
- "/mnt/fs3/fs4/fs5\n" +
- "/mnt/fs3/fs4/fs5/otherdir\n" +
- "/mnt/tailpid\n");
- done();
- },
- },
- {
- name: "Hard Links Mounted",
- timeout: 60,
- mounts:
- [
- { path: "/fs1a" },
- { path: "/fs1a/fs2" },
- { path: "/fs1b" },
- ],
- start: () =>
- {
- emulator.serial0_send("echo foobar > /mnt/fs1a/file\n");
- emulator.serial0_send("echo start-capture;\\\n");
- emulator.serial0_send("{ ln /mnt/fs1a/file /mnt/fs1a/fs2/link-child 2>/dev/null || \n");
- emulator.serial0_send(" echo link at child fs - fails >> /mnt/fs1a/file; };\\\n");
- emulator.serial0_send("{ ln /mnt/fs1a/file /mnt/link-parent 2>/dev/null || \n");
- emulator.serial0_send(" echo link at parent fs - fails >> /mnt/fs1a/file; };\\\n");
- emulator.serial0_send("ln /mnt/fs1a/file /mnt/fs1a/link;\\\n");
- emulator.serial0_send("echo link at common fs >> /mnt/fs1a/link;\\\n");
- emulator.serial0_send("mv /mnt/fs1a/link /mnt/fs1a/link2;\\\n");
- emulator.serial0_send("echo rename >> /mnt/fs1a/link2;\\\n");
- emulator.serial0_send("{ mv /mnt/fs1a/link2 /mnt/link3 2>/dev/null || \n");
- emulator.serial0_send(" echo jump to parent - fails >> /mnt/fs1a/link2; };\\\n");
- emulator.serial0_send("{ mv /mnt/fs1a/link2 /mnt/fs1b/link3 2>/dev/null || \n");
- emulator.serial0_send(" echo jump outside - fails >> /mnt/fs1a/link2; };\\\n");
- emulator.serial0_send("cat /mnt/fs1a/file;\\\n");
- emulator.serial0_send("echo done-hard-links-mounted\n");
- },
- capture_trigger: "start-capture",
- end_trigger: "done-hard-links-mounted",
- end: (capture, done) =>
- {
- assert_equal(capture,
- "foobar\n" +
- "link at child fs - fails\n" +
- "link at parent fs - fails\n" +
- "link at common fs\n" +
- "rename\n" +
- "jump to parent - fails\n" +
- "jump outside - fails\n");
- done();
- },
- },
- {
- name: "Using '..' across filesystems",
- timeout: 60,
- mounts:
- [
- { path: "/a/fs2" },
- ],
- start: () =>
- {
- emulator.serial0_send("mkdir /mnt/a/fs2/c\n");
- emulator.serial0_send("echo foobar > /mnt/a/fs2/../file\n");
- emulator.serial0_send("cd /mnt/a/fs2/c\n");
- emulator.serial0_send("echo baz >> ../../file\n");
- emulator.serial0_send("mv /mnt/a/file ../../renamed\n");
- emulator.serial0_send("cp /mnt/a/renamed ../../file\n");
- emulator.serial0_send("echo start-capture;\\\n");
- emulator.serial0_send("cat /mnt/a/file;\\\n");
- emulator.serial0_send("cat /mnt/a/renamed;\\\n");
- emulator.serial0_send("rm ../../renamed;\\\n");
- emulator.serial0_send("test ! -e /mnt/a/renamed && echo removed;\\\n");
- emulator.serial0_send("cd /;\\\n");
- emulator.serial0_send("echo done-readdir-parent-mount\n");
- },
- capture_trigger: "start-capture",
- end_trigger:"done-readdir-parent-mount",
- end: (capture, done) =>
- {
- assert_equal(capture,
- "foobar\n" +
- "baz\n" +
- "foobar\n" +
- "baz\n" +
- "removed\n");
- done();
- },
- },
- ];
- let test_num = 0;
- let test_timeout = 0;
- let test_has_failed = false;
- const failed_tests = [];
- function test_fail()
- {
- if(!test_has_failed)
- {
- test_has_failed = true;
- failed_tests.push(test_num);
- }
- }
- const emulator = new V86({
- bios: { url: __dirname + "/../../bios/seabios.bin" },
- vga_bios: { url: __dirname + "/../../bios/vgabios.bin" },
- cdrom: { url: __dirname + "/../../images/linux4.iso" },
- autostart: true,
- memory_size: 64 * 1024 * 1024,
- filesystem: {
- baseurl: __dirname + "/testfs/",
- },
- disable_jit: +process.env.DISABLE_JIT,
- log_level: SHOW_LOGS ? 0x400000 : 0,
- });
- let ran_command = false;
- let line = "";
- let capturing = false;
- let capture = "";
- let next_trigger;
- let next_trigger_handler;
- async function prepare_test()
- {
- console.log("\nPreparing test #%d: %s", test_num, tests[test_num].name);
- if(tests[test_num].timeout)
- {
- test_timeout = setTimeout(() =>
- {
- console.error("[-] Test #%d (%s) took longer than %s sec. Timing out and terminating.", test_num, tests[test_num].name, tests[test_num].timeout);
- process.exit(1);
- }, tests[test_num].timeout * 1000);
- }
- console.log(" Nuking /mnt");
- emulator.fs9p.RecursiveDelete("");
- if(tests[test_num].use_fsjson)
- {
- console.log(" Reloading files from json");
- emulator.fs9p.load_from_json(testfsjson);
- }
- console.log(" Configuring mounts");
- if(tests[test_num].mounts && tests[test_num].mounts.length > 0)
- {
- for(const { path, baseurl, basefs } of tests[test_num].mounts)
- {
- await async function() {
- return new Promise((resolve, reject) => {
- emulator.serial0_send("mkdir -p /mnt" + path + "\n");
- emulator.serial0_send("rmdir /mnt" + path + "\n");
- emulator.serial0_send("echo done-premount\n");
- next_trigger = "done-premount";
- next_trigger_handler = resolve;
- });
- }();
- emulator.mount_fs(path, baseurl, basefs);
- }
- }
- console.log(" Loading additional files");
- if(tests[test_num].files)
- {
- let remaining = tests[test_num].files.length;
- for(const f of tests[test_num].files)
- {
- await emulator.create_file(f.file, f.data);
- }
- }
- console.log("Starting test #%d: %s", test_num, tests[test_num].name);
- capture = "";
- tests[test_num].start();
- if(tests[test_num].capture_trigger)
- {
- next_trigger = tests[test_num].capture_trigger;
- next_trigger_handler = start_capture;
- }
- else
- {
- next_trigger = tests[test_num].end_trigger;
- next_trigger_handler = end_test;
- }
- }
- function start_capture()
- {
- console.log("Capturing...");
- capture = "";
- capturing = true;
- next_trigger = tests[test_num].end_trigger;
- next_trigger_handler = end_test;
- }
- function end_test()
- {
- capturing = false;
- if(tests[test_num].timeout)
- {
- clearTimeout(test_timeout);
- }
- tests[test_num].end(capture, report_test);
- }
- function report_test()
- {
- if(!test_has_failed)
- {
- console.log("[+] Test #%d passed: %s", test_num, tests[test_num].name);
- }
- else
- {
- if(tests[test_num].allow_failure)
- {
- console.warn("Test #%d failed: %s (failure allowed)", test_num, tests[test_num].name);
- }
- else
- {
- console.error("[-] Test #%d failed: %s", test_num, tests[test_num].name);
- if(STOP_ON_FIRST_FAILURE)
- {
- finish_tests();
- }
- }
- test_has_failed = false;
- }
- test_num++;
- if(test_num < tests.length)
- {
- prepare_test();
- }
- else
- {
- finish_tests();
- }
- }
- function finish_tests()
- {
- emulator.stop();
- console.log("\nTests finished.");
- if(failed_tests.length === 0)
- {
- console.log("All tests passed");
- }
- else
- {
- let unallowed_failure = false;
- console.error("[-] Failed %d out of %d tests:", failed_tests.length, tests.length);
- for(const num of failed_tests)
- {
- if(tests[num].allow_failure)
- {
- console.warn("#%d %s (failure allowed)", num, tests[num].name);
- }
- else
- {
- unallowed_failure = true;
- console.error("[-] #%d %s", num, tests[num].name);
- }
- }
- if(unallowed_failure)
- {
- process.exit(1);
- }
- }
- }
- emulator.bus.register("emulator-started", function()
- {
- console.log("Booting now, please stand by");
- });
- emulator.add_listener("serial0-output-byte", function(byte)
- {
- var chr = String.fromCharCode(byte);
- if(chr < " " && chr !== "\n" && chr !== "\t" || chr > "~")
- {
- return;
- }
- let new_line = "";
- let is_new_line = false;
- if(chr === "\n")
- {
- is_new_line = true;
- new_line = line;
- line = "";
- }
- else
- {
- line += chr;
- }
- if(!ran_command && line.endsWith("~% "))
- {
- ran_command = true;
- prepare_test();
- }
- else if(new_line === next_trigger)
- {
- next_trigger_handler();
- }
- else if(is_new_line && capturing)
- {
- capture += new_line + "\n";
- console.log(" Captured: %s", new_line);
- }
- else if(is_new_line)
- {
- console.log(" Serial: %s", new_line);
- }
- });
|