summaryrefslogtreecommitdiff
path: root/src/server/tests.rs
blob: ee122ddd08c490fcc9d7a2b995cc83978bd2be64 (plain)
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
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
// Copyright 2019 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

use super::*;

use std::borrow::Cow;
use std::collections::{HashSet, VecDeque};
use std::env;
use std::ffi::{CString, OsString};
use std::fs::{self, File};
use std::io::{self, Cursor};
use std::mem;
use std::ops::Deref;
use std::os::unix::ffi::OsStringExt;
use std::os::unix::fs::MetadataExt;
use std::path::{Component, Path, PathBuf};
use std::u32;

// Used to indicate that there is no fid associated with this message.
const P9_NOFID: u32 = u32::MAX;

// The fid associated with the root directory of the server.
const ROOT_FID: u32 = 1;

// How big we want the default buffer to be when running tests.
const DEFAULT_BUFFER_SIZE: u32 = 4096;

// Joins `path` to `buf`.  If `path` is '..', removes the last component from `buf`
// only if `buf` != `root` but does nothing if `buf` == `root`.  Pushes `path` onto
// `buf` if it is a normal path component.
//
// Returns an error if `path` is absolute, has more than one component, or contains
// a '.' component.
fn join_path<P: AsRef<Path>, R: AsRef<Path>>(
    mut buf: PathBuf,
    path: P,
    root: R,
) -> io::Result<PathBuf> {
    let path = path.as_ref();
    let root = root.as_ref();
    debug_assert!(buf.starts_with(root));

    if path.components().count() > 1 {
        return Err(io::Error::from_raw_os_error(libc::EINVAL));
    }

    for component in path.components() {
        match component {
            // Prefix should only appear on windows systems.
            Component::Prefix(_) => return Err(io::Error::from_raw_os_error(libc::EINVAL)),
            // Absolute paths are not allowed.
            Component::RootDir => return Err(io::Error::from_raw_os_error(libc::EINVAL)),
            // '.' elements are not allowed.
            Component::CurDir => return Err(io::Error::from_raw_os_error(libc::EINVAL)),
            Component::ParentDir => {
                // We only remove the parent path if we are not already at the root of the
                // file system.
                if buf != root {
                    buf.pop();
                }
            }
            Component::Normal(element) => buf.push(element),
        }
    }

    Ok(buf)
}

// Automatically deletes the path it contains when it goes out of scope.
struct ScopedPath<P: AsRef<Path>>(P);

impl<P: AsRef<Path>> AsRef<Path> for ScopedPath<P> {
    fn as_ref(&self) -> &Path {
        self.0.as_ref()
    }
}

impl<P: AsRef<Path>> Deref for ScopedPath<P> {
    type Target = Path;

    fn deref(&self) -> &Self::Target {
        self.0.as_ref()
    }
}

impl<P: AsRef<Path>> Drop for ScopedPath<P> {
    fn drop(&mut self) {
        if let Err(e) = fs::remove_dir_all(&**self) {
            println!("Failed to remove {}: {}", self.display(), e);
        }
    }
}

enum DirEntry<'a> {
    File {
        name: &'a str,
        content: &'a [u8],
    },
    Directory {
        name: &'a str,
        entries: &'a [DirEntry<'a>],
    },
}

impl<'a> DirEntry<'a> {
    // Creates `self` in the path given by `dir`.
    fn create(&self, dir: &mut Cow<Path>) {
        match *self {
            DirEntry::File { name, content } => {
                let mut f = File::create(dir.join(name)).expect("failed to create file");
                f.write_all(content).expect("failed to write file content");
            }
            DirEntry::Directory { name, entries } => {
                dir.to_mut().push(name);

                fs::create_dir_all(&**dir).expect("failed to create directory");
                for e in entries {
                    e.create(dir);
                }

                assert!(dir.to_mut().pop());
            }
        }
    }
}

// Creates a file with `name` in `dir` and fills it with random
// content.
fn create_local_file<P: AsRef<Path>>(dir: P, name: &str) -> Vec<u8> {
    let mut content = Vec::new();
    File::open("/dev/urandom")
        .and_then(|f| f.take(200).read_to_end(&mut content))
        .expect("failed to read from /dev/urandom");

    let f = DirEntry::File {
        name,
        content: &content,
    };
    f.create(&mut Cow::from(dir.as_ref()));

    content
}

