run-03-readpartial.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. #include <ccan/io/io.h>
  2. /* Include the C files directly. */
  3. #include <ccan/io/poll.c>
  4. #include <ccan/io/io.c>
  5. #include <ccan/tap/tap.h>
  6. #include <sys/wait.h>
  7. #include <stdio.h>
  8. #ifndef PORT
  9. #define PORT "65003"
  10. #endif
  11. struct data {
  12. int state;
  13. size_t bytes;
  14. char buf[4];
  15. };
  16. static void finish_ok(struct io_conn *conn, struct data *d)
  17. {
  18. ok1(d->state == 1);
  19. d->state++;
  20. io_break(d);
  21. }
  22. static struct io_plan *init_conn(struct io_conn *conn, struct data *d)
  23. {
  24. ok1(d->state == 0);
  25. d->state++;
  26. io_set_finish(conn, finish_ok, d);
  27. return io_read_partial(conn, d->buf, sizeof(d->buf), &d->bytes,
  28. io_close_cb, d);
  29. }
  30. static int make_listen_fd(const char *port, struct addrinfo **info)
  31. {
  32. int fd, on = 1;
  33. struct addrinfo *addrinfo, hints;
  34. memset(&hints, 0, sizeof(hints));
  35. hints.ai_family = AF_UNSPEC;
  36. hints.ai_socktype = SOCK_STREAM;
  37. hints.ai_flags = AI_PASSIVE;
  38. hints.ai_protocol = 0;
  39. if (getaddrinfo(NULL, port, &hints, &addrinfo) != 0)
  40. return -1;
  41. fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
  42. addrinfo->ai_protocol);
  43. if (fd < 0)
  44. return -1;
  45. setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
  46. if (bind(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0) {
  47. close(fd);
  48. return -1;
  49. }
  50. if (listen(fd, 1) != 0) {
  51. close(fd);
  52. return -1;
  53. }
  54. *info = addrinfo;
  55. return fd;
  56. }
  57. static void write_to_socket(const char *str, const struct addrinfo *addrinfo)
  58. {
  59. int fd, i;
  60. fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
  61. addrinfo->ai_protocol);
  62. if (fd < 0)
  63. exit(1);
  64. if (connect(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0)
  65. exit(2);
  66. signal(SIGPIPE, SIG_IGN);
  67. for (i = 0; i < strlen(str); i++) {
  68. if (write(fd, str + i, 1) != 1)
  69. break;
  70. }
  71. close(fd);
  72. }
  73. int main(void)
  74. {
  75. struct data *d = malloc(sizeof(*d));
  76. struct addrinfo *addrinfo;
  77. struct io_listener *l;
  78. int fd, status;
  79. /* This is how many tests you plan to run */
  80. plan_tests(22);
  81. d->state = 0;
  82. fd = make_listen_fd(PORT, &addrinfo);
  83. ok1(fd >= 0);
  84. l = io_new_listener(NULL, fd, init_conn, d);
  85. ok1(l);
  86. fflush(stdout);
  87. if (!fork()) {
  88. io_close_listener(l);
  89. write_to_socket("hellothere", addrinfo);
  90. freeaddrinfo(addrinfo);
  91. free(d);
  92. exit(0);
  93. }
  94. ok1(io_loop() == d);
  95. ok1(d->state == 2);
  96. ok1(d->bytes > 0);
  97. ok1(d->bytes <= sizeof(d->buf));
  98. ok1(memcmp(d->buf, "hellothere", d->bytes) == 0);
  99. ok1(wait(&status));
  100. ok1(WIFEXITED(status));
  101. ok1(WEXITSTATUS(status) == 0);
  102. fflush(stdout);
  103. if (!fork()) {
  104. io_close_listener(l);
  105. write_to_socket("hi", addrinfo);
  106. freeaddrinfo(addrinfo);
  107. free(d);
  108. exit(0);
  109. }
  110. d->state = 0;
  111. ok1(io_loop() == d);
  112. ok1(d->state == 2);
  113. ok1(d->bytes > 0);
  114. ok1(d->bytes <= strlen("hi"));
  115. ok1(memcmp(d->buf, "hi", d->bytes) == 0);
  116. freeaddrinfo(addrinfo);
  117. free(d);
  118. io_close_listener(l);
  119. ok1(wait(&status));
  120. ok1(WIFEXITED(status));
  121. ok1(WEXITSTATUS(status) == 0);
  122. /* This exits depending on whether all tests passed */
  123. return exit_status();
  124. }