smb-server 0.12.1

SMB2/3 file-sharing server library with pluggable storage backends.
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
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
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
//! What a read-only backend refuses, what a read asks the store for, and how
//! the backend is built.

use std::sync::Arc;
use std::time::{Duration, Instant, SystemTime};

use tokio::sync::Notify;

use super::fixture::{MIB, OLD, TestBucket, foreground, pattern, put};
use super::gateway::REGION;
use super::moves::path;
use super::proxy::{Fault, Proxy, Reply};
use super::share::{
    DELETE, FILE_CREATE, FILE_DELETE_ON_CLOSE, FILE_OPEN_IF, FILE_OVERWRITE, FILE_OVERWRITE_IF,
    READ, Rig, Spec, WRITE, WRITE_ATTRIBUTES, builder, filetime, heads, is_data_read, listings,
    names, open_handle, put_with, reads, requests,
};
use super::writes::changes;
use crate::ntstatus::{
    STATUS_ACCESS_DENIED, STATUS_FILE_IS_A_DIRECTORY, STATUS_OBJECT_NAME_NOT_FOUND,
    STATUS_OBJECT_PATH_NOT_FOUND, STATUS_UNEXPECTED_IO_ERROR,
};
use crate::s3::error::S3Error;
use crate::{
    FileTimes, OpenIntent, OpenOptions, S3Backend, ShareBackend, SmbError, SmbPath, VolumeInfo,
};

#[tokio::test]
async fn every_open_that_would_change_something_is_refused() {
    let mut rig = Rig::new().await;
    put(rig.store(), "file.txt", b"data").await;
    put_with(rig.store(), "dir/", b"", &[]).await;
    put(rig.store(), "dir/inner", b"x").await;
    let write_flags = [WRITE, WRITE_ATTRIBUTES, WRITE | WRITE_ATTRIBUTES];
    for name in [
        "file.txt",
        "dir",
        "missing.txt",
        "nodir\\missing.txt",
        "dir\\new.txt",
    ] {
        for disposition in [FILE_CREATE, FILE_OPEN_IF, FILE_OVERWRITE, FILE_OVERWRITE_IF] {
            let spec = Spec::ANY.disposition(disposition);
            let status = rig.smb.create(name, spec).await.unwrap_err();
            // The server asks whether a file exists before it truncates it,
            // and a name that is missing is not found.
            let expected = match (disposition, name) {
                (FILE_OVERWRITE, "missing.txt" | "dir\\new.txt") => STATUS_OBJECT_NAME_NOT_FOUND,
                (FILE_OVERWRITE, "nodir\\missing.txt") => STATUS_OBJECT_PATH_NOT_FOUND,
                _ => STATUS_ACCESS_DENIED,
            };
            assert_eq!(status, expected, "{name} disposition {disposition}");
        }
        for access in write_flags {
            let spec = Spec::ANY.access(READ | access);
            let status = rig.smb.create(name, spec).await.unwrap_err();
            assert_eq!(status, STATUS_ACCESS_DENIED, "{name} access {access:#x}");
        }
        let delete = Spec::ANY
            .access(READ | DELETE)
            .options(FILE_DELETE_ON_CLOSE);
        assert_eq!(
            rig.smb.create(name, delete).await.unwrap_err(),
            STATUS_ACCESS_DENIED,
            "{name} delete on close"
        );
    }
    for name in ["newdir", "dir\\newdir", "file.txt", "dir"] {
        let make = Spec::DIR.disposition(FILE_CREATE);
        assert_eq!(
            rig.smb.create(name, make).await.unwrap_err(),
            STATUS_ACCESS_DENIED,
            "{name}"
        );
        let open_or_make = Spec::DIR.disposition(FILE_OPEN_IF);
        assert_eq!(
            rig.smb.create(name, open_or_make).await.unwrap_err(),
            STATUS_ACCESS_DENIED,
            "{name}"
        );
    }
    assert_eq!(changes(&rig), 0);
}

