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 "test.h"
25
26 #include "testutil.h"
27 #include "warnless.h"
28 #include "memdebug.h"
29
test(char * URL)30 CURLcode test(char *URL)
31 {
32 CURLcode res = CURLE_OK;
33 char *url_after = NULL;
34 CURLU *curlu = curl_url();
35 char error_buffer[CURL_ERROR_SIZE] = "";
36 CURL *curl;
37
38 easy_init(curl);
39
40 curl_url_set(curlu, CURLUPART_URL, URL, CURLU_DEFAULT_SCHEME);
41 easy_setopt(curl, CURLOPT_CURLU, curlu);
42 easy_setopt(curl, CURLOPT_ERRORBUFFER, error_buffer);
43 easy_setopt(curl, CURLOPT_VERBOSE, 1L);
44 /* msys2 times out instead of CURLE_COULDNT_CONNECT, so make it faster */
45 easy_setopt(curl, CURLOPT_CONNECTTIMEOUT_MS, 5000L);
46 /* set a port number that makes this request fail */
47 easy_setopt(curl, CURLOPT_PORT, 1L);
48 res = curl_easy_perform(curl);
49 if(res != CURLE_COULDNT_CONNECT && res != CURLE_OPERATION_TIMEDOUT) {
50 fprintf(stderr, "failure expected, "
51 "curl_easy_perform returned %d: <%s>, <%s>\n",
52 res, curl_easy_strerror(res), error_buffer);
53 if(res == CURLE_OK)
54 res = TEST_ERR_MAJOR_BAD; /* force an error return */
55 goto test_cleanup;
56 }
57 res = CURLE_OK; /* reset for next use */
58
59 /* print the used url */
60 curl_url_get(curlu, CURLUPART_URL, &url_after, 0);
61 fprintf(stderr, "curlu now: <%s>\n", url_after);
62 curl_free(url_after);
63 url_after = NULL;
64
65 /* now reset CURLOP_PORT to go back to originally set port number */
66 easy_setopt(curl, CURLOPT_PORT, 0L);
67
68 res = curl_easy_perform(curl);
69 if(res)
70 fprintf(stderr, "success expected, "
71 "curl_easy_perform returned %d: <%s>, <%s>\n",
72 res, curl_easy_strerror(res), error_buffer);
73
74 /* print url */
75 curl_url_get(curlu, CURLUPART_URL, &url_after, 0);
76 fprintf(stderr, "curlu now: <%s>\n", url_after);
77
78 test_cleanup:
79 curl_free(url_after);
80 curl_easy_cleanup(curl);
81 curl_url_cleanup(curlu);
82 curl_global_cleanup();
83
84 return res;
85 }
86