binary.rs
This file
Path
tests/binary.rs
468 lines · 17.3 KiB · Rust
Last changed by
cli: document that `-c/--count` can be inconsistent with `-l/--files-with-matches`Andrew Gallantbefore adoption1b07c66
History of this fileTake it
1use crate::util::{Dir, TestCommand};23// This file contains a smattering of tests specifically for checking ripgrep's4// handling of binary files. There's quite a bit of discussion on this in this5// bug report: https://github.com/BurntSushi/ripgrep/issues/30667// Our haystack is the first 2,133 lines of Gutenberg's copy of "A Study in8// Scarlet," with a NUL byte at line 1870: `abcdef\x00`.9//10// The position and size of the haystack is, unfortunately, significant. In11// particular, the NUL byte is specifically inserted at some point *after* the12// first 65,536 bytes, which corresponds to the initial capacity of the buffer13// that ripgrep uses to read files. (grep for DEFAULT_BUFFER_CAPACITY.) The14// position of the NUL byte ensures that we can execute some search on the15// initial buffer contents without ever detecting any binary data. Moreover,16// when using a memory map for searching, only the first 65,536 bytes are17// scanned for a NUL byte, so no binary bytes are detected at all when using18// a memory map (unless our query matches line 1898).19//20// One last note: in the tests below, we use --no-mmap heavily because binary21// detection with memory maps is a bit different. Namely, NUL bytes are only22// searched for in the first few KB of the file and in a match. Normally, NUL23// bytes are searched for everywhere.24const HAY: &'static [u8] = include_bytes!("./data/sherlock-nul.txt");2526// Tests for binary file detection when using memory maps.27// As noted in the original comments, with memory maps binary detection28// works differently - NUL bytes are only searched for in the first few KB29// of the file and in matches.30//31// Note that we don't run these on macOS, which has memory maps forcefully32// disabled because they suck so much.3334// Test that matches in a binary file with memory maps work as expected35// with implicit file search (via glob pattern).36#[cfg(not(target_os = "macos"))]37rgtest!(mmap_match_implicit, |dir: Dir, mut cmd: TestCommand| {38 dir.create_bytes("hay", HAY);39 cmd.args(&["--mmap", "-n", "Project Gutenberg EBook", "-g", "hay"]);4041 // With mmap, we get a match and a warning about binary content42 let expected = "\43hay:1:The Project Gutenberg EBook of A Study In Scarlet, by Arthur Conan Doyle44";45 eqnice!(expected, cmd.stdout());46});4748// Test with an explicit file argument when using memory maps.49#[cfg(not(target_os = "macos"))]50rgtest!(mmap_match_explicit, |dir: Dir, mut cmd: TestCommand| {51 dir.create_bytes("hay", HAY);52 cmd.args(&["--mmap", "-n", "Project Gutenberg EBook", "hay"]);5354 let expected = "\551:The Project Gutenberg EBook of A Study In Scarlet, by Arthur Conan Doyle56";57 eqnice!(expected, cmd.stdout());58});5960// Test specifically with a pattern that matches near the NUL byte which should61// trigger binary detection with memory maps.62#[cfg(not(target_os = "macos"))]63rgtest!(mmap_match_near_nul, |dir: Dir, mut cmd: TestCommand| {64 dir.create_bytes("hay", HAY);65 // Pattern that matches around line 1898 where the NUL byte is.66 // Note: Using direct file path instead of glob.67 cmd.args(&["--mmap", "-n", "abcdef", "hay"]);6869 let expected = "\70binary file matches (found \"\\0\" byte around offset 77041)71";72 eqnice!(expected, cmd.stdout());73});7475// Test with --count option to ensure full file scanning works with mmap.76#[cfg(not(target_os = "macos"))]77rgtest!(mmap_match_count, |dir: Dir, mut cmd: TestCommand| {78 dir.create_bytes("hay", HAY);79 cmd.args(&["--mmap", "-c", "Project Gutenberg EBook|Heaven", "hay"]);8081 // With mmap, since we're counting all matches and might not82 // encounter the NUL byte during initial detection, the count83 // should still be reported.84 eqnice!("2\n", cmd.stdout());85});8687// Test binary detection with mmap when pattern would match before and after NUL88// byte.89#[cfg(not(target_os = "macos"))]90rgtest!(mmap_match_multiple, |dir: Dir, mut cmd: TestCommand| {91 dir.create_bytes("hay", HAY);92 // Use explicit file path.93 cmd.args(&["--mmap", "-n", "Project Gutenberg EBook|Heaven", "hay"]);9495 // With explicit file and memory maps, matches before and after NUL byte96 // are shown.97 let expected = "\981:The Project Gutenberg EBook of A Study In Scarlet, by Arthur Conan Doyle991871:\"No. Heaven knows what the objects of his studies are. But here we100";101 eqnice!(expected, cmd.stdout());102});103104// Test that --binary flag can have odd results when searching with a memory105// map.106#[cfg(not(target_os = "macos"))]107rgtest!(mmap_binary_flag, |dir: Dir, mut cmd: TestCommand| {108 dir.create_bytes("hay", HAY);109 // Use glob pattern.110 cmd.args(&["--mmap", "-n", "--binary", "Heaven", "-g", "hay"]);111112 let expected = "\113hay:1871:\"No. Heaven knows what the objects of his studies are. But here we114";115 eqnice!(expected, cmd.stdout());116});117118// Test that using -a/--text flag works as expected with mmap.119#[cfg(not(target_os = "macos"))]120rgtest!(mmap_text_flag, |dir: Dir, mut cmd: TestCommand| {121 dir.create_bytes("hay", HAY);122 cmd.args(&["--mmap", "-n", "--text", "Heaven", "-g", "hay"]);123124 // With --text flag, binary detection should be disabled.125 let expected = "\126hay:1871:\"No. Heaven knows what the objects of his studies are. But here we127";128 eqnice!(expected, cmd.stdout());129});130131// Test pattern that matches before and after the NUL byte with memory maps.132#[cfg(not(target_os = "macos"))]133rgtest!(mmap_after_nul_match, |dir: Dir, mut cmd: TestCommand| {134 dir.create_bytes("hay", HAY);135 // Use explicit file path.136 cmd.args(&["--mmap", "-n", "medical student", "hay"]);137138 // With explicit file and memory maps, all matches are shown139 let expected = "\140176:\"A medical student, I suppose?\" said I.141409:\"A medical student, I suppose?\" said I.142642:\"A medical student, I suppose?\" said I.143875:\"A medical student, I suppose?\" said I.1441108:\"A medical student, I suppose?\" said I.1451341:\"A medical student, I suppose?\" said I.1461574:\"A medical student, I suppose?\" said I.1471807:\"A medical student, I suppose?\" said I.1481867:\"And yet you say he is not a medical student?\"149";150 eqnice!(expected, cmd.stdout());151});152153// This tests that ripgrep prints a warning message if it finds and prints a154// match in a binary file before detecting that it is a binary file. The point155// here is to notify that user that the search of the file is only partially156// complete.157//158// This applies to files that are *implicitly* searched via a recursive159// directory traversal. In particular, this results in a WARNING message being160// printed. We make our file "implicit" by doing a recursive search with a glob161// that matches our file.162rgtest!(after_match1_implicit, |dir: Dir, mut cmd: TestCommand| {163 dir.create_bytes("hay", HAY);164 cmd.args(&["--no-mmap", "-n", "Project Gutenberg EBook", "-g", "hay"]);165166 let expected = "\167hay:1:The Project Gutenberg EBook of A Study In Scarlet, by Arthur Conan Doyle168hay: WARNING: stopped searching binary file after match (found \"\\0\" byte around offset 77041)169";170 eqnice!(expected, cmd.stdout());171});172173// Like after_match1_implicit, except we provide a file to search174// explicitly. This results in identical behavior, but a different message.175rgtest!(after_match1_explicit, |dir: Dir, mut cmd: TestCommand| {176 dir.create_bytes("hay", HAY);177 cmd.args(&["--no-mmap", "-n", "Project Gutenberg EBook", "hay"]);178179 let expected = "\1801:The Project Gutenberg EBook of A Study In Scarlet, by Arthur Conan Doyle181binary file matches (found \"\\0\" byte around offset 77041)182";183 eqnice!(expected, cmd.stdout());184});185186// Like after_match1_explicit, except we feed our content on stdin.187rgtest!(after_match1_stdin, |_: Dir, mut cmd: TestCommand| {188 cmd.args(&["--no-mmap", "-n", "Project Gutenberg EBook"]);189190 let expected = "\1911:The Project Gutenberg EBook of A Study In Scarlet, by Arthur Conan Doyle192binary file matches (found \"\\0\" byte around offset 77041)193";194 eqnice!(expected, cmd.pipe(HAY));195});196197// Like after_match1_implicit, but provides the --binary flag, which198// disables binary filtering. Thus, this matches the behavior of ripgrep as199// if the file were given explicitly.200rgtest!(after_match1_implicit_binary, |dir: Dir, mut cmd: TestCommand| {201 dir.create_bytes("hay", HAY);202 cmd.args(&[203 "--no-mmap",204 "-n",205 "--binary",206 "Project Gutenberg EBook",207 "-g",208 "hay",209 ]);210211 let expected = "\212hay:1:The Project Gutenberg EBook of A Study In Scarlet, by Arthur Conan Doyle213hay: binary file matches (found \"\\0\" byte around offset 77041)214";215 eqnice!(expected, cmd.stdout());216});217218// Like after_match1_implicit, but enables -a/--text, so no binary219// detection should be performed.220rgtest!(after_match1_implicit_text, |dir: Dir, mut cmd: TestCommand| {221 dir.create_bytes("hay", HAY);222 cmd.args(&[223 "--no-mmap",224 "-n",225 "--text",226 "Project Gutenberg EBook",227 "-g",228 "hay",229 ]);230231 let expected = "\232hay:1:The Project Gutenberg EBook of A Study In Scarlet, by Arthur Conan Doyle233";234 eqnice!(expected, cmd.stdout());235});236237// Like after_match1_implicit_text, but enables -a/--text, so no binary238// detection should be performed.239rgtest!(after_match1_explicit_text, |dir: Dir, mut cmd: TestCommand| {240 dir.create_bytes("hay", HAY);241 cmd.args(&["--no-mmap", "-n", "--text", "Project Gutenberg EBook", "hay"]);242243 let expected = "\2441:The Project Gutenberg EBook of A Study In Scarlet, by Arthur Conan Doyle245";246 eqnice!(expected, cmd.stdout());247});248249// Like after_match1_implicit, except this asks ripgrep to print all matching250// files.251//252// This is an interesting corner case that one might consider a bug, however,253// it's unlikely to be fixed. Namely, ripgrep probably shouldn't print `hay`254// as a matching file since it is in fact a binary file, and thus should be255// filtered out by default. However, the --files-with-matches flag will print256// out the path of a matching file as soon as a match is seen and then stop257// searching completely. Therefore, the NUL byte is never actually detected.258//259// The only way to fix this would be to kill ripgrep's performance in this case260// and continue searching the entire file for a NUL byte. (Similarly if the261// --quiet flag is set. See the next test.)262rgtest!(after_match1_implicit_path, |dir: Dir, mut cmd: TestCommand| {263 dir.create_bytes("hay", HAY);264 cmd.args(&["--no-mmap", "-l", "Project Gutenberg EBook", "-g", "hay"]);265 eqnice!("hay\n", cmd.stdout());266});267268// Like after_match1_implicit_path, except this indicates that a match was269// found with no other output. (This is the same bug described above, but270// manifest as an exit code with no output.)271rgtest!(after_match1_implicit_quiet, |dir: Dir, mut cmd: TestCommand| {272 dir.create_bytes("hay", HAY);273 cmd.args(&["--no-mmap", "-q", "Project Gutenberg EBook", "-g", "hay"]);274 eqnice!("", cmd.stdout());275});276277// This sets up the same test as after_match1_implicit_path, but instead of278// just printing the matching files, this includes the full count of matches.279// In this case, we need to search the entire file, so ripgrep correctly280// detects the binary data and suppresses output.281rgtest!(after_match1_implicit_count, |dir: Dir, mut cmd: TestCommand| {282 dir.create_bytes("hay", HAY);283 cmd.args(&["--no-mmap", "-c", "Project Gutenberg EBook", "-g", "hay"]);284 cmd.assert_err();285});286287// Like after_match1_implicit_count, except the --binary flag is provided,288// which makes ripgrep disable binary data filtering even for implicit files.289rgtest!(290 after_match1_implicit_count_binary,291 |dir: Dir, mut cmd: TestCommand| {292 dir.create_bytes("hay", HAY);293 cmd.args(&[294 "--no-mmap",295 "-c",296 "--binary",297 "Project Gutenberg EBook",298 "-g",299 "hay",300 ]);301 eqnice!("hay:1\n", cmd.stdout());302 }303);304305// Like after_match1_implicit_count, except the file path is provided306// explicitly, so binary filtering is disabled and a count is correctly307// reported.308rgtest!(after_match1_explicit_count, |dir: Dir, mut cmd: TestCommand| {309 dir.create_bytes("hay", HAY);310 cmd.args(&["--no-mmap", "-c", "Project Gutenberg EBook", "hay"]);311 eqnice!("1\n", cmd.stdout());312});313314// This tests that a match way before the NUL byte is shown, but a match after315// the NUL byte is not.316rgtest!(after_match2_implicit, |dir: Dir, mut cmd: TestCommand| {317 dir.create_bytes("hay", HAY);318 cmd.args(&[319 "--no-mmap",320 "-n",321 "Project Gutenberg EBook|a medical student",322 "-g",323 "hay",324 ]);325326 let expected = "\327hay:1:The Project Gutenberg EBook of A Study In Scarlet, by Arthur Conan Doyle328hay: WARNING: stopped searching binary file after match (found \"\\0\" byte around offset 77041)329";330 eqnice!(expected, cmd.stdout());331});332333// Like after_match2_implicit, but enables -a/--text, so no binary334// detection should be performed.335rgtest!(after_match2_implicit_text, |dir: Dir, mut cmd: TestCommand| {336 dir.create_bytes("hay", HAY);337 cmd.args(&[338 "--no-mmap",339 "-n",340 "--text",341 "Project Gutenberg EBook|a medical student",342 "-g",343 "hay",344 ]);345346 let expected = "\347hay:1:The Project Gutenberg EBook of A Study In Scarlet, by Arthur Conan Doyle348hay:1867:\"And yet you say he is not a medical student?\"349";350 eqnice!(expected, cmd.stdout());351});352353// This tests that ripgrep *silently* quits before finding a match that occurs354// after a NUL byte.355rgtest!(before_match1_implicit, |dir: Dir, mut cmd: TestCommand| {356 dir.create_bytes("hay", HAY);357 cmd.args(&["--no-mmap", "-n", "Heaven", "-g", "hay"]);358 cmd.assert_err();359});360361// This tests that ripgrep *does not* silently quit before finding a match that362// occurs after a NUL byte when a file is explicitly searched.363rgtest!(before_match1_explicit, |dir: Dir, mut cmd: TestCommand| {364 dir.create_bytes("hay", HAY);365 cmd.args(&["--no-mmap", "-n", "Heaven", "hay"]);366367 let expected = "\368binary file matches (found \"\\0\" byte around offset 77041)369";370 eqnice!(expected, cmd.stdout());371});372373// Like before_match1_implicit, but enables the --binary flag, which374// disables binary filtering. Thus, this matches the behavior of ripgrep as if375// the file were given explicitly.376rgtest!(before_match1_implicit_binary, |dir: Dir, mut cmd: TestCommand| {377 dir.create_bytes("hay", HAY);378 cmd.args(&["--no-mmap", "-n", "--binary", "Heaven", "-g", "hay"]);379380 let expected = "\381hay: binary file matches (found \"\\0\" byte around offset 77041)382";383 eqnice!(expected, cmd.stdout());384});385386// Like before_match1_implicit, but enables -a/--text, so no binary387// detection should be performed.388rgtest!(before_match1_implicit_text, |dir: Dir, mut cmd: TestCommand| {389 dir.create_bytes("hay", HAY);390 cmd.args(&["--no-mmap", "-n", "--text", "Heaven", "-g", "hay"]);391392 let expected = "\393hay:1871:\"No. Heaven knows what the objects of his studies are. But here we394";395 eqnice!(expected, cmd.stdout());396});397398// This tests that ripgrep *silently* quits before finding a match that occurs399// before a NUL byte, but within the same buffer as the NUL byte.400rgtest!(before_match2_implicit, |dir: Dir, mut cmd: TestCommand| {401 dir.create_bytes("hay", HAY);402 cmd.args(&["--no-mmap", "-n", "a medical student", "-g", "hay"]);403 cmd.assert_err();404});405406// This tests that ripgrep *does not* silently quit before finding a match that407// occurs before a NUL byte, but within the same buffer as the NUL byte. Even408// though the match occurs before the NUL byte, ripgrep still doesn't print it409// because it has already scanned ahead to detect the NUL byte. (This matches410// the behavior of GNU grep.)411rgtest!(before_match2_explicit, |dir: Dir, mut cmd: TestCommand| {412 dir.create_bytes("hay", HAY);413 cmd.args(&["--no-mmap", "-n", "a medical student", "hay"]);414415 let expected = "\416binary file matches (found \"\\0\" byte around offset 77041)417";418 eqnice!(expected, cmd.stdout());419});420421// Like before_match1_implicit, but enables -a/--text, so no binary422// detection should be performed.423rgtest!(before_match2_implicit_text, |dir: Dir, mut cmd: TestCommand| {424 dir.create_bytes("hay", HAY);425 cmd.args(&["--no-mmap", "-n", "--text", "a medical student", "-g", "hay"]);426427 let expected = "\428hay:1867:\"And yet you say he is not a medical student?\"429";430 eqnice!(expected, cmd.stdout());431});432433// See: https://github.com/BurntSushi/ripgrep/issues/3131434rgtest!(435 matching_files_inconsistent_with_count,436 |dir: Dir, _cmd: TestCommand| {437 let mut file1 = String::new();438 file1.push_str("cat here\n");439 for _ in 0..150_000 {440 file1.push_str("padding line\n");441 }442 file1.push_str("\x00");443444 dir.create("file1.txt", &file1);445 dir.create("file2.txt", "cat here");446447 let got = dir.command().args(&["--sort=path", "-l", "cat"]).stdout();448 eqnice!("file1.txt\nfile2.txt\n", got);449450 // This is the inconsistent result that can't really be avoided without451 // either making `-l/--files-with-matches` much slower or changing452 // what "binary filtering" means.453 let got = dir.command().args(&["--sort=path", "-c", "cat"]).stdout();454 eqnice!("file2.txt:1\n", got);455456 let got = dir457 .command()458 .args(&["--sort=path", "-c", "cat", "--binary"])459 .stdout();460 eqnice!("file1.txt:1\nfile2.txt:1\n", got);461462 let got = dir463 .command()464 .args(&["--sort=path", "-c", "cat", "--text"])465 .stdout();466 eqnice!("file1.txt:1\nfile2.txt:1\n", got);467 }468);