|
16 | 16 | LandingChecks, |
17 | 17 | PatchCollectionAssessor, |
18 | 18 | PreventDotGithubCheck, |
| 19 | + PreventEmptyBinaryCheck, |
19 | 20 | PreventHgDirectoryCheck, |
20 | 21 | PreventNSPRNSSCheck, |
21 | 22 | PreventSignedCommitsCheck, |
@@ -646,6 +647,99 @@ def test_check_prevent_submodules(): |
646 | 647 | ), "Check should prevent revisions from introducing submodules." |
647 | 648 |
|
648 | 649 |
|
| 650 | +# Patches exercising the binary file paths in `rs_parsepatch`. These reflect |
| 651 | +# the GIT binary patch format Phabricator emits via `differential.getrawdiff`. |
| 652 | +GIT_DIFF_EMPTY_BINARY_ADD = """\ |
| 653 | +diff --git a/empty.png b/empty.png |
| 654 | +new file mode 100644 |
| 655 | +GIT binary patch |
| 656 | +literal 0 |
| 657 | +HcmV?d00001 |
| 658 | +
|
| 659 | +""" |
| 660 | + |
| 661 | +GIT_DIFF_NONEMPTY_BINARY_ADD = """\ |
| 662 | +diff --git a/real.png b/real.png |
| 663 | +new file mode 100644 |
| 664 | +GIT binary patch |
| 665 | +literal 87 |
| 666 | +zcmZ?wbhEHbWMp7uXkdSr1_lE>3=9k!85kJ?7+e9j7+M3l85kIz?VMfFP&3pMz`(EJ |
| 667 | +o&%nUd&(O%n&CtNI%g_nQz5(_qzaP=zsBtDUMI;Cj1_p+T0VKi-RR910 |
| 668 | +
|
| 669 | +""" |
| 670 | + |
| 671 | +GIT_DIFF_BINARY_DELETE = """\ |
| 672 | +diff --git a/old.png b/old.png |
| 673 | +deleted file mode 100644 |
| 674 | +GIT binary patch |
| 675 | +literal 0 |
| 676 | +HcmV?d00001 |
| 677 | +
|
| 678 | +""" |
| 679 | + |
| 680 | + |
| 681 | +def test_check_prevent_empty_binary_text_only(): |
| 682 | + """Text-only diffs are unaffected.""" |
| 683 | + parsed_diff = rs_parsepatch.get_diffs( |
| 684 | + GIT_DIFF_FILENAME_TEMPLATE.format(filename="src/foo.cpp") |
| 685 | + ) |
| 686 | + check = PreventEmptyBinaryCheck() |
| 687 | + for diff in parsed_diff: |
| 688 | + check.next_diff(diff) |
| 689 | + assert check.result() is None, "Text-only diffs must not trigger the check." |
| 690 | + |
| 691 | + |
| 692 | +def test_check_prevent_empty_binary_nonempty_addition(): |
| 693 | + """Adding a non-empty binary file is allowed.""" |
| 694 | + parsed_diff = rs_parsepatch.get_diffs(GIT_DIFF_NONEMPTY_BINARY_ADD) |
| 695 | + check = PreventEmptyBinaryCheck() |
| 696 | + for diff in parsed_diff: |
| 697 | + check.next_diff(diff) |
| 698 | + assert check.result() is None, ( |
| 699 | + "Adding a binary file with content must not trigger the check." |
| 700 | + ) |
| 701 | + |
| 702 | + |
| 703 | +def test_check_prevent_empty_binary_deletion(): |
| 704 | + """Deleting a binary file is allowed even though its hunk is zero-sized.""" |
| 705 | + parsed_diff = rs_parsepatch.get_diffs(GIT_DIFF_BINARY_DELETE) |
| 706 | + check = PreventEmptyBinaryCheck() |
| 707 | + for diff in parsed_diff: |
| 708 | + check.next_diff(diff) |
| 709 | + assert check.result() is None, "Deleting a binary file must not trigger the check." |
| 710 | + |
| 711 | + |
| 712 | +def test_check_prevent_empty_binary_addition(): |
| 713 | + """Adding a zero-byte binary file is the bug 1709608 signature.""" |
| 714 | + parsed_diff = rs_parsepatch.get_diffs(GIT_DIFF_EMPTY_BINARY_ADD) |
| 715 | + check = PreventEmptyBinaryCheck() |
| 716 | + for diff in parsed_diff: |
| 717 | + check.next_diff(diff) |
| 718 | + result = check.result() |
| 719 | + assert result is not None, "Adding a zero-byte binary file must trigger the check." |
| 720 | + assert "empty.png" in result |
| 721 | + assert "1709608" in result |
| 722 | + |
| 723 | + |
| 724 | +def test_check_prevent_empty_binary_mixed(): |
| 725 | + """In a mixed diff, only the empty addition is reported.""" |
| 726 | + parsed_diff = rs_parsepatch.get_diffs( |
| 727 | + GIT_DIFF_EMPTY_BINARY_ADD |
| 728 | + + GIT_DIFF_NONEMPTY_BINARY_ADD |
| 729 | + + GIT_DIFF_FILENAME_TEMPLATE.format(filename="src/bar.cpp") |
| 730 | + ) |
| 731 | + check = PreventEmptyBinaryCheck() |
| 732 | + for diff in parsed_diff: |
| 733 | + check.next_diff(diff) |
| 734 | + result = check.result() |
| 735 | + assert result is not None |
| 736 | + assert "empty.png" in result |
| 737 | + assert "real.png" not in result, ( |
| 738 | + "Non-empty binary additions must not appear in the report." |
| 739 | + ) |
| 740 | + assert "src/bar.cpp" not in result, "Text additions must not appear in the report." |
| 741 | + |
| 742 | + |
649 | 743 | def test_check_bug_references_public_bugs(): |
650 | 744 | patch_helper = HgPatchHelper.from_string_io( |
651 | 745 | io.StringIO( |
|
0 commit comments