diff options
Diffstat (limited to 'meta/lib/patchtest/tests/test_patch.py')
-rw-r--r-- | meta/lib/patchtest/tests/test_patch.py | 131 |
1 files changed, 131 insertions, 0 deletions
diff --git a/meta/lib/patchtest/tests/test_patch.py b/meta/lib/patchtest/tests/test_patch.py new file mode 100644 index 0000000000..d08b8a5019 --- /dev/null +++ b/meta/lib/patchtest/tests/test_patch.py | |||
@@ -0,0 +1,131 @@ | |||
1 | # Checks related to the patch's CVE lines | ||
2 | # | ||
3 | # Copyright (C) 2016 Intel Corporation | ||
4 | # | ||
5 | # SPDX-License-Identifier: GPL-2.0-only | ||
6 | # | ||
7 | |||
8 | import base | ||
9 | import os | ||
10 | import patchtest_patterns | ||
11 | import pyparsing | ||
12 | |||
13 | class TestPatch(base.Base): | ||
14 | |||
15 | @classmethod | ||
16 | def setUpClassLocal(cls): | ||
17 | cls.newpatches = [] | ||
18 | # get just those relevant patches: new software patches | ||
19 | for patch in cls.patchset: | ||
20 | if patch.path.endswith('.patch') and patch.is_added_file: | ||
21 | cls.newpatches.append(patch) | ||
22 | |||
23 | cls.mark = str(patchtest_patterns.signed_off_by_prefix).strip('"') | ||
24 | |||
25 | # match PatchSignedOffBy.mark with '+' preceding it | ||
26 | cls.prog = patchtest_patterns.patch_signed_off_by | ||
27 | |||
28 | def setUp(self): | ||
29 | if self.unidiff_parse_error: | ||
30 | self.skip('Parse error %s' % self.unidiff_parse_error) | ||
31 | |||
32 | self.valid_status = ", ".join(patchtest_patterns.upstream_status_nonliteral_valid_status) | ||
33 | self.standard_format = "Upstream-Status: <Valid status>" | ||
34 | |||
35 | # we are just interested in series that introduce CVE patches, thus discard other | ||
36 | # possibilities: modification to current CVEs, patch directly introduced into the | ||
37 | # recipe, upgrades already including the CVE, etc. | ||
38 | new_cves = [p for p in self.patchset if p.path.endswith('.patch') and p.is_added_file] | ||
39 | if not new_cves: | ||
40 | self.skip('No new CVE patches introduced') | ||
41 | |||
42 | def test_upstream_status_presence_format(self): | ||
43 | if not TestPatch.newpatches: | ||
44 | self.skip("There are no new software patches, no reason to test Upstream-Status presence/format") | ||
45 | |||
46 | for newpatch in TestPatch.newpatches: | ||
47 | payload = newpatch.__str__() | ||
48 | if not patchtest_patterns.upstream_status_regex.search_string(payload): | ||
49 | self.fail( | ||
50 | "Added patch file is missing Upstream-Status: <Valid status> in the commit message", | ||
51 | data=[ | ||
52 | ("Standard format", self.standard_format), | ||
53 | ("Valid status", self.valid_status), | ||
54 | ], | ||
55 | ) | ||
56 | for line in payload.splitlines(): | ||
57 | if patchtest_patterns.patchmetadata_regex.match(line): | ||
58 | continue | ||
59 | if patchtest_patterns.upstream_status_regex.search_string(line): | ||
60 | if patchtest_patterns.inappropriate.searchString(line): | ||
61 | try: | ||
62 | patchtest_patterns.upstream_status_inappropriate_info.parseString( | ||
63 | line.lstrip("+") | ||
64 | ) | ||
65 | except pyparsing.ParseException as pe: | ||
66 | self.fail( | ||
67 | "Upstream-Status is Inappropriate, but no reason was provided", | ||
68 | data=[ | ||
69 | ("Current", pe.pstr), | ||
70 | ( | ||
71 | "Standard format", | ||
72 | "Upstream-Status: Inappropriate [reason]", | ||
73 | ), | ||
74 | ], | ||
75 | ) | ||
76 | elif patchtest_patterns.submitted.searchString(line): | ||
77 | try: | ||
78 | patchtest_patterns.upstream_status_submitted_info.parseString( | ||
79 | line.lstrip("+") | ||
80 | ) | ||
81 | except pyparsing.ParseException as pe: | ||
82 | self.fail( | ||
83 | "Upstream-Status is Submitted, but it is not mentioned where", | ||
84 | data=[ | ||
85 | ("Current", pe.pstr), | ||
86 | ( | ||
87 | "Standard format", | ||
88 | "Upstream-Status: Submitted [where]", | ||
89 | ), | ||
90 | ], | ||
91 | ) | ||
92 | else: | ||
93 | try: | ||
94 | patchtest_patterns.upstream_status.parseString(line.lstrip("+")) | ||
95 | except pyparsing.ParseException as pe: | ||
96 | self.fail( | ||
97 | "Upstream-Status is in incorrect format", | ||
98 | data=[ | ||
99 | ("Current", pe.pstr), | ||
100 | ("Standard format", self.standard_format), | ||
101 | ("Valid status", self.valid_status), | ||
102 | ], | ||
103 | ) | ||
104 | |||
105 | def test_signed_off_by_presence(self): | ||
106 | if not TestPatch.newpatches: | ||
107 | self.skip("There are no new software patches, no reason to test %s presence" % PatchSignedOffBy.mark) | ||
108 | |||
109 | for newpatch in TestPatch.newpatches: | ||
110 | payload = newpatch.__str__() | ||
111 | for line in payload.splitlines(): | ||
112 | if patchtest_patterns.patchmetadata_regex.match(line): | ||
113 | continue | ||
114 | if TestPatch.prog.search_string(payload): | ||
115 | break | ||
116 | else: | ||
117 | self.fail('A patch file has been added without a Signed-off-by tag: \'%s\'' % os.path.basename(newpatch.path)) | ||
118 | |||
119 | def test_cve_tag_format(self): | ||
120 | for commit in TestPatch.commits: | ||
121 | if patchtest_patterns.cve.search_string( | ||
122 | commit.shortlog | ||
123 | ) or patchtest_patterns.cve.search_string(commit.commit_message): | ||
124 | tag_found = False | ||
125 | for line in commit.payload.splitlines(): | ||
126 | if patchtest_patterns.cve_payload_tag.search_string(line): | ||
127 | tag_found = True | ||
128 | break | ||
129 | if not tag_found: | ||
130 | self.fail('Missing or incorrectly formatted CVE tag in patch file. Correct or include the CVE tag in the patch with format: "CVE: CVE-YYYY-XXXX"', | ||
131 | commit=commit) | ||