fn check_qid(qid: &Qid, md: &fs::Metadata) {
    let ty = if md.is_dir() {
        P9_QTDIR
    } else if md.is_file() {
        P9_QTFILE
    } else if md.file_type().is_symlink() {
        P9_QTSYMLINK
    } else {
        panic!("unknown file type: {:?}", md.file_type());
    };
    assert_eq!(qid.ty, ty);
    assert_eq!(qid.version, md.mtime() as u32);
    assert_eq!(qid.path, md.ino());
}

fn check_attr(server: &mut Server, fid: u32, md: &fs::Metadata) {
    let tgetattr = Tgetattr {
        fid,
        request_mask: P9_GETATTR_BASIC,
    };

    let rgetattr = server.get_attr(&tgetattr).expect("failed to call get_attr");

    let ty = if md.is_dir() {
        P9_QTDIR
    } else if md.is_file() {
        P9_QTFILE
    } else if md.file_type().is_symlink() {
        P9_QTSYMLINK
    } else {
        panic!("unknown file type: {:?}", md.file_type());
    };
    assert_eq!(rgetattr.valid, P9_GETATTR_BASIC);
    assert_eq!(rgetattr.qid.ty, ty);
    assert_eq!(rgetattr.qid.version, md.mtime() as u32);
    assert_eq!(rgetattr.qid.path, md.ino());
    assert_eq!(rgetattr.mode, md.mode());
    assert_eq!(rgetattr.uid, md.uid());
    assert_eq!(rgetattr.gid, md.gid());
    assert_eq!(rgetattr.nlink, md.nlink());
    assert_eq!(rgetattr.rdev, md.rdev());
    assert_eq!(rgetattr.size, md.size());
    assert_eq!(rgetattr.atime_sec, md.atime() as u64);
    assert_eq!(rgetattr.atime_nsec, md.atime_nsec() as u64);
    assert_eq!(rgetattr.mtime_sec, md.mtime() as u64);
    assert_eq!(rgetattr.mtime_nsec, md.mtime_nsec() as u64);
    assert_eq!(rgetattr.ctime_sec, md.ctime() as u64);
    assert_eq!(rgetattr.ctime_nsec, md.ctime_nsec() as u64);
    assert_eq!(rgetattr.btime_sec, 0);
    assert_eq!(rgetattr.btime_nsec, 0);
    assert_eq!(rgetattr.gen, 0);
    assert_eq!(rgetattr.data_version, 0);
}

fn check_content(server: &mut Server, content: &[u8], fid: u32) {
    for offset in 0..content.len() {
        let tread = Tread {
            fid,
            offset: offset as u64,
            count: DEFAULT_BUFFER_SIZE,
        };

        let rread = server.read(&tread).expect("failed to read file");
        assert_eq!(content[offset..], rread.data[..]);
    }
}

fn walk<P: Into<PathBuf>>(
    server: &mut Server,
    start: P,
    fid: u32,
    newfid: u32,
    names: Vec<String>,
) {
    let mut mds = Vec::with_capacity(names.len());
    let mut buf = start.into();
    for name in &names {
        buf.push(name);
        mds.push(
            buf.symlink_metadata()
                .expect("failed to get metadata for path"),
        );
    }

    let twalk = Twalk {
        fid,
        newfid,
        wnames: names,
    };

    let rwalk = server.walk(twalk).expect("failed to walk directoy");
    assert_eq!(mds.len(), rwalk.wqids.len());
    for (md, qid) in mds.iter().zip(rwalk.wqids.iter()) {
        check_qid(qid, md);
    }
}

fn open<P: Into<PathBuf>>(
    server: &mut Server,
    dir: P,
    dir_fid: u32,
    name: &str,
    fid: u32,
    flags: u32,
) -> io::Result<Rlopen> {
    let wnames = if name.is_empty() {
        vec![]
    } else {
        vec![String::from(name)]
    };
    walk(server, dir, dir_fid, fid, wnames);

    let tlopen = Tlopen { fid, flags };

    server.lopen(&tlopen)
}

fn write<P: AsRef<Path>>(server: &mut Server, dir: P, name: &str, fid: u32, flags: u32) {
    let file_path = dir.as_ref().join(name);
    let file_len = if file_path.exists() {
        fs::symlink_metadata(&file_path)
            .expect("unable to get metadata for file")
            .len() as usize
    } else {
        0usize
    };
    let mut new_content = Vec::new();
    File::open("/dev/urandom")
        .and_then(|f| f.take(200).read_to_end(&mut new_content))
        .expect("failed to read from /dev/urandom");

    let twrite = Twrite {
        fid,
        offset: 0,
        data: Data(new_content),
    };

    let rwrite = server.write(&twrite).expect("failed to write file");
    assert_eq!(rwrite.count, twrite.data.len() as u32);

    let tfsync = Tfsync { fid, datasync: 0 };
    server.fsync(&tfsync).expect("failed to sync file contents");

    let actual_content = fs::read(file_path).expect("failed to read back content from file");

    // If the file was opened append-only, then the content should have been
    // written to the end even though the offset was 0.
    let idx = if flags & P9_APPEND == 0 { 0 } else { file_len };
    assert_eq!(actual_content[idx..], twrite.data[..]);
}

