fif 0.8.0

A command-line tool for detecting and optionally correcting files with incorrect extensions.
Documentation
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
// SPDX-FileCopyrightText: 2021-2024 Lynnesbian
// SPDX-License-Identifier: GPL-3.0-or-later

use std::collections::{BTreeMap, HashMap};
use std::ffi::OsStr;
use std::path::{Path, PathBuf};

use clap::Parser;
use fif::files::{mime_extension_lookup, scan_directory, scan_from_walkdir, BUF_SIZE};
use fif::findings::Findings;
use fif::formats::{Format, PowerShell, Shell};
use fif::mime_db::MimeDb;
use fif::utils::APPLICATION_ZIP;
use fif::{String, MIMEDB};
use itertools::Itertools;
use maplit::{btreeset, hashmap};
use mime::{Mime, APPLICATION_OCTET_STREAM, APPLICATION_PDF, IMAGE_JPEG, IMAGE_PNG};

use crate::parameters::ExtensionSet;
use crate::parameters::Parameters;

const JPEG_BYTES: &[u8] = b"\xFF\xD8\xFF";
const PNG_BYTES: &[u8] = b"\x89\x50\x4E\x47\x0D\x0A\x1A\x0A";
const PDF_BYTES: &[u8] = b"%PDF-";
const ZIP_BYTES: &[u8] = b"PK\x03\x04";

#[test]
/// Ensure that `extension_from_path` successfully returns the extension from a set of paths.
fn get_ext() {
	let ext_checks: HashMap<_, Option<&OsStr>> = hashmap![
		Path::new("test.txt") => Some(OsStr::new("txt")),
		Path::new("test.zip") => Some(OsStr::new("zip")),
		Path::new("test.tar.gz") => Some(OsStr::new("gz")),
		Path::new("test.") => Some(OsStr::new("")),
		Path::new("test") => None,
		Path::new(".hidden") => None,
	];

	for (path, ext) in ext_checks {
		assert_eq!(path.extension(), ext);
	}
}

#[test]
/// Ensure that the MIME types for JPEG, PNG, PDF, and ZIP are detected from their magic numbers.
fn detect_type() {
	assert_eq!(MIMEDB.get_type(JPEG_BYTES), Some(IMAGE_JPEG));
	assert_eq!(MIMEDB.get_type(PNG_BYTES), Some(IMAGE_PNG));
	assert_eq!(MIMEDB.get_type(PDF_BYTES), Some(APPLICATION_PDF));
	assert_eq!(MIMEDB.get_type(ZIP_BYTES), Some(APPLICATION_ZIP.clone()));
}

#[test]
/// Ensure that `mime_extension_lookup` works as expected, and that the set of extensions for JPEG, PNG, PDF, and ZIP
/// contain "jpg", "png", "pdf", and "zip", respectively.
fn recommend_ext() {
	let tests = hashmap![
		&IMAGE_JPEG => "jpg",
		&IMAGE_PNG => "png",
		&APPLICATION_PDF => "pdf",
		&*APPLICATION_ZIP => "zip",
	];

	for (mime, ext) in tests {
		assert!(
			mime_extension_lookup(mime.essence_str().into())
				.unwrap()
				.contains(&String::from(ext)),
			"mime_extension_lookup for {} didn't contain {}!",
			mime.essence_str(),
			ext
		);
	}
}

