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
// Copyright 2022 The ChromiumOS Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

use std::path::PathBuf;
use std::str::FromStr;

use anyhow::anyhow;
use anyhow::bail;
use anyhow::Context;
use devices::IommuDevType;
use devices::PciAddress;
use devices::SerialParameters;
use libc::getegid;
use libc::geteuid;
use serde::Deserialize;
use serde::Serialize;
use serde_keyvalue::from_key_values;
use serde_keyvalue::FromKeyValues;

use crate::crosvm::config::Config;

#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, FromKeyValues)]
#[serde(deny_unknown_fields, rename_all = "kebab-case")]
pub enum HypervisorKind {
    Kvm {
        device: Option<PathBuf>,
    },
    #[cfg(any(target_arch = "arm", target_arch = "aarch64"))]
    #[cfg(feature = "geniezone")]
    Geniezone {
        device: Option<PathBuf>,
    },
    #[cfg(all(any(target_arch = "arm", target_arch = "aarch64"), feature = "gunyah"))]
    Gunyah {
        device: Option<PathBuf>,
    },
}

// Doesn't do anything on unix.
pub fn check_serial_params(_serial_params: &SerialParameters) -> Result<(), String> {
    Ok(())
}

pub fn validate_config(_cfg: &mut Config) -> std::result::Result<(), String> {
    Ok(())
}

/// VFIO device structure for creating a new instance based on command line options.
#[derive(Serialize, Deserialize, FromKeyValues)]
#[serde(deny_unknown_fields, rename_all = "kebab-case")]
pub struct VfioOption {
    /// Path to the VFIO device.
    pub path: PathBuf,

    /// IOMMU type to use for this VFIO device.
    #[serde(default)]
    pub iommu: IommuDevType,

    /// PCI address to use for the VFIO device in the guest.
    /// If not specified, defaults to mirroring the host PCI address.
    pub guest_address: Option<PciAddress>,

    /// The symbol that labels the overlay device tree node which corresponds to this
    /// VFIO device.
    pub dt_symbol: Option<String>,
}

#[derive(Default, Eq, PartialEq, Serialize, Deserialize)]
pub enum SharedDirKind {
    FS,
    #[default]
    P9,
}

impl FromStr for SharedDirKind {
    type Err = anyhow::Error;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        use SharedDirKind::*;
        match s {
            "fs" | "FS" => Ok(FS),
            "9p" | "9P" | "p9" | "P9" => Ok(P9),
            _ => {
                bail!("invalid file system type");
            }
        }
    }
}

pub struct SharedDir {
    pub src: PathBuf,
    pub tag: String,
    pub kind: SharedDirKind,
    pub ugid: (Option<u32>, Option<u32>),
    pub uid_map: String,
    pub gid_map: String,
    pub fs_cfg: devices::virtio::fs::Config,
    pub p9_cfg: p9::Config,
}

impl Default for SharedDir {
    fn default() -> SharedDir {
        SharedDir {
            src: Default::default(),
            tag: Default::default(),
            kind: Default::default(),
            ugid: (None, None),
            // SAFETY: trivially safe
            uid_map: format!("0 {} 1", unsafe { geteuid() }),
            // SAFETY: trivially safe
            gid_map: format!("0 {} 1", unsafe { getegid() }),
            fs_cfg: Default::default(),
            p9_cfg: Default::default(),
        }
    }
}

struct UgidConfig {
    uid: Option<u32>,
    gid: Option<u32>,
    uid_map: String,
    gid_map: String,
}

impl Default for UgidConfig {
    fn default() -> Self {
        Self {
            uid: None,
            gid: None,
            // SAFETY: geteuid never fails.
            uid_map: format!("0 {} 1", unsafe { geteuid() }),
            // SAFETY: getegid never fails.
            gid_map: format!("0 {} 1", unsafe { getegid() }),
        }
    }
}

