1
0

testcjdroute.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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/events/EventBase.h"
  20. #include "util/CString.h"
  21. #include "memory/Allocator.h"
  22. #include "wire/Message.h"
  23. #include "test/FuzzTest.h"
  24. #include "util/Js.h"
  25. #include <stdio.h>
  26. #include <unistd.h>
  27. #include <fcntl.h>
  28. #ifdef SUBNODE
  29. #define testcjdroute_SUBNODE 1
  30. #else
  31. #define testcjdroute_SUBNODE 0
  32. #endif
  33. Js({ return builder.config.cjdnsTest_prototypes; })
  34. typedef int (* Test)(int argc, char** argv);
  35. typedef void* (* FuzzTestInit)(struct Allocator* alloc, struct Random* rand);
  36. typedef void (* FuzzTest)(void* ctx, struct Message* fuzz);
  37. typedef struct FuzzTest* (* MkFuzz)(struct Allocator* alloc);
  38. static const struct {
  39. Test func;
  40. char* name;
  41. } TESTS[] = { Js({ return builder.config.cjdnsTest_tests }) };
  42. static const int TEST_COUNT = (int) (sizeof(TESTS) / sizeof(*TESTS));
  43. static const struct {
  44. FuzzTestInit init;
  45. FuzzTest fuzz;
  46. char* name;
  47. } FUZZ_TESTS[] = { Js({ return builder.config.cjdnsTest_fuzzTests }) };
  48. static const int FUZZ_TEST_COUNT = (int) (sizeof(FUZZ_TESTS) / sizeof(*FUZZ_TESTS));
  49. static const char* FUZZ_CASES[] = { Js({ return builder.config.cjdnsTest_fuzzCases }) };
  50. static const int FUZZ_CASE_COUNT = (int) (sizeof(FUZZ_CASES) / sizeof(*FUZZ_CASES));
  51. Js({ builder.config.cjdnsTest_files.forEach((f) => js.linkerDependency(f)); })
  52. static uint64_t runTest(Test test,
  53. char* name,
  54. uint64_t startTime,
  55. int argc,
  56. char** argv,
  57. int quiet)
  58. {
  59. if (!quiet) { fprintf(stderr, "Running test %s", name); }
  60. Assert_true(!test(argc, argv));
  61. if (!quiet) {
  62. uint64_t now = Time_hrtime();
  63. char* seventySpaces =
  64. " ";
  65. int count = CString_strlen(name);
  66. if (count > 69) { count = 69; }
  67. fprintf(stderr, "%s%d.%d ms\n",
  68. &seventySpaces[count],
  69. (int)((now - startTime)/1000000),
  70. (int)((now - startTime)/1000)%1000);
  71. return now;
  72. }
  73. return startTime;
  74. }
  75. static void usage(char* appName)
  76. {
  77. printf("%s <test> run one test\n", appName);
  78. printf("%s all run every test\n\n", appName);
  79. printf("Flags:\n");
  80. printf(" --quiet # Don't write test timings to stderr");
  81. printf(" --stderr-to <path> # Redirect stderr to a file (append mode)");
  82. printf(" --inittests # When using `fuzz` first initialize ALL tests");
  83. printf("\n");
  84. printf("Available Tests:\n");
  85. for (int i = 0; i < TEST_COUNT; i++) {
  86. printf("%s %s\n", appName, TESTS[i].name);
  87. }
  88. printf("\nAvailable Fuzz Tests:\n");
  89. for (int i = 0; i < FUZZ_CASE_COUNT; i++) {
  90. printf("%s fuzz < %s\n", appName, FUZZ_CASES[i]);
  91. }
  92. }
  93. static void readFile(int fileNo, struct Allocator* alloc, struct Message* fuzz)
  94. {
  95. uint32_t msgLen = Message_getLength(fuzz);
  96. uint8_t* bytes = Er_assert(Message_peakBytes(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. Er_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. struct Message* fuzz,
  117. const char* testCase,
  118. int quiet)
  119. {
  120. if (Message_getLength(fuzz) < 4) { return 100; }
  121. uint32_t selector = Er_assert(Message_epop32be(fuzz));
  122. if (selector >= (uint32_t)FUZZ_TEST_COUNT) {
  123. printf("selector [%x] out of bounds [%u]\n", selector, FUZZ_TEST_COUNT);
  124. return 100;
  125. }
  126. if (!testCase) { testCase = FUZZ_TESTS[selector].name; }
  127. if (!quiet) { fprintf(stderr, "Running fuzz %s", testCase); }
  128. void* ctx = ctxs ? ctxs[selector] : FUZZ_TESTS[selector].init(alloc, rand);
  129. FUZZ_TESTS[selector].fuzz(ctx, fuzz);
  130. return 0;
  131. }
  132. static uint64_t runFuzzTestManual(
  133. struct Allocator* alloc,
  134. struct Random* detRand,
  135. const char* testCase,
  136. uint64_t startTime,
  137. int quiet)
  138. {
  139. int f = open(testCase, O_RDONLY);
  140. Assert_true(f > -1);
  141. struct Message* fuzz = Message_new(4096, 128, alloc);
  142. readFile(f, alloc, fuzz);
  143. close(f);
  144. runFuzzTest(NULL, alloc, detRand, fuzz, testCase, quiet);
  145. if (!quiet) {
  146. uint64_t now = Time_hrtime();
  147. char* seventySpaces =
  148. " ";
  149. int count = CString_strlen(testCase);
  150. if (count > 69) { count = 69; }
  151. fprintf(stderr, "%s%d.%d ms\n",
  152. &seventySpaces[count],
  153. (int)((now - startTime)/1000000),
  154. (int)((now - startTime)/1000)%1000);
  155. return now;
  156. }
  157. return startTime;
  158. }
  159. // We don't really want to let AFL write the random seed because the amount of mixing
  160. // that occurs between the input and output makes any attempt at optimizing the seed
  161. // useless.
  162. //
  163. // We do, however, want to make sure that crashes discovered by AFL are reproducable.
  164. //
  165. // We might imagine letting part of the AFL message content be the random data which
  166. // is returned, but we don't know how much will be requested in advance. Possibly
  167. // something for the future.
  168. #define RANDOM_SEED "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
  169. static int fuzzMain(struct Allocator* alloc, struct Random* detRand, int initTests, int quiet)
  170. {
  171. struct Message* fuzz = Message_new(4096, 128, alloc);
  172. #ifdef __AFL_INIT
  173. // Enable AFL deferred forkserver mode. Requires compilation using afl-clang-fast
  174. initTests = 1;
  175. #endif
  176. void** ctxs = (initTests) ? initFuzzTests(alloc, detRand) : NULL;
  177. #ifdef __AFL_INIT
  178. __AFL_INIT();
  179. #endif
  180. readFile(STDIN_FILENO, alloc, fuzz);
  181. int out = runFuzzTest(ctxs, alloc, detRand, fuzz, NULL, quiet);
  182. printf("\n");
  183. return out;
  184. }
  185. static void stderrTo(char* file)
  186. {
  187. int f = open(file, O_CREAT | O_APPEND | O_WRONLY, 0666);
  188. Assert_true(f > -1);
  189. Assert_true(dup2(f, STDERR_FILENO) == STDERR_FILENO);
  190. }
  191. static int main2(int argc, char** argv, struct Allocator* alloc, struct Random* detRand)
  192. {
  193. int initTests = 0;
  194. int quiet = 0;
  195. for (int i = 0; i < argc; i++) {
  196. if (!CString_strcmp("--inittests", argv[i])) { initTests = 1; }
  197. if (!CString_strcmp("--stderr-to", argv[i]) && argc > i + 1) { stderrTo(argv[i + 1]); }
  198. if (!CString_strcmp("--quiet", argv[i])) { quiet = 1; }
  199. }
  200. if (argc > 1 && !CString_strcmp("fuzz", argv[1])) {
  201. return fuzzMain(alloc, detRand, initTests, quiet);
  202. }
  203. uint64_t now = Time_hrtime();
  204. uint64_t startTime = now;
  205. if (argc < 2) {
  206. Assert_true(argc > 0);
  207. usage(argv[0]);
  208. return 100;
  209. }
  210. if (argc > 1 && CString_strcmp("all", argv[1])) {
  211. for (int i = 0; i < TEST_COUNT; i++) {
  212. if (!CString_strcmp(TESTS[i].name, argv[1])) {
  213. TESTS[i].func(argc, argv);
  214. return 0;
  215. }
  216. }
  217. for (int i = 0; i < FUZZ_CASE_COUNT; i++) {
  218. if (!CString_strcmp(FUZZ_CASES[i], argv[1])) {
  219. runFuzzTestManual(alloc, detRand, FUZZ_CASES[i], now, quiet);
  220. return 0;
  221. }
  222. }
  223. usage(argv[0]);
  224. return 100;
  225. }
  226. for (int i = 0; i < TEST_COUNT; i++) {
  227. now = runTest(TESTS[i].func, TESTS[i].name, now, argc, argv, quiet);
  228. }
  229. for (int i = 0; i < FUZZ_CASE_COUNT; i++) {
  230. // TODO(cjd): Apparently a race condition in the allocator
  231. // if you have async freeing in progress and then you come in and
  232. // free the root allocator, you get an assertion.
  233. //
  234. //struct Allocator* child = Allocator_child(alloc);
  235. struct Allocator* child = Allocator_new(1<<24);
  236. now = runFuzzTestManual(child, detRand, FUZZ_CASES[i], now, quiet);
  237. Allocator_free(child);
  238. }
  239. if (!quiet) {
  240. fprintf(stderr, "Total test time %d.%d ms\n",
  241. (int)((now - startTime)/1000000),
  242. (int)((now - startTime)/1000)%1000);
  243. }
  244. return 0;
  245. }
  246. int testcjdroute_main(int argc, char** argv);
  247. int testcjdroute_main(int argc, char** argv)
  248. {
  249. struct Allocator* alloc = Allocator_new(1<<24);
  250. Allocator_free(alloc);
  251. alloc = Allocator_new(1<<24);
  252. RandomSeed_t* rs = DeterminentRandomSeed_new(alloc, RANDOM_SEED);
  253. struct Random* detRand = Random_newWithSeed(alloc, NULL, rs, NULL);
  254. int out = main2(argc, argv, alloc, detRand);
  255. Allocator_free(alloc);
  256. return out;
  257. }