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
//! Object keys, and the directory listings built from the keys of a store.

use std::cmp::Ordering;
use std::collections::{HashMap, HashSet};
use std::time::SystemTime;

use crate::error::{SmbError, SmbResult};
use crate::path::is_valid_component;

use super::xml::Listing;

/// Longest object key, in bytes.
pub(super) const MAX_KEY_LEN: usize = 1024;

/// The key of the entry `name` in the directory with key `dir`. The root of a
/// share without a prefix has the key `""`.
pub(super) fn join_key(dir: &str, name: &str) -> String {
    if dir.is_empty() {
        name.to_owned()
    } else {
        format!("{dir}/{name}")
    }
}

/// [`join_key`], failing for a key longer than the store takes.
pub(super) fn child_key(dir: &str, name: &str) -> SmbResult<String> {
    let key = join_key(dir, name);
    if key.len() > MAX_KEY_LEN {
        return Err(SmbError::NameInvalid);
    }
    Ok(key)
}

/// The last name of `key`.
pub(super) fn last_name(key: &str) -> &str {
    key.rsplit_once('/').map_or(key, |(_, name)| name)
}

/// The key of the directory `key` is in. Empty for a key with one name.
pub(super) fn parent_key(key: &str) -> &str {
    key.rsplit_once('/').map_or("", |(dir, _)| dir)
}

/// The prefix of the keys below the directory with key `dir`, which is also
/// the key of its marker object. Empty for the root of a share without a
/// prefix, which has no marker.
pub(super) fn dir_prefix(dir: &str) -> String {
    if dir.is_empty() {
        String::new()
    } else {
        format!("{dir}/")
    }
}

/// The path of `key` below the share root `prefix`, with `/` between the
/// names.
pub(super) fn path_below(prefix: &str, key: &str) -> String {
    key.strip_prefix(prefix)
        .map_or(key, |rest| rest.trim_start_matches('/'))
        .to_owned()
}

/// A key or a name as names are compared. Only [`slot`] makes one, so a map
/// keyed by slots is never looked up by a spelling.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub(super) struct Slot(String);

impl Slot {
    /// Whether the slot begins with the whole of `prefix`.
    pub(super) fn starts_with(&self, prefix: &Slot) -> bool {
        self.0.starts_with(&prefix.0)
    }
}

/// `text`, a key or a name, as names are compared: lowercased when `fold` is
/// set, which is when names match without regard to case.
pub(super) fn slot(text: &str, fold: bool) -> Slot {
    Slot(if fold {
        text.to_lowercase()
    } else {
        text.to_owned()
    })
}

/// The key of the entry at `components` below the share root `root`.
pub(super) fn path_key(root: &str, components: &[String]) -> SmbResult<String> {
    components
        .iter()
        .try_fold(root.to_owned(), |dir, name| child_key(&dir, name))
}

/// What a listing says about an object.
#[derive(Debug, Clone, PartialEq, Eq)]
pub(super) struct FileStamp {
    pub(super) size: u64,
    pub(super) etag: String,
    pub(super) last_modified: SystemTime,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub(super) enum RowKind {
    File(FileStamp),
    Dir,
}

/// An entry of a directory: a name in its stored spelling.
#[derive(Debug, Clone, PartialEq, Eq)]
pub(super) struct Row {
    pub(super) name: String,
    pub(super) kind: RowKind,
}

impl Row {
    pub(super) fn is_dir(&self) -> bool {
        matches!(self.kind, RowKind::Dir)
    }

    /// The bytes of the key the row stands for, relative to its directory.
    fn key_bytes(&self) -> impl Iterator<Item = u8> + '_ {
        self.name.bytes().chain(self.is_dir().then_some(b'/'))
    }
}

/// A change the backend made to the row of a name.
#[derive(Debug, Clone)]
pub(super) enum RowEdit {
    /// The row put in place of the row of its name.
    Put(Row),
    /// The directory row taken out, or the file row if `dir` is not set. A row
    /// of the other kind stays.
    Take { dir: bool },
}

impl RowEdit {
    /// The row a name has once `edits` are made to `row`, oldest first.
    fn apply(edits: &[RowEdit], row: Option<Row>) -> Option<Row> {
        edits.iter().fold(row, |row, edit| match edit {
            RowEdit::Put(put) => Some(put.clone()),
            RowEdit::Take { dir } => row.filter(|held| held.is_dir() != *dir),
        })
    }
}

/// All the entries of one directory.
#[derive(Debug, Default, PartialEq, Eq)]
pub(super) struct DirListing {
    /// In the order of the keys they stand for.
    pub(super) rows: Vec<Row>,
}

