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