t_patch.sh revision 1.2 1 1.2 nia # $NetBSD: t_patch.sh,v 1.2 2021/02/20 08:59:24 nia Exp $
2 1.1 jruoho #
3 1.2 nia # Copyright (c) 2020, 2021 The NetBSD Foundation, Inc.
4 1.1 jruoho # All rights reserved.
5 1.1 jruoho #
6 1.1 jruoho # Redistribution and use in source and binary forms, with or without
7 1.1 jruoho # modification, are permitted provided that the following conditions
8 1.1 jruoho # are met:
9 1.1 jruoho # 1. Redistributions of source code must retain the above copyright
10 1.1 jruoho # notice, this list of conditions and the following disclaimer.
11 1.1 jruoho # 2. Redistributions in binary form must reproduce the above copyright
12 1.1 jruoho # notice, this list of conditions and the following disclaimer in the
13 1.1 jruoho # documentation and/or other materials provided with the distribution.
14 1.1 jruoho #
15 1.1 jruoho # THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
16 1.1 jruoho # ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17 1.1 jruoho # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18 1.1 jruoho # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
19 1.1 jruoho # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20 1.1 jruoho # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21 1.1 jruoho # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 1.1 jruoho # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 1.1 jruoho # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24 1.1 jruoho # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 1.1 jruoho # POSSIBILITY OF SUCH DAMAGE.
26 1.1 jruoho #
27 1.1 jruoho
28 1.1 jruoho patch_lines() {
29 1.1 jruoho
30 1.1 jruoho printf "%$1s" | tr " " "a" > longlines 2>/dev/null
31 1.1 jruoho
32 1.1 jruoho cat << EOF > longlines.patch
33 1.1 jruoho --- ./longlines.orig 2019-10-16 09:25:30.667656644 +0000
34 1.1 jruoho +++ ./longlines
35 1.1 jruoho @@ -1 +1 @@
36 1.1 jruoho EOF
37 1.1 jruoho printf -- "-%$1s\n" | tr " " "a" >> longlines.patch 2>/dev/null
38 1.1 jruoho printf -- "+%$1s" | tr " " "b" >> longlines.patch 2>/dev/null
39 1.1 jruoho
40 1.1 jruoho patch longlines < longlines.patch
41 1.1 jruoho
42 1.1 jruoho if [ ! $? -eq 0 ]; then
43 1.1 jruoho atf_fail "Failed to patch long lines"
44 1.1 jruoho fi
45 1.1 jruoho }
46 1.1 jruoho
47 1.1 jruoho atf_test_case lines
48 1.1 jruoho lines_head()
49 1.1 jruoho {
50 1.1 jruoho atf_set "descr" "Test patching lines"
51 1.1 jruoho }
52 1.1 jruoho
53 1.1 jruoho lines_body()
54 1.1 jruoho {
55 1.1 jruoho lines="1 10 100 1000 8100"
56 1.1 jruoho
57 1.1 jruoho for line in $lines; do
58 1.1 jruoho patch_lines $line
59 1.1 jruoho done
60 1.1 jruoho }
61 1.1 jruoho
62 1.1 jruoho atf_test_case long_lines
63 1.1 jruoho long_lines_head()
64 1.1 jruoho {
65 1.1 jruoho atf_set "descr" "Test patching long lines (PR bin/54620)"
66 1.1 jruoho }
67 1.1 jruoho
68 1.1 jruoho long_lines_body()
69 1.1 jruoho {
70 1.1 jruoho atf_expect_fail "PR bin/54620"
71 1.1 jruoho patch_lines 10000
72 1.1 jruoho }
73 1.1 jruoho
74 1.2 nia atf_test_case backup_simple
75 1.2 nia backup_simple_head()
76 1.2 nia {
77 1.2 nia atf_set "descr" "Test backup type 'simple' (-V simple)"
78 1.2 nia }
79 1.2 nia
80 1.2 nia backup_simple_body()
81 1.2 nia {
82 1.2 nia # Create the backup file so it's overwritten.
83 1.2 nia touch to_patch.orig
84 1.2 nia printf '%s\n' 'original file' > to_patch
85 1.2 nia
86 1.2 nia cat << EOF > test_diff.patch
87 1.2 nia --- original_file 2021-02-20 09:21:07.100869692 +0100
88 1.2 nia +++ new_file 2021-02-20 09:21:10.912906887 +0100
89 1.2 nia @@ -1 +1 @@
90 1.2 nia -original text
91 1.2 nia +new text
92 1.2 nia EOF
93 1.2 nia cksum=$(sha256 -n to_patch | cut -d' ' -f1)
94 1.2 nia patch -V simple to_patch < test_diff.patch
95 1.2 nia atf_check [ -f to_patch.orig ]
96 1.2 nia origfile_cksum=$(sha256 -n to_patch.orig | cut -d' ' -f1)
97 1.2 nia atf_check_equal "$cksum" "$origfile_cksum"
98 1.2 nia atf_check grep -q -m 1 '^new text$' to_patch
99 1.2 nia }
100 1.2 nia
101 1.2 nia atf_test_case backup_none
102 1.2 nia backup_none_head()
103 1.2 nia {
104 1.2 nia atf_set "descr" "Test backup type 'none' (-V none)"
105 1.2 nia }
106 1.2 nia
107 1.2 nia backup_none_body()
108 1.2 nia {
109 1.2 nia printf '%s\n' 'original file' > to_patch
110 1.2 nia
111 1.2 nia cat << EOF > test_diff.patch
112 1.2 nia --- original_file 2021-02-20 09:21:07.100869692 +0100
113 1.2 nia +++ new_file 2021-02-20 09:21:10.912906887 +0100
114 1.2 nia @@ -1 +1 @@
115 1.2 nia -original text
116 1.2 nia +new text
117 1.2 nia EOF
118 1.2 nia # Patch would mistakenly create 'simple' backup files when unwanted:
119 1.2 nia # http://mail-index.netbsd.org/tech-userlevel/2021/02/19/msg012901.html
120 1.2 nia patch -V none to_patch < test_diff.patch
121 1.2 nia atf_check [ ! -f to_patch.orig ]
122 1.2 nia atf_check grep -q -m 1 '^new text$' to_patch
123 1.2 nia
124 1.2 nia # Environment variables should be overriden.
125 1.2 nia printf '%s\n' 'original file' > to_patch
126 1.2 nia VERSION_CONTROL=existing patch -V none to_patch \
127 1.2 nia < test_diff.patch
128 1.2 nia atf_check [ ! -f to_patch.orig ]
129 1.2 nia atf_check grep -q -m 1 '^new text$' to_patch
130 1.2 nia
131 1.2 nia # --posix should imply -V none.
132 1.2 nia printf '%s\n' 'original file' > to_patch
133 1.2 nia patch --posix to_patch < test_diff.patch
134 1.2 nia atf_check [ ! -f to_patch.orig ]
135 1.2 nia atf_check grep -q -m 1 '^new text$' to_patch
136 1.2 nia }
137 1.2 nia
138 1.2 nia atf_test_case backup_numbered
139 1.2 nia backup_numbered_head()
140 1.2 nia {
141 1.2 nia atf_set "descr" "Test backup type 'numbered' (-V numbered)"
142 1.2 nia }
143 1.2 nia
144 1.2 nia backup_numbered_body()
145 1.2 nia {
146 1.2 nia printf '%s\n' 'original file' > to_patch
147 1.2 nia
148 1.2 nia cat << EOF > test_diff.patch
149 1.2 nia --- original_file 2021-02-20 09:21:07.100869692 +0100
150 1.2 nia +++ new_file 2021-02-20 09:21:10.912906887 +0100
151 1.2 nia @@ -1 +1 @@
152 1.2 nia -original text
153 1.2 nia +new text
154 1.2 nia EOF
155 1.2 nia cksum1=$(sha256 -n to_patch | cut -d' ' -f1)
156 1.2 nia patch -V numbered to_patch < test_diff.patch
157 1.2 nia atf_check grep -q -m 1 '^new text$' to_patch
158 1.2 nia
159 1.2 nia cat << EOF > test_diff2.patch
160 1.2 nia --- new_file 2021-02-20 09:44:52.363230019 +0100
161 1.2 nia +++ newer_file 2021-02-20 09:43:54.592863401 +0100
162 1.2 nia @@ -1 +1 @@
163 1.2 nia -new text
164 1.2 nia +newer text
165 1.2 nia EOF
166 1.2 nia cksum2=$(sha256 -n to_patch | cut -d' ' -f1)
167 1.2 nia patch -V numbered to_patch < test_diff2.patch
168 1.2 nia atf_check grep -q -m 1 '^newer text$' to_patch
169 1.2 nia
170 1.2 nia # Make sure the backup files match the original files.
171 1.2 nia origfile_cksum1=$(sha256 -n to_patch.~1~ | cut -d' ' -f1)
172 1.2 nia origfile_cksum2=$(sha256 -n to_patch.~2~ | cut -d' ' -f1)
173 1.2 nia atf_check [ -f to_patch.~1~ ]
174 1.2 nia atf_check_equal "$cksum1" "$origfile_cksum1"
175 1.2 nia atf_check [ -f to_patch.~2~ ]
176 1.2 nia atf_check_equal "$cksum2" "$origfile_cksum2"
177 1.2 nia }
178 1.2 nia
179 1.1 jruoho atf_init_test_cases()
180 1.1 jruoho {
181 1.1 jruoho atf_add_test_case lines
182 1.1 jruoho atf_add_test_case long_lines
183 1.2 nia atf_add_test_case backup_simple
184 1.2 nia atf_add_test_case backup_none
185 1.2 nia atf_add_test_case backup_numbered
186 1.1 jruoho }
187