#[tokio::test]
async fn the_backend_refuses_each_kind_of_change_directly() {
    let rig = Rig::new().await;
    put(rig.store(), "file.txt", b"data").await;
    put(rig.store(), "dir/inner", b"x").await;
    let backend = &rig.backend;
    let sent = requests(&rig.proxy);
    let intents = [
        OpenIntent::Create,
        OpenIntent::OpenOrCreate,
        OpenIntent::OverwriteOrCreate,
        OpenIntent::Truncate,
    ];
    for intent in intents {
        for name in ["file.txt", "dir", "none"] {
            let opts = OpenOptions {
                intent,
                ..OpenOptions::default()
            };
            assert!(
                matches!(
                    backend.open(&path(name), opts).await,
                    Err(SmbError::AccessDenied)
                ),
                "{name} {intent:?}"
            );
        }
    }
    for opts in [
        OpenOptions {
            write: true,
            ..OpenOptions::default()
        },
        OpenOptions {
            write_attributes: true,
            ..OpenOptions::default()
        },
        OpenOptions {
            delete_on_close: true,
            ..OpenOptions::default()
        },
    ] {
        assert!(matches!(
            backend.open(&path("file.txt"), opts).await,
            Err(SmbError::AccessDenied)
        ));
    }
    assert!(matches!(
        backend.unlink(&path("file.txt")).await,
        Err(SmbError::AccessDenied)
    ));
    assert!(matches!(
        backend.unlink(&path("dir")).await,
        Err(SmbError::AccessDenied)
    ));
    assert!(matches!(
        backend.unlink(&SmbPath::root()).await,
        Err(SmbError::AccessDenied)
    ));
    assert!(matches!(
        backend
            .rename(&path("file.txt"), &path("other.txt"), false)
            .await,
        Err(SmbError::AccessDenied)
    ));

    assert_eq!(
        requests(&rig.proxy),
        sent,
        "a refused call asks the store nothing"
    );

    let file = open_handle(backend, "file.txt").await;
    let dir = open_handle(backend, "dir").await;
    let sent = requests(&rig.proxy);
    for handle in [&file, &dir] {
        assert!(matches!(
            handle.write(0, b"x").await,
            Err(SmbError::AccessDenied)
        ));
        assert!(matches!(
            handle.write_owned(0, vec![1, 2, 3]).await,
            Err(SmbError::AccessDenied)
        ));
        assert!(matches!(
            handle.truncate(0).await,
            Err(SmbError::AccessDenied)
        ));
        assert!(matches!(
            handle.set_times(FileTimes::all(SystemTime::now())).await,
            Err(SmbError::AccessDenied)
        ));
        assert!(matches!(
            handle.set_attributes(0x1).await,
            Err(SmbError::AccessDenied)
        ));
    }
    assert_eq!(requests(&rig.proxy), sent);
    assert_eq!(changes(&rig), 0);
}

#[tokio::test]
async fn a_rename_or_a_delete_through_the_server_changes_nothing() {
    let mut rig = Rig::new().await;
    put(rig.store(), "file.txt", b"data").await;
    put(rig.store(), "dir/inner", b"x").await;
    for name in ["file.txt", "dir"] {
        let open = Spec::ANY.access(READ | DELETE);
        let target = rig.smb.create(name, open).await.unwrap();
        assert_eq!(
            rig.smb.rename(target.id, "renamed").await,
            STATUS_ACCESS_DENIED,
            "{name}"
        );
        rig.smb.close(target.id).await;
    }
    // Marking an open for deletion makes the server delete at the last close,
    // which the backend refuses.
    let target = rig
        .smb
        .create("file.txt", Spec::FILE.access(READ | DELETE))
        .await
        .unwrap();
    let _ = rig.smb.set_delete(target.id).await;
    rig.smb.close(target.id).await;
    let other = rig
        .smb
        .create("dir", Spec::DIR.access(READ | DELETE))
        .await
        .unwrap();
    let _ = rig.smb.set_delete(other.id).await;
    rig.smb.close(other.id).await;

    assert_eq!(rig.smb.contents("file.txt", 100).await, b"data");
    assert_eq!(rig.smb.contents("dir\\inner", 100).await, b"x");
    assert_eq!(
        rig.smb.open("renamed").await.unwrap_err(),
        STATUS_OBJECT_NAME_NOT_FOUND
    );
    assert_eq!(changes(&rig), 0);
}