impl DirListing {
    /// The listing with `row` added, in place of a row of the same name.
    pub(super) fn with_row(&self, row: Row) -> DirListing {
        let mut rows: Vec<Row> = self
            .rows
            .iter()
            .filter(|held| held.name != row.name)
            .cloned()
            .collect();
        rows.push(row);
        rows.sort_by(by_key);
        DirListing { rows }
    }

    /// The listing with each name in `edits` given the row its changes leave,
    /// oldest first, or none.
    pub(super) fn edited(self, mut edits: HashMap<String, Vec<RowEdit>>) -> DirListing {
        let mut rows: Vec<Row> = Vec::with_capacity(self.rows.len());
        for row in self.rows {
            match edits.remove(&row.name) {
                Some(made) => rows.extend(RowEdit::apply(&made, Some(row))),
                None => rows.push(row),
            }
        }
        rows.extend(
            edits
                .into_values()
                .filter_map(|made| RowEdit::apply(&made, None)),
        );
        rows.sort_by(by_key);
        DirListing { rows }
    }

    /// The listing without the directory row named `name`, or without the file
    /// row if `dir` is not set.
    pub(super) fn without_row(&self, name: &str, dir: bool) -> DirListing {
        DirListing {
            rows: self
                .rows
                .iter()
                .filter(|held| held.name != name || held.is_dir() != dir)
                .cloned()
                .collect(),
        }
    }

    /// The row named exactly `name`.
    pub(super) fn find_exact(&self, name: &str) -> Option<&Row> {
        self.rows.iter().find(|row| row.name == name)
    }

    /// Whether a directory row, or a file row if `dir` is not set, has a name
    /// that differs from `name` in case only and none is named `name`.
    pub(super) fn has_other_spelling_only(&self, name: &str, dir: bool) -> bool {
        let folded = name.to_lowercase();
        let mut other = false;
        for row in self.rows.iter().filter(|row| row.is_dir() == dir) {
            if row.name == name {
                return false;
            }
            other |= folds_to(&row.name, &folded);
        }
        other
    }

    /// Whether a row has a name that differs from `name` in case only.
    pub(super) fn has_other_spelling(&self, name: &str) -> bool {
        let folded = name.to_lowercase();
        self.rows
            .iter()
            .any(|row| row.name != name && folds_to(&row.name, &folded))
    }

    /// The row named `name`, or else the first row in key order whose name
    /// folds to the same lowercase text. With `dirs_only`, only directories
    /// count.
    pub(super) fn find_folded(&self, name: &str, dirs_only: bool) -> Option<&Row> {
        let wanted = |row: &&Row| !dirs_only || row.is_dir();
        if let Some(row) = self.find_exact(name).filter(wanted) {
            return Some(row);
        }
        let folded = name.to_lowercase();
        self.rows
            .iter()
            .filter(wanted)
            .find(|row| folds_to(&row.name, &folded))
    }
}

/// Whether `name` is `folded` once lowercased. `folded` is already lowercase.
fn folds_to(name: &str, folded: &str) -> bool {
    if name.is_ascii() {
        name.len() == folded.len()
            && name
                .bytes()
                .zip(folded.bytes())
                .all(|(a, b)| a.to_ascii_lowercase() == b)
    } else {
        name.to_lowercase() == folded
    }
}

/// Builds a [`DirListing`] from the pages of a delimited listing of one
/// directory.
pub(super) struct ListingBuilder {
    prefix: String,
    /// Whether any key below the directory was seen.
    any: bool,
    files: Vec<Row>,
    dirs: Vec<Row>,
}

impl ListingBuilder {
    /// A builder for the directory whose keys start with `prefix`.
    pub(super) fn new(prefix: String) -> Self {
        ListingBuilder {
            prefix,
            any: false,
            files: Vec::new(),
            dirs: Vec::new(),
        }
    }

    /// Takes in one page. Keys outside the directory, with an empty path
    /// component, or that no client could open are left out of the rows, but
    /// the directory exists for them.
    pub(super) fn add(&mut self, page: &Listing) {
        for object in &page.objects {
            let Some(rest) = object.key.strip_prefix(&self.prefix) else {
                continue;
            };
            self.any = true;
            if is_valid_component(rest) {
                self.files.push(Row {
                    name: rest.to_owned(),
                    kind: RowKind::File(FileStamp {
                        size: object.size,
                        etag: object.etag.clone(),
                        last_modified: object.last_modified,
                    }),
                });
            }
        }
        for common in &page.prefixes {
            let Some(rest) = common
                .strip_prefix(&self.prefix)
                .and_then(|rest| rest.strip_suffix('/'))
            else {
                continue;
            };
            self.any = true;
            if is_valid_component(rest) {
                self.dirs.push(Row {
                    name: rest.to_owned(),
                    kind: RowKind::Dir,
                });
            }
        }
    }

