xf.li | 6c8fc1e | 2023-08-12 00:11:09 -0700 | [diff] [blame^] | 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 | |
| 26 | #include "noproxy.h" |
| 27 | |
| 28 | static CURLcode unit_setup(void) |
| 29 | { |
| 30 | return CURLE_OK; |
| 31 | } |
| 32 | |
| 33 | static void unit_stop(void) |
| 34 | { |
| 35 | |
| 36 | } |
| 37 | |
| 38 | struct check { |
| 39 | const char *a; |
| 40 | const char *n; |
| 41 | unsigned int bits; |
| 42 | bool match; |
| 43 | }; |
| 44 | |
| 45 | struct noproxy { |
| 46 | const char *a; |
| 47 | const char *n; |
| 48 | bool match; |
| 49 | }; |
| 50 | |
| 51 | UNITTEST_START |
| 52 | #if defined(DEBUGBUILD) && !defined(CURL_DISABLE_PROXY) |
| 53 | { |
| 54 | int i; |
| 55 | int err = 0; |
| 56 | struct check list4[]= { |
| 57 | { "192.160.0.1", "192.160.0.1", 33, FALSE}, |
| 58 | { "192.160.0.1", "192.160.0.1", 32, TRUE}, |
| 59 | { "192.160.0.1", "192.160.0.1", 0, TRUE}, |
| 60 | { "192.160.0.1", "192.160.0.1", 24, TRUE}, |
| 61 | { "192.160.0.1", "192.160.0.1", 26, TRUE}, |
| 62 | { "192.160.0.1", "192.160.0.1", 20, TRUE}, |
| 63 | { "192.160.0.1", "192.160.0.1", 18, TRUE}, |
| 64 | { "192.160.0.1", "192.160.0.1", 12, TRUE}, |
| 65 | { "192.160.0.1", "192.160.0.1", 8, TRUE}, |
| 66 | { "192.160.0.1", "10.0.0.1", 8, FALSE}, |
| 67 | { "192.160.0.1", "10.0.0.1", 32, FALSE}, |
| 68 | { "192.160.0.1", "10.0.0.1", 0, FALSE}, |
| 69 | { NULL, NULL, 0, FALSE} /* end marker */ |
| 70 | }; |
| 71 | struct check list6[]= { |
| 72 | { "::1", "::1", 0, TRUE}, |
| 73 | { "::1", "::1", 128, TRUE}, |
| 74 | { "::1", "0:0::1", 128, TRUE}, |
| 75 | { "::1", "0:0::1", 129, FALSE}, |
| 76 | { "fe80::ab47:4396:55c9:8474", "fe80::ab47:4396:55c9:8474", 64, TRUE}, |
| 77 | { NULL, NULL, 0, FALSE} /* end marker */ |
| 78 | }; |
| 79 | struct noproxy list[]= { |
| 80 | { "foobar", "barfoo", FALSE}, |
| 81 | { "foobar", "foobar", TRUE}, |
| 82 | { "192.168.0.1", "foobar", FALSE}, |
| 83 | { "192.168.0.1", "192.168.0.0/16", TRUE}, |
| 84 | { "192.168.0.1", "192.168.0.0/24", TRUE}, |
| 85 | { "192.168.0.1", "192.168.0.0/32", FALSE}, |
| 86 | { "192.168.0.1", "192.168.0.0", FALSE}, |
| 87 | { "192.168.1.1", "192.168.0.0/24", FALSE}, |
| 88 | { "192.168.1.1", "foo, bar, 192.168.0.0/24", FALSE}, |
| 89 | { "192.168.1.1", "foo, bar, 192.168.0.0/16", TRUE}, |
| 90 | { "[::1]", "foo, bar, 192.168.0.0/16", FALSE}, |
| 91 | { "[::1]", "foo, bar, ::1/64", TRUE}, |
| 92 | { "bar", "foo, bar, ::1/64", TRUE}, |
| 93 | { "BAr", "foo, bar, ::1/64", TRUE}, |
| 94 | { "BAr", "foo,,,,, bar, ::1/64", TRUE}, |
| 95 | { "www.example.com", "foo, .example.com", TRUE}, |
| 96 | { "www.example.com", "www2.example.com, .example.net", FALSE}, |
| 97 | { "example.com", ".example.com, .example.net", TRUE}, |
| 98 | { "nonexample.com", ".example.com, .example.net", FALSE}, |
| 99 | { NULL, NULL, FALSE} |
| 100 | }; |
| 101 | for(i = 0; list4[i].a; i++) { |
| 102 | bool match = Curl_cidr4_match(list4[i].a, list4[i].n, list4[i].bits); |
| 103 | if(match != list4[i].match) { |
| 104 | fprintf(stderr, "%s in %s/%u should %smatch\n", |
| 105 | list4[i].a, list4[i].n, list4[i].bits, |
| 106 | list4[i].match ? "": "not "); |
| 107 | err++; |
| 108 | } |
| 109 | } |
| 110 | for(i = 0; list6[i].a; i++) { |
| 111 | bool match = Curl_cidr6_match(list6[i].a, list6[i].n, list6[i].bits); |
| 112 | if(match != list6[i].match) { |
| 113 | fprintf(stderr, "%s in %s/%u should %smatch\n", |
| 114 | list6[i].a, list6[i].n, list6[i].bits, |
| 115 | list6[i].match ? "": "not "); |
| 116 | err++; |
| 117 | } |
| 118 | } |
| 119 | for(i = 0; list[i].a; i++) { |
| 120 | bool match = Curl_check_noproxy(list[i].a, list[i].n); |
| 121 | if(match != list[i].match) { |
| 122 | fprintf(stderr, "%s in %s should %smatch\n", |
| 123 | list[i].a, list[i].n, |
| 124 | list[i].match ? "": "not "); |
| 125 | err++; |
| 126 | } |
| 127 | } |
| 128 | return err; |
| 129 | } |
| 130 | #else |
| 131 | return 0; |
| 132 | #endif |
| 133 | UNITTEST_STOP |