#[tokio::test]
async fn the_handle_of_a_file_and_of_a_directory_answer_what_they_are() {
    let rig = Rig::new().await;
    put(rig.store(), "file.txt", b"data").await;
    put(rig.store(), "dir/inner", b"x").await;
    let file = open_handle(&rig.backend, "file.txt").await;
    let dir = open_handle(&rig.backend, "dir").await;
    file.flush().await.unwrap();
    dir.flush().await.unwrap();
    assert!(matches!(dir.read(0, 10).await, Err(SmbError::IsDirectory)));

    let info = file.stat().await.unwrap();
    assert_eq!(info.name, "file.txt");
    assert_eq!((info.end_of_file, info.allocation_size), (4, 4096));
    assert!(!info.is_directory && !info.read_only);
    let info = dir.stat().await.unwrap();
    assert_eq!(info.name, "dir");
    assert!(info.is_directory);
    assert_eq!((info.end_of_file, info.allocation_size), (0, 0));
    file.close().await.unwrap();
    dir.close().await.unwrap();
}

#[tokio::test]
async fn stat_makes_no_request_and_a_close_makes_none_either() {
    let rig = Rig::new().await;
    put(rig.store(), "file.txt", b"data").await;
    let file = open_handle(&rig.backend, "file.txt").await;
    let sent = requests(&rig.proxy);
    for _ in 0..3 {
        file.stat().await.unwrap();
    }
    file.close().await.unwrap();
    assert_eq!(requests(&rig.proxy), sent);
}

#[tokio::test]
async fn capabilities_and_the_volume_describe_a_read_only_share() {
    let rig = Rig::new().await;
    let caps = rig.backend.capabilities();
    assert!(caps.is_read_only);
    assert!(!caps.case_sensitive);
    assert!(!caps.named_streams && !caps.extended_attributes && !caps.block_cloning);
    assert_eq!(
        rig.backend.volume_info().await.unwrap(),
        VolumeInfo::default()
    );

    let sensitive = Rig::with(|builder| builder.case_insensitive(false)).await;
    let caps = sensitive.backend.capabilities();
    assert!(caps.is_read_only && caps.case_sensitive);
}

#[tokio::test]
async fn an_object_smaller_than_the_default_chunk_is_read_by_one_request_for_all_of_it() {
    let mut rig = Rig::new().await;
    let data = pattern(0, 3 * MIB + 5);
    put(rig.store(), "data.bin", &data).await;
    let file = rig.smb.open("data.bin").await.unwrap();
    let size = data.len() as u64;
    for (offset, len) in [
        (0, 10),
        (100, 65_536),
        (1 << 20, 65_536),
        (size - 5, 1000),
        (size - 1, 1),
    ] {
        let end = (offset + u64::from(len)).min(size);
        let got = rig.smb.read(file.id, offset, len).await.unwrap();
        assert_eq!(got, &data[offset as usize..end as usize]);
    }
    assert_eq!(reads(&rig.proxy), 1);
    let read = rig.proxy.seen().into_iter().find(is_data_read).unwrap();
    let wanted = format!("bytes=0-{}", size - 1);
    assert_eq!(read.header("range"), Some(wanted.as_str()));
}

#[tokio::test]
async fn a_read_of_an_object_that_is_gone_fails() {
    let mut rig = Rig::new().await;
    put(rig.store(), "f.txt", b"data").await;
    let file = rig.smb.open("f.txt").await.unwrap();
    rig.store()
        .delete_object("f.txt", foreground())
        .await
        .unwrap();
    assert_eq!(
        rig.smb.read(file.id, 0, 100).await.unwrap_err(),
        STATUS_OBJECT_NAME_NOT_FOUND
    );
    assert_eq!(
        rig.smb.open("f.txt").await.unwrap_err(),
        STATUS_OBJECT_NAME_NOT_FOUND
    );
}

