-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathtest_shred.rs
207 lines (158 loc) · 4.63 KB
/
test_shred.rs
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
// 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 wipesync
use crate::common::util::TestScenario;
#[test]
fn test_invalid_arg() {
new_ucmd!().arg("--definitely-invalid").fails().code_is(1);
}
#[test]
fn test_invalid_remove_arg() {
new_ucmd!().arg("--remove=unknown").fails().code_is(1);
}
#[test]
fn test_ambiguous_remove_arg() {
new_ucmd!().arg("--remove=wip").fails().code_is(1);
}
#[test]
fn test_shred() {
let (at, mut ucmd) = at_and_ucmd!();
let file = "test_shred";
let file_original_content = "test_shred file content";
at.write(file, file_original_content);
ucmd.arg(file).succeeds();
// File exists
assert!(at.file_exists(file));
// File is obfuscated
assert!(at.read_bytes(file) != file_original_content.as_bytes());
}
#[test]
fn test_shred_remove() {
let (at, mut ucmd) = at_and_ucmd!();
let file = "test_shred_remove";
at.touch(file);
ucmd.arg("--remove").arg(file).succeeds();
// File was deleted
assert!(!at.file_exists(file));
}
#[test]
fn test_shred_remove_unlink() {
// spell-checker:disable-next-line
for argument in ["--remove=unlink", "--remove=unlin", "--remove=u"] {
let (at, mut ucmd) = at_and_ucmd!();
let file = "test_shred_remove_unlink";
at.touch(file);
ucmd.arg(argument).arg(file).succeeds();
// File was deleted
assert!(!at.file_exists(file));
}
}
#[test]
fn test_shred_remove_wipe() {
let (at, mut ucmd) = at_and_ucmd!();
let file = "test_shred_remove_wipe";
at.touch(file);
ucmd.arg("--remove=wipe").arg(file).succeeds();
// File was deleted
assert!(!at.file_exists(file));
}
#[test]
fn test_shred_remove_wipesync() {
// spell-checker:disable-next-line
for argument in ["--remove=wipesync", "--remove=wipesyn", "--remove=wipes"] {
let (at, mut ucmd) = at_and_ucmd!();
let file = "test_shred_remove_wipesync";
at.touch(file);
ucmd.arg(argument).arg(file).succeeds();
// File was deleted
assert!(!at.file_exists(file));
}
}
#[test]
fn test_shred_u() {
let scene = TestScenario::new(util_name!());
let at = &scene.fixtures;
let file_a = "test_shred_remove_a";
let file_b = "test_shred_remove_b";
// Create file_a and file_b.
at.touch(file_a);
at.touch(file_b);
// Shred file_a.
scene.ucmd().arg("-u").arg(file_a).succeeds();
// file_a was deleted, file_b exists.
assert!(!at.file_exists(file_a));
assert!(at.file_exists(file_b));
}
#[test]
fn test_shred_force() {
let scene = TestScenario::new(util_name!());
let at = &scene.fixtures;
let file = "test_shred_force";
// Create file_a.
at.touch(file);
assert!(at.file_exists(file));
// Make file_a readonly.
at.set_readonly(file);
// Try shred -u.
scene.ucmd().arg("-u").arg(file).run();
// file_a was not deleted because it is readonly.
assert!(at.file_exists(file));
// Try shred -u -f.
scene.ucmd().arg("-u").arg("-f").arg(file).run();
// file_a was deleted.
assert!(!at.file_exists(file));
}
#[test]
fn test_hex() {
let (at, mut ucmd) = at_and_ucmd!();
let file = "test_hex";
at.touch(file);
ucmd.arg("--size=0x10").arg(file).succeeds();
}
#[test]
fn test_shred_empty() {
let scene = TestScenario::new(util_name!());
let at = &scene.fixtures;
let file_a = "test_shred_remove_a";
at.touch(file_a);
// Shred file_a and verify that, as it is empty, it doesn't have "pass 1/3 (random)"
scene
.ucmd()
.arg("-uv")
.arg(file_a)
.succeeds()
.stderr_does_not_contain("1/3 (random)");
assert!(!at.file_exists(file_a));
// if the file isn't empty, we should have random
at.touch(file_a);
at.write(file_a, "1");
scene
.ucmd()
.arg("-uv")
.arg(file_a)
.succeeds()
.stderr_contains("1/3 (random)");
assert!(!at.file_exists(file_a));
}
#[test]
#[cfg(all(unix, feature = "chmod"))]
fn test_shred_fail_no_perm() {
use std::path::Path;
let scene = TestScenario::new(util_name!());
let at = &scene.fixtures;
let dir = "dir";
let file = "test_shred_remove_a";
let binding = Path::new("dir").join(file);
let path = binding.to_str().unwrap();
at.mkdir(dir);
at.touch(path);
scene.ccmd("chmod").arg("a-w").arg(dir).succeeds();
scene
.ucmd()
.arg("-uv")
.arg(path)
.fails()
.stderr_contains("Couldn't rename to");
}