netrc: cache the netrc file in memory

So that on redirects etc it does not reread the file but just parses it
again.

Reported-by: Pierre-Etienne Meunier
Fixes #15248
Closes #15259
This commit is contained in:
Daniel Stenberg 2024-10-10 18:08:07 +02:00
parent 962097b8dd
commit 3b43a05e00
No known key found for this signature in database
GPG key ID: 5CC908FDB71E12C2
6 changed files with 298 additions and 194 deletions

View file

@ -49,17 +49,22 @@ static void unit_stop(void)
}
UNITTEST_START
{
int result;
struct store_netrc store;
/*
* Test a non existent host in our netrc file.
*/
result = Curl_parsenetrc("test.example.com", &s_login, &s_password, arg);
Curl_netrc_init(&store);
result = Curl_parsenetrc(&store,
"test.example.com", &s_login, &s_password, arg);
fail_unless(result == 1, "Host not found should return 1");
abort_unless(s_password != NULL, "returned NULL!");
fail_unless(s_password[0] == 0, "password should not have been changed");
abort_unless(s_login != NULL, "returned NULL!");
fail_unless(s_login[0] == 0, "login should not have been changed");
Curl_netrc_cleanup(&store);
/*
* Test a non existent login in our netrc file.
@ -67,13 +72,16 @@ UNITTEST_START
free(s_login);
s_login = strdup("me");
abort_unless(s_login != NULL, "returned NULL!");
result = Curl_parsenetrc("example.com", &s_login, &s_password, arg);
Curl_netrc_init(&store);
result = Curl_parsenetrc(&store,
"example.com", &s_login, &s_password, arg);
fail_unless(result == 0, "Host should have been found");
abort_unless(s_password != NULL, "returned NULL!");
fail_unless(s_password[0] == 0, "password should not have been changed");
abort_unless(s_login != NULL, "returned NULL!");
fail_unless(strncmp(s_login, "me", 2) == 0,
"login should not have been changed");
Curl_netrc_cleanup(&store);
/*
* Test a non existent login and host in our netrc file.
@ -81,13 +89,16 @@ UNITTEST_START
free(s_login);
s_login = strdup("me");
abort_unless(s_login != NULL, "returned NULL!");
result = Curl_parsenetrc("test.example.com", &s_login, &s_password, arg);
Curl_netrc_init(&store);
result = Curl_parsenetrc(&store,
"test.example.com", &s_login, &s_password, arg);
fail_unless(result == 1, "Host not found should return 1");
abort_unless(s_password != NULL, "returned NULL!");
fail_unless(s_password[0] == 0, "password should not have been changed");
abort_unless(s_login != NULL, "returned NULL!");
fail_unless(strncmp(s_login, "me", 2) == 0,
"login should not have been changed");
Curl_netrc_cleanup(&store);
/*
* Test a non existent login (substring of an existing one) in our
@ -96,13 +107,16 @@ UNITTEST_START
free(s_login);
s_login = strdup("admi");
abort_unless(s_login != NULL, "returned NULL!");
result = Curl_parsenetrc("example.com", &s_login, &s_password, arg);
Curl_netrc_init(&store);
result = Curl_parsenetrc(&store,
"example.com", &s_login, &s_password, arg);
fail_unless(result == 0, "Host should have been found");
abort_unless(s_password != NULL, "returned NULL!");
fail_unless(s_password[0] == 0, "password should not have been changed");
abort_unless(s_login != NULL, "returned NULL!");
fail_unless(strncmp(s_login, "admi", 4) == 0,
"login should not have been changed");
Curl_netrc_cleanup(&store);
/*
* Test a non existent login (superstring of an existing one)
@ -111,13 +125,16 @@ UNITTEST_START
free(s_login);
s_login = strdup("adminn");
abort_unless(s_login != NULL, "returned NULL!");
result = Curl_parsenetrc("example.com", &s_login, &s_password, arg);
Curl_netrc_init(&store);
result = Curl_parsenetrc(&store,
"example.com", &s_login, &s_password, arg);
fail_unless(result == 0, "Host should have been found");
abort_unless(s_password != NULL, "returned NULL!");
fail_unless(s_password[0] == 0, "password should not have been changed");
abort_unless(s_login != NULL, "returned NULL!");
fail_unless(strncmp(s_login, "adminn", 6) == 0,
"login should not have been changed");
Curl_netrc_cleanup(&store);
/*
* Test for the first existing host in our netrc file
@ -126,13 +143,16 @@ UNITTEST_START
free(s_login);
s_login = strdup("");
abort_unless(s_login != NULL, "returned NULL!");
result = Curl_parsenetrc("example.com", &s_login, &s_password, arg);
Curl_netrc_init(&store);
result = Curl_parsenetrc(&store,
"example.com", &s_login, &s_password, arg);
fail_unless(result == 0, "Host should have been found");
abort_unless(s_password != NULL, "returned NULL!");
fail_unless(strncmp(s_password, "passwd", 6) == 0,
"password should be 'passwd'");
abort_unless(s_login != NULL, "returned NULL!");
fail_unless(strncmp(s_login, "admin", 5) == 0, "login should be 'admin'");
Curl_netrc_cleanup(&store);
/*
* Test for the first existing host in our netrc file
@ -141,13 +161,16 @@ UNITTEST_START
free(s_password);
s_password = strdup("");
abort_unless(s_password != NULL, "returned NULL!");
result = Curl_parsenetrc("example.com", &s_login, &s_password, arg);
Curl_netrc_init(&store);
result = Curl_parsenetrc(&store,
"example.com", &s_login, &s_password, arg);
fail_unless(result == 0, "Host should have been found");
abort_unless(s_password != NULL, "returned NULL!");
fail_unless(strncmp(s_password, "passwd", 6) == 0,
"password should be 'passwd'");
abort_unless(s_login != NULL, "returned NULL!");
fail_unless(strncmp(s_login, "admin", 5) == 0, "login should be 'admin'");
Curl_netrc_cleanup(&store);
/*
* Test for the second existing host in our netrc file
@ -159,13 +182,16 @@ UNITTEST_START
free(s_login);
s_login = strdup("");
abort_unless(s_login != NULL, "returned NULL!");
result = Curl_parsenetrc("curl.example.com", &s_login, &s_password, arg);
Curl_netrc_init(&store);
result = Curl_parsenetrc(&store,
"curl.example.com", &s_login, &s_password, arg);
fail_unless(result == 0, "Host should have been found");
abort_unless(s_password != NULL, "returned NULL!");
fail_unless(strncmp(s_password, "none", 4) == 0,
"password should be 'none'");
abort_unless(s_login != NULL, "returned NULL!");
fail_unless(strncmp(s_login, "none", 4) == 0, "login should be 'none'");
Curl_netrc_cleanup(&store);
/*
* Test for the second existing host in our netrc file
@ -174,14 +200,18 @@ UNITTEST_START
free(s_password);
s_password = strdup("");
abort_unless(s_password != NULL, "returned NULL!");
result = Curl_parsenetrc("curl.example.com", &s_login, &s_password, arg);
Curl_netrc_init(&store);
result = Curl_parsenetrc(&store,
"curl.example.com", &s_login, &s_password, arg);
fail_unless(result == 0, "Host should have been found");
abort_unless(s_password != NULL, "returned NULL!");
fail_unless(strncmp(s_password, "none", 4) == 0,
"password should be 'none'");
abort_unless(s_login != NULL, "returned NULL!");
fail_unless(strncmp(s_login, "none", 4) == 0, "login should be 'none'");
Curl_netrc_cleanup(&store);
}
UNITTEST_STOP
#else