fn create<P: Into<PathBuf>>(
    server: &mut Server,
    dir: P,
    dir_fid: u32,
    fid: u32,
    name: &str,
    flags: u32,
    mode: u32,
) -> io::Result<Rlcreate> {
    // The `fid` in the lcreate call initially points to the directory
    // but is supposed to point to the newly created file after the call
    // completes.  Duplicate the fid so that we don't end up consuming the
    // directory fid.
    walk(server, dir, dir_fid, fid, Vec::new());

    let tlcreate = Tlcreate {
        fid,
        name: String::from(name),
        flags,
        mode,
        gid: 0,
    };

    server.lcreate(tlcreate)
}

struct Readdir<'a> {
    server: &'a mut Server,
    fid: u32,
    offset: u64,
    cursor: Cursor<Vec<u8>>,
}

impl<'a> Iterator for Readdir<'a> {
    type Item = Dirent;

    fn next(&mut self) -> Option<Self::Item> {
        if self.cursor.position() >= self.cursor.get_ref().len() as u64 {
            let treaddir = Treaddir {
                fid: self.fid,
                offset: self.offset,
                count: DEFAULT_BUFFER_SIZE,
            };

            let Rreaddir { data } = self
                .server
                .readdir(&treaddir)
                .expect("failed to read directory");
            if data.is_empty() {
                // No more entries.
                return None;
            }

            mem::drop(mem::replace(&mut self.cursor, Cursor::new(data.0)));
        }

        let dirent: Dirent = WireFormat::decode(&mut self.cursor).expect("failed to decode dirent");
        self.offset = dirent.offset;

        Some(dirent)
    }
}

fn readdir(server: &mut Server, fid: u32) -> Readdir {
    Readdir {
        server,
        fid,
        offset: 0,
        cursor: Cursor::new(Vec::new()),
    }
}

// Sets up the server to start handling messages.  Creates a new temporary
// directory to act as the server root and sends an initial Tattach message.
// At the end of setup, fid 1 points to the root of the server.
fn setup<P: AsRef<Path>>(name: P) -> (ScopedPath<OsString>, Server) {
    let mut test_dir = env::var_os("T")
        .map(PathBuf::from)
        .unwrap_or_else(env::temp_dir);
    test_dir.push(name);

    let mut os_str = OsString::from(test_dir);
    os_str.push(".XXXXXX");

    // Create a c string and release ownership.  This seems like the only way
    // to get a *mut c_char.
    let buf = CString::new(os_str.into_vec())
        .expect("failed to create CString")
        .into_raw();

    // Safe because this will only modify the contents of `buf`.
    let ret = unsafe { libc::mkdtemp(buf) };

    // Take ownership of the buffer back before checking the result.  Safe because
    // this was created by a call to into_raw() above and mkdtemp will not overwrite
    // the trailing '\0'.
    let buf = unsafe { CString::from_raw(buf) };

    assert!(!ret.is_null());

    let test_dir = ScopedPath(OsString::from_vec(buf.into_bytes()));

    // Create a basic file system hierarchy.
    let entries = [
        DirEntry::Directory {
            name: "subdir",
            entries: &[
                DirEntry::File {
                    name: "b",
                    content: b"hello, world!",
                },
                DirEntry::Directory {
                    name: "nested",
                    entries: &[DirEntry::File {
                        name: "Огонь по готовности!",
                        content: &[
                            0xe9u8, 0xbeu8, 0x8du8, 0xe3u8, 0x81u8, 0x8cu8, 0xe6u8, 0x88u8, 0x91u8,
                            0xe3u8, 0x81u8, 0x8cu8, 0xe6u8, 0x95u8, 0xb5u8, 0xe3u8, 0x82u8, 0x92u8,
                            0xe5u8, 0x96u8, 0xb0u8, 0xe3u8, 0x82u8, 0x89u8, 0xe3u8, 0x81u8, 0x86u8,
                            0x21u8,
                        ],
                    }],
                },
            ],
        },
        DirEntry::File {
            name: "世界.txt",
            content: &[
                0xe3u8, 0x81u8, 0x93u8, 0xe3u8, 0x82u8, 0x93u8, 0xe3u8, 0x81u8, 0xabu8, 0xe3u8,
                0x81u8, 0xa1u8, 0xe3u8, 0x81u8, 0xafu8,
            ],
        },
    ];

    for e in &entries {
        e.create(&mut Cow::from(&*test_dir));
    }

    let md = test_dir
        .symlink_metadata()
        .expect("failed to get metadata for root dir");

    let mut server = Server::new(&*test_dir, Default::default(), Default::default())
        .expect("Failed to create server");

    let tversion = Tversion {
        msize: DEFAULT_BUFFER_SIZE,
        version: String::from("9P2000.L"),
    };

    let rversion = server
        .version(&tversion)
        .expect("failed to get version from server");
    assert_eq!(rversion.msize, DEFAULT_BUFFER_SIZE);
    assert_eq!(rversion.version, "9P2000.L");

    let tattach = Tattach {
        fid: ROOT_FID,
        afid: P9_NOFID,
        uname: String::from("unittest"),
        aname: String::from(""),
        n_uname: 1000,
    };

    let rattach = server.attach(&tattach).expect("failed to attach to server");
    check_qid(&rattach.qid, &md);

    (test_dir, server)
}

