unit1304.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2016, 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.haxx.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. static const char * const filename1 = "log/netrc1304";
  47. memcpy(filename, filename1, strlen(filename1));
  48. /*
  49. * Test a non existent host in our netrc file.
  50. */
  51. result = Curl_parsenetrc("test.example.com", &login, &password, filename);
  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, filename);
  64. fail_unless(result == 0, "Host should be 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, filename);
  77. fail_unless(result == 1, "Host should be found");
  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, filename);
  91. fail_unless(result == 0, "Host should be 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, filename);
  105. fail_unless(result == 0, "Host should be 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, filename);
  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, 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. 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, filename);
  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, filename);
  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. /* TODO:
  171. * Test over the size limit password / login!
  172. * Test files with a bad format
  173. */
  174. UNITTEST_STOP