Skip to content

Commit 91ebdde

Browse files
committed
tests: riscv: pmp: Make permission change tests dynamic
The existing PMP permission tests relied on hardcoded region indices (e.g., index 0 or 2). This makes the tests fragile and dependent on the exact memory attribute region setup. This change refactors the tests to dynamically determine region counts and indices based on the output of `mem_attr_get_regions()`: - `test_pmp_change_perm_invalid_permission`: Uses the last valid region index to test invalid permission flags. - `test_pmp_change_perm_invalid_region_index`: Uses the total number of regions as the out-of-bounds index. - `test_successful_permission_change`: Locates the index of the target test region (`dt_regions[0]`) by matching its base address and size. The test now also inverts the X bit of the current permissions to ensure a change is applied and verified. These changes make the tests more robust and less dependent on a specific static configuration. Signed-off-by: Firas Sammoura <[email protected]>
1 parent 8f0df40 commit 91ebdde

File tree

1 file changed

+35
-11
lines changed
  • tests/arch/riscv/pmp/mem-attr-entries/src

1 file changed

+35
-11
lines changed

tests/arch/riscv/pmp/mem-attr-entries/src/main.c

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -127,33 +127,53 @@ ZTEST(riscv_pmp_memattr_entries, test_dt_pmp_perm_conversion)
127127

128128
ZTEST(riscv_pmp_memattr_entries, test_pmp_change_perm_invalid_permission)
129129
{
130+
const struct mem_attr_region_t *region;
131+
size_t num_regions = mem_attr_get_regions(&region);
132+
size_t idx = num_regions - 1;
133+
130134
/* Invalid permission: 0x08 (bit 3) is outside of R|W|X (0x7) mask */
131-
zassert_equal(z_riscv_pmp_change_permissions(0, 0x08), -EINVAL,
135+
zassert_equal(z_riscv_pmp_change_permissions(idx, 0x08), -EINVAL,
132136
"Should return -EINVAL for invalid permission bits.");
133137
}
134138

135139
ZTEST(riscv_pmp_memattr_entries, test_pmp_change_perm_invalid_region_index)
136140
{
137-
/* region_idx = 2, which is out of bounds */
138-
size_t idx = 2;
141+
const struct mem_attr_region_t *region;
142+
size_t num_regions = mem_attr_get_regions(&region);
143+
size_t invalid_idx = num_regions;
139144

140-
zassert_equal(z_riscv_pmp_change_permissions(idx, PMP_R), -EINVAL,
145+
zassert_equal(z_riscv_pmp_change_permissions(invalid_idx, PMP_R), -EINVAL,
141146
"Should return -EINVAL for region index out of bounds.");
142147
}
143148

144149
ZTEST(riscv_pmp_memattr_entries, test_successful_permission_change)
145150
{
146-
const uint8_t new_perm = 0x03; /* Read (0x1) | Write (0x2) */
147-
const size_t idx = 0;
151+
const struct mem_attr_region_t *region;
152+
size_t num_regions = mem_attr_get_regions(&region);
153+
size_t idx;
154+
155+
/* Find the matching index of memory attribute region for dt_regions[0] */
156+
for (idx = 0; idx < num_regions; ++idx) {
157+
if ((region[idx].dt_addr == dt_regions[0].base) &&
158+
(region[idx].dt_size == dt_regions[0].size)) {
159+
break;
160+
}
161+
}
148162

149-
zassert_not_equal(new_perm, dt_regions[idx].perm,
150-
"Ensure new permission is different from existing permission");
163+
zassert_not_equal(idx, num_regions,
164+
"No matching memory attribute region found for dt_regions[0].");
151165

152-
int ret = z_riscv_pmp_change_permissions(idx, new_perm);
166+
/* Store original permission to revert later if needed, and calculate new permission */
167+
uint8_t original_perm = dt_regions[0].perm;
168+
uint8_t new_perm = PMP_X ^ original_perm; /* Toggle X bit */
153169

154-
dt_regions[idx].perm = new_perm;
170+
/* Update the expectations */
171+
dt_regions[0].perm = new_perm;
155172

156-
zassert_equal(ret, 0, "Function should return 0 on success.");
173+
/* Apply the permission change to the hardware */
174+
int ret = z_riscv_pmp_change_permissions(idx, new_perm);
175+
176+
zassert_equal(ret, 0, "z_riscv_pmp_change_permissions should return 0 on success.");
157177

158178
const size_t num_pmpcfg_regs = CONFIG_PMP_SLOTS / sizeof(unsigned long);
159179
const size_t num_pmpaddr_regs = CONFIG_PMP_SLOTS;
@@ -192,6 +212,10 @@ ZTEST(riscv_pmp_memattr_entries, test_successful_permission_change)
192212
"found.",
193213
i + 1, dt_regions[i].base, dt_regions[i].size, dt_regions[i].perm);
194214
}
215+
216+
/* Restore original permissions to leave system in original state */
217+
z_riscv_pmp_change_permissions(idx, original_perm);
218+
dt_regions[0].perm = original_perm;
195219
}
196220

197221
ZTEST_SUITE(riscv_pmp_memattr_entries, NULL, NULL, NULL, NULL, NULL);

0 commit comments

Comments
 (0)