#[test]
fn path_joins() {
    let root = PathBuf::from("/a/b/c");
    let path = PathBuf::from("/a/b/c/d/e/f");

    assert_eq!(
        &join_path(path.clone(), "nested", &root).expect("normal"),
        Path::new("/a/b/c/d/e/f/nested")
    );

    let p1 = join_path(path, "..", &root).expect("parent 1");
    assert_eq!(&p1, Path::new("/a/b/c/d/e/"));

    let p2 = join_path(p1, "..", &root).expect("parent 2");
    assert_eq!(&p2, Path::new("/a/b/c/d/"));

    let p3 = join_path(p2, "..", &root).expect("parent 3");
    assert_eq!(&p3, Path::new("/a/b/c/"));

    let p4 = join_path(p3, "..", &root).expect("parent of root");
    assert_eq!(&p4, Path::new("/a/b/c/"));
}

#[test]
fn invalid_joins() {
    let root = PathBuf::from("/a");
    let path = PathBuf::from("/a/b");

    join_path(path.clone(), ".", &root).expect_err("current directory");
    join_path(path.clone(), "c/d/e", &root).expect_err("too many components");
    join_path(path, "/c/d/e", &root).expect_err("absolute path");
}

#[test]
fn clunk() {
    let (_test_dir, mut server) = setup("clunk");

    let tclunk = Tclunk { fid: ROOT_FID };
    server.clunk(&tclunk).expect("failed to clunk root fid");
}

#[test]
fn get_attr() {
    let (test_dir, mut server) = setup("get_attr");

    let md = test_dir
        .symlink_metadata()
        .expect("failed to get metadata for test dir");

    check_attr(&mut server, ROOT_FID, &md);
}

#[test]
fn tree_walk() {
    let (test_dir, mut server) = setup("readdir");

    let mut next_fid = ROOT_FID + 1;

    let mut dirs = VecDeque::new();
    dirs.push_back(test_dir.to_path_buf());

    while let Some(dir) = dirs.pop_front() {
        let dfid = next_fid;
        next_fid += 1;

        let wnames: Vec<String> = dir
            .strip_prefix(&test_dir)
            .expect("test directory is not prefix of subdir")
            .components()
            .map(|c| Path::new(&c).to_string_lossy().to_string())
            .collect();
        walk(&mut server, &*test_dir, ROOT_FID, dfid, wnames);

        let md = dir.symlink_metadata().expect("failed to get metadata");

        check_attr(&mut server, dfid, &md);

        let fid = next_fid;
        next_fid += 1;
        open(&mut server, &dir, dfid, "", fid, P9_DIRECTORY).expect("Failed to open directory");
        for dirent in readdir(&mut server, fid) {
            if dirent.name == "." || dirent.name == ".." {
                continue;
            }

            let entry_path = dir.join(&dirent.name);
            assert!(
                entry_path.exists(),
                "directory entry \"{}\" does not exist",
                entry_path.display()
            );
            let md = fs::symlink_metadata(&entry_path).expect("failed to get metadata for entry");

            let ty = if md.is_dir() {
                dirs.push_back(dir.join(dirent.name));
                libc::DT_DIR
            } else if md.is_file() {
                libc::DT_REG
            } else if md.file_type().is_symlink() {
                libc::DT_LNK
            } else {
                panic!("unknown file type: {:?}", md.file_type());
            };

            assert_eq!(dirent.ty, ty);
            check_qid(&dirent.qid, &md);
        }

        let tclunk = Tclunk { fid };
        server.clunk(&tclunk).expect("failed to clunk fid");
    }
}

