summaryrefslogtreecommitdiffstats
path: root/src/boot/efi/test-bcd.c
blob: 3f93ca05c58cb18d6cc8d986adc9ba89db6cf0c2 (plain)
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
/* SPDX-License-Identifier: LGPL-2.1-or-later */

#include "alloc-util.h"
#include "bcd.h"
#include "compress.h"
#include "fileio.h"
#include "tests.h"
#include "utf8.h"

/* Include the implementation directly, so we can poke at some internals. */
#include "bcd.c"

static void load_bcd(const char *path, void **ret_bcd, size_t *ret_bcd_len) {
        size_t len;
        _cleanup_free_ char *fn = NULL, *compressed = NULL;

        assert_se(get_testdata_dir(path, &fn) >= 0);
        assert_se(read_full_file_full(AT_FDCWD, fn, UINT64_MAX, SIZE_MAX, 0, NULL, &compressed, &len) >= 0);
        assert_se(decompress_blob_zstd(compressed, len, ret_bcd, ret_bcd_len, SIZE_MAX) >= 0);
}

static void test_get_bcd_title_one(
                const char *path,
                const char16_t *title_expect,
                size_t title_len_expect) {

        size_t len;
        _cleanup_free_ void *bcd = NULL;

        log_info("/* %s(%s) */", __func__, path);

        load_bcd(path, &bcd, &len);

        char16_t *title = get_bcd_title(bcd, len);
        if (title_expect) {
                assert_se(title);
                assert_se(memcmp(title, title_expect, title_len_expect) == 0);
        } else
                assert_se(!title);
}

TEST(get_bcd_title) {
        test_get_bcd_title_one("test-bcd/win10.bcd.zst", u"Windows 10", sizeof(u"Windows 10"));

        test_get_bcd_title_one("test-bcd/description-bad-type.bcd.zst", NULL, 0);
        test_get_bcd_title_one("test-bcd/description-empty.bcd.zst", NULL, 0);
        test_get_bcd_title_one("test-bcd/description-missing.bcd.zst", NULL, 0);
        test_get_bcd_title_one("test-bcd/description-too-small.bcd.zst", NULL, 0);
        test_get_bcd_title_one("test-bcd/displayorder-bad-name.bcd.zst", NULL, 0);
        test_get_bcd_title_one("test-bcd/displayorder-bad-size.bcd.zst", NULL, 0);
        test_get_bcd_title_one("test-bcd/displayorder-bad-type.bcd.zst", NULL, 0);
        test_get_bcd_title_one("test-bcd/empty.bcd.zst", NULL, 0);
}

TEST(base_block) {
        size_t len;
        BaseBlock backup;
        uint8_t *bcd_base;
        _cleanup_free_ BaseBlock *bcd = NULL;

        load_bcd("test-bcd/win10.bcd.zst", (void **) &bcd, &len);
        backup = *bcd;
        bcd_base = (uint8_t *) bcd;

        assert_se(get_bcd_title(bcd_base, len));

        /* Try various "corruptions" of the base block. */

        assert_se(!get_bcd_title(bcd_base, sizeof(BaseBlock) - 1));

        bcd->sig = 0;
        assert_se(!get_bcd_title(bcd_base, len));
        *bcd = backup;

        bcd->version_minor = 2;
        assert_se(!get_bcd_title(bcd_base, len));
        *bcd = backup;

        bcd->version_major = 4;
        assert_se(!get_bcd_title(bcd_base, len));
        *bcd = backup;

        bcd->type = 1;
        assert_se(!get_bcd_title(bcd_base, len));
        *bcd = backup;

        bcd->primary_seqnum++;
        assert_se(!get_bcd_title(bcd_base, len));
        *bcd = backup;
}

TEST(bad_bcd) {
        size_t len;
        uint8_t *hbins;
        uint32_t offset;
        _cleanup_free_ void *bcd = NULL;

        /* This BCD hive has been manipulated to have bad offsets/sizes at various places. */
        load_bcd("test-bcd/corrupt.bcd.zst", &bcd, &len);

        assert_se(len >= HIVE_CELL_OFFSET);
        hbins = (uint8_t *) bcd + HIVE_CELL_OFFSET;
        len -= HIVE_CELL_OFFSET;
        offset = ((BaseBlock *) bcd)->root_cell_offset;

        const Key *root = get_key(hbins, len, offset, "\0");
        assert_se(root);
        assert_se(!get_key(hbins, sizeof(Key) - 1, offset, "\0"));

        assert_se(!get_key(hbins, len, offset, "\0BadOffset\0"));
        assert_se(!get_key(hbins, len, offset, "\0BadSig\0"));
        assert_se(!get_key(hbins, len, offset, "\0BadKeyNameLen\0"));
        assert_se(!get_key(hbins, len, offset, "\0SubkeyBadOffset\0Dummy\0"));
        assert_se(!get_key(hbins, len, offset, "\0SubkeyBadSig\0Dummy\0"));
        assert_se(!get_key(hbins, len, offset, "\0SubkeyBadNEntries\0Dummy\0"));

        assert_se(!get_key_value(hbins, len, root, "Dummy"));

        const Key *kv_bad_offset = get_key(hbins, len, offset, "\0KeyValuesBadOffset\0");
        assert_se(kv_bad_offset);
        assert_se(!get_key_value(hbins, len, kv_bad_offset, "Dummy"));

        const Key *kv_bad_n_key_values = get_key(hbins, len, offset, "\0KeyValuesBadNKeyValues\0");
        assert_se(kv_bad_n_key_values);
        assert_se(!get_key_value(hbins, len, kv_bad_n_key_values, "Dummy"));

        const Key *kv = get_key(hbins, len, offset, "\0KeyValues\0");
        assert_se(kv);

        assert_se(!get_key_value(hbins, len, kv, "BadOffset"));
        assert_se(!get_key_value(hbins, len, kv, "BadSig"));
        assert_se(!get_key_value(hbins, len, kv, "BadNameLen"));
        assert_se(!get_key_value(hbins, len, kv, "InlineData"));
        assert_se(!get_key_value(hbins, len, kv, "BadDataOffset"));
        assert_se(!get_key_value(hbins, len, kv, "BadDataSize"));
}

TEST(argv_bcds) {
        for (int i = 1; i < saved_argc; i++) {
                size_t len;
                _cleanup_free_ void *bcd = NULL;

                assert_se(read_full_file_full(
                        AT_FDCWD,
                        saved_argv[i],
                        UINT64_MAX,
                        SIZE_MAX,
                        0,
                        NULL,
                        (char **) &bcd,
                        &len) >= 0);

                char16_t *title = get_bcd_title(bcd, len);
                if (title) {
                        _cleanup_free_ char *title_utf8 = utf16_to_utf8(title, SIZE_MAX);
                        log_info("%s: \"%s\"", saved_argv[i], title_utf8);
                } else
                        log_info("%s: Bad BCD", saved_argv[i]);
        }
}

DEFINE_TEST_MAIN(LOG_INFO);