1/***************************************************************************
2 *                                  _   _ ____  _
3 *  Project                     ___| | | |  _ \| |
4 *                             / __| | | | |_) | |
5 *                            | (__| |_| |  _ <| |___
6 *                             \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) 1998 - 2011, 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
24#include "netrc.h"
25
26static char login[LOGINSIZE];
27static char password[PASSWORDSIZE];
28static char filename[64];
29
30static CURLcode unit_setup(void)
31{
32  password[0] = 0;
33  login[0] = 0;
34  return CURLE_OK;
35}
36
37static void unit_stop(void)
38{
39}
40
41UNITTEST_START
42  int result;
43
44  static const char* filename1 = "log/netrc1304";
45  memcpy(filename, filename1, strlen(filename1));
46
47  /*
48   * Test a non existent host in our netrc file.
49   */
50  result = Curl_parsenetrc("test.example.com", login, password, filename);
51  fail_unless(result == 1, "Host not found should return 1");
52  fail_unless(password[0] == 0, "password should not have been changed");
53  fail_unless(login[0] == 0, "login should not have been changed");
54
55  /*
56   * Test a non existent login in our netrc file.
57   */
58  memcpy(login, "me", 2);
59  result = Curl_parsenetrc("example.com", login, password, filename);
60  fail_unless(result == 0, "Host should be found");
61  fail_unless(password[0] == 0, "password should not have been changed");
62  fail_unless(strncmp(login, "me", 2) == 0, "login should not have been changed");
63
64  /*
65   * Test a non existent login and host in our netrc file.
66   */
67  memcpy(login, "me", 2);
68  result = Curl_parsenetrc("test.example.com", login, password, filename);
69  fail_unless(result == 1, "Host should be found");
70  fail_unless(password[0] == 0, "password should not have been changed");
71  fail_unless(strncmp(login, "me", 2) == 0, "login should not have been changed");
72
73  /*
74   * Test a non existent login (substring of an existing one) in our
75   * netrc file.
76   */
77  memcpy(login, "admi", 4);
78  result = Curl_parsenetrc("example.com", login, password, filename);
79  fail_unless(result == 0, "Host should be found");
80  fail_unless(password[0] == 0, "password should not have been changed");
81  fail_unless(strncmp(login, "admi", 4) == 0, "login should not have been changed");
82
83  /*
84   * Test a non existent login (superstring of an existing one)
85   * in our netrc file.
86   */
87  memcpy(login, "adminn", 6);
88  result = Curl_parsenetrc("example.com", login, password, filename);
89  fail_unless(result == 0, "Host should be found");
90  fail_unless(password[0] == 0, "password should not have been changed");
91  fail_unless(strncmp(login, "adminn", 6) == 0, "login should not have been changed");
92
93  /*
94   * Test for the first existing host in our netrc file
95   * with login[0] = 0.
96   */
97  login[0] = 0;
98  result = Curl_parsenetrc("example.com", login, password, filename);
99  fail_unless(result == 0, "Host should have been found");
100  fail_unless(strncmp(password, "passwd", 6) == 0,
101              "password should be 'passwd'");
102  fail_unless(strncmp(login, "admin", 5) == 0, "login should be 'admin'");
103
104  /*
105   * Test for the first existing host in our netrc file
106   * with login[0] != 0.
107   */
108  password[0] = 0;
109  result = Curl_parsenetrc("example.com", login, password, filename);
110  fail_unless(result == 0, "Host should have been found");
111  fail_unless(strncmp(password, "passwd", 6) == 0,
112              "password should be 'passwd'");
113  fail_unless(strncmp(login, "admin", 5) == 0, "login should be 'admin'");
114
115  /*
116   * Test for the second existing host in our netrc file
117   * with login[0] = 0.
118   */
119  password[0] = 0;
120  login[0] = 0;
121  result = Curl_parsenetrc("curl.example.com", login, password, filename);
122  fail_unless(result == 0, "Host should have been found");
123  fail_unless(strncmp(password, "none", 4) == 0,
124              "password should be 'none'");
125  fail_unless(strncmp(login, "none", 4) == 0, "login should be 'none'");
126
127  /*
128   * Test for the second existing host in our netrc file
129   * with login[0] != 0.
130   */
131  password[0] = 0;
132  result = Curl_parsenetrc("curl.example.com", login, password, filename);
133  fail_unless(result == 0, "Host should have been found");
134  fail_unless(strncmp(password, "none", 4) == 0,
135              "password should be 'none'");
136  fail_unless(strncmp(login, "none", 4) == 0, "login should be 'none'");
137
138  /* TODO:
139   * Test over the size limit password / login!
140   * Test files with a bad format
141   */
142UNITTEST_STOP
143