-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathmain.rs
More file actions
636 lines (545 loc) · 20.8 KB
/
Copy pathmain.rs
File metadata and controls
636 lines (545 loc) · 20.8 KB
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
#![allow(missing_docs)]
use async_ftp::{FtpStream, types::Result};
use libunftp::{ServerBuilder, auth::DefaultUser, options::FtpsRequired};
use pretty_assertions::assert_eq;
use rstest::{fixture, rstest};
use std::fmt::Debug;
use std::path::PathBuf;
use std::str;
use std::sync::atomic::{AtomicU16, Ordering};
use unftp_sbe_fs::{Filesystem, ServerExt};
fn ensure_login_required<T: Debug>(r: Result<T>) {
let err = r.unwrap_err().to_string();
if !err.contains("530 Please authenticate") {
panic!("Could execute command without logging in!");
}
}
fn ensure_ftps_required<T: Debug>(r: Result<T>) {
let err = r.unwrap_err().to_string();
if !err.contains("534") {
panic!("FTPS enforcement is broken!");
}
}
static TESTPORT: AtomicU16 = AtomicU16::new(1234);
struct Harness {
root: PathBuf,
_tempdir: tempfile::TempDir,
addr: String,
}
async fn custom_server_harness<S>(s: S) -> Harness
where
S: Fn(PathBuf) -> ServerBuilder<Filesystem, DefaultUser>,
{
let port = TESTPORT.fetch_add(1, Ordering::Relaxed);
let addr = format!("127.0.0.1:{}", port);
let tempdir = tempfile::TempDir::new().unwrap();
let root = tempdir.path().to_path_buf();
let server = s(root.clone()).build().unwrap().listen(addr.clone());
tokio::spawn(server);
while async_ftp::FtpStream::connect(&addr).await.is_err() {
tokio::time::sleep(std::time::Duration::from_millis(10)).await;
}
Harness { root, addr, _tempdir: tempdir }
}
#[fixture]
async fn harness() -> Harness {
custom_server_harness(libunftp::Server::with_fs).await
}
#[rstest]
#[awt]
#[tokio::test]
async fn connect(#[future] harness: Harness) {
async_ftp::FtpStream::connect(harness.addr).await.unwrap();
}
#[rstest]
#[awt]
#[tokio::test]
async fn login(#[future] harness: Harness) {
let username = "koen";
let password = "hoi";
let mut ftp_stream = async_ftp::FtpStream::connect(harness.addr).await.unwrap();
ftp_stream.login(username, password).await.unwrap();
}
struct FtpsRequireWorksConfig {
username: &'static str,
mode_control_chan: FtpsRequired,
mode_data_chan: FtpsRequired,
give534: bool,
give534_data: bool,
}
#[rstest(config,
// control channel tests
case(FtpsRequireWorksConfig {
username: "anonymous",
mode_control_chan: FtpsRequired::None,
mode_data_chan: FtpsRequired::None,
give534: false,
give534_data: false,
}),
case(FtpsRequireWorksConfig {
username: "the-user",
mode_control_chan: FtpsRequired::None,
mode_data_chan: FtpsRequired::None,
give534: false,
give534_data: false,
}),
case(FtpsRequireWorksConfig {
username: "anonymous",
mode_control_chan: FtpsRequired::All,
mode_data_chan: FtpsRequired::None,
give534: true,
give534_data: false,
}),
case(FtpsRequireWorksConfig {
username: "the-user",
mode_control_chan: FtpsRequired::All,
mode_data_chan: FtpsRequired::None,
give534: true,
give534_data: false,
}),
case(FtpsRequireWorksConfig {
username: "AnonyMous",
mode_control_chan: FtpsRequired::Accounts,
mode_data_chan: FtpsRequired::None,
give534: false,
give534_data: false,
}),
case(FtpsRequireWorksConfig {
username: "the-user",
mode_control_chan: FtpsRequired::Accounts,
mode_data_chan: FtpsRequired::None,
give534: true,
give534_data: false,
}),
// Data channel tests
case(FtpsRequireWorksConfig {
username: "anonymous",
mode_control_chan: FtpsRequired::None,
mode_data_chan: FtpsRequired::None,
give534: false,
give534_data: false,
}),
case(FtpsRequireWorksConfig {
username: "the-user",
mode_control_chan: FtpsRequired::None,
mode_data_chan: FtpsRequired::None,
give534: false,
give534_data: false,
}),
case(FtpsRequireWorksConfig {
username: "anonymous",
mode_control_chan: FtpsRequired::None,
mode_data_chan: FtpsRequired::All,
give534: false,
give534_data: true,
}),
case(FtpsRequireWorksConfig {
username: "the-user",
mode_control_chan: FtpsRequired::None,
mode_data_chan: FtpsRequired::All,
give534: false,
give534_data: true,
}),
case(FtpsRequireWorksConfig {
username: "AnonyMous",
mode_control_chan: FtpsRequired::None,
mode_data_chan: FtpsRequired::Accounts,
give534: false,
give534_data: false,
}),
case(FtpsRequireWorksConfig {
username: "the-user",
mode_control_chan: FtpsRequired::None,
mode_data_chan: FtpsRequired::Accounts,
give534: false,
give534_data: true,
}),
)]
#[awt]
#[tokio::test]
async fn ftps_require_works(config: FtpsRequireWorksConfig) {
let s = |path| libunftp::Server::with_fs(path).ftps_required(config.mode_control_chan, config.mode_data_chan);
let h = custom_server_harness(s).await;
let mut ftp_stream = async_ftp::FtpStream::connect(h.addr).await.unwrap();
let result = ftp_stream.login(config.username, "blah").await;
if config.give534 {
ensure_ftps_required(result);
}
if config.give534_data {
let result = ftp_stream.list(None).await;
ensure_ftps_required(result);
}
}
#[rstest]
#[awt]
#[tokio::test(flavor = "current_thread")]
async fn noop(#[future] harness: Harness) {
let mut ftp_stream = async_ftp::FtpStream::connect(harness.addr).await.unwrap();
ftp_stream.noop().await.unwrap();
}
#[rstest]
#[awt]
#[tokio::test]
async fn get(#[future] harness: Harness) {
use std::io::Write;
let mut filename = harness.root.clone();
// Create a temporary file in the FTP root that we'll retrieve
filename.push("bla.txt");
let mut f = std::fs::File::create(filename.clone()).unwrap();
// Write some random data to our file
let mut data = vec![0; 1024];
getrandom::fill(&mut data).expect("Error generating random bytes");
f.write_all(&data).unwrap();
// Retrieve the remote file
let mut ftp_stream = FtpStream::connect(harness.addr).await.unwrap();
ensure_login_required(ftp_stream.simple_retr("bla.txt").await);
ftp_stream.login("hoi", "jij").await.unwrap();
let remote_file = ftp_stream.simple_retr("bla.txt").await.unwrap();
let remote_data = remote_file.into_inner();
assert_eq!(remote_data, data);
}
#[rstest]
#[awt]
#[tokio::test]
async fn put(#[future] harness: Harness) {
use std::io::Cursor;
let content = b"Hello from this test!\n";
let mut ftp_stream = FtpStream::connect(harness.addr).await.unwrap();
let mut reader = Cursor::new(content);
ensure_login_required(ftp_stream.put("greeting.txt", &mut reader).await);
ftp_stream.login("hoi", "jij").await.unwrap();
ftp_stream.put("greeting.txt", &mut reader).await.unwrap();
// retrieve file back again, and check if we got the same back.
let remote_data = ftp_stream.simple_retr("greeting.txt").await.unwrap().into_inner();
assert_eq!(remote_data, content);
}
mod list {
use super::*;
/// test the exact format of the output
#[cfg(unix)]
#[rstest]
#[awt]
#[tokio::test]
async fn format(#[future] harness: Harness) {
use regex::Regex;
use std::os::unix::fs::{MetadataExt, OpenOptionsExt, fchown};
// Create a filename in the ftp root that we will look for in the `LIST` output
let path = harness.root.join("test.txt");
let f = std::fs::OpenOptions::new()
.read(true)
.write(true)
.create(true)
.truncate(true)
.mode(0o754)
.open(path)
.unwrap();
// Because most OSes set the file's gid to its parent directory's, and the parent
// directory's is often root, deliberately set it to something more interesting.
fchown(&f, None, Some(nix::unistd::Gid::effective().as_raw())).unwrap();
let md = f.metadata().unwrap();
let uid = md.uid();
let gid = md.gid();
let link_count = md.nlink();
let size = md.len();
let mut ftp_stream = FtpStream::connect(harness.addr).await.unwrap();
ensure_login_required(ftp_stream.list(None).await);
ftp_stream.login("hoi", "jij").await.unwrap();
let list = ftp_stream.list(None).await.unwrap();
let pat = format!("^-rwxr-xr--\\s+{link_count}\\s+{uid}\\s+{gid}\\s+{size}.*test.txt");
let re = Regex::new(&pat).unwrap();
for entry in list {
if entry.contains("test.txt") {
assert!(re.is_match(&entry), "\"{entry}\" did not match pattern {re:?}");
return;
}
}
panic!("Entry not found");
}
#[rstest]
#[awt]
#[tokio::test]
async fn root(#[future] harness: Harness) {
// Create a filename in the ftp root that we will look for in the `LIST` output
let path = harness.root.join("test.txt");
{
let _f = std::fs::File::create(path);
}
let mut ftp_stream = FtpStream::connect(harness.addr).await.unwrap();
ensure_login_required(ftp_stream.list(None).await);
ftp_stream.login("hoi", "jij").await.unwrap();
let list = ftp_stream.list(None).await.unwrap();
let mut found = false;
for entry in list {
if entry.contains("test.txt") {
found = true;
break;
}
}
assert!(found);
}
#[rstest]
#[awt]
#[tokio::test]
async fn subdir(#[future] harness: Harness) {
let dir_in_root = tempfile::TempDir::new_in(harness.root).unwrap();
// Create a filename in the subdirectory that we will look for in the `LIST` output
let path = dir_in_root.path().join("test.txt");
{
let _f = std::fs::File::create(path);
}
let mut ftp_stream = FtpStream::connect(harness.addr).await.unwrap();
ensure_login_required(ftp_stream.list(None).await);
ftp_stream.login("hoi", "jij").await.unwrap();
let list = ftp_stream.list(dir_in_root.path().file_name().and_then(std::ffi::OsStr::to_str)).await.unwrap();
let mut found = false;
for entry in list {
if entry.contains("test.txt") {
found = true;
break;
}
}
assert!(found);
}
/// test the exact format of the output for symlinks
#[cfg(unix)]
#[rstest]
#[case::relative(harness(), false)]
// Symlinks with absolute paths can be read, too
// https://github.com/bytecodealliance/cap-std/issues/353
#[case::absolute(harness(), true)]
#[awt]
#[tokio::test]
async fn symlink(
#[case]
#[future]
harness: Harness,
#[case] absolute: bool,
) {
use regex::Regex;
use std::os::unix::fs::MetadataExt;
// Create a filename in the ftp root that we will look for in the `LIST` output
let path = harness.root.join("link");
let target = if absolute { "/target" } else { "target" };
std::os::unix::fs::symlink(target, &path).unwrap();
let md = std::fs::symlink_metadata(&path).unwrap();
let uid = md.uid();
let gid = md.gid();
let link_count = md.nlink();
let size = md.len();
let mut ftp_stream = FtpStream::connect(harness.addr).await.unwrap();
ensure_login_required(ftp_stream.list(None).await);
ftp_stream.login("hoi", "jij").await.unwrap();
let list = ftp_stream.list(None).await.unwrap();
let pat = format!("^l[rwx-]{{9}}\\s+{link_count}\\s+{uid}\\s+{gid}\\s+{size}.*link -> {target}");
let re = Regex::new(&pat).unwrap();
for entry in list {
if entry.contains("link") {
assert!(re.is_match(&entry), "\"{entry}\" did not match pattern {re:?}");
return;
}
}
panic!("Entry not found");
}
}
mod mdtm {
use super::*;
use pretty_assertions::assert_eq;
/// Get the modification time of a regular file
#[rstest]
#[awt]
#[tokio::test]
async fn regular(#[future] harness: Harness) {
// Create a filename in the ftp root that we will look for in the `LIST` output
let path = harness.root.join("test.txt");
let f = std::fs::File::create(path).unwrap();
let modified = f.metadata().unwrap().modified().unwrap();
let mut ftp_stream = FtpStream::connect(harness.addr).await.unwrap();
ensure_login_required(ftp_stream.list(None).await);
ftp_stream.login("hoi", "jij").await.unwrap();
let r = ftp_stream.mdtm("test.txt").await.unwrap().unwrap();
assert_eq!(r.to_rfc2822(), chrono::DateTime::<chrono::Utc>::from(modified).to_rfc2822());
}
/// Get the modification time of a symlink
#[rstest]
#[case::relative(harness(), false)]
#[case::absolute(harness(), true)]
#[awt]
#[tokio::test]
async fn symlink(
#[case]
#[future]
harness: Harness,
#[case] absolute: bool,
) {
// Create a filename in the ftp root that we will look for in the `LIST` output
let path = harness.root.join("link");
let target = if absolute { "/target" } else { "target" };
std::os::unix::fs::symlink(target, &path).unwrap();
let md = std::fs::symlink_metadata(&path).unwrap();
let modified = md.modified().unwrap();
let mut ftp_stream = FtpStream::connect(harness.addr).await.unwrap();
ensure_login_required(ftp_stream.list(None).await);
ftp_stream.login("hoi", "jij").await.unwrap();
let r = ftp_stream.mdtm("link").await.unwrap().unwrap();
assert_eq!(r.to_rfc2822(), chrono::DateTime::<chrono::Utc>::from(modified).to_rfc2822());
}
}
#[rstest]
#[awt]
#[tokio::test]
async fn pwd(#[future] harness: Harness) {
let mut ftp_stream = FtpStream::connect(harness.addr).await.unwrap();
// Make sure we fail if we're not logged in
ensure_login_required(ftp_stream.pwd().await);
ftp_stream.login("hoi", "jij").await.unwrap();
let pwd = ftp_stream.pwd().await.unwrap();
assert_eq!(&pwd, "/");
}
#[rstest]
#[awt]
#[tokio::test]
async fn cwd(#[future] harness: Harness) {
let path = harness.root.clone();
let mut ftp_stream = FtpStream::connect(harness.addr).await.unwrap();
let dir_in_root = tempfile::TempDir::new_in(path).unwrap();
let basename = dir_in_root.path().file_name().unwrap();
ensure_login_required(ftp_stream.cwd(basename.to_str().unwrap()).await);
ftp_stream.login("hoi", "jij").await.unwrap();
ftp_stream.cwd(basename.to_str().unwrap()).await.unwrap();
let pwd = ftp_stream.pwd().await.unwrap();
assert_eq!(std::path::Path::new(&pwd), std::path::Path::new("/").join(basename));
}
#[rstest]
#[awt]
#[tokio::test]
async fn cdup(#[future] harness: Harness) {
let path = harness.root.clone();
let mut ftp_stream = FtpStream::connect(harness.addr).await.unwrap();
let dir_in_root = tempfile::TempDir::new_in(path).unwrap();
let basename = dir_in_root.path().file_name().unwrap();
ensure_login_required(ftp_stream.cdup().await);
ftp_stream.login("hoi", "jij").await.unwrap();
ftp_stream.cwd(basename.to_str().unwrap()).await.unwrap();
let pwd = ftp_stream.pwd().await.unwrap();
assert_eq!(std::path::Path::new(&pwd), std::path::Path::new("/").join(basename));
ftp_stream.cdup().await.unwrap();
let pwd = ftp_stream.pwd().await.unwrap();
assert_eq!(std::path::Path::new(&pwd), std::path::Path::new("/"));
}
#[rstest]
#[awt]
#[tokio::test]
async fn dele(#[future] harness: Harness) {
let mut ftp_stream = FtpStream::connect(harness.addr).await.unwrap();
let file_in_root = tempfile::NamedTempFile::new_in(harness.root).unwrap();
let file_name = file_in_root.path().file_name().unwrap().to_str().unwrap();
ensure_login_required(ftp_stream.rm(file_name).await);
ftp_stream.login("hoi", "jij").await.unwrap();
ftp_stream.rm(file_name).await.unwrap();
assert_eq!(std::fs::metadata(file_in_root.path()).unwrap_err().kind(), std::io::ErrorKind::NotFound);
}
#[rstest]
#[awt]
#[tokio::test]
async fn rmd(#[future] harness: Harness) {
let mut ftp_stream = FtpStream::connect(harness.addr).await.unwrap();
let dir_in_root = tempfile::tempdir_in(harness.root).unwrap();
let file_name = dir_in_root.path().file_name().unwrap().to_str().unwrap();
ensure_login_required(ftp_stream.rm(file_name).await);
ftp_stream.login("hoi", "jij").await.unwrap();
ftp_stream.rmdir(file_name).await.unwrap();
assert_eq!(std::fs::metadata(dir_in_root.path()).unwrap_err().kind(), std::io::ErrorKind::NotFound);
}
#[rstest]
#[awt]
#[tokio::test]
async fn quit(#[future] harness: Harness) {
let mut ftp_stream = FtpStream::connect(harness.addr).await.unwrap();
ftp_stream.quit().await.unwrap();
// Make sure the connection is actually closed
// This may take some time, so we'll poll for a bit.
let mut c = 0;
while ftp_stream.noop().await.is_ok() {
assert!(c < 100, "Timeout waiting for connection to close");
c += 1;
tokio::time::sleep(std::time::Duration::from_millis(10)).await;
}
}
#[rstest]
#[awt]
#[tokio::test]
async fn nlst(#[future] harness: Harness) {
// Create a filename that we wanna see in the `NLST` output
let path = harness.root.join("test.txt");
{
let _f = std::fs::File::create(path);
}
let mut ftp_stream = FtpStream::connect(harness.addr).await.unwrap();
ensure_login_required(ftp_stream.nlst(None).await);
ftp_stream.login("hoi", "jij").await.unwrap();
let list = ftp_stream.nlst(None).await.unwrap();
assert_eq!(list, vec!["test.txt"]);
}
#[rstest]
#[awt]
#[tokio::test]
async fn mkdir(#[future] harness: Harness) {
let mut ftp_stream = FtpStream::connect(harness.addr).await.unwrap();
let new_dir_name = "hallo";
ensure_login_required(ftp_stream.mkdir(new_dir_name).await);
ftp_stream.login("hoi", "jij").await.unwrap();
ftp_stream.mkdir(new_dir_name).await.unwrap();
let full_path = harness.root.join(new_dir_name);
let metadata = std::fs::metadata(full_path).unwrap();
assert!(metadata.is_dir());
}
#[rstest]
#[awt]
#[tokio::test]
async fn rename(#[future] harness: Harness) {
// Create a file that we will rename
let full_from = harness.root.join("ikbenhier.txt");
let _f = std::fs::File::create(&full_from);
let from_filename = full_from.file_name().unwrap().to_str().unwrap();
// What we'll rename our file to
let full_to = harness.root.join("nu ben ik hier.txt");
let to_filename = full_to.file_name().unwrap().to_str().unwrap();
let mut ftp_stream = FtpStream::connect(harness.addr).await.expect("Failed to connect");
// Make sure we fail if we're not logged in
ensure_login_required(ftp_stream.rename(from_filename, to_filename).await);
// Do the renaming
ftp_stream.login("some", "user").await.unwrap();
ftp_stream.rename(from_filename, to_filename).await.expect("Failed to rename");
// Make sure the old filename is gone
std::fs::metadata(full_from).expect_err("Renamed file still exists with old name");
// Make sure the new filename exists
let metadata = std::fs::metadata(full_to).expect("New filename not created");
assert!(metadata.is_file());
}
// This test hang on the latest Rust version it seems. Disabling till we fix
// #[tokio::test]
// async fn size() {
// let addr = "127.0.0.1:1251";
// let root = std::env::temp_dir();
// tokio::spawn(libunftp::Server::with_fs(root.clone()).listen(addr));
// tokio::time::sleep(Duration::new(1, 0)).await;
//
// let mut ftp_stream = FtpStream::connect(addr).await.unwrap();
// let file_in_root = tempfile::NamedTempFile::new_in(root).unwrap();
// let file_name = file_in_root.path().file_name().unwrap().to_str().unwrap();
//
// let mut w = BufWriter::new(&file_in_root);
// w.write_all(b"Hello unftp").expect("Should be able to write to the temp file.");
// w.flush().expect("Should be able to flush the temp file.");
//
// // Make sure we fail if we're not logged in
// ensure_login_required(ftp_stream.size(file_name).await);
// ftp_stream.login("hoi", "jij").await.unwrap();
//
// // Make sure we fail if we don't supply a path
// ftp_stream.size("").await.unwrap_err();
// let size1 = ftp_stream.size(file_name).await;
// let size2 = size1.unwrap();
// let size3 = size2.unwrap();
// assert_eq!(size3, fs::metadata(&file_in_root).unwrap().len() as usize, "Wrong size returned.");
// }