xref: /curl/tests/unit/unit1397.c (revision 78d6232f)
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 
26 
unit_setup(void)27 static CURLcode unit_setup(void)
28 {
29   return CURLE_OK;
30 }
31 
unit_stop(void)32 static void unit_stop(void)
33 {
34 }
35 
36 /* only these backends define the tested functions */
37 #if defined(USE_OPENSSL) || defined(USE_SCHANNEL)
38 #include "vtls/hostcheck.h"
39 struct testcase {
40   const char *host;
41   const char *pattern;
42   bool match;
43 };
44 
45 static struct testcase tests[] = {
46   {"", "", FALSE},
47   {"a", "", FALSE},
48   {"", "b", FALSE},
49   {"a", "b", FALSE},
50   {"aa", "bb", FALSE},
51   {"\xff", "\xff", TRUE},
52   {"aa.aa.aa", "aa.aa.bb", FALSE},
53   {"aa.aa.aa", "aa.aa.aa", TRUE},
54   {"aa.aa.aa", "*.aa.bb", FALSE},
55   {"aa.aa.aa", "*.aa.aa", TRUE},
56   {"192.168.0.1", "192.168.0.1", TRUE},
57   {"192.168.0.1", "*.168.0.1", FALSE},
58   {"192.168.0.1", "*.0.1", FALSE},
59   {"h.ello", "*.ello", FALSE},
60   {"h.ello.", "*.ello", FALSE},
61   {"h.ello", "*.ello.", FALSE},
62   {"h.e.llo", "*.e.llo", TRUE},
63   {"h.e.llo", " *.e.llo", FALSE},
64   {" h.e.llo", "*.e.llo", TRUE},
65   {"h.e.llo.", "*.e.llo", TRUE},
66   {"*.e.llo.", "*.e.llo", TRUE},
67   {"************.e.llo.", "*.e.llo", TRUE},
68   {"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
69    "BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB"
70    "CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC"
71    "DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD"
72    "EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE"
73    ".e.llo.", "*.e.llo", TRUE},
74   {"\xfe\xfe.e.llo.", "*.e.llo", TRUE},
75   {"h.e.llo.", "*.e.llo.", TRUE},
76   {"h.e.llo", "*.e.llo.", TRUE},
77   {".h.e.llo", "*.e.llo.", FALSE},
78   {"h.e.llo", "*.*.llo.", FALSE},
79   {"h.e.llo", "h.*.llo", FALSE},
80   {"h.e.llo", "h.e.*", FALSE},
81   {"hello", "*.ello", FALSE},
82   {"hello", "**llo", FALSE},
83   {"bar.foo.example.com", "*.example.com", FALSE},
84   {"foo.example.com", "*.example.com", TRUE},
85   {"baz.example.net", "b*z.example.net", FALSE},
86   {"foobaz.example.net", "*baz.example.net", FALSE},
87   {"xn--l8j.example.local", "x*.example.local", FALSE},
88   {"xn--l8j.example.net", "*.example.net", TRUE},
89   {"xn--l8j.example.net", "*j.example.net", FALSE},
90   {"xn--l8j.example.net", "xn--l8j.example.net", TRUE},
91   {"xn--l8j.example.net", "xn--l8j.*.net", FALSE},
92   {"xl8j.example.net", "*.example.net", TRUE},
93   {"fe80::3285:a9ff:fe46:b619", "*::3285:a9ff:fe46:b619", FALSE},
94   {"fe80::3285:a9ff:fe46:b619", "fe80::3285:a9ff:fe46:b619", TRUE},
95   {NULL, NULL, FALSE}
96 };
97 
98 UNITTEST_START
99 {
100   int i;
101   for(i = 0; tests[i].host; i++) {
102     if(tests[i].match != Curl_cert_hostcheck(tests[i].pattern,
103                                              strlen(tests[i].pattern),
104                                              tests[i].host,
105                                              strlen(tests[i].host))) {
106       fprintf(stderr,
107               "HOST: %s\n"
108               "PTRN: %s\n"
109               "did %sMATCH\n",
110               tests[i].host,
111               tests[i].pattern,
112               tests[i].match ? "NOT ": "");
113       unitfail++;
114     }
115   }
116 }
117 
118 UNITTEST_STOP
119 #else
120 
121 UNITTEST_START
122 
123 UNITTEST_STOP
124 #endif
125