unit1304.c 6.7 KB

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