#[test]
/// Create a simple directory with some files, run `scan_directory` on it, and ensure that the files have their
/// associated MIME types correctly deduced.
fn simple_directory() {
	use std::borrow::Borrow;
	use std::env::set_current_dir;
	use std::fs::{canonicalize, File};
	use std::io::Write;

	use tempfile::tempdir;

	use crate::parameters::ScanOpts;

	// set of files to scan. all but the last files have magic numbers corresponding to their extension, except for
	// "wrong.jpg", which is actually a png.
	let files = hashmap![
		"test.jpg" => JPEG_BYTES,
		"test.jpeg" => JPEG_BYTES,
		"test.png" => PNG_BYTES,
		"test.pdf" => PDF_BYTES,
		"test.zip" => ZIP_BYTES,
		"wrong.jpg" => PNG_BYTES,
		"ignore.fake_ext" => ZIP_BYTES,
	];

	let dir = tempdir().expect("Failed to create temporary directory.");
	set_current_dir(dir.path()).expect("Failed to change directory.");

	for (name, bytes) in &files {
		let mut file = File::create(dir.path().join(name)).unwrap_or_else(|_| panic!("Failed to create file: {name}"));

		file
			.write_all(bytes)
			.unwrap_or_else(|_| panic!("Failed to write to file: {name}"));
		drop(file);
	}

	let scan_opts = ScanOpts {
		hidden: true,
		extensionless: false,
		follow_symlinks: false,
		ignore_unknown_exts: true,
	};

	let entries = scan_directory(dir.path(), None, None, &scan_opts).expect("Directory scan failed.");

	// there should be one file missing: "ignore.fake_ext"
	assert_eq!(entries.len(), files.len() - 1);

	let use_threads = cfg!(feature = "multi-threaded");

	let results = scan_from_walkdir(&entries, false, use_threads).0;
	let canonical_results = scan_from_walkdir(&entries, true, use_threads).0;
	assert_eq!(results.len(), canonical_results.len());

	for (result, canonical_result) in results.iter().zip(canonical_results.iter()) {
		// there should be no IO errors during this test. any IO errors encountered are outside the scope of this test.

		// paths should be canonical
		assert_eq!(canonicalize(&result.file).unwrap(), canonical_result.file);

		if !result.valid {
			// the only invalid file detected should be "wrong.jpg", which is a misnamed png file
			// 1. ensure detected extension is "jpg"
			assert_eq!(result.file.as_path().extension().unwrap(), OsStr::new("jpg"));
			// 2. ensure detected MIME type is IMAGE_PNG
			assert_eq!(result.mime, IMAGE_PNG);
			// 3. ensure the recommended extension for "wrong.jpg" is "png"
			assert_eq!(&result.recommended_extension().unwrap(), &String::from("png"));
			// 4. ensure the recommended filename for "wrong.jpg" is "wrong.png"
			assert_eq!(result.recommended_path().unwrap().file_name(), Some(OsStr::new("wrong.png")));
			continue;
		}

		// check if the recommended extension for this file is in the list of known extensions for its MIME type - for
		// example, if the file is determined to be an IMAGE_PNG, its recommended extension should be one of the extensions
		// returned by `mime_extension_lookup(IMAGE_PNG)`.
		assert!(mime_extension_lookup(result.mime.essence_str().into())
			.unwrap()
			.contains(&result.recommended_extension().unwrap()));

		// ensure that the recommended_name function outputs something beginning with "test"
		assert!(result
			.recommended_path()
			.unwrap()
			.file_name()
			.unwrap()
			.to_string_lossy()
			.starts_with("test"));

		// make sure the guessed MIME type is correct based on the extension of the scanned file
		// because we already know that the extensions match the MIME type (as we created these files ourselves earlier in
		// the test), all files with the "jpg" extension should be IMAGE_JPEGs, etc.
		let ext = result.file.as_path().extension().unwrap();
		assert_eq!(
			result.mime,
			match ext.to_string_lossy().borrow() {
				"jpg" | "jpeg" => IMAGE_JPEG,
				"png" => IMAGE_PNG,
				"pdf" => APPLICATION_PDF,
				"zip" => APPLICATION_ZIP.clone(),
				_ => APPLICATION_OCTET_STREAM, // general "fallback" type
			},
			"Incorrect MIME type detected - got {:?} for a {:?} file",
			result.mime,
			ext
		);
	}
}

#[test]
/// Ensure that command line argument parsing works correctly - flags are interpreted, booleans are set, and so on.
fn argument_parsing() {
	use crate::parameters::ScanOpts;

	// pass `-f`, which enables following symlinks, and `-E images`, which scans files with image extensions
	let args: Parameters = Parameters::parse_from(vec!["fif", "-f", "-E", "images"]);

	// check if "jpg" is in the list of extensions to be scanned
	assert!(
		args
			.extensions()
			.expect("args.extensions() should be Some(_)!")
			.contains(&"jpg"),
		"args.extensions() should contain the `images` set!"
	);

	// make sure "scan_hidden" is false
	assert!(!args.scan_hidden);

	// exts should be none
	assert!(args.exts.is_none());

	// there shouldn't be any excluded extensions
	assert!(args.excluded_extensions().is_none());

	// get the ScanOpts, and make sure they match expectations
	assert_eq!(
		args.get_scan_opts(),
		ScanOpts {
			hidden: false,
			extensionless: false,
			follow_symlinks: true,
			ignore_unknown_exts: false,
		},
		"ScanOpts are incorrect"
	);
}

#[test]
/// Ensure that `fif -e jpg dir` is interpreted as "scan for jpg files in dir" and not "scan for jpg and dir files"
fn positional_args() {
	for flag in ["-x", "-e", "-X", "-E"] {
		assert_eq!(
			Parameters::parse_from(vec!["fif", flag, "images", "directory"]).dir,
			PathBuf::from("directory")
		);
	}
}

