summaryrefslogtreecommitdiffstats
path: root/test/libapt/authconf_test.cc
blob: 3a7b149a9b1e9ab9bab387295f6f9f282b22f688 (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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
#include <config.h>

#include <apt-pkg/error.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("https://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);

   openTemporaryFile("doublesignedfile", fd, 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 socks5h://example.last/debian login debian password rules)apt");
   U = URI("https://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("https://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("https://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("https://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("https://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("https://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("https://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("https://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("https://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("https://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("https://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("https://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;
   openTemporaryFile("doublesignedfile", fd, 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("https://example.org/foo");
   EXPECT_TRUE(MaybeAddAuth(fd, U));
   EXPECT_TRUE(U.User.empty());
   EXPECT_TRUE(U.Password.empty());
}
TEST(NetRCTest, BadFileEndsMachine)
{
   FileFd fd;
   openTemporaryFile("doublesignedfile", fd, R"apt(
machine example.org login foo1 password bar
machine)apt");

   URI U("https://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("https://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("https://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;
   openTemporaryFile("doublesignedfile", fd, R"apt(
machine example.org login foo1 password bar
machine example.net login)apt");

   URI U("https://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("https://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("https://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;
   openTemporaryFile("doublesignedfile", fd, R"apt(
machine example.org login foo1 password bar
machine example.net password)apt");

   URI U("https://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("https://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("https://foo:bar@example.net/foo");
   EXPECT_TRUE(MaybeAddAuth(fd, U));
   EXPECT_EQ("foo", U.User);
   EXPECT_EQ("bar", U.Password);
}

TEST(NetRCTest, MatchesOnlyHTTPS)
{
   FileFd fd;
   openTemporaryFile("doublesignedfile", fd, R"apt(
machine https.example login foo1 password bar
machine http://http.example login foo1 password bar
)apt");

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

   _error->PushToStack();
   EXPECT_TRUE(fd.Seek(0));
   U = URI("http://https.example/foo");
   EXPECT_TRUE(MaybeAddAuth(fd, U));
   EXPECT_TRUE(U.User.empty());
   EXPECT_TRUE(U.Password.empty());
   EXPECT_FALSE(_error->empty());
   EXPECT_TRUE(U.Password.empty());
   _error->RevertToStack();

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

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

}