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:
|
|
26 qemu_control = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
|
|
27 qemu_control.bind('qemucontrol.sock')
|
|
28 qemu_control.listen(0)
|
|
29
|
|
30 # Listen to the serial output:
|
|
31 qemu_serial = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
|
|
32 qemu_serial.bind('qemuserial.sock')
|
|
33 qemu_serial.listen(0)
|
|
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']
|
|
39 p = subprocess.Popen(args)
|
|
40 #stdout=subprocess.DEVNULL,
|
|
41 #stderr=subprocess.DEVNULL)
|
355
|
42
|
|
43 # Give process some time to boot:
|
364
|
44 qemu_serial, address_peer = qemu_serial.accept()
|
|
45 qemu_control, address_peer = qemu_control.accept()
|
|
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'))
|
364
|
62 try:
|
|
63 p.wait(timeout=3)
|
|
64 except subprocess.TimeoutExpired:
|
|
65 p.kill()
|
|
66 print(p.communicate())
|
355
|
67 qemu_control.close()
|
|
68 qemu_serial.close()
|
|
69
|
364
|
70 try:
|
|
71 os.remove('qemucontrol.sock')
|
|
72 except FileNotFoundError:
|
|
73 pass
|
|
74
|
|
75 try:
|
|
76 os.remove('qemuserial.sock')
|
|
77 except FileNotFoundError:
|
|
78 pass
|
|
79
|
355
|
80 # Check that output was correct:
|
|
81 return data
|
|
82
|
338
|
83
|
|
84 class EmulationTestCase(ZccBaseTestCase):
|
|
85 """ Tests the compiler driver """
|
|
86 def setUp(self):
|
|
87 if 'TESTEMU' not in os.environ:
|
|
88 self.skipTest('Not running emulation tests')
|
|
89
|
|
90 def testM3Bare(self):
|
|
91 """ Build bare m3 binary and emulate it """
|
|
92 recipe = os.path.join(testdir, 'm3_bare', 'recipe.yaml')
|
|
93 self.buildRecipe(recipe)
|
355
|
94 data = runQemu('m3_bare/bare.bin')
|
346
|
95 self.assertEqual('Hello worle', data)
|
|
96
|
|
97 def testA9Bare(self):
|
|
98 """ Build vexpress cortex-A9 binary and emulate it """
|
|
99 recipe = os.path.join(testdir, '..', 'examples', 'qemu_a9_hello', 'recipe.yaml')
|
|
100 self.buildRecipe(recipe)
|
355
|
101 data = runQemu('../examples/qemu_a9_hello/hello.bin', machine='vexpress-a9')
|
346
|
102 self.assertEqual('Hello worle', data)
|
338
|
103
|
352
|
104 def testKernelVexpressA9(self):
|
|
105 """ Build vexpress cortex-A9 binary and emulate it """
|
|
106 recipe = os.path.join(testdir, '..', 'kernel', 'arm.yaml')
|
|
107 self.buildRecipe(recipe)
|
355
|
108 data = runQemu('../kernel/kernel_arm.bin', machine='vexpress-a9')
|
361
|
109 self.assertEqual('Welcome to lcfos!', data[:17])
|
352
|
110
|
338
|
111
|
|
112 if __name__ == '__main__':
|
|
113 unittest.main()
|