• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2020, 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  ***************************************************************************/
22 #include "curlcheck.h"
23 #include "netrc.h"
24 #include "memdebug.h" /* LAST include file */
25 
26 static char *login;
27 static char *password;
28 static char filename[64];
29 
unit_setup(void)30 static CURLcode unit_setup(void)
31 {
32   password = strdup("");
33   login = strdup("");
34   if(!password || !login) {
35     Curl_safefree(password);
36     Curl_safefree(login);
37     return CURLE_OUT_OF_MEMORY;
38   }
39   return CURLE_OK;
40 }
41 
unit_stop(void)42 static void unit_stop(void)
43 {
44   Curl_safefree(password);
45   Curl_safefree(login);
46 }
47 
48 UNITTEST_START
49   int result;
50   bool login_changed;
51   bool password_changed;
52 
53   static const char * const filename1 = "log/netrc1304";
54   memcpy(filename, filename1, strlen(filename1));
55 
56   /*
57    * Test a non existent host in our netrc file.
58    */
59   result = Curl_parsenetrc("test.example.com", &login, &password,
60              &login_changed, &password_changed, filename);
61   fail_unless(result == 1, "Host not found should return 1");
62   abort_unless(password != NULL, "returned NULL!");
63   fail_unless(password[0] == 0, "password should not have been changed");
64   abort_unless(login != NULL, "returned NULL!");
65   fail_unless(login[0] == 0, "login should not have been changed");
66 
67   /*
68    * Test a non existent login in our netrc file.
69    */
70   free(login);
71   login = strdup("me");
72   abort_unless(login != NULL, "returned NULL!");
73   result = Curl_parsenetrc("example.com", &login, &password,
74              &login_changed, &password_changed, filename);
75   fail_unless(result == 0, "Host should have been found");
76   abort_unless(password != NULL, "returned NULL!");
77   fail_unless(password[0] == 0, "password should not have been changed");
78   fail_unless(!password_changed, "password should not have been changed");
79   abort_unless(login != NULL, "returned NULL!");
80   fail_unless(strncmp(login, "me", 2) == 0,
81               "login should not have been changed");
82   fail_unless(!login_changed, "login should not have been changed");
83 
84   /*
85    * Test a non existent login and host in our netrc file.
86    */
87   free(login);
88   login = strdup("me");
89   abort_unless(login != NULL, "returned NULL!");
90   result = Curl_parsenetrc("test.example.com", &login, &password,
91              &login_changed, &password_changed, filename);
92   fail_unless(result == 1, "Host not found should return 1");
93   abort_unless(password != NULL, "returned NULL!");
94   fail_unless(password[0] == 0, "password should not have been changed");
95   abort_unless(login != NULL, "returned NULL!");
96   fail_unless(strncmp(login, "me", 2) == 0,
97               "login should not have been changed");
98 
99   /*
100    * Test a non existent login (substring of an existing one) in our
101    * netrc file.
102    */
103   free(login);
104   login = strdup("admi");
105   abort_unless(login != NULL, "returned NULL!");
106   result = Curl_parsenetrc("example.com", &login, &password,
107              &login_changed, &password_changed, filename);
108   fail_unless(result == 0, "Host should have been found");
109   abort_unless(password != NULL, "returned NULL!");
110   fail_unless(password[0] == 0, "password should not have been changed");
111   fail_unless(!password_changed, "password should not have been changed");
112   abort_unless(login != NULL, "returned NULL!");
113   fail_unless(strncmp(login, "admi", 4) == 0,
114               "login should not have been changed");
115   fail_unless(!login_changed, "login should not have been changed");
116 
117   /*
118    * Test a non existent login (superstring of an existing one)
119    * in our netrc file.
120    */
121   free(login);
122   login = strdup("adminn");
123   abort_unless(login != NULL, "returned NULL!");
124   result = Curl_parsenetrc("example.com", &login, &password,
125              &login_changed, &password_changed, filename);
126   fail_unless(result == 0, "Host should have been found");
127   abort_unless(password != NULL, "returned NULL!");
128   fail_unless(password[0] == 0, "password should not have been changed");
129   fail_unless(!password_changed, "password should not have been changed");
130   abort_unless(login != NULL, "returned NULL!");
131   fail_unless(strncmp(login, "adminn", 6) == 0,
132               "login should not have been changed");
133   fail_unless(!login_changed, "login should not have been changed");
134 
135   /*
136    * Test for the first existing host in our netrc file
137    * with login[0] = 0.
138    */
139   free(login);
140   login = strdup("");
141   abort_unless(login != NULL, "returned NULL!");
142   result = Curl_parsenetrc("example.com", &login, &password,
143              &login_changed, &password_changed, filename);
144   fail_unless(result == 0, "Host should have been found");
145   abort_unless(password != NULL, "returned NULL!");
146   fail_unless(strncmp(password, "passwd", 6) == 0,
147               "password should be 'passwd'");
148   fail_unless(password_changed, "password should have been changed");
149   abort_unless(login != NULL, "returned NULL!");
150   fail_unless(strncmp(login, "admin", 5) == 0, "login should be 'admin'");
151   fail_unless(login_changed, "login should have been changed");
152 
153   /*
154    * Test for the first existing host in our netrc file
155    * with login[0] != 0.
156    */
157   free(password);
158   password = strdup("");
159   abort_unless(password != NULL, "returned NULL!");
160   result = Curl_parsenetrc("example.com", &login, &password,
161              &login_changed, &password_changed, filename);
162   fail_unless(result == 0, "Host should have been found");
163   abort_unless(password != NULL, "returned NULL!");
164   fail_unless(strncmp(password, "passwd", 6) == 0,
165               "password should be 'passwd'");
166   fail_unless(password_changed, "password should have been changed");
167   abort_unless(login != NULL, "returned NULL!");
168   fail_unless(strncmp(login, "admin", 5) == 0, "login should be 'admin'");
169   fail_unless(!login_changed, "login should not have been changed");
170 
171   /*
172    * Test for the second existing host in our netrc file
173    * with login[0] = 0.
174    */
175   free(password);
176   password = strdup("");
177   abort_unless(password != NULL, "returned NULL!");
178   free(login);
179   login = strdup("");
180   abort_unless(login != NULL, "returned NULL!");
181   result = Curl_parsenetrc("curl.example.com", &login, &password,
182              &login_changed, &password_changed, filename);
183   fail_unless(result == 0, "Host should have been found");
184   abort_unless(password != NULL, "returned NULL!");
185   fail_unless(strncmp(password, "none", 4) == 0,
186               "password should be 'none'");
187   fail_unless(password_changed, "password should have been changed");
188   abort_unless(login != NULL, "returned NULL!");
189   fail_unless(strncmp(login, "none", 4) == 0, "login should be 'none'");
190   fail_unless(login_changed, "login should have been changed");
191 
192   /*
193    * Test for the second existing host in our netrc file
194    * with login[0] != 0.
195    */
196   free(password);
197   password = strdup("");
198   abort_unless(password != NULL, "returned NULL!");
199   result = Curl_parsenetrc("curl.example.com", &login, &password,
200              &login_changed, &password_changed, filename);
201   fail_unless(result == 0, "Host should have been found");
202   abort_unless(password != NULL, "returned NULL!");
203   fail_unless(strncmp(password, "none", 4) == 0,
204               "password should be 'none'");
205   fail_unless(password_changed, "password should have been changed");
206   abort_unless(login != NULL, "returned NULL!");
207   fail_unless(strncmp(login, "none", 4) == 0, "login should be 'none'");
208   fail_unless(!login_changed, "login should not have been changed");
209 
210 UNITTEST_STOP
211