338
|
1 import unittest
|
|
2 import os
|
|
3 import sys
|
|
4 import subprocess
|
|
5 import socket
|
|
6 import time
|
|
7
|
|
8 from testzcc import ZccBaseTestCase
|
|
9
|
|
10 # Store testdir for safe switch back to directory:
|
|
11 testdir = os.path.dirname(os.path.abspath(__file__))
|
|
12
|
369
|
13 def tryrm(fn):
|
|
14 try:
|
|
15 os.remove(fn)
|
|
16 except OSError:
|
|
17 pass
|
|
18
|
355
|
19 def runQemu(kernel, machine='lm3s811evb'):
|
364
|
20 """ Runs qemu on a given kernel file """
|
|
21
|
369
|
22 tryrm('qemucontrol.sock')
|
|
23 tryrm('qemuserial.sock')
|
364
|
24
|
|
25 # Listen to the control socket:
|
366
|
26 qemu_control_serve = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
|
|
27 qemu_control_serve.bind('qemucontrol.sock')
|
|
28 qemu_control_serve.listen(0)
|
364
|
29
|
|
30 # Listen to the serial output:
|
366
|
31 qemu_serial_serve = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
|
|
32 qemu_serial_serve.bind('qemuserial.sock')
|
|
33 qemu_serial_serve.listen(0)
|
364
|
34
|
355
|
35 args = ['qemu-system-arm', '-M', machine, '-m', '16M',
|
|
36 '-nographic', '-kernel', kernel, '-monitor',
|
364
|
37 'unix:qemucontrol.sock',
|
|
38 '-serial', 'unix:qemuserial.sock', '-S']
|
370
|
39 p = subprocess.Popen(args)
|
355
|
40
|
|
41 # Give process some time to boot:
|
366
|
42 qemu_serial, address_peer = qemu_serial_serve.accept()
|
|
43 qemu_control, address_peer = qemu_control_serve.accept()
|
|
44
|
|
45 # Give the go command:
|
364
|
46 qemu_control.send('cont\n'.encode('ascii'))
|
355
|
47
|
|
48 qemu_serial.settimeout(0.2)
|
|
49
|
|
50 # Receive all data:
|
|
51 data = bytearray()
|
364
|
52 for i in range(400):
|
355
|
53 try:
|
|
54 data += qemu_serial.recv(1)
|
|
55 except socket.timeout as e:
|
|
56 break
|
|
57 data = data.decode('ascii')
|
|
58 # print(data)
|
|
59
|
|
60 # Send quit command:
|
|
61 qemu_control.send("quit\n".encode('ascii'))
|
371
|
62 if hasattr(subprocess, 'TimeoutExpired'):
|
|
63 try:
|
|
64 p.wait(timeout=3)
|
|
65 except subprocess.TimeoutExpired:
|
|
66 p.kill()
|
|
67 else:
|
|
68 time.sleep(2)
|
364
|
69 p.kill()
|
355
|
70 qemu_control.close()
|
|
71 qemu_serial.close()
|
366
|
72 qemu_control_serve.close()
|
|
73 qemu_serial_serve.close()
|
355
|
74
|
369
|
75 tryrm('qemucontrol.sock')
|
|
76 tryrm('qemuserial.sock')
|
364
|
77
|
355
|
78 # Check that output was correct:
|
|
79 return data
|
|
80
|
338
|
81
|
|
82 class EmulationTestCase(ZccBaseTestCase):
|
|
83 """ Tests the compiler driver """
|
|
84 def setUp(self):
|
|
85 if 'TESTEMU' not in os.environ:
|
|
86 self.skipTest('Not running emulation tests')
|
|
87
|
|
88 def testM3Bare(self):
|
|
89 """ Build bare m3 binary and emulate it """
|
|
90 recipe = os.path.join(testdir, 'm3_bare', 'recipe.yaml')
|
|
91 self.buildRecipe(recipe)
|
355
|
92 data = runQemu('m3_bare/bare.bin')
|
346
|
93 self.assertEqual('Hello worle', data)
|
|
94
|
|
95 def testA9Bare(self):
|
|
96 """ Build vexpress cortex-A9 binary and emulate it """
|
|
97 recipe = os.path.join(testdir, '..', 'examples', 'qemu_a9_hello', 'recipe.yaml')
|
|
98 self.buildRecipe(recipe)
|
355
|
99 data = runQemu('../examples/qemu_a9_hello/hello.bin', machine='vexpress-a9')
|
346
|
100 self.assertEqual('Hello worle', data)
|
338
|
101
|
352
|
102 def testKernelVexpressA9(self):
|
|
103 """ Build vexpress cortex-A9 binary and emulate it """
|
|
104 recipe = os.path.join(testdir, '..', 'kernel', 'arm.yaml')
|
|
105 self.buildRecipe(recipe)
|
355
|
106 data = runQemu('../kernel/kernel_arm.bin', machine='vexpress-a9')
|
361
|
107 self.assertEqual('Welcome to lcfos!', data[:17])
|
352
|
108
|
338
|
109
|
|
110 if __name__ == '__main__':
|
|
111 unittest.main()
|