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