testipsub.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 "testutil.h"
  17. #include "fspr_general.h"
  18. #include "fspr_network_io.h"
  19. #include "fspr_errno.h"
  20. static void test_bad_input(abts_case *tc, void *data)
  21. {
  22. struct {
  23. const char *ipstr;
  24. const char *mask;
  25. fspr_status_t expected_rv;
  26. } testcases[] =
  27. {
  28. /* so we have a few good inputs in here; sue me */
  29. {"my.host.name", NULL, APR_EINVAL}
  30. ,{"127.0.0.256", NULL, APR_EBADIP}
  31. ,{"127.0.0.1", NULL, APR_SUCCESS}
  32. ,{"127.0.0.1", "32", APR_SUCCESS}
  33. ,{"127.0.0.1", "1", APR_SUCCESS}
  34. ,{"127.0.0.1", "15", APR_SUCCESS}
  35. ,{"127.0.0.1", "-1", APR_EBADMASK}
  36. ,{"127.0.0.1", "0", APR_EBADMASK}
  37. ,{"127.0.0.1", "33", APR_EBADMASK}
  38. ,{"127.0.0.1", "255.0.0.0", APR_SUCCESS}
  39. ,{"127.0.0.1", "255.0", APR_EBADMASK}
  40. ,{"127.0.0.1", "255.255.256.0", APR_EBADMASK}
  41. ,{"127.0.0.1", "abc", APR_EBADMASK}
  42. ,{"127", NULL, APR_SUCCESS}
  43. ,{"127.0.0.1.2", NULL, APR_EBADIP}
  44. ,{"127.0.0.1.2", "8", APR_EBADIP}
  45. ,{"127", "255.0.0.0", APR_EBADIP} /* either EBADIP or EBADMASK seems fine */
  46. #if APR_HAVE_IPV6
  47. ,{"::1", NULL, APR_SUCCESS}
  48. ,{"::1", "20", APR_SUCCESS}
  49. ,{"::ffff:9.67.113.15", NULL, APR_EBADIP} /* yes, this is goodness */
  50. ,{"fe80::", "16", APR_SUCCESS}
  51. ,{"fe80::", "255.0.0.0", APR_EBADMASK}
  52. ,{"fe80::1", "0", APR_EBADMASK}
  53. ,{"fe80::1", "-1", APR_EBADMASK}
  54. ,{"fe80::1", "1", APR_SUCCESS}
  55. ,{"fe80::1", "33", APR_SUCCESS}
  56. ,{"fe80::1", "128", APR_SUCCESS}
  57. ,{"fe80::1", "129", APR_EBADMASK}
  58. #else
  59. /* do some IPv6 stuff and verify that it fails with APR_EBADIP */
  60. ,{"::ffff:9.67.113.15", NULL, APR_EBADIP}
  61. #endif
  62. };
  63. int i;
  64. fspr_ipsubnet_t *ipsub;
  65. fspr_status_t rv;
  66. for (i = 0; i < (sizeof testcases / sizeof testcases[0]); i++) {
  67. rv = fspr_ipsubnet_create(&ipsub, testcases[i].ipstr, testcases[i].mask, p);
  68. ABTS_INT_EQUAL(tc, rv, testcases[i].expected_rv);
  69. }
  70. }
  71. static void test_singleton_subnets(abts_case *tc, void *data)
  72. {
  73. const char *v4addrs[] = {
  74. "127.0.0.1", "129.42.18.99", "63.161.155.20", "207.46.230.229", "64.208.42.36",
  75. "198.144.203.195", "192.18.97.241", "198.137.240.91", "62.156.179.119",
  76. "204.177.92.181"
  77. };
  78. fspr_ipsubnet_t *ipsub;
  79. fspr_sockaddr_t *sa;
  80. fspr_status_t rv;
  81. int i, j, rc;
  82. for (i = 0; i < sizeof v4addrs / sizeof v4addrs[0]; i++) {
  83. rv = fspr_ipsubnet_create(&ipsub, v4addrs[i], NULL, p);
  84. ABTS_TRUE(tc, rv == APR_SUCCESS);
  85. for (j = 0; j < sizeof v4addrs / sizeof v4addrs[0]; j++) {
  86. rv = fspr_sockaddr_info_get(&sa, v4addrs[j], APR_INET, 0, 0, p);
  87. ABTS_TRUE(tc, rv == APR_SUCCESS);
  88. rc = fspr_ipsubnet_test(ipsub, sa);
  89. if (!strcmp(v4addrs[i], v4addrs[j])) {
  90. ABTS_TRUE(tc, rc != 0);
  91. }
  92. else {
  93. ABTS_TRUE(tc, rc == 0);
  94. }
  95. }
  96. }
  97. /* same for v6? */
  98. }
  99. static void test_interesting_subnets(abts_case *tc, void *data)
  100. {
  101. struct {
  102. const char *ipstr, *mask;
  103. int family;
  104. char *in_subnet, *not_in_subnet;
  105. } testcases[] =
  106. {
  107. {"9.67", NULL, APR_INET, "9.67.113.15", "10.1.2.3"}
  108. ,{"9.67.0.0", "16", APR_INET, "9.67.113.15", "10.1.2.3"}
  109. ,{"9.67.0.0", "255.255.0.0", APR_INET, "9.67.113.15", "10.1.2.3"}
  110. ,{"9.67.113.99", "16", APR_INET, "9.67.113.15", "10.1.2.3"}
  111. ,{"9.67.113.99", "255.255.255.0", APR_INET, "9.67.113.15", "10.1.2.3"}
  112. #if APR_HAVE_IPV6
  113. ,{"fe80::", "8", APR_INET6, "fe80::1", "ff01::1"}
  114. ,{"ff01::", "8", APR_INET6, "ff01::1", "fe80::1"}
  115. ,{"3FFE:8160::", "28", APR_INET6, "3ffE:816e:abcd:1234::1", "3ffe:8170::1"}
  116. ,{"127.0.0.1", NULL, APR_INET6, "::ffff:127.0.0.1", "fe80::1"}
  117. ,{"127.0.0.1", "8", APR_INET6, "::ffff:127.0.0.1", "fe80::1"}
  118. #endif
  119. };
  120. fspr_ipsubnet_t *ipsub;
  121. fspr_sockaddr_t *sa;
  122. fspr_status_t rv;
  123. int i, rc;
  124. for (i = 0; i < sizeof testcases / sizeof testcases[0]; i++) {
  125. rv = fspr_ipsubnet_create(&ipsub, testcases[i].ipstr, testcases[i].mask, p);
  126. ABTS_TRUE(tc, rv == APR_SUCCESS);
  127. rv = fspr_sockaddr_info_get(&sa, testcases[i].in_subnet, testcases[i].family, 0, 0, p);
  128. ABTS_TRUE(tc, rv == APR_SUCCESS);
  129. rc = fspr_ipsubnet_test(ipsub, sa);
  130. ABTS_TRUE(tc, rc != 0);
  131. rv = fspr_sockaddr_info_get(&sa, testcases[i].not_in_subnet, testcases[i].family, 0, 0, p);
  132. ABTS_TRUE(tc, rv == APR_SUCCESS);
  133. rc = fspr_ipsubnet_test(ipsub, sa);
  134. ABTS_TRUE(tc, rc == 0);
  135. }
  136. }
  137. static void test_badmask_str(abts_case *tc, void *data)
  138. {
  139. char buf[128];
  140. ABTS_STR_EQUAL(tc, fspr_strerror(APR_EBADMASK, buf, sizeof buf),
  141. "The specified network mask is invalid.");
  142. }
  143. static void test_badip_str(abts_case *tc, void *data)
  144. {
  145. char buf[128];
  146. ABTS_STR_EQUAL(tc, fspr_strerror(APR_EBADIP, buf, sizeof buf),
  147. "The specified IP address is invalid.");
  148. }
  149. abts_suite *testipsub(abts_suite *suite)
  150. {
  151. suite = ADD_SUITE(suite)
  152. abts_run_test(suite, test_bad_input, NULL);
  153. abts_run_test(suite, test_singleton_subnets, NULL);
  154. abts_run_test(suite, test_interesting_subnets, NULL);
  155. abts_run_test(suite, test_badmask_str, NULL);
  156. abts_run_test(suite, test_badip_str, NULL);
  157. return suite;
  158. }