| 1 | // Copyright (C) 2020 Francois Marier |
| 2 | // |
| 3 | // This program is free software: you can redistribute it and/or modify |
| 4 | // it under the terms of the GNU General Public License as published by |
| 5 | // the Free Software Foundation, either version 3 of the License, or |
| 6 | // (at your option) any later version. |
| 7 | // |
| 8 | // This program is distributed in the hope that it will be useful, |
| 9 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | // GNU General Public License for more details. |
| 12 | // |
| 13 | // You should have received a copy of the GNU General Public License |
| 14 | // along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 15 | |
| 16 | #[cfg(test)] |
| 17 | mod tests { |
| 18 | use std::ffi::OsString; |
| 19 | use std::fs::{self, File}; |
| 20 | use std::io::{self, Write}; |
| 21 | use std::path::{Path, PathBuf}; |
| 22 | use tempfile::tempdir; |
| 23 | |
| 24 | #[test] |
| 25 | fn read_config() { |
| 26 | use super::super::read_config; |
| 27 | |
| 28 | use std::os::unix::fs::PermissionsExt; |
| 29 | |
| 30 | let dir = tempdir().unwrap(); |
| 31 | let file_path = dir.path().join("oneline"); |
| 32 | writeln!(File::create(&file_path).unwrap(), "/home").unwrap(); |
| 33 | let paths = read_config(&file_path).unwrap(); |
| 34 | assert_eq!(paths.len(), 1); |
| 35 | assert_eq!(paths, vec![PathBuf::from("/home")]); |
| 36 | |
| 37 | // Make the file unreadable and check for an error. |
| 38 | let mut perms = fs::metadata(&file_path).unwrap().permissions(); |
| 39 | perms.set_mode(0o200); // not readable by anyone |
| 40 | fs::set_permissions(&file_path, perms).unwrap(); |
| 41 | assert!(read_config(&file_path).is_none()); |
| 42 | } |
| 43 | |
| 44 | #[test] |
| 45 | fn read_config_missing_file() { |
| 46 | use super::super::read_config; |
| 47 | |
| 48 | let dir = tempdir().unwrap(); |
| 49 | let paths = read_config(dir.path().join("missing")).unwrap(); |
| 50 | assert!(paths.is_empty()); |
| 51 | } |
| 52 | |
| 53 | #[test] |
| 54 | fn read_config_empty_file() { |
| 55 | use super::super::read_config; |
| 56 | |
| 57 | let dir = tempdir().unwrap(); |
| 58 | let file_path = dir.path().join("empty"); |
| 59 | File::create(&file_path).unwrap(); |
| 60 | assert!(read_config(&file_path).unwrap().is_empty()); |
| 61 | } |
| 62 | |
| 63 | #[test] |
| 64 | fn parse_line() { |
| 65 | use super::super::parse_line; |
| 66 | use super::super::MAX_GLOB_EXPANSION; |
| 67 | |
| 68 | let filename = Path::new("/"); |
| 69 | |
| 70 | // Invalid lines |
| 71 | assert!(parse_line(filename.display(), Ok("/�".to_string())) |
| 72 | .unwrap() |
| 73 | .is_empty()); |
| 74 | assert!(parse_line( |
| 75 | filename.display(), |
| 76 | Err(io::Error::new(io::ErrorKind::Other, "")) |
| 77 | ) |
| 78 | .is_none()); |
| 79 | assert!(parse_line(filename.display(), Ok("/usr/***/bin".to_string())).is_none()); |
| 80 | |
| 81 | // Valid lines |
| 82 | assert_eq!( |
| 83 | parse_line(filename.display(), Ok("/".to_string())).unwrap(), |
| 84 | vec![PathBuf::from("/")] |
| 85 | ); |
| 86 | assert_eq!( |
| 87 | parse_line(filename.display(), Ok("/tmp/".to_string())).unwrap(), |
| 88 | vec![PathBuf::from("/tmp")] |
| 89 | ); |
| 90 | assert_eq!( |
| 91 | parse_line(filename.display(), Ok("/**".to_string())) |
| 92 | .unwrap() |
| 93 | .len(), |
| 94 | MAX_GLOB_EXPANSION |
| 95 | ); |
| 96 | } |
| 97 | |
| 98 | #[test] |
| 99 | fn symlink_canonicalize() { |
| 100 | use super::super::symlink_canonicalize; |
| 101 | |
| 102 | assert_eq!( |
| 103 | symlink_canonicalize(Path::new("/usr/bin")), |
| 104 | Some(PathBuf::from("/usr/bin")) |
| 105 | ); |
| 106 | assert_eq!( |
| 107 | symlink_canonicalize(Path::new("/usr/bin/../bin/sh")), |
| 108 | Some(PathBuf::from("/usr/bin/sh")) |
| 109 | ); |
| 110 | assert_eq!( |
| 111 | symlink_canonicalize(Path::new("/usr/")), |
| 112 | Some(PathBuf::from("/usr")) |
| 113 | ); |
| 114 | assert_eq!( |
| 115 | symlink_canonicalize(Path::new("/usr/.")), |
| 116 | Some(PathBuf::from("/usr")) |
| 117 | ); |
| 118 | assert_eq!( |
| 119 | symlink_canonicalize(Path::new("/usr/bin/./.././local")), |
| 120 | Some(PathBuf::from("/usr/local")) |
| 121 | ); |
| 122 | assert_eq!( |
| 123 | symlink_canonicalize(Path::new("/usr/..")), |
| 124 | Some(PathBuf::from("/")) |
| 125 | ); |
| 126 | assert_eq!( |
| 127 | symlink_canonicalize(Path::new("/")), |
| 128 | Some(PathBuf::from("/")) |
| 129 | ); |
| 130 | assert_eq!( |
| 131 | symlink_canonicalize(Path::new("/..")), |
| 132 | Some(PathBuf::from("/")) |
| 133 | ); |
| 134 | assert_eq!( |
| 135 | symlink_canonicalize(Path::new("/usr/bin")), |
| 136 | Some(PathBuf::from("/usr/bin")) |
| 137 | ); |
| 138 | |
| 139 | // Relative path |
| 140 | assert!(symlink_canonicalize(Path::new("Cargo.toml")) |
| 141 | .unwrap() |
| 142 | .is_absolute()); |
| 143 | |
| 144 | // Non-existent path |
| 145 | assert_eq!( |
| 146 | symlink_canonicalize(Path::new("/non/existent/path/to/file")), |
| 147 | None |
| 148 | ); |
| 149 | } |
| 150 | |
| 151 | #[test] |
| 152 | fn normalize_path() { |
| 153 | use super::super::normalize_path; |
| 154 | |
| 155 | assert_eq!(normalize_path(&OsString::from("/".to_string())), "/"); |
| 156 | assert_eq!(normalize_path(&OsString::from("/../.".to_string())), "/"); |
| 157 | assert_eq!(normalize_path(&OsString::from("/usr".to_string())), "/usr"); |
| 158 | assert_eq!(normalize_path(&OsString::from("/usr/".to_string())), "/usr"); |
| 159 | assert_eq!( |
| 160 | normalize_path(&OsString::from("/home/../usr".to_string())), |
| 161 | "/usr" |
| 162 | ); |
| 163 | assert_eq!(normalize_path(&OsString::from("".to_string())), ""); |
| 164 | assert_eq!(normalize_path(&OsString::from("foo".to_string())), "foo"); |
| 165 | assert_eq!( |
| 166 | normalize_path(&OsString::from("/tmp/�/".to_string())), |
| 167 | "/tmp/�/" |
| 168 | ); |
| 169 | } |
| 170 | |
| 171 | #[test] |
| 172 | fn filter_arguments() { |
| 173 | use super::super::filter_arguments; |
| 174 | |
| 175 | // Simple cases |
| 176 | assert_eq!( |
| 177 | filter_arguments( |
| 178 | vec![OsString::from("/safe".to_string())].into_iter(), |
| 179 | &vec![PathBuf::from("/safe")] |
| 180 | ), |
| 181 | Vec::<OsString>::new() |
| 182 | ); |
| 183 | assert_eq!( |
| 184 | filter_arguments( |
| 185 | vec![ |
| 186 | OsString::from("/safe".to_string()), |
| 187 | OsString::from("/unsafe".to_string()) |
| 188 | ] |
| 189 | .into_iter(), |
| 190 | &vec![PathBuf::from("/safe")] |
| 191 | ), |
| 192 | vec![OsString::from("/unsafe".to_string())] |
| 193 | ); |
| 194 | |
| 195 | // Degenerate cases |
| 196 | assert_eq!( |
| 197 | filter_arguments(Vec::<OsString>::new().into_iter(), &Vec::<PathBuf>::new()), |
| 198 | Vec::<OsString>::new() |
| 199 | ); |
| 200 | assert_eq!( |
| 201 | filter_arguments( |
| 202 | vec![ |
| 203 | OsString::from("/safe".to_string()), |
| 204 | OsString::from("/unsafe".to_string()) |
| 205 | ] |
| 206 | .into_iter(), |
| 207 | &Vec::<PathBuf>::new() |
| 208 | ), |
| 209 | vec![ |
| 210 | OsString::from("/safe".to_string()), |
| 211 | OsString::from("/unsafe".to_string()) |
| 212 | ] |
| 213 | ); |
| 214 | assert_eq!( |
| 215 | filter_arguments( |
| 216 | Vec::<OsString>::new().into_iter(), |
| 217 | &vec![PathBuf::from("/safe")] |
| 218 | ), |
| 219 | Vec::<OsString>::new() |
| 220 | ); |
| 221 | |
| 222 | // Relative path |
| 223 | assert_eq!( |
| 224 | filter_arguments( |
| 225 | vec![ |
| 226 | OsString::from("/../".to_string()), |
| 227 | OsString::from("/unsafe".to_string()) |
| 228 | ] |
| 229 | .into_iter(), |
| 230 | &vec![PathBuf::from("/")] |
| 231 | ), |
| 232 | vec![OsString::from("/unsafe".to_string())] |
| 233 | ); |
| 234 | } |
| 235 | |
| 236 | #[test] |
| 237 | fn filter_arguments_symlinks() { |
| 238 | use super::super::filter_arguments; |
| 239 | |
| 240 | use std::os::unix::fs; |
| 241 | |
| 242 | let dir = tempdir().unwrap(); |
| 243 | let empty_file = dir.path().join("empty"); |
| 244 | File::create(&empty_file).unwrap(); |
| 245 | |
| 246 | // Normal symlinks should not be protected. |
| 247 | let unprotected_symlink = dir.path().join("unprotected_symlink"); |
| 248 | fs::symlink(&empty_file, &unprotected_symlink).unwrap(); |
| 249 | |
| 250 | // A symlink explicitly listed in a config file should be protected. |
| 251 | let protected_symlink = dir.path().join("protected_symlink"); |
| 252 | fs::symlink(&empty_file, &protected_symlink).unwrap(); |
| 253 | |
| 254 | // A symlink to a protected file should not be protected itself. |
| 255 | let symlink_to_protected_file = dir.path().join("usr"); |
| 256 | fs::symlink("/usr", &symlink_to_protected_file).unwrap(); |
| 257 | |
| 258 | assert_eq!( |
| 259 | filter_arguments( |
| 260 | vec![ |
| 261 | OsString::from(&empty_file), |
| 262 | OsString::from(&unprotected_symlink), |
| 263 | OsString::from(&protected_symlink), |
| 264 | OsString::from(&symlink_to_protected_file), |
| 265 | ] |
| 266 | .into_iter(), |
| 267 | &vec![PathBuf::from("/usr"), PathBuf::from(&protected_symlink)] |
| 268 | ), |
| 269 | vec![empty_file, unprotected_symlink, symlink_to_protected_file] |
| 270 | ); |
| 271 | } |
| 272 | |
| 273 | #[test] |
| 274 | fn read_config_files() { |
| 275 | use super::super::read_config_files; |
| 276 | use super::super::DEFAULT_PATHS; |
| 277 | |
| 278 | use std::io::Write; |
| 279 | use tempfile::tempdir; |
| 280 | |
| 281 | let dir = tempdir().unwrap(); |
| 282 | let file_path1 = dir.path().join("home"); |
| 283 | writeln!(File::create(&file_path1).unwrap(), "/home").unwrap(); |
| 284 | let file_path2 = dir.path().join("tmp"); |
| 285 | writeln!(File::create(&file_path2).unwrap(), "/tmp").unwrap(); |
| 286 | |
| 287 | // Empty config |
| 288 | assert_eq!(read_config_files(&[], &[]).len(), DEFAULT_PATHS.len()); |
| 289 | |
| 290 | // Sorted |
| 291 | assert_eq!( |
| 292 | read_config_files( |
| 293 | &[file_path2.to_str().unwrap(), file_path1.to_str().unwrap()], |
| 294 | &[] |
| 295 | ), |
| 296 | vec![PathBuf::from("/home"), PathBuf::from("/tmp")] |
| 297 | ); |
| 298 | |
| 299 | // Duplicate lines |
| 300 | assert_eq!( |
| 301 | read_config_files( |
| 302 | &[file_path1.to_str().unwrap(), file_path1.to_str().unwrap()], |
| 303 | &[] |
| 304 | ), |
| 305 | vec![PathBuf::from("/home")] |
| 306 | ); |
| 307 | } |
| 308 | |
| 309 | #[test] |
| 310 | fn run() { |
| 311 | use super::super::run; |
| 312 | use super::super::REAL_RM; |
| 313 | |
| 314 | use std::io::Write; |
| 315 | use tempfile::tempdir; |
| 316 | |
| 317 | let dir = tempdir().unwrap(); |
| 318 | let empty_file = dir.path().join("empty").to_str().unwrap().to_string(); |
| 319 | File::create(&empty_file).unwrap(); |
| 320 | let missing_file = dir.path().join("missing").to_str().unwrap().to_string(); |
| 321 | let file1 = dir.path().join("file1").to_str().unwrap().to_string(); |
| 322 | let file2 = dir.path().join("file2").to_str().unwrap().to_string(); |
| 323 | File::create(&file1).unwrap(); |
| 324 | File::create(&file2).unwrap(); |
| 325 | |
| 326 | // Trying to delete a directory without "-r" should fail. |
| 327 | assert_eq!( |
| 328 | run( |
| 329 | REAL_RM, |
| 330 | vec![OsString::from(dir.path())].into_iter(), |
| 331 | &[], |
| 332 | &[] |
| 333 | ), |
| 334 | 1 |
| 335 | ); |
| 336 | |
| 337 | // One file to delete, one directory to ignore. |
| 338 | assert_eq!(Path::new(&empty_file).exists(), true); |
| 339 | assert_eq!( |
| 340 | run( |
| 341 | REAL_RM, |
| 342 | vec![ |
| 343 | OsString::from(&empty_file), |
| 344 | OsString::from("/usr".to_string()) |
| 345 | ] |
| 346 | .into_iter(), |
| 347 | &[], |
| 348 | &[] |
| 349 | ), |
| 350 | 0 |
| 351 | ); |
| 352 | assert_eq!(Path::new(&empty_file).exists(), false); |
| 353 | |
| 354 | // When the real rm can't be found, run() fails. |
| 355 | File::create(&empty_file).unwrap(); |
| 356 | assert_eq!(Path::new(&empty_file).exists(), true); |
| 357 | assert_eq!( |
| 358 | run( |
| 359 | &missing_file, |
| 360 | vec![OsString::from(&empty_file)].into_iter(), |
| 361 | &[], |
| 362 | &[] |
| 363 | ), |
| 364 | 1 |
| 365 | ); |
| 366 | assert_eq!(Path::new(&empty_file).exists(), true); |
| 367 | |
| 368 | // Trying to delete a missing file should fail. |
| 369 | assert_eq!( |
| 370 | run( |
| 371 | REAL_RM, |
| 372 | vec![OsString::from(&missing_file)].into_iter(), |
| 373 | &[], |
| 374 | &[] |
| 375 | ), |
| 376 | 1 |
| 377 | ); |
| 378 | |
| 379 | // The "--help" option should work. |
| 380 | assert_eq!( |
| 381 | run( |
| 382 | REAL_RM, |
| 383 | vec![OsString::from("--help".to_string())].into_iter(), |
| 384 | &[], |
| 385 | &[] |
| 386 | ), |
| 387 | 0 |
| 388 | ); |
| 389 | |
| 390 | // The contents of a directory can be protected using a wildcard. |
| 391 | let config_file = dir.path().join("config").to_str().unwrap().to_string(); |
| 392 | writeln!( |
| 393 | File::create(&config_file).unwrap(), |
| 394 | "{}", |
| 395 | dir.path().join("*").to_str().unwrap() |
| 396 | ) |
| 397 | .unwrap(); |
| 398 | assert_eq!( |
| 399 | run( |
| 400 | REAL_RM, |
| 401 | vec![OsString::from(&file1), OsString::from(&file2)].into_iter(), |
| 402 | &[&config_file], |
| 403 | &[] |
| 404 | ), |
| 405 | 1 |
| 406 | ); |
| 407 | assert_eq!(Path::new(&file1).exists(), true); |
| 408 | assert_eq!(Path::new(&file2).exists(), true); |
| 409 | } |
| 410 | |
| 411 | #[test] |
| 412 | fn ensure_real_rm_is_callable() { |
| 413 | use super::super::ensure_real_rm_is_callable; |
| 414 | |
| 415 | assert!(ensure_real_rm_is_callable().is_ok()); |
| 416 | } |
| 417 | } |