impl UgidConfig {
    /// Parse a key-value pair of ugid config to update `UgidConfig`.
    /// Returns whether `self` was updated or not.
    fn parse_ugid_config(&mut self, kind: &str, value: &str) -> anyhow::Result<bool> {
        match kind {
            "uid" => {
                self.uid = Some(value.parse().context("`uid` must be an integer")?);
            }
            "gid" => {
                self.gid = Some(value.parse().context("`gid` must be an integer")?);
            }
            "uidmap" => self.uid_map = value.into(),
            "gidmap" => self.gid_map = value.into(),
            _ => {
                return Ok(false);
            }
        }
        Ok(true)
    }
}

impl FromStr for SharedDir {
    type Err = anyhow::Error;

    fn from_str(param: &str) -> Result<Self, Self::Err> {
        // This is formatted as multiple fields, each separated by ":". The first 2 fields are
        // fixed (src:tag).  The rest may appear in any order:
        //
        // * type=TYPE - must be one of "p9" or "fs" (default: p9)
        // * uidmap=UIDMAP - a uid map in the format "inner outer count[,inner outer count]"
        //   (default: "0 <current euid> 1")
        // * gidmap=GIDMAP - a gid map in the same format as uidmap (default: "0 <current egid> 1")
        // * privileged_quota_uids=UIDS - Space-separated list of privileged uid values. When
        //   performing quota-related operations, these UIDs are treated as if they have CAP_FOWNER.
        // * timeout=TIMEOUT - a timeout value in seconds, which indicates how long attributes and
        //   directory contents should be considered valid (default: 5)
        // * cache=CACHE - one of "never", "always", or "auto" (default: auto)
        // * writeback=BOOL - indicates whether writeback caching should be enabled (default: false)
        // * uid=UID - uid of the device process in the user namespace created by minijail.
        //   (default: 0)
        // * gid=GID - gid of the device process in the user namespace created by minijail.
        //   (default: 0)
        // * max_dynamic_perm=uint - number of maximum number of dynamic permissions paths (default:
        //   0) This feature is arc_quota specific feature.
        // * max_dynamic_xattr=uint - number of maximum number of dynamic xattr paths (default: 0).
        //   This feature is arc_quota specific feature.
        // * security_ctx=BOOL - indicates whether use FUSE_SECURITY_CONTEXT feature or not.
        //
        // These two options (uid/gid) are useful when the crosvm process has no
        // CAP_SETGID/CAP_SETUID but an identity mapping of the current user/group
        // between the VM and the host is required.
        // Say the current user and the crosvm process has uid 5000, a user can use
        // "uid=5000" and "uidmap=5000 5000 1" such that files owned by user 5000
        // still appear to be owned by user 5000 in the VM. These 2 options are
        // useful only when there is 1 user in the VM accessing shared files.
        // If multiple users want to access the shared file, gid/uid options are
        // useless. It'd be better to create a new user namespace and give
        // CAP_SETUID/CAP_SETGID to the crosvm.
        let mut components = param.split(':');
        let src = PathBuf::from(
            components
                .next()
                .context("missing source path for `shared-dir`")?,
        );
        let tag = components
            .next()
            .context("missing tag for `shared-dir`")?
            .to_owned();

        if !src.is_dir() {
            bail!("source path for `shared-dir` must be a directory");
        }

        let mut shared_dir = SharedDir {
            src,
            tag,
            ..Default::default()
        };
        let mut type_opts = vec![];
        let mut ugid_cfg = UgidConfig::default();
        for opt in components {
            let mut o = opt.splitn(2, '=');
            let kind = o.next().context("`shared-dir` options must not be empty")?;
            let value = o
                .next()
                .context("`shared-dir` options must be of the form `kind=value`")?;

            if !ugid_cfg
                .parse_ugid_config(kind, value)
                .context("failed to parse ugid config")?
            {
                match kind {
                    "type" => {
                        shared_dir.kind = value.parse().with_context(|| {
                            anyhow!("`type` must be one of `fs` or `9p` but {value}")
                        })?
                    }
                    _ => type_opts.push(opt),
                }
            }
        }
        shared_dir.ugid = (ugid_cfg.uid, ugid_cfg.gid);
        shared_dir.uid_map = ugid_cfg.uid_map;
        shared_dir.gid_map = ugid_cfg.gid_map;

        match shared_dir.kind {
            SharedDirKind::FS => {
                shared_dir.fs_cfg = from_key_values(&type_opts.join(","))
                    .map_err(|e| anyhow!("failed to parse fs config '{:?}': {e}", type_opts))?;

                if shared_dir.fs_cfg.ascii_casefold && !shared_dir.fs_cfg.negative_timeout.is_zero()
                {
                    // Disallow the combination of `ascii_casefold` and `negative_timeout` because
                    // negative dentry caches doesn't wort well in scenarios like the following:
                    // 1. Lookup "foo", an non-existing file. Negative dentry is cached on the
                    //    guest.
                    // 2. Create "FOO".
                    // 3. Lookup "foo". This needs to be successful on the casefold directory, but
                    //    the lookup can fail due the negative cache created at 1.
                    bail!("'negative_timeout' cannot be used with 'ascii_casefold'");
                }
            }
            SharedDirKind::P9 => {
                shared_dir.p9_cfg = type_opts
                    .join(":")
                    .parse()
                    .map_err(|e| anyhow!("failed to parse 9p config '{:?}': {e}", type_opts))?;
            }
        }
        Ok(shared_dir)
    }
}

