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