unit1304.c 6.5 KB

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