#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
#[serde(deny_unknown_fields)]
pub struct PmemExt2Option {
    pub path: PathBuf,
    pub blocks_per_group: u32,
    pub inodes_per_group: u32,
    pub size: u32,
    pub ugid: (Option<u32>, Option<u32>),
    pub uid_map: String,
    pub gid_map: String,
}

impl Default for PmemExt2Option {
    fn default() -> Self {
        let blocks_per_group = 4096;
        let inodes_per_group = 1024;
        let size = ext2::BLOCK_SIZE as u32 * blocks_per_group; // only one block group
        let ugid_cfg = UgidConfig::default();
        Self {
            path: Default::default(),
            blocks_per_group,
            inodes_per_group,
            size,
            ugid: (ugid_cfg.uid, ugid_cfg.gid),
            uid_map: ugid_cfg.uid_map,
            gid_map: ugid_cfg.gid_map,
        }
    }
}

pub fn parse_pmem_ext2_option(param: &str) -> Result<PmemExt2Option, String> {
    let mut opt = PmemExt2Option::default();
    let mut components = param.split(':');
    opt.path = PathBuf::from(
        components
            .next()
            .ok_or("missing source path for `pmem-ext2`")?,
    );

    let mut ugid_cfg = UgidConfig::default();
    for c in components {
        let mut o = c.splitn(2, '=');
        let kind = o.next().ok_or("`pmem-ext2` options must not be empty")?;
        let value = o
            .next()
            .ok_or("`pmem-ext2` options must be of the form `kind=value`")?;

        if !ugid_cfg
            .parse_ugid_config(kind, value)
            .map_err(|e| format!("failed to parse ugid config for pmem-ext2: {:#}", e))?
        {
            match kind {
                "blocks_per_group" => {
                    opt.blocks_per_group = value.parse().map_err(|e| {
                        format!("failed to parse blocks_per_groups '{value}': {:#}", e)
                    })?
                }
                "inodes_per_group" => {
                    opt.inodes_per_group = value.parse().map_err(|e| {
                        format!("failed to parse inodes_per_groups '{value}': {:#}", e)
                    })?
                }
                "size" => {
                    opt.size = value
                        .parse()
                        .map_err(|e| format!("failed to parse memory size '{value}': {:#}", e))?
                }
                _ => return Err(format!("invalid `pmem-ext2` option: {}", kind)),
            }
        }
    }
    opt.ugid = (ugid_cfg.uid, ugid_cfg.gid);
    opt.uid_map = ugid_cfg.uid_map;
    opt.gid_map = ugid_cfg.gid_map;

    Ok(opt)
}

#[cfg(test)]
mod tests {
    use std::path::Path;
    use std::path::PathBuf;
    use std::time::Duration;

    use argh::FromArgs;
    use devices::virtio::fs::CachePolicy;

    use super::*;
    use crate::crosvm::config::from_key_values;