/// Makes the proxy answer for the object `key`, `abc`, as a store that sends
/// no ETag does. `modified` and `total` are the `Last-Modified` and the
/// `Content-Range` length of ranged reads, and `etag` the `ETag` they carry, if
/// any.
fn bare_object(rig: &Rig, key: &str, modified: &str, total: u64, etag: Option<&str>) {
    let target = format!("/{}/{key}", rig.bucket.name);
    let head = target.clone();
    rig.proxy.inject(
        move |seen| seen.method == "HEAD" && seen.target == head,
        Fault::Reply(
            Reply::new(200, "")
                .declaring(3)
                .header("Last-Modified", OLD)
                .header("X-Amz-Meta-Mtime", "1700000000"),
        ),
        usize::MAX,
    );
    let mut read = Reply::new(206, "abc")
        .header("Last-Modified", modified)
        .header("Content-Range", &format!("bytes 0-2/{total}"));
    if let Some(etag) = etag {
        read = read.header("ETag", etag);
    }
    rig.proxy.inject(
        move |seen| is_data_read(seen) && seen.target == target,
        Fault::Reply(read),
        usize::MAX,
    );
}

#[tokio::test]
async fn an_object_without_an_etag_is_read_without_if_match() {
    let mut rig = Rig::new().await;
    bare_object(&rig, "bare", OLD, 3, None);
    let file = rig.smb.open("bare").await.unwrap();
    assert_eq!(file.size, 3);
    assert_eq!(file.times[2], filetime(1_700_000_000, 0));
    assert_eq!(rig.smb.read(file.id, 0, 10).await.unwrap(), b"abc");
    let read = rig.proxy.seen().pop().unwrap();
    assert_eq!(read.header("range"), Some("bytes=0-2"));
    assert_eq!(read.header("if-match"), None, "there is no ETag to match");
}

#[tokio::test]
async fn an_object_without_an_etag_is_requested_for_every_read() {
    let mut rig = Rig::new().await;
    bare_object(&rig, "bare", OLD, 3, None);
    let file = rig.smb.open("bare").await.unwrap();
    for n in 1..=3 {
        assert_eq!(rig.smb.read(file.id, 0, 10).await.unwrap(), b"abc");
        assert_eq!(reads(&rig.proxy), n);
    }
}

#[tokio::test]
async fn an_object_without_an_etag_that_changes_size_or_time_or_gets_one_is_not_read() {
    for (modified, total, etag) in [
        ("Fri, 25 May 2013 00:00:00 GMT", 3, None),
        (OLD, 4, None),
        (OLD, 3, Some("\"abc\"")),
    ] {
        let mut rig = Rig::new().await;
        // The open sees one object and the read another.
        bare_object(&rig, "bare", OLD, 3, None);
        let file = rig.smb.open("bare").await.unwrap();
        rig.proxy.clear_rules();
        bare_object(&rig, "bare", modified, total, etag);
        let heads_before = heads(&rig.proxy);
        assert_eq!(
            rig.smb.read(file.id, 0, 10).await.unwrap_err(),
            STATUS_UNEXPECTED_IO_ERROR,
            "{modified} {total} {etag:?}"
        );
        assert_eq!(reads(&rig.proxy), 1);
        assert_eq!(heads(&rig.proxy), heads_before, "nothing is refreshed");
    }
}

#[tokio::test]
async fn the_bucket_check_is_one_listing_of_the_prefix_for_one_key() {
    let bucket = TestBucket::new().await;
    let proxy = Proxy::start(bucket.gateway.addr).await;
    for (prefix, wanted) in [("", None), ("a/b", Some("a%2Fb%2F")), ("/x/", Some("x%2F"))] {
        let before = proxy.seen().len();
        builder(&bucket, proxy.addr)
            .prefix(prefix)
            .build()
            .await
            .unwrap();
        let seen = proxy.seen();
        assert_eq!(seen.len(), before + 1, "{prefix:?}");
        let check = &seen[before];
        assert_eq!(check.method, "GET");
        assert!(check.has_param("list-type") && check.has_param("max-keys"));
        assert!(check.query().contains("max-keys=1"), "{}", check.query());
        assert!(!check.has_param("delimiter"));
        match wanted {
            Some(wanted) => assert!(
                check.query().contains(&format!("prefix={wanted}")),
                "{}",
                check.query()
            ),
            None => assert!(!check.has_param("prefix")),
        }
    }
}

