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'))
|
364
|
62 try:
|
|
63 p.wait(timeout=3)
|
|
64 except subprocess.TimeoutExpired:
|
|
65 p.kill()
|
355
|
66 qemu_control.close()
|
|
67 qemu_serial.close()
|
366
|
68 qemu_control_serve.close()
|
|
69 qemu_serial_serve.close()
|
355
|
70
|
369
|
71 tryrm('qemucontrol.sock')
|
|
72 tryrm('qemuserial.sock')
|
364
|
73
|
355
|
74 # Check that output was correct:
|
|
75 return data
|
|
76
|
338
|
77
|
|
78 class EmulationTestCase(ZccBaseTestCase):
|
|
79 """ Tests the compiler driver """
|
|
80 def setUp(self):
|
|
81 if 'TESTEMU' not in os.environ:
|
|
82 self.skipTest('Not running emulation tests')
|
|
83
|
|
84 def testM3Bare(self):
|
|
85 """ Build bare m3 binary and emulate it """
|
|
86 recipe = os.path.join(testdir, 'm3_bare', 'recipe.yaml')
|
|
87 self.buildRecipe(recipe)
|
355
|
88 data = runQemu('m3_bare/bare.bin')
|
346
|
89 self.assertEqual('Hello worle', data)
|
|
90
|
|
91 def testA9Bare(self):
|
|
92 """ Build vexpress cortex-A9 binary and emulate it """
|
|
93 recipe = os.path.join(testdir, '..', 'examples', 'qemu_a9_hello', 'recipe.yaml')
|
|
94 self.buildRecipe(recipe)
|
355
|
95 data = runQemu('../examples/qemu_a9_hello/hello.bin', machine='vexpress-a9')
|
346
|
96 self.assertEqual('Hello worle', data)
|
338
|
97
|
352
|
98 def testKernelVexpressA9(self):
|
|
99 """ Build vexpress cortex-A9 binary and emulate it """
|
|
100 recipe = os.path.join(testdir, '..', 'kernel', 'arm.yaml')
|
|
101 self.buildRecipe(recipe)
|
355
|
102 data = runQemu('../kernel/kernel_arm.bin', machine='vexpress-a9')
|
361
|
103 self.assertEqual('Welcome to lcfos!', data[:17])
|
352
|
104
|
338
|
105
|
|
106 if __name__ == '__main__':
|
|
107 unittest.main()
|