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