examples_run.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. #include <tools/ccanlint/ccanlint.h>
  2. #include <tools/tools.h>
  3. #include <ccan/talloc/talloc.h>
  4. #include <ccan/foreach/foreach.h>
  5. #include <ccan/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 = talloc_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, bool *exact,
  76. unsigned *line)
  77. {
  78. char *expect;
  79. const char *fmt;
  80. for (; lines[*line]; (*line)++) {
  81. char *p = lines[*line] + strspn(lines[*line], " \t");
  82. if (!strstarts(p, "//"))
  83. continue;
  84. p += strspn(p, "/ ");
  85. foreach_ptr(fmt,
  86. "given '%s', outputs '%s'",
  87. "given '%s' outputs '%s'",
  88. "given \"%s\", outputs \"%s\"",
  89. "given \"%s\" outputs \"%s\"") {
  90. if (scan_for(file, p, fmt, input, &expect)) {
  91. *exact = true;
  92. return expect;
  93. }
  94. }
  95. foreach_ptr(fmt,
  96. "given '%s', output contains '%s'",
  97. "given '%s' output contains '%s'",
  98. "given \"%s\", output contains \"%s\"",
  99. "given \"%s\" output contains \"%s\"") {
  100. if (scan_for(file, p, fmt, input, &expect)) {
  101. *exact = false;
  102. return expect;
  103. }
  104. }
  105. foreach_ptr(fmt, "outputs '%s'", "outputs \"%s\"") {
  106. if (scan_for(file, p, fmt, &expect)) {
  107. *input = cast_const(char *, "");
  108. *exact = true;
  109. return expect;
  110. }
  111. }
  112. foreach_ptr(fmt,
  113. "given '%s', output contains '%s'",
  114. "given '%s' output contains '%s'",
  115. "given \"%s\", output contains \"%s\"",
  116. "given \"%s\" output contains \"%s\"") {
  117. if (scan_for(file, p, fmt, input, &expect)) {
  118. *exact = false;
  119. return expect;
  120. }
  121. }
  122. /* Unquoted versions... we can get this wrong! */
  123. foreach_ptr(fmt,
  124. "given %s, outputs '%s'",
  125. "given '%s', outputs %s",
  126. "given %s, outputs \"%s\"",
  127. "given \"%s\", outputs %s",
  128. "given %s, outputs %s",
  129. "given %s outputs '%s'",
  130. "given '%s' outputs %s",
  131. "given %s outputs \"%s\"",
  132. "given \"%s\" outputs %s",
  133. "given %s outputs %s") {
  134. if (scan_for(file, p, fmt, input, &expect)) {
  135. *exact = true;
  136. return expect;
  137. }
  138. }
  139. foreach_ptr(fmt,
  140. "given %s, output contains '%s'",
  141. "given '%s', output contains %s",
  142. "given %s, output contains \"%s\"",
  143. "given \"%s\", output contains %s",
  144. "given %s, output contains %s",
  145. "given %s output contains '%s'",
  146. "given '%s' output contains %s",
  147. "given %s output contains \"%s\"",
  148. "given \"%s\" output contains %s",
  149. "given %s output contains %s") {
  150. if (scan_for(file, p, fmt, input, &expect)) {
  151. *exact = false;
  152. return expect;
  153. }
  154. }
  155. foreach_ptr(fmt,
  156. "outputs '%s'",
  157. "outputs \"%s\"",
  158. "outputs %s") {
  159. if (scan_for(file, p, fmt, &expect)) {
  160. *input = cast_const(char *, "");
  161. *exact = true;
  162. return expect;
  163. }
  164. }
  165. foreach_ptr(fmt,
  166. "output contains '%s'",
  167. "output contains \"%s\"",
  168. "output contains %s") {
  169. if (scan_for(file, p, fmt, &expect)) {
  170. *input = cast_const(char *, "");
  171. *exact = false;
  172. return expect;
  173. }
  174. }
  175. }
  176. return NULL;
  177. }
  178. static char *trim(char *string)
  179. {
  180. while (strends(string, "\n"))
  181. string[strlen(string)-1] = '\0';
  182. return string;
  183. }
  184. static char *unexpected(struct ccan_file *i, const char *input,
  185. const char *expect, bool exact)
  186. {
  187. char *output, *cmd;
  188. bool ok;
  189. unsigned int default_time = default_timeout_ms;
  190. cmd = talloc_asprintf(i, "echo '%s' | %s %s",
  191. input, i->compiled[COMPILE_NORMAL], input);
  192. output = run_with_timeout(i, cmd, &ok, &default_time);
  193. if (!ok)
  194. return talloc_asprintf(i, "Exited with non-zero status\n");
  195. if (exact) {
  196. if (streq(output, expect) || streq(trim(output), expect))
  197. return NULL;
  198. } else {
  199. if (strstr(output, expect))
  200. return NULL;
  201. }
  202. return output;
  203. }
  204. static void run_examples(struct manifest *m, bool keep,
  205. unsigned int *timeleft, struct score *score)
  206. {
  207. struct ccan_file *i;
  208. struct list_head *list;
  209. score->total = 0;
  210. score->pass = true;
  211. foreach_ptr(list, &m->examples, &m->mangled_examples) {
  212. list_for_each(list, i, list) {
  213. char **lines, *expect, *input, *output;
  214. unsigned int linenum = 0;
  215. bool exact;
  216. lines = get_ccan_file_lines(i);
  217. for (expect = find_expect(i, lines, &input, &exact,
  218. &linenum);
  219. expect;
  220. linenum++,
  221. expect = find_expect(i, lines, &input,
  222. &exact, &linenum)) {
  223. if (i->compiled[COMPILE_NORMAL] == NULL)
  224. continue;
  225. score->total++;
  226. output = unexpected(i, input, expect, exact);
  227. if (!output) {
  228. score->score++;
  229. continue;
  230. }
  231. score_file_error(score, i, linenum+1,
  232. "output '%s' didn't %s '%s'\n",
  233. output,
  234. exact ? "match" : "contain",
  235. expect);
  236. score->pass = false;
  237. }
  238. }
  239. }
  240. }
  241. /* FIXME: Test with reduced features, valgrind! */
  242. struct ccanlint examples_run = {
  243. .key = "examples_run",
  244. .name = "Module examples with expected output give that output",
  245. .check = run_examples,
  246. .can_run = can_run,
  247. .needs = "examples_compile"
  248. };
  249. REGISTER_TEST(examples_run);