unit1304.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2022, 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. 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. 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. 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. 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. abort_unless(login != NULL, "returned NULL!");
  72. fail_unless(strncmp(login, "me", 2) == 0,
  73. "login should not have been changed");
  74. /*
  75. * Test a non existent login and host in our netrc file.
  76. */
  77. free(login);
  78. login = strdup("me");
  79. abort_unless(login != NULL, "returned NULL!");
  80. result = Curl_parsenetrc("test.example.com", &login, &password,
  81. filename);
  82. fail_unless(result == 1, "Host not found should return 1");
  83. abort_unless(password != NULL, "returned NULL!");
  84. fail_unless(password[0] == 0, "password should not have been changed");
  85. abort_unless(login != NULL, "returned NULL!");
  86. fail_unless(strncmp(login, "me", 2) == 0,
  87. "login should not have been changed");
  88. /*
  89. * Test a non existent login (substring of an existing one) in our
  90. * netrc file.
  91. */
  92. free(login);
  93. login = strdup("admi");
  94. abort_unless(login != NULL, "returned NULL!");
  95. result = Curl_parsenetrc("example.com", &login, &password,
  96. filename);
  97. fail_unless(result == 0, "Host should have been found");
  98. abort_unless(password != NULL, "returned NULL!");
  99. fail_unless(password[0] == 0, "password should not have been changed");
  100. abort_unless(login != NULL, "returned NULL!");
  101. fail_unless(strncmp(login, "admi", 4) == 0,
  102. "login should not have been changed");
  103. /*
  104. * Test a non existent login (superstring of an existing one)
  105. * in our netrc file.
  106. */
  107. free(login);
  108. login = strdup("adminn");
  109. abort_unless(login != NULL, "returned NULL!");
  110. result = Curl_parsenetrc("example.com", &login, &password,
  111. filename);
  112. fail_unless(result == 0, "Host should have been found");
  113. abort_unless(password != NULL, "returned NULL!");
  114. fail_unless(password[0] == 0, "password should not have been changed");
  115. abort_unless(login != NULL, "returned NULL!");
  116. fail_unless(strncmp(login, "adminn", 6) == 0,
  117. "login should not have been changed");
  118. /*
  119. * Test for the first existing host in our netrc file
  120. * with login[0] = 0.
  121. */
  122. free(login);
  123. login = strdup("");
  124. abort_unless(login != NULL, "returned NULL!");
  125. result = Curl_parsenetrc("example.com", &login, &password,
  126. filename);
  127. fail_unless(result == 0, "Host should have been found");
  128. abort_unless(password != NULL, "returned NULL!");
  129. fail_unless(strncmp(password, "passwd", 6) == 0,
  130. "password should be 'passwd'");
  131. abort_unless(login != NULL, "returned NULL!");
  132. fail_unless(strncmp(login, "admin", 5) == 0, "login should be 'admin'");
  133. /*
  134. * Test for the first existing host in our netrc file
  135. * with login[0] != 0.
  136. */
  137. free(password);
  138. password = strdup("");
  139. abort_unless(password != NULL, "returned NULL!");
  140. result = Curl_parsenetrc("example.com", &login, &password, filename);
  141. fail_unless(result == 0, "Host should have been found");
  142. abort_unless(password != NULL, "returned NULL!");
  143. fail_unless(strncmp(password, "passwd", 6) == 0,
  144. "password should be 'passwd'");
  145. abort_unless(login != NULL, "returned NULL!");
  146. fail_unless(strncmp(login, "admin", 5) == 0, "login should be 'admin'");
  147. /*
  148. * Test for the second existing host in our netrc file
  149. * with login[0] = 0.
  150. */
  151. free(password);
  152. password = strdup("");
  153. abort_unless(password != NULL, "returned NULL!");
  154. free(login);
  155. login = strdup("");
  156. abort_unless(login != NULL, "returned NULL!");
  157. result = Curl_parsenetrc("curl.example.com", &login, &password,
  158. filename);
  159. fail_unless(result == 0, "Host should have been found");
  160. abort_unless(password != NULL, "returned NULL!");
  161. fail_unless(strncmp(password, "none", 4) == 0,
  162. "password should be 'none'");
  163. abort_unless(login != NULL, "returned NULL!");
  164. fail_unless(strncmp(login, "none", 4) == 0, "login should be 'none'");
  165. /*
  166. * Test for the second existing host in our netrc file
  167. * with login[0] != 0.
  168. */
  169. free(password);
  170. password = strdup("");
  171. abort_unless(password != NULL, "returned NULL!");
  172. result = Curl_parsenetrc("curl.example.com", &login, &password,
  173. filename);
  174. fail_unless(result == 0, "Host should have been found");
  175. abort_unless(password != NULL, "returned NULL!");
  176. fail_unless(strncmp(password, "none", 4) == 0,
  177. "password should be 'none'");
  178. abort_unless(login != NULL, "returned NULL!");
  179. fail_unless(strncmp(login, "none", 4) == 0, "login should be 'none'");
  180. UNITTEST_STOP