#[tokio::test]
async fn a_missing_bucket_fails_the_build_with_the_store_answer() {
    let bucket = TestBucket::new().await;
    let error = S3Backend::builder(
        format!("http://{}", bucket.gateway.addr),
        REGION,
        "no-such-bucket-here",
    )
    .credentials(&bucket.gateway.access_key, &bucket.gateway.secret_key)
    .read_only()
    .build()
    .await
    .unwrap_err();
    assert_eq!(error.http_status(), Some(404), "{error:?}");
    assert_eq!(error.code(), Some("NoSuchBucket"));
}

#[tokio::test]
async fn credentials_the_store_refuses_fail_the_build_and_leak_nothing() {
    let bucket = TestBucket::new().await;
    let secret = "not-the-secret-0123456789";
    for (access, secret_key) in [
        (bucket.gateway.access_key.as_str(), secret),
        ("nobody", bucket.gateway.secret_key.as_str()),
    ] {
        let error = builder(&bucket, bucket.gateway.addr)
            .credentials(access, secret_key)
            .build()
            .await
            .unwrap_err();
        assert_eq!(error.http_status(), Some(403), "{error:?}");
        for shown in [error.to_string(), format!("{error:?}")] {
            for private in [secret, access, secret_key, "Authorization", "Signature="] {
                assert!(!shown.contains(private), "{private} in {shown}");
            }
        }
    }
    let proxy = Proxy::start(bucket.gateway.addr).await;
    proxy.inject(
        |_| true,
        Fault::Reply(Reply::error(403, "AccessDenied")),
        usize::MAX,
    );
    let error = builder(&bucket, proxy.addr)
        .session_token("a-session-token-value")
        .build()
        .await
        .unwrap_err();
    assert_eq!(error.http_status(), Some(403), "{error:?}");
    let sent = proxy.seen().pop().unwrap();
    assert_eq!(
        sent.header("x-amz-security-token"),
        Some("a-session-token-value")
    );
    assert!(!format!("{error:?} {error}").contains("a-session-token-value"));
}

#[tokio::test]
async fn a_backend_that_is_not_read_only_is_refused_before_any_request() {
    let bucket = TestBucket::new().await;
    let proxy = Proxy::start(bucket.gateway.addr).await;
    let error = S3Backend::builder(format!("http://{}", proxy.addr), REGION, &bucket.name)
        .credentials(&bucket.gateway.access_key, &bucket.gateway.secret_key)
        .build()
        .await
        .unwrap_err();
    let S3Error::InvalidArgument(why) = error else {
        panic!("{error:?}");
    };
    assert!(why.contains("read_only()"), "{why}");
    assert!(proxy.seen().is_empty());
}

