Skip to content

Commit efa8050

Browse files
committed
Add unit test
1 parent 1b31de8 commit efa8050

File tree

1 file changed

+120
-0
lines changed

1 file changed

+120
-0
lines changed
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
#!/usr/bin/env python3
2+
# Copyright (c) 2019 Arm Limited and Contributors. All rights reserved.
3+
#
4+
# SPDX-License-Identifier: Apache-2.0
5+
6+
"""Pytest for testing elf-float-checker."""
7+
8+
import importlib
9+
import mock
10+
import subprocess
11+
from pathlib import Path
12+
13+
14+
TARGET = importlib.import_module("elf-float-checker")
15+
16+
SYMBOL_TABLE_NO_FLOATS = (
17+
" Symbol table '.symtab' contains 2723 entries:\n"
18+
"Num: Value Size Type Bind Vis Ndx Name\n"
19+
" 0: 00000000 0 NOTYPE LOCAL DEFAULT UND \n"
20+
" 1: 000045fd 16 FUNC GLOBAL HIDDEN 3 lp_ticker_clear_interrupt\n"
21+
" 2: 00004609 16 FUNC GLOBAL HIDDEN 3 __aeabi_uwrite4\n"
22+
" 3: 00004615 16 FUNC GLOBAL HIDDEN 3 lp_ticker_fire_interrupt\n"
23+
" 4: 00004625 36 FUNC GLOBAL HIDDEN 3 lp_ticker_free\n"
24+
" 5: 00004645 8 FUNC GLOBAL HIDDEN 3 lp_ticker_get_info\n"
25+
" 6: 0000464d 116 FUNC GLOBAL HIDDEN 3 __aeabi_lasr\n"
26+
" 7: 000046bd 20 FUNC GLOBAL HIDDEN 3 lp_ticker_irq_handler\n"
27+
" 8: 000046d1 16 FUNC GLOBAL HIDDEN 3 lp_ticker_read\n"
28+
" 9: 000046e1 52 FUNC GLOBAL HIDDEN 3 __aeabi_lmul\n"
29+
)
30+
31+
SYMBOL_TABLE_FLOATS = (
32+
" Symbol table '.symtab' contains 2723 entries:\n"
33+
"Num: Value Size Type Bind Vis Ndx Name\n"
34+
" 0: 00000000 0 NOTYPE LOCAL DEFAULT UND \n"
35+
" 1: 000045fd 16 FUNC GLOBAL HIDDEN 3 __aeabi_cdcmpeq\n"
36+
" 2: 00004609 16 FUNC GLOBAL HIDDEN 3 lp_ticker_disable_interrupt\n"
37+
" 3: 00004615 16 FUNC GLOBAL HIDDEN 3 __aeabi_cfcmpeq\n"
38+
" 4: 00004625 36 FUNC GLOBAL HIDDEN 3 __aeabi_f2iz\n"
39+
" 5: 00004645 8 FUNC GLOBAL HIDDEN 3 lp_ticker_get_info\n"
40+
" 6: 0000464d 116 FUNC GLOBAL HIDDEN 3 __aeabi_h2f_alt\n"
41+
" 7: 000046bd 20 FUNC GLOBAL HIDDEN 3 lp_ticker_irq_handler\n"
42+
" 8: 000046d1 16 FUNC GLOBAL HIDDEN 3 __aeabi_i2d\n"
43+
" 9: 000046e1 52 FUNC GLOBAL HIDDEN 3 __aeabi_d2iz\n"
44+
" 10: 000046f1 52 FUNC GLOBAL HIDDEN 3 __aeabi_i2f\n"
45+
)
46+
47+
FLOAT_SYMBOLS_IN_TABLE = [
48+
"__aeabi_cdcmpeq",
49+
"__aeabi_cfcmpeq",
50+
"__aeabi_f2iz",
51+
"__aeabi_h2f_alt",
52+
"__aeabi_i2d",
53+
"__aeabi_d2iz",
54+
"__aeabi_i2f",
55+
]
56+
57+
ELF_FORMAT_FILE = "mbed-os-example.elf"
58+
READELF_CMD = ["arm-none-eabi-readelf", "--symbols", "mbed-os-example.elf"]
59+
60+
61+
class TestElfFloatChecker:
62+
"""Test class"""
63+
64+
@classmethod
65+
def setup_class(cls):
66+
# create a dummy elf file
67+
Path(ELF_FORMAT_FILE).touch()
68+
69+
@classmethod
70+
def teardown_class(cls):
71+
# remove the dummy elf file
72+
Path(ELF_FORMAT_FILE).unlink()
73+
74+
@mock.patch("subprocess.run")
75+
def test_correctly_detect_absence_of_float_symbols(
76+
self, mock_subprocess_run
77+
):
78+
"""Test that no false positive occur."""
79+
mock_subprocess_run.return_value = subprocess.CompletedProcess(
80+
args=(
81+
"arm-none-eabi-readelf --symbols mbed-os-example.elf',"
82+
" check=True, stderr=-2, stdin=None, stdout=-1"
83+
),
84+
returncode=0,
85+
stdout=SYMBOL_TABLE_NO_FLOATS.encode(),
86+
stderr=None,
87+
)
88+
assert [] == TARGET.check_float_symbols(ELF_FORMAT_FILE)
89+
mock_subprocess_run.assert_called_with(
90+
READELF_CMD,
91+
check=True,
92+
stdin=None,
93+
stdout=subprocess.PIPE,
94+
stderr=subprocess.STDOUT,
95+
)
96+
97+
@mock.patch("subprocess.run")
98+
def test_correctly_detect_presence_of_float_symbols(
99+
self, mock_subprocess_run
100+
):
101+
"""Test that float symbols can be discovered in a symbol table."""
102+
mock_subprocess_run.return_value = subprocess.CompletedProcess(
103+
args=(
104+
"arm-none-eabi-readelf --symbols mbed-os-example.elf',"
105+
" check=True, stderr=-2, stdin=None, stdout=-1"
106+
),
107+
returncode=0,
108+
stdout=SYMBOL_TABLE_FLOATS.encode(),
109+
stderr=None,
110+
)
111+
assert FLOAT_SYMBOLS_IN_TABLE == TARGET.check_float_symbols(
112+
ELF_FORMAT_FILE
113+
)
114+
mock_subprocess_run.assert_called_with(
115+
READELF_CMD,
116+
check=True,
117+
stdin=None,
118+
stdout=subprocess.PIPE,
119+
stderr=subprocess.STDOUT,
120+
)

0 commit comments

Comments
 (0)