irondrop 2.7.0

Drop files, not dependencies - a well tested fully featured & battle-ready server in a single Rust binary with support for indexing through 10M files.
Documentation
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
// SPDX-License-Identifier: MIT
//! SSL/TLS integration tests for the file server.

use irondrop::cli::Cli;
use irondrop::server::run_server;
use rcgen::generate_simple_self_signed;
use reqwest::StatusCode;
use reqwest::blocking::Client;
use std::fs::{self, File};
use std::io::Write;
use std::net::SocketAddr;
use std::path::PathBuf;
use std::sync::mpsc;
use std::thread::{self, JoinHandle};
use tempfile::{TempDir, tempdir};

/// A helper struct to manage a running test server with SSL support.
struct TestServer {
    addr: SocketAddr,
    shutdown_tx: mpsc::Sender<()>,
    handle: Option<JoinHandle<()>>,
    _temp_dir: TempDir,
}

impl Drop for TestServer {
    fn drop(&mut self) {
        if let Some(handle) = self.handle.take() {
            self.shutdown_tx.send(()).ok();
            handle.join().unwrap();
        }
    }
}

/// Ensure the rustls crypto provider is installed (idempotent).
fn install_crypto_provider() {
    let _ = rustls::crypto::ring::default_provider().install_default();
}

/// Generate self-signed TLS certificates for testing.
fn generate_test_certs(dir: &std::path::Path) -> (PathBuf, PathBuf) {
    let subject_alt_names = vec!["localhost".to_string(), "127.0.0.1".to_string()];
    let cert = generate_simple_self_signed(subject_alt_names).unwrap();

    let cert_path = dir.join("cert.pem");
    let key_path = dir.join("key.pem");

    fs::write(&cert_path, cert.cert.pem()).unwrap();
    fs::write(&key_path, cert.signing_key.serialize_pem()).unwrap();

    (cert_path, key_path)
}

/// Build a reqwest client that accepts self-signed certificates.
fn https_client() -> Client {
    Client::builder()
        .danger_accept_invalid_certs(true)
        .build()
        .unwrap()
}

/// Start an HTTPS test server with optional authentication.
fn setup_ssl_server(username: Option<String>, password: Option<String>) -> TestServer {
    install_crypto_provider();
    let dir = tempdir().unwrap();

    // Create a test file for downloads.
    let file_path = dir.path().join("test.txt");
    let mut file = File::create(&file_path).unwrap();
    writeln!(file, "hello from ssl test file").unwrap();

    // Create a subdirectory with a file for directory listing tests.
    let sub_dir = dir.path().join("subdir");
    fs::create_dir(&sub_dir).unwrap();
    let sub_file = sub_dir.join("nested.txt");
    let mut f = File::create(&sub_file).unwrap();
    writeln!(f, "nested content").unwrap();

    let (cert_path, key_path) = generate_test_certs(dir.path());

    let cli = Cli {
        directory: dir.path().to_path_buf(),
        listen: Some("127.0.0.1".to_string()),
        port: Some(0),
        allowed_extensions: Some("*".to_string()),
        threads: Some(4),
        chunk_size: Some(1024),
        verbose: Some(false),
        detailed_logging: Some(false),
        username,
        password,
        enable_upload: Some(false),
        max_upload_size: Some(10240),
        enable_webdav: Some(false),
        config_file: None,
        log_dir: None,
        ssl_cert: Some(cert_path),
        ssl_key: Some(key_path),
    };

    let (shutdown_tx, shutdown_rx) = mpsc::channel();
    let (addr_tx, addr_rx) = mpsc::channel();

    let server_handle = thread::spawn(move || {
        if let Err(e) = run_server(cli, Some(shutdown_rx), Some(addr_tx)) {
            eprintln!("SSL server thread failed: {e}");
        }
    });

    let server_addr = addr_rx.recv().unwrap();

    TestServer {
        addr: server_addr,
        shutdown_tx,
        handle: Some(server_handle),
        _temp_dir: dir,
    }
}

// ---------------------------------------------------------------------------
// Test 1: Basic HTTPS request returns 200 OK
// ---------------------------------------------------------------------------
#[test]
fn test_https_basic_request() {
    let server = setup_ssl_server(None, None);
    let client = https_client();

    let res = client
        .get(format!("https://{}/", server.addr))
        .send()
        .unwrap();
    assert_eq!(res.status(), StatusCode::OK);
}

// ---------------------------------------------------------------------------
// Test 2: File download over HTTPS returns correct content
// ---------------------------------------------------------------------------
#[test]
fn test_https_file_download() {
    let server = setup_ssl_server(None, None);
    let client = https_client();

    let res = client
        .get(format!("https://{}/test.txt", server.addr))
        .send()
        .unwrap();
    assert_eq!(res.status(), StatusCode::OK);
    assert_eq!(res.text().unwrap(), "hello from ssl test file\n");
}