#[test]
fn create_existing_file() {
    let (test_dir, mut server) = setup("create_existing");

    let name = "existing";
    create_local_file(&test_dir, name);

    let fid = ROOT_FID + 1;
    create(
        &mut server,
        &*test_dir,
        ROOT_FID,
        fid,
        name,
        P9_APPEND,
        0o644,
    )
    .expect_err("successfully created existing file");
}

enum SetAttrKind {
    File,
    Directory,
}

fn set_attr_test<F>(kind: SetAttrKind, set_fields: F) -> io::Result<fs::Metadata>
where
    F: FnOnce(&mut Tsetattr),
{
    let (test_dir, mut server) = setup("set_attr");

    let name = "existing";
    match kind {
        SetAttrKind::File => {
            create_local_file(&test_dir, name);
        }
        SetAttrKind::Directory => {
            let tmkdir = Tmkdir {
                dfid: ROOT_FID,
                name: String::from(name),
                mode: 0o755,
                gid: 0,
            };

            let rmkdir = server.mkdir(tmkdir).expect("failed to create directory");
            let md = fs::symlink_metadata(test_dir.join(name))
                .expect("failed to get metadata for directory");

            assert!(md.is_dir());
            check_qid(&rmkdir.qid, &md);
        }
    };

    let fid = ROOT_FID + 1;
    walk(
        &mut server,
        &*test_dir,
        ROOT_FID,
        fid,
        vec![String::from(name)],
    );

    let mut tsetattr = Tsetattr {
        fid,
        valid: 0,
        mode: 0,
        uid: 0,
        gid: 0,
        size: 0,
        atime_sec: 0,
        atime_nsec: 0,
        mtime_sec: 0,
        mtime_nsec: 0,
    };

    set_fields(&mut tsetattr);
    server.set_attr(&tsetattr)?;

    fs::symlink_metadata(test_dir.join(name))
}

#[test]
fn set_len() {
    let len = 661;
    let md = set_attr_test(SetAttrKind::File, |tsetattr| {
        tsetattr.valid = P9_SETATTR_SIZE;
        tsetattr.size = len;
    })
    .expect("failed to run set length of file");

    assert_eq!(md.size(), len);
}

#[test]
fn set_file_mode() {
    let mode = 0o640;
    let err = set_attr_test(SetAttrKind::File, |tsetattr| {
        tsetattr.valid = P9_SETATTR_MODE;
        tsetattr.mode = mode;
    })
    .expect_err("successfully set mode");

    assert_eq!(err.kind(), io::ErrorKind::PermissionDenied);
}

#[test]
fn set_file_uid() {
    let uid = 294;
    let err = set_attr_test(SetAttrKind::File, |tsetattr| {
        tsetattr.valid = P9_SETATTR_UID;
        tsetattr.uid = uid;
    })
    .expect_err("successfully set uid");

    assert_eq!(err.kind(), io::ErrorKind::PermissionDenied);
}

#[test]
fn set_file_gid() {
    let gid = 9024;
    let err = set_attr_test(SetAttrKind::File, |tsetattr| {
        tsetattr.valid = P9_SETATTR_GID;
        tsetattr.gid = gid;
    })
    .expect_err("successfully set gid");

    assert_eq!(err.kind(), io::ErrorKind::PermissionDenied);
}

#[test]
fn set_file_mtime() {
    let (secs, nanos) = (1245247825, 524617);
    let md = set_attr_test(SetAttrKind::File, |tsetattr| {
        tsetattr.valid = P9_SETATTR_MTIME | P9_SETATTR_MTIME_SET;
        tsetattr.mtime_sec = secs;
        tsetattr.mtime_nsec = nanos;
    })
    .expect("failed to set mtime");

    assert_eq!(md.mtime() as u64, secs);
    assert_eq!(md.mtime_nsec() as u64, nanos);
}

#[test]
fn set_file_atime() {
    let (secs, nanos) = (9247605, 4016);
    let md = set_attr_test(SetAttrKind::File, |tsetattr| {
        tsetattr.valid = P9_SETATTR_ATIME | P9_SETATTR_ATIME_SET;
        tsetattr.atime_sec = secs;
        tsetattr.atime_nsec = nanos;
    })
    .expect("failed to set atime");

    assert_eq!(md.atime() as u64, secs);
    assert_eq!(md.atime_nsec() as u64, nanos);
}

