/apps/apitester/test_sctp_recvmsg.c

https://bitbucket.org/brucec/sctpdrv · C · 201 lines · 144 code · 27 blank · 30 comment · 55 complexity · 8b1aa2ba91704b0364362f96d61da457 MD5 · raw file

  1. /*-
  2. * Copyright (c) 2001-2007 by Cisco Systems, Inc. All rights reserved.
  3. * Copyright (c) 2001-2007, by Michael Tuexen, tuexen@fh-muenster.de. All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * a) Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. *
  11. * b) Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in
  13. * the documentation and/or other materials provided with the distribution.
  14. *
  15. * c) Neither the name of Cisco Systems, Inc. nor the names of its
  16. * contributors may be used to endorse or promote products derived
  17. * from this software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  21. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  22. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  23. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  24. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  25. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  26. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  27. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  28. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  29. * THE POSSIBILITY OF SUCH DAMAGE.
  30. */
  31. #include <winsock2.h>
  32. #include <mswsock.h>
  33. #include <WS2tcpip.h>
  34. #include <string.h>
  35. #include <stdio.h>
  36. #include <assert.h>
  37. #include <errno.h>
  38. #include <fcntl.h>
  39. #include <ws2sctp.h>
  40. #include "sctp_utilities.h"
  41. #include "api_tests.h"
  42. DEFINE_APITEST(sctp_recvmsg, rd_compl)
  43. {
  44. int fd[2], n;
  45. int flags;
  46. char buffer[10];
  47. if (sctp_socketpair(fd, 0) < 0)
  48. return strerror(errno);
  49. if (send(fd[0], "Hello", 5, 0) != 5) {
  50. closesocket(fd[0]);
  51. closesocket(fd[1]);
  52. return "sending failed";
  53. }
  54. flags = 0;
  55. n = sctp_recvmsg(fd[1], buffer, 10, NULL, 0, NULL, &flags);
  56. closesocket(fd[0]);
  57. closesocket(fd[1]);
  58. if (n < 0) {
  59. return strerror(errno);
  60. } else if (n != 5) {
  61. return "Wrong length";
  62. } else if (flags != MSG_EOR) {
  63. return "Wrong flags";
  64. } else {
  65. return NULL;
  66. }
  67. }
  68. DEFINE_APITEST(sctp_recvmsg, rd_incompl)
  69. {
  70. int fd[2], n;
  71. int flags;
  72. char buffer[10];
  73. if (sctp_socketpair(fd, 0) < 0)
  74. return strerror(errno);
  75. if (send(fd[0], "Hello", 5, 0) != 5) {
  76. closesocket(fd[0]);
  77. closesocket(fd[1]);
  78. return "sending failed";
  79. }
  80. flags = 0;
  81. n = sctp_recvmsg(fd[1], buffer, 1, NULL, 0, NULL, &flags);
  82. closesocket(fd[0]);
  83. closesocket(fd[1]);
  84. if (n < 0) {
  85. return strerror(errno);
  86. } else if (n != 1) {
  87. return "Wrong length";
  88. } else if (flags != 0) {
  89. return "Wrong flags";
  90. } else {
  91. return NULL;
  92. }
  93. }
  94. DEFINE_APITEST(sctp_recvmsg, msg_peek_in)
  95. {
  96. int fd[2], n;
  97. int flags;
  98. char buffer[10];
  99. if (sctp_socketpair(fd, 0) < 0)
  100. return strerror(errno);
  101. if (send(fd[0], "Hello", 5, 0) != 5) {
  102. closesocket(fd[0]);
  103. closesocket(fd[1]);
  104. return "sending failed";
  105. }
  106. flags = MSG_PEEK;
  107. n = sctp_recvmsg(fd[1], buffer, 1, NULL, 0, NULL, &flags);
  108. closesocket(fd[0]);
  109. closesocket(fd[1]);
  110. if (n < 0) {
  111. return strerror(errno);
  112. } else if (n != 1) {
  113. return "Wrong length";
  114. } else if (flags != 0) {
  115. printf("flags = %x", flags);
  116. return "Wrong flags";
  117. } else {
  118. return NULL;
  119. }
  120. }
  121. DEFINE_APITEST(sctp_recvmsg, msg_eor_in)
  122. {
  123. int fd[2], n;
  124. int flags;
  125. char buffer[10];
  126. if (sctp_socketpair(fd, 0) < 0)
  127. return strerror(errno);
  128. if (send(fd[0], "Hello", 5, 0) != 5) {
  129. closesocket(fd[0]);
  130. closesocket(fd[1]);
  131. return "sending failed";
  132. }
  133. flags = MSG_EOR;
  134. n = sctp_recvmsg(fd[1], buffer, 1, NULL, 0, NULL, &flags);
  135. closesocket(fd[0]);
  136. closesocket(fd[1]);
  137. if (n < 0) {
  138. return strerror(errno);
  139. } else if (n != 1) {
  140. return "Wrong length";
  141. } else if (flags != 0) {
  142. printf("flags = %x", flags);
  143. return "Wrong flags";
  144. } else {
  145. return NULL;
  146. }
  147. }
  148. DEFINE_APITEST(sctp_recvmsg, msg_notification_in)
  149. {
  150. int fd[2], n;
  151. int flags;
  152. char buffer[10];
  153. if (sctp_socketpair(fd, 0) < 0)
  154. return strerror(errno);
  155. if (send(fd[0], "Hello", 5, 0) != 5) {
  156. closesocket(fd[0]);
  157. closesocket(fd[1]);
  158. return "sending failed";
  159. }
  160. flags = MSG_NOTIFICATION;
  161. n = sctp_recvmsg(fd[1], buffer, 1, NULL, 0, NULL, &flags);
  162. closesocket(fd[0]);
  163. closesocket(fd[1]);
  164. if (n < 0) {
  165. return strerror(errno);
  166. } else if (n != 1) {
  167. return "Wrong length";
  168. } else if (flags != 0) {
  169. printf("flags = %x", flags);
  170. return "Wrong flags";
  171. } else {
  172. return NULL;
  173. }
  174. }