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
|
// SPDX-License-Identifier: GPL-2.0+
/*
* Tests for driver-model hash-provider selection
*
* Copyright (C) 2026 James Hilliard
*/
#include <dm.h>
#include <dm/device-internal.h>
#include <dm/root.h>
#include <dm/test.h>
#include <dm/uclass-internal.h>
#include <u-boot/hash.h>
#include <test/test.h>
#include <test/ut.h>
static int unsupported_calls;
static int success_calls;
static int hard_error_calls;
static int hash_test_unsupported(struct udevice *dev, enum HASH_ALGO algo,
const void *ibuf, const uint32_t ilen,
void *obuf, uint32_t chunk_sz)
{
unsupported_calls++;
return -EOPNOTSUPP;
}
static int hash_test_success(struct udevice *dev, enum HASH_ALGO algo,
const void *ibuf, const uint32_t ilen,
void *obuf, uint32_t chunk_sz)
{
ssize_t digest_size;
success_calls++;
digest_size = hash_algo_digest_size(algo);
if (digest_size < 0)
return digest_size;
memset(obuf, 0x5a, digest_size);
return 0;
}
static int hash_test_hard_error(struct udevice *dev, enum HASH_ALGO algo,
const void *ibuf, const uint32_t ilen,
void *obuf, uint32_t chunk_sz)
{
hard_error_calls++;
return -EINVAL;
}
static const struct hash_ops hash_test_unsupported_ops = {
.hash_digest_wd = hash_test_unsupported,
};
static const struct hash_ops hash_test_success_ops = {
.hash_digest_wd = hash_test_success,
};
static const struct hash_ops hash_test_hard_error_ops = {
.hash_digest_wd = hash_test_hard_error,
};
U_BOOT_DRIVER(hash_test_unsupported_drv) = {
.name = "hash_test_unsupported",
.id = UCLASS_HASH,
.ops = &hash_test_unsupported_ops,
};
U_BOOT_DRIVER(hash_test_success_drv) = {
.name = "hash_test_success",
.id = UCLASS_HASH,
.ops = &hash_test_success_ops,
};
U_BOOT_DRIVER(hash_test_hard_error_drv) = {
.name = "hash_test_hard_error",
.id = UCLASS_HASH,
.ops = &hash_test_hard_error_ops,
};
static int hash_test_unbind_all(void)
{
struct udevice *dev;
int ret;
for (;;) {
ret = uclass_find_first_device(UCLASS_HASH, &dev);
if (ret || !dev)
return ret;
if (device_active(dev)) {
ret = device_remove(dev, DM_REMOVE_NORMAL);
if (ret)
return ret;
}
ret = device_unbind(dev);
if (ret)
return ret;
}
}
static int hash_test_bind(const struct driver *drv, const char *name)
{
struct udevice *dev;
return device_bind(dm_root(), drv, name, 0, ofnode_null(), &dev);
}
static int dm_test_hash_provider_selection(struct unit_test_state *uts)
{
u8 digest[32];
int ret;
ut_assertok(hash_test_unbind_all());
ut_assertok(hash_test_bind(DM_DRIVER_GET(hash_test_unsupported_drv),
"hash-unsupported"));
ut_assertok(hash_test_bind(DM_DRIVER_GET(hash_test_success_drv),
"hash-success"));
unsupported_calls = 0;
success_calls = 0;
memset(digest, 0, sizeof(digest));
ret = hash_digest_wd_lookup(HASH_ALGO_SHA256, "test", 4, digest, 4);
ut_assertok(ret);
ut_asserteq(1, unsupported_calls);
ut_asserteq(1, success_calls);
for (int i = 0; i < sizeof(digest); i++)
ut_asserteq(0x5a, digest[i]);
memset(digest, 0, sizeof(digest));
ret = hash_digest_wd_lookup(HASH_ALGO_INVALID, "test", 4, digest, 4);
ut_asserteq(-EINVAL, ret);
ut_asserteq(2, unsupported_calls);
ut_asserteq(2, success_calls);
for (int i = 0; i < sizeof(digest); i++)
ut_asserteq(0, digest[i]);
ut_assertok(hash_test_unbind_all());
ut_assertok(hash_test_bind(DM_DRIVER_GET(hash_test_hard_error_drv),
"hash-hard-error"));
ut_assertok(hash_test_bind(DM_DRIVER_GET(hash_test_success_drv),
"hash-success"));
hard_error_calls = 0;
success_calls = 0;
ret = hash_digest_wd_lookup(HASH_ALGO_SHA256, "test", 4, digest, 4);
ut_asserteq(-EINVAL, ret);
ut_asserteq(1, hard_error_calls);
ut_asserteq(0, success_calls);
return 0;
}
DM_TEST(dm_test_hash_provider_selection, UTF_SCAN_FDT);
|