From 0eb8d5168062a10b2e433dd3ea239cf37c3ada98 Mon Sep 17 00:00:00 2001 From: Bhaskar Brahma Date: Thu, 6 Oct 2022 12:02:55 -0700 Subject: [PATCH] Fixes ExtensionUtils.run_command_get_output() (#1604) * Fixes ExtensionUtils.run_command_get_output() ExtensionUtils.run_command_get_output() now decodes the output of commands into a utf-8 string. Fixes VMAccess extension's inability to accept non latin characters as password. * Addressed PR comments * Updated extension version --- .vscode/launch.json | 20 ++++++++++++++++++++ Utils/extensionutils.py | 10 +++++----- Utils/test/non_latin_characters.txt | 1 + Utils/test/test_encode.py | 15 +++++++++++++++ VMAccess/manifest.xml | 2 +- 5 files changed, 42 insertions(+), 6 deletions(-) create mode 100644 .vscode/launch.json create mode 100644 Utils/test/non_latin_characters.txt diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 000000000..e55b75059 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,20 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "name": "Python: test_encode.py", + "type": "python", + "request": "launch", + "program": "test_encode.py", + "console": "integratedTerminal", + "justMyCode": true, + "cwd": "${workspaceFolder}/Utils/test", + "env" : { + "PYTHONPATH": "${workspaceFolder}" + } + } + ] +} \ No newline at end of file diff --git a/Utils/extensionutils.py b/Utils/extensionutils.py index 2eac5885e..f0393a228 100644 --- a/Utils/extensionutils.py +++ b/Utils/extensionutils.py @@ -192,15 +192,15 @@ def run_command_get_output(cmd, chk_err=True, log_cmd=True): logger.error('CalledProcessError. Error Code is ' + str(e.returncode)) logger.error('CalledProcessError. Command string was ' + str(cmd)) logger.error( - 'CalledProcessError. Command result was ' + (e.output[:-1]).decode('latin-1')) - return e.returncode, e.output.decode('latin-1') + 'CalledProcessError. Command result was ' + (e.output[:-1]).decode('utf-8')) + return e.returncode, e.output.decode('utf-8') except EnvironmentError as e: if chk_err and log_cmd: logger.error( 'CalledProcessError. Error message is ' + str(e)) return e.errno, str(e) # noinspection PyUnboundLocalVariable - return 0, output.decode('latin-1') + return 0, output.decode('utf-8') def run(cmd, chk_err=True): @@ -238,8 +238,8 @@ def run_send_stdin(cmd, cmd_input, chk_err=True, log_cmd=True): logger.error('CalledProcessError. Error Code is ' + str(me.returncode)) logger.error('CalledProcessError. Command was ' + str(cmd)) logger.error( - 'CalledProcessError. Command result was ' + output[0].decode('latin-1')) - return me.returncode, output[0].decode('latin-1') + 'CalledProcessError. Command result was ' + output[0].decode('utf-8')) + return me.returncode, output[0].decode('utf-8') def get_line_starting_with(prefix, filepath): diff --git a/Utils/test/non_latin_characters.txt b/Utils/test/non_latin_characters.txt new file mode 100644 index 000000000..99fe40df2 --- /dev/null +++ b/Utils/test/non_latin_characters.txt @@ -0,0 +1 @@ +ü \ No newline at end of file diff --git a/Utils/test/test_encode.py b/Utils/test/test_encode.py index 7808e8edf..e9de56a20 100644 --- a/Utils/test/test_encode.py +++ b/Utils/test/test_encode.py @@ -26,6 +26,21 @@ def test_encode(self): known_non_ascii_character = b"%c" % encoded_contents[2353] self.assertEqual(known_non_ascii_character, b'\x9d') +class TestRunCommandGetOutput(unittest.TestCase): + def test_output(self): + cmd = ["cat", "non_latin_characters.txt"] + return_code, output_string = eu.run_command_get_output(cmd) + self.assertEqual(0, return_code) + expected_character_byte = b'\xc3\xbc' + expected_character = expected_character_byte.decode("utf-8") + self.assertEqual(expected_character, output_string[0]) + + def test_stdin(self): + cmd = ['bash', '-c', 'read ; echo $REPLY'] + cmd_input = b'\xc3\xbc' # ü character + return_code, output_string = eu.run_send_stdin(cmd, cmd_input) + self.assertEqual(0, return_code) + self.assertEqual(cmd_input.decode('utf-8'), output_string[0]) if __name__ == '__main__': unittest.main() diff --git a/VMAccess/manifest.xml b/VMAccess/manifest.xml index 407adbaaa..396b0248d 100644 --- a/VMAccess/manifest.xml +++ b/VMAccess/manifest.xml @@ -2,7 +2,7 @@ Microsoft.OSTCExtensions VMAccessForLinux - 1.5.11 + 1.5.12 VmRole