PageRenderTime 54ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/runcommand/test-runcommand.c

https://gitlab.com/humodz/so2sh
C | 148 lines | 86 code | 43 blank | 19 comment | 11 complexity | 0c0fc0417ddac4da68d7b747abbcbc27 MD5 | raw file
  1. /* test-runcommand-v1 - Program to test runcommand function version 1. */
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <unistd.h>
  5. #include <sys/types.h>
  6. #include <sys/stat.h>
  7. #include <fcntl.h>
  8. #include <foo.h>
  9. #include <debug.h>
  10. #define LOGFILE "foo.log"
  11. #define EXECOK 0
  12. #define EXECFAIL 1
  13. int go_on = 1;
  14. void finish (void)
  15. {
  16. go_on = 0;
  17. }
  18. /* Run the = finish"program with arguments and report results. */
  19. int tryrun (char **args, int correct_exit_status, int correct_termination,
  20. int *io, int execresult, int mode)
  21. {
  22. int pid, nbytes, test, count;
  23. command_t command;
  24. /* Run the command.*/
  25. command.args = args;
  26. command.io = io;
  27. command.mode = mode;
  28. runcommand_onexit = finish;
  29. /*printf("tryrun mode %d\n",command.mode); */
  30. pid = runcommand (&command);
  31. if (pid==-2)
  32. {
  33. printf ("%s %s (pid %d) execution failed %n",
  34. args[0], args[1], pid, &nbytes);
  35. test = execresult == EXECFAIL ? 1 : 0;
  36. printf ("%*s\n", 80-nbytes, test ? "OK" : "NO");
  37. return test;
  38. }
  39. if (command.mode == NONBLOCK)
  40. {
  41. printf ("%s %s (pid %d) execution started asynchronously %n",
  42. args[0], args[1], pid, &nbytes);
  43. count =0;
  44. while (go_on)
  45. {
  46. sleep(1);
  47. count++;
  48. }
  49. test = (count == 2) ? 1 : 0;
  50. printf ("%*s\n", 80-nbytes, test ? "OK" : "NO");
  51. return test;
  52. }
  53. printf ("%s %s (pid %d) terminated %s and %s (status %d) %n",
  54. args[0], args[1], pid,
  55. command.termination == NORMAL_TERMINATION ? "normally" : "abnormally",
  56. command.exit_status == EXIT_SUCCESS ? "sucessfully" : "unsucessfully",
  57. command.exit_status, &nbytes);
  58. test = (command.exit_status == correct_exit_status)
  59. && (command.termination == correct_termination);
  60. printf ("%*s\n", 80-nbytes, test ? "OK" : "NO");
  61. return test;
  62. }
  63. /* Main function: returns successfully if all tests are ok;
  64. returns unsucessfully otherwise. */
  65. int main (int argc, char **argv)
  66. {
  67. int result, fd;
  68. /* int count; */
  69. /* Test cases. */
  70. char *args1[] = {"ls", "runcommand.c", NULL}; /* File does exist. */
  71. char *args2[] = {"ls", "nosuchfile", NULL}; /* File does not exist. */
  72. char *args3[] = {"./segfault", "", NULL}; /* Abnormal termination. */
  73. char *args4[] = {"./io", NULL}; /* Test IO redirection. */
  74. char *args5[] = {"./nosuchfile", NULL}; /* Exec failed. */
  75. char *args6[] = {"./delay", NULL}; /* Test nonblock. */
  76. int io[3], io2[3];
  77. result = EXIT_SUCCESS;
  78. /* Disable standard streams for convenience. */
  79. fatal ((io[0] = open ("/dev/null", O_WRONLY)) <0);
  80. fatal ((io[1] = open ("/dev/null", O_WRONLY)) <0);
  81. fatal ((io[2] = open ("/dev/null", O_WRONLY)) <0);
  82. result |=tryrun (args1, 0, NORMAL_TERMINATION, io, EXECOK, BLOCK); /* Normal, success. */
  83. result |=tryrun (args2, 2, NORMAL_TERMINATION, io, EXECOK, BLOCK); /* Normal, failire. */
  84. result |=tryrun (args3, EXIT_FAILURE, ABNORMAL_TERMINATION, io, EXECOK, BLOCK); /* Abnormal (success). */
  85. /* Test redirection. */
  86. fatal ((fd = open ("in.txt", O_CREAT | O_TRUNC | O_RDWR, S_IRUSR | S_IWUSR)) <0);
  87. write (fd, "a", 1);
  88. fatal(close(fd)<0);
  89. fatal ((io2[0] = open ("in.txt", O_RDONLY)) <0);
  90. fatal ((io2[1] = open ("out.txt", O_CREAT | O_TRUNC | O_RDWR, S_IRUSR | S_IWUSR)) <0);
  91. fatal ((io2[2] = open ("err.txt", O_CREAT | O_TRUNC | O_RDWR, S_IRUSR | S_IWUSR)) <0);
  92. result |=tryrun (args4, 0, NORMAL_TERMINATION, io2, EXECOK, BLOCK); /* Normal, success. */
  93. /* Test whether exec failed. */
  94. result |=tryrun (args5, 0, 0, io, EXECFAIL, BLOCK); /* Normal, success. */
  95. /* Test nonblock. */
  96. result |=tryrun (args6, 0, 0, io2, EXECFAIL, NONBLOCK); /* Normal, success. */
  97. /* count =0; */
  98. /* while (go_on) */
  99. /* { */
  100. /* sleep(1); */
  101. /* count++; */
  102. /* } */
  103. /* printf ("ok, count %d\n", count); */
  104. return result;
  105. }