t-command.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /*
  2. * libdpkg - Debian packaging suite library routines
  3. * t-command.c - test command implementation
  4. *
  5. * Copyright © 2010-2012 Guillem Jover <guillem@debian.org>
  6. *
  7. * This is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  19. */
  20. #include <config.h>
  21. #include <compat.h>
  22. #include <sys/types.h>
  23. #include <sys/stat.h>
  24. #include <fcntl.h>
  25. #include <stdlib.h>
  26. #include <unistd.h>
  27. #include <dpkg/test.h>
  28. #include <dpkg/subproc.h>
  29. #include <dpkg/command.h>
  30. #include <dpkg/dpkg.h>
  31. static void
  32. test_command_init(void)
  33. {
  34. struct command cmd;
  35. command_init(&cmd, "/absolute/path/to/progname", NULL);
  36. test_str(cmd.filename, ==, "/absolute/path/to/progname");
  37. test_str(cmd.name, ==, "progname");
  38. test_pass(cmd.argc == 0);
  39. test_pass(cmd.argv[0] == NULL);
  40. command_destroy(&cmd);
  41. test_pass(cmd.filename == NULL);
  42. test_pass(cmd.name == NULL);
  43. test_pass(cmd.argc == 0);
  44. test_pass(cmd.argv == NULL);
  45. command_init(&cmd, "progname", NULL);
  46. test_str(cmd.filename, ==, "progname");
  47. test_str(cmd.name, ==, "progname");
  48. test_pass(cmd.argc == 0);
  49. test_pass(cmd.argv[0] == NULL);
  50. command_destroy(&cmd);
  51. command_init(&cmd, "progname", "description");
  52. test_str(cmd.filename, ==, "progname");
  53. test_str(cmd.name, ==, "description");
  54. test_pass(cmd.argc == 0);
  55. test_pass(cmd.argv[0] == NULL);
  56. command_destroy(&cmd);
  57. }
  58. static void
  59. test_command_grow_argv(void)
  60. {
  61. struct command cmd;
  62. int argv_size, i;
  63. command_init(&cmd, "test", NULL);
  64. argv_size = cmd.argv_size + 4;
  65. for (i = 0; i < argv_size; i++)
  66. command_add_arg(&cmd, "arg");
  67. test_pass(cmd.argc == argv_size);
  68. test_pass(cmd.argv_size >= argv_size);
  69. test_str(cmd.argv[0], ==, "arg");
  70. test_str(cmd.argv[argv_size - 1], ==, "arg");
  71. test_pass(cmd.argv[argv_size] == NULL);
  72. command_destroy(&cmd);
  73. }
  74. static void
  75. test_command_add_arg(void)
  76. {
  77. struct command cmd;
  78. command_init(&cmd, "test", NULL);
  79. command_add_arg(&cmd, "arg 0");
  80. test_pass(cmd.argc == 1);
  81. test_str(cmd.argv[0], ==, "arg 0");
  82. test_pass(cmd.argv[1] == NULL);
  83. command_add_arg(&cmd, "arg 1");
  84. test_pass(cmd.argc == 2);
  85. test_str(cmd.argv[0], ==, "arg 0");
  86. test_str(cmd.argv[1], ==, "arg 1");
  87. test_pass(cmd.argv[2] == NULL);
  88. command_add_arg(&cmd, "arg 2");
  89. test_pass(cmd.argc == 3);
  90. test_str(cmd.argv[0], ==, "arg 0");
  91. test_str(cmd.argv[1], ==, "arg 1");
  92. test_str(cmd.argv[2], ==, "arg 2");
  93. test_pass(cmd.argv[3] == NULL);
  94. command_destroy(&cmd);
  95. }
  96. static void
  97. test_command_add_argl(void)
  98. {
  99. struct command cmd;
  100. const char *args[] = {
  101. "arg 1",
  102. "arg 2",
  103. "arg 3",
  104. NULL,
  105. };
  106. command_init(&cmd, "test", NULL);
  107. command_add_arg(&cmd, "arg 0");
  108. command_add_argl(&cmd, args);
  109. test_pass(cmd.argc == 4);
  110. test_str(cmd.argv[0], ==, "arg 0");
  111. test_str(cmd.argv[1], ==, "arg 1");
  112. test_str(cmd.argv[2], ==, "arg 2");
  113. test_str(cmd.argv[3], ==, "arg 3");
  114. test_pass(cmd.argv[4] == NULL);
  115. command_destroy(&cmd);
  116. }
  117. static void
  118. test_command_add_args(void)
  119. {
  120. struct command cmd;
  121. command_init(&cmd, "test", NULL);
  122. command_add_arg(&cmd, "arg 0");
  123. command_add_args(&cmd, "arg 1", "arg 2", "arg 3", NULL);
  124. test_pass(cmd.argc == 4);
  125. test_str(cmd.argv[0], ==, "arg 0");
  126. test_str(cmd.argv[1], ==, "arg 1");
  127. test_str(cmd.argv[2], ==, "arg 2");
  128. test_str(cmd.argv[3], ==, "arg 3");
  129. test_pass(cmd.argv[4] == NULL);
  130. command_destroy(&cmd);
  131. }
  132. static void
  133. test_command_exec(void)
  134. {
  135. struct command cmd;
  136. pid_t pid;
  137. int ret;
  138. command_init(&cmd, "true", "exec test");
  139. command_add_arg(&cmd, "arg 0");
  140. command_add_arg(&cmd, "arg 1");
  141. pid = subproc_fork();
  142. if (pid == 0)
  143. command_exec(&cmd);
  144. ret = subproc_wait_check(pid, "command exec test", 0);
  145. test_pass(ret == 0);
  146. }
  147. static void
  148. test_command_shell(void)
  149. {
  150. pid_t pid;
  151. int ret;
  152. pid = subproc_fork();
  153. if (pid == 0)
  154. command_shell("true", "command shell pass test");
  155. ret = subproc_wait_check(pid, "command shell pass test", 0);
  156. test_pass(ret == 0);
  157. pid = subproc_fork();
  158. if (pid == 0)
  159. command_shell("false", "command shell fail test");
  160. ret = subproc_wait_check(pid, "command shell fail test", PROCNOERR);
  161. test_fail(ret == 0);
  162. unsetenv("SHELL");
  163. pid = subproc_fork();
  164. if (pid == 0)
  165. command_shell("true", "command default shell test");
  166. ret = subproc_wait_check(pid, "command default shell test", 0);
  167. test_pass(ret == 0);
  168. }
  169. static void
  170. test_dup_file(int fd, const char *filename, int flags)
  171. {
  172. int newfd;
  173. newfd = open(filename, flags);
  174. dup2(newfd, fd);
  175. close(newfd);
  176. }
  177. static void
  178. test_command_pager(void)
  179. {
  180. const char *pager, *default_pager;
  181. int origfd = dup(STDOUT_FILENO);
  182. /* Test stdout being a tty. */
  183. test_todo_block("environment might not expose controlling terminal") {
  184. test_dup_file(STDOUT_FILENO, "/dev/tty", O_WRONLY);
  185. setenv("PAGER", "test-pager", 1);
  186. pager = command_get_pager();
  187. unsetenv("PAGER");
  188. default_pager = command_get_pager();
  189. dup2(origfd, STDOUT_FILENO);
  190. test_str(pager, ==, "test-pager");
  191. test_str(default_pager, ==, DEFAULTPAGER);
  192. }
  193. /* Test stdout not being a tty. */
  194. test_dup_file(STDOUT_FILENO, "/dev/null", O_WRONLY);
  195. pager = command_get_pager();
  196. dup2(origfd, STDOUT_FILENO);
  197. test_str(pager, ==, CAT);
  198. }
  199. static void
  200. test(void)
  201. {
  202. test_plan(52);
  203. test_command_init();
  204. test_command_grow_argv();
  205. test_command_add_arg();
  206. test_command_add_argl();
  207. test_command_add_args();
  208. test_command_exec();
  209. test_command_shell();
  210. test_command_pager();
  211. }