File: test_mkfifo.rs

package info (click to toggle)
rust-coreutils 0.7.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 505,620 kB
  • sloc: ansic: 103,594; asm: 28,570; sh: 8,910; python: 5,581; makefile: 472; cpp: 97; javascript: 72
file content (241 lines) | stat: -rw-r--r-- 7,156 bytes parent folder | download
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
// This file is part of the uutils coreutils package.
//
// For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code.

// spell-checker:ignore nconfined

#[cfg(feature = "feat_selinux")]
use uucore::selinux::get_getfattr_output;
use uutests::new_ucmd;
use uutests::util::TestScenario;
use uutests::util_name;

#[test]
fn test_invalid_arg() {
    new_ucmd!().arg("--definitely-invalid").fails_with_code(1);
}

#[test]
fn test_create_fifo_missing_operand() {
    new_ucmd!().fails().stderr_is("mkfifo: missing operand\n");
}

#[test]
fn test_create_one_fifo() {
    new_ucmd!().arg("abc").succeeds();
}

#[test]
fn test_create_one_fifo_with_invalid_mode() {
    new_ucmd!()
        .arg("abcd")
        .arg("-m")
        .arg("invalid")
        .fails()
        .stderr_contains("invalid mode");

    new_ucmd!()
        .arg("abcd")
        .arg("-m")
        .arg("0999")
        .fails()
        .stderr_contains("invalid mode");
}

#[test]
fn test_create_one_fifo_with_non_file_permission_mode() {
    new_ucmd!()
        .arg("abcd")
        .arg("-m")
        .arg("1777")
        .fails()
        .stderr_is("mkfifo: mode must specify only file permission bits\n");
    new_ucmd!()
        .arg("abcd")
        .arg("-m")
        .arg("1999")
        .fails()
        .stderr_contains("invalid mode");
}

#[test]
fn test_create_multiple_fifos() {
    new_ucmd!()
        .arg("abcde")
        .arg("def")
        .arg("sed")
        .arg("dum")
        .succeeds();
}

#[test]
fn test_create_one_fifo_with_mode() {
    new_ucmd!().arg("abcde").arg("-m600").succeeds();
}

#[test]
fn test_create_one_fifo_already_exists() {
    new_ucmd!()
        .arg("abcdef")
        .arg("abcdef")
        .fails()
        .stderr_is("mkfifo: cannot create fifo 'abcdef': File exists\n");
}

#[test]
fn test_create_fifo_with_mode_and_umask() {
    use uucore::fs::display_permissions;
    let scene = TestScenario::new(util_name!());
    let at = &scene.fixtures;

    let test_fifo_creation = |mode: &str, umask: u16, expected: &str| {
        scene
            .ucmd()
            .arg("-m")
            .arg(mode)
            .arg(format!("fifo_test_{mode}"))
            .umask(libc::mode_t::from(umask))
            .succeeds();

        let metadata = std::fs::metadata(at.subdir.join(format!("fifo_test_{mode}"))).unwrap();
        let permissions = display_permissions(&metadata, true);
        assert_eq!(permissions, expected.to_string());
    };

    test_fifo_creation("734", 0o077, "prwx-wxr--"); // spell-checker:disable-line
    test_fifo_creation("706", 0o777, "prwx---rw-"); // spell-checker:disable-line
    test_fifo_creation("a=rwx", 0o022, "prwxrwxrwx"); // spell-checker:disable-line
    test_fifo_creation("a=rx", 0o022, "pr-xr-xr-x"); // spell-checker:disable-line
    test_fifo_creation("a=r", 0o022, "pr--r--r--"); // spell-checker:disable-line
    test_fifo_creation("=rwx", 0o022, "prwxr-xr-x"); // spell-checker:disable-line
    test_fifo_creation("u+w", 0o022, "prw-rw-rw-"); // spell-checker:disable-line
    test_fifo_creation("u-w", 0o022, "pr--rw-rw-"); // spell-checker:disable-line
    test_fifo_creation("u+x", 0o022, "prwxrw-rw-"); // spell-checker:disable-line
    test_fifo_creation("u-r,g-w,o+x", 0o022, "p-w-r--rwx"); // spell-checker:disable-line
    test_fifo_creation("a=rwx,o-w", 0o022, "prwxrwxr-x"); // spell-checker:disable-line
    test_fifo_creation("=rwx,o-w", 0o022, "prwxr-xr-x"); // spell-checker:disable-line
    test_fifo_creation("ug+rw,o+r", 0o022, "prw-rw-rw-"); // spell-checker:disable-line
    test_fifo_creation("u=rwx,g=rx,o=", 0o022, "prwxr-x---"); // spell-checker:disable-line
}

