Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 | # SPDX-License-Identifier: GPL-2.0+ # Copyright (c) 2025, Canonical Ltd. """Test U-Boot library functionality""" import os import subprocess import pytest import utils def check_output(out): """Check output from the ulib test""" assert 'Hello, world from ub_printf' in out assert '- U-Boot' in out assert 'Uses libc printf before ulib_init' in out assert 'another printf()' in out @pytest.mark.buildconfigspec("ulib") def test_ulib_shared(ubman): """Test the ulib shared library test program""" build = ubman.config.build_dir prog = os.path.join(build, 'test', 'ulib', 'ulib_test') # Skip test if ulib_test doesn't exist (clang) if not os.path.exists(prog): pytest.skip('ulib_test not found - library build may be disabled') out = utils.run_and_log(ubman, [prog], cwd=build) check_output(out) assert 'dynamically linked' in out @pytest.mark.boardspec('sandbox') def test_ulib_static(ubman): """Test the ulib static library test program""" build = ubman.config.build_dir prog = os.path.join(build, 'test', 'ulib', 'ulib_test_static') # Skip test if ulib_test_static doesn't exist (clang) if not os.path.exists(prog): pytest.skip('ulib_test_static not found - library build may be disabled') out = utils.run_and_log(ubman, [prog]) check_output(out) assert 'statically linked' in out def check_demo_output(ubman, out): """Check output from the ulib demo programs exactly line by line""" lines = out.split('\n') # Read the actual system version from /proc/version with open('/proc/version', 'r', encoding='utf-8') as f: proc_version = f.read().strip() expected = [ 'U-Boot Library Demo Helper\r', '==========================\r', 'System version:helper: Adding 42 + 13 = 55\r', '=================================\r', 'Demo complete\r', f'U-Boot version: {ubman.u_boot_version_string}', '', f' {proc_version}', '', 'Read 1 line(s) using U-Boot library functions.', 'Helper function result: 55', '' ] assert len(lines) == len(expected), \ f"Expected {len(expected)} lines, got {len(lines)}" for i, expected in enumerate(expected): # Exact match for all other lines assert lines[i] == expected, \ f"Line {i}: expected '{expected}', got '{lines[i]}'" def check_rust_demo_output(_ubman, out): """Check output from the Rust ulib demo programs exactly line by line""" lines = out.split('\n') # Read the actual system version from /proc/version with open('/proc/version', 'r', encoding='utf-8') as f: proc_version = f.read().strip() # Check individual parts of the output assert len(lines) == 13, f"Expected 13 lines, got {len(lines)}" assert lines[0] == 'U-Boot Library Demo Helper\r' assert lines[1] == '==========================\r' assert lines[2].startswith('U-Boot version: ') and lines[2].endswith('\r') assert lines[3] == '\r' assert lines[4] == 'System version:\r' assert lines[5] == f' {proc_version}\r' assert lines[6] == '\r' assert lines[7] == 'Read 1 line(s) using U-Boot library functions.\r' assert lines[8] == 'helper: Adding 42 + 13 = 55\r' assert lines[9] == 'Helper function result: 55\r' assert lines[10] == '=================================\r' assert lines[11] == 'Demo complete (hi from rust)\r' assert lines[12] == '' @pytest.mark.boardspec('sandbox') def test_ulib_demos(ubman): """Test both ulib demo programs (dynamic and static).""" build = ubman.config.build_dir src = ubman.config.source_dir examples = os.path.join(src, 'examples', 'ulib') test_program = os.path.join(build, 'test', 'ulib', 'ulib_test') # Skip test if ulib_test doesn't exist (clang) if not os.path.exists(test_program): pytest.skip('ulib_test not found - library build may be disabled') # Build the demo programs - clean first to ensure fresh build, since this # test is run in the source directory cmd = ['make', 'clean'] utils.run_and_log(ubman, cmd, cwd=examples) cmd = ['make', f'UBOOT_BUILD={os.path.abspath(build)}', f'srctree={src}'] utils.run_and_log(ubman, cmd, cwd=examples) # Test static demo program demo_static = os.path.join(examples, 'demo_static') out_static = utils.run_and_log(ubman, [demo_static]) check_demo_output(ubman, out_static) # Test dynamic demo program (with proper LD_LIBRARY_PATH) demo = os.path.join(examples, 'demo') env = os.environ.copy() env['LD_LIBRARY_PATH'] = os.path.abspath(build) out_dynamic = utils.run_and_log(ubman, [demo], env=env) check_demo_output(ubman, out_dynamic) @pytest.mark.boardspec('sandbox') def test_ulib_rust_demos(ubman): """Test both Rust ulib demo programs (dynamic and static).""" build = ubman.config.build_dir src = ubman.config.source_dir examples = os.path.join(src, 'examples', 'rust') test_program = os.path.join(build, 'test', 'ulib', 'ulib_test') # Skip test if ulib_test doesn't exist (clang) if not os.path.exists(test_program): pytest.skip('ulib_test not found - library build may be disabled') # Check if cargo is available try: utils.run_and_log(ubman, ['cargo', '--version']) except (subprocess.CalledProcessError, FileNotFoundError): pytest.skip('cargo not found - Rust toolchain required') # Build the Rust demo programs - clean first to ensure fresh build cmd = ['make', 'clean'] utils.run_and_log(ubman, cmd, cwd=examples) cmd = ['make', f'UBOOT_BUILD={os.path.abspath(build)}', f'srctree={src}'] utils.run_and_log(ubman, cmd, cwd=examples) # Test static demo program demo_static = os.path.join(examples, 'demo_static') out_static = utils.run_and_log(ubman, [demo_static]) check_rust_demo_output(ubman, out_static) # Test dynamic demo program (with proper LD_LIBRARY_PATH) demo = os.path.join(examples, 'demo') env = os.environ.copy() env['LD_LIBRARY_PATH'] = os.path.abspath(build) out_dynamic = utils.run_and_log(ubman, [demo], env=env) check_rust_demo_output(ubman, out_dynamic) @pytest.mark.boardspec('sandbox') def test_ulib_api_header(ubman): """Test that the u-boot-api.h header is generated correctly.""" hdr = os.path.join(ubman.config.build_dir, 'include', 'u-boot-api.h') # Skip if header doesn't exist (clang) if not os.path.exists(hdr): pytest.skip('u-boot-api.h not found - library build may be disabled') # Read and verify header content with open(hdr, 'r', encoding='utf-8') as inf: out = inf.read() # Check header guard assert '#ifndef __ULIB_API_H' in out assert '#define __ULIB_API_H' in out assert '#endif /* __ULIB_API_H */' in out # Check required includes assert '#include <stdarg.h>' in out assert '#include <stddef.h>' in out # Check for renamed function declarations assert 'ub_printf' in out assert 'ub_snprintf' in out assert 'ub_vprintf' in out # Check that functions have proper signatures assert 'ub_printf(const char *fmt, ...)' in out assert 'ub_snprintf(char *buf, size_t size, const char *fmt, ...)' in out assert 'ub_vprintf(const char *fmt, va_list args)' in out |