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