summaryrefslogtreecommitdiffstats
path: root/third_party/libwebrtc/build/fuchsia/qemu_target_test.py
blob: 44b3802909268534da42f797a3ba34ffd9d9d96c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/usr/bin/python2
# Copyright 2018 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import qemu_target
import shutil
import subprocess
import tempfile
import time
import unittest

TEST_PAYLOAD = "Let's get this payload across the finish line!"

tmpdir = tempfile.mkdtemp()

# Register the target with the context manager so that it always gets
# torn down on process exit. Otherwise there might be lingering QEMU instances
# if Python crashes or is interrupted.
with qemu_target.QemuTarget(tmpdir, 'x64') as target:
  class TestQemuTarget(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
      target.Start()

    @classmethod
    def tearDownClass(cls):
      target.Shutdown()
      shutil.rmtree(tmpdir)

    def testCopyBidirectional(self):
      tmp_path = tmpdir + "/payload"
      with open(tmp_path, "w") as tmpfile:
        tmpfile.write(TEST_PAYLOAD)
      target.PutFile(tmp_path, '/tmp/payload')

      tmp_path_roundtrip = tmp_path + ".roundtrip"
      target.GetFile('/tmp/payload', tmp_path_roundtrip)
      with open(tmp_path_roundtrip) as roundtrip:
        self.assertEqual(TEST_PAYLOAD, roundtrip.read())

    def testRunCommand(self):
      self.assertEqual(0, target.RunCommand(['true']))
      self.assertEqual(1, target.RunCommand(['false']))

    def testRunCommandPiped(self):
      proc = target.RunCommandPiped(['cat'],
                                    stdin=subprocess.PIPE,
                                    stdout=subprocess.PIPE)
      proc.stdin.write(TEST_PAYLOAD)
      proc.stdin.flush()
      proc.stdin.close()
      self.assertEqual(TEST_PAYLOAD, proc.stdout.readline())
      proc.kill()


  if __name__ == '__main__':
    unittest.main()