#[test]
fn set_dir_mode() {
    let mode = 0o640;
    let err = set_attr_test(SetAttrKind::Directory, |tsetattr| {
        tsetattr.valid = P9_SETATTR_MODE;
        tsetattr.mode = mode;
    })
    .expect_err("successfully set mode");

    assert_eq!(err.kind(), io::ErrorKind::PermissionDenied);
}

#[test]
fn set_dir_uid() {
    let uid = 294;
    let err = set_attr_test(SetAttrKind::Directory, |tsetattr| {
        tsetattr.valid = P9_SETATTR_UID;
        tsetattr.uid = uid;
    })
    .expect_err("successfully set uid");

    assert_eq!(err.kind(), io::ErrorKind::PermissionDenied);
}

#[test]
fn set_dir_gid() {
    let gid = 9024;
    let err = set_attr_test(SetAttrKind::Directory, |tsetattr| {
        tsetattr.valid = P9_SETATTR_GID;
        tsetattr.gid = gid;
    })
    .expect_err("successfully set gid");

    assert_eq!(err.kind(), io::ErrorKind::PermissionDenied);
}

#[test]
fn set_dir_mtime() {
    let (secs, nanos) = (1245247825, 524617);
    let md = set_attr_test(SetAttrKind::Directory, |tsetattr| {
        tsetattr.valid = P9_SETATTR_MTIME | P9_SETATTR_MTIME_SET;
        tsetattr.mtime_sec = secs;
        tsetattr.mtime_nsec = nanos;
    })
    .expect("failed to set mtime");

    assert_eq!(md.mtime() as u64, secs);
    assert_eq!(md.mtime_nsec() as u64, nanos);
}

#[test]
fn set_dir_atime() {
    let (secs, nanos) = (9247605, 4016);
    let md = set_attr_test(SetAttrKind::Directory, |tsetattr| {
        tsetattr.valid = P9_SETATTR_ATIME | P9_SETATTR_ATIME_SET;
        tsetattr.atime_sec = secs;
        tsetattr.atime_nsec = nanos;
    })
    .expect("failed to set atime");

    assert_eq!(md.atime() as u64, secs);
    assert_eq!(md.atime_nsec() as u64, nanos);
}

#[test]
fn huge_directory() {
    let (test_dir, mut server) = setup("huge_directory");

    let name = "newdir";
    let newdir = test_dir.join(name);
    fs::create_dir(&newdir).expect("failed to create directory");

    let dfid = ROOT_FID + 1;
    walk(
        &mut server,
        &*test_dir,
        ROOT_FID,
        dfid,
        vec![String::from(name)],
    );

    // Create ~4K files in the directory and then attempt to read them all.
    let mut filenames = HashSet::with_capacity(4096);
    for i in 0..4096 {
        let name = format!("file_{}", i);
        create_local_file(&newdir, &name);
        assert!(filenames.insert(name));
    }

    let fid = dfid + 1;
    open(&mut server, &newdir, dfid, "", fid, P9_DIRECTORY).expect("Failed to open directory");
    for f in readdir(&mut server, fid) {
        let path = newdir.join(&f.name);

        let md = fs::symlink_metadata(path).expect("failed to get metadata for path");
        check_qid(&f.qid, &md);

        if f.name == "." || f.name == ".." {
            assert_eq!(f.ty, libc::DT_DIR);
        } else {
            assert_eq!(f.ty, libc::DT_REG);
            assert!(filenames.remove(&f.name));
        }
    }

    assert!(filenames.is_empty());
}

#[test]
fn mkdir() {
    let (test_dir, mut server) = setup("mkdir");

    let name = "conan";
    let tmkdir = Tmkdir {
        dfid: ROOT_FID,
        name: String::from(name),
        mode: 0o755,
        gid: 0,
    };

    let rmkdir = server.mkdir(tmkdir).expect("failed to create directory");
    let md =
        fs::symlink_metadata(test_dir.join(name)).expect("failed to get metadata for directory");

    assert!(md.is_dir());
    check_qid(&rmkdir.qid, &md);
}

