1# SPDX-License-Identifier: GPL-2.0+
2# Copyright (c) 2025
3#
4# Test that mkimage validates image references in configurations
5
6import os
7import subprocess
8import pytest
9import fit_util
10import re
11
12@pytest.mark.boardspec('sandbox')
13@pytest.mark.requiredtool('dtc')
14def test_fit_invalid_image_reference(ubman):
15    """Test that mkimage fails when configuration references a missing image"""
16
17    its_fname = fit_util.make_fname(ubman, "invalid.its")
18    itb_fname = fit_util.make_fname(ubman, "invalid.itb")
19    kernel = fit_util.make_kernel(ubman, 'kernel.bin', 'kernel')
20
21    # Write ITS with an invalid reference to a nonexistent image
22    its_text = '''
23/dts-v1/;
24
25/ {
26    images {
27        kernel@1 {
28            description = "Test Kernel";
29            data = /incbin/("kernel.bin");
30            type = "kernel";
31            arch = "sandbox";
32            os = "linux";
33            compression = "none";
34            load = <0x40000>;
35            entry = <0x40000>;
36        };
37    };
38
39    configurations {
40        default = "conf@1";
41        conf@1 {
42            kernel = "kernel@1";
43            fdt = "notexist";
44        };
45    };
46};
47'''
48
49    with open(its_fname, 'w') as f:
50        f.write(its_text)
51
52    mkimage = os.path.join(ubman.config.build_dir, 'tools/mkimage')
53    cmd = [mkimage, '-f', its_fname, itb_fname]
54
55    result = subprocess.run(cmd, capture_output=True, text=True)
56
57    assert result.returncode != 0, "mkimage should fail due to missing image reference"
58    assert "references undefined image 'notexist'" in result.stderr
59
60@pytest.mark.boardspec('sandbox')
61@pytest.mark.requiredtool('dtc')
62def test_fit_invalid_default_config(ubman):
63    """Test that mkimage fails when default config is missing"""
64
65    its_fname = fit_util.make_fname(ubman, "invalid.its")
66    itb_fname = fit_util.make_fname(ubman, "invalid.itb")
67    kernel = fit_util.make_kernel(ubman, 'kernel.bin', 'kernel')
68
69    # Write ITS with an invalid reference to a nonexistent default config
70    its_text = '''
71/dts-v1/;
72
73/ {
74    images {
75        kernel@1 {
76            description = "Test Kernel";
77            data = /incbin/("kernel.bin");
78            type = "kernel";
79            arch = "sandbox";
80            os = "linux";
81            compression = "none";
82            load = <0x40000>;
83            entry = <0x40000>;
84        };
85    };
86
87    configurations {
88        default = "conf@1";
89        conf@2 {
90            kernel = "kernel@1";
91        };
92    };
93};
94'''
95
96    with open(its_fname, 'w') as f:
97        f.write(its_text)
98
99    mkimage = os.path.join(ubman.config.build_dir, 'tools/mkimage')
100    cmd = [mkimage, '-f', its_fname, itb_fname]
101
102    result = subprocess.run(cmd, capture_output=True, text=True)
103
104    assert result.returncode != 0, "mkimage should fail due to missing default config"
105    assert re.search(r"Default configuration '.*' not found under /configurations", result.stderr)
106