Skip to content

Commit 29badb8

Browse files
committed
Add unit test
1 parent 1b31de8 commit 29badb8

File tree

1 file changed

+122
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)