    /// The listing, or `None` when the store has no key below the directory.
    /// A name that is both a directory and an object is the directory.
    pub(super) fn finish(self) -> Option<DirListing> {
        let ListingBuilder {
            any, files, dirs, ..
        } = self;
        if !any {
            return None;
        }
        let dir_names: HashSet<&str> = dirs.iter().map(|row| row.name.as_str()).collect();
        let mut rows: Vec<Row> = files
            .into_iter()
            .filter(|row| !dir_names.contains(row.name.as_str()))
            .collect();
        rows.extend(dirs);
        rows.sort_by(by_key);
        rows.dedup_by(|a, b| a.name == b.name && a.is_dir() == b.is_dir());
        Some(DirListing { rows })
    }
}

fn by_key(a: &Row, b: &Row) -> Ordering {
    a.key_bytes().cmp(b.key_bytes())
}

#[cfg(test)]
mod tests {
    use std::time::{Duration, UNIX_EPOCH};

    use super::*;
    use crate::s3::xml::ListedObject;

    fn object(key: &str) -> ListedObject {
        ListedObject {
            key: key.to_owned(),
            size: key.len() as u64,
            etag: "\"e\"".to_owned(),
            last_modified: UNIX_EPOCH + Duration::from_secs(1_000),
        }
    }

    fn page(objects: &[&str], prefixes: &[&str]) -> Listing {
        Listing {
            objects: objects.iter().map(|key| object(key)).collect(),
            prefixes: prefixes.iter().map(|p| (*p).to_owned()).collect(),
            next_token: None,
        }
    }

    fn listing(prefix: &str, pages: &[Listing]) -> DirListing {
        built(prefix, pages).unwrap_or_default()
    }

    fn built(prefix: &str, pages: &[Listing]) -> Option<DirListing> {
        let mut builder = ListingBuilder::new(prefix.to_owned());
        for page in pages {
            builder.add(page);
        }
        builder.finish()
    }

    fn names(listing: &DirListing) -> Vec<(&str, bool)> {
        listing
            .rows
            .iter()
            .map(|row| (row.name.as_str(), row.is_dir()))
            .collect()
    }

    #[test]
    fn keys_join_the_prefix_and_the_components() {
        let components = ["a".to_owned(), "b c".to_owned()];
        assert_eq!(path_key("", &components).unwrap(), "a/b c");
        assert_eq!(path_key("p/q", &components).unwrap(), "p/q/a/b c");
        assert_eq!(path_key("p", &[]).unwrap(), "p");
        assert_eq!(path_key("", &[]).unwrap(), "");
        assert_eq!(join_key("", "x"), "x");
        assert_eq!(join_key("p/q", "x"), "p/q/x");
        assert_eq!(child_key("p/q", "x").unwrap(), "p/q/x");
        assert_eq!(dir_prefix(""), "");
        assert_eq!(dir_prefix("p"), "p/");
    }

    #[test]
    fn the_name_of_a_key_is_its_last_component() {
        assert_eq!(last_name("a/b/c.txt"), "c.txt");
        assert_eq!(last_name("c.txt"), "c.txt");
        assert_eq!(last_name(""), "");
    }

    #[test]
    fn the_directory_of_a_key_is_everything_before_its_last_component() {
        assert_eq!(parent_key("a/b/c.txt"), "a/b");
        assert_eq!(parent_key("c.txt"), "");
    }

    #[test]
    fn names_are_compared_as_lowercase_only_when_case_is_ignored() {
        assert_eq!(
            slot("Dir/\u{c9}COLE.TXT", true),
            slot("dir/\u{e9}cole.txt", false)
        );
        assert_ne!(
            slot("Dir/\u{c9}COLE.TXT", false),
            slot("dir/\u{e9}cole.txt", false)
        );
        assert_eq!(slot("A\u{3a3}/B", true), slot("a\u{3c2}/b", true));
        assert!(slot("Dir/A", true).starts_with(&slot("DIR/", true)));
        assert!(!slot("Dir/A", false).starts_with(&slot("dir/", false)));
    }