    #[test]
    fn parse_coiommu_options() {
        use std::time::Duration;

        use devices::CoIommuParameters;
        use devices::CoIommuUnpinPolicy;

        // unpin_policy
        let coiommu_params = from_key_values::<CoIommuParameters>("unpin_policy=off").unwrap();
        assert_eq!(
            coiommu_params,
            CoIommuParameters {
                unpin_policy: CoIommuUnpinPolicy::Off,
                ..Default::default()
            }
        );
        let coiommu_params = from_key_values::<CoIommuParameters>("unpin_policy=lru").unwrap();
        assert_eq!(
            coiommu_params,
            CoIommuParameters {
                unpin_policy: CoIommuUnpinPolicy::Lru,
                ..Default::default()
            }
        );
        let coiommu_params = from_key_values::<CoIommuParameters>("unpin_policy=foo");
        assert!(coiommu_params.is_err());

        // unpin_interval
        let coiommu_params = from_key_values::<CoIommuParameters>("unpin_interval=42").unwrap();
        assert_eq!(
            coiommu_params,
            CoIommuParameters {
                unpin_interval: Duration::from_secs(42),
                ..Default::default()
            }
        );
        let coiommu_params = from_key_values::<CoIommuParameters>("unpin_interval=foo");
        assert!(coiommu_params.is_err());

        // unpin_limit
        let coiommu_params = from_key_values::<CoIommuParameters>("unpin_limit=256").unwrap();
        assert_eq!(
            coiommu_params,
            CoIommuParameters {
                unpin_limit: Some(256),
                ..Default::default()
            }
        );
        let coiommu_params = from_key_values::<CoIommuParameters>("unpin_limit=0");
        assert!(coiommu_params.is_err());
        let coiommu_params = from_key_values::<CoIommuParameters>("unpin_limit=foo");
        assert!(coiommu_params.is_err());

        // unpin_gen_threshold
        let coiommu_params =
            from_key_values::<CoIommuParameters>("unpin_gen_threshold=32").unwrap();
        assert_eq!(
            coiommu_params,
            CoIommuParameters {
                unpin_gen_threshold: 32,
                ..Default::default()
            }
        );
        let coiommu_params = from_key_values::<CoIommuParameters>("unpin_gen_threshold=foo");
        assert!(coiommu_params.is_err());

        // All together
        let coiommu_params = from_key_values::<CoIommuParameters>(
            "unpin_policy=lru,unpin_interval=90,unpin_limit=8,unpin_gen_threshold=64",
        )
        .unwrap();
        assert_eq!(
            coiommu_params,
            CoIommuParameters {
                unpin_policy: CoIommuUnpinPolicy::Lru,
                unpin_interval: Duration::from_secs(90),
                unpin_limit: Some(8),
                unpin_gen_threshold: 64,
            }
        );

        // invalid parameter
        let coiommu_params = from_key_values::<CoIommuParameters>("unpin_invalid_param=0");
        assert!(coiommu_params.is_err());
    }

    #[test]
    fn vfio_pci_path() {
        let config: Config = crate::crosvm::cmdline::RunCommand::from_args(
            &[],
            &["--vfio", "/path/to/dev", "/dev/null"],
        )
        .unwrap()
        .try_into()
        .unwrap();

        let vfio = config.vfio.first().unwrap();

        assert_eq!(vfio.path, PathBuf::from("/path/to/dev"));
        assert_eq!(vfio.iommu, IommuDevType::NoIommu);
        assert_eq!(vfio.guest_address, None);
    }

    #[test]
    fn vfio_pci_path_coiommu() {
        let config: Config = crate::crosvm::cmdline::RunCommand::from_args(
            &[],
            &["--vfio", "/path/to/dev,iommu=coiommu", "/dev/null"],
        )
        .unwrap()
        .try_into()
        .unwrap();

        let vfio = config.vfio.first().unwrap();

        assert_eq!(vfio.path, PathBuf::from("/path/to/dev"));
        assert_eq!(vfio.iommu, IommuDevType::CoIommu);
        assert_eq!(vfio.guest_address, None);
    }

