testcjdroute.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /* vim: set expandtab ts=4 sw=4: */
  2. /*
  3. * You may redistribute this program and/or modify it under the terms of
  4. * the GNU General Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. */
  15. #include "crypto/random/Random.h"
  16. #include "crypto/random/test/DeterminentRandomSeed.h"
  17. #include "util/Assert.h"
  18. #include "util/events/Time.h"
  19. #include "util/CString.h"
  20. #include "memory/Allocator.h"
  21. #include "wire/Message.h"
  22. #include "util/Js.h"
  23. #include "util/events/libuv/Glock.h"
  24. #include <stdio.h>
  25. #include <unistd.h>
  26. #include <fcntl.h>
  27. #ifdef SUBNODE
  28. #define testcjdroute_SUBNODE 1
  29. #else
  30. #define testcjdroute_SUBNODE 0
  31. #endif
  32. Js({ return builder.config.cjdnsTest_prototypes; })
  33. typedef int (* Test)(int argc, char** argv);
  34. typedef void* (* FuzzTestInit)(struct Allocator* alloc, struct Random* rand);
  35. typedef void (* FuzzTest)(void* ctx, Message_t* fuzz);
  36. typedef struct FuzzTest* (* MkFuzz)(struct Allocator* alloc);
  37. static const struct {
  38. Test func;
  39. char* name;
  40. } TESTS[] = { Js({ return builder.config.cjdnsTest_tests }) };
  41. static const int TEST_COUNT = (int) (sizeof(TESTS) / sizeof(*TESTS));
  42. static const struct {
  43. FuzzTestInit init;
  44. FuzzTest fuzz;
  45. char* name;
  46. } FUZZ_TESTS[] = { Js({ return builder.config.cjdnsTest_fuzzTests }) };
  47. static const int FUZZ_TEST_COUNT = (int) (sizeof(FUZZ_TESTS) / sizeof(*FUZZ_TESTS));
  48. static const char* FUZZ_CASES[] = { Js({ return builder.config.cjdnsTest_fuzzCases }) };
  49. static const int FUZZ_CASE_COUNT = (int) (sizeof(FUZZ_CASES) / sizeof(*FUZZ_CASES));
  50. Js({ builder.config.cjdnsTest_files.forEach((f) => js.linkerDependency(f)); })
  51. static uint64_t runTest(Test test,
  52. char* name,
  53. uint64_t startTime,
  54. int argc,
  55. char** argv,
  56. int quiet)
  57. {
  58. if (!quiet) { fprintf(stderr, "Running test %s", name); }
  59. Assert_true(!test(argc, argv));
  60. if (!quiet) {
  61. uint64_t now = Time_hrtime();
  62. char* seventySpaces =
  63. " ";
  64. int count = CString_strlen(name);
  65. if (count > 69) { count = 69; }
  66. fprintf(stderr, "%s%d.%d ms\n",
  67. &seventySpaces[count],
  68. (int)((now - startTime)/1000000),
  69. (int)((now - startTime)/1000)%1000);
  70. return now;
  71. }
  72. return startTime;
  73. }
  74. static void usage(char* appName)
  75. {
  76. printf("%s <test> run one test\n", appName);
  77. printf("%s all run every test\n\n", appName);
  78. printf("Flags:\n");
  79. printf(" --quiet # Don't write test timings to stderr");
  80. printf(" --stderr-to <path> # Redirect stderr to a file (append mode)");
  81. printf(" --inittests # When using `fuzz` first initialize ALL tests");
  82. printf("\n");
  83. printf("Available Tests:\n");
  84. for (int i = 0; i < TEST_COUNT; i++) {
  85. printf("%s %s\n", appName, TESTS[i].name);
  86. }
  87. printf("\nAvailable Fuzz Tests:\n");
  88. for (int i = 0; i < FUZZ_CASE_COUNT; i++) {
  89. printf("%s fuzz < %s\n", appName, FUZZ_CASES[i]);
  90. }
  91. }
  92. static void readFile(int fileNo, struct Allocator* alloc, Message_t* fuzz)
  93. {
  94. int32_t msgLen = Message_getLength(fuzz);
  95. uint8_t* bytes = NULL;
  96. Err_assert(Message_peakBytes(&bytes, fuzz, msgLen));
  97. ssize_t length = read(fileNo, bytes, msgLen);
  98. if (length >= msgLen) {
  99. printf("No test files over [%d] bytes\n", msgLen);
  100. length = 0;
  101. }
  102. Err_assert(Message_truncate(fuzz, length));
  103. }
  104. static void** initFuzzTests(struct Allocator* alloc, struct Random* rand)
  105. {
  106. void** contexts = Allocator_calloc(alloc, sizeof(char*), FUZZ_TEST_COUNT);
  107. for (int i = 0; i < FUZZ_TEST_COUNT; i++) {
  108. contexts[i] = FUZZ_TESTS[i].init(alloc, rand);
  109. }
  110. return contexts;
  111. }
  112. static int runFuzzTest(
  113. void** ctxs,
  114. struct Allocator* alloc,
  115. struct Random* rand,
  116. Message_t* fuzz,
  117. const char* testCase,
  118. int quiet)
  119. {
  120. if (Message_getLength(fuzz) < 4) { return 100; }
  121. uint32_t selector = -1;
  122. Err_assert(Message_epop32be(&selector, fuzz));
  123. if (selector >= (uint32_t)FUZZ_TEST_COUNT) {
  124. printf("selector [%x] out of bounds [%u]\n", selector, FUZZ_TEST_COUNT);
  125. return 100;
  126. }
  127. if (!testCase) { testCase = FUZZ_TESTS[selector].name; }
  128. if (!quiet) { fprintf(stderr, "Running fuzz %s", testCase); }
  129. void* ctx = ctxs ? ctxs[selector] : FUZZ_TESTS[selector].init(alloc, rand);
  130. FUZZ_TESTS[selector].fuzz(ctx, fuzz);
  131. return 0;
  132. }
  133. static uint64_t runFuzzTestManual(
  134. struct Allocator* alloc,
  135. struct Random* detRand,
  136. const char* testCase,
  137. uint64_t startTime,
  138. int quiet)
  139. {
  140. int f = open(testCase, O_RDONLY);
  141. Assert_true(f > -1);
  142. Message_t* fuzz = Message_new(4096, 128, alloc);
  143. readFile(f, alloc, fuzz);
  144. close(f);
  145. runFuzzTest(NULL, alloc, detRand, fuzz, testCase, quiet);
  146. if (!quiet) {
  147. uint64_t now = Time_hrtime();
  148. char* seventySpaces =
  149. " ";
  150. int count = CString_strlen(testCase);
  151. if (count > 69) { count = 69; }
  152. fprintf(stderr, "%s%d.%d ms\n",
  153. &seventySpaces[count],
  154. (int)((now - startTime)/1000000),
  155. (int)((now - startTime)/1000)%1000);
  156. return now;
  157. }
  158. return startTime;
  159. }
  160. // We don't really want to let AFL write the random seed because the amount of mixing
  161. // that occurs between the input and output makes any attempt at optimizing the seed
  162. // useless.
  163. //
  164. // We do, however, want to make sure that crashes discovered by AFL are reproducable.
  165. //
  166. // We might imagine letting part of the AFL message content be the random data which
  167. // is returned, but we don't know how much will be requested in advance. Possibly
  168. // something for the future.
  169. #define RANDOM_SEED "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
  170. static int fuzzMain(struct Allocator* alloc, struct Random* detRand, int initTests, int quiet)
  171. {
  172. Message_t* fuzz = Message_new(4096, 128, alloc);
  173. #ifdef __AFL_INIT
  174. // Enable AFL deferred forkserver mode. Requires compilation using afl-clang-fast
  175. initTests = 1;
  176. #endif
  177. void** ctxs = (initTests) ? initFuzzTests(alloc, detRand) : NULL;
  178. #ifdef __AFL_INIT
  179. __AFL_INIT();
  180. #endif
  181. readFile(STDIN_FILENO, alloc, fuzz);
  182. int out = runFuzzTest(ctxs, alloc, detRand, fuzz, NULL, quiet);
  183. printf("\n");
  184. return out;
  185. }
  186. static void stderrTo(char* file)
  187. {
  188. int f = open(file, O_CREAT | O_APPEND | O_WRONLY, 0666);
  189. Assert_true(f > -1);
  190. Assert_true(dup2(f, STDERR_FILENO) == STDERR_FILENO);
  191. }
  192. static int main2(int argc, char** argv, struct Allocator* alloc, struct Random* detRand)
  193. {
  194. int initTests = 0;
  195. int quiet = 0;
  196. for (int i = 0; i < argc; i++) {
  197. if (!CString_strcmp("--inittests", argv[i])) { initTests = 1; }
  198. if (!CString_strcmp("--stderr-to", argv[i]) && argc > i + 1) { stderrTo(argv[i + 1]); }
  199. if (!CString_strcmp("--quiet", argv[i])) { quiet = 1; }
  200. }
  201. if (argc > 1 && !CString_strcmp("fuzz", argv[1])) {
  202. return fuzzMain(alloc, detRand, initTests, quiet);
  203. }
  204. uint64_t now = Time_hrtime();
  205. uint64_t startTime = now;
  206. if (argc < 2) {
  207. Assert_true(argc > 0);
  208. usage(argv[0]);
  209. return 100;
  210. }
  211. if (argc > 1 && CString_strcmp("all", argv[1])) {
  212. for (int i = 0; i < TEST_COUNT; i++) {
  213. if (!CString_strcmp(TESTS[i].name, argv[1])) {
  214. TESTS[i].func(argc, argv);
  215. return 0;
  216. }
  217. }
  218. for (int i = 0; i < FUZZ_CASE_COUNT; i++) {
  219. if (!CString_strcmp(FUZZ_CASES[i], argv[1])) {
  220. runFuzzTestManual(alloc, detRand, FUZZ_CASES[i], now, quiet);
  221. return 0;
  222. }
  223. }
  224. usage(argv[0]);
  225. return 100;
  226. }
  227. for (int i = 0; i < TEST_COUNT; i++) {
  228. now = runTest(TESTS[i].func, TESTS[i].name, now, argc, argv, quiet);
  229. }
  230. for (int i = 0; i < FUZZ_CASE_COUNT; i++) {
  231. struct Allocator* child = Allocator_child(alloc);
  232. now = runFuzzTestManual(child, detRand, FUZZ_CASES[i], now, quiet);
  233. Allocator_free(child);
  234. }
  235. if (!quiet) {
  236. fprintf(stderr, "Total test time %d.%d ms\n",
  237. (int)((now - startTime)/1000000),
  238. (int)((now - startTime)/1000)%1000);
  239. }
  240. return 0;
  241. }
  242. int testcjdroute_main(int argc, char** argv);
  243. int testcjdroute_main(int argc, char** argv)
  244. {
  245. Glock_init();
  246. struct Allocator* alloc = Allocator_new(1<<24);
  247. Allocator_free(alloc);
  248. alloc = Allocator_new(1<<24);
  249. RandomSeed_t* rs = DeterminentRandomSeed_new(alloc, RANDOM_SEED);
  250. struct Random* detRand = NULL;
  251. Err_assert(Random_newWithSeed(&detRand, alloc, NULL, rs));
  252. int out = main2(argc, argv, alloc, detRand);
  253. Allocator_free(alloc);
  254. // We need to drop the lock before we exit, otherwise the rust thread can't complete.
  255. Glock_beginBlockingCall();
  256. return out;
  257. }