unit1304.c 8.1 KB

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