examples_run.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. #include <tools/ccanlint/ccanlint.h>
  2. #include <tools/tools.h>
  3. #include <ccan/foreach/foreach.h>
  4. #include <ccan/str/str.h>
  5. #include <ccan/tal/str/str.h>
  6. #include <ccan/cast/cast.h>
  7. #include <sys/types.h>
  8. #include <sys/stat.h>
  9. #include <fcntl.h>
  10. #include <stdint.h>
  11. #include <string.h>
  12. #include <unistd.h>
  13. #include <ctype.h>
  14. #include <assert.h>
  15. static const char *can_run(struct manifest *m)
  16. {
  17. struct list_head *list;
  18. if (safe_mode)
  19. return "Safe mode enabled";
  20. foreach_ptr(list, &m->examples, &m->mangled_examples)
  21. if (!list_empty(list))
  22. return NULL;
  23. return "No examples";
  24. }
  25. /* Very dumb scanner, allocates %s-strings. */
  26. static bool scan_forv(const void *ctx,
  27. const char *input, const char *fmt, va_list *args)
  28. {
  29. va_list ap;
  30. bool ret;
  31. if (input[0] == '\0' || fmt[0] == '\0')
  32. return input[0] == fmt[0];
  33. va_copy(ap, *args);
  34. if (cisspace(fmt[0])) {
  35. /* One format space can swallow many input spaces */
  36. ret = false;
  37. while (cisspace(input[0])) {
  38. if (scan_forv(ctx, ++input, fmt+1, &ap)) {
  39. ret = true;
  40. break;
  41. }
  42. }
  43. } else if (fmt[0] != '%') {
  44. if (toupper(input[0]) != toupper(fmt[0]))
  45. ret = false;
  46. else
  47. ret = scan_forv(ctx, input+1, fmt+1, &ap);
  48. } else {
  49. char **p = va_arg(ap, char **);
  50. unsigned int len;
  51. ret = false;
  52. assert(fmt[1] == 's');
  53. for (len = 1; input[len-1]; len++) {
  54. ret = scan_forv(ctx, input + len, fmt+2, &ap);
  55. if (ret) {
  56. *p = tal_strndup(ctx, input, len);
  57. ret = true;
  58. break;
  59. }
  60. }
  61. }
  62. va_end(ap);
  63. return ret;
  64. }
  65. static bool scan_for(const void *ctx, const char *input, const char *fmt, ...)
  66. {
  67. bool ret;
  68. va_list ap;
  69. va_start(ap, fmt);
  70. ret = scan_forv(ctx, input, fmt, &ap);
  71. va_end(ap);
  72. return ret;
  73. }
  74. static char *find_expect(struct ccan_file *file,
  75. char **lines, char **input,
  76. bool *contains, bool *whitespace, bool *error,
  77. unsigned *line)
  78. {
  79. char *expect;
  80. *error = false;
  81. for (; lines[*line]; (*line)++) {
  82. char *p = lines[*line] + strspn(lines[*line], " \t");
  83. if (!strstarts(p, "//"))
  84. continue;
  85. p += strspn(p, "/ ");
  86. /* With or without input? */
  87. if (strncasecmp(p, "given", strlen("given")) == 0) {
  88. /* Must be of form <given "X"> */
  89. if (!scan_for(file, p, "given \"%s\" %s", input, &p)) {
  90. *error = true;
  91. return p;
  92. }
  93. } else {
  94. *input = NULL;
  95. }
  96. if (scan_for(file, p, "outputs \"%s\"", &expect)) {
  97. *whitespace = true;
  98. *contains = false;
  99. return expect;
  100. }
  101. if (scan_for(file, p, "output contains \"%s\"", &expect)) {
  102. *whitespace = true;
  103. *contains = true;
  104. return expect;
  105. }
  106. /* Whitespace-ignoring versions. */
  107. if (scan_for(file, p, "outputs %s", &expect)) {
  108. *whitespace = false;
  109. *contains = false;
  110. return expect;
  111. }
  112. if (scan_for(file, p, "output contains %s", &expect)) {
  113. *whitespace = false;
  114. *contains = true;
  115. return expect;
  116. }
  117. /* Other malformed line? */
  118. if (*input || !strncasecmp(p, "output", strlen("output"))) {
  119. *error = true;
  120. return p;
  121. }
  122. }
  123. return NULL;
  124. }
  125. static char *unexpected(struct ccan_file *i, const char *input,
  126. const char *expect, bool contains, bool whitespace)
  127. {
  128. char *output, *cmd;
  129. const char *p;
  130. bool ok;
  131. unsigned int default_time = default_timeout_ms;
  132. if (input)
  133. cmd = tal_fmt(i, "echo '%s' | %s %s",
  134. input, i->compiled[COMPILE_NORMAL], input);
  135. else
  136. cmd = tal_fmt(i, "%s", i->compiled[COMPILE_NORMAL]);
  137. output = run_with_timeout(i, cmd, &ok, &default_time);
  138. if (!ok)
  139. return tal_fmt(i, "Exited with non-zero status\n");
  140. /* Substitute \n */
  141. while ((p = strstr(expect, "\\n")) != NULL) {
  142. expect = tal_fmt(cmd, "%.*s\n%s", (int)(p - expect), expect,
  143. p+2);
  144. }
  145. if (!whitespace) {
  146. /* Normalize to spaces. */
  147. expect = tal_strjoin(cmd,
  148. tal_strsplit(cmd, expect, " \n\t",
  149. STR_NO_EMPTY),
  150. " ", STR_NO_TRAIL);
  151. output = tal_strjoin(cmd,
  152. tal_strsplit(cmd, output, " \n\t",
  153. STR_NO_EMPTY),
  154. " ", STR_NO_TRAIL);
  155. }
  156. if (contains) {
  157. if (strstr(output, expect))
  158. return NULL;
  159. } else {
  160. if (streq(output, expect))
  161. return NULL;
  162. }
  163. return output;
  164. }
  165. static void run_examples(struct manifest *m,
  166. unsigned int *timeleft UNNEEDED, struct score *score)
  167. {
  168. struct ccan_file *i;
  169. struct list_head *list;
  170. score->total = 0;
  171. score->pass = true;
  172. foreach_ptr(list, &m->examples, &m->mangled_examples) {
  173. list_for_each(list, i, list) {
  174. char **lines, *expect, *input, *output;
  175. unsigned int linenum = 0;
  176. bool contains, whitespace, error;
  177. lines = get_ccan_file_lines(i);
  178. for (expect = find_expect(i, lines, &input,
  179. &contains, &whitespace, &error,
  180. &linenum);
  181. expect;
  182. linenum++,
  183. expect = find_expect(i, lines, &input,
  184. &contains, &whitespace,
  185. &error, &linenum)) {
  186. if (error) {
  187. score_file_error(score, i, linenum+1,
  188. "Unparsable test line '%s'",
  189. lines[linenum]);
  190. score->pass = false;
  191. break;
  192. }
  193. if (i->compiled[COMPILE_NORMAL] == NULL)
  194. continue;
  195. score->total++;
  196. output = unexpected(i, input, expect,
  197. contains, whitespace);
  198. if (!output) {
  199. score->score++;
  200. continue;
  201. }
  202. score_file_error(score, i, linenum+1,
  203. "output '%s' didn't %s '%s'\n",
  204. output,
  205. contains ? "contain" : "match",
  206. expect);
  207. score->pass = false;
  208. }
  209. }
  210. }
  211. }
  212. /* FIXME: Test with reduced features, valgrind! */
  213. struct ccanlint examples_run = {
  214. .key = "examples_run",
  215. .name = "Module examples with expected output give that output",
  216. .check = run_examples,
  217. .can_run = can_run,
  218. .needs = "examples_compile"
  219. };
  220. REGISTER_TEST(examples_run);