    #[test]
    fn vfio_pci_path_viommu_guest_address() {
        let config: Config = crate::crosvm::cmdline::RunCommand::from_args(
            &[],
            &[
                "--vfio",
                "/path/to/dev,iommu=viommu,guest-address=42:15.4",
                "/dev/null",
            ],
        )
        .unwrap()
        .try_into()
        .unwrap();

        let vfio = config.vfio.first().unwrap();

        assert_eq!(vfio.path, PathBuf::from("/path/to/dev"));
        assert_eq!(vfio.iommu, IommuDevType::VirtioIommu);
        assert_eq!(
            vfio.guest_address,
            Some(PciAddress::new(0, 0x42, 0x15, 4).unwrap())
        );
    }

    #[test]
    fn vfio_platform() {
        let config: Config = crate::crosvm::cmdline::RunCommand::from_args(
            &[],
            &["--vfio-platform", "/path/to/dev", "/dev/null"],
        )
        .unwrap()
        .try_into()
        .unwrap();

        let vfio = config.vfio.first().unwrap();

        assert_eq!(vfio.path, PathBuf::from("/path/to/dev"));
    }

    #[test]
    fn hypervisor_default() {
        let config: Config = crate::crosvm::cmdline::RunCommand::from_args(&[], &["/dev/null"])
            .unwrap()
            .try_into()
            .unwrap();

        assert_eq!(config.hypervisor, None);
    }

    #[test]
    fn hypervisor_kvm() {
        let config: Config = crate::crosvm::cmdline::RunCommand::from_args(
            &[],
            &["--hypervisor", "kvm", "/dev/null"],
        )
        .unwrap()
        .try_into()
        .unwrap();

        assert_eq!(
            config.hypervisor,
            Some(HypervisorKind::Kvm { device: None })
        );
    }

    #[test]
    fn hypervisor_kvm_device() {
        let config: Config = crate::crosvm::cmdline::RunCommand::from_args(
            &[],
            &["--hypervisor", "kvm[device=/not/default]", "/dev/null"],
        )
        .unwrap()
        .try_into()
        .unwrap();

        assert_eq!(
            config.hypervisor,
            Some(HypervisorKind::Kvm {
                device: Some(PathBuf::from("/not/default"))
            })
        );
    }

    #[test]
    #[cfg(any(target_arch = "arm", target_arch = "aarch64"))]
    #[cfg(feature = "geniezone")]
    fn hypervisor_geniezone() {
        let config: Config = crate::crosvm::cmdline::RunCommand::from_args(
            &[],
            &["--hypervisor", "geniezone", "/dev/null"],
        )
        .unwrap()
        .try_into()
        .unwrap();

        assert_eq!(
            config.hypervisor,
            Some(HypervisorKind::Geniezone { device: None })
        );
    }

    #[test]
    #[cfg(any(target_arch = "arm", target_arch = "aarch64"))]
    #[cfg(feature = "geniezone")]
    fn hypervisor_geniezone_device() {
        let config: Config = crate::crosvm::cmdline::RunCommand::from_args(
            &[],
            &[
                "--hypervisor",
                "geniezone[device=/not/default]",
                "/dev/null",
            ],
        )
        .unwrap()
        .try_into()
        .unwrap();

        assert_eq!(
            config.hypervisor,
            Some(HypervisorKind::Geniezone {
                device: Some(PathBuf::from("/not/default"))
            })
        );
    }

    #[test]
    #[cfg(all(any(target_arch = "arm", target_arch = "aarch64"), feature = "gunyah"))]
    fn hypervisor_gunyah() {
        let config: Config = crate::crosvm::cmdline::RunCommand::from_args(
            &[],
            &["--hypervisor", "gunyah", "/dev/null"],
        )
        .unwrap()
        .try_into()
        .unwrap();

        assert_eq!(
            config.hypervisor,
            Some(HypervisorKind::Gunyah { device: None })
        );
    }

