xref: /aosp_15_r20/external/curl/tests/unit/unit1304.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 #include "netrc.h"
26*6236dae4SAndroid Build Coastguard Worker #include "memdebug.h" /* LAST include file */
27*6236dae4SAndroid Build Coastguard Worker 
28*6236dae4SAndroid Build Coastguard Worker #ifndef CURL_DISABLE_NETRC
29*6236dae4SAndroid Build Coastguard Worker 
30*6236dae4SAndroid Build Coastguard Worker static char *s_login;
31*6236dae4SAndroid Build Coastguard Worker static char *s_password;
32*6236dae4SAndroid Build Coastguard Worker 
unit_setup(void)33*6236dae4SAndroid Build Coastguard Worker static CURLcode unit_setup(void)
34*6236dae4SAndroid Build Coastguard Worker {
35*6236dae4SAndroid Build Coastguard Worker   s_password = strdup("");
36*6236dae4SAndroid Build Coastguard Worker   s_login = strdup("");
37*6236dae4SAndroid Build Coastguard Worker   if(!s_password || !s_login) {
38*6236dae4SAndroid Build Coastguard Worker     Curl_safefree(s_password);
39*6236dae4SAndroid Build Coastguard Worker     Curl_safefree(s_login);
40*6236dae4SAndroid Build Coastguard Worker     return CURLE_OUT_OF_MEMORY;
41*6236dae4SAndroid Build Coastguard Worker   }
42*6236dae4SAndroid Build Coastguard Worker   return CURLE_OK;
43*6236dae4SAndroid Build Coastguard Worker }
44*6236dae4SAndroid Build Coastguard Worker 
unit_stop(void)45*6236dae4SAndroid Build Coastguard Worker static void unit_stop(void)
46*6236dae4SAndroid Build Coastguard Worker {
47*6236dae4SAndroid Build Coastguard Worker   Curl_safefree(s_password);
48*6236dae4SAndroid Build Coastguard Worker   Curl_safefree(s_login);
49*6236dae4SAndroid Build Coastguard Worker }
50*6236dae4SAndroid Build Coastguard Worker 
51*6236dae4SAndroid Build Coastguard Worker UNITTEST_START
52*6236dae4SAndroid Build Coastguard Worker {
53*6236dae4SAndroid Build Coastguard Worker   int result;
54*6236dae4SAndroid Build Coastguard Worker   struct store_netrc store;
55*6236dae4SAndroid Build Coastguard Worker 
56*6236dae4SAndroid Build Coastguard Worker   /*
57*6236dae4SAndroid Build Coastguard Worker    * Test a non existent host in our netrc file.
58*6236dae4SAndroid Build Coastguard Worker    */
59*6236dae4SAndroid Build Coastguard Worker   Curl_netrc_init(&store);
60*6236dae4SAndroid Build Coastguard Worker   result = Curl_parsenetrc(&store,
61*6236dae4SAndroid Build Coastguard Worker                            "test.example.com", &s_login, &s_password, arg);
62*6236dae4SAndroid Build Coastguard Worker   fail_unless(result == 1, "Host not found should return 1");
63*6236dae4SAndroid Build Coastguard Worker   abort_unless(s_password != NULL, "returned NULL!");
64*6236dae4SAndroid Build Coastguard Worker   fail_unless(s_password[0] == 0, "password should not have been changed");
65*6236dae4SAndroid Build Coastguard Worker   abort_unless(s_login != NULL, "returned NULL!");
66*6236dae4SAndroid Build Coastguard Worker   fail_unless(s_login[0] == 0, "login should not have been changed");
67*6236dae4SAndroid Build Coastguard Worker   Curl_netrc_cleanup(&store);
68*6236dae4SAndroid Build Coastguard Worker 
69*6236dae4SAndroid Build Coastguard Worker   /*
70*6236dae4SAndroid Build Coastguard Worker    * Test a non existent login in our netrc file.
71*6236dae4SAndroid Build Coastguard Worker    */
72*6236dae4SAndroid Build Coastguard Worker   free(s_login);
73*6236dae4SAndroid Build Coastguard Worker   s_login = strdup("me");
74*6236dae4SAndroid Build Coastguard Worker   abort_unless(s_login != NULL, "returned NULL!");
75*6236dae4SAndroid Build Coastguard Worker   Curl_netrc_init(&store);
76*6236dae4SAndroid Build Coastguard Worker   result = Curl_parsenetrc(&store,
77*6236dae4SAndroid Build Coastguard Worker                            "example.com", &s_login, &s_password, arg);
78*6236dae4SAndroid Build Coastguard Worker   fail_unless(result == 0, "Host should have been found");
79*6236dae4SAndroid Build Coastguard Worker   abort_unless(s_password != NULL, "returned NULL!");
80*6236dae4SAndroid Build Coastguard Worker   fail_unless(s_password[0] == 0, "password should not have been changed");
81*6236dae4SAndroid Build Coastguard Worker   abort_unless(s_login != NULL, "returned NULL!");
82*6236dae4SAndroid Build Coastguard Worker   fail_unless(strncmp(s_login, "me", 2) == 0,
83*6236dae4SAndroid Build Coastguard Worker               "login should not have been changed");
84*6236dae4SAndroid Build Coastguard Worker   Curl_netrc_cleanup(&store);
85*6236dae4SAndroid Build Coastguard Worker 
86*6236dae4SAndroid Build Coastguard Worker   /*
87*6236dae4SAndroid Build Coastguard Worker    * Test a non existent login and host in our netrc file.
88*6236dae4SAndroid Build Coastguard Worker    */
89*6236dae4SAndroid Build Coastguard Worker   free(s_login);
90*6236dae4SAndroid Build Coastguard Worker   s_login = strdup("me");
91*6236dae4SAndroid Build Coastguard Worker   abort_unless(s_login != NULL, "returned NULL!");
92*6236dae4SAndroid Build Coastguard Worker   Curl_netrc_init(&store);
93*6236dae4SAndroid Build Coastguard Worker   result = Curl_parsenetrc(&store,
94*6236dae4SAndroid Build Coastguard Worker                            "test.example.com", &s_login, &s_password, arg);
95*6236dae4SAndroid Build Coastguard Worker   fail_unless(result == 1, "Host not found should return 1");
96*6236dae4SAndroid Build Coastguard Worker   abort_unless(s_password != NULL, "returned NULL!");
97*6236dae4SAndroid Build Coastguard Worker   fail_unless(s_password[0] == 0, "password should not have been changed");
98*6236dae4SAndroid Build Coastguard Worker   abort_unless(s_login != NULL, "returned NULL!");
99*6236dae4SAndroid Build Coastguard Worker   fail_unless(strncmp(s_login, "me", 2) == 0,
100*6236dae4SAndroid Build Coastguard Worker               "login should not have been changed");
101*6236dae4SAndroid Build Coastguard Worker   Curl_netrc_cleanup(&store);
102*6236dae4SAndroid Build Coastguard Worker 
103*6236dae4SAndroid Build Coastguard Worker   /*
104*6236dae4SAndroid Build Coastguard Worker    * Test a non existent login (substring of an existing one) in our
105*6236dae4SAndroid Build Coastguard Worker    * netrc file.
106*6236dae4SAndroid Build Coastguard Worker    */
107*6236dae4SAndroid Build Coastguard Worker   free(s_login);
108*6236dae4SAndroid Build Coastguard Worker   s_login = strdup("admi");
109*6236dae4SAndroid Build Coastguard Worker   abort_unless(s_login != NULL, "returned NULL!");
110*6236dae4SAndroid Build Coastguard Worker   Curl_netrc_init(&store);
111*6236dae4SAndroid Build Coastguard Worker   result = Curl_parsenetrc(&store,
112*6236dae4SAndroid Build Coastguard Worker                            "example.com", &s_login, &s_password, arg);
113*6236dae4SAndroid Build Coastguard Worker   fail_unless(result == 0, "Host should have been found");
114*6236dae4SAndroid Build Coastguard Worker   abort_unless(s_password != NULL, "returned NULL!");
115*6236dae4SAndroid Build Coastguard Worker   fail_unless(s_password[0] == 0, "password should not have been changed");
116*6236dae4SAndroid Build Coastguard Worker   abort_unless(s_login != NULL, "returned NULL!");
117*6236dae4SAndroid Build Coastguard Worker   fail_unless(strncmp(s_login, "admi", 4) == 0,
118*6236dae4SAndroid Build Coastguard Worker               "login should not have been changed");
119*6236dae4SAndroid Build Coastguard Worker   Curl_netrc_cleanup(&store);
120*6236dae4SAndroid Build Coastguard Worker 
121*6236dae4SAndroid Build Coastguard Worker   /*
122*6236dae4SAndroid Build Coastguard Worker    * Test a non existent login (superstring of an existing one)
123*6236dae4SAndroid Build Coastguard Worker    * in our netrc file.
124*6236dae4SAndroid Build Coastguard Worker    */
125*6236dae4SAndroid Build Coastguard Worker   free(s_login);
126*6236dae4SAndroid Build Coastguard Worker   s_login = strdup("adminn");
127*6236dae4SAndroid Build Coastguard Worker   abort_unless(s_login != NULL, "returned NULL!");
128*6236dae4SAndroid Build Coastguard Worker   Curl_netrc_init(&store);
129*6236dae4SAndroid Build Coastguard Worker   result = Curl_parsenetrc(&store,
130*6236dae4SAndroid Build Coastguard Worker                            "example.com", &s_login, &s_password, arg);
131*6236dae4SAndroid Build Coastguard Worker   fail_unless(result == 0, "Host should have been found");
132*6236dae4SAndroid Build Coastguard Worker   abort_unless(s_password != NULL, "returned NULL!");
133*6236dae4SAndroid Build Coastguard Worker   fail_unless(s_password[0] == 0, "password should not have been changed");
134*6236dae4SAndroid Build Coastguard Worker   abort_unless(s_login != NULL, "returned NULL!");
135*6236dae4SAndroid Build Coastguard Worker   fail_unless(strncmp(s_login, "adminn", 6) == 0,
136*6236dae4SAndroid Build Coastguard Worker               "login should not have been changed");
137*6236dae4SAndroid Build Coastguard Worker   Curl_netrc_cleanup(&store);
138*6236dae4SAndroid Build Coastguard Worker 
139*6236dae4SAndroid Build Coastguard Worker   /*
140*6236dae4SAndroid Build Coastguard Worker    * Test for the first existing host in our netrc file
141*6236dae4SAndroid Build Coastguard Worker    * with s_login[0] = 0.
142*6236dae4SAndroid Build Coastguard Worker    */
143*6236dae4SAndroid Build Coastguard Worker   free(s_login);
144*6236dae4SAndroid Build Coastguard Worker   s_login = strdup("");
145*6236dae4SAndroid Build Coastguard Worker   abort_unless(s_login != NULL, "returned NULL!");
146*6236dae4SAndroid Build Coastguard Worker   Curl_netrc_init(&store);
147*6236dae4SAndroid Build Coastguard Worker   result = Curl_parsenetrc(&store,
148*6236dae4SAndroid Build Coastguard Worker                            "example.com", &s_login, &s_password, arg);
149*6236dae4SAndroid Build Coastguard Worker   fail_unless(result == 0, "Host should have been found");
150*6236dae4SAndroid Build Coastguard Worker   abort_unless(s_password != NULL, "returned NULL!");
151*6236dae4SAndroid Build Coastguard Worker   fail_unless(strncmp(s_password, "passwd", 6) == 0,
152*6236dae4SAndroid Build Coastguard Worker               "password should be 'passwd'");
153*6236dae4SAndroid Build Coastguard Worker   abort_unless(s_login != NULL, "returned NULL!");
154*6236dae4SAndroid Build Coastguard Worker   fail_unless(strncmp(s_login, "admin", 5) == 0, "login should be 'admin'");
155*6236dae4SAndroid Build Coastguard Worker   Curl_netrc_cleanup(&store);
156*6236dae4SAndroid Build Coastguard Worker 
157*6236dae4SAndroid Build Coastguard Worker   /*
158*6236dae4SAndroid Build Coastguard Worker    * Test for the first existing host in our netrc file
159*6236dae4SAndroid Build Coastguard Worker    * with s_login[0] != 0.
160*6236dae4SAndroid Build Coastguard Worker    */
161*6236dae4SAndroid Build Coastguard Worker   free(s_password);
162*6236dae4SAndroid Build Coastguard Worker   s_password = strdup("");
163*6236dae4SAndroid Build Coastguard Worker   abort_unless(s_password != NULL, "returned NULL!");
164*6236dae4SAndroid Build Coastguard Worker   Curl_netrc_init(&store);
165*6236dae4SAndroid Build Coastguard Worker   result = Curl_parsenetrc(&store,
166*6236dae4SAndroid Build Coastguard Worker                            "example.com", &s_login, &s_password, arg);
167*6236dae4SAndroid Build Coastguard Worker   fail_unless(result == 0, "Host should have been found");
168*6236dae4SAndroid Build Coastguard Worker   abort_unless(s_password != NULL, "returned NULL!");
169*6236dae4SAndroid Build Coastguard Worker   fail_unless(strncmp(s_password, "passwd", 6) == 0,
170*6236dae4SAndroid Build Coastguard Worker               "password should be 'passwd'");
171*6236dae4SAndroid Build Coastguard Worker   abort_unless(s_login != NULL, "returned NULL!");
172*6236dae4SAndroid Build Coastguard Worker   fail_unless(strncmp(s_login, "admin", 5) == 0, "login should be 'admin'");
173*6236dae4SAndroid Build Coastguard Worker   Curl_netrc_cleanup(&store);
174*6236dae4SAndroid Build Coastguard Worker 
175*6236dae4SAndroid Build Coastguard Worker   /*
176*6236dae4SAndroid Build Coastguard Worker    * Test for the second existing host in our netrc file
177*6236dae4SAndroid Build Coastguard Worker    * with s_login[0] = 0.
178*6236dae4SAndroid Build Coastguard Worker    */
179*6236dae4SAndroid Build Coastguard Worker   free(s_password);
180*6236dae4SAndroid Build Coastguard Worker   s_password = strdup("");
181*6236dae4SAndroid Build Coastguard Worker   abort_unless(s_password != NULL, "returned NULL!");
182*6236dae4SAndroid Build Coastguard Worker   free(s_login);
183*6236dae4SAndroid Build Coastguard Worker   s_login = strdup("");
184*6236dae4SAndroid Build Coastguard Worker   abort_unless(s_login != NULL, "returned NULL!");
185*6236dae4SAndroid Build Coastguard Worker   Curl_netrc_init(&store);
186*6236dae4SAndroid Build Coastguard Worker   result = Curl_parsenetrc(&store,
187*6236dae4SAndroid Build Coastguard Worker                            "curl.example.com", &s_login, &s_password, arg);
188*6236dae4SAndroid Build Coastguard Worker   fail_unless(result == 0, "Host should have been found");
189*6236dae4SAndroid Build Coastguard Worker   abort_unless(s_password != NULL, "returned NULL!");
190*6236dae4SAndroid Build Coastguard Worker   fail_unless(strncmp(s_password, "none", 4) == 0,
191*6236dae4SAndroid Build Coastguard Worker               "password should be 'none'");
192*6236dae4SAndroid Build Coastguard Worker   abort_unless(s_login != NULL, "returned NULL!");
193*6236dae4SAndroid Build Coastguard Worker   fail_unless(strncmp(s_login, "none", 4) == 0, "login should be 'none'");
194*6236dae4SAndroid Build Coastguard Worker   Curl_netrc_cleanup(&store);
195*6236dae4SAndroid Build Coastguard Worker 
196*6236dae4SAndroid Build Coastguard Worker   /*
197*6236dae4SAndroid Build Coastguard Worker    * Test for the second existing host in our netrc file
198*6236dae4SAndroid Build Coastguard Worker    * with s_login[0] != 0.
199*6236dae4SAndroid Build Coastguard Worker    */
200*6236dae4SAndroid Build Coastguard Worker   free(s_password);
201*6236dae4SAndroid Build Coastguard Worker   s_password = strdup("");
202*6236dae4SAndroid Build Coastguard Worker   abort_unless(s_password != NULL, "returned NULL!");
203*6236dae4SAndroid Build Coastguard Worker   Curl_netrc_init(&store);
204*6236dae4SAndroid Build Coastguard Worker   result = Curl_parsenetrc(&store,
205*6236dae4SAndroid Build Coastguard Worker                            "curl.example.com", &s_login, &s_password, arg);
206*6236dae4SAndroid Build Coastguard Worker   fail_unless(result == 0, "Host should have been found");
207*6236dae4SAndroid Build Coastguard Worker   abort_unless(s_password != NULL, "returned NULL!");
208*6236dae4SAndroid Build Coastguard Worker   fail_unless(strncmp(s_password, "none", 4) == 0,
209*6236dae4SAndroid Build Coastguard Worker               "password should be 'none'");
210*6236dae4SAndroid Build Coastguard Worker   abort_unless(s_login != NULL, "returned NULL!");
211*6236dae4SAndroid Build Coastguard Worker   fail_unless(strncmp(s_login, "none", 4) == 0, "login should be 'none'");
212*6236dae4SAndroid Build Coastguard Worker   Curl_netrc_cleanup(&store);
213*6236dae4SAndroid Build Coastguard Worker 
214*6236dae4SAndroid Build Coastguard Worker }
215*6236dae4SAndroid Build Coastguard Worker UNITTEST_STOP
216*6236dae4SAndroid Build Coastguard Worker 
217*6236dae4SAndroid Build Coastguard Worker #else
218*6236dae4SAndroid Build Coastguard Worker static CURLcode unit_setup(void)
219*6236dae4SAndroid Build Coastguard Worker {
220*6236dae4SAndroid Build Coastguard Worker   return CURLE_OK;
221*6236dae4SAndroid Build Coastguard Worker }
222*6236dae4SAndroid Build Coastguard Worker static void unit_stop(void)
223*6236dae4SAndroid Build Coastguard Worker {
224*6236dae4SAndroid Build Coastguard Worker }
225*6236dae4SAndroid Build Coastguard Worker UNITTEST_START
226*6236dae4SAndroid Build Coastguard Worker UNITTEST_STOP
227*6236dae4SAndroid Build Coastguard Worker 
228*6236dae4SAndroid Build Coastguard Worker #endif
229