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
|
355
|
13 def runQemu(kernel, machine='lm3s811evb'):
|
364
|
14 """ Runs qemu on a given kernel file """
|
|
15 try:
|
|
16 os.remove('qemucontrol.sock')
|
|
17 except FileNotFoundError:
|
|
18 pass
|
|
19
|
|
20 try:
|
|
21 os.remove('qemuserial.sock')
|
|
22 except FileNotFoundError:
|
|
23 pass
|
|
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']
|
366
|
39 p = subprocess.Popen(args,
|
|
40 stdout=subprocess.DEVNULL,
|
|
41 stderr=subprocess.DEVNULL)
|
355
|
42
|
|
43 # Give process some time to boot:
|
366
|
44 qemu_serial, address_peer = qemu_serial_serve.accept()
|
|
45 qemu_control, address_peer = qemu_control_serve.accept()
|
|
46
|
|
47 # Give the go command:
|
364
|
48 qemu_control.send('cont\n'.encode('ascii'))
|
355
|
49
|
|
50 qemu_serial.settimeout(0.2)
|
|
51
|
|
52 # Receive all data:
|
|
53 data = bytearray()
|
364
|
54 for i in range(400):
|
355
|
55 try:
|
|
56 data += qemu_serial.recv(1)
|
|
57 except socket.timeout as e:
|
|
58 break
|
|
59 data = data.decode('ascii')
|
|
60 # print(data)
|
|
61
|
|
62 # Send quit command:
|
|
63 qemu_control.send("quit\n".encode('ascii'))
|
364
|
64 try:
|
|
65 p.wait(timeout=3)
|
|
66 except subprocess.TimeoutExpired:
|
|
67 p.kill()
|
355
|
68 qemu_control.close()
|
|
69 qemu_serial.close()
|
366
|
70 qemu_control_serve.close()
|
|
71 qemu_serial_serve.close()
|
355
|
72
|
364
|
73 try:
|
|
74 os.remove('qemucontrol.sock')
|
|
75 except FileNotFoundError:
|
|
76 pass
|
|
77
|
|
78 try:
|
|
79 os.remove('qemuserial.sock')
|
|
80 except FileNotFoundError:
|
|
81 pass
|
|
82
|
355
|
83 # Check that output was correct:
|
|
84 return data
|
|
85
|
338
|
86
|
|
87 class EmulationTestCase(ZccBaseTestCase):
|
|
88 """ Tests the compiler driver """
|
|
89 def setUp(self):
|
|
90 if 'TESTEMU' not in os.environ:
|
|
91 self.skipTest('Not running emulation tests')
|
|
92
|
|
93 def testM3Bare(self):
|
|
94 """ Build bare m3 binary and emulate it """
|
|
95 recipe = os.path.join(testdir, 'm3_bare', 'recipe.yaml')
|
|
96 self.buildRecipe(recipe)
|
355
|
97 data = runQemu('m3_bare/bare.bin')
|
346
|
98 self.assertEqual('Hello worle', data)
|
|
99
|
|
100 def testA9Bare(self):
|
|
101 """ Build vexpress cortex-A9 binary and emulate it """
|
|
102 recipe = os.path.join(testdir, '..', 'examples', 'qemu_a9_hello', 'recipe.yaml')
|
|
103 self.buildRecipe(recipe)
|
355
|
104 data = runQemu('../examples/qemu_a9_hello/hello.bin', machine='vexpress-a9')
|
346
|
105 self.assertEqual('Hello worle', data)
|
338
|
106
|
352
|
107 def testKernelVexpressA9(self):
|
|
108 """ Build vexpress cortex-A9 binary and emulate it """
|
|
109 recipe = os.path.join(testdir, '..', 'kernel', 'arm.yaml')
|
|
110 self.buildRecipe(recipe)
|
355
|
111 data = runQemu('../kernel/kernel_arm.bin', machine='vexpress-a9')
|
361
|
112 self.assertEqual('Welcome to lcfos!', data[:17])
|
352
|
113
|
338
|
114
|
|
115 if __name__ == '__main__':
|
|
116 unittest.main()
|