unit1614.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2022, 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", TRUE},
  74. { "www.example.com.", "localhost,.example.com,.example.de", TRUE},
  75. { "example.com", "localhost,.example.com,.example.de", TRUE},
  76. { "example.com.", "localhost,.example.com,.example.de", TRUE},
  77. { "www.example.com", "localhost,.example.com.,.example.de", TRUE},
  78. { "www.example.com", "localhost,www.example.com.,.example.de", TRUE},
  79. { "example.com", "localhost,example.com,.example.de", TRUE},
  80. { "example.com.", "localhost,example.com,.example.de", TRUE},
  81. { "www.example.com", "localhost,example.com,.example.de", FALSE},
  82. { "127.0.0.1", "127.0.0.1,localhost", TRUE},
  83. { "127.0.0.1", "127.0.0.1,localhost,", TRUE},
  84. { "127.0.0.1", "127.0.0.1/8,localhost,", TRUE},
  85. { "127.0.0.1", "127.0.0.1/28,localhost,", TRUE},
  86. { "127.0.0.1", "127.0.0.1/31,localhost,", TRUE},
  87. { "127.0.0.1", "localhost,127.0.0.1", TRUE},
  88. { "127.0.0.1", "localhost,127.0.0.1.127.0.0.1.127.0.0.1.127.0.0.1."
  89. "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."
  90. "0.0.1.127.0.0.1.127.0.0." /* 128 bytes "address" */, FALSE},
  91. { "127.0.0.1", "localhost,127.0.0.1.127.0.0.1.127.0.0.1.127.0.0.1."
  92. "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."
  93. "0.0.1.127.0.0.1.127.0.0" /* 127 bytes "address" */, FALSE},
  94. { "localhost", "localhost,127.0.0.1", TRUE},
  95. { "localhost", "127.0.0.1,localhost", TRUE},
  96. { "foobar", "barfoo", FALSE},
  97. { "foobar", "foobar", TRUE},
  98. { "192.168.0.1", "foobar", FALSE},
  99. { "192.168.0.1", "192.168.0.0/16", TRUE},
  100. { "192.168.0.1", "192.168.0.0/24", TRUE},
  101. { "192.168.0.1", "192.168.0.0/32", FALSE},
  102. { "192.168.0.1", "192.168.0.0", FALSE},
  103. { "192.168.1.1", "192.168.0.0/24", FALSE},
  104. { "192.168.1.1", "foo, bar, 192.168.0.0/24", FALSE},
  105. { "192.168.1.1", "foo, bar, 192.168.0.0/16", TRUE},
  106. { "[::1]", "foo, bar, 192.168.0.0/16", FALSE},
  107. { "[::1]", "foo, bar, ::1/64", TRUE},
  108. { "bar", "foo, bar, ::1/64", TRUE},
  109. { "BAr", "foo, bar, ::1/64", TRUE},
  110. { "BAr", "foo,,,,, bar, ::1/64", TRUE},
  111. { "www.example.com", "foo, .example.com", TRUE},
  112. { "www.example.com", "www2.example.com, .example.net", FALSE},
  113. { "example.com", ".example.com, .example.net", TRUE},
  114. { "nonexample.com", ".example.com, .example.net", FALSE},
  115. { NULL, NULL, FALSE}
  116. };
  117. for(i = 0; list4[i].a; i++) {
  118. bool match = Curl_cidr4_match(list4[i].a, list4[i].n, list4[i].bits);
  119. if(match != list4[i].match) {
  120. fprintf(stderr, "%s in %s/%u should %smatch\n",
  121. list4[i].a, list4[i].n, list4[i].bits,
  122. list4[i].match ? "": "not ");
  123. err++;
  124. }
  125. }
  126. for(i = 0; list6[i].a; i++) {
  127. bool match = Curl_cidr6_match(list6[i].a, list6[i].n, list6[i].bits);
  128. if(match != list6[i].match) {
  129. fprintf(stderr, "%s in %s/%u should %smatch\n",
  130. list6[i].a, list6[i].n, list6[i].bits,
  131. list6[i].match ? "": "not ");
  132. err++;
  133. }
  134. }
  135. for(i = 0; list[i].a; i++) {
  136. bool match = Curl_check_noproxy(list[i].a, list[i].n);
  137. if(match != list[i].match) {
  138. fprintf(stderr, "%s in %s should %smatch\n",
  139. list[i].a, list[i].n,
  140. list[i].match ? "": "not ");
  141. err++;
  142. }
  143. }
  144. return err;
  145. }
  146. #else
  147. return 0;
  148. #endif
  149. UNITTEST_STOP