unit1614.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at https://curl.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. * SPDX-License-Identifier: curl
  22. *
  23. ***************************************************************************/
  24. #include "curlcheck.h"
  25. #include "noproxy.h"
  26. static CURLcode unit_setup(void)
  27. {
  28. return CURLE_OK;
  29. }
  30. static void unit_stop(void)
  31. {
  32. }
  33. struct check {
  34. const char *a;
  35. const char *n;
  36. unsigned int bits;
  37. bool match;
  38. };
  39. struct noproxy {
  40. const char *a;
  41. const char *n;
  42. bool match;
  43. };
  44. UNITTEST_START
  45. #if defined(DEBUGBUILD) && !defined(CURL_DISABLE_PROXY)
  46. {
  47. int i;
  48. int err = 0;
  49. struct check list4[]= {
  50. { "192.160.0.1", "192.160.0.1", 33, FALSE},
  51. { "192.160.0.1", "192.160.0.1", 32, TRUE},
  52. { "192.160.0.1", "192.160.0.1", 0, TRUE},
  53. { "192.160.0.1", "192.160.0.1", 24, TRUE},
  54. { "192.160.0.1", "192.160.0.1", 26, TRUE},
  55. { "192.160.0.1", "192.160.0.1", 20, TRUE},
  56. { "192.160.0.1", "192.160.0.1", 18, TRUE},
  57. { "192.160.0.1", "192.160.0.1", 12, TRUE},
  58. { "192.160.0.1", "192.160.0.1", 8, TRUE},
  59. { "192.160.0.1", "10.0.0.1", 8, FALSE},
  60. { "192.160.0.1", "10.0.0.1", 32, FALSE},
  61. { "192.160.0.1", "10.0.0.1", 0, FALSE},
  62. { NULL, NULL, 0, FALSE} /* end marker */
  63. };
  64. #ifdef USE_IPV6
  65. struct check list6[]= {
  66. { "::1", "::1", 0, TRUE},
  67. { "::1", "::1", 128, TRUE},
  68. { "::1", "0:0::1", 128, TRUE},
  69. { "::1", "0:0::1", 129, FALSE},
  70. { "fe80::ab47:4396:55c9:8474", "fe80::ab47:4396:55c9:8474", 64, TRUE},
  71. { NULL, NULL, 0, FALSE} /* end marker */
  72. };
  73. #endif
  74. struct noproxy list[]= {
  75. { "www.example.com", "localhost .example.com .example.de", FALSE},
  76. { "www.example.com", "localhost,.example.com,.example.de", TRUE},
  77. { "www.example.com.", "localhost,.example.com,.example.de", TRUE},
  78. { "example.com", "localhost,.example.com,.example.de", TRUE},
  79. { "example.com.", "localhost,.example.com,.example.de", TRUE},
  80. { "www.example.com", "localhost,.example.com.,.example.de", TRUE},
  81. { "www.example.com", "localhost,www.example.com.,.example.de", TRUE},
  82. { "example.com", "localhost,example.com,.example.de", TRUE},
  83. { "example.com.", "localhost,example.com,.example.de", TRUE},
  84. { "nexample.com", "localhost,example.com,.example.de", FALSE},
  85. { "www.example.com", "localhost,example.com,.example.de", TRUE},
  86. { "127.0.0.1", "127.0.0.1,localhost", TRUE},
  87. { "127.0.0.1", "127.0.0.1,localhost,", TRUE},
  88. { "127.0.0.1", "127.0.0.1/8,localhost,", TRUE},
  89. { "127.0.0.1", "127.0.0.1/28,localhost,", TRUE},
  90. { "127.0.0.1", "127.0.0.1/31,localhost,", TRUE},
  91. { "127.0.0.1", "localhost,127.0.0.1", TRUE},
  92. { "127.0.0.1", "localhost,127.0.0.1.127.0.0.1.127.0.0.1.127.0.0.1."
  93. "127.0.0.1.127.0.0.1.127.0.0.1.127.0.0.1.127.0.0.1.127.0.0.1.127."
  94. "0.0.1.127.0.0.1.127.0.0." /* 128 bytes "address" */, FALSE},
  95. { "127.0.0.1", "localhost,127.0.0.1.127.0.0.1.127.0.0.1.127.0.0.1."
  96. "127.0.0.1.127.0.0.1.127.0.0.1.127.0.0.1.127.0.0.1.127.0.0.1.127."
  97. "0.0.1.127.0.0.1.127.0.0" /* 127 bytes "address" */, FALSE},
  98. { "localhost", "localhost,127.0.0.1", TRUE},
  99. { "localhost", "127.0.0.1,localhost", TRUE},
  100. { "foobar", "barfoo", FALSE},
  101. { "foobar", "foobar", TRUE},
  102. { "192.168.0.1", "foobar", FALSE},
  103. { "192.168.0.1", "192.168.0.0/16", TRUE},
  104. { "192.168.0.1", "192.168.0.0/24", TRUE},
  105. { "192.168.0.1", "192.168.0.0/32", FALSE},
  106. { "192.168.0.1", "192.168.0.0", FALSE},
  107. { "192.168.1.1", "192.168.0.0/24", FALSE},
  108. { "192.168.1.1", "192.168.0.0/33", FALSE},
  109. { "192.168.1.1", "foo, bar, 192.168.0.0/24", FALSE},
  110. { "192.168.1.1", "foo, bar, 192.168.0.0/16", TRUE},
  111. #ifdef USE_IPV6
  112. { "[::1]", "foo, bar, 192.168.0.0/16", FALSE},
  113. { "[::1]", "foo, bar, ::1/64", TRUE},
  114. { "[::1]", "::1/64", TRUE},
  115. { "[::1]", "::1/96", TRUE},
  116. { "[::1]", "::1/129", FALSE},
  117. { "bar", "foo, bar, ::1/64", TRUE},
  118. { "BAr", "foo, bar, ::1/64", TRUE},
  119. { "BAr", "foo,,,,, bar, ::1/64", TRUE},
  120. #endif
  121. { "www.example.com", "foo, .example.com", TRUE},
  122. { "www.example.com", "www2.example.com, .example.net", FALSE},
  123. { "example.com", ".example.com, .example.net", TRUE},
  124. { "nonexample.com", ".example.com, .example.net", FALSE},
  125. { NULL, NULL, FALSE}
  126. };
  127. for(i = 0; list4[i].a; i++) {
  128. bool match = Curl_cidr4_match(list4[i].a, list4[i].n, list4[i].bits);
  129. if(match != list4[i].match) {
  130. fprintf(stderr, "%s in %s/%u should %smatch\n",
  131. list4[i].a, list4[i].n, list4[i].bits,
  132. list4[i].match ? "": "not ");
  133. err++;
  134. }
  135. }
  136. #ifdef USE_IPV6
  137. for(i = 0; list6[i].a; i++) {
  138. bool match = Curl_cidr6_match(list6[i].a, list6[i].n, list6[i].bits);
  139. if(match != list6[i].match) {
  140. fprintf(stderr, "%s in %s/%u should %smatch\n",
  141. list6[i].a, list6[i].n, list6[i].bits,
  142. list6[i].match ? "": "not ");
  143. err++;
  144. }
  145. }
  146. #endif
  147. for(i = 0; list[i].a; i++) {
  148. bool match = Curl_check_noproxy(list[i].a, list[i].n);
  149. if(match != list[i].match) {
  150. fprintf(stderr, "%s in %s should %smatch\n",
  151. list[i].a, list[i].n,
  152. list[i].match ? "": "not ");
  153. err++;
  154. }
  155. }
  156. fail_if(err, "errors");
  157. }
  158. #endif
  159. UNITTEST_STOP