#[tokio::test]
async fn settings_that_cannot_be_used_are_refused_before_any_request() {
    let bucket = TestBucket::new().await;
    let proxy = Proxy::start(bucket.gateway.addr).await;
    let base = || builder(&bucket, proxy.addr);
    for (what, built) in [
        (
            "zero timeout",
            base().request_timeout(Duration::ZERO).build().await,
        ),
        (
            "zero concurrency",
            base().upload_concurrency(0).build().await,
        ),
        ("bad prefix", base().prefix("a//b").build().await),
        (
            "no credentials",
            S3Backend::builder(format!("http://{}", proxy.addr), REGION, &bucket.name)
                .read_only()
                .build()
                .await,
        ),
        (
            "userinfo",
            S3Backend::builder(
                format!("http://user:hunter2-secret@{}", proxy.addr),
                REGION,
                &bucket.name,
            )
            .credentials("a", "s")
            .read_only()
            .build()
            .await,
        ),
        (
            "scheme",
            S3Backend::builder(format!("ftp://{}", proxy.addr), REGION, &bucket.name)
                .credentials("a", "s")
                .read_only()
                .build()
                .await,
        ),
        (
            "empty region",
            S3Backend::builder(format!("http://{}", proxy.addr), "", &bucket.name)
                .credentials("a", "s")
                .read_only()
                .build()
                .await,
        ),
        (
            "empty bucket",
            S3Backend::builder(format!("http://{}", proxy.addr), REGION, "")
                .credentials("a", "s")
                .read_only()
                .build()
                .await,
        ),
    ] {
        let error = built.map(|_| ()).unwrap_err();
        assert!(
            matches!(error, S3Error::InvalidArgument(_)),
            "{what}: {error:?}"
        );
        let shown = error.to_string();
        assert!(!shown.contains("hunter2-secret"), "{what}: {shown}");
    }
    assert!(proxy.seen().is_empty());
}

#[tokio::test]
async fn the_debug_output_of_a_backend_hides_its_credentials() {
    let rig = Rig::new().await;
    let shown = format!("{:?}", rig.backend);
    assert!(!shown.contains(&rig.bucket.gateway.secret_key), "{shown}");
    assert!(!shown.contains(&rig.bucket.gateway.access_key), "{shown}");
}

#[tokio::test]
async fn clones_share_what_one_of_them_learned() {
    let rig = Rig::new().await;
    put(rig.store(), "f.txt", b"data").await;
    let other = rig.backend.clone();
    open_handle(&rig.backend, "f.txt").await;
    let sent = requests(&rig.proxy);
    let file = open_handle(&other, "f.txt").await;
    assert_eq!(file.stat().await.unwrap().end_of_file, 4);
    assert_eq!(requests(&rig.proxy), sent);
}

#[tokio::test]
async fn a_second_open_within_the_ttl_makes_no_request() {
    let mut rig = Rig::new().await;
    put(rig.store(), "d/f.txt", b"data").await;
    let first = rig.smb.open("d\\f.txt").await.unwrap();
    rig.smb.close(first.id).await;
    let sent = requests(&rig.proxy);
    assert_eq!(heads(&rig.proxy), 1);
    for _ in 0..3 {
        let again = rig.smb.open("d\\f.txt").await.unwrap();
        assert_eq!(again.size, 4);
        rig.smb.close(again.id).await;
    }
    let dir = rig.smb.open_dir("d").await.unwrap();
    rig.smb.close(dir.id).await;
    let dir = rig.smb.open_dir("d").await.unwrap();
    rig.smb.close(dir.id).await;
    // The directory was a new name, so it cost its requests once.
    assert_eq!(requests(&rig.proxy), sent + 2);
}

#[tokio::test]
async fn a_name_that_was_missing_is_remembered_as_missing() {
    let mut rig = Rig::with(|builder| builder.case_insensitive(false)).await;
    put(rig.store(), "d/x", b"x").await;
    for _ in 0..3 {
        assert_eq!(
            rig.smb.open("d\\nope").await.unwrap_err(),
            STATUS_OBJECT_NAME_NOT_FOUND
        );
    }
    // The first call asked for the name and for the directory, the others for nothing.
    assert_eq!(heads(&rig.proxy), 2);
}

#[tokio::test]
async fn a_name_missing_from_a_fresh_listing_costs_no_request() {
    let mut rig = Rig::new().await;
    for key in ["d/a.txt", "d/b.txt"] {
        put(rig.store(), key, b"x").await;
    }
    let dir = rig.smb.open_dir("d").await.unwrap();
    assert_eq!(rig.smb.list(dir.id, "*").await.len(), 2);
    let sent = requests(&rig.proxy);
    for name in ["d\\desktop.ini", "d\\Thumbs.db", "d\\desktop.ini"] {
        assert_eq!(
            rig.smb.open(name).await.unwrap_err(),
            STATUS_OBJECT_NAME_NOT_FOUND,
            "{name}"
        );
    }
    assert_eq!(requests(&rig.proxy), sent);
    // A name the listing has needs no HEAD either, as the listing read it.
    let a = rig.smb.open("d\\a.txt").await.unwrap();
    assert_eq!(a.size, 1);
    assert_eq!(requests(&rig.proxy), sent);
}

