xref: /aosp_15_r20/external/curl/tests/unit/unit1620.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) Daniel Stenberg, <[email protected]>, et al.
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 "curlcheck.h"
25*6236dae4SAndroid Build Coastguard Worker 
26*6236dae4SAndroid Build Coastguard Worker #include "urldata.h"
27*6236dae4SAndroid Build Coastguard Worker #include "url.h"
28*6236dae4SAndroid Build Coastguard Worker 
29*6236dae4SAndroid Build Coastguard Worker #include "memdebug.h" /* LAST include file */
30*6236dae4SAndroid Build Coastguard Worker 
unit_setup(void)31*6236dae4SAndroid Build Coastguard Worker static CURLcode unit_setup(void)
32*6236dae4SAndroid Build Coastguard Worker {
33*6236dae4SAndroid Build Coastguard Worker   CURLcode res = CURLE_OK;
34*6236dae4SAndroid Build Coastguard Worker   global_init(CURL_GLOBAL_ALL);
35*6236dae4SAndroid Build Coastguard Worker   return res;
36*6236dae4SAndroid Build Coastguard Worker }
37*6236dae4SAndroid Build Coastguard Worker 
unit_stop(void)38*6236dae4SAndroid Build Coastguard Worker static void unit_stop(void)
39*6236dae4SAndroid Build Coastguard Worker {
40*6236dae4SAndroid Build Coastguard Worker   curl_global_cleanup();
41*6236dae4SAndroid Build Coastguard Worker }
42*6236dae4SAndroid Build Coastguard Worker 
test_parse(const char * input,const char * exp_username,const char * exp_password,const char * exp_options)43*6236dae4SAndroid Build Coastguard Worker static void test_parse(
44*6236dae4SAndroid Build Coastguard Worker   const char *input,
45*6236dae4SAndroid Build Coastguard Worker   const char *exp_username,
46*6236dae4SAndroid Build Coastguard Worker   const char *exp_password,
47*6236dae4SAndroid Build Coastguard Worker   const char *exp_options)
48*6236dae4SAndroid Build Coastguard Worker {
49*6236dae4SAndroid Build Coastguard Worker   char *userstr = NULL;
50*6236dae4SAndroid Build Coastguard Worker   char *passwdstr = NULL;
51*6236dae4SAndroid Build Coastguard Worker   char *options = NULL;
52*6236dae4SAndroid Build Coastguard Worker   CURLcode rc = Curl_parse_login_details(input, strlen(input),
53*6236dae4SAndroid Build Coastguard Worker                                 &userstr, &passwdstr, &options);
54*6236dae4SAndroid Build Coastguard Worker   fail_unless(rc == CURLE_OK, "Curl_parse_login_details() failed");
55*6236dae4SAndroid Build Coastguard Worker 
56*6236dae4SAndroid Build Coastguard Worker   fail_unless(!!exp_username == !!userstr, "username expectation failed");
57*6236dae4SAndroid Build Coastguard Worker   fail_unless(!!exp_password == !!passwdstr, "password expectation failed");
58*6236dae4SAndroid Build Coastguard Worker   fail_unless(!!exp_options == !!options, "options expectation failed");
59*6236dae4SAndroid Build Coastguard Worker 
60*6236dae4SAndroid Build Coastguard Worker   if(!unitfail) {
61*6236dae4SAndroid Build Coastguard Worker     fail_unless(!exp_username || strcmp(userstr, exp_username) == 0,
62*6236dae4SAndroid Build Coastguard Worker                 "userstr should be equal to exp_username");
63*6236dae4SAndroid Build Coastguard Worker     fail_unless(!exp_password || strcmp(passwdstr, exp_password) == 0,
64*6236dae4SAndroid Build Coastguard Worker                 "passwdstr should be equal to exp_password");
65*6236dae4SAndroid Build Coastguard Worker     fail_unless(!exp_options || strcmp(options, exp_options) == 0,
66*6236dae4SAndroid Build Coastguard Worker                 "options should be equal to exp_options");
67*6236dae4SAndroid Build Coastguard Worker   }
68*6236dae4SAndroid Build Coastguard Worker 
69*6236dae4SAndroid Build Coastguard Worker   free(userstr);
70*6236dae4SAndroid Build Coastguard Worker   free(passwdstr);
71*6236dae4SAndroid Build Coastguard Worker   free(options);
72*6236dae4SAndroid Build Coastguard Worker }
73*6236dae4SAndroid Build Coastguard Worker 
74*6236dae4SAndroid Build Coastguard Worker UNITTEST_START
75*6236dae4SAndroid Build Coastguard Worker {
76*6236dae4SAndroid Build Coastguard Worker   CURLcode rc;
77*6236dae4SAndroid Build Coastguard Worker   struct Curl_easy *empty;
78*6236dae4SAndroid Build Coastguard Worker   enum dupstring i;
79*6236dae4SAndroid Build Coastguard Worker 
80*6236dae4SAndroid Build Coastguard Worker   bool async = FALSE;
81*6236dae4SAndroid Build Coastguard Worker   bool protocol_connect = FALSE;
82*6236dae4SAndroid Build Coastguard Worker 
83*6236dae4SAndroid Build Coastguard Worker   rc = Curl_open(&empty);
84*6236dae4SAndroid Build Coastguard Worker   if(rc)
85*6236dae4SAndroid Build Coastguard Worker     goto unit_test_abort;
86*6236dae4SAndroid Build Coastguard Worker   fail_unless(rc == CURLE_OK, "Curl_open() failed");
87*6236dae4SAndroid Build Coastguard Worker 
88*6236dae4SAndroid Build Coastguard Worker   rc = Curl_connect(empty, &async, &protocol_connect);
89*6236dae4SAndroid Build Coastguard Worker   fail_unless(rc == CURLE_URL_MALFORMAT,
90*6236dae4SAndroid Build Coastguard Worker               "Curl_connect() failed to return CURLE_URL_MALFORMAT");
91*6236dae4SAndroid Build Coastguard Worker 
92*6236dae4SAndroid Build Coastguard Worker   fail_unless(empty->magic == CURLEASY_MAGIC_NUMBER,
93*6236dae4SAndroid Build Coastguard Worker               "empty->magic should be equal to CURLEASY_MAGIC_NUMBER");
94*6236dae4SAndroid Build Coastguard Worker 
95*6236dae4SAndroid Build Coastguard Worker   /* double invoke to ensure no dependency on internal state */
96*6236dae4SAndroid Build Coastguard Worker   rc = Curl_connect(empty, &async, &protocol_connect);
97*6236dae4SAndroid Build Coastguard Worker   fail_unless(rc == CURLE_URL_MALFORMAT,
98*6236dae4SAndroid Build Coastguard Worker               "Curl_connect() failed to return CURLE_URL_MALFORMAT");
99*6236dae4SAndroid Build Coastguard Worker 
100*6236dae4SAndroid Build Coastguard Worker   rc = Curl_init_userdefined(empty);
101*6236dae4SAndroid Build Coastguard Worker   fail_unless(rc == CURLE_OK, "Curl_userdefined() failed");
102*6236dae4SAndroid Build Coastguard Worker 
103*6236dae4SAndroid Build Coastguard Worker   rc = Curl_init_do(empty, empty->conn);
104*6236dae4SAndroid Build Coastguard Worker   fail_unless(rc == CURLE_OK, "Curl_init_do() failed");
105*6236dae4SAndroid Build Coastguard Worker 
106*6236dae4SAndroid Build Coastguard Worker   test_parse("hostname", "hostname", NULL, NULL);
107*6236dae4SAndroid Build Coastguard Worker   test_parse("user:password", "user", "password", NULL);
108*6236dae4SAndroid Build Coastguard Worker   test_parse("user:password;options", "user", "password", "options");
109*6236dae4SAndroid Build Coastguard Worker   test_parse("user:password;options;more", "user", "password", "options;more");
110*6236dae4SAndroid Build Coastguard Worker   test_parse("", "", NULL, NULL);
111*6236dae4SAndroid Build Coastguard Worker   test_parse(":", "", "", NULL);
112*6236dae4SAndroid Build Coastguard Worker   test_parse(":;", "", "", NULL);
113*6236dae4SAndroid Build Coastguard Worker   test_parse(":password", "", "password", NULL);
114*6236dae4SAndroid Build Coastguard Worker   test_parse(":password;", "", "password", NULL);
115*6236dae4SAndroid Build Coastguard Worker   test_parse(";options", "", NULL, "options");
116*6236dae4SAndroid Build Coastguard Worker   test_parse("user;options", "user", NULL, "options");
117*6236dae4SAndroid Build Coastguard Worker   test_parse("user:;options", "user", "", "options");
118*6236dae4SAndroid Build Coastguard Worker   test_parse("user;options:password", "user", "password", "options");
119*6236dae4SAndroid Build Coastguard Worker   test_parse("user;options:", "user", "", "options");
120*6236dae4SAndroid Build Coastguard Worker 
121*6236dae4SAndroid Build Coastguard Worker   Curl_freeset(empty);
122*6236dae4SAndroid Build Coastguard Worker   for(i = (enum dupstring)0; i < STRING_LAST; i++) {
123*6236dae4SAndroid Build Coastguard Worker     fail_unless(empty->set.str[i] == NULL,
124*6236dae4SAndroid Build Coastguard Worker                 "Curl_free() did not set to NULL");
125*6236dae4SAndroid Build Coastguard Worker   }
126*6236dae4SAndroid Build Coastguard Worker 
127*6236dae4SAndroid Build Coastguard Worker   rc = Curl_close(&empty);
128*6236dae4SAndroid Build Coastguard Worker   fail_unless(rc == CURLE_OK, "Curl_close() failed");
129*6236dae4SAndroid Build Coastguard Worker 
130*6236dae4SAndroid Build Coastguard Worker }
131*6236dae4SAndroid Build Coastguard Worker UNITTEST_STOP
132