summaryrefslogtreecommitdiffstats
path: root/test/libapt/authconf_test.cc
blob: a7b2f2c98f56f745e5f62c8d2c96d622aeac6889 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#include <config.h>

#include <apt-pkg/fileutl.h>
#include <apt-pkg/netrc.h>
#include <apt-pkg/strutl.h>

#include <string>

#include <gtest/gtest.h>

#include "file-helpers.h"

TEST(NetRCTest, Parsing)
{
   FileFd fd;
   URI U("http://file.not/open");
   EXPECT_FALSE(MaybeAddAuth(fd, U));
   EXPECT_TRUE(U.User.empty());
   EXPECT_TRUE(U.Password.empty());
   EXPECT_EQ("file.not", U.Host);
   EXPECT_EQ("/open", U.Path);

   createTemporaryFile("doublesignedfile", fd, nullptr, R"apt(
machine example.netter login bar password foo
machine example.net login foo password bar

machine example.org:90 login apt password apt
machine	example.org:8080
login
example	password 	 foobar

machine example.org
login anonymous
password pass

machine example.com/foo login user1 unknown token password pass1
machine example.com/bar password pass2 login user2
		  unknown token
machine example.com/user login user
machine example.netter login unused password firstentry
machine example.last/debian login debian password rules)apt");
   U = URI("http://example.net/foo");
   EXPECT_TRUE(MaybeAddAuth(fd, U));
   EXPECT_EQ("foo", U.User);
   EXPECT_EQ("bar", U.Password);
   EXPECT_EQ("example.net", U.Host);
   EXPECT_EQ("/foo", U.Path);

   EXPECT_TRUE(fd.Seek(0));
   U = URI("http://user:pass@example.net/foo");
   EXPECT_TRUE(MaybeAddAuth(fd, U));
   EXPECT_EQ("user", U.User);
   EXPECT_EQ("pass", U.Password);
   EXPECT_EQ("example.net", U.Host);
   EXPECT_EQ("/foo", U.Path);

   EXPECT_TRUE(fd.Seek(0));
   U = URI("http://example.org:90/foo");
   EXPECT_TRUE(MaybeAddAuth(fd, U));
   EXPECT_EQ("apt", U.User);
   EXPECT_EQ("apt", U.Password);
   EXPECT_EQ("example.org", U.Host);
   EXPECT_EQ(90u, U.Port);
   EXPECT_EQ("/foo", U.Path);

   EXPECT_TRUE(fd.Seek(0));
   U = URI("http://example.org:8080/foo");
   EXPECT_TRUE(MaybeAddAuth(fd, U));
   EXPECT_EQ("example", U.User);
   EXPECT_EQ("foobar", U.Password);

   EXPECT_TRUE(fd.Seek(0));
   U = URI("http://example.net:42/foo");
   EXPECT_TRUE(MaybeAddAuth(fd, U));
   EXPECT_EQ("foo", U.User);
   EXPECT_EQ("bar", U.Password);

   EXPECT_TRUE(fd.Seek(0));
   U = URI("http://example.org/foo");
   EXPECT_TRUE(MaybeAddAuth(fd, U));
   EXPECT_EQ("anonymous", U.User);
   EXPECT_EQ("pass", U.Password);

   EXPECT_TRUE(fd.Seek(0));
   U = URI("http://example.com/apt");
   EXPECT_TRUE(MaybeAddAuth(fd, U));
   EXPECT_TRUE(U.User.empty());
   EXPECT_TRUE(U.Password.empty());

   EXPECT_TRUE(fd.Seek(0));
   U = URI("http://example.com/foo");
   EXPECT_TRUE(MaybeAddAuth(fd, U));
   EXPECT_EQ("user1", U.User);
   EXPECT_EQ("pass1", U.Password);

   EXPECT_TRUE(fd.Seek(0));
   U = URI("http://example.com/fooo");
   EXPECT_TRUE(MaybeAddAuth(fd, U));
   EXPECT_EQ("user1", U.User);
   EXPECT_EQ("pass1", U.Password);

   EXPECT_TRUE(fd.Seek(0));
   U = URI("http://example.com/fo");
   EXPECT_TRUE(MaybeAddAuth(fd, U));
   EXPECT_TRUE(U.User.empty());
   EXPECT_TRUE(U.Password.empty());

   EXPECT_TRUE(fd.Seek(0));
   U = URI("http://example.com/bar");
   EXPECT_TRUE(MaybeAddAuth(fd, U));
   EXPECT_EQ("user2", U.User);
   EXPECT_EQ("pass2", U.Password);

   EXPECT_TRUE(fd.Seek(0));
   U = URI("http://example.com/user");
   EXPECT_TRUE(MaybeAddAuth(fd, U));
   EXPECT_EQ("user", U.User);
   EXPECT_TRUE(U.Password.empty());

   EXPECT_TRUE(fd.Seek(0));
   U = URI("socks5h://example.last/debian");
   EXPECT_TRUE(MaybeAddAuth(fd, U));
   EXPECT_EQ("debian", U.User);
   EXPECT_EQ("rules", U.Password);

   EXPECT_TRUE(fd.Seek(0));
   U = URI("socks5h://example.debian/");
   EXPECT_TRUE(MaybeAddAuth(fd, U));
   EXPECT_TRUE(U.User.empty());
   EXPECT_TRUE(U.Password.empty());

   EXPECT_TRUE(fd.Seek(0));
   U = URI("socks5h://user:pass@example.debian/");
   EXPECT_TRUE(MaybeAddAuth(fd, U));
   EXPECT_EQ("user", U.User);
   EXPECT_EQ("pass", U.Password);
}
TEST(NetRCTest, BadFileNoMachine)
{
   FileFd fd;
   createTemporaryFile("doublesignedfile", fd, nullptr, R"apt(
foo example.org login foo1 password bar
machin example.org login foo2 password bar
machine2 example.org login foo3 password bar
)apt");

   URI U("http://example.org/foo");
   EXPECT_TRUE(MaybeAddAuth(fd, U));
   EXPECT_TRUE(U.User.empty());
   EXPECT_TRUE(U.Password.empty());
}
TEST(NetRCTest, BadFileEndsMachine)
{
   FileFd fd;
   createTemporaryFile("doublesignedfile", fd, nullptr, R"apt(
machine example.org login foo1 password bar
machine)apt");

   URI U("http://example.org/foo");
   EXPECT_TRUE(MaybeAddAuth(fd, U));
   EXPECT_EQ("foo1", U.User);
   EXPECT_EQ("bar", U.Password);

   EXPECT_TRUE(fd.Seek(0));
   U = URI("http://example.net/foo");
   EXPECT_FALSE(MaybeAddAuth(fd, U));
   EXPECT_TRUE(U.User.empty());
   EXPECT_TRUE(U.Password.empty());

   EXPECT_TRUE(fd.Seek(0));
   U = URI("http://foo:bar@example.net/foo");
   EXPECT_TRUE(MaybeAddAuth(fd, U));
   EXPECT_EQ("foo", U.User);
   EXPECT_EQ("bar", U.Password);
}
TEST(NetRCTest, BadFileEndsLogin)
{
   FileFd fd;
   createTemporaryFile("doublesignedfile", fd, nullptr, R"apt(
machine example.org login foo1 password bar
machine example.net login)apt");

   URI U("http://example.org/foo");
   EXPECT_TRUE(MaybeAddAuth(fd, U));
   EXPECT_EQ("foo1", U.User);
   EXPECT_EQ("bar", U.Password);

   EXPECT_TRUE(fd.Seek(0));
   U = URI("http://example.net/foo");
   EXPECT_FALSE(MaybeAddAuth(fd, U));
   EXPECT_TRUE(U.User.empty());
   EXPECT_TRUE(U.Password.empty());

   EXPECT_TRUE(fd.Seek(0));
   U = URI("http://foo:bar@example.net/foo");
   EXPECT_TRUE(MaybeAddAuth(fd, U));
   EXPECT_EQ("foo", U.User);
   EXPECT_EQ("bar", U.Password);
}
TEST(NetRCTest, BadFileEndsPassword)
{
   FileFd fd;
   createTemporaryFile("doublesignedfile", fd, nullptr, R"apt(
machine example.org login foo1 password bar
machine example.net password)apt");

   URI U("http://example.org/foo");
   EXPECT_TRUE(MaybeAddAuth(fd, U));
   EXPECT_EQ("foo1", U.User);
   EXPECT_EQ("bar", U.Password);

   EXPECT_TRUE(fd.Seek(0));
   U = URI("http://example.net/foo");
   EXPECT_FALSE(MaybeAddAuth(fd, U));
   EXPECT_TRUE(U.User.empty());
   EXPECT_TRUE(U.Password.empty());

   EXPECT_TRUE(fd.Seek(0));
   U = URI("http://foo:bar@example.net/foo");
   EXPECT_TRUE(MaybeAddAuth(fd, U));
   EXPECT_EQ("foo", U.User);
   EXPECT_EQ("bar", U.Password);
}