blob: 3fab94b8525d68a351e209e96ad6a2d568605d7a [file] [log] [blame]
xf.li6c8fc1e2023-08-12 00:11:09 -07001/***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) 1998 - 2022, 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
28static char *login;
29static char *password;
30static char filename[64];
31
32static CURLcode unit_setup(void)
33{
34 password = strdup("");
35 login = strdup("");
36 if(!password || !login) {
37 Curl_safefree(password);
38 Curl_safefree(login);
39 return CURLE_OUT_OF_MEMORY;
40 }
41 return CURLE_OK;
42}
43
44static void unit_stop(void)
45{
46 Curl_safefree(password);
47 Curl_safefree(login);
48}
49
50UNITTEST_START
51 int result;
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 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 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 abort_unless(login != NULL, "returned NULL!");
79 fail_unless(strncmp(login, "me", 2) == 0,
80 "login should not have been changed");
81
82 /*
83 * Test a non existent login and host in our netrc file.
84 */
85 free(login);
86 login = strdup("me");
87 abort_unless(login != NULL, "returned NULL!");
88 result = Curl_parsenetrc("test.example.com", &login, &password,
89 filename);
90 fail_unless(result == 1, "Host not found should return 1");
91 abort_unless(password != NULL, "returned NULL!");
92 fail_unless(password[0] == 0, "password should not have been changed");
93 abort_unless(login != NULL, "returned NULL!");
94 fail_unless(strncmp(login, "me", 2) == 0,
95 "login should not have been changed");
96
97 /*
98 * Test a non existent login (substring of an existing one) in our
99 * netrc file.
100 */
101 free(login);
102 login = strdup("admi");
103 abort_unless(login != NULL, "returned NULL!");
104 result = Curl_parsenetrc("example.com", &login, &password,
105 filename);
106 fail_unless(result == 0, "Host should have been found");
107 abort_unless(password != NULL, "returned NULL!");
108 fail_unless(password[0] == 0, "password should not have been changed");
109 abort_unless(login != NULL, "returned NULL!");
110 fail_unless(strncmp(login, "admi", 4) == 0,
111 "login should not have been changed");
112
113 /*
114 * Test a non existent login (superstring of an existing one)
115 * in our netrc file.
116 */
117 free(login);
118 login = strdup("adminn");
119 abort_unless(login != NULL, "returned NULL!");
120 result = Curl_parsenetrc("example.com", &login, &password,
121 filename);
122 fail_unless(result == 0, "Host should have been found");
123 abort_unless(password != NULL, "returned NULL!");
124 fail_unless(password[0] == 0, "password should not have been changed");
125 abort_unless(login != NULL, "returned NULL!");
126 fail_unless(strncmp(login, "adminn", 6) == 0,
127 "login should not have been changed");
128
129 /*
130 * Test for the first existing host in our netrc file
131 * with login[0] = 0.
132 */
133 free(login);
134 login = strdup("");
135 abort_unless(login != NULL, "returned NULL!");
136 result = Curl_parsenetrc("example.com", &login, &password,
137 filename);
138 fail_unless(result == 0, "Host should have been found");
139 abort_unless(password != NULL, "returned NULL!");
140 fail_unless(strncmp(password, "passwd", 6) == 0,
141 "password should be 'passwd'");
142 abort_unless(login != NULL, "returned NULL!");
143 fail_unless(strncmp(login, "admin", 5) == 0, "login should be 'admin'");
144
145 /*
146 * Test for the first existing host in our netrc file
147 * with login[0] != 0.
148 */
149 free(password);
150 password = strdup("");
151 abort_unless(password != NULL, "returned NULL!");
152 result = Curl_parsenetrc("example.com", &login, &password, filename);
153 fail_unless(result == 0, "Host should have been found");
154 abort_unless(password != NULL, "returned NULL!");
155 fail_unless(strncmp(password, "passwd", 6) == 0,
156 "password should be 'passwd'");
157 abort_unless(login != NULL, "returned NULL!");
158 fail_unless(strncmp(login, "admin", 5) == 0, "login should be 'admin'");
159
160 /*
161 * Test for the second existing host in our netrc file
162 * with login[0] = 0.
163 */
164 free(password);
165 password = strdup("");
166 abort_unless(password != NULL, "returned NULL!");
167 free(login);
168 login = strdup("");
169 abort_unless(login != NULL, "returned NULL!");
170 result = Curl_parsenetrc("curl.example.com", &login, &password,
171 filename);
172 fail_unless(result == 0, "Host should have been found");
173 abort_unless(password != NULL, "returned NULL!");
174 fail_unless(strncmp(password, "none", 4) == 0,
175 "password should be 'none'");
176 abort_unless(login != NULL, "returned NULL!");
177 fail_unless(strncmp(login, "none", 4) == 0, "login should be 'none'");
178
179 /*
180 * Test for the second existing host in our netrc file
181 * with login[0] != 0.
182 */
183 free(password);
184 password = strdup("");
185 abort_unless(password != NULL, "returned NULL!");
186 result = Curl_parsenetrc("curl.example.com", &login, &password,
187 filename);
188 fail_unless(result == 0, "Host should have been found");
189 abort_unless(password != NULL, "returned NULL!");
190 fail_unless(strncmp(password, "none", 4) == 0,
191 "password should be 'none'");
192 abort_unless(login != NULL, "returned NULL!");
193 fail_unless(strncmp(login, "none", 4) == 0, "login should be 'none'");
194
195UNITTEST_STOP