    #[test]
    #[cfg(all(any(target_arch = "arm", target_arch = "aarch64"), feature = "gunyah"))]
    fn hypervisor_gunyah_device() {
        let config: Config = crate::crosvm::cmdline::RunCommand::from_args(
            &[],
            &["--hypervisor", "gunyah[device=/not/default]", "/dev/null"],
        )
        .unwrap()
        .try_into()
        .unwrap();

        assert_eq!(
            config.hypervisor,
            Some(HypervisorKind::Gunyah {
                device: Some(PathBuf::from("/not/default"))
            })
        );
    }

    #[test]
    fn parse_shared_dir() {
        // Although I want to test /usr/local/bin, Use / instead of
        // /usr/local/bin, as /usr/local/bin doesn't always exist.
        let s = "/:usr_local_bin:type=fs:cache=always:uidmap=0 655360 5000,5000 600 50,5050 660410 1994950:gidmap=0 655360 1065,1065 20119 1,1066 656426 3934,5000 600 50,5050 660410 1994950:timeout=3600:rewrite-security-xattrs=true:writeback=true";

        let shared_dir: SharedDir = s.parse().unwrap();
        assert_eq!(shared_dir.src, Path::new("/").to_path_buf());
        assert_eq!(shared_dir.tag, "usr_local_bin");
        assert!(shared_dir.kind == SharedDirKind::FS);
        assert_eq!(
            shared_dir.uid_map,
            "0 655360 5000,5000 600 50,5050 660410 1994950"
        );
        assert_eq!(
            shared_dir.gid_map,
            "0 655360 1065,1065 20119 1,1066 656426 3934,5000 600 50,5050 660410 1994950"
        );
        assert_eq!(shared_dir.fs_cfg.ascii_casefold, false);
        assert_eq!(shared_dir.fs_cfg.timeout, Duration::from_secs(3600));
        assert_eq!(shared_dir.fs_cfg.negative_timeout, Duration::ZERO);
        assert_eq!(shared_dir.fs_cfg.writeback, true);
        assert_eq!(
            shared_dir.fs_cfg.cache_policy,
            devices::virtio::fs::CachePolicy::Always
        );
        assert_eq!(shared_dir.fs_cfg.rewrite_security_xattrs, true);
        assert_eq!(shared_dir.fs_cfg.use_dax, false);
        assert_eq!(shared_dir.fs_cfg.posix_acl, true);
        assert_eq!(shared_dir.ugid, (None, None));
    }

    #[test]
    fn parse_shared_dir_parses_ascii_casefold_and_posix_acl() {
        // Although I want to test /usr/local/bin, Use / instead of
        // /usr/local/bin, as /usr/local/bin doesn't always exist.
        let s = "/:usr_local_bin:type=fs:ascii_casefold=true:posix_acl=false";

        let shared_dir: SharedDir = s.parse().unwrap();
        assert_eq!(shared_dir.fs_cfg.ascii_casefold, true);
        assert_eq!(shared_dir.fs_cfg.posix_acl, false);
    }

    #[test]
    fn parse_shared_dir_negative_timeout() {
        // Although I want to test /usr/local/bin, Use / instead of
        // /usr/local/bin, as /usr/local/bin doesn't always exist.
        let s = "/:usr_local_bin:type=fs:cache=always:timeout=3600:negative_timeout=60";

        let shared_dir: SharedDir = s.parse().unwrap();
        assert_eq!(shared_dir.src, Path::new("/").to_path_buf());
        assert_eq!(shared_dir.tag, "usr_local_bin");
        assert!(shared_dir.kind == SharedDirKind::FS);
        assert_eq!(
            shared_dir.fs_cfg.cache_policy,
            devices::virtio::fs::CachePolicy::Always
        );
        assert_eq!(shared_dir.fs_cfg.timeout, Duration::from_secs(3600));
        assert_eq!(shared_dir.fs_cfg.negative_timeout, Duration::from_secs(60));
    }

