unit1304.c 6.1 KB

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