xref: /aosp_15_r20/external/curl/tests/libtest/libprereq.c (revision 6236dae45794135f37c4eb022389c904c8b0090d)
1*6236dae4SAndroid Build Coastguard Worker /***************************************************************************
2*6236dae4SAndroid Build Coastguard Worker  *                                  _   _ ____  _
3*6236dae4SAndroid Build Coastguard Worker  *  Project                     ___| | | |  _ \| |
4*6236dae4SAndroid Build Coastguard Worker  *                             / __| | | | |_) | |
5*6236dae4SAndroid Build Coastguard Worker  *                            | (__| |_| |  _ <| |___
6*6236dae4SAndroid Build Coastguard Worker  *                             \___|\___/|_| \_\_____|
7*6236dae4SAndroid Build Coastguard Worker  *
8*6236dae4SAndroid Build Coastguard Worker  * Copyright (C) Max Dymond, <[email protected]>
9*6236dae4SAndroid Build Coastguard Worker  *
10*6236dae4SAndroid Build Coastguard Worker  * This software is licensed as described in the file COPYING, which
11*6236dae4SAndroid Build Coastguard Worker  * you should have received as part of this distribution. The terms
12*6236dae4SAndroid Build Coastguard Worker  * are also available at https://curl.se/docs/copyright.html.
13*6236dae4SAndroid Build Coastguard Worker  *
14*6236dae4SAndroid Build Coastguard Worker  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15*6236dae4SAndroid Build Coastguard Worker  * copies of the Software, and permit persons to whom the Software is
16*6236dae4SAndroid Build Coastguard Worker  * furnished to do so, under the terms of the COPYING file.
17*6236dae4SAndroid Build Coastguard Worker  *
18*6236dae4SAndroid Build Coastguard Worker  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19*6236dae4SAndroid Build Coastguard Worker  * KIND, either express or implied.
20*6236dae4SAndroid Build Coastguard Worker  *
21*6236dae4SAndroid Build Coastguard Worker  * SPDX-License-Identifier: curl
22*6236dae4SAndroid Build Coastguard Worker  *
23*6236dae4SAndroid Build Coastguard Worker  ***************************************************************************/
24*6236dae4SAndroid Build Coastguard Worker #include "test.h"
25*6236dae4SAndroid Build Coastguard Worker 
26*6236dae4SAndroid Build Coastguard Worker typedef struct prcs {
27*6236dae4SAndroid Build Coastguard Worker   int prereq_retcode;
28*6236dae4SAndroid Build Coastguard Worker   int ipv6;
29*6236dae4SAndroid Build Coastguard Worker } PRCS;
30*6236dae4SAndroid Build Coastguard Worker 
prereq_callback(void * clientp,char * conn_primary_ip,char * conn_local_ip,int conn_primary_port,int conn_local_port)31*6236dae4SAndroid Build Coastguard Worker static int prereq_callback(void *clientp,
32*6236dae4SAndroid Build Coastguard Worker                            char *conn_primary_ip,
33*6236dae4SAndroid Build Coastguard Worker                            char *conn_local_ip,
34*6236dae4SAndroid Build Coastguard Worker                            int conn_primary_port,
35*6236dae4SAndroid Build Coastguard Worker                            int conn_local_port)
36*6236dae4SAndroid Build Coastguard Worker {
37*6236dae4SAndroid Build Coastguard Worker   PRCS *prereq_cb = (PRCS *)clientp;
38*6236dae4SAndroid Build Coastguard Worker 
39*6236dae4SAndroid Build Coastguard Worker   if(prereq_cb->ipv6) {
40*6236dae4SAndroid Build Coastguard Worker     printf("Connected to [%s]\n", conn_primary_ip);
41*6236dae4SAndroid Build Coastguard Worker     printf("Connected from [%s]\n", conn_local_ip);
42*6236dae4SAndroid Build Coastguard Worker   }
43*6236dae4SAndroid Build Coastguard Worker   else {
44*6236dae4SAndroid Build Coastguard Worker     printf("Connected to %s\n", conn_primary_ip);
45*6236dae4SAndroid Build Coastguard Worker     printf("Connected from %s\n", conn_local_ip);
46*6236dae4SAndroid Build Coastguard Worker   }
47*6236dae4SAndroid Build Coastguard Worker 
48*6236dae4SAndroid Build Coastguard Worker   printf("Remote port = %d\n", conn_primary_port);
49*6236dae4SAndroid Build Coastguard Worker   printf("Local port = %d\n", conn_local_port);
50*6236dae4SAndroid Build Coastguard Worker   printf("Returning = %d\n", prereq_cb->prereq_retcode);
51*6236dae4SAndroid Build Coastguard Worker   return prereq_cb->prereq_retcode;
52*6236dae4SAndroid Build Coastguard Worker }
53*6236dae4SAndroid Build Coastguard Worker 
test(char * URL)54*6236dae4SAndroid Build Coastguard Worker CURLcode test(char *URL)
55*6236dae4SAndroid Build Coastguard Worker {
56*6236dae4SAndroid Build Coastguard Worker   PRCS prereq_cb;
57*6236dae4SAndroid Build Coastguard Worker   CURLcode ret = CURLE_OK;
58*6236dae4SAndroid Build Coastguard Worker   CURL *curl = NULL;
59*6236dae4SAndroid Build Coastguard Worker 
60*6236dae4SAndroid Build Coastguard Worker   prereq_cb.prereq_retcode = CURL_PREREQFUNC_OK;
61*6236dae4SAndroid Build Coastguard Worker   prereq_cb.ipv6 = 0;
62*6236dae4SAndroid Build Coastguard Worker 
63*6236dae4SAndroid Build Coastguard Worker   curl_global_init(CURL_GLOBAL_ALL);
64*6236dae4SAndroid Build Coastguard Worker   curl = curl_easy_init();
65*6236dae4SAndroid Build Coastguard Worker 
66*6236dae4SAndroid Build Coastguard Worker   if(curl) {
67*6236dae4SAndroid Build Coastguard Worker     if(strstr(URL, "#ipv6")) {
68*6236dae4SAndroid Build Coastguard Worker       /* The IP addresses should be surrounded by brackets! */
69*6236dae4SAndroid Build Coastguard Worker       prereq_cb.ipv6 = 1;
70*6236dae4SAndroid Build Coastguard Worker     }
71*6236dae4SAndroid Build Coastguard Worker     if(strstr(URL, "#err")) {
72*6236dae4SAndroid Build Coastguard Worker       /* Set the callback to exit with failure */
73*6236dae4SAndroid Build Coastguard Worker       prereq_cb.prereq_retcode = CURL_PREREQFUNC_ABORT;
74*6236dae4SAndroid Build Coastguard Worker     }
75*6236dae4SAndroid Build Coastguard Worker 
76*6236dae4SAndroid Build Coastguard Worker     curl_easy_setopt(curl, CURLOPT_URL, URL);
77*6236dae4SAndroid Build Coastguard Worker     curl_easy_setopt(curl, CURLOPT_PREREQFUNCTION, prereq_callback);
78*6236dae4SAndroid Build Coastguard Worker     curl_easy_setopt(curl, CURLOPT_PREREQDATA, &prereq_cb);
79*6236dae4SAndroid Build Coastguard Worker     curl_easy_setopt(curl, CURLOPT_WRITEDATA, stderr);
80*6236dae4SAndroid Build Coastguard Worker 
81*6236dae4SAndroid Build Coastguard Worker     if(strstr(URL, "#redir")) {
82*6236dae4SAndroid Build Coastguard Worker       /* Enable follow-location */
83*6236dae4SAndroid Build Coastguard Worker       curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
84*6236dae4SAndroid Build Coastguard Worker     }
85*6236dae4SAndroid Build Coastguard Worker 
86*6236dae4SAndroid Build Coastguard Worker     ret = curl_easy_perform(curl);
87*6236dae4SAndroid Build Coastguard Worker     if(ret) {
88*6236dae4SAndroid Build Coastguard Worker       fprintf(stderr, "%s:%d curl_easy_perform() failed with code %d (%s)\n",
89*6236dae4SAndroid Build Coastguard Worker           __FILE__, __LINE__, ret, curl_easy_strerror(ret));
90*6236dae4SAndroid Build Coastguard Worker       goto test_cleanup;
91*6236dae4SAndroid Build Coastguard Worker     }
92*6236dae4SAndroid Build Coastguard Worker   }
93*6236dae4SAndroid Build Coastguard Worker 
94*6236dae4SAndroid Build Coastguard Worker test_cleanup:
95*6236dae4SAndroid Build Coastguard Worker   curl_easy_cleanup(curl);
96*6236dae4SAndroid Build Coastguard Worker   curl_global_cleanup();
97*6236dae4SAndroid Build Coastguard Worker 
98*6236dae4SAndroid Build Coastguard Worker   return ret;
99*6236dae4SAndroid Build Coastguard Worker }
100