#[test]
fn test_create_fifo_with_umask() {
    use uucore::fs::display_permissions;
    let scene = TestScenario::new(util_name!());
    let at = &scene.fixtures;

    let test_fifo_creation = |umask: u16, expected: &str| {
        scene
            .ucmd()
            .arg("fifo_test")
            .umask(libc::mode_t::from(umask))
            .succeeds();

        let metadata = std::fs::metadata(at.subdir.join("fifo_test")).unwrap();
        let permissions = display_permissions(&metadata, true);
        assert_eq!(permissions, expected.to_string());
        at.remove("fifo_test");
    };

    test_fifo_creation(0o022, "prw-r--r--"); // spell-checker:disable-line
    test_fifo_creation(0o777, "p---------"); // spell-checker:disable-line
}

#[test]
fn test_create_fifo_permission_denied() {
    let scene = TestScenario::new(util_name!());
    let at = &scene.fixtures;

    let no_exec_dir = "owner_no_exec_dir";
    let named_pipe = "owner_no_exec_dir/mkfifo_err";

    at.mkdir(no_exec_dir);
    at.set_mode(no_exec_dir, 0o644);

    // We no longer attempt to modify file permission if the file was failed to be created.
    // Therefore the error message should only contain "cannot create".
    let err_msg = format!("mkfifo: cannot create fifo '{named_pipe}': File exists\n");

    scene
        .ucmd()
        .arg(named_pipe)
        .arg("-m")
        .arg("666")
        .fails()
        .stderr_is(err_msg.as_str());
}

#[test]
#[cfg(feature = "feat_selinux")]
fn test_mkfifo_selinux() {
    let ts = TestScenario::new(util_name!());
    let at = &ts.fixtures;
    let dest = "test_file";
    let args = [
        "-Z",
        "--context",
        "--context=unconfined_u:object_r:user_tmp_t:s0",
    ];
    for arg in args {
        ts.ucmd().arg(arg).arg(dest).succeeds();
        assert!(at.is_fifo("test_file"));

        let context_value = get_getfattr_output(&at.plus_as_string(dest));
        assert!(
            context_value.contains("unconfined_u"),
            "Expected 'unconfined_u' not found in getfattr output:\n{context_value}"
        );
        at.remove(&at.plus_as_string(dest));
    }
}

#[test]
#[cfg(feature = "feat_selinux")]
fn test_mkfifo_selinux_invalid() {
    let scene = TestScenario::new(util_name!());
    let at = &scene.fixtures;
    let dest = "orig";

    let args = [
        "--context=a",
        "--context=unconfined_u:object_r:user_tmp_t:s0:a",
        "--context=nconfined_u:object_r:user_tmp_t:s0",
    ];
    for arg in args {
        new_ucmd!()
            .arg(arg)
            .arg(dest)
            .fails()
            .stderr_contains("failed to");
        if at.file_exists(dest) {
            at.remove(dest);
        }
    }
}

#[test]
fn test_mkfifo_permission_unchanged_when_failed() {
    use uucore::fs::display_permissions;

    let scene = TestScenario::new(util_name!());
    let at = &scene.fixtures;

    let file_name = "test_file";
    at.write(file_name, "content");
    at.set_mode(file_name, 0o600);

    let err_msg = format!("mkfifo: cannot create fifo '{file_name}': File exists\n");

    scene
        .ucmd()
        .arg(file_name)
        .arg("-m")
        .arg("666")
        .fails()
        .stderr_is(err_msg.as_str());
    let metadata = std::fs::metadata(at.subdir.join(file_name)).unwrap();
    let permissions = display_permissions(&metadata, true);
    let expected = "-rw-------";
    assert_eq!(permissions, expected.to_string());
}