    #[test]
    fn a_key_over_the_limit_is_an_invalid_name() {
        // Four names and three separators take 803 bytes.
        let mut components = vec!["x".repeat(200); 4];
        components.push("y".repeat(220));
        assert_eq!(path_key("", &components).unwrap().len(), MAX_KEY_LEN);
        components[4].push('y');
        assert!(matches!(
            path_key("", &components),
            Err(SmbError::NameInvalid)
        ));
        components[4].pop();
        components[4].pop();
        assert_eq!(path_key("", &components).unwrap().len(), MAX_KEY_LEN - 1);
        // The prefix and its separator count.
        assert!(matches!(
            path_key("p", &components),
            Err(SmbError::NameInvalid)
        ));
        components[4].pop();
        components[4].pop();
        assert_eq!(path_key("p", &components).unwrap().len(), MAX_KEY_LEN - 1);
    }

    #[test]
    fn multibyte_names_count_bytes_in_the_key() {
        // 255 UTF-16 units of 3 bytes each.
        let name = "\u{20ac}".repeat(255);
        let components = vec![name; 2];
        assert!(matches!(
            path_key("", &components),
            Err(SmbError::NameInvalid)
        ));
        assert!(path_key("", &components[..1]).is_ok());
    }

    #[test]
    fn rows_come_from_objects_and_common_prefixes() {
        let listing = listing("d/", &[page(&["d/a.txt", "d/b"], &["d/sub/", "d/other/"])]);
        assert_eq!(
            names(&listing),
            [
                ("a.txt", false),
                ("b", false),
                ("other", true),
                ("sub", true)
            ]
        );
        let RowKind::File(stamp) = &listing.find_exact("a.txt").unwrap().kind else {
            panic!("not a file");
        };
        assert_eq!(stamp.size, 7);
        assert_eq!(stamp.etag, "\"e\"");
        assert_eq!(stamp.last_modified, UNIX_EPOCH + Duration::from_secs(1_000));
    }

    #[test]
    fn a_row_added_takes_its_place_in_key_order_and_replaces_one_of_its_name() {
        let base = listing("", &[page(&["a0", "a.txt"], &["b/"])]);
        let row = |name: &str, is_dir: bool| Row {
            name: name.to_owned(),
            kind: if is_dir {
                RowKind::Dir
            } else {
                RowKind::File(FileStamp {
                    size: 1,
                    etag: String::new(),
                    last_modified: UNIX_EPOCH,
                })
            },
        };
        let added = base.with_row(row("a", true));
        assert_eq!(
            names(&added),
            [("a.txt", false), ("a", true), ("a0", false), ("b", true)]
        );
        let replaced = added.with_row(row("b", false));
        assert_eq!(
            names(&replaced),
            [("a.txt", false), ("a", true), ("a0", false), ("b", false)]
        );
        assert_eq!(names(&base).len(), 3, "the original is not changed");
    }

    #[test]
    fn rows_follow_the_order_of_the_keys() {
        // "a.txt" sorts before "a/", and "a/" before "a0".
        let listing = listing("", &[page(&["a0", "a.txt", "a-b"], &["a/"])]);
        assert_eq!(
            names(&listing),
            [("a-b", false), ("a.txt", false), ("a", true), ("a0", false)]
        );
    }

    #[test]
    fn pages_are_merged() {
        let listing = listing(
            "",
            &[
                page(&["b", "c"], &["a/"]),
                page(&["d"], &["e/"]),
                page(&[], &[]),
            ],
        );
        assert_eq!(
            names(&listing),
            [
                ("a", true),
                ("b", false),
                ("c", false),
                ("d", false),
                ("e", true)
            ]
        );
    }

    #[test]
    fn the_marker_is_not_a_row_and_shows_that_the_directory_exists() {
        let marker = built("d/", &[page(&["d/"], &[])]).unwrap();
        assert!(marker.rows.is_empty());
        assert!(built("d/", &[page(&[], &[])]).is_none());
        assert!(built("d/", &[]).is_none());
    }

    #[test]
    fn names_no_client_can_open_are_left_out() {
        let long = "x".repeat(256);
        let listing = listing(
            "",
            &[page(
                &[
                    "ok",
                    "bad:name",
                    "bad*name",
                    "tab\tname",
                    "back\\slash",
                    ".",
                    "..",
                    &long,
                ],
                &["fine/", "wild?/", "a:b/"],
            )],
        );
        assert_eq!(names(&listing), [("fine", true), ("ok", false)]);
    }

    #[test]
    fn keys_with_an_empty_component_are_left_out() {
        let listing = listing("d/", &[page(&["d//x", "d/ok"], &["d//", "d/a//", "d/b/"])]);
        assert_eq!(names(&listing), [("b", true), ("ok", false)]);
    }

