t-command.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. * libdpkg - Debian packaging suite library routines
  3. * t-command.c - test command implementation
  4. *
  5. * Copyright © 2010 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 <http://www.gnu.org/licenses/>.
  19. */
  20. #include <config.h>
  21. #include <compat.h>
  22. #include <dpkg/test.h>
  23. #include <dpkg/subproc.h>
  24. #include <dpkg/command.h>
  25. static void
  26. test_command_init(void)
  27. {
  28. struct command cmd;
  29. command_init(&cmd, "/absolute/path/to/progname", NULL);
  30. test_str(cmd.filename, ==, "/absolute/path/to/progname");
  31. test_str(cmd.name, ==, "progname");
  32. test_pass(cmd.argc == 0);
  33. test_pass(cmd.argv[0] == NULL);
  34. command_destroy(&cmd);
  35. test_pass(cmd.filename == NULL);
  36. test_pass(cmd.name == NULL);
  37. test_pass(cmd.argc == 0);
  38. test_pass(cmd.argv == NULL);
  39. command_init(&cmd, "progname", NULL);
  40. test_str(cmd.filename, ==, "progname");
  41. test_str(cmd.name, ==, "progname");
  42. test_pass(cmd.argc == 0);
  43. test_pass(cmd.argv[0] == NULL);
  44. command_destroy(&cmd);
  45. command_init(&cmd, "progname", "description");
  46. test_str(cmd.filename, ==, "progname");
  47. test_str(cmd.name, ==, "description");
  48. test_pass(cmd.argc == 0);
  49. test_pass(cmd.argv[0] == NULL);
  50. command_destroy(&cmd);
  51. }
  52. static void
  53. test_command_add_arg(void)
  54. {
  55. struct command cmd;
  56. command_init(&cmd, "test", NULL);
  57. command_add_arg(&cmd, "arg 0");
  58. test_pass(cmd.argc == 1);
  59. test_str(cmd.argv[0], ==, "arg 0");
  60. test_pass(cmd.argv[1] == NULL);
  61. command_add_arg(&cmd, "arg 1");
  62. test_pass(cmd.argc == 2);
  63. test_str(cmd.argv[0], ==, "arg 0");
  64. test_str(cmd.argv[1], ==, "arg 1");
  65. test_pass(cmd.argv[2] == NULL);
  66. command_add_arg(&cmd, "arg 2");
  67. test_pass(cmd.argc == 3);
  68. test_str(cmd.argv[0], ==, "arg 0");
  69. test_str(cmd.argv[1], ==, "arg 1");
  70. test_str(cmd.argv[2], ==, "arg 2");
  71. test_pass(cmd.argv[3] == NULL);
  72. command_destroy(&cmd);
  73. }
  74. static void
  75. test_command_add_argl(void)
  76. {
  77. struct command cmd;
  78. const char *args[] = {
  79. "arg 1",
  80. "arg 2",
  81. "arg 3",
  82. NULL,
  83. };
  84. command_init(&cmd, "test", NULL);
  85. command_add_arg(&cmd, "arg 0");
  86. command_add_argl(&cmd, args);
  87. test_pass(cmd.argc == 4);
  88. test_str(cmd.argv[0], ==, "arg 0");
  89. test_str(cmd.argv[1], ==, "arg 1");
  90. test_str(cmd.argv[2], ==, "arg 2");
  91. test_str(cmd.argv[3], ==, "arg 3");
  92. test_pass(cmd.argv[4] == NULL);
  93. command_destroy(&cmd);
  94. }
  95. static void
  96. test_command_add_args(void)
  97. {
  98. struct command cmd;
  99. command_init(&cmd, "test", NULL);
  100. command_add_arg(&cmd, "arg 0");
  101. command_add_args(&cmd, "arg 1", "arg 2", "arg 3", NULL);
  102. test_pass(cmd.argc == 4);
  103. test_str(cmd.argv[0], ==, "arg 0");
  104. test_str(cmd.argv[1], ==, "arg 1");
  105. test_str(cmd.argv[2], ==, "arg 2");
  106. test_str(cmd.argv[3], ==, "arg 3");
  107. test_pass(cmd.argv[4] == NULL);
  108. command_destroy(&cmd);
  109. }
  110. static void
  111. test_command_exec(void)
  112. {
  113. struct command cmd;
  114. pid_t pid;
  115. command_init(&cmd, "/bin/true", "exec test");
  116. command_add_arg(&cmd, "arg 0");
  117. command_add_arg(&cmd, "arg 1");
  118. pid = subproc_fork();
  119. if (pid == 0)
  120. command_exec(&cmd);
  121. subproc_wait_check(pid, "command exec test", 0);
  122. }
  123. static void
  124. test(void)
  125. {
  126. test_command_init();
  127. test_command_add_arg();
  128. test_command_add_argl();
  129. test_command_add_args();
  130. test_command_exec();
  131. }