commit: 0339031c22f3e30d95d1234ce08a47ae5df8e3f8 Author: Felix Bier <flx.bier <AT> gmail <DOT> com> AuthorDate: Tue May 18 20:53:11 2021 +0000 Commit: Zac Medico <zmedico <AT> gentoo <DOT> org> CommitDate: Mon May 24 06:17:13 2021 +0000 URL: https://gitweb.gentoo.org/proj/portage.git/commit/?id=0339031c
Add unit tests for _shell_quote This commit adds unit tests that cover the existing functionality of _shell_quote, by checking that various special characters are escaped/quoted correctly. Signed-off-by: Felix Bier <felix.bier <AT> rohde-schwarz.com> Signed-off-by: Zac Medico <zmedico <AT> gentoo.org> lib/portage/tests/ebuild/test_shell_quote.py | 79 ++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) diff --git a/lib/portage/tests/ebuild/test_shell_quote.py b/lib/portage/tests/ebuild/test_shell_quote.py new file mode 100644 index 000000000..ce419488a --- /dev/null +++ b/lib/portage/tests/ebuild/test_shell_quote.py @@ -0,0 +1,79 @@ +# Copyright 2021 Gentoo Foundation +# Distributed under the terms of the GNU General Public License v2 + +from portage import _shell_quote +from portage.tests import TestCase + +class ShellQuoteTestCase(TestCase): + + def testShellQuote(self): + test_data = [ + + # String contains no special characters, should be preserved. + ("abc","abc"), + + # String contains whitespace, should be double-quoted to prevent word splitting. + ("abc xyz","\"abc xyz\""), + ("abc xyz","\"abc xyz\""), + (" abcxyz ","\" abcxyz \""), + ("abc\txyz","\"abc\txyz\""), + ("abc\t\txyz","\"abc\t\txyz\""), + ("\tabcxyz\t","\"\tabcxyz\t\""), + ("abc\nxyz","\"abc\nxyz\""), + ("abc\n\nxyz","\"abc\n\nxyz\""), + ("\nabcxyz\n","\"\nabcxyz\n\""), + + # String contains > or <, should be double-quoted to prevent redirection. + ("abc>xyz","\"abc>xyz\""), + ("abc>>xyz","\"abc>>xyz\""), + (">abcxyz>","\">abcxyz>\""), + ("abc<xyz","\"abc<xyz\""), + ("abc<<xyz","\"abc<<xyz\""), + ("<abcxyz<","\"<abcxyz<\""), + + # String contains =, should be double-quoted to prevent assignment. + ("abc=xyz","\"abc=xyz\""), + ("abc==xyz","\"abc==xyz\""), + ("=abcxyz=","\"=abcxyz=\""), + + # String contains *, should be double-quoted to prevent globbing. + ("abc*xyz","\"abc*xyz\""), + ("abc**xyz","\"abc**xyz\""), + ("*abcxyz*","\"*abcxyz*\""), + + # String contains $, should be escaped to prevent parameter expansion. + # Also double-quoted, though not strictly needed. + ("abc$xyz","\"abc\\$xyz\""), + ("abc$$xyz","\"abc\\$\\$xyz\""), + ("$abcxyz$","\"\\$abcxyz\\$\""), + + # String contains `, should be escaped to prevent command substitution. + # Also double-quoted, though not strictly needed. + ("abc`xyz","\"abc\\`xyz\""), + ("abc``xyz","\"abc\\`\\`xyz\""), + ("`abc`","\"\\`abc\\`\""), + + # String contains \, should be escaped to prevent it from escaping + # the next character. Also double-quoted, though not strictly needed. + ("abc\\xyz", "\"abc\\\\xyz\""), + ("abc\\\\xyz", "\"abc\\\\\\\\xyz\""), + ("\\abcxyz\\", "\"\\\\abcxyz\\\\\""), + + # String contains ", should be escaped to prevent it from unexpectedly + # ending a previous double-quote or starting a new double-quote. Also + # double-quoted, though not strictly needed. + ("abc\"xyz","\"abc\\\"xyz\""), + ("abc\"\"xyz","\"abc\\\"\\\"xyz\""), + ("\"abcxyz\"","\"\\\"abcxyz\\\"\""), + + # String contains ', should be double-quoted to prevent it from unexpectedly + # ending a previous single-quote or starting a new single-quote. + ("abc'xyz","\"abc'xyz\""), + ("abc''xyz","\"abc''xyz\""), + ("'abcxyz'","\"'abcxyz'\""), + + ] + + for (data,expected_result) in test_data: + result = _shell_quote(data) + self.assertEqual(result, expected_result)