test_vmexec.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. from unittest import TestCase
  2. from unittest.mock import patch, call
  3. from qubesagent.vmexec import main, decode, DecodeError
  4. class TestVmExec(TestCase):
  5. def test_00_decode_simple(self):
  6. self.assertEqual(decode('echo+Hello'), [b'echo', b'Hello'])
  7. def test_01_decode_empty(self):
  8. self.assertEqual(decode('echo+'), [b'echo', b''])
  9. def test_02_decode_escaping(self):
  10. self.assertEqual(decode('echo+Hello-20world'),
  11. [b'echo', b'Hello world'])
  12. self.assertEqual(decode('-0A-0D'),
  13. [b'\n\r'])
  14. self.assertEqual(decode('-2Fbin-2Fls'),
  15. [b'/bin/ls'])
  16. self.assertEqual(decode('ls+--la'),
  17. [b'ls', b'-la'])
  18. self.assertEqual(decode('ls+---61'),
  19. [b'ls', b'-a'])
  20. self.assertEqual(decode('ls+----help'),
  21. [b'ls', b'--help'])
  22. def test_03_decode_errors(self):
  23. with self.assertRaises(DecodeError):
  24. decode('illegal/slash')
  25. with self.assertRaises(DecodeError):
  26. decode('illegal-singledash')
  27. with self.assertRaises(DecodeError):
  28. decode('smalletters-0a-0d')
  29. with self.assertRaises(DecodeError):
  30. decode('incompletebyte-A')
  31. with self.assertRaises(DecodeError):
  32. decode('incomplete-Abyte')
  33. with self.assertRaises(DecodeError):
  34. decode('ls+---threeslashes')
  35. def test_10_main_exec(self):
  36. with patch('os.execvp') as mock_execvp:
  37. main(['vmexec', 'ls+--la'])
  38. self.assertEqual(mock_execvp.call_args_list, [
  39. call(b'ls', [b'ls', b'-la'])])
  40. def test_11_main_fail(self):
  41. with self.assertRaises(SystemExit):
  42. main(['vmexec'])
  43. with self.assertRaises(SystemExit):
  44. main(['vmexec', 'illegal/slash'])