#[test]
fn unlink_all() {
    let (test_dir, mut server) = setup("readdir");

    let mut next_fid = ROOT_FID + 1;

    let mut dirs = VecDeque::new();
    dirs.push_back((ROOT_FID, test_dir.to_path_buf()));

    // First iterate over the whole directory.
    let mut unlinks = VecDeque::new();
    while let Some((dfid, dir)) = dirs.pop_front() {
        let mut names = VecDeque::new();
        for entry in fs::read_dir(dir).expect("failed to read directory") {
            let entry = entry.expect("unable to iterate over directory");
            let ft = entry
                .file_type()
                .expect("failed to get file type for entry");
            if ft.is_dir() {
                let fid = next_fid;
                next_fid += 1;

                let wnames: Vec<String> = entry
                    .path()
                    .strip_prefix(&test_dir)
                    .expect("test directory is not prefix of subdir")
                    .components()
                    .map(|c| Path::new(&c).to_string_lossy().to_string())
                    .collect();
                walk(&mut server, &*test_dir, ROOT_FID, fid, wnames);
                dirs.push_back((fid, entry.path()));
            }

            names.push_back((
                entry
                    .file_name()
                    .into_string()
                    .expect("failed to convert entry name to string"),
                if ft.is_dir() {
                    libc::AT_REMOVEDIR as u32
                } else {
                    0
                },
            ));
        }

        unlinks.push_back((dfid, names));
    }

    // Now remove everything in reverse order.
    while let Some((dfid, names)) = unlinks.pop_back() {
        for (name, flags) in names {
            let tunlinkat = Tunlinkat {
                dirfd: dfid,
                name,
                flags,
            };

            server.unlink_at(tunlinkat).expect("failed to unlink path");
        }
    }
}

#[test]
fn rename_at() {
    let (test_dir, mut server) = setup("rename");

    let name = "oldfile";
    let content = create_local_file(&test_dir, name);

    let newname = "newfile";
    let trename = Trenameat {
        olddirfid: ROOT_FID,
        oldname: String::from(name),
        newdirfid: ROOT_FID,
        newname: String::from(newname),
    };

    server.rename_at(trename).expect("failed to rename file");

    assert!(!test_dir.join(name).exists());

    let mut newcontent = Vec::with_capacity(content.len());
    let size = File::open(test_dir.join(newname))
        .expect("failed to open file")
        .read_to_end(&mut newcontent)
        .expect("failed to read new file content");
    assert_eq!(size, content.len());
    assert_eq!(newcontent, content);
}

macro_rules! open_test {
    ($name:ident, $flags:expr) => {
        #[test]
        fn $name() {
            let (test_dir, mut server) = setup("open");

            let fid = ROOT_FID + 1;
            let name = "test.txt";
            let content = create_local_file(&test_dir, name);

            let rlopen = open(&mut server, &*test_dir, ROOT_FID, name, fid, $flags as u32)
                .expect("failed to open file");

            let md =
                fs::symlink_metadata(test_dir.join(name)).expect("failed to get metadata for file");
            check_qid(&rlopen.qid, &md);
            assert_eq!(rlopen.iounit, md.blksize() as u32);

            check_attr(&mut server, fid, &md);

            // Check that the file has the proper contents as long as we didn't
            // truncate it first.
            if $flags & P9_TRUNC == 0 && $flags & P9_WRONLY == 0 {
                check_content(&mut server, &content, fid);
            }

            // Check that we can write to the file.
            if $flags & P9_RDWR != 0 || $flags & P9_WRONLY != 0 {
                write(&mut server, &test_dir, name, fid, $flags);
            }

            let tclunk = Tclunk { fid };
            server.clunk(&tclunk).expect("Unable to clunk file");
        }
    };
    ($name:ident, $flags:expr, $expected_err:expr) => {
        #[test]
        fn $name() {
            let (test_dir, mut server) = setup("open_fail");

            let fid = ROOT_FID + 1;
            let name = "test.txt";
            create_local_file(&test_dir, name);

            let err = open(&mut server, &*test_dir, ROOT_FID, name, fid, $flags as u32)
                .expect_err("successfully opened file");
            assert_eq!(err.kind(), $expected_err);

            let tclunk = Tclunk { fid };
            server.clunk(&tclunk).expect("Unable to clunk file");
        }
    };
}

open_test!(read_only_file_open, P9_RDONLY);
open_test!(read_write_file_open, P9_RDWR);
open_test!(write_only_file_open, P9_WRONLY);

open_test!(create_read_only_file_open, P9_CREATE | P9_RDONLY);
open_test!(create_read_write_file_open, P9_CREATE | P9_RDWR);
open_test!(create_write_only_file_open, P9_CREATE | P9_WRONLY);

open_test!(append_read_only_file_open, P9_APPEND | P9_RDONLY);
open_test!(append_read_write_file_open, P9_APPEND | P9_RDWR);
open_test!(append_write_only_file_open, P9_APPEND | P9_WRONLY);

