1*207defd0Sandvar# $NetBSD: t_patch.sh,v 1.4 2021/09/11 20:28:06 andvar Exp $ 227ee1d29Sjruoho# 3ada1367bSnia# Copyright (c) 2020, 2021 The NetBSD Foundation, Inc. 427ee1d29Sjruoho# All rights reserved. 527ee1d29Sjruoho# 627ee1d29Sjruoho# Redistribution and use in source and binary forms, with or without 727ee1d29Sjruoho# modification, are permitted provided that the following conditions 827ee1d29Sjruoho# are met: 927ee1d29Sjruoho# 1. Redistributions of source code must retain the above copyright 1027ee1d29Sjruoho# notice, this list of conditions and the following disclaimer. 1127ee1d29Sjruoho# 2. Redistributions in binary form must reproduce the above copyright 1227ee1d29Sjruoho# notice, this list of conditions and the following disclaimer in the 1327ee1d29Sjruoho# documentation and/or other materials provided with the distribution. 1427ee1d29Sjruoho# 1527ee1d29Sjruoho# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 1627ee1d29Sjruoho# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 1727ee1d29Sjruoho# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 1827ee1d29Sjruoho# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 1927ee1d29Sjruoho# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 2027ee1d29Sjruoho# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 2127ee1d29Sjruoho# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 2227ee1d29Sjruoho# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 2327ee1d29Sjruoho# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 2427ee1d29Sjruoho# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 2527ee1d29Sjruoho# POSSIBILITY OF SUCH DAMAGE. 2627ee1d29Sjruoho# 2727ee1d29Sjruoho 2827ee1d29Sjruohopatch_lines() { 2927ee1d29Sjruoho 3027ee1d29Sjruoho printf "%$1s" | tr " " "a" > longlines 2>/dev/null 3127ee1d29Sjruoho 3227ee1d29Sjruoho cat << EOF > longlines.patch 3327ee1d29Sjruoho--- ./longlines.orig 2019-10-16 09:25:30.667656644 +0000 3427ee1d29Sjruoho+++ ./longlines 3527ee1d29Sjruoho@@ -1 +1 @@ 3627ee1d29SjruohoEOF 3727ee1d29Sjruoho printf -- "-%$1s\n" | tr " " "a" >> longlines.patch 2>/dev/null 3827ee1d29Sjruoho printf -- "+%$1s" | tr " " "b" >> longlines.patch 2>/dev/null 3927ee1d29Sjruoho 4027ee1d29Sjruoho patch longlines < longlines.patch 4127ee1d29Sjruoho 4227ee1d29Sjruoho if [ ! $? -eq 0 ]; then 4327ee1d29Sjruoho atf_fail "Failed to patch long lines" 4427ee1d29Sjruoho fi 4527ee1d29Sjruoho} 4627ee1d29Sjruoho 4727ee1d29Sjruohoatf_test_case lines 4827ee1d29Sjruoholines_head() 4927ee1d29Sjruoho{ 5027ee1d29Sjruoho atf_set "descr" "Test patching lines" 5127ee1d29Sjruoho} 5227ee1d29Sjruoho 5327ee1d29Sjruoholines_body() 5427ee1d29Sjruoho{ 5527ee1d29Sjruoho lines="1 10 100 1000 8100" 5627ee1d29Sjruoho 5727ee1d29Sjruoho for line in $lines; do 5827ee1d29Sjruoho patch_lines $line 5927ee1d29Sjruoho done 6027ee1d29Sjruoho} 6127ee1d29Sjruoho 6227ee1d29Sjruohoatf_test_case long_lines 6327ee1d29Sjruoholong_lines_head() 6427ee1d29Sjruoho{ 6527ee1d29Sjruoho atf_set "descr" "Test patching long lines (PR bin/54620)" 6627ee1d29Sjruoho} 6727ee1d29Sjruoho 6827ee1d29Sjruoholong_lines_body() 6927ee1d29Sjruoho{ 7027ee1d29Sjruoho patch_lines 10000 7127ee1d29Sjruoho} 7227ee1d29Sjruoho 73ada1367bSniaatf_test_case backup_simple 74ada1367bSniabackup_simple_head() 75ada1367bSnia{ 76ada1367bSnia atf_set "descr" "Test backup type 'simple' (-V simple)" 77ada1367bSnia} 78ada1367bSnia 79ada1367bSniabackup_simple_body() 80ada1367bSnia{ 81ada1367bSnia # Create the backup file so it's overwritten. 82ada1367bSnia touch to_patch.orig 83ada1367bSnia printf '%s\n' 'original file' > to_patch 84ada1367bSnia 85ada1367bSnia cat << EOF > test_diff.patch 86ada1367bSnia--- original_file 2021-02-20 09:21:07.100869692 +0100 87ada1367bSnia+++ new_file 2021-02-20 09:21:10.912906887 +0100 88ada1367bSnia@@ -1 +1 @@ 89ada1367bSnia-original text 90ada1367bSnia+new text 91ada1367bSniaEOF 92ada1367bSnia cksum=$(sha256 -n to_patch | cut -d' ' -f1) 93ada1367bSnia patch -V simple to_patch < test_diff.patch 94ada1367bSnia atf_check [ -f to_patch.orig ] 95ada1367bSnia origfile_cksum=$(sha256 -n to_patch.orig | cut -d' ' -f1) 96ada1367bSnia atf_check_equal "$cksum" "$origfile_cksum" 97ada1367bSnia atf_check grep -q -m 1 '^new text$' to_patch 98ada1367bSnia} 99ada1367bSnia 100ada1367bSniaatf_test_case backup_none 101ada1367bSniabackup_none_head() 102ada1367bSnia{ 103ada1367bSnia atf_set "descr" "Test backup type 'none' (-V none)" 104ada1367bSnia} 105ada1367bSnia 106ada1367bSniabackup_none_body() 107ada1367bSnia{ 108ada1367bSnia printf '%s\n' 'original file' > to_patch 109ada1367bSnia 110ada1367bSnia cat << EOF > test_diff.patch 111ada1367bSnia--- original_file 2021-02-20 09:21:07.100869692 +0100 112ada1367bSnia+++ new_file 2021-02-20 09:21:10.912906887 +0100 113ada1367bSnia@@ -1 +1 @@ 114ada1367bSnia-original text 115ada1367bSnia+new text 116ada1367bSniaEOF 117ada1367bSnia # Patch would mistakenly create 'simple' backup files when unwanted: 118ada1367bSnia # http://mail-index.netbsd.org/tech-userlevel/2021/02/19/msg012901.html 119ada1367bSnia patch -V none to_patch < test_diff.patch 120ada1367bSnia atf_check [ ! -f to_patch.orig ] 121ada1367bSnia atf_check grep -q -m 1 '^new text$' to_patch 122ada1367bSnia 123*207defd0Sandvar # Environment variables should be overridden. 124ada1367bSnia printf '%s\n' 'original file' > to_patch 125ada1367bSnia VERSION_CONTROL=existing patch -V none to_patch \ 126ada1367bSnia < test_diff.patch 127ada1367bSnia atf_check [ ! -f to_patch.orig ] 128ada1367bSnia atf_check grep -q -m 1 '^new text$' to_patch 129ada1367bSnia 130ada1367bSnia # --posix should imply -V none. 131ada1367bSnia printf '%s\n' 'original file' > to_patch 132ada1367bSnia patch --posix to_patch < test_diff.patch 133ada1367bSnia atf_check [ ! -f to_patch.orig ] 134ada1367bSnia atf_check grep -q -m 1 '^new text$' to_patch 135ada1367bSnia} 136ada1367bSnia 137ada1367bSniaatf_test_case backup_numbered 138ada1367bSniabackup_numbered_head() 139ada1367bSnia{ 140ada1367bSnia atf_set "descr" "Test backup type 'numbered' (-V numbered)" 141ada1367bSnia} 142ada1367bSnia 143ada1367bSniabackup_numbered_body() 144ada1367bSnia{ 145ada1367bSnia printf '%s\n' 'original file' > to_patch 146ada1367bSnia 147ada1367bSnia cat << EOF > test_diff.patch 148ada1367bSnia--- original_file 2021-02-20 09:21:07.100869692 +0100 149ada1367bSnia+++ new_file 2021-02-20 09:21:10.912906887 +0100 150ada1367bSnia@@ -1 +1 @@ 151ada1367bSnia-original text 152ada1367bSnia+new text 153ada1367bSniaEOF 154ada1367bSnia cksum1=$(sha256 -n to_patch | cut -d' ' -f1) 155ada1367bSnia patch -V numbered to_patch < test_diff.patch 156ada1367bSnia atf_check grep -q -m 1 '^new text$' to_patch 157ada1367bSnia 158ada1367bSnia cat << EOF > test_diff2.patch 159ada1367bSnia--- new_file 2021-02-20 09:44:52.363230019 +0100 160ada1367bSnia+++ newer_file 2021-02-20 09:43:54.592863401 +0100 161ada1367bSnia@@ -1 +1 @@ 162ada1367bSnia-new text 163ada1367bSnia+newer text 164ada1367bSniaEOF 165ada1367bSnia cksum2=$(sha256 -n to_patch | cut -d' ' -f1) 166ada1367bSnia patch -V numbered to_patch < test_diff2.patch 167ada1367bSnia atf_check grep -q -m 1 '^newer text$' to_patch 168ada1367bSnia 169ada1367bSnia # Make sure the backup files match the original files. 170ada1367bSnia origfile_cksum1=$(sha256 -n to_patch.~1~ | cut -d' ' -f1) 171ada1367bSnia origfile_cksum2=$(sha256 -n to_patch.~2~ | cut -d' ' -f1) 172ada1367bSnia atf_check [ -f to_patch.~1~ ] 173ada1367bSnia atf_check_equal "$cksum1" "$origfile_cksum1" 174ada1367bSnia atf_check [ -f to_patch.~2~ ] 175ada1367bSnia atf_check_equal "$cksum2" "$origfile_cksum2" 176ada1367bSnia} 177ada1367bSnia 17827ee1d29Sjruohoatf_init_test_cases() 17927ee1d29Sjruoho{ 18027ee1d29Sjruoho atf_add_test_case lines 18127ee1d29Sjruoho atf_add_test_case long_lines 182ada1367bSnia atf_add_test_case backup_simple 183ada1367bSnia atf_add_test_case backup_none 184ada1367bSnia atf_add_test_case backup_numbered 18527ee1d29Sjruoho} 186