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