aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFelix Bier <flx.bier@gmail.com>2021-05-18 22:53:11 +0200
committerZac Medico <zmedico@gentoo.org>2021-05-23 23:17:13 -0700
commit0339031c22f3e30d95d1234ce08a47ae5df8e3f8 (patch)
tree72e783cd1e1686a4b1a4756b378be31b5687b4e0
parentenv-update: skip PATH in systemd user environment definition (diff)
downloadportage-0339031c22f3e30d95d1234ce08a47ae5df8e3f8.tar.gz
portage-0339031c22f3e30d95d1234ce08a47ae5df8e3f8.tar.bz2
portage-0339031c22f3e30d95d1234ce08a47ae5df8e3f8.zip
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@rohde-schwarz.com> Signed-off-by: Zac Medico <zmedico@gentoo.org>
-rw-r--r--lib/portage/tests/ebuild/test_shell_quote.py79
1 files changed, 79 insertions, 0 deletions
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)