#[tokio::test]
async fn an_open_of_a_listed_file_reads_its_metadata_once_when_listings_read_none() {
    let mut rig = Rig::with(|builder| builder.listing_metadata_max(0)).await;
    put_with(rig.store(), "d/a.txt", b"x", &[("mtime", "1700000000")]).await;
    let dir = rig.smb.open_dir("d").await.unwrap();
    let listed = rig.smb.list(dir.id, "*").await;
    assert_eq!(names(&listed), ["a.txt"]);
    assert!(
        listed[0].times[2] > filetime(1_700_001_000, 0),
        "the time the store modified it"
    );
    let (head_count, list_count) = (heads(&rig.proxy), listings(&rig.proxy));
    let opened = rig.smb.open("d\\a.txt").await.unwrap();
    assert_eq!(heads(&rig.proxy), head_count + 1);
    assert_eq!(listings(&rig.proxy), list_count);
    // The open knows the stored time the listing did not.
    assert_eq!(opened.times[2], filetime(1_700_000_000, 0));
    let again = rig.smb.open("d\\a.txt").await.unwrap();
    assert_eq!(heads(&rig.proxy), head_count + 1);
    assert_eq!(again.times, opened.times);
}

#[tokio::test]
async fn an_object_changed_in_the_store_shows_after_the_ttl() {
    let rig = Rig::with(|builder| builder.metadata_ttl(Duration::from_millis(250))).await;
    let store = rig.store().clone();
    put_with(&store, "f.txt", b"one", &[("mtime", "1700000000")]).await;
    let mut smb = rig.smb;
    let first = smb.open("f.txt").await.unwrap();
    assert_eq!(first.size, 3);
    put_with(&store, "f.txt", b"three", &[("mtime", "1700000600")]).await;
    tokio::time::sleep(Duration::from_millis(400)).await;
    let before = heads(&rig.proxy);
    let second = smb.open("f.txt").await.unwrap();
    assert_eq!(second.size, 5);
    assert_eq!(second.times[2], filetime(1_700_000_600, 0));
    assert_eq!(heads(&rig.proxy), before + 1);
}

#[tokio::test]
async fn a_zero_ttl_asks_the_store_every_time() {
    let mut rig = Rig::with(|builder| builder.metadata_ttl(Duration::ZERO)).await;
    put(rig.store(), "f.txt", b"data").await;
    for n in 1..=3 {
        rig.smb.open("f.txt").await.unwrap();
        assert_eq!(heads(&rig.proxy), n);
    }
}

#[tokio::test]
async fn a_directory_wins_over_an_object_of_the_same_name() {
    let mut rig = Rig::new().await;
    put(rig.store(), "both/inner.txt", b"x").await;
    // The gateway cannot hold an object and a directory under one name, so the
    // proxy answers for the object.
    rig.proxy.inject(
        |seen| seen.method == "HEAD" && seen.target.ends_with("/both"),
        Fault::Reply(
            Reply::new(200, "")
                .header("Last-Modified", OLD)
                .header("ETag", "\"abc\""),
        ),
        usize::MAX,
    );
    let opened = rig.smb.create("both", Spec::ANY).await.unwrap();
    assert_eq!(opened.attributes & 0x10, 0x10);
    assert_eq!(
        rig.smb.create("both", Spec::FILE).await.unwrap_err(),
        STATUS_FILE_IS_A_DIRECTORY
    );
    let dir = rig.smb.open_dir("both").await.unwrap();
    assert_eq!(names(&rig.smb.list(dir.id, "*").await), ["inner.txt"]);
}

