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