main.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. #include <errno.h>
  2. #include <fcntl.h>
  3. #include <signal.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <sys/stat.h>
  8. #include <sys/wait.h>
  9. #include <unistd.h>
  10. #define TEST_ASSERT(left, right, success_msg, fail_msg) \
  11. do { \
  12. if ((left) == (right)) { \
  13. printf("[PASS] %s\n", success_msg); \
  14. } else { \
  15. printf("[FAIL] %s: Expected %d, but got %d\n", fail_msg, (right), \
  16. (left)); \
  17. } \
  18. } while (0)
  19. #define FIFO_PATH "/bin/test_fifo" // 使用 /tmp 目录避免权限问题
  20. typedef struct {
  21. int fd;
  22. int error_code;
  23. } FifoWriteResult;
  24. // 信号处理函数
  25. void sigpipe_handler(int signo) {
  26. if (signo == SIGPIPE) {
  27. printf("Received SIGPIPE signal. Write operation failed.\n");
  28. }
  29. }
  30. const char *scenarios[] = {"No readers (FIFO never had readers)",
  31. "Reader exists but disconnects",
  32. "Active reader exists"};
  33. FifoWriteResult test_fifo_write(int scenario_index, int nonblocking) {
  34. FifoWriteResult result = {.fd = -1, .error_code = 0};
  35. int fd;
  36. const char *data = "Hello, FIFO!";
  37. // Set write mode and non-blocking flag
  38. int flags = O_WRONLY;
  39. if (nonblocking) {
  40. flags |= O_NONBLOCK;
  41. }
  42. // Open the FIFO write end
  43. fd = open(FIFO_PATH, flags);
  44. if (fd == -1) {
  45. result.fd = fd;
  46. result.error_code = errno;
  47. if (errno == ENXIO) {
  48. printf("Result: Failed to open FIFO for writing (ENXIO: No readers).\n");
  49. } else {
  50. perror("Failed to open FIFO for writing");
  51. }
  52. return result; // Return early with error details
  53. }
  54. // Write data
  55. ssize_t bytes_written = write(fd, data, strlen(data));
  56. if (bytes_written == -1) {
  57. result.error_code = errno;
  58. if (bytes_written == -1) {
  59. if (errno == EPIPE) {
  60. printf("Result: Write failed with EPIPE (no readers available).\n");
  61. } else if (errno == ENXIO) {
  62. printf("Result: Write failed with ENXIO (FIFO never had readers).\n");
  63. } else if (errno == EAGAIN) {
  64. printf("Result: Write failed with EAGAIN (nonblocking write, pipe full "
  65. "or no readers).\n");
  66. } else {
  67. perror("Write failed with an unexpected error");
  68. }
  69. } else {
  70. printf("Result: Write succeeded. Bytes written: %zd\n", bytes_written);
  71. }
  72. result.fd = fd;
  73. close(fd);
  74. return result; // Return with fd and error_code
  75. }
  76. }
  77. void test_case1(int nonblocking) {
  78. // Case 1: No readers (FIFO never had readers)
  79. FifoWriteResult result = test_fifo_write(0, nonblocking);
  80. char buffer[100];
  81. sprintf(buffer, "Fail with unexpected error %d", result.error_code);
  82. TEST_ASSERT(result.error_code, ENXIO, "write(2) fails with the error ENXIO",
  83. buffer);
  84. }
  85. void test_case2(int nonblocking) {
  86. pid_t reader_pid;
  87. // Case 2: Reader exists but disconnects
  88. reader_pid = fork();
  89. if (reader_pid == 0) {
  90. // Child process acts as a reader
  91. int reader_fd = open(FIFO_PATH, O_RDONLY);
  92. if (reader_fd == -1) {
  93. perror("Reader failed to open FIFO");
  94. exit(EXIT_FAILURE);
  95. }
  96. sleep(2); // Simulate a brief existence of the reader
  97. close(reader_fd);
  98. exit(EXIT_SUCCESS);
  99. }
  100. sleep(5); // Ensure the reader has opened the FIFO
  101. FifoWriteResult result = test_fifo_write(1, nonblocking);
  102. waitpid(reader_pid, NULL, 0); // Wait for the reader process to exit
  103. if (nonblocking) {
  104. TEST_ASSERT(result.error_code, EPIPE,
  105. "Non-Blocking Write failed with EPIPE",
  106. "Non-Blocking Write failed with wrong error type");
  107. } else {
  108. TEST_ASSERT(result.error_code, EPIPE, "Blocking Write failed with EPIPE",
  109. "Blocking Write failed with wrong error type");
  110. }
  111. }
  112. void test_case3(int nonblocking) {
  113. pid_t reader_pid;
  114. // Case 3: Active reader exists
  115. reader_pid = fork();
  116. if (reader_pid == 0) {
  117. // Child process acts as a reader
  118. int reader_fd = open(FIFO_PATH, O_RDONLY);
  119. if (reader_fd == -1) {
  120. perror("Reader failed to open FIFO");
  121. exit(EXIT_FAILURE);
  122. }
  123. sleep(5); // Keep the reader active
  124. close(reader_fd);
  125. exit(EXIT_SUCCESS);
  126. }
  127. sleep(1); // Ensure the reader has opened the FIFO
  128. FifoWriteResult result = test_fifo_write(2, nonblocking);
  129. waitpid(reader_pid, NULL, 0); // Wait for the reader process to exit
  130. TEST_ASSERT(result.error_code, 0, "write succeed", "write failed");
  131. }
  132. void run_tests(int nonblocking) {
  133. for (int i = 0; i < 3; i++) {
  134. printf("\n--- Testing: %s (nonblocking=%d) ---\n", scenarios[i],
  135. nonblocking);
  136. switch (i) {
  137. case 0:
  138. // test_case1(nonblocking);
  139. break;
  140. case 1:
  141. test_case2(nonblocking);
  142. break;
  143. case 2:
  144. // test_case3(nonblocking);
  145. break;
  146. }
  147. }
  148. }
  149. void test_blocking() {
  150. // 创建 FIFO
  151. if (mkfifo(FIFO_PATH, 0666) == -1 && errno != EEXIST) {
  152. perror("mkfifo failed");
  153. exit(EXIT_FAILURE);
  154. }
  155. // 测试阻塞模式下的三种情况
  156. printf("========== Testing Blocking Mode ==========\n");
  157. run_tests(0); // 阻塞模式
  158. // 删除 FIFO
  159. unlink(FIFO_PATH);
  160. }
  161. void test_non_blocking() {
  162. // 创建 FIFO
  163. if (mkfifo(FIFO_PATH, 0666) == -1 && errno != EEXIST) {
  164. perror("mkfifo failed");
  165. exit(EXIT_FAILURE);
  166. }
  167. // 测试非阻塞模式下的三种情况
  168. printf("\n========== Testing Nonblocking Mode ==========\n");
  169. run_tests(1); // 非阻塞模式
  170. // 删除 FIFO
  171. unlink(FIFO_PATH);
  172. }
  173. int main() {
  174. // 设置 SIGPIPE 信号处理
  175. signal(SIGPIPE, sigpipe_handler);
  176. // test_blocking();
  177. test_non_blocking();
  178. printf("\nAll tests completed.\n");
  179. return 0;
  180. }