#[tokio::test]
async fn a_store_that_refuses_access_fails_the_open_as_denied() {
    let mut rig = Rig::new().await;
    put(rig.store(), "f.txt", b"x").await;
    rig.proxy.inject(
        |seen| seen.method == "HEAD",
        Fault::Reply(Reply::new(403, "")),
        usize::MAX,
    );
    assert_eq!(
        rig.smb.open("f.txt").await.unwrap_err(),
        STATUS_ACCESS_DENIED
    );
}

#[tokio::test]
async fn a_store_that_keeps_failing_fails_the_open_after_five_tries() {
    let mut rig = Rig::new().await;
    put(rig.store(), "f.txt", b"x").await;
    rig.proxy.inject(
        |seen| seen.method == "HEAD",
        Fault::Reply(Reply::error(503, "SlowDown")),
        usize::MAX,
    );
    assert_eq!(
        rig.smb.open("f.txt").await.unwrap_err(),
        STATUS_UNEXPECTED_IO_ERROR
    );
    assert_eq!(heads(&rig.proxy), 5);
}

#[tokio::test]
async fn the_requests_of_one_call_share_one_deadline() {
    let mut rig = Rig::with(|builder| builder.request_timeout(Duration::from_secs(2))).await;
    put_with(rig.store(), "d/", b"", &[]).await;
    put(rig.store(), "d/x", b"x").await;
    // The listing of the keys below the directory answers after 1.5 s, and
    // the HEAD of its marker never answers. The marker is asked for with the
    // time that is left of the two seconds, not with two seconds of its own,
    // which would end the call after 3.5 s.
    let gate = Arc::new(Notify::new());
    rig.proxy.inject(
        |seen| {
            seen.method == "GET"
                && seen.has_param("max-keys")
                && seen.query().contains("prefix=d%2F")
        },
        Fault::Hold(Arc::clone(&gate)),
        1,
    );
    rig.proxy.inject(
        |seen| seen.method == "HEAD" && seen.target.ends_with("/d/"),
        Fault::Stall,
        usize::MAX,
    );
    tokio::spawn(async move {
        tokio::time::sleep(Duration::from_millis(1500)).await;
        gate.notify_one();
    });
    let started = Instant::now();
    assert_eq!(
        rig.smb.open_dir("d").await.unwrap_err(),
        STATUS_UNEXPECTED_IO_ERROR
    );
    let elapsed = started.elapsed();
    assert!(elapsed >= Duration::from_millis(1800), "{elapsed:?}");
    assert!(elapsed < Duration::from_millis(3200), "{elapsed:?}");
}

#[tokio::test]
async fn requests_that_go_out_together_stop_together() {
    let mut rig = Rig::with(|builder| builder.request_timeout(Duration::from_secs(2))).await;
    put(rig.store(), "f.txt", b"x").await;
    rig.proxy.inject(|_| true, Fault::Stall, usize::MAX);
    let started = Instant::now();
    assert_eq!(
        rig.smb.open("f.txt").await.unwrap_err(),
        STATUS_UNEXPECTED_IO_ERROR
    );
    let elapsed = started.elapsed();
    assert!(elapsed >= Duration::from_millis(1800), "{elapsed:?}");
    assert!(elapsed < Duration::from_millis(3500), "{elapsed:?}");
}

#[tokio::test]
async fn a_stall_in_a_read_ends_at_the_deadline() {
    let mut rig = Rig::with(|builder| builder.request_timeout(Duration::from_secs(1))).await;
    put(rig.store(), "f.txt", pattern(0, 100)).await;
    let file = rig.smb.open("f.txt").await.unwrap();
    rig.proxy
        .inject(|seen| seen.method == "GET", Fault::Stall, usize::MAX);
    let started = Instant::now();
    assert_eq!(
        rig.smb.read(file.id, 0, 10).await.unwrap_err(),
        STATUS_UNEXPECTED_IO_ERROR
    );
    // A call that waited for the idle limit of a minute would take longer.
    assert!(
        started.elapsed() < Duration::from_secs(5),
        "{:?}",
        started.elapsed()
    );
}