    #[test]
    fn parse_shared_dir_oem() {
        let shared_dir: SharedDir = "/:oem_etc:type=fs:cache=always:uidmap=0 299 1, 5000 600 50:gidmap=0 300 1, 5000 600 50:timeout=3600:rewrite-security-xattrs=true".parse().unwrap();
        assert_eq!(shared_dir.src, Path::new("/").to_path_buf());
        assert_eq!(shared_dir.tag, "oem_etc");
        assert!(shared_dir.kind == SharedDirKind::FS);
        assert_eq!(shared_dir.uid_map, "0 299 1, 5000 600 50");
        assert_eq!(shared_dir.gid_map, "0 300 1, 5000 600 50");
        assert_eq!(shared_dir.fs_cfg.ascii_casefold, false);
        assert_eq!(shared_dir.fs_cfg.timeout, Duration::from_secs(3600));
        assert_eq!(shared_dir.fs_cfg.negative_timeout, Duration::ZERO);
        assert_eq!(shared_dir.fs_cfg.writeback, false);
        assert_eq!(
            shared_dir.fs_cfg.cache_policy,
            devices::virtio::fs::CachePolicy::Always
        );
        assert_eq!(shared_dir.fs_cfg.rewrite_security_xattrs, true);
        assert_eq!(shared_dir.fs_cfg.use_dax, false);
        assert_eq!(shared_dir.fs_cfg.posix_acl, true);
        assert_eq!(shared_dir.ugid, (None, None));
    }

    #[test]
    #[cfg(feature = "arc_quota")]
    fn parse_shared_dir_arcvm_data() {
        // Test an actual ARCVM argument for /data/, where the path is replaced with `/`.
        let arcvm_arg = "/:_data:type=fs:cache=always:uidmap=0 655360 5000,5000 600 50,5050 660410 1994950:gidmap=0 655360 1065,1065 20119 1,1066 656426 3934,5000 600 50,5050 660410 1994950:timeout=3600:rewrite-security-xattrs=true:writeback=true:privileged_quota_uids=0";
        assert_eq!(
            arcvm_arg.parse::<SharedDir>().unwrap().fs_cfg,
            devices::virtio::fs::Config {
                cache_policy: CachePolicy::Always,
                timeout: Duration::from_secs(3600),
                rewrite_security_xattrs: true,
                writeback: true,
                privileged_quota_uids: vec![0],
                ..Default::default()
            }
        );
    }

    #[test]
    fn parse_shared_dir_ugid_set() {
        let shared_dir: SharedDir =
            "/:hostRoot:type=fs:uidmap=40417 40417 1:gidmap=5000 5000 1:uid=40417:gid=5000"
                .parse()
                .unwrap();
        assert_eq!(shared_dir.src, Path::new("/").to_path_buf());
        assert_eq!(shared_dir.tag, "hostRoot");
        assert!(shared_dir.kind == SharedDirKind::FS);
        assert_eq!(shared_dir.uid_map, "40417 40417 1");
        assert_eq!(shared_dir.gid_map, "5000 5000 1");
        assert_eq!(shared_dir.ugid, (Some(40417), Some(5000)));
    }

    #[test]
    fn parse_shared_dir_vm_fio() {
        // Tests shared-dir argurments used in ChromeOS's vm.Fio tast tests.

        // --shared-dir for rootfs
        let shared_dir: SharedDir =
            "/:root:type=fs:cache=always:timeout=5:writeback=false:dax=false:ascii_casefold=false"
                .parse()
                .unwrap();
        assert_eq!(
            shared_dir.fs_cfg,
            devices::virtio::fs::Config {
                cache_policy: CachePolicy::Always,
                timeout: Duration::from_secs(5),
                writeback: false,
                use_dax: false,
                ascii_casefold: false,
                ..Default::default()
            }
        );

        // --shared-dir for vm.Fio.virtiofs_dax_*
        let shared_dir: SharedDir =
            "/:shared:type=fs:cache=auto:timeout=1:writeback=true:dax=true:ascii_casefold=false"
                .parse()
                .unwrap();
        assert_eq!(
            shared_dir.fs_cfg,
            devices::virtio::fs::Config {
                cache_policy: CachePolicy::Auto,
                timeout: Duration::from_secs(1),
                writeback: true,
                use_dax: true,
                ascii_casefold: false,
                ..Default::default()
            }
        );
    }