open_test!(trunc_read_only_file_open, P9_TRUNC | P9_RDONLY);
open_test!(trunc_read_write_file_open, P9_TRUNC | P9_RDWR);
open_test!(trunc_write_only_file_open, P9_TRUNC | P9_WRONLY);

open_test!(
    create_append_read_only_file_open,
    P9_CREATE | P9_APPEND | P9_RDONLY
);
open_test!(
    create_append_read_write_file_open,
    P9_CREATE | P9_APPEND | P9_RDWR
);
open_test!(
    create_append_wronly_file_open,
    P9_CREATE | P9_APPEND | P9_WRONLY
);

open_test!(
    create_trunc_read_only_file_open,
    P9_CREATE | P9_TRUNC | P9_RDONLY
);
open_test!(
    create_trunc_read_write_file_open,
    P9_CREATE | P9_TRUNC | P9_RDWR
);
open_test!(
    create_trunc_wronly_file_open,
    P9_CREATE | P9_TRUNC | P9_WRONLY
);

open_test!(
    append_trunc_read_only_file_open,
    P9_APPEND | P9_TRUNC | P9_RDONLY
);
open_test!(
    append_trunc_read_write_file_open,
    P9_APPEND | P9_TRUNC | P9_RDWR
);
open_test!(
    append_trunc_wronly_file_open,
    P9_APPEND | P9_TRUNC | P9_WRONLY
);

open_test!(
    create_append_trunc_read_only_file_open,
    P9_CREATE | P9_APPEND | P9_TRUNC | P9_RDONLY
);
open_test!(
    create_append_trunc_read_write_file_open,
    P9_CREATE | P9_APPEND | P9_TRUNC | P9_RDWR
);
open_test!(
    create_append_trunc_wronly_file_open,
    P9_CREATE | P9_APPEND | P9_TRUNC | P9_WRONLY
);

open_test!(
    create_excl_read_only_file_open,
    P9_CREATE | P9_EXCL | P9_RDONLY,
    io::ErrorKind::AlreadyExists
);
open_test!(
    create_excl_read_write_file_open,
    P9_CREATE | P9_EXCL | P9_RDWR,
    io::ErrorKind::AlreadyExists
);
open_test!(
    create_excl_wronly_file_open,
    P9_CREATE | P9_EXCL | P9_WRONLY,
    io::ErrorKind::AlreadyExists
);

macro_rules! create_test {
    ($name:ident, $flags:expr, $mode:expr) => {
        #[test]
        fn $name() {
            let (test_dir, mut server) = setup("create");

            let name = "foo.txt";
            let fid = ROOT_FID + 1;
            let rlcreate = create(&mut server, &*test_dir, ROOT_FID, fid, name, $flags, $mode)
                .expect("failed to create file");

            let md =
                fs::symlink_metadata(test_dir.join(name)).expect("failed to get metadata for file");
            assert_eq!(rlcreate.iounit, md.blksize() as u32);
            check_qid(&rlcreate.qid, &md);
            check_attr(&mut server, fid, &md);

            // Check that we can write to the file.
            if $flags & P9_RDWR != 0 || $flags & P9_WRONLY != 0 {
                write(&mut server, &test_dir, name, fid, $flags);
            }

            let tclunk = Tclunk { fid };
            server.clunk(&tclunk).expect("Unable to clunk file");
        }
    };
    ($name:ident, $flags:expr, $mode:expr, $expected_err:expr) => {
        #[test]
        fn $name() {
            let (test_dir, mut server) = setup("create_fail");

            let name = "foo.txt";
            // The `fid` in the lcreate call initially points to the directory
            // but is supposed to point to the newly created file after the call
            // completes.  Duplicate the fid so that we don't end up consuming the
            // root fid.
            let fid = ROOT_FID + 1;
            let err = create(&mut server, &*test_dir, ROOT_FID, fid, name, $flags, $mode)
                .expect_err("successfully created file");
            assert_eq!(err.kind(), $expected_err);
        }
    };
}

create_test!(read_only_file_create, P9_RDONLY, 0o600u32);
create_test!(read_write_file_create, P9_RDWR, 0o600u32);
create_test!(write_only_file_create, P9_WRONLY, 0o600u32);

create_test!(
    append_read_only_file_create,
    P9_APPEND | P9_RDONLY,
    0o600u32
);
create_test!(append_read_write_file_create, P9_APPEND | P9_RDWR, 0o600u32);
create_test!(append_wronly_file_create, P9_APPEND | P9_WRONLY, 0o600u32);