tests-scripts.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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/sh 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. size_t len;
  160. snprintf(link, sizeof(link), "/proc/%d/fd/%d", getpid(), dir_fd);
  161. len = readlink(link, filename, sizeof(filename));
  162. if (len < 0) {
  163. pr_err("Failed to readlink %s", link);
  164. return;
  165. }
  166. filename[len++] = '/';
  167. strcpy(&filename[len], name);
  168. tests = calloc(2, sizeof(*tests));
  169. if (!tests) {
  170. pr_err("Out of memory while building script test suite list\n");
  171. return;
  172. }
  173. tests[0].name = strdup_check(name);
  174. tests[0].desc = strdup_check(desc);
  175. tests[0].run_case = shell_test__run;
  176. test_suite = zalloc(sizeof(*test_suite));
  177. if (!test_suite) {
  178. pr_err("Out of memory while building script test suite list\n");
  179. free(tests);
  180. return;
  181. }
  182. test_suite->desc = desc;
  183. test_suite->test_cases = tests;
  184. test_suite->priv = strdup_check(filename);
  185. /* Realloc is good enough, though we could realloc by chunks, not that
  186. * anyone will ever measure performance here */
  187. result_tmp = realloc(*result, (*result_sz + 1) * sizeof(*result_tmp));
  188. if (result_tmp == NULL) {
  189. pr_err("Out of memory while building script test suite list\n");
  190. free(tests);
  191. free(test_suite);
  192. return;
  193. }
  194. /* Add file to end and NULL terminate the struct array */
  195. *result = result_tmp;
  196. (*result)[*result_sz] = test_suite;
  197. (*result_sz)++;
  198. }
  199. static void append_scripts_in_dir(int dir_fd,
  200. struct test_suite ***result,
  201. size_t *result_sz)
  202. {
  203. struct dirent **entlist;
  204. struct dirent *ent;
  205. int n_dirs, i;
  206. /* List files, sorted by alpha */
  207. n_dirs = scandirat(dir_fd, ".", &entlist, NULL, alphasort);
  208. if (n_dirs == -1)
  209. return;
  210. for (i = 0; i < n_dirs && (ent = entlist[i]); i++) {
  211. int fd;
  212. if (ent->d_name[0] == '.')
  213. continue; /* Skip hidden files */
  214. if (is_test_script(dir_fd, ent->d_name)) { /* It's a test */
  215. char *desc = shell_test__description(dir_fd, ent->d_name);
  216. if (desc) /* It has a desc line - valid script */
  217. append_script(dir_fd, ent->d_name, desc, result, result_sz);
  218. continue;
  219. }
  220. if (ent->d_type != DT_DIR) {
  221. struct stat st;
  222. if (ent->d_type != DT_UNKNOWN)
  223. continue;
  224. fstatat(dir_fd, ent->d_name, &st, 0);
  225. if (!S_ISDIR(st.st_mode))
  226. continue;
  227. }
  228. if (strncmp(ent->d_name, "base_", 5) == 0)
  229. continue; /* Skip scripts that have a separate driver. */
  230. fd = openat(dir_fd, ent->d_name, O_PATH);
  231. append_scripts_in_dir(fd, result, result_sz);
  232. close(fd);
  233. }
  234. for (i = 0; i < n_dirs; i++) /* Clean up */
  235. zfree(&entlist[i]);
  236. free(entlist);
  237. }
  238. struct test_suite **create_script_test_suites(void)
  239. {
  240. struct test_suite **result = NULL, **result_tmp;
  241. size_t result_sz = 0;
  242. int dir_fd = shell_tests__dir_fd(); /* Walk dir */
  243. /*
  244. * Append scripts if fd is good, otherwise return a NULL terminated zero
  245. * length array.
  246. */
  247. if (dir_fd >= 0)
  248. append_scripts_in_dir(dir_fd, &result, &result_sz);
  249. result_tmp = realloc(result, (result_sz + 1) * sizeof(*result_tmp));
  250. if (result_tmp == NULL) {
  251. pr_err("Out of memory while building script test suite list\n");
  252. abort();
  253. }
  254. /* NULL terminate the test suite array. */
  255. result = result_tmp;
  256. result[result_sz] = NULL;
  257. if (dir_fd >= 0)
  258. close(dir_fd);
  259. return result;
  260. }