#[test]
/// Ensure the `exclude` flag (`-x`) overrides `-e` and `-E`.
fn exclude_overrides() {
	// pass `-E images`, which includes many image extensions, and `-x jpg,png`, which should remove "jpg" and "png" from
	// the extensions list
	let args: Parameters = Parameters::parse_from(vec!["fif", "-x", "jpg,png", "-E", "images"]);
	let extensions = args.extensions();
	assert!(extensions.is_some(), "Extensions should contain the `images` set!");
	let extensions = extensions.unwrap();

	assert!(!extensions.contains(&"jpg"), "\"jpg\" should be excluded!");
	assert!(!extensions.contains(&"png"), "\"png\" should be excluded!");
	assert!(extensions.contains(&"jpeg"), "\"jpeg\" should be included!");

	// pass `-e abc,def,ghi,jkl` and `-x abc,def` -- extensions() should only contain "ghi" and "jkl"
	let args: Parameters = Parameters::parse_from(vec!["fif", "-e", "abc,def,ghi,jkl", "-x", "abc,def"]);
	let extensions = args.extensions();
	assert!(extensions.is_some(), "Extensions should be set!");
	assert_eq!(extensions, Some(btreeset!["ghi", "jkl"]));
}

#[test]
/// Ensure the `exclude_set` flag (`-X`) overrides `-e`.
fn exclude_set_overrides_includes() {
	// pass `-e jpg,flac` and `-X images` -- which should produce the equivalent of `-e flag`
	let args: Parameters = Parameters::parse_from(vec!["fif", "-e", "jpg,flac", "-X", "images"]);
	let extensions = args.extensions();
	assert!(extensions.is_some(), "Extensions should be set!");
	assert_eq!(extensions, Some(btreeset!["flac"]));
}

#[test]
/// Ensure the `exclude_set` flag (`-X`) overrides `-E`.
fn exclude_set_overrides_include_set() {
	// pass `-E media` and `-X images` -- which should produce the equivalent of `-E audio,video`
	let args: Parameters = Parameters::parse_from(vec!["fif", "-E", "media", "-X", "images"]);
	let extensions = args.extensions();
	assert!(extensions.is_some(), "Extensions should be set!");
	let extensions = extensions.unwrap();

	// ensure all of audio and video's extensions are here
	for &ext in ExtensionSet::Audio
		.extensions()
		.iter()
		.chain(ExtensionSet::Video.extensions().iter())
	{
		assert!(extensions.contains(&ext), "Extensions should contain {ext}!");
	}

	// ensure all of images' extensions are excluded
	for ext in ExtensionSet::Images.extensions() {
		assert!(!extensions.contains(&ext), "Extensions should not contain {ext}!");
	}
}

#[test]
/// Generate random series of bytes and try to identify them. This test makes no assertions and can only fail if the
/// mime database somehow panics or hangs.
fn identify_random_bytes() {
	use rand::prelude::*;
	let mut rng = rand::rng();
	let mut bytes: [u8; BUF_SIZE * 2] = [0; BUF_SIZE * 2];
	let mut results: BTreeMap<Mime, i32> = BTreeMap::new();

	for _ in 1..1000 {
		rng.fill_bytes(&mut bytes);
		if let Some(detected_type) = MIMEDB.get_type(&bytes) {
			*results.entry(detected_type).or_insert(0) += 1;
		}
	}

	for (mime, count) in &results {
		println!("{mime}:\t{count} counts");
	}
	println!("No type found:\t{} counts", 1000 - results.values().sum::<i32>());
}

#[test]
/// Ensure that, for a given file "wrong.bad", which should have extension "good", the shell output contains something
/// like "mv wrong.bad wrong.good".
fn outputs_move_commands() {
	use std::io::Read;

	// create an example finding stating that "misnamed_file.png" has been identified as a jpeg file
	let findings = vec![Findings {
		file: Path::new("misnamed_file.png").to_path_buf(),
		valid: false,
		mime: IMAGE_JPEG,
	}];

	for format in &["Shell", "PowerShell"] {
		let mut cursor = std::io::Cursor::new(Vec::new());
		let mut contents = std::string::String::new();

		match *format {
			"Shell" => Shell.write_all(&mut cursor, &findings, &[]),
			"PowerShell" => PowerShell.write_all(&mut cursor, &findings, &[]),
			_ => unreachable!(),
		}
		.expect("Failed to write to cursor");

		cursor.set_position(0);
		cursor
			.read_to_string(&mut contents)
			.expect("Failed to read from cursor to string");

		// the output should contain a command like "mv -i misnamed_file.png misnamed_file.jpg"
		assert!(
			contents.contains("misnamed_file.jpg") && contents.contains("misnamed_file.png"),
			"{format} output doesn't contain move command!\n===\n{contents}"
		);
	}
}

