tests-scripts.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #include <dirent.h>
  3. #include <errno.h>
  4. #include <fcntl.h>
  5. #include <linux/ctype.h>
  6. #include <linux/kernel.h>
  7. #include <linux/string.h>
  8. #include <linux/zalloc.h>
  9. #include <string.h>
  10. #include <stdlib.h>
  11. #include <sys/types.h>
  12. #include <unistd.h>
  13. #include <subcmd/exec-cmd.h>
  14. #include <subcmd/parse-options.h>
  15. #include <sys/wait.h>
  16. #include <sys/stat.h>
  17. #include <api/io.h>
  18. #include "builtin.h"
  19. #include "tests-scripts.h"
  20. #include "color.h"
  21. #include "debug.h"
  22. #include "hist.h"
  23. #include "intlist.h"
  24. #include "string2.h"
  25. #include "symbol.h"
  26. #include "tests.h"
  27. #include "util/rlimit.h"
  28. #include "util/util.h"
  29. static int shell_tests__dir_fd(void)
  30. {
  31. struct stat st;
  32. char path[PATH_MAX], path2[PATH_MAX], *exec_path;
  33. static const char * const devel_dirs[] = {
  34. "./tools/perf/tests/shell",
  35. "./tests/shell",
  36. "./source/tests/shell"
  37. };
  38. int fd;
  39. char *p;
  40. for (size_t i = 0; i < ARRAY_SIZE(devel_dirs); ++i) {
  41. fd = open(devel_dirs[i], O_PATH);
  42. if (fd >= 0)
  43. return fd;
  44. }
  45. /* Use directory of executable */
  46. if (readlink("/proc/self/exe", path2, sizeof path2) < 0)
  47. return -1;
  48. /* Follow another level of symlink if there */
  49. if (lstat(path2, &st) == 0 && (st.st_mode & S_IFMT) == S_IFLNK) {
  50. scnprintf(path, sizeof(path), path2);
  51. if (readlink(path, path2, sizeof path2) < 0)
  52. return -1;
  53. }
  54. /* Get directory */
  55. p = strrchr(path2, '/');
  56. if (p)
  57. *p = 0;
  58. scnprintf(path, sizeof(path), "%s/tests/shell", path2);
  59. fd = open(path, O_PATH);
  60. if (fd >= 0)
  61. return fd;
  62. scnprintf(path, sizeof(path), "%s/source/tests/shell", path2);
  63. fd = open(path, O_PATH);
  64. if (fd >= 0)
  65. return fd;
  66. /* Then installed path. */
  67. exec_path = get_argv_exec_path();
  68. scnprintf(path, sizeof(path), "%s/tests/shell", exec_path);
  69. free(exec_path);
  70. return open(path, O_PATH);
  71. }
  72. static char *shell_test__description(int dir_fd, const char *name)
  73. {
  74. struct io io;
  75. char buf[128], desc[256];
  76. int ch, pos = 0;
  77. io__init(&io, openat(dir_fd, name, O_RDONLY), buf, sizeof(buf));
  78. if (io.fd < 0)
  79. return NULL;
  80. /* Skip first line - should be #!/bin/bash Shebang */
  81. if (io__get_char(&io) != '#')
  82. goto err_out;
  83. if (io__get_char(&io) != '!')
  84. goto err_out;
  85. do {
  86. ch = io__get_char(&io);
  87. if (ch < 0)
  88. goto err_out;
  89. } while (ch != '\n');
  90. do {
  91. ch = io__get_char(&io);
  92. if (ch < 0)
  93. goto err_out;
  94. } while (ch == '#' || isspace(ch));
  95. while (ch > 0 && ch != '\n') {
  96. desc[pos++] = ch;
  97. if (pos >= (int)sizeof(desc) - 1)
  98. break;
  99. ch = io__get_char(&io);
  100. }
  101. while (pos > 0 && isspace(desc[--pos]))
  102. ;
  103. desc[++pos] = '\0';
  104. close(io.fd);
  105. return strdup(desc);
  106. err_out:
  107. close(io.fd);
  108. return NULL;
  109. }
  110. /* Is this full file path a shell script */
  111. static bool is_shell_script(int dir_fd, const char *path)
  112. {
  113. const char *ext;
  114. ext = strrchr(path, '.');
  115. if (!ext)
  116. return false;
  117. if (!strcmp(ext, ".sh")) { /* Has .sh extension */
  118. if (faccessat(dir_fd, path, R_OK | X_OK, 0) == 0) /* Is executable */
  119. return true;
  120. }
  121. return false;
  122. }
  123. /* Is this file in this dir a shell script (for test purposes) */
  124. static bool is_test_script(int dir_fd, const char *name)
  125. {
  126. return is_shell_script(dir_fd, name);
  127. }
  128. /* Duplicate a string and fall over and die if we run out of memory */
  129. static char *strdup_check(const char *str)
  130. {
  131. char *newstr;
  132. newstr = strdup(str);
  133. if (!newstr) {
  134. pr_err("Out of memory while duplicating test script string\n");
  135. abort();
  136. }
  137. return newstr;
  138. }
  139. static int shell_test__run(struct test_suite *test, int subtest __maybe_unused)
  140. {
  141. const char *file = test->priv;
  142. int err;
  143. char *cmd = NULL;
  144. if (asprintf(&cmd, "%s%s", file, verbose ? " -v" : "") < 0)
  145. return TEST_FAIL;
  146. err = system(cmd);
  147. free(cmd);
  148. if (!err)
  149. return TEST_OK;
  150. return WEXITSTATUS(err) == 2 ? TEST_SKIP : TEST_FAIL;
  151. }
  152. static void append_script(int dir_fd, const char *name, char *desc,
  153. struct test_suite ***result,
  154. size_t *result_sz)
  155. {
  156. char filename[PATH_MAX], link[128];
  157. struct test_suite *test_suite, **result_tmp;
  158. struct test_case *tests;
  159. ssize_t len;
  160. char *exclusive;
  161. snprintf(link, sizeof(link), "/proc/%d/fd/%d", getpid(), dir_fd);
  162. len = readlink(link, filename, sizeof(filename));
  163. if (len < 0) {
  164. pr_err("Failed to readlink %s", link);
  165. return;
  166. }
  167. filename[len++] = '/';
  168. strcpy(&filename[len], name);
  169. tests = calloc(2, sizeof(*tests));
  170. if (!tests) {
  171. pr_err("Out of memory while building script test suite list\n");
  172. return;
  173. }
  174. tests[0].name = strdup_check(name);
  175. exclusive = strstr(desc, " (exclusive)");
  176. if (exclusive != NULL) {
  177. tests[0].exclusive = true;
  178. exclusive[0] = '\0';
  179. }
  180. tests[0].desc = strdup_check(desc);
  181. tests[0].run_case = shell_test__run;
  182. test_suite = zalloc(sizeof(*test_suite));
  183. if (!test_suite) {
  184. pr_err("Out of memory while building script test suite list\n");
  185. free(tests);
  186. return;
  187. }
  188. test_suite->desc = desc;
  189. test_suite->test_cases = tests;
  190. test_suite->priv = strdup_check(filename);
  191. /* Realloc is good enough, though we could realloc by chunks, not that
  192. * anyone will ever measure performance here */
  193. result_tmp = realloc(*result, (*result_sz + 1) * sizeof(*result_tmp));
  194. if (result_tmp == NULL) {
  195. pr_err("Out of memory while building script test suite list\n");
  196. free(tests);
  197. free(test_suite);
  198. return;
  199. }
  200. /* Add file to end and NULL terminate the struct array */
  201. *result = result_tmp;
  202. (*result)[*result_sz] = test_suite;
  203. (*result_sz)++;
  204. }
  205. static void append_scripts_in_dir(int dir_fd,
  206. struct test_suite ***result,
  207. size_t *result_sz)
  208. {
  209. struct dirent **entlist;
  210. struct dirent *ent;
  211. int n_dirs, i;
  212. /* List files, sorted by alpha */
  213. n_dirs = scandirat(dir_fd, ".", &entlist, NULL, alphasort);
  214. if (n_dirs == -1)
  215. return;
  216. for (i = 0; i < n_dirs && (ent = entlist[i]); i++) {
  217. int fd;
  218. if (ent->d_name[0] == '.')
  219. continue; /* Skip hidden files */
  220. if (is_test_script(dir_fd, ent->d_name)) { /* It's a test */
  221. char *desc = shell_test__description(dir_fd, ent->d_name);
  222. if (desc) /* It has a desc line - valid script */
  223. append_script(dir_fd, ent->d_name, desc, result, result_sz);
  224. continue;
  225. }
  226. if (ent->d_type != DT_DIR) {
  227. struct stat st;
  228. if (ent->d_type != DT_UNKNOWN)
  229. continue;
  230. fstatat(dir_fd, ent->d_name, &st, 0);
  231. if (!S_ISDIR(st.st_mode))
  232. continue;
  233. }
  234. if (strncmp(ent->d_name, "base_", 5) == 0)
  235. continue; /* Skip scripts that have a separate driver. */
  236. fd = openat(dir_fd, ent->d_name, O_PATH);
  237. append_scripts_in_dir(fd, result, result_sz);
  238. close(fd);
  239. }
  240. for (i = 0; i < n_dirs; i++) /* Clean up */
  241. zfree(&entlist[i]);
  242. free(entlist);
  243. }
  244. struct test_suite **create_script_test_suites(void)
  245. {
  246. struct test_suite **result = NULL, **result_tmp;
  247. size_t result_sz = 0;
  248. int dir_fd = shell_tests__dir_fd(); /* Walk dir */
  249. /*
  250. * Append scripts if fd is good, otherwise return a NULL terminated zero
  251. * length array.
  252. */
  253. if (dir_fd >= 0)
  254. append_scripts_in_dir(dir_fd, &result, &result_sz);
  255. result_tmp = realloc(result, (result_sz + 1) * sizeof(*result_tmp));
  256. if (result_tmp == NULL) {
  257. pr_err("Out of memory while building script test suite list\n");
  258. abort();
  259. }
  260. /* NULL terminate the test suite array. */
  261. result = result_tmp;
  262. result[result_sz] = NULL;
  263. if (dir_fd >= 0)
  264. close(dir_fd);
  265. return result;
  266. }