2
0

testsockopt.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /* Licensed to the Apache Software Foundation (ASF) under one or more
  2. * contributor license agreements. See the NOTICE file distributed with
  3. * this work for additional information regarding copyright ownership.
  4. * The ASF licenses this file to You under the Apache License, Version 2.0
  5. * (the "License"); you may not use this file except in compliance with
  6. * the License. You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "fspr_network_io.h"
  17. #include "fspr_errno.h"
  18. #include "fspr_general.h"
  19. #include "fspr_lib.h"
  20. #include "testutil.h"
  21. static fspr_socket_t *sock = NULL;
  22. static void create_socket(abts_case *tc, void *data)
  23. {
  24. fspr_status_t rv;
  25. rv = fspr_socket_create(&sock, APR_INET, SOCK_STREAM, 0, p);
  26. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  27. ABTS_PTR_NOTNULL(tc, sock);
  28. }
  29. static void set_keepalive(abts_case *tc, void *data)
  30. {
  31. fspr_status_t rv;
  32. fspr_int32_t ck;
  33. rv = fspr_socket_opt_set(sock, APR_SO_KEEPALIVE, 1);
  34. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  35. rv = fspr_socket_opt_get(sock, APR_SO_KEEPALIVE, &ck);
  36. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  37. ABTS_INT_EQUAL(tc, 1, ck);
  38. }
  39. static void set_debug(abts_case *tc, void *data)
  40. {
  41. fspr_status_t rv1, rv2;
  42. fspr_int32_t ck;
  43. /* On some platforms APR_SO_DEBUG can only be set as root; just test
  44. * for get/set consistency of this option. */
  45. rv1 = fspr_socket_opt_set(sock, APR_SO_DEBUG, 1);
  46. rv2 = fspr_socket_opt_get(sock, APR_SO_DEBUG, &ck);
  47. APR_ASSERT_SUCCESS(tc, "get SO_DEBUG option", rv2);
  48. if (rv1 == APR_SUCCESS) {
  49. ABTS_INT_EQUAL(tc, 1, ck);
  50. } else {
  51. ABTS_INT_EQUAL(tc, 0, ck);
  52. }
  53. }
  54. static void remove_keepalive(abts_case *tc, void *data)
  55. {
  56. fspr_status_t rv;
  57. fspr_int32_t ck;
  58. rv = fspr_socket_opt_get(sock, APR_SO_KEEPALIVE, &ck);
  59. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  60. ABTS_INT_EQUAL(tc, 1, ck);
  61. rv = fspr_socket_opt_set(sock, APR_SO_KEEPALIVE, 0);
  62. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  63. rv = fspr_socket_opt_get(sock, APR_SO_KEEPALIVE, &ck);
  64. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  65. ABTS_INT_EQUAL(tc, 0, ck);
  66. }
  67. static void corkable(abts_case *tc, void *data)
  68. {
  69. #if !APR_HAVE_CORKABLE_TCP
  70. ABTS_NOT_IMPL(tc, "TCP isn't corkable");
  71. #else
  72. fspr_status_t rv;
  73. fspr_int32_t ck;
  74. rv = fspr_socket_opt_set(sock, APR_TCP_NODELAY, 1);
  75. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  76. rv = fspr_socket_opt_get(sock, APR_TCP_NODELAY, &ck);
  77. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  78. ABTS_INT_EQUAL(tc, 1, ck);
  79. rv = fspr_socket_opt_set(sock, APR_TCP_NOPUSH, 1);
  80. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  81. rv = fspr_socket_opt_get(sock, APR_TCP_NOPUSH, &ck);
  82. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  83. ABTS_INT_EQUAL(tc, 1, ck);
  84. rv = fspr_socket_opt_get(sock, APR_TCP_NODELAY, &ck);
  85. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  86. /* TCP_NODELAY is now in an unknown state; it may be zero if
  87. * TCP_NOPUSH and TCP_NODELAY are mutually exclusive on this
  88. * platform, e.g. Linux < 2.6. */
  89. rv = fspr_socket_opt_set(sock, APR_TCP_NOPUSH, 0);
  90. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  91. rv = fspr_socket_opt_get(sock, APR_TCP_NODELAY, &ck);
  92. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  93. ABTS_INT_EQUAL(tc, 1, ck);
  94. #endif
  95. }
  96. static void close_socket(abts_case *tc, void *data)
  97. {
  98. fspr_status_t rv;
  99. rv = fspr_socket_close(sock);
  100. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  101. }
  102. abts_suite *testsockopt(abts_suite *suite)
  103. {
  104. suite = ADD_SUITE(suite)
  105. abts_run_test(suite, create_socket, NULL);
  106. abts_run_test(suite, set_keepalive, NULL);
  107. abts_run_test(suite, set_debug, NULL);
  108. abts_run_test(suite, remove_keepalive, NULL);
  109. abts_run_test(suite, corkable, NULL);
  110. abts_run_test(suite, close_socket, NULL);
  111. return suite;
  112. }