main.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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,
  83. "write(2) fails with the error ENXIO",
  84. buffer);
  85. }
  86. void test_case2(int nonblocking) {
  87. pid_t reader_pid;
  88. // Case 2: Reader exists but disconnects
  89. reader_pid = fork();
  90. if (reader_pid == 0) {
  91. // Child process acts as a reader
  92. int reader_fd = open(FIFO_PATH, O_RDONLY);
  93. if (reader_fd == -1) {
  94. perror("Reader failed to open FIFO");
  95. exit(EXIT_FAILURE);
  96. }
  97. sleep(1); // Simulate a brief existence of the reader
  98. close(reader_fd);
  99. exit(EXIT_SUCCESS);
  100. }
  101. sleep(5); // Ensure the reader has opened the FIFO
  102. FifoWriteResult result = test_fifo_write(1, nonblocking);
  103. waitpid(reader_pid, NULL, 0); // Wait for the reader process to exit
  104. if (nonblocking) {
  105. TEST_ASSERT(result.fd != -1 && result.error_code == EPIPE, 1, "Non-Blocking Write failed with EPIPE", "Non-Blocking Write failed with wrong error type");
  106. } else {
  107. TEST_ASSERT(result.fd != -1 && result.error_code == EPIPE, 1, "Blocking Write failed with EPIPE", "Blocking Write failed with wrong error type");
  108. }
  109. }
  110. void test_case3(int nonblocking) {
  111. pid_t reader_pid;
  112. // Case 3: Active reader exists
  113. reader_pid = fork();
  114. if (reader_pid == 0) {
  115. // Child process acts as a reader
  116. int reader_fd = open(FIFO_PATH, O_RDONLY);
  117. if (reader_fd == -1) {
  118. perror("Reader failed to open FIFO");
  119. exit(EXIT_FAILURE);
  120. }
  121. sleep(5); // Keep the reader active
  122. close(reader_fd);
  123. exit(EXIT_SUCCESS);
  124. }
  125. sleep(1); // Ensure the reader has opened the FIFO
  126. FifoWriteResult result = test_fifo_write(2, nonblocking);
  127. waitpid(reader_pid, NULL, 0); // Wait for the reader process to exit
  128. TEST_ASSERT(result.error_code, 0, "write succeed", "write failed");
  129. }
  130. void run_tests(int nonblocking) {
  131. for (int i = 0; i < 3; i++) {
  132. printf("\n--- Testing: %s (nonblocking=%d) ---\n", scenarios[i],
  133. nonblocking);
  134. switch (i) {
  135. case 0:
  136. test_case1(nonblocking);
  137. break;
  138. case 1:
  139. test_case2(nonblocking);
  140. break;
  141. case 2:
  142. test_case3(nonblocking);
  143. break;
  144. }
  145. }
  146. }
  147. int main() {
  148. // 设置 SIGPIPE 信号处理
  149. signal(SIGPIPE, sigpipe_handler);
  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. // 测试非阻塞模式下的三种情况
  159. printf("\n========== Testing Nonblocking Mode ==========\n");
  160. run_tests(1); // 非阻塞模式
  161. // 删除 FIFO
  162. unlink(FIFO_PATH);
  163. printf("\nAll tests completed.\n");
  164. return 0;
  165. }