// ---------------------------------------------------------------------------
// Test 3: Directory listing works over HTTPS
// ---------------------------------------------------------------------------
#[test]
fn test_https_directory_listing() {
    let server = setup_ssl_server(None, None);
    let client = https_client();

    let res = client
        .get(format!("https://{}/", server.addr))
        .send()
        .unwrap();
    assert_eq!(res.status(), StatusCode::OK);
    let body = res.text().unwrap();
    assert!(body.contains("test.txt"), "listing should contain test.txt");
    assert!(body.contains("subdir"), "listing should contain subdir");
}

// ---------------------------------------------------------------------------
// Test 4: HTTPS with basic authentication
// ---------------------------------------------------------------------------
#[test]
fn test_https_with_authentication() {
    let server = setup_ssl_server(Some("admin".to_string()), Some("secret".to_string()));
    let client = https_client();

    // Without credentials -> 401 Unauthorized
    let res = client
        .get(format!("https://{}/", server.addr))
        .send()
        .unwrap();
    assert_eq!(res.status(), StatusCode::UNAUTHORIZED);
    assert!(res.headers().contains_key("www-authenticate"));

    // With correct credentials -> 200 OK
    let res = client
        .get(format!("https://{}/", server.addr))
        .basic_auth("admin", Some("secret"))
        .send()
        .unwrap();
    assert_eq!(res.status(), StatusCode::OK);
    let body = res.text().unwrap();
    assert!(body.contains("test.txt"));
}

// ---------------------------------------------------------------------------
// Test 5: Health endpoint works over HTTPS
// ---------------------------------------------------------------------------
#[test]
fn test_https_health_endpoint() {
    let server = setup_ssl_server(None, None);
    let client = https_client();

    let res = client
        .get(format!("https://{}/_irondrop/health", server.addr))
        .send()
        .unwrap();
    assert_eq!(res.status(), StatusCode::OK);
}

// ---------------------------------------------------------------------------
// Test 5b: Monitor endpoint works over HTTPS (HTML and JSON)
// ---------------------------------------------------------------------------
#[test]
fn test_https_monitor_endpoint() {
    let server = setup_ssl_server(None, None);
    let client = https_client();

    // HTML monitor page
    let res = client
        .get(format!("https://{}/_irondrop/monitor", server.addr))
        .send()
        .unwrap();
    assert_eq!(res.status(), StatusCode::OK);
    let body = res.text().unwrap();
    assert!(
        body.contains("html") || body.contains("HTML"),
        "monitor should return an HTML page"
    );

    // JSON monitor endpoint
    let res = client
        .get(format!("https://{}/_irondrop/monitor?json=1", server.addr))
        .send()
        .unwrap();
    assert_eq!(res.status(), StatusCode::OK);
    let body = res.text().unwrap();
    assert!(
        body.contains("total_requests") || body.contains("uptime"),
        "JSON monitor should contain stats fields"
    );

    // Legacy /monitor path
    let res = client
        .get(format!("https://{}/monitor", server.addr))
        .send()
        .unwrap();
    assert_eq!(res.status(), StatusCode::OK);
}

// ---------------------------------------------------------------------------
// Test 6: Server fails to start with non-existent cert file
// ---------------------------------------------------------------------------
#[test]
fn test_ssl_missing_cert_file() {
    install_crypto_provider();
    let dir = tempdir().unwrap();

    // Create a valid key but point cert to a non-existent file.
    let (_, key_path) = generate_test_certs(dir.path());
    let bogus_cert = dir.path().join("nonexistent_cert.pem");

    let cli = Cli {
        directory: dir.path().to_path_buf(),
        listen: Some("127.0.0.1".to_string()),
        port: Some(0),
        allowed_extensions: Some("*".to_string()),
        threads: Some(4),
        chunk_size: Some(1024),
        verbose: Some(false),
        detailed_logging: Some(false),
        username: None,
        password: None,
        enable_upload: Some(false),
        max_upload_size: Some(10240),
        enable_webdav: Some(false),
        config_file: None,
        log_dir: None,
        ssl_cert: Some(bogus_cert),
        ssl_key: Some(key_path),
    };

    let (shutdown_tx, shutdown_rx) = mpsc::channel();
    let result = run_server(cli, Some(shutdown_rx), None);
    assert!(
        result.is_err(),
        "run_server should fail with missing cert file"
    );
    drop(shutdown_tx);
}