    #[test]
    fn parse_cache_policy() {
        // The default policy is `auto`.
        assert_eq!(
            "/:_data:type=fs"
                .parse::<SharedDir>()
                .unwrap()
                .fs_cfg
                .cache_policy,
            CachePolicy::Auto
        );
        assert_eq!(
            "/:_data:type=fs:cache=always"
                .parse::<SharedDir>()
                .unwrap()
                .fs_cfg
                .cache_policy,
            CachePolicy::Always
        );
        assert_eq!(
            "/:_data:type=fs:cache=auto"
                .parse::<SharedDir>()
                .unwrap()
                .fs_cfg
                .cache_policy,
            CachePolicy::Auto
        );
        assert_eq!(
            "/:_data:type=fs:cache=never"
                .parse::<SharedDir>()
                .unwrap()
                .fs_cfg
                .cache_policy,
            CachePolicy::Never
        );

        // cache policy is case-sensitive
        assert!("/:_data:type=fs:cache=Always".parse::<SharedDir>().is_err());
        assert!("/:_data:type=fs:cache=ALWAYS".parse::<SharedDir>().is_err());
        assert!("/:_data:type=fs:cache=Auto".parse::<SharedDir>().is_err());
        assert!("/:_data:type=fs:cache=AUTO".parse::<SharedDir>().is_err());
        assert!("/:_data:type=fs:cache=Never".parse::<SharedDir>().is_err());
        assert!("/:_data:type=fs:cache=NEVER".parse::<SharedDir>().is_err());

        // we don't accept unknown policy
        assert!("/:_data:type=fs:cache=foobar".parse::<SharedDir>().is_err());
    }

    #[cfg(feature = "arc_quota")]
    #[test]
    fn parse_privileged_quota_uids() {
        assert_eq!(
            "/:_data:type=fs:privileged_quota_uids=0"
                .parse::<SharedDir>()
                .unwrap()
                .fs_cfg
                .privileged_quota_uids,
            vec![0]
        );
        assert_eq!(
            "/:_data:type=fs:privileged_quota_uids=0 1 2 3 4"
                .parse::<SharedDir>()
                .unwrap()
                .fs_cfg
                .privileged_quota_uids,
            vec![0, 1, 2, 3, 4]
        );
    }

    #[test]
    fn parse_dax() {
        // DAX is disabled by default
        assert!(
            !"/:_data:type=fs"
                .parse::<SharedDir>()
                .unwrap()
                .fs_cfg
                .use_dax
        );
        assert!(
            "/:_data:type=fs:dax=true"
                .parse::<SharedDir>()
                .unwrap()
                .fs_cfg
                .use_dax
        );
        assert!(
            !"/:_data:type=fs:dax=false"
                .parse::<SharedDir>()
                .unwrap()
                .fs_cfg
                .use_dax
        );
    }

    #[test]
    fn parse_pmem_ext2() {
        let config: Config = crate::crosvm::cmdline::RunCommand::from_args(
            &[],
            &["--pmem-ext2", "/path/to/dir", "/dev/null"],
        )
        .unwrap()
        .try_into()
        .unwrap();

        let opt = config.pmem_ext2.first().unwrap();

        assert_eq!(opt.path, PathBuf::from("/path/to/dir"));
    }

    #[test]
    fn parse_pmem_ext2_size() {
        let blocks_per_group = 2048;
        let inodes_per_group = 1024;
        let size = 4096 * blocks_per_group;

        let config: Config = crate::crosvm::cmdline::RunCommand::from_args(
            &[],
            &[
                "--pmem-ext2",
                &format!("/path/to/dir:blocks_per_group={blocks_per_group}:inodes_per_group={inodes_per_group}:size={size}"),
                "/dev/null",
            ],
        )
        .unwrap()
        .try_into()
        .unwrap();

        let opt = config.pmem_ext2.first().unwrap();

        assert_eq!(opt.path, PathBuf::from("/path/to/dir"));
        assert_eq!(opt.blocks_per_group, blocks_per_group);
        assert_eq!(opt.inodes_per_group, inodes_per_group);
        assert_eq!(opt.size, size);
    }
}