xref: /curl/tests/libtest/libprereq.c (revision 25cbc2f7)
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) Max Dymond, <max.dymond@microsoft.com>
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 typedef struct prcs {
27   int prereq_retcode;
28   int ipv6;
29 } PRCS;
30 
prereq_callback(void * clientp,char * conn_primary_ip,char * conn_local_ip,int conn_primary_port,int conn_local_port)31 static int prereq_callback(void *clientp,
32                            char *conn_primary_ip,
33                            char *conn_local_ip,
34                            int conn_primary_port,
35                            int conn_local_port)
36 {
37   PRCS *prereq_cb = (PRCS *)clientp;
38 
39   if(prereq_cb->ipv6) {
40     printf("Connected to [%s]\n", conn_primary_ip);
41     printf("Connected from [%s]\n", conn_local_ip);
42   }
43   else {
44     printf("Connected to %s\n", conn_primary_ip);
45     printf("Connected from %s\n", conn_local_ip);
46   }
47 
48   printf("Remote port = %d\n", conn_primary_port);
49   printf("Local port = %d\n", conn_local_port);
50   printf("Returning = %d\n", prereq_cb->prereq_retcode);
51   return prereq_cb->prereq_retcode;
52 }
53 
test(char * URL)54 CURLcode test(char *URL)
55 {
56   PRCS prereq_cb;
57   CURLcode ret = CURLE_OK;
58   CURL *curl = NULL;
59 
60   prereq_cb.prereq_retcode = CURL_PREREQFUNC_OK;
61   prereq_cb.ipv6 = 0;
62 
63   curl_global_init(CURL_GLOBAL_ALL);
64   curl = curl_easy_init();
65 
66   if(curl) {
67     if(strstr(URL, "#ipv6")) {
68       /* The IP addresses should be surrounded by brackets! */
69       prereq_cb.ipv6 = 1;
70     }
71     if(strstr(URL, "#err")) {
72       /* Set the callback to exit with failure */
73       prereq_cb.prereq_retcode = CURL_PREREQFUNC_ABORT;
74     }
75 
76     curl_easy_setopt(curl, CURLOPT_URL, URL);
77     curl_easy_setopt(curl, CURLOPT_PREREQFUNCTION, prereq_callback);
78     curl_easy_setopt(curl, CURLOPT_PREREQDATA, &prereq_cb);
79     curl_easy_setopt(curl, CURLOPT_WRITEDATA, stderr);
80 
81     if(strstr(URL, "#redir")) {
82       /* Enable follow-location */
83       curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
84     }
85 
86     ret = curl_easy_perform(curl);
87     if(ret) {
88       fprintf(stderr, "%s:%d curl_easy_perform() failed with code %d (%s)\n",
89           __FILE__, __LINE__, ret, curl_easy_strerror(ret));
90       goto test_cleanup;
91     }
92   }
93 
94 test_cleanup:
95   curl_easy_cleanup(curl);
96   curl_global_cleanup();
97 
98   return ret;
99 }
100