// ---------------------------------------------------------------------------
// Test 7: Server fails to start with non-existent key file
// ---------------------------------------------------------------------------
#[test]
fn test_ssl_missing_key_file() {
    install_crypto_provider();
    let dir = tempdir().unwrap();

    // Create a valid cert but point key to a non-existent file.
    let (cert_path, _) = generate_test_certs(dir.path());
    let bogus_key = dir.path().join("nonexistent_key.pem");

    let cli = Cli {
        directory: dir.path().to_path_buf(),
        listen: Some("127.0.0.1".to_string()),
        port: Some(0),
        allowed_extensions: Some("*".to_string()),
        threads: Some(4),
        chunk_size: Some(1024),
        verbose: Some(false),
        detailed_logging: Some(false),
        username: None,
        password: None,
        enable_upload: Some(false),
        max_upload_size: Some(10240),
        enable_webdav: Some(false),
        config_file: None,
        log_dir: None,
        ssl_cert: Some(cert_path),
        ssl_key: Some(bogus_key),
    };

    let (shutdown_tx, shutdown_rx) = mpsc::channel();
    let result = run_server(cli, Some(shutdown_rx), None);
    assert!(
        result.is_err(),
        "run_server should fail with missing key file"
    );
    drop(shutdown_tx);
}

// ---------------------------------------------------------------------------
// Test 8: Validation error when only cert is provided (no key)
// ---------------------------------------------------------------------------
#[test]
fn test_ssl_cert_without_key() {
    let dir = tempdir().unwrap();
    let (cert_path, _) = generate_test_certs(dir.path());

    let cli = Cli {
        directory: dir.path().to_path_buf(),
        listen: Some("127.0.0.1".to_string()),
        port: Some(0),
        allowed_extensions: Some("*".to_string()),
        threads: Some(4),
        chunk_size: Some(1024),
        verbose: Some(false),
        detailed_logging: Some(false),
        username: None,
        password: None,
        enable_upload: Some(false),
        max_upload_size: Some(10240),
        enable_webdav: Some(false),
        config_file: None,
        log_dir: None,
        ssl_cert: Some(cert_path),
        ssl_key: None,
    };

    let result = cli.validate();
    assert!(
        result.is_err(),
        "validate() should fail when ssl_cert is set without ssl_key"
    );
    let err_msg = format!("{}", result.unwrap_err());
    assert!(
        err_msg.contains("Both --ssl-cert and --ssl-key must be provided together"),
        "error message should mention both flags are required, got: {err_msg}"
    );
}

// ---------------------------------------------------------------------------
// Test 9: Validation error when only key is provided (no cert)
// ---------------------------------------------------------------------------
#[test]
fn test_ssl_key_without_cert() {
    let dir = tempdir().unwrap();
    let (_, key_path) = generate_test_certs(dir.path());

    let cli = Cli {
        directory: dir.path().to_path_buf(),
        listen: Some("127.0.0.1".to_string()),
        port: Some(0),
        allowed_extensions: Some("*".to_string()),
        threads: Some(4),
        chunk_size: Some(1024),
        verbose: Some(false),
        detailed_logging: Some(false),
        username: None,
        password: None,
        enable_upload: Some(false),
        max_upload_size: Some(10240),
        enable_webdav: Some(false),
        config_file: None,
        log_dir: None,
        ssl_cert: None,
        ssl_key: Some(key_path),
    };

    let result = cli.validate();
    assert!(
        result.is_err(),
        "validate() should fail when ssl_key is set without ssl_cert"
    );
    let err_msg = format!("{}", result.unwrap_err());
    assert!(
        err_msg.contains("Both --ssl-cert and --ssl-key must be provided together"),
        "error message should mention both flags are required, got: {err_msg}"
    );
}

// ---------------------------------------------------------------------------
// Test 10: Plain HTTP still works when no SSL config is provided
// ---------------------------------------------------------------------------
#[test]
fn test_http_still_works_without_ssl() {
    let dir = tempdir().unwrap();

    let file_path = dir.path().join("hello.txt");
    let mut file = File::create(&file_path).unwrap();
    writeln!(file, "plain http content").unwrap();

    let cli = Cli {
        directory: dir.path().to_path_buf(),
        listen: Some("127.0.0.1".to_string()),
        port: Some(0),
        allowed_extensions: Some("*".to_string()),
        threads: Some(4),
        chunk_size: Some(1024),
        verbose: Some(false),
        detailed_logging: Some(false),
        username: None,
        password: None,
        enable_upload: Some(false),
        max_upload_size: Some(10240),
        enable_webdav: Some(false),
        config_file: None,
        log_dir: None,
        ssl_cert: None,
        ssl_key: None,
    };

    let (shutdown_tx, shutdown_rx) = mpsc::channel();
    let (addr_tx, addr_rx) = mpsc::channel();

    let server_handle = thread::spawn(move || {
        if let Err(e) = run_server(cli, Some(shutdown_rx), Some(addr_tx)) {
            eprintln!("HTTP server thread failed: {e}");
        }
    });

    let server_addr = addr_rx.recv().unwrap();

    let server = TestServer {
        addr: server_addr,
        shutdown_tx,
        handle: Some(server_handle),
        _temp_dir: dir,
    };

    let client = Client::new();
    let res = client
        .get(format!("http://{}/hello.txt", server.addr))
        .send()
        .unwrap();
    assert_eq!(res.status(), StatusCode::OK);
    assert_eq!(res.text().unwrap(), "plain http content\n");
}