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