unit1304.c 7.7 KB

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