1 1.15 martin # $NetBSD: t_sed.sh,v 1.15 2025/06/03 19:03:23 martin Exp $ 2 1.1 jruoho # 3 1.1 jruoho # Copyright (c) 2012 The NetBSD Foundation, Inc. 4 1.1 jruoho # All rights reserved. 5 1.1 jruoho # 6 1.1 jruoho # This code is derived from software contributed to The NetBSD Foundation 7 1.2 dholland # by Jukka Ruohonen and David A. Holland. 8 1.1 jruoho # 9 1.1 jruoho # Redistribution and use in source and binary forms, with or without 10 1.1 jruoho # modification, are permitted provided that the following conditions 11 1.1 jruoho # are met: 12 1.1 jruoho # 1. Redistributions of source code must retain the above copyright 13 1.1 jruoho # notice, this list of conditions and the following disclaimer. 14 1.1 jruoho # 2. Redistributions in binary form must reproduce the above copyright 15 1.1 jruoho # notice, this list of conditions and the following disclaimer in the 16 1.1 jruoho # documentation and/or other materials provided with the distribution. 17 1.1 jruoho # 18 1.1 jruoho # THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 19 1.1 jruoho # ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 20 1.1 jruoho # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 21 1.1 jruoho # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 22 1.1 jruoho # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23 1.1 jruoho # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24 1.1 jruoho # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25 1.1 jruoho # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26 1.1 jruoho # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27 1.1 jruoho # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28 1.1 jruoho # POSSIBILITY OF SUCH DAMAGE. 29 1.1 jruoho # 30 1.1 jruoho 31 1.4 jruoho atf_test_case c2048 32 1.4 jruoho c2048_head() { 33 1.4 jruoho atf_set "descr" "Test that sed(1) does not fail when the " \ 34 1.10 gutterid "2048th character is a backslash (PR bin/25899)" 35 1.4 jruoho } 36 1.4 jruoho 37 1.4 jruoho c2048_body() { 38 1.4 jruoho 39 1.5 jmmv atf_check -s exit:0 -o inline:'foo\n' -e empty \ 40 1.5 jmmv -x "echo foo | sed -f $(atf_get_srcdir)/d_c2048.in" 41 1.4 jruoho } 42 1.4 jruoho 43 1.1 jruoho atf_test_case emptybackref 44 1.1 jruoho emptybackref_head() { 45 1.8 christos atf_set "descr" "Test that sed(1) handles empty back references" 46 1.1 jruoho } 47 1.1 jruoho 48 1.1 jruoho emptybackref_body() { 49 1.1 jruoho 50 1.1 jruoho atf_check -o inline:"foo1bar1\n" \ 51 1.1 jruoho -x "echo foo1bar1 | sed -ne '/foo\(.*\)bar\1/p'" 52 1.1 jruoho 53 1.1 jruoho atf_check -o inline:"foobar\n" \ 54 1.1 jruoho -x "echo foobar | sed -ne '/foo\(.*\)bar\1/p'" 55 1.1 jruoho } 56 1.1 jruoho 57 1.3 jruoho atf_test_case longlines 58 1.3 jruoho longlines_head() { 59 1.3 jruoho atf_set "descr" "Test that sed(1) handles " \ 60 1.3 jruoho "long lines correctly (PR bin/42261)" 61 1.3 jruoho } 62 1.3 jruoho 63 1.3 jruoho longlines_body() { 64 1.3 jruoho 65 1.3 jruoho str=$(awk 'BEGIN {while(x<2043){printf "x";x++}}') 66 1.3 jruoho echo $str > input 67 1.3 jruoho 68 1.3 jruoho atf_check -o save:output -x "echo x | sed s,x,${str},g" 69 1.3 jruoho atf_check -s exit:0 -o empty -e empty -x "diff input output" 70 1.3 jruoho } 71 1.3 jruoho 72 1.2 dholland atf_test_case rangeselection 73 1.2 dholland rangeselection_head() { 74 1.2 dholland atf_set "descr" "Test that sed(1) handles " \ 75 1.2 dholland "range selection correctly" 76 1.2 dholland } 77 1.2 dholland 78 1.2 dholland rangeselection_body() { 79 1.2 dholland # basic cases 80 1.2 dholland atf_check -o inline:"D\n" \ 81 1.2 dholland -x "printf 'A\nB\nC\nD\n' | sed '1,3d'" 82 1.2 dholland atf_check -o inline:"A\n" \ 83 1.2 dholland -x "printf 'A\nB\nC\nD\n' | sed '2,4d'" 84 1.10 gutterid # two non-overlapping ranges 85 1.2 dholland atf_check -o inline:"C\n" \ 86 1.2 dholland -x "printf 'A\nB\nC\nD\nE\n' | sed '1,2d;4,5d'" 87 1.2 dholland # overlapping ranges; the first prevents the second from being entered 88 1.2 dholland atf_check -o inline:"D\nE\n" \ 89 1.2 dholland -x "printf 'A\nB\nC\nD\nE\n' | sed '1,3d;3,5d'" 90 1.2 dholland # the 'n' command can also prevent ranges from being entered 91 1.2 dholland atf_check -o inline:"B\nB\nC\nD\n" \ 92 1.2 dholland -x "printf 'A\nB\nC\nD\n' | sed '1,3s/A/B/;1,3n;1,3s/B/C/'" 93 1.2 dholland atf_check -o inline:"B\nC\nC\nD\n" \ 94 1.2 dholland -x "printf 'A\nB\nC\nD\n' | sed '1,3s/A/B/;1,3n;2,3s/B/C/'" 95 1.2 dholland 96 1.2 dholland # basic cases using regexps 97 1.2 dholland atf_check -o inline:"D\n" \ 98 1.2 dholland -x "printf 'A\nB\nC\nD\n' | sed '/A/,/C/d'" 99 1.2 dholland atf_check -o inline:"A\n" \ 100 1.2 dholland -x "printf 'A\nB\nC\nD\n' | sed '/B/,/D/d'" 101 1.10 gutterid # two non-overlapping ranges 102 1.2 dholland atf_check -o inline:"C\n" \ 103 1.2 dholland -x "printf 'A\nB\nC\nD\nE\n' | sed '/A/,/B/d;/D/,/E/d'" 104 1.2 dholland # two overlapping ranges; the first blocks the second as above 105 1.2 dholland atf_check -o inline:"D\nE\n" \ 106 1.2 dholland -x "printf 'A\nB\nC\nD\nE\n' | sed '/A/,/C/d;/C/,/E/d'" 107 1.2 dholland # the 'n' command makes some lines invisible to downstreap regexps 108 1.2 dholland atf_check -o inline:"B\nC\nC\nD\n" \ 109 1.2 dholland -x "printf 'A\nB\nC\nD\n' | sed '/A/,/C/s/A/B/;1,3n;/B/,/C/s/B/C/'" 110 1.2 dholland 111 1.2 dholland # a range ends at the *first* matching end line 112 1.2 dholland atf_check -o inline:"D\nC\n" \ 113 1.2 dholland -x "printf 'A\nB\nC\nD\nC\n' | sed '/A/,/C/d'" 114 1.2 dholland # another matching start line within the range has no effect 115 1.2 dholland atf_check -o inline:"D\nC\n" \ 116 1.2 dholland -x "printf 'A\nB\nA\nC\nD\nC\n' | sed '/A/,/C/d'" 117 1.2 dholland } 118 1.2 dholland 119 1.6 christos atf_test_case preserve_leading_ws_ia 120 1.6 christos preserve_leading_ws_ia_head() { 121 1.6 christos atf_set "descr" "Test that sed(1) preserves leading whitespace " \ 122 1.6 christos "in insert and append (PR bin/49872)" 123 1.6 christos } 124 1.6 christos 125 1.6 christos preserve_leading_ws_ia_body() { 126 1.6 christos atf_check -o inline:" 1 2 3\n4 5 6\n 7 8 9\n\n" \ 127 1.6 christos -x 'echo | sed -e "/^$/i\\ 128 1.6 christos 1 2 3\\ 129 1.6 christos 4 5 6\\ 130 1.6 christos 7 8 9"' 131 1.6 christos } 132 1.6 christos 133 1.7 christos atf_test_case escapes_in_subst 134 1.7 christos escapes_in_subst_head() { 135 1.7 christos atf_set "descr" "Test that sed(1) expands \x \d \o escapes " \ 136 1.9 andvar "in substitution strings" 137 1.7 christos } 138 1.7 christos 139 1.7 christos escapes_in_subst_body() { 140 1.15 martin # basic tests 141 1.7 christos atf_check -o inline:"fooXbar\n" \ 142 1.7 christos -x 'echo "foo bar" | sed -e "s/ /\x58/"' 143 1.7 christos atf_check -o inline:"fooXbar\n" \ 144 1.7 christos -x 'echo "foo bar" | sed -e "s/ /\o130/"' 145 1.7 christos atf_check -o inline:"fooXbar\n" \ 146 1.7 christos -x 'echo "foo bar" | sed -e "s/ /\d88/"' 147 1.13 gutterid 148 1.15 martin # overflowing the escaped char value 149 1.15 martin # atf_expect_fail "PR bin/59453: sed misparses \[dox]number escapes" 150 1.13 gutterid atf_check -o inline:"#8ball\n" \ 151 1.14 bad -x "echo | sed -e 's/^/\d358ball/'" 152 1.13 gutterid 153 1.13 gutterid atf_check -o inline:"#3duh\n" \ 154 1.14 bad -x "echo | sed -e 's/^/\o0433duh/'" 155 1.13 gutterid 156 1.13 gutterid atf_check -o inline:"#duh\n" \ 157 1.14 bad -x "echo | sed -e 's/^/\x23duh/'" 158 1.15 martin 159 1.15 martin # check that escapes end after 2 or 3 chars 160 1.15 martin atf_check -o inline:"00000000 09 38 62 61 6c 6c 0a |.8ball.|\n" \ 161 1.15 martin -x "echo | sed -e 's/^/\d0098ball/' | hexdump -C | head -1" 162 1.15 martin 163 1.15 martin atf_check -o inline:"00000000 07 37 62 61 6c 6c 0a |.7ball.|\n" \ 164 1.15 martin -x "echo | sed -e 's/^/\o0077ball/' | hexdump -C | head -1" 165 1.15 martin 166 1.15 martin atf_check -o inline:"00000000 01 38 62 61 6c 6c 0a |.8ball.|\n" \ 167 1.15 martin -x "echo | sed -e 's/^/\x018ball/' | hexdump -C | head -1" 168 1.13 gutterid } 169 1.13 gutterid 170 1.7 christos atf_test_case escapes_in_re 171 1.7 christos escapes_in_re_head() { 172 1.7 christos atf_set "descr" "Test that sed(1) expands \x \d \o escapes " \ 173 1.7 christos "in regex strings" 174 1.7 christos } 175 1.7 christos 176 1.7 christos escapes_in_re_body() { 177 1.7 christos atf_check -o inline:"foo bar\n" \ 178 1.7 christos -x 'echo "fooXbar" | sed -e "s/\x58/ /"' 179 1.7 christos atf_check -o inline:"foo bar\n" \ 180 1.7 christos -x 'echo "fooXbar" | sed -e "s/\o130/ /"' 181 1.7 christos atf_check -o inline:"foo bar\n" \ 182 1.7 christos -x 'echo "fooXbar" | sed -e "s/\d88/ /"' 183 1.7 christos } 184 1.7 christos 185 1.7 christos atf_test_case escapes_in_re_bracket 186 1.7 christos escapes_in_re_bracket_head() { 187 1.7 christos atf_set "descr" "Test that sed(1) does not expand \x \d \o escapes " \ 188 1.7 christos "in regex strings inside braces" 189 1.7 christos } 190 1.7 christos 191 1.7 christos escapes_in_re_bracket_body() { 192 1.7 christos atf_check -o inline:"foo bar\n" \ 193 1.7 christos -x 'echo "foo\\x58bar" | sed -e "s/[\x58]/ /g"' 194 1.7 christos atf_check -o inline:"f bar\n" \ 195 1.7 christos -x 'echo "fooo\\130bar" | sed -e "s/[\o130]/ /g"' 196 1.7 christos atf_check -o inline:"foo bar\n" \ 197 1.7 christos -x 'echo "foo\\d88bar" | sed -e "s/[\d88]/ /g"' 198 1.7 christos } 199 1.10 gutterid 200 1.11 gutterid atf_test_case relative_addressing 201 1.11 gutterid relative_addressing_head() { 202 1.11 gutterid atf_set "descr" "Test that sed(1) handles relative addressing " \ 203 1.11 gutterid "properly (PR bin/49109)" 204 1.11 gutterid } 205 1.11 gutterid 206 1.11 gutterid relative_addressing_body() { 207 1.11 gutterid atf_check -o match:"3" -x 'seq 1 4 | sed -n "1,+2p" | wc -l' 208 1.11 gutterid } 209 1.11 gutterid 210 1.1 jruoho atf_init_test_cases() { 211 1.4 jruoho atf_add_test_case c2048 212 1.1 jruoho atf_add_test_case emptybackref 213 1.3 jruoho atf_add_test_case longlines 214 1.2 dholland atf_add_test_case rangeselection 215 1.6 christos atf_add_test_case preserve_leading_ws_ia 216 1.7 christos atf_add_test_case escapes_in_subst 217 1.7 christos atf_add_test_case escapes_in_re 218 1.7 christos atf_add_test_case escapes_in_re_bracket 219 1.11 gutterid atf_add_test_case relative_addressing 220 1.1 jruoho } 221