Something went wrong. Try again.
FTP for Zig 0.16: a client, a server, and the wire protocol as a layer you can use on its own, all built on std.Io.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie <jeff@ocjtech.us>// SPDX-License-Identifier: MIT
//! The client against the server, over a loopback socket.//!//! The unit tests either side of this cover each half against what the RFCs//! say. These cover the two halves against each other, which is the only way//! to catch the mistakes that are self-consistent: a reply the server writes//! in a shape its own parser would accept and no other client would, a//! transfer whose end-of-file the two ends disagree about, a data connection//! opened in an order that works only when the same process is on both ends of//! it and nothing is really in flight.//!//! Every test runs the server in a task of its own, because a session and the//! client talking to it both block on the same socket and neither can run to//! completion first.
const std = @import("std");const Io = std.Io;const net = Io.net;const testing = std.testing;
const ftp = @import("ftp");
/// A server on a loopback port, serving a temporary directory, with a client/// already connected to it.////// Must not be moved once `start` has returned: the server's file system and/// authenticator, and the client's reader and writer, are all pointers into/// it.const Fixture = struct { tmp: testing.TmpDir, backing: ftp.server.DirFilesystem, credentials: ftp.server.Auth.Simple, server: ftp.server.Server, sessions: Io.Group, connection: ftp.Client.Connection,
const gpa = testing.allocator;
fn start(f: *Fixture, access: ftp.server.Auth.Access) !void { const io = testing.io; f.tmp = testing.tmpDir(.{}); errdefer f.tmp.cleanup();
f.backing = .init(f.tmp.dir, .{}); f.server = try .listen(io, gpa, .{ .ip4 = .loopback(0) }, .{ .filesystem = f.backing.filesystem(), .auth = .anonymousWith(&f.credentials, access), .greeting = "zig-ftp test server", }, .{}); errdefer f.server.deinit();
f.sessions = .init; // Without a second task the accept and the connect deadlock: whichever // runs first blocks waiting for the other. f.sessions.concurrent(io, serve, .{&f.server}) catch return error.SkipZigTest; errdefer { f.server.shutdown(); f.sessions.await(io) catch {}; }
try f.connection.connect(io, gpa, f.server.localAddress(), .{}); }
fn stop(f: *Fixture) void { f.connection.close(); f.server.shutdown(); f.sessions.await(testing.io) catch {}; f.server.deinit(); f.tmp.cleanup(); }
fn client(f: *Fixture) *ftp.Client { return &f.connection.client; }
/// Logs in as the anonymous user the fixture is configured for. fn login(f: *Fixture) !void { try f.client().login("anonymous", "test@example.com", null); }
fn writeFile(f: *Fixture, name: []const u8, data: []const u8) !void { try f.tmp.dir.writeFile(testing.io, .{ .sub_path = name, .data = data }); }
fn readFile(f: *Fixture, name: []const u8, buffer: []u8) ![]u8 { return f.tmp.dir.readFile(testing.io, name, buffer); }};
fn serve(server: *ftp.server.Server) void { // The accept loop ends when the fixture shuts the listening socket down, // and there is nobody left to report anything to if it ends some other // way. server.serve() catch {};}
fn expectListed(listing: ftp.Client.Listing, name: []const u8) !ftp.protocol.listing.Entry { for (listing.entries) |entry| { if (std.mem.eql(u8, entry.name, name)) return entry; } std.debug.print("no entry named \"{s}\" in:\n", .{name}); for (listing.entries) |entry| std.debug.print(" {s}\n", .{entry.name}); return error.TestExpectedEqual;}
test "log in and look around" { var f: Fixture = undefined; try f.start(.read_only); defer f.stop(); try f.login();
var buffer: [256]u8 = undefined; try testing.expectEqualStrings("/", try f.client().workingDirectory(&buffer)); try testing.expectEqualStrings("UNIX Type: L8", try f.client().systemType(&buffer));
// `login` reads the feature list, so the client already knows the server // has the extensions before it is asked to use one. try testing.expect(f.client().features.mlst); try testing.expect(f.client().features.size); try testing.expect(f.client().features.rest_stream); try testing.expect(f.client().features.epsv); try testing.expect(!f.client().features.auth_tls);
try f.client().noop(); try f.client().quit();}
test "a bad password is refused" { var f: Fixture = undefined; try f.start(.read_only); defer f.stop();
try testing.expectError(error.CommandFailed, f.client().login("root", "hunter2", null)); try testing.expectEqual(@as(u16, 530), f.client().last_reply.code.value);
// And nothing may be done without a login. try testing.expectError(error.CommandFailed, f.client().changeDirectory("/")); try testing.expectEqual(@as(u16, 530), f.client().last_reply.code.value);}
test "listing a directory" { var f: Fixture = undefined; try f.start(.read_only); defer f.stop(); try f.writeFile("readme.txt", "hello\n"); try f.writeFile("data.bin", "\x00\x01\x02\x03"); try f.tmp.dir.createDir(testing.io, "pub", .default_dir); try f.login();
var listing = try f.client().list(Fixture.gpa, null); defer listing.deinit(Fixture.gpa);
// The server has `MLSD`, so the client must have used it rather than // guessing at `ls` output. try testing.expectEqual(ftp.Client.Listing.Format.machine, listing.format);
const readme = try expectListed(listing, "readme.txt"); try testing.expectEqual(ftp.protocol.listing.Kind.file, readme.kind); try testing.expectEqual(@as(?u64, 6), readme.size); try testing.expect(readme.permissions.?.retrieve); // Read-only access, so nothing may be written. try testing.expect(!readme.permissions.?.store);
const directory = try expectListed(listing, "pub"); try testing.expectEqual(ftp.protocol.listing.Kind.dir, directory.kind); try testing.expect(directory.permissions.?.enter);
// RFC 3659 §7.5.1 asks a machine listing to describe the directory // itself, so a client learns what it may do with it in the same reply. _ = try expectListed(listing, ".");}
test "listing with LIST, for a client that has not asked for features" { var f: Fixture = undefined; try f.start(.read_only); defer f.stop(); try f.writeFile("readme.txt", "hello\n");
// Logging in without `readFeatures` leaves `features.mlst` false, which // is what an old server looks like from here. _ = try f.client().command(.user, "anonymous"); _ = try f.client().command(.pass, "test@example.com");
var listing = try f.client().list(Fixture.gpa, null); defer listing.deinit(Fixture.gpa); try testing.expectEqual(ftp.Client.Listing.Format.human, listing.format);
const readme = try expectListed(listing, "readme.txt"); try testing.expectEqual(@as(?u64, 6), readme.size); try testing.expectEqual(ftp.protocol.listing.Kind.file, readme.kind);}
test "NLST gives names and nothing else" { var f: Fixture = undefined; try f.start(.read_only); defer f.stop(); try f.writeFile("one", "1"); try f.writeFile("two", "2"); try f.login();
var text: std.ArrayList(u8) = .empty; defer text.deinit(Fixture.gpa); var names: std.ArrayList([]const u8) = .empty; defer names.deinit(Fixture.gpa); try f.client().nameList(Fixture.gpa, null, &text, &names);
try testing.expectEqual(@as(usize, 2), names.items.len); var seen_one = false; for (names.items) |name| { if (std.mem.eql(u8, name, "one")) seen_one = true; } try testing.expect(seen_one);}
test "retrieving a file" { var f: Fixture = undefined; try f.start(.read_only); defer f.stop(); const contents = "the quick brown fox\njumps over the lazy dog\n"; try f.writeFile("fox.txt", contents); try f.login();
var buffer: [256]u8 = undefined; var w: Io.Writer = .fixed(&buffer); const moved = try f.client().retrieve("fox.txt", &w, .{}); try testing.expectEqual(@as(u64, contents.len), moved); try testing.expectEqualStrings(contents, w.buffered());}
test "retrieving a file that is not there" { var f: Fixture = undefined; try f.start(.read_only); defer f.stop(); try f.tmp.dir.createDir(testing.io, "adirectory", .default_dir); try f.login();
var buffer: [16]u8 = undefined; var w: Io.Writer = .fixed(&buffer); try testing.expectError(error.CommandFailed, f.client().retrieve("nothing", &w, .{})); try testing.expectEqual(@as(u16, 550), f.client().last_reply.code.value);
// The control connection must still be usable: a failed transfer is a // failed command, not a failed session. try f.client().noop();
// And the refusal must come *instead of* the 150, not after it. A client // told the transfer is starting has usually already created the file it // means to write into, and a 550 then leaves it holding an empty one. _ = try f.client().command(.epsv, null); try f.client().send(.retr, "nothing"); const first = try f.client().receive(); try testing.expectEqual(@as(u16, 550), first.code.value);
// A directory is refused the same way rather than streaming whatever the // operating system thinks its bytes are. _ = try f.client().command(.epsv, null); try f.client().send(.retr, "adirectory"); try testing.expectEqual(@as(u16, 550), (try f.client().receive()).code.value);
try f.client().noop();}
test "retrieving from an offset" { var f: Fixture = undefined; try f.start(.read_only); defer f.stop(); try f.writeFile("numbers", "0123456789"); try f.login();
var buffer: [16]u8 = undefined; var w: Io.Writer = .fixed(&buffer); try testing.expectEqual(@as(u64, 4), try f.client().retrieve("numbers", &w, .{ .offset = 6 })); try testing.expectEqualStrings("6789", w.buffered());
// And the offset applies to that transfer only. var second: [16]u8 = undefined; var w2: Io.Writer = .fixed(&second); try testing.expectEqual(@as(u64, 10), try f.client().retrieve("numbers", &w2, .{}));}
test "storing a file" { var f: Fixture = undefined; try f.start(.all); defer f.stop(); try f.login();
const contents = "uploaded contents\n"; var source: Io.Reader = .fixed(contents); try testing.expectEqual( @as(u64, contents.len), try f.client().store("upload.txt", &source, .{}), );
var buffer: [64]u8 = undefined; try testing.expectEqualStrings(contents, try f.readFile("upload.txt", &buffer));}
test "appending to a file" { var f: Fixture = undefined; try f.start(.all); defer f.stop(); try f.writeFile("log", "first\n"); try f.login();
var source: Io.Reader = .fixed("second\n"); _ = try f.client().append("log", &source, .{});
var buffer: [64]u8 = undefined; try testing.expectEqualStrings("first\nsecond\n", try f.readFile("log", &buffer));}
test "a read-only login may not write" { var f: Fixture = undefined; try f.start(.read_only); defer f.stop(); try f.writeFile("existing", "x"); try f.login();
var source: Io.Reader = .fixed("nope"); try testing.expectError(error.CommandFailed, f.client().store("new.txt", &source, .{})); try testing.expectError(error.CommandFailed, f.client().deleteFile("existing")); try testing.expectError(error.CommandFailed, f.client().makeDirectory("new", &.{})); try testing.expectError(error.CommandFailed, f.client().rename("existing", "other"));
// Reading still works, and the file is untouched. var buffer: [16]u8 = undefined; try testing.expectEqualStrings("x", try f.readFile("existing", &buffer));}
test "ASCII mode converts line endings in both directions" { var f: Fixture = undefined; try f.start(.all); defer f.stop(); // A file stored with the local line ending, as it would be on disk here. try f.writeFile("text", "one\ntwo\nthree\n"); try f.login();
// Fetched in ASCII mode it crosses the network as CRLF and arrives back // as LF, so what the client sees matches what is on the server's disk. var buffer: [64]u8 = undefined; var w: Io.Writer = .fixed(&buffer); _ = try f.client().retrieve("text", &w, .{ .type = .{ .ascii = .non_print } }); try testing.expectEqualStrings("one\ntwo\nthree\n", w.buffered());
// And the other way: a CRLF file sent in ASCII mode lands with the // server's line endings. var source: Io.Reader = .fixed("alpha\r\nbeta\r\n"); _ = try f.client().store("back", &source, .{ .type = .{ .ascii = .non_print } }); var read_back: [64]u8 = undefined; try testing.expectEqualStrings("alpha\nbeta\n", try f.readFile("back", &read_back));}
test "binary mode changes nothing" { var f: Fixture = undefined; try f.start(.all); defer f.stop(); const bytes = "\x00\r\n\r\xff\n\x1a"; try f.writeFile("raw", bytes); try f.login();
var buffer: [64]u8 = undefined; var w: Io.Writer = .fixed(&buffer); _ = try f.client().retrieve("raw", &w, .{ .type = .image }); try testing.expectEqualStrings(bytes, w.buffered());}
test "directories and renaming" { var f: Fixture = undefined; try f.start(.all); defer f.stop(); try f.login();
var buffer: [256]u8 = undefined; const created = try f.client().makeDirectory("pub", &buffer); try testing.expectEqualStrings("/pub", created);
try f.client().changeDirectory("pub"); var cwd: [256]u8 = undefined; try testing.expectEqualStrings("/pub", try f.client().workingDirectory(&cwd));
// A relative path is resolved against the working directory. var source: Io.Reader = .fixed("inside"); _ = try f.client().store("file.txt", &source, .{}); var read_back: [16]u8 = undefined; try testing.expectEqualStrings("inside", try f.readFile("pub/file.txt", &read_back));
try f.client().rename("file.txt", "renamed.txt"); try testing.expectEqualStrings("inside", try f.readFile("pub/renamed.txt", &read_back));
try f.client().deleteFile("renamed.txt"); try f.client().changeToParent(); try testing.expectEqualStrings("/", try f.client().workingDirectory(&cwd)); try f.client().removeDirectory("pub");
try testing.expectError(error.CommandFailed, f.client().changeDirectory("pub"));}
test "size and modification time" { var f: Fixture = undefined; try f.start(.read_only); defer f.stop(); try f.writeFile("sized", "0123456789"); try f.login();
try testing.expectEqual(@as(u64, 10), try f.client().size("sized"));
const modified = try f.client().modificationTime("sized"); const now = ftp.protocol.time.secondsFromTimestamp(Io.Timestamp.now(testing.io, .real)); // The file was written moments ago. A generous window, because the test // is about the timestamp surviving the round trip and not about clocks. try testing.expect(modified <= now + 60 and modified > now - 3600);
// RFC 3659 §4.2: `SIZE` in ASCII mode has no answer worth giving, which // is why `Client.size` switches to `TYPE I` before it asks. Sent by hand, // the refusal is visible. try f.client().setType(.{ .ascii = .non_print }); try testing.expectEqual(@as(u16, 550), (try f.client().command(.size, "sized")).code.value);}
test "MLST answers on the control connection" { var f: Fixture = undefined; try f.start(.read_only); defer f.stop(); try f.writeFile("one.txt", "abc"); try f.login();
var buffer: [512]u8 = undefined; const entry = try f.client().statPath("one.txt", &buffer); try testing.expectEqualStrings("/one.txt", entry.name); try testing.expectEqual(ftp.protocol.listing.Kind.file, entry.kind); try testing.expectEqual(@as(?u64, 3), entry.size);}
test "a client cannot name anything outside the served directory" { var f: Fixture = undefined; try f.start(.all); defer f.stop(); try f.writeFile("inside", "safe"); try f.login();
// Every one of these resolves back to somewhere inside the root, so none // of them finds anything -- rather than finding the real /etc/passwd. const attempts = [_][]const u8{ "../../../../../../etc/passwd", "/../../etc/passwd", "..", "/..", "pub/../../../etc/passwd", }; var buffer: [64]u8 = undefined; for (attempts) |attempt| { var w: Io.Writer = .fixed(&buffer); try testing.expectError(error.CommandFailed, f.client().retrieve(attempt, &w, .{})); }
// `CWD ..` at the root stays at the root rather than climbing out of it. try f.client().changeToParent(); var cwd: [64]u8 = undefined; try testing.expectEqualStrings("/", try f.client().workingDirectory(&cwd));
// And what is inside is still reachable, so the containment has not // simply broken everything. var w: Io.Writer = .fixed(&buffer); _ = try f.client().retrieve("inside", &w, .{}); try testing.expectEqualStrings("safe", w.buffered());}
test "active mode" { var f: Fixture = undefined; try f.start(.read_only); defer f.stop(); try f.writeFile("active.txt", "over a connection the server opened"); try f.login();
// The server connects back to the client for the data, which is what // `PORT` and `EPRT` are for and what almost nothing does any more. f.client().options.passive = false;
var buffer: [64]u8 = undefined; var w: Io.Writer = .fixed(&buffer); _ = try f.client().retrieve("active.txt", &w, .{}); try testing.expectEqualStrings("over a connection the server opened", w.buffered());
var listing = try f.client().list(Fixture.gpa, null); defer listing.deinit(Fixture.gpa); _ = try expectListed(listing, "active.txt");}
test "PASV rather than EPSV" { var f: Fixture = undefined; try f.start(.read_only); defer f.stop(); try f.writeFile("old.txt", "for a client that predates RFC 2428"); try f.login();
f.client().options.extended_addressing = false;
var buffer: [64]u8 = undefined; var w: Io.Writer = .fixed(&buffer); _ = try f.client().retrieve("old.txt", &w, .{}); try testing.expectEqualStrings("for a client that predates RFC 2428", w.buffered());}
test "PORT rather than EPRT" { var f: Fixture = undefined; try f.start(.read_only); defer f.stop(); try f.writeFile("old.txt", "the oldest way of all"); try f.login();
f.client().options.passive = false; f.client().options.extended_addressing = false;
var buffer: [64]u8 = undefined; var w: Io.Writer = .fixed(&buffer); _ = try f.client().retrieve("old.txt", &w, .{}); try testing.expectEqualStrings("the oldest way of all", w.buffered());}
test "a transfer command with no data connection arranged" { var f: Fixture = undefined; try f.start(.read_only); defer f.stop(); try f.writeFile("x", "x"); try f.login();
// Sent by hand, without the `PASV` the client would normally send first. const reply = try f.client().command(.retr, "x"); try testing.expectEqual(@as(u16, 425), reply.code.value); try f.client().noop();}
test "STOU picks a name and says which" { var f: Fixture = undefined; try f.start(.all); defer f.stop(); try f.login();
// No wrapper for `STOU` on the client, because the reply has to be read // for the name and there is no shape of API that makes that pleasant. _ = try f.client().command(.epsv, null); const port = try ftp.protocol.address.parseExtendedPassiveReply(f.client().last_reply.firstLine()); var target = f.client().server_address; target.setPort(port); const data = try net.IpAddress.connect(&target, testing.io, .{ .mode = .stream });
try f.client().send(.stou, null); try testing.expect((try f.client().receive()).code.isPositive()); var write_buffer: [64]u8 = undefined; var data_writer = data.writer(testing.io, &write_buffer); try data_writer.interface.writeAll("unique contents"); try data_writer.interface.flush(); data.close(testing.io);
const final = try f.client().receiveFinal(); try testing.expectEqual(@as(u16, 226), final.code.value); // RFC 1123 §4.1.2.9: the name must be reported as "FILE: <name>". try testing.expect(std.mem.startsWith(u8, final.firstLine(), "FILE: /ftp"));}
test "STAT with a path lists on the control connection" { var f: Fixture = undefined; try f.start(.read_only); defer f.stop(); try f.writeFile("statted.txt", "abc"); try f.login();
const reply = try f.client().command(.stat, "/"); try testing.expectEqual(@as(u16, 212), reply.code.value); try testing.expect(std.mem.find(u8, reply.text, "statted.txt") != null);}
test "LIST with the flags clients still send" { var f: Fixture = undefined; try f.start(.read_only); defer f.stop(); try f.writeFile("flagged.txt", "abc"); try f.login();
// `LIST -la` used to go to a shell. Nothing here runs one, but clients // still send the flags and a server that read them as a pathname would // answer 550 to a working client. for ([_][]const u8{ "-l", "-la", "-al /", "-l /" }) |arg| { var text: std.ArrayList(u8) = .empty; defer text.deinit(Fixture.gpa); var names: std.ArrayList([]const u8) = .empty; defer names.deinit(Fixture.gpa); // `NLST` takes the same argument and is the easiest to check. try f.client().nameList(Fixture.gpa, arg, &text, &names); try testing.expectEqual(@as(usize, 1), names.items.len); try testing.expectEqualStrings("flagged.txt", names.items[0]); }}
test "LIST and NLST on a file name that one file" { var f: Fixture = undefined; try f.start(.read_only); defer f.stop(); try f.writeFile("single.txt", "abc"); try f.tmp.dir.createDir(testing.io, "pub", .default_dir); try f.writeFile("pub/deep.txt", "d"); try f.login();
// This is what a client's `mget` does to expand each of its arguments, // and a server that answers 550 to it cannot be fetched from that way. for ([_][]const u8{ "single.txt", "/single.txt", "pub/deep.txt" }) |argument| { var text: std.ArrayList(u8) = .empty; defer text.deinit(Fixture.gpa); var names: std.ArrayList([]const u8) = .empty; defer names.deinit(Fixture.gpa); try f.client().nameList(Fixture.gpa, argument, &text, &names); try testing.expectEqual(@as(usize, 1), names.items.len); // Named as it was asked for, so the client can turn round and send it // in a `RETR` without working out what it is relative to. try testing.expectEqualStrings(argument, names.items[0]); }
// `MLSD` is excluded on purpose: RFC 3659 §7.2 gives it directories, and // `MLST` is the one that takes a single path. try testing.expectEqual(@as(u16, 550), (try f.client().command(.mlsd, "single.txt")).code.value);
// And a path that is not there is still a 550 either way. var text: std.ArrayList(u8) = .empty; defer text.deinit(Fixture.gpa); var names: std.ArrayList([]const u8) = .empty; defer names.deinit(Fixture.gpa); try testing.expectError( error.CommandFailed, f.client().nameList(Fixture.gpa, "nothing", &text, &names), );}
test "unknown and unimplemented commands" { var f: Fixture = undefined; try f.start(.read_only); defer f.stop(); try f.login();
try f.client().sendName("XYZZY", "plugh"); try testing.expectEqual(@as(u16, 500), (try f.client().receiveFinal()).code.value);
// Named but not offered, which is a different answer: 502 tells a client // to stop asking rather than to check its spelling. try testing.expectEqual(@as(u16, 502), (try f.client().command(.auth, "TLS")).code.value); try testing.expectEqual(@as(u16, 502), (try f.client().command(.site, "CHMOD 777 x")).code.value);
// And the session survives all of it. try f.client().noop();}
test "the session survives commands that make no sense" { var f: Fixture = undefined; try f.start(.all); defer f.stop(); try f.login();
// Out of order. try testing.expectEqual(@as(u16, 503), (try f.client().command(.rnto, "x")).code.value); // Missing arguments. try testing.expectEqual(@as(u16, 501), (try f.client().command(.cwd, null)).code.value); try testing.expectEqual(@as(u16, 501), (try f.client().command(.type, null)).code.value); // Arguments that are not what the command takes. try testing.expectEqual(@as(u16, 501), (try f.client().command(.type, "Q")).code.value); try testing.expectEqual(@as(u16, 504), (try f.client().command(.stru, "R")).code.value); try testing.expectEqual(@as(u16, 504), (try f.client().command(.mode, "B")).code.value); try testing.expectEqual(@as(u16, 501), (try f.client().command(.port, "not an address")).code.value); try testing.expectEqual(@as(u16, 501), (try f.client().command(.rest, "backwards")).code.value);
try f.client().noop();}
test "the FTP bounce attack is refused" { var f: Fixture = undefined; try f.start(.read_only); defer f.stop(); try f.login();
// An address that is not the client's own. Accepting it would let anyone // who can log in use the server to reach hosts and ports they cannot. const reply = try f.client().command(.eprt, "|1|192.0.2.99|8080|"); try testing.expectEqual(@as(u16, 501), reply.code.value);
// A privileged port is refused even when the host is right. const low = try f.client().command(.eprt, "|1|127.0.0.1|22|"); try testing.expectEqual(@as(u16, 501), low.code.value);}
test "EPSV ALL forbids the other ways of opening a data connection" { var f: Fixture = undefined; try f.start(.read_only); defer f.stop(); try f.writeFile("x", "x"); try f.login();
try testing.expectEqual(@as(u16, 200), (try f.client().command(.epsv, "ALL")).code.value); try testing.expectEqual(@as(u16, 501), (try f.client().command(.pasv, null)).code.value); try testing.expectEqual(@as(u16, 501), (try f.client().command(.eprt, "|1|127.0.0.1|9999|")).code.value);
// `EPSV` itself still works, which is the point of the promise. var buffer: [16]u8 = undefined; var w: Io.Writer = .fixed(&buffer); _ = try f.client().retrieve("x", &w, .{}); try testing.expectEqualStrings("x", w.buffered());}
test "a file name with spaces in it" { var f: Fixture = undefined; try f.start(.all); defer f.stop(); try f.login();
const name = "two spaces and a trailing one "; var source: Io.Reader = .fixed("contents"); _ = try f.client().store(name, &source, .{});
var buffer: [64]u8 = undefined; try testing.expectEqualStrings("contents", try f.readFile(name, &buffer));
var listing = try f.client().list(Fixture.gpa, null); defer listing.deinit(Fixture.gpa); _ = try expectListed(listing, name);
var w: Io.Writer = .fixed(&buffer); _ = try f.client().retrieve(name, &w, .{}); try testing.expectEqualStrings("contents", w.buffered());}
test "a pathname a client tried to smuggle a command into" { var f: Fixture = undefined; try f.start(.all); defer f.stop(); try f.login();
// The client refuses to send it at all, which is where this has to be // stopped: once it is on the wire it is two commands. var buffer: [16]u8 = undefined; var w: Io.Writer = .fixed(&buffer); try testing.expectError( error.InvalidArgument, f.client().retrieve("x\r\nDELE everything", &w, .{}), ); try f.client().noop();}
test "several clients at once" { var f: Fixture = undefined; try f.start(.read_only); defer f.stop(); try f.writeFile("shared.txt", "read by everybody"); try f.login();
// The fixture's own client is already connected; two more join it, and // all three are served before any of them leaves. var second: ftp.Client.Connection = undefined; try second.connect(testing.io, Fixture.gpa, f.server.localAddress(), .{}); defer second.close(); var third: ftp.Client.Connection = undefined; try third.connect(testing.io, Fixture.gpa, f.server.localAddress(), .{}); defer third.close();
try second.client.login("anonymous", "second@example.com", null); try third.client.login("anonymous", "third@example.com", null);
for ([_]*ftp.Client{ f.client(), &second.client, &third.client }) |client| { var buffer: [64]u8 = undefined; var w: Io.Writer = .fixed(&buffer); _ = try client.retrieve("shared.txt", &w, .{}); try testing.expectEqualStrings("read by everybody", w.buffered()); }}
test "a client that hangs up without saying QUIT" { var f: Fixture = undefined; try f.start(.read_only); defer f.stop(); try f.login();
var rude: ftp.Client.Connection = undefined; try rude.connect(testing.io, Fixture.gpa, f.server.localAddress(), .{}); try rude.client.login("anonymous", "rude@example.com", null); rude.close();
// The server must still be serving, which the fixture's own client proves. try f.client().noop();}