MockForge FTP
FTP protocol support for MockForge.
This crate provides FTP-specific functionality for creating mock FTP servers, including virtual file systems, fixture-driven responses, and file transfer simulation. Perfect for testing FTP clients, file upload workflows, and FTP-based integrations.
Features
- FTP Server Mocking: Full FTP protocol support with standard commands
- Virtual File System: In-memory file storage with configurable content
- Fixture Management: YAML-based configuration for file structures
- Upload Handling: Configurable validation and storage for file uploads
- Template Support: Dynamic content generation using Handlebars templates
- Protocol Compliance: Supports passive/active modes and authentication
Quick Start
Basic FTP Server
use FtpServer;
use FtpConfig;
async
Server with Custom Configuration
use FtpConfig;
use FtpServer;
async
Virtual File System
MockForge FTP includes a powerful virtual file system for creating realistic file structures:
Static Files
use ;
use PathBuf;
Generated Files
use ;
// Generate a 1MB file with random content
let content = Generated ;
// Generate a file with incremental bytes (0, 1, 2, ...)
let incremental = Generated ;
Template Files
use FileContent;
// Use Handlebars templates for dynamic content
let content = Template;
Fixture System
Define complete FTP server configurations using YAML fixtures:
identifier: "test-server"
name: "Test FTP Server"
description: "A test FTP server with sample files"
virtual_files:
- path: "/welcome.txt"
content:
type: "static"
content: "Welcome to MockForge FTP!"
permissions: "644"
owner: "ftp"
group: "ftp"
- path: "/data.bin"
content:
type: "generated"
size: 1048576 # 1MB
pattern: "Random"
permissions: "644"
owner: "ftp"
group: "ftp"
upload_rules:
- path_pattern: "/uploads/.*\\.txt"
auto_accept: true
validation:
max_size_bytes: 1048576 # 1MB limit
allowed_extensions:
storage:
type: "memory"
- path_pattern: "/logs/.*\\.log"
auto_accept: true
storage:
type: "file"
path: "/tmp/ftp_logs"
Loading Fixtures
Load a YAML file before constructing the server:
use FtpConfig;
use ;
For programmatic fixtures in an async application:
use ;
use FileContentConfig;
use FtpConfig;
async
FtpServer::new(config) starts with an empty registry — FtpSpecRegistry
fixtures are bound at construction and cannot be mutated afterwards
(spec_registry() hands out a shared immutable handle). FtpServer::new is a
synchronous constructor; only start() is async. The registry's
with_fixtures builder consumes and returns a registry; it cannot be called on
the immutable handle returned by an already-created server. Use the server
constructor above instead. Use the VFS's async accessors inside async tasks.
Upload Handling
Configure how the server handles file uploads:
Upload Rules
use ;
use Regex;
let rule = UploadRule ;
Storage Options
- Memory: Store uploaded files in the virtual file system
- File: Write to the local file system
- Discard: Accept uploads but don't store them
FTP Protocol Support
MockForge FTP supports standard FTP commands:
- Connection: USER, PASS, QUIT
- Navigation: CWD, CDUP, PWD
- Directory: LIST, NLST, MKD, RMD
- File Operations: RETR, STOR, DELE, RNFR/RNTO
- System: SYST, FEAT, OPTS
- Transfer Modes: Passive and active mode support
Testing FTP Clients
Use MockForge FTP to test FTP client applications:
use FtpStream; // FTP client library
use ;
use FtpConfig;
use task;
async
Configuration
FtpConfig
use FtpConfig;
let config = FtpConfig ;
Integration with MockForge
MockForge FTP integrates seamlessly with the MockForge ecosystem:
- MockForge Core: Shared configuration and logging
- MockForge CLI: Command-line interface for FTP server management
- MockForge Plugins: Extend FTP functionality with custom plugins
Performance
MockForge FTP is optimized for testing scenarios:
- In-Memory Storage: Fast file operations without disk I/O
- Lazy Content Generation: Files generated on-demand
- Concurrent Connections: Handle multiple simultaneous FTP clients
- Low Memory Footprint: Efficient storage for large file simulations
Examples
See the examples directory for complete working examples including:
- Basic FTP server setup
- Fixture-driven configurations
- Upload handling scenarios
- Integration testing patterns
Related Crates
mockforge-core: Core mocking functionalitylibunftp: Underlying FTP server implementationsuppaftp: FTP client for testing
License
Licensed under MIT OR Apache-2.0
Starting the CLI with configuration
For a CLI built with the ftp feature, mockforge ftp serve --config ftp-config.yaml
loads either standalone FTP settings or the ftp section of a full server YAML.
For example:
host: 127.0.0.1
port: 2121
virtual_root: /
fixtures_dir: fixtures
passive_ports:
timeout_secs: 300
fixtures_dir is resolved relative to the configuration file, independently of
the working directory. Each .yaml or .yml file contains one FtpFixture
using the fixture schema above. Missing or invalid configuration and fixture
files fail startup with a diagnostic.
Explicit --port, --host, and --virtual-root flags override file values.
Without a configuration file, the standalone CLI retains port 2121, host
127.0.0.1, and virtual root /. File settings use FtpConfig defaults for omitted
fields. The passive port range and idle timeout apply to actual FTP connections.
Authentication policy and connection caps remain incomplete: the listener uses
libunftp's anonymous authenticator and does not currently enforce
allow_anonymous: false or max_connections. These fields do not provide access
control or a concurrency limit.