| 1 | #!/bin/sh |
| 2 | |
| 3 | test_description='git am with corrupt input' |
| 4 | |
| 5 | . ./test-lib.sh |
| 6 | |
| 7 | if ! test_have_prereq ICONV |
| 8 | then |
| 9 | skip_all='skipping am encoding corruption tests; iconv not available' |
| 10 | test_done |
| 11 | fi |
| 12 | |
| 13 | make_mbox_with_nul () { |
| 14 | space=' ' |
| 15 | q_nul_in_subject= |
| 16 | q_nul_in_body= |
| 17 | while test $# -ne 0 |
| 18 | do |
| 19 | case "$1" in |
| 20 | subject) q_nul_in_subject='=00' ;; |
| 21 | body) q_nul_in_body='=00' ;; |
| 22 | esac && |
| 23 | shift |
| 24 | done && |
| 25 | cat <<-EOF |
| 26 | From ec7364544f690c560304f5a5de9428ea3b978b26 Mon Sep 17 00:00:00 2001 |
| 27 | From: A U Thor <author@example.com> |
| 28 | Date: Sun, 19 Apr 2020 13:42:07 +0700 |
| 29 | Subject: [PATCH] =?ISO-8859-1?q?=C4=CB${q_nul_in_subject}=D1=CF=D6?= |
| 30 | MIME-Version: 1.0 |
| 31 | Content-Type: text/plain; charset=ISO-8859-1 |
| 32 | Content-Transfer-Encoding: quoted-printable |
| 33 | |
| 34 | abc${q_nul_in_body}def |
| 35 | --- |
| 36 | diff --git a/afile b/afile |
| 37 | new file mode 100644 |
| 38 | index 0000000000..e69de29bb2 |
| 39 | --$space |
| 40 | 2.26.1 |
| 41 | EOF |
| 42 | } |
| 43 | |
| 44 | test_expect_success setup ' |
| 45 | # Note the missing "+++" line: |
| 46 | cat >bad-patch.diff <<-\EOF && |
| 47 | From: A U Thor <au.thor@example.com> |
| 48 | diff --git a/f b/f |
| 49 | index 7898192..6178079 100644 |
| 50 | --- a/f |
| 51 | @@ -1 +1 @@ |
| 52 | -a |
| 53 | +b |
| 54 | EOF |
| 55 | |
| 56 | echo a >f && |
| 57 | git add f && |
| 58 | test_tick && |
| 59 | git commit -m initial |
| 60 | ' |
| 61 | |
| 62 | # This used to fail before, too, but with a different diagnostic. |
| 63 | # fatal: unable to write file '(null)' mode 100644: Bad address |
| 64 | # Also, it had the unwanted side-effect of deleting f. |
| 65 | test_expect_success 'try to apply corrupted patch' ' |
| 66 | test_when_finished "git am --abort" && |
| 67 | test_must_fail git -c advice.amWorkDir=false -c advice.mergeConflict=false am bad-patch.diff 2>actual && |
| 68 | test_path_is_file f && |
| 69 | test_grep "error: git diff header lacks filename information at .*rebase-apply/patch:4" actual |
| 70 | ' |
| 71 | |
| 72 | test_expect_success "NUL in commit message's body" ' |
| 73 | test_when_finished "git am --abort" && |
| 74 | make_mbox_with_nul body >body.patch && |
| 75 | test_must_fail git am body.patch 2>err && |
| 76 | test_grep "a NUL byte in commit log message not allowed" err |
| 77 | ' |
| 78 | |
| 79 | test_expect_success "NUL in commit message's header" " |
| 80 | test_when_finished 'git am --abort' && |
| 81 | make_mbox_with_nul subject >subject.patch && |
| 82 | test_must_fail git mailinfo msg patch <subject.patch 2>err && |
| 83 | test_grep \"a NUL byte in 'Subject' is not allowed\" err && |
| 84 | test_must_fail git am subject.patch 2>err && |
| 85 | test_grep \"a NUL byte in 'Subject' is not allowed\" err |
| 86 | " |
| 87 | |
| 88 | test_done |