    #[test]
    fn a_directory_of_unlistable_keys_exists_with_no_rows() {
        let junk = built("d/", &[page(&["d//x", "d/bad:name"], &["d/a//"])]).unwrap();
        assert!(junk.rows.is_empty());
    }

    #[test]
    fn keys_outside_the_directory_are_left_out() {
        let listing = listing(
            "d/",
            &[page(
                &["other/x", "dd/x", "d/in"],
                &["other/", "dd/", "d/sub/"],
            )],
        );
        assert_eq!(names(&listing), [("in", false), ("sub", true)]);
        assert!(built("d/", &[page(&["other/x", "dd/x"], &["other/"])]).is_none());
    }

    #[test]
    fn keys_below_a_subdirectory_are_not_rows_of_the_directory() {
        let listing = listing("d/", &[page(&["d/sub/deep", "d/file"], &[])]);
        assert_eq!(names(&listing), [("file", false)]);
    }

    #[test]
    fn a_directory_wins_over_an_object_of_the_same_name() {
        let listing = listing(
            "",
            &[page(&["a", "b"], &["a/"]), page(&["A"], &["c/", "b/"])],
        );
        assert_eq!(
            names(&listing),
            [("A", false), ("a", true), ("b", true), ("c", true)]
        );
        assert!(matches!(
            listing.find_exact("a").unwrap().kind,
            RowKind::Dir
        ));
        assert!(matches!(
            listing.find_exact("b").unwrap().kind,
            RowKind::Dir
        ));
    }

    #[test]
    fn a_repeated_row_is_kept_once() {
        let listing = listing("", &[page(&["a"], &["d/"]), page(&["a"], &["d/"])]);
        assert_eq!(names(&listing), [("a", false), ("d", true)]);
    }

    #[test]
    fn an_exact_name_is_found_exactly() {
        let listing = listing("", &[page(&["File", "file", "FILE"], &[])]);
        assert_eq!(listing.find_exact("file").unwrap().name, "file");
        assert!(listing.find_exact("fIle").is_none());
        assert_eq!(listing.find_folded("file", false).unwrap().name, "file");
        assert_eq!(listing.find_folded("FILE", false).unwrap().name, "FILE");
    }

    #[test]
    fn a_folded_name_takes_the_first_match_in_key_order() {
        // Uppercase letters sort before lowercase ones.
        let listing = listing("", &[page(&["readme", "README", "Readme"], &[])]);
        assert_eq!(listing.find_folded("rEaDmE", false).unwrap().name, "README");
        assert!(listing.find_folded("readm", false).is_none());
        assert!(listing.find_folded("readmee", false).is_none());
    }

    #[test]
    fn a_folded_name_can_be_limited_to_directories() {
        let listing = listing("", &[page(&["Data", "DATA"], &["data/", "dAtA/"])]);
        assert_eq!(listing.find_folded("DaTa", true).unwrap().name, "dAtA");
        assert_eq!(listing.find_folded("DaTa", false).unwrap().name, "DATA");
        assert_eq!(listing.find_folded("Data", true).unwrap().name, "dAtA");
        assert_eq!(listing.find_folded("data", true).unwrap().name, "data");
        let files_first = self::listing("", &[page(&["Cfg"], &["cfg/"])]);
        assert_eq!(files_first.find_folded("CFG", true).unwrap().name, "cfg");
        assert!(
            self::listing("", &[page(&["Cfg"], &[])])
                .find_folded("cfg", true)
                .is_none()
        );
    }

    #[test]
    fn folding_uses_unicode_lowercase() {
        let listing = listing(
            "",
            &[page(&["\u{c9}t\u{e9}", "stra\u{df}e", "\u{3a3}"], &[])],
        );
        let found = |name| {
            listing
                .find_folded(name, false)
                .map(|row| row.name.as_str())
        };
        assert_eq!(found("\u{e9}T\u{c9}"), Some("\u{c9}t\u{e9}"));
        assert_eq!(found("STRA\u{df}E"), Some("stra\u{df}e"));
        assert_eq!(found("\u{3c3}"), Some("\u{3a3}"));
        assert!(folds_to("ABC", "abc"));
        assert!(!folds_to("ABC", "abd"));
        assert!(!folds_to("AB", "abc"));
        assert!(!folds_to("ab", "\u{e0}b"));
        assert!(folds_to("\u{39f}\u{3a3}", "\u{3bf}\u{3c2}"));
    }
}