summaryrefslogtreecommitdiff
path: root/test/common/mod.rs
blob: 544a1ae3f0ad4a3d917c5057082df6a5b7f5a8e2 (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
use cfg_if::cfg_if;

#[macro_export] macro_rules! skip {
    ($($reason: expr),+) => {
        use ::std::io::{self, Write};

        let stderr = io::stderr();
        let mut handle = stderr.lock();
        writeln!(handle, $($reason),+).unwrap();
        return;
    }
}

cfg_if! {
    if #[cfg(any(target_os = "android", target_os = "linux"))] {
        #[macro_export] macro_rules! require_capability {
            ($name:expr, $capname:ident) => {
                use ::caps::{Capability, CapSet, has_cap};

                if !has_cap(None, CapSet::Effective, Capability::$capname)
                    .unwrap()
                {
                    skip!("{} requires capability {}. Skipping test.", $name, Capability::$capname);
                }
            }
        }
    } else if #[cfg(not(target_os = "redox"))] {
        #[macro_export] macro_rules! require_capability {
            ($name:expr, $capname:ident) => {}
        }
    }
}

/// Skip the test if we don't have the ability to mount file systems.
#[cfg(target_os = "freebsd")]
#[macro_export] macro_rules! require_mount {
    ($name:expr) => {
        use ::sysctl::CtlValue;
        use nix::unistd::Uid;

        if !Uid::current().is_root() && CtlValue::Int(0) == ::sysctl::value("vfs.usermount").unwrap()
        {
            skip!("{} requires the ability to mount file systems. Skipping test.", $name);
        }
    }
}

#[cfg(any(target_os = "linux", target_os= "android"))]
#[macro_export] macro_rules! skip_if_cirrus {
    ($reason:expr) => {
        if std::env::var_os("CIRRUS_CI").is_some() {
            skip!("{}", $reason);
        }
    }
}

#[cfg(target_os = "freebsd")]
#[macro_export] macro_rules! skip_if_jailed {
    ($name:expr) => {
        use ::sysctl::CtlValue;

        if let CtlValue::Int(1) = ::sysctl::value("security.jail.jailed")
            .unwrap()
        {
            skip!("{} cannot run in a jail. Skipping test.", $name);
        }
    }
}

#[cfg(not(any(target_os = "redox", target_os = "fuchsia")))]
#[macro_export] macro_rules! skip_if_not_root {
    ($name:expr) => {
        use nix::unistd::Uid;

        if !Uid::current().is_root() {
            skip!("{} requires root privileges. Skipping test.", $name);
        }
    };
}

cfg_if! {
    if #[cfg(any(target_os = "android", target_os = "linux"))] {
        #[macro_export] macro_rules! skip_if_seccomp {
            ($name:expr) => {
                if let Ok(s) = std::fs::read_to_string("/proc/self/status") {
                    for l in s.lines() {
                        let mut fields = l.split_whitespace();
                        if fields.next() == Some("Seccomp:") &&
                            fields.next() != Some("0")
                        {
                            skip!("{} cannot be run in Seccomp mode.  Skipping test.",
                                stringify!($name));
                        }
                    }
                }
            }
        }
    } else if #[cfg(not(target_os = "redox"))] {
        #[macro_export] macro_rules! skip_if_seccomp {
            ($name:expr) => {}
        }
    }
}

cfg_if! {
    if #[cfg(target_os = "linux")] {
        #[macro_export] macro_rules! require_kernel_version {
            ($name:expr, $version_requirement:expr) => {
                use semver::{Version, VersionReq};

                let version_requirement = VersionReq::parse($version_requirement)
                        .expect("Bad match_version provided");

                let uname = nix::sys::utsname::uname().unwrap();
                println!("{}", uname.sysname().to_str().unwrap());
                println!("{}", uname.nodename().to_str().unwrap());
                println!("{}", uname.release().to_str().unwrap());
                println!("{}", uname.version().to_str().unwrap());
                println!("{}", uname.machine().to_str().unwrap());

                // Fix stuff that the semver parser can't handle
                let fixed_release = &uname.release().to_str().unwrap().to_string()
                    // Fedora 33 reports version as 4.18.el8_2.x86_64 or
                    // 5.18.200-fc33.x86_64.  Remove the underscore.
                    .replace("_", "-")
                    // Cirrus-CI reports version as 4.19.112+ .  Remove the +
                    .replace("+", "");
                let mut version = Version::parse(fixed_release).unwrap();

                //Keep only numeric parts
                version.pre = semver::Prerelease::EMPTY;
                version.build = semver::BuildMetadata::EMPTY;

                if !version_requirement.matches(&version) {
                    skip!("Skip {} because kernel version `{}` doesn't match the requirement `{}`",
                        stringify!($name), version, version_requirement);
                }
            }
        }
    }
}