/
test_netrc.cc
334 lines (276 loc) · 8.57 KB
/
test_netrc.cc
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
#include <iostream>
#include <fstream>
#include <string>
#include <map>
//#include <exception>
#include <cstdlib>
#include <boost/algorithm/string.hpp>
#define BOOST_TEST_MODULE test_netrc
// see test_opts.cc for more info on BOOST_TEST_DYN_LINK
//#define BOOST_TEST_DYN_LINK
#include <boost/test/unit_test.hpp>
#include "netrc.hpp"
#ifdef __has_include
# if __has_include(<filesystem>)
# include <filesystem> // gcc8 (Fedora29+)
namespace fs = std::filesystem;
# elif __has_include(<experimental/filesystem>)
# include <experimental/filesystem> // gcc7 (Ubuntu 18.04)
namespace fs = std::experimental::filesystem;
# endif
#else
#error no __has_include
#endif
BOOST_AUTO_TEST_CASE(test_zero) {
int i {0};
BOOST_REQUIRE(i==0);
}
BOOST_AUTO_TEST_CASE(test_one) {
int i {1};
BOOST_REQUIRE(i==1);
}
void verify(netrc_map& netrc)
{
std::array<std::string,4> tests { "172.16.22.1", "172.16.17.1", "172.19.10.119", "default" };
for (auto s : tests) {
// running inside test code so want the exception to propogate
auto auth = netrc.at(s);
std::cout << s << " username=" << std::get<0>(auth) << " password=" << std::get<1>(auth) << "\n";
// try {
// auto auth = netrc.at(s);
// std::cout << s << " username=" << std::get<0>(auth) << " password=" << std::get<1>(auth) << "\n";
// } catch (std::out_of_range& err ) {
// std::cerr << "machine " << s << " not found\n";
// };
}
}
BOOST_AUTO_TEST_CASE(test_simple)
{
std::string testbuf {
// C++ raw string literal F T W
R"#(machine 172.16.17.1
login admin
password 12345
# can have entire thing on one line
# oh, and comments, too
machine 192.168.0.1 login admin password 12345
machine 172.16.22.1
login admin
password 12345
machine 172.19.10.119
login admin
password 00000000
default login admin password hythloday@example.com
# TODO put fail tests cases into own string
# weird whitespace
# machine foo.bar.baz
# login dave
# password this is invalid because embedded spaces
)#"
};
std::stringstream infile{testbuf};
auto netrc = netrc_parse(infile);
verify(netrc);
}
BOOST_AUTO_TEST_CASE(test_only_default)
{
std::string testbuf {
R"#(default login admin password hythloday@example.com
)#"};
std::stringstream infile{testbuf};
auto netrc = netrc_parse(infile);
auto default_ = netrc.at("default");
auto [a,b,c] = default_;
BOOST_REQUIRE(a=="admin");
BOOST_REQUIRE(b=="hythloday@example.com");
auto [x,y,z] = netrc.at("default");
}
BOOST_AUTO_TEST_CASE(test_whitespace)
{
std::string testbuf {
R"#(
#
# should not see this machine
# comment machine ignore.example.com login foo password foo
machine 172.16.17.1 login admin
password 12345
# can have entire thing on one line
# oh, and comments, too
machine 192.168.0.1 login admin password 12345
machine 172.16.22.1
login admin
account foobarbaz
password 12345
machine 172.19.10.119
login admin
password 00000000
default login admin password hythloday@example.com
)#"
};
std::stringstream infile{testbuf};
#if 1
// tinkering with stringstream
std::vector<std::string> netrc_lines;
for( std::string line ; getline(infile,line) ; netrc_lines.push_back(line) );
std::cout << "read " << netrc_lines.size() << " lines\n";
// seems both these lines are required in order to re-read the stream
infile.clear();
infile.seekg(std::ios_base::beg);
// re-read the stream
netrc_lines.clear();
for( std::string line ; getline(infile,line) ; netrc_lines.push_back(line) );
std::cout << "read " << netrc_lines.size() << " lines\n";
BOOST_TEST(netrc_lines.size() == 22);
infile.clear();
infile.seekg(std::ios_base::beg);
#endif
auto netrc = netrc_parse(infile);
verify(netrc);
// verify not see the 'ignore.example.com'
BOOST_REQUIRE_THROW( netrc.at("ignore.example.com"), std::out_of_range);
}
class NetrcFile
{
public:
fs::path path;
std::ofstream outfile;
int fd;
NetrcFile() : path(fs::temp_directory_path() / "netrctestXXXXXX")
{
char filename[FILENAME_MAX+1] = {};
strncpy(filename, path.native().c_str(), FILENAME_MAX);
fd = mkstemp(filename);
if (fd < 0) {
std::clog << "unable to create temp file=" << path << " errno="
<< errno << " error=\"" << strerror(errno) << "\n";
throw file_error(path);
}
path = filename;
outfile.open(path);
std::cout << "NetrcFile " << path << "\n";
};
~NetrcFile()
{
std::cout << "~NetrcFile " << path << "\n";
close(fd);
outfile.close();
bool success = fs::remove(path);
if (!success) {
// can't throw in a destuctor so we'll just complain loudly
std::cerr << "failed to remove the file=" << path << std::endl;
}
};
};
class NetrcFileDefault : public NetrcFile
{
public:
NetrcFileDefault()
{
outfile << "default login admin password hythloday@example.com\n" << std::endl;
};
};
BOOST_FIXTURE_TEST_CASE(test_file, NetrcFile)
{
outfile << "default login admin password hythloday@example.com\n" << std::endl;
// file should be created with 0600 so change to incorrect permissions
// davep 20190113 ; perms::add_perms doesn't seem to exist yet so big hammer set the perms
fs::permissions(path, fs::perms::owner_read|fs::perms::owner_write|fs::perms::group_read|fs::perms::group_write);
// fs::permissions(path, fs::perms::add_perms|fs::perms::group_read|fs::perms::group_write);
// verify failure if permissions are wrong
BOOST_REQUIRE_THROW( netrc_parse_file(path.native()), file_error);
fs::permissions(path, fs::perms::owner_read|fs::perms::owner_write);
auto netrc = netrc_parse_file(path.native());
auto [a,b,c] = netrc.at("default");
std::cout << a << " " << b << " " << c << "\n";
BOOST_REQUIRE(a=="admin");
BOOST_REQUIRE(b=="hythloday@example.com");
BOOST_REQUIRE(c.empty());
}
BOOST_FIXTURE_TEST_CASE(test_file_with_fixture, NetrcFile)
{
outfile << "machine 192.168.0.1 login admin password 12345\n" << std::endl;
auto netrc = netrc_parse_file(path.native());
auto [a,b,c] = netrc.at("192.168.0.1");
BOOST_REQUIRE(a=="admin");
BOOST_REQUIRE(b=="12345");
BOOST_REQUIRE(c.empty());
}
BOOST_FIXTURE_TEST_CASE(test_file_with_default_fixture, NetrcFileDefault)
{
auto netrc = netrc_parse_file(path.native());
auto [a,b,c] = netrc.at("default");
BOOST_REQUIRE(a=="admin");
BOOST_REQUIRE(b=="hythloday@example.com");
BOOST_REQUIRE(c.empty());
}
BOOST_FIXTURE_TEST_CASE(test_file_with_macdef, NetrcFileDefault)
{
outfile << "machine 192.168.0.1 login admin password 12345\n" << std::endl;
outfile << "macdef init\nfoo\nbar\nbaz\n\n" << std::endl;
auto netrc = netrc_parse_file(path.native());
auto [login,password,account] = netrc.at("default");
BOOST_REQUIRE(login=="admin");
BOOST_REQUIRE(password=="hythloday@example.com");
BOOST_REQUIRE(account.empty());
std::tie(login,password,account) = netrc.at("192.168.0.1");
BOOST_REQUIRE(login=="admin");
BOOST_REQUIRE(password=="12345");
BOOST_REQUIRE(account.empty());
}
BOOST_FIXTURE_TEST_CASE(test_file_with_macdef_2, NetrcFileDefault)
{
outfile << "macdef init\nfoo\nbar\nbaz\n\n" << std::endl;
outfile << "machine 192.168.0.1 login admin password 12345\n" << std::endl;
outfile << "default login admin password hythloday@example.com\n" << std::endl;
auto netrc = netrc_parse_file(path.native());
auto [login,password,account] = netrc.at("default");
BOOST_REQUIRE(login=="admin");
BOOST_REQUIRE(password=="hythloday@example.com");
BOOST_REQUIRE(account.empty());
std::tie(login,password,account) = netrc.at("192.168.0.1");
BOOST_REQUIRE(login=="admin");
BOOST_REQUIRE(password=="12345");
BOOST_REQUIRE(account.empty());
}
BOOST_FIXTURE_TEST_CASE(test_file_with_macdef_3, NetrcFile)
{
outfile << "machine 192.168.0.1 login admin password 12345\n";
// outfile << "macdef init\nfoo\nbar\nbaz\n\n";
outfile << "default login admin password 12345\n";
outfile << "macdef init\nfoo\nbar\nbaz" << std::endl;
auto netrc = netrc_parse_file(path.native());
auto [login,password,account] = netrc.at("default");
BOOST_REQUIRE(login=="admin");
BOOST_REQUIRE(password=="12345");
BOOST_REQUIRE(account.empty());
std::tie(login,password,account) = netrc.at("192.168.0.1");
BOOST_REQUIRE(login=="admin");
BOOST_REQUIRE(password=="12345");
BOOST_REQUIRE(account.empty());
}
BOOST_AUTO_TEST_CASE(test_home_netrc)
{
// look for a $HOME/.netrc and parse it if it exists
std::string home = std::getenv("HOME");
fs::path path(home);
path.append(".netrc");
if ( !fs::exists(path) ) {
// try the windows version
path = home;
path.append("_netrc");
if ( !fs::exists(path) ) {
std::cerr << "no .netrc found\n";
return;
}
}
auto netrc = netrc_parse_file(path.native());
netrc.at("default");
// verify(netrc);
//
// // parse netrc files from cli
// for (int i=1 ; i<argc ; i++) {
// std::string f = argv[i];
// }
//
// return EXIT_SUCCESS;
}