]> git.ipfire.org Git - thirdparty/git.git/blame - t/t7816-grep-binary-pattern.sh
grep: make the behavior for NUL-byte in patterns sane
[thirdparty/git.git] / t / t7816-grep-binary-pattern.sh
CommitLineData
d316af05
ÆAB
1#!/bin/sh
2
3test_description='git grep with a binary pattern files'
4
25754125 5. ./lib-gettext.sh
d316af05 6
25754125 7nul_match_internal () {
d316af05 8 matches=$1
25754125
ÆAB
9 prereqs=$2
10 lc_all=$3
11 extra_flags=$4
12 flags=$5
13 pattern=$6
d316af05
ÆAB
14 pattern_human=$(echo "$pattern" | sed 's/Q/<NUL>/g')
15
16 if test "$matches" = 1
17 then
25754125 18 test_expect_success $prereqs "LC_ALL='$lc_all' git grep $extra_flags -f f $flags '$pattern_human' a" "
d316af05 19 printf '$pattern' | q_to_nul >f &&
25754125 20 LC_ALL='$lc_all' git grep $extra_flags -f f $flags a
d316af05
ÆAB
21 "
22 elif test "$matches" = 0
23 then
25754125
ÆAB
24 test_expect_success $prereqs "LC_ALL='$lc_all' git grep $extra_flags -f f $flags '$pattern_human' a" "
25 >stderr &&
d316af05 26 printf '$pattern' | q_to_nul >f &&
25754125
ÆAB
27 test_must_fail env LC_ALL=\"$lc_all\" git grep $extra_flags -f f $flags a 2>stderr &&
28 test_i18ngrep ! 'This is only supported with -P under PCRE v2' stderr
d316af05 29 "
25754125 30 elif test "$matches" = P
d316af05 31 then
25754125
ÆAB
32 test_expect_success $prereqs "error, PCRE v2 only: LC_ALL='$lc_all' git grep -f f $flags '$pattern_human' a" "
33 >stderr &&
d316af05 34 printf '$pattern' | q_to_nul >f &&
25754125
ÆAB
35 test_must_fail env LC_ALL=\"$lc_all\" git grep -f f $flags a 2>stderr &&
36 test_i18ngrep 'This is only supported with -P under PCRE v2' stderr
d316af05
ÆAB
37 "
38 else
39 test_expect_success "PANIC: Test framework error. Unknown matches value $matches" 'false'
40 fi
41}
42
25754125
ÆAB
43nul_match () {
44 matches=$1
45 matches_pcre2=$2
46 matches_pcre2_locale=$3
47 flags=$4
48 pattern=$5
49 pattern_human=$(echo "$pattern" | sed 's/Q/<NUL>/g')
50
51 nul_match_internal "$matches" "" "C" "" "$flags" "$pattern"
52 nul_match_internal "$matches_pcre2" "LIBPCRE2" "C" "-P" "$flags" "$pattern"
53 nul_match_internal "$matches_pcre2_locale" "LIBPCRE2,GETTEXT_LOCALE" "$is_IS_locale" "-P" "$flags" "$pattern"
54}
55
d316af05
ÆAB
56test_expect_success 'setup' "
57 echo 'binaryQfileQm[*]cQ*æQð' | q_to_nul >a &&
58 git add a &&
59 git commit -m.
60"
61
25754125
ÆAB
62# Simple fixed-string matching that can use kwset (no -i && non-ASCII)
63nul_match 1 1 1 '-F' 'yQf'
64nul_match 0 0 0 '-F' 'yQx'
65nul_match 1 1 1 '-Fi' 'YQf'
66nul_match 0 0 0 '-Fi' 'YQx'
67nul_match 1 1 1 '' 'yQf'
68nul_match 0 0 0 '' 'yQx'
69nul_match 1 1 1 '' 'æQð'
70nul_match 1 1 1 '-F' 'eQm[*]c'
71nul_match 1 1 1 '-Fi' 'EQM[*]C'
d316af05
ÆAB
72
73# Regex patterns that would match but shouldn't with -F
25754125
ÆAB
74nul_match 0 0 0 '-F' 'yQ[f]'
75nul_match 0 0 0 '-F' '[y]Qf'
76nul_match 0 0 0 '-Fi' 'YQ[F]'
77nul_match 0 0 0 '-Fi' '[Y]QF'
78nul_match 0 0 0 '-F' 'æQ[ð]'
79nul_match 0 0 0 '-F' '[æ]Qð'
d316af05 80
25754125
ÆAB
81# The -F kwset codepath can't handle -i && non-ASCII...
82nul_match P 1 1 '-i' '[æ]Qð'
d316af05 83
25754125
ÆAB
84# ...PCRE v2 only matches non-ASCII with -i casefolding under UTF-8
85# semantics
86nul_match P P P '-Fi' 'ÆQ[Ð]'
87nul_match P 0 1 '-i' 'ÆQ[Ð]'
88nul_match P 0 1 '-i' '[Æ]QÐ'
89nul_match P 0 1 '-i' '[Æ]Qð'
90nul_match P 0 1 '-i' 'ÆQÐ'
d316af05 91
25754125
ÆAB
92# \0 in regexes can only work with -P & PCRE v2
93nul_match P 1 1 '' 'yQ[f]'
94nul_match P 1 1 '' '[y]Qf'
95nul_match P 1 1 '-i' 'YQ[F]'
96nul_match P 1 1 '-i' '[Y]Qf'
97nul_match P 1 1 '' 'æQ[ð]'
98nul_match P 1 1 '' '[æ]Qð'
99nul_match P 0 1 '-i' 'ÆQ[Ð]'
100nul_match P 1 1 '' 'eQm.*cQ'
101nul_match P 1 1 '-i' 'EQM.*cQ'
102nul_match P 0 0 '' 'eQm[*]c'
103nul_match P 0 0 '-i' 'EQM[*]C'
d316af05 104
25754125
ÆAB
105# Assert that we're using REG_STARTEND and the pattern doesn't match
106# just because it's cut off at the first \0.
107nul_match 0 0 0 '-i' 'NOMATCHQð'
108nul_match P 0 0 '-i' '[Æ]QNOMATCH'
109nul_match P 0 0 '-i' '[æ]QNOMATCH'
d316af05
ÆAB
110
111# Ensure that the matcher doesn't regress to something that stops at
112# \0
25754125
ÆAB
113nul_match 0 0 0 '-F' 'yQ[f]'
114nul_match 0 0 0 '-Fi' 'YQ[F]'
115nul_match 0 0 0 '' 'yQNOMATCH'
116nul_match 0 0 0 '' 'QNOMATCH'
117nul_match 0 0 0 '-i' 'YQNOMATCH'
118nul_match 0 0 0 '-i' 'QNOMATCH'
119nul_match 0 0 0 '-F' 'æQ[ð]'
120nul_match P P P '-Fi' 'ÆQ[Ð]'
121nul_match P 0 1 '-i' 'ÆQ[Ð]'
122nul_match 0 0 0 '' 'yQNÓMATCH'
123nul_match 0 0 0 '' 'QNÓMATCH'
124nul_match 0 0 0 '-i' 'YQNÓMATCH'
125nul_match 0 0 0 '-i' 'QNÓMATCH'
d316af05
ÆAB
126
127test_done