unit1304.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2022, Daniel Stenberg, <[email protected]>, 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. static char *login;
  28. static char *password;
  29. static char filename[64];
  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. static void unit_stop(void)
  42. {
  43. Curl_safefree(password);
  44. Curl_safefree(login);
  45. }
  46. UNITTEST_START
  47. int result;
  48. bool login_changed;
  49. bool password_changed;
  50. static const char * const filename1 = "log/netrc1304";
  51. memcpy(filename, filename1, strlen(filename1));
  52. /*
  53. * Test a non existent host in our netrc file.
  54. */
  55. result = Curl_parsenetrc("test.example.com", &login, &password,
  56. &login_changed, &password_changed, filename);
  57. fail_unless(result == 1, "Host not found should return 1");
  58. abort_unless(password != NULL, "returned NULL!");
  59. fail_unless(password[0] == 0, "password should not have been changed");
  60. abort_unless(login != NULL, "returned NULL!");
  61. fail_unless(login[0] == 0, "login should not have been changed");
  62. /*
  63. * Test a non existent login in our netrc file.
  64. */
  65. free(login);
  66. login = strdup("me");
  67. abort_unless(login != NULL, "returned NULL!");
  68. result = Curl_parsenetrc("example.com", &login, &password,
  69. &login_changed, &password_changed, filename);
  70. fail_unless(result == 0, "Host should have been found");
  71. abort_unless(password != NULL, "returned NULL!");
  72. fail_unless(password[0] == 0, "password should not have been changed");
  73. fail_unless(!password_changed, "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. fail_unless(!login_changed, "login should not have been changed");
  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,
  85. &login_changed, &password_changed, filename);
  86. fail_unless(result == 1, "Host not found should return 1");
  87. abort_unless(password != NULL, "returned NULL!");
  88. fail_unless(password[0] == 0, "password should not have been changed");
  89. abort_unless(login != NULL, "returned NULL!");
  90. fail_unless(strncmp(login, "me", 2) == 0,
  91. "login should not have been changed");
  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,
  100. &login_changed, &password_changed, filename);
  101. fail_unless(result == 0, "Host should have been found");
  102. abort_unless(password != NULL, "returned NULL!");
  103. fail_unless(password[0] == 0, "password should not have been changed");
  104. fail_unless(!password_changed, "password should not have been changed");
  105. abort_unless(login != NULL, "returned NULL!");
  106. fail_unless(strncmp(login, "admi", 4) == 0,
  107. "login should not have been changed");
  108. fail_unless(!login_changed, "login should not have been changed");
  109. /*
  110. * Test a non existent login (superstring of an existing one)
  111. * in our netrc file.
  112. */
  113. free(login);
  114. login = strdup("adminn");
  115. abort_unless(login != NULL, "returned NULL!");
  116. result = Curl_parsenetrc("example.com", &login, &password,
  117. &login_changed, &password_changed, filename);
  118. fail_unless(result == 0, "Host should have been found");
  119. abort_unless(password != NULL, "returned NULL!");
  120. fail_unless(password[0] == 0, "password should not have been changed");
  121. fail_unless(!password_changed, "password should not have been changed");
  122. abort_unless(login != NULL, "returned NULL!");
  123. fail_unless(strncmp(login, "adminn", 6) == 0,
  124. "login should not have been changed");
  125. fail_unless(!login_changed, "login should not have been changed");
  126. /*
  127. * Test for the first existing host in our netrc file
  128. * with login[0] = 0.
  129. */
  130. free(login);
  131. login = strdup("");
  132. abort_unless(login != NULL, "returned NULL!");
  133. result = Curl_parsenetrc("example.com", &login, &password,
  134. &login_changed, &password_changed, filename);
  135. fail_unless(result == 0, "Host should have been found");
  136. abort_unless(password != NULL, "returned NULL!");
  137. fail_unless(strncmp(password, "passwd", 6) == 0,
  138. "password should be 'passwd'");
  139. fail_unless(password_changed, "password should have been changed");
  140. abort_unless(login != NULL, "returned NULL!");
  141. fail_unless(strncmp(login, "admin", 5) == 0, "login should be 'admin'");
  142. fail_unless(login_changed, "login should have been changed");
  143. /*
  144. * Test for the first existing host in our netrc file
  145. * with login[0] != 0.
  146. */
  147. free(password);
  148. password = strdup("");
  149. abort_unless(password != NULL, "returned NULL!");
  150. result = Curl_parsenetrc("example.com", &login, &password,
  151. &login_changed, &password_changed, filename);
  152. fail_unless(result == 0, "Host should have been found");
  153. abort_unless(password != NULL, "returned NULL!");
  154. fail_unless(strncmp(password, "passwd", 6) == 0,
  155. "password should be 'passwd'");
  156. fail_unless(password_changed, "password should have been changed");
  157. abort_unless(login != NULL, "returned NULL!");
  158. fail_unless(strncmp(login, "admin", 5) == 0, "login should be 'admin'");
  159. fail_unless(!login_changed, "login should not have been changed");
  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. &login_changed, &password_changed, 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. fail_unless(password_changed, "password should have been changed");
  177. abort_unless(login != NULL, "returned NULL!");
  178. fail_unless(strncmp(login, "none", 4) == 0, "login should be 'none'");
  179. fail_unless(login_changed, "login should have been changed");
  180. /*
  181. * Test for the second existing host in our netrc file
  182. * with login[0] != 0.
  183. */
  184. free(password);
  185. password = strdup("");
  186. abort_unless(password != NULL, "returned NULL!");
  187. result = Curl_parsenetrc("curl.example.com", &login, &password,
  188. &login_changed, &password_changed, filename);
  189. fail_unless(result == 0, "Host should have been found");
  190. abort_unless(password != NULL, "returned NULL!");
  191. fail_unless(strncmp(password, "none", 4) == 0,
  192. "password should be 'none'");
  193. fail_unless(password_changed, "password should have been changed");
  194. abort_unless(login != NULL, "returned NULL!");
  195. fail_unless(strncmp(login, "none", 4) == 0, "login should be 'none'");
  196. fail_unless(!login_changed, "login should not have been changed");
  197. UNITTEST_STOP