-
-
Notifications
You must be signed in to change notification settings - Fork 31.8k
gh-103186: assert in tests that UnsafeMailcapInput warnings are provided #103217
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
gh-103186: assert in tests that UnsafeMailcapInput warnings are provided #103217
Conversation
The asserted result is the None value (unsafe input rejected by mailcap)
This comment was marked as outdated.
This comment was marked as outdated.
Lib/test/test_mailcap.py
Outdated
with warnings_helper.check_warnings(('', tc[2]["warn_type"]), quiet=True): | ||
self.assertEqual(mailcap.subst(*tc[0]), tc[1]) | ||
else: | ||
self.assertEqual(mailcap.subst(*tc[0]), tc[1]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems a bit over-engineered. I'd split this into a separate test function, which tests that the warning is issued in cases that deserve the warning. This exception is raised from 3 places in the Lib/mailcap.py, so shouldn't we have at least 3 test cases for it?
CC @encukou .
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi Irit,
Thank you, I'll rework the affected cases into a separate test function and update the patch.
Will add 2x new test cases for unsafe filename and parameters, only the MIME type warnings are raised at present
Lib/test/test_mailcap.py
Outdated
with self.assertWarnsRegex(mailcap.UnsafeMailcapInput,'Refusing to substitute MIME type.*into a shell'): | ||
self.assertEqual(mailcap.subst("echo %t", "audio/*", "foo.txt"), None) | ||
|
||
with self.assertWarnsRegex(mailcap.UnsafeMailcapInput,'Refusing to use mailcap with filename.*Use a safe temporary filename.'): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PEP 8: wrap lines at 80 chars, and have whitespace between the ,
and the next function arg.
with self.assertWarnsRegex(mailcap.UnsafeMailcapInput,'Refusing to use mailcap with filename.*Use a safe temporary filename.'): | |
with self.assertWarnsRegex(mailcap.UnsafeMailcapInput, | |
'Refusing to use mailcap with filename.*Use a safe temporary filename.'): |
Lib/test/test_mailcap.py
Outdated
@@ -245,6 +241,19 @@ def test_test(self): | |||
] | |||
self._run_cases(cases) | |||
|
|||
def test_unsafe_mailcap_input(self): | |||
c = MAILCAPDICT |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
c = MAILCAPDICT | |
c = MAILCAPDICT |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is only used in one place, you could move it there.
Lib/test/test_mailcap.py
Outdated
self.assertEqual(mailcap.findmatch(MAILCAPDICT, | ||
"audio/wav", filename="foo*.txt"), (None, None)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't want to break the first arg into two lines and then continue with the second arg on the same line.
You can assign the return value of findmatch to a variable and then compare the variable to expected value.
Otherwise, add a newline before the (None, None) and indent it like "mailcap.findmatch".
Lib/test/test_mailcap.py
Outdated
@@ -242,17 +242,20 @@ def test_test(self): | |||
self._run_cases(cases) | |||
|
|||
def test_unsafe_mailcap_input(self): | |||
c = MAILCAPDICT | |||
plist = ["total=*"] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is also only used in one place.
Co-authored-by: Irit Katriel <[email protected]>
Lib/test/test_mailcap.py
Outdated
self.assertEqual(mailcap.subst("echo %t", "audio/*", "foo.txt"), None) | ||
|
||
with self.assertWarnsRegex(mailcap.UnsafeMailcapInput, | ||
'Refusing to use mailcap with filename.*Use a safe temporary filename.'): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
'Refusing to use mailcap with filename.*Use a safe temporary filename.'): | |
'Refusing to use mailcap with filename.*' | |
'Use a safe temporary filename.'): |
Lib/test/test_mailcap.py
Outdated
plist = ["total=*"] | ||
with self.assertWarnsRegex(mailcap.UnsafeMailcapInput, | ||
'Refusing to substitute parameter.*into a shell command'): | ||
self.assertEqual(mailcap.subst("echo %{total}", "audio/wav", "foo.txt", plist), None) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is also > 80 chars
Thank you @TabLand, congrats on your first commit to cpython! |
First of hopefully lots of more in the future! |
Thanks @TabLand for the PR, and @iritkatriel for merging it 🌮🎉.. I'm working now to backport this PR to: 3.11. |
GH-108800 is a backport of this pull request to the 3.11 branch. |
… provided (pythonGH-103217) (cherry picked from commit 1724553) Co-authored-by: Ijtaba Hussain <[email protected]>
…e provided (GH-103217) (GH-108800) (cherry picked from commit 1724553) Co-authored-by: Ijtaba Hussain <[email protected]>
These warnings are expected as the function result being asserted is None