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'):
|
|
14 """ Runs qemu on a given kernel file on some machine """
|
|
15 args = ['qemu-system-arm', '-M', machine, '-m', '16M',
|
|
16 '-nographic', '-kernel', kernel, '-monitor',
|
|
17 'unix:qemucontrol.sock,server',
|
|
18 '-serial', 'unix:qemuserial.sock,server']
|
|
19 p = subprocess.Popen(args, stdout=subprocess.DEVNULL,
|
|
20 stderr=subprocess.DEVNULL)
|
|
21
|
|
22 # Give process some time to boot:
|
|
23 time.sleep(0.5)
|
|
24
|
|
25 # Connect to the control socket:
|
|
26 qemu_control = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
|
|
27 qemu_control.connect('qemucontrol.sock')
|
|
28
|
|
29 time.sleep(0.5)
|
|
30
|
|
31 # Now connect to the serial output:
|
|
32 qemu_serial = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
|
|
33 qemu_serial.connect('qemuserial.sock')
|
|
34
|
|
35 time.sleep(0.5)
|
|
36
|
|
37 qemu_serial.settimeout(0.2)
|
|
38
|
|
39 # Receive all data:
|
|
40 data = bytearray()
|
|
41 for i in range(40):
|
|
42 try:
|
|
43 data += qemu_serial.recv(1)
|
|
44 except socket.timeout as e:
|
|
45 break
|
|
46 data = data.decode('ascii')
|
|
47 # print(data)
|
|
48
|
|
49 # Send quit command:
|
|
50 qemu_control.send("quit\n".encode('ascii'))
|
|
51 p.wait(timeout=3)
|
|
52 qemu_control.close()
|
|
53 qemu_serial.close()
|
|
54
|
|
55 # Check that output was correct:
|
|
56 return data
|
|
57
|
338
|
58
|
|
59 class EmulationTestCase(ZccBaseTestCase):
|
|
60 """ Tests the compiler driver """
|
|
61 def setUp(self):
|
|
62 if 'TESTEMU' not in os.environ:
|
|
63 self.skipTest('Not running emulation tests')
|
|
64
|
|
65 def testM3Bare(self):
|
|
66 """ Build bare m3 binary and emulate it """
|
|
67 recipe = os.path.join(testdir, 'm3_bare', 'recipe.yaml')
|
|
68 self.buildRecipe(recipe)
|
355
|
69 data = runQemu('m3_bare/bare.bin')
|
346
|
70 self.assertEqual('Hello worle', data)
|
|
71
|
|
72 def testA9Bare(self):
|
|
73 """ Build vexpress cortex-A9 binary and emulate it """
|
|
74 recipe = os.path.join(testdir, '..', 'examples', 'qemu_a9_hello', 'recipe.yaml')
|
|
75 self.buildRecipe(recipe)
|
355
|
76 data = runQemu('../examples/qemu_a9_hello/hello.bin', machine='vexpress-a9')
|
346
|
77 self.assertEqual('Hello worle', data)
|
338
|
78
|
352
|
79 def testKernelVexpressA9(self):
|
|
80 """ Build vexpress cortex-A9 binary and emulate it """
|
|
81 recipe = os.path.join(testdir, '..', 'kernel', 'arm.yaml')
|
|
82 self.buildRecipe(recipe)
|
355
|
83 data = runQemu('../kernel/kernel_arm.bin', machine='vexpress-a9')
|
361
|
84 self.assertEqual('Welcome to lcfos!', data[:17])
|
352
|
85
|
338
|
86
|
|
87 if __name__ == '__main__':
|
|
88 unittest.main()
|