2
0

unit1614.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. bool space; /* space separated */
  44. };
  45. UNITTEST_START
  46. #if defined(DEBUGBUILD) && !defined(CURL_DISABLE_PROXY)
  47. {
  48. int i;
  49. int err = 0;
  50. struct check list4[]= {
  51. { "192.160.0.1", "192.160.0.1", 33, FALSE},
  52. { "192.160.0.1", "192.160.0.1", 32, TRUE},
  53. { "192.160.0.1", "192.160.0.1", 0, TRUE},
  54. { "192.160.0.1", "192.160.0.1", 24, TRUE},
  55. { "192.160.0.1", "192.160.0.1", 26, TRUE},
  56. { "192.160.0.1", "192.160.0.1", 20, TRUE},
  57. { "192.160.0.1", "192.160.0.1", 18, TRUE},
  58. { "192.160.0.1", "192.160.0.1", 12, TRUE},
  59. { "192.160.0.1", "192.160.0.1", 8, TRUE},
  60. { "192.160.0.1", "10.0.0.1", 8, FALSE},
  61. { "192.160.0.1", "10.0.0.1", 32, FALSE},
  62. { "192.160.0.1", "10.0.0.1", 0, FALSE},
  63. { NULL, NULL, 0, FALSE} /* end marker */
  64. };
  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. struct noproxy list[]= {
  74. { "www.example.com", "localhost .example.com .example.de", TRUE, TRUE},
  75. { "www.example.com", "localhost,.example.com,.example.de", TRUE, FALSE},
  76. { "www.example.com.", "localhost,.example.com,.example.de", TRUE, FALSE},
  77. { "example.com", "localhost,.example.com,.example.de", TRUE, FALSE},
  78. { "example.com.", "localhost,.example.com,.example.de", TRUE, FALSE},
  79. { "www.example.com", "localhost,.example.com.,.example.de", TRUE, FALSE},
  80. { "www.example.com", "localhost,www.example.com.,.example.de",
  81. TRUE, FALSE},
  82. { "example.com", "localhost,example.com,.example.de", TRUE, FALSE},
  83. { "example.com.", "localhost,example.com,.example.de", TRUE, FALSE},
  84. { "nexample.com", "localhost,example.com,.example.de", FALSE, FALSE},
  85. { "www.example.com", "localhost,example.com,.example.de", TRUE, FALSE},
  86. { "127.0.0.1", "127.0.0.1,localhost", TRUE, FALSE},
  87. { "127.0.0.1", "127.0.0.1,localhost,", TRUE, FALSE},
  88. { "127.0.0.1", "127.0.0.1/8,localhost,", TRUE, FALSE},
  89. { "127.0.0.1", "127.0.0.1/28,localhost,", TRUE, FALSE},
  90. { "127.0.0.1", "127.0.0.1/31,localhost,", TRUE, FALSE},
  91. { "127.0.0.1", "localhost,127.0.0.1", TRUE, FALSE},
  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, 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, FALSE},
  98. { "localhost", "localhost,127.0.0.1", TRUE, FALSE},
  99. { "localhost", "127.0.0.1,localhost", TRUE, FALSE},
  100. { "foobar", "barfoo", FALSE, FALSE},
  101. { "foobar", "foobar", TRUE, FALSE},
  102. { "192.168.0.1", "foobar", FALSE, FALSE},
  103. { "192.168.0.1", "192.168.0.0/16", TRUE, FALSE},
  104. { "192.168.0.1", "192.168.0.0/24", TRUE, FALSE},
  105. { "192.168.0.1", "192.168.0.0/32", FALSE, FALSE},
  106. { "192.168.0.1", "192.168.0.0", FALSE, FALSE},
  107. { "192.168.1.1", "192.168.0.0/24", FALSE, FALSE},
  108. { "192.168.1.1", "foo, bar, 192.168.0.0/24", FALSE, FALSE},
  109. { "192.168.1.1", "foo, bar, 192.168.0.0/16", TRUE, FALSE},
  110. { "[::1]", "foo, bar, 192.168.0.0/16", FALSE, FALSE},
  111. { "[::1]", "foo, bar, ::1/64", TRUE, FALSE},
  112. { "bar", "foo, bar, ::1/64", TRUE, FALSE},
  113. { "BAr", "foo, bar, ::1/64", TRUE, FALSE},
  114. { "BAr", "foo,,,,, bar, ::1/64", TRUE, FALSE},
  115. { "www.example.com", "foo, .example.com", TRUE, FALSE},
  116. { "www.example.com", "www2.example.com, .example.net", FALSE, FALSE},
  117. { "example.com", ".example.com, .example.net", TRUE, FALSE},
  118. { "nonexample.com", ".example.com, .example.net", FALSE, FALSE},
  119. { NULL, NULL, FALSE, FALSE}
  120. };
  121. for(i = 0; list4[i].a; i++) {
  122. bool match = Curl_cidr4_match(list4[i].a, list4[i].n, list4[i].bits);
  123. if(match != list4[i].match) {
  124. fprintf(stderr, "%s in %s/%u should %smatch\n",
  125. list4[i].a, list4[i].n, list4[i].bits,
  126. list4[i].match ? "": "not ");
  127. err++;
  128. }
  129. }
  130. for(i = 0; list6[i].a; i++) {
  131. bool match = Curl_cidr6_match(list6[i].a, list6[i].n, list6[i].bits);
  132. if(match != list6[i].match) {
  133. fprintf(stderr, "%s in %s/%u should %smatch\n",
  134. list6[i].a, list6[i].n, list6[i].bits,
  135. list6[i].match ? "": "not ");
  136. err++;
  137. }
  138. }
  139. for(i = 0; list[i].a; i++) {
  140. bool spacesep = FALSE;
  141. bool match = Curl_check_noproxy(list[i].a, list[i].n, &spacesep);
  142. if(match != list[i].match) {
  143. fprintf(stderr, "%s in %s should %smatch\n",
  144. list[i].a, list[i].n,
  145. list[i].match ? "": "not ");
  146. err++;
  147. }
  148. if(spacesep != list[i].space) {
  149. fprintf(stderr, "%s is claimed to be %sspace separated\n",
  150. list[i].n, list[i].space?"":"NOT ");
  151. err++;
  152. }
  153. }
  154. fail_if(err, "errors");
  155. }
  156. #endif
  157. UNITTEST_STOP