unit1304.c 6.7 KB

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