wisp_network.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. #!/usr/bin/env -S node --experimental-websocket
  2. "use strict";
  3. process.on("unhandledRejection", exn => { throw exn; });
  4. const TEST_RELEASE_BUILD = +process.env.TEST_RELEASE_BUILD;
  5. const V86 = require(`../../build/${TEST_RELEASE_BUILD ? "libv86" : "libv86-debug"}.js`).V86;
  6. const assert = require("assert").strict;
  7. const SHOW_LOGS = false;
  8. const tests =
  9. [
  10. {
  11. name: "DHCP",
  12. start: () =>
  13. {
  14. emulator.serial0_send("udhcpc\n");
  15. emulator.serial0_send("echo -e done\\\\tudhcpc\n");
  16. },
  17. end_trigger: "done\tudhcpc",
  18. end: (capture) =>
  19. {
  20. assert(/lease of 192.168.86.100 obtained/.test(capture), "lease of 192.168.86.100 obtained");
  21. },
  22. },
  23. {
  24. name: "ifconfig",
  25. start: () =>
  26. {
  27. emulator.serial0_send("ifconfig\n");
  28. emulator.serial0_send("echo -e done\\\\tifconfig\n");
  29. },
  30. end_trigger: "done\tifconfig",
  31. end: (capture) =>
  32. {
  33. assert(/192.168.86.100/.test(capture), "192.168.86.100");
  34. },
  35. },
  36. {
  37. name: "route",
  38. start: () =>
  39. {
  40. emulator.serial0_send("ip route\n");
  41. emulator.serial0_send("echo -e done\\\\troute\n");
  42. },
  43. end_trigger: "done\troute",
  44. end: (capture) =>
  45. {
  46. assert(/192.168.86.1/.test(capture), "192.168.86.100");
  47. },
  48. },
  49. //{
  50. // name: "arp -a",
  51. // start: () =>
  52. // {
  53. // emulator.serial0_send("arp -a\n");
  54. // emulator.serial0_send("echo -e done\\\\tarp\n");
  55. // },
  56. // end_trigger: "done\tarp",
  57. // end: (capture) =>
  58. // {
  59. // assert(/.192.168.86.1. at 52:54:00:01:02:03 \[ether\] {2}on eth0/.test(capture), "(192.168.86.1) at 52:54:00:01:02:03 [ether] on eth0");
  60. // },
  61. //},
  62. {
  63. name: "Curl example.org",
  64. allow_failure: true,
  65. start: () =>
  66. {
  67. emulator.serial0_send("wget -T 10 -O - example.org\n");
  68. emulator.serial0_send("echo -e done\\\\texample.org\n");
  69. },
  70. end_trigger: "done\texample.org",
  71. end: (capture) =>
  72. {
  73. assert(/This domain is for use in illustrative examples in documents/.test(capture), "got example.org text");
  74. },
  75. },
  76. ];
  77. const emulator = new V86({
  78. bios: { url: __dirname + "/../../bios/seabios.bin" },
  79. vga_bios: { url: __dirname + "/../../bios/vgabios.bin" },
  80. cdrom: { url: __dirname + "/../../images/linux4.iso" },
  81. autostart: true,
  82. memory_size: 64 * 1024 * 1024,
  83. disable_jit: +process.env.DISABLE_JIT,
  84. network_relay_url: "wisps://wisp.mercurywork.shop/",
  85. log_level: SHOW_LOGS ? 0x400000 : 0,
  86. });
  87. let test_num = 0;
  88. let booted = false;
  89. let line = "";
  90. let capture = "";
  91. let end_trigger;
  92. emulator.bus.register("emulator-started", function()
  93. {
  94. console.log("Booting now, please stand by");
  95. });
  96. emulator.add_listener("serial0-output-byte", function(byte)
  97. {
  98. const chr = String.fromCharCode(byte);
  99. if(chr < " " && chr !== "\n" && chr !== "\t" || chr > "~")
  100. {
  101. return;
  102. }
  103. let new_line = "";
  104. if(chr === "\n")
  105. {
  106. console.log(" Captured: %s", line);
  107. new_line = line;
  108. capture += line + "\n";
  109. line = "";
  110. }
  111. else
  112. {
  113. line += chr;
  114. }
  115. if(new_line === end_trigger)
  116. {
  117. let test_has_failed = false;
  118. try {
  119. tests[test_num].end(capture);
  120. } catch(e) {
  121. console.log(e);
  122. test_has_failed = true;
  123. }
  124. if(!test_has_failed)
  125. {
  126. console.log("[+] Test #%d passed: %s", test_num, tests[test_num].name);
  127. }
  128. else
  129. {
  130. if(tests[test_num].allow_failure)
  131. {
  132. console.warn("[!] Test #%d failed: %s (failure allowed)", test_num, tests[test_num].name);
  133. }
  134. else
  135. {
  136. console.error("[-] Test #%d failed: %s", test_num, tests[test_num].name);
  137. process.exit(1);
  138. }
  139. }
  140. test_num++;
  141. }
  142. if(!booted && line.endsWith("~% ") || new_line === end_trigger)
  143. {
  144. booted = true;
  145. if(test_num >= tests.length)
  146. {
  147. emulator.stop();
  148. emulator.destroy();
  149. console.log("Tests finished.");
  150. }
  151. else
  152. {
  153. console.log("Starting test #%d: %s", test_num, tests[test_num].name);
  154. capture = "";
  155. end_trigger = tests[test_num].end_trigger;
  156. tests[test_num].start();
  157. }
  158. }
  159. });