unit1614.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. struct check list6[]= {
  65. { "::1", "::1", 0, TRUE},
  66. { "::1", "::1", 128, TRUE},
  67. { "::1", "0:0::1", 128, TRUE},
  68. { "::1", "0:0::1", 129, FALSE},
  69. { "fe80::ab47:4396:55c9:8474", "fe80::ab47:4396:55c9:8474", 64, TRUE},
  70. { NULL, NULL, 0, FALSE} /* end marker */
  71. };
  72. struct noproxy list[]= {
  73. { "www.example.com", "localhost .example.com .example.de", FALSE},
  74. { "www.example.com", "localhost,.example.com,.example.de", TRUE},
  75. { "www.example.com.", "localhost,.example.com,.example.de", TRUE},
  76. { "example.com", "localhost,.example.com,.example.de", TRUE},
  77. { "example.com.", "localhost,.example.com,.example.de", TRUE},
  78. { "www.example.com", "localhost,.example.com.,.example.de", TRUE},
  79. { "www.example.com", "localhost,www.example.com.,.example.de", TRUE},
  80. { "example.com", "localhost,example.com,.example.de", TRUE},
  81. { "example.com.", "localhost,example.com,.example.de", TRUE},
  82. { "nexample.com", "localhost,example.com,.example.de", FALSE},
  83. { "www.example.com", "localhost,example.com,.example.de", TRUE},
  84. { "127.0.0.1", "127.0.0.1,localhost", TRUE},
  85. { "127.0.0.1", "127.0.0.1,localhost,", TRUE},
  86. { "127.0.0.1", "127.0.0.1/8,localhost,", TRUE},
  87. { "127.0.0.1", "127.0.0.1/28,localhost,", TRUE},
  88. { "127.0.0.1", "127.0.0.1/31,localhost,", TRUE},
  89. { "127.0.0.1", "localhost,127.0.0.1", TRUE},
  90. { "127.0.0.1", "localhost,127.0.0.1.127.0.0.1.127.0.0.1.127.0.0.1."
  91. "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."
  92. "0.0.1.127.0.0.1.127.0.0." /* 128 bytes "address" */, FALSE},
  93. { "127.0.0.1", "localhost,127.0.0.1.127.0.0.1.127.0.0.1.127.0.0.1."
  94. "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."
  95. "0.0.1.127.0.0.1.127.0.0" /* 127 bytes "address" */, FALSE},
  96. { "localhost", "localhost,127.0.0.1", TRUE},
  97. { "localhost", "127.0.0.1,localhost", TRUE},
  98. { "foobar", "barfoo", FALSE},
  99. { "foobar", "foobar", TRUE},
  100. { "192.168.0.1", "foobar", FALSE},
  101. { "192.168.0.1", "192.168.0.0/16", TRUE},
  102. { "192.168.0.1", "192.168.0.0/24", TRUE},
  103. { "192.168.0.1", "192.168.0.0/32", FALSE},
  104. { "192.168.0.1", "192.168.0.0", FALSE},
  105. { "192.168.1.1", "192.168.0.0/24", FALSE},
  106. { "192.168.1.1", "192.168.0.0/33", FALSE},
  107. { "192.168.1.1", "foo, bar, 192.168.0.0/24", FALSE},
  108. { "192.168.1.1", "foo, bar, 192.168.0.0/16", TRUE},
  109. { "[::1]", "foo, bar, 192.168.0.0/16", FALSE},
  110. { "[::1]", "foo, bar, ::1/64", TRUE},
  111. { "[::1]", "::1/64", TRUE},
  112. { "[::1]", "::1/96", TRUE},
  113. { "[::1]", "::1/129", FALSE},
  114. { "bar", "foo, bar, ::1/64", TRUE},
  115. { "BAr", "foo, bar, ::1/64", TRUE},
  116. { "BAr", "foo,,,,, bar, ::1/64", TRUE},
  117. { "www.example.com", "foo, .example.com", TRUE},
  118. { "www.example.com", "www2.example.com, .example.net", FALSE},
  119. { "example.com", ".example.com, .example.net", TRUE},
  120. { "nonexample.com", ".example.com, .example.net", FALSE},
  121. { NULL, NULL, FALSE}
  122. };
  123. for(i = 0; list4[i].a; i++) {
  124. bool match = Curl_cidr4_match(list4[i].a, list4[i].n, list4[i].bits);
  125. if(match != list4[i].match) {
  126. fprintf(stderr, "%s in %s/%u should %smatch\n",
  127. list4[i].a, list4[i].n, list4[i].bits,
  128. list4[i].match ? "": "not ");
  129. err++;
  130. }
  131. }
  132. for(i = 0; list6[i].a; i++) {
  133. bool match = Curl_cidr6_match(list6[i].a, list6[i].n, list6[i].bits);
  134. if(match != list6[i].match) {
  135. fprintf(stderr, "%s in %s/%u should %smatch\n",
  136. list6[i].a, list6[i].n, list6[i].bits,
  137. list6[i].match ? "": "not ");
  138. err++;
  139. }
  140. }
  141. for(i = 0; list[i].a; i++) {
  142. bool match = Curl_check_noproxy(list[i].a, list[i].n);
  143. if(match != list[i].match) {
  144. fprintf(stderr, "%s in %s should %smatch\n",
  145. list[i].a, list[i].n,
  146. list[i].match ? "": "not ");
  147. err++;
  148. }
  149. }
  150. fail_if(err, "errors");
  151. }
  152. #endif
  153. UNITTEST_STOP