#[test]
#[cfg(feature = "json")]
/// Ensure JSON output is valid.
fn test_json() {
	use std::io::Read;

	use crate::formats::Json;
	// create an example finding stating that "misnamed_file.png" has been identified as a jpeg file
	let findings = vec![Findings {
		file: Path::new("misnamed_file.png").to_path_buf(),
		valid: false,
		mime: IMAGE_JPEG,
	}];

	let mut cursor = std::io::Cursor::new(Vec::new());
	let mut contents = std::string::String::new();

	Json
		.write_all(&mut cursor, &findings, &[])
		.expect("Failed to write to cursor");

	cursor.set_position(0);
	cursor
		.read_to_string(&mut contents)
		.expect("Failed to read from cursor to string");

	// the output should contain the file's MIME type
	assert!(
		contents.contains(IMAGE_JPEG.essence_str()),
		"JSON output doesn't contain move command!\n===\n{contents}"
	);
}

#[test]
/// Ensure that the Media extension set contains all (is a superset) of Audio, Video, and Images.
fn media_contains_audio_video_images() {
	use crate::parameters::ExtensionSet::{Audio, Images, Media, Video};
	let media_exts = Media.extensions();

	// assert every extension in the audio/video/image sets is contained in the media set
	[Audio.extensions(), Video.extensions(), Images.extensions()]
		.concat()
		.into_iter()
		.for_each(|ext| assert!(media_exts.contains(&ext)));

	assert_eq!(
		Parameters::parse_from(["fif", "-E", "media"]).extensions(),
		Parameters::parse_from(["fif", "-E", "audio,video,images"]).extensions()
	);
}

#[test]
/// Ensure that the `writables!` and `writablesln!` macros produce the output they should.
fn writables_is_correct() {
	use fif::formats::Writable;
	use fif::{writables, writablesln};

	assert_eq!(
		&["henlo".into(), Path::new("henlo").into(), Writable::Newline,],
		writables!["henlo", (Path::new("henlo")), Newline]
	);

	assert_eq!(
		&["henlo".into(), Path::new("henlo").into(), Writable::Newline, Writable::Newline],
		writablesln!["henlo", (Path::new("henlo")), Newline]
	);
}

#[test]
/// Test various combinations of verbosity flags.
fn verbosity() {
	use log::LevelFilter;
	assert!(
		Parameters::try_parse_from(["fif", "-q", "-v"]).is_err(),
		"Failed to reject usage of both -q and -v!"
	);

	let expected_results = hashmap![
		"-qqqqqqqq" => LevelFilter::Off,
		"-qqq" => LevelFilter::Off,
		"-qq" => LevelFilter::Error,
		"-q" => LevelFilter::Warn,
		"-s" => LevelFilter::Info,
		"-v" => LevelFilter::Debug,
		"-vv" => LevelFilter::Trace,
		"-vvv" => LevelFilter::Trace,
		"-vvvvvvvv" => LevelFilter::Trace,
	];

	for (flags, level) in expected_results {
		assert_eq!(Parameters::parse_from(["fif", flags]).get_verbosity(), level);
	}
}

#[test]
/// Ensures `os_name()`'s output is the same as [`std::env::consts::OS`], capitalisation notwithstanding
fn validate_os_name() {
	assert_eq!(fif::utils::os_name().to_lowercase(), std::env::consts::OS.to_lowercase());
}

#[test]
/// Ensures that [`Findings`] are sorted properly.
fn sort_findings() {
	let findings = [Findings {
			file: Path::new("ccc").to_path_buf(),
			valid: false,
			mime: IMAGE_JPEG,
		},
		Findings {
			file: Path::new("bbb.xyz").to_path_buf(),
			valid: true,
			mime: IMAGE_PNG,
		},
		Findings {
			file: Path::new("aaa").to_path_buf(),
			valid: true,
			mime: APPLICATION_PDF,
		}];
	let mut findings = findings.iter().sorted_unstable();

	assert_eq!(findings.next().unwrap().file, Path::new("aaa"));
	assert_eq!(findings.next().unwrap().file, Path::new("bbb.xyz"));
	assert_eq!(findings.next().unwrap().file, Path::new("ccc"));
	assert_eq!(findings.next(), None);
}