| 1 |
# Copyright (c) 2023, Oracle and/or its affiliates. |
| 2 |
# SPDX-License-Identifier: LGPL-2.1-or-later |
| 3 |
|
| 4 |
from drgn import NULL, Object |
| 5 |
from drgn.helpers.linux.idr import idr_find, idr_for_each, idr_for_each_entry |
| 6 |
from tests.linux_kernel import LinuxKernelTestCase, skip_unless_have_test_kmod |
| 7 |
|
| 8 |
|
| 9 |
@skip_unless_have_test_kmod |
| 10 |
class TestIDR(LinuxKernelTestCase): |
| 11 |
def test_idr_find_empty(self): |
| 12 |
root = self.prog["drgn_test_idr_empty"].address_of_() |
| 13 |
self.assertIdentical(idr_find(root, 0), NULL(self.prog, "void *")) |
| 14 |
self.assertIdentical(idr_find(root, 100), NULL(self.prog, "void *")) |
| 15 |
|
| 16 |
def test_idr_for_each_empty(self): |
| 17 |
root = self.prog["drgn_test_idr_empty"].address_of_() |
| 18 |
self.assertIdentical(list(idr_for_each(root)), []) |
| 19 |
|
| 20 |
def test_idr_find_one(self): |
| 21 |
root = self.prog["drgn_test_idr_one"].address_of_() |
| 22 |
self.assertIdentical(idr_find(root, 0), NULL(self.prog, "void *")) |
| 23 |
self.assertIdentical(idr_find(root, 65), NULL(self.prog, "void *")) |
| 24 |
self.assertIdentical(idr_find(root, 66), Object(self.prog, "void *", 0xDEADB00)) |
| 25 |
self.assertIdentical(idr_find(root, 67), NULL(self.prog, "void *")) |
| 26 |
self.assertIdentical(idr_find(root, 100), NULL(self.prog, "void *")) |
| 27 |
self.assertIdentical(idr_find(root, 256 + 66), NULL(self.prog, "void *")) |
| 28 |
self.assertIdentical(idr_find(root, 2**24 + 66), NULL(self.prog, "void *")) |
| 29 |
self.assertIdentical(idr_find(root, 2**56 + 66), NULL(self.prog, "void *")) |
| 30 |
|
| 31 |
def test_idr_for_each_one(self): |
| 32 |
root = self.prog["drgn_test_idr_one"].address_of_() |
| 33 |
self.assertIdentical( |
| 34 |
list(idr_for_each(root)), |
| 35 |
[(66, Object(self.prog, "void *", 0xDEADB00))], |
| 36 |
) |
| 37 |
|
| 38 |
def test_idr_lookup_one_at_zero(self): |
| 39 |
root = self.prog["drgn_test_idr_one_at_zero"].address_of_() |
| 40 |
self.assertIdentical(idr_find(root, 0), Object(self.prog, "void *", 0x1234)) |
| 41 |
self.assertIdentical(idr_find(root, 1), NULL(self.prog, "void *")) |
| 42 |
self.assertIdentical(idr_find(root, 100), NULL(self.prog, "void *")) |
| 43 |
|
| 44 |
def test_idr_for_each_one_at_zero(self): |
| 45 |
root = self.prog["drgn_test_idr_one_at_zero"].address_of_() |
| 46 |
self.assertIdentical( |
| 47 |
list(idr_for_each(root)), [(0, Object(self.prog, "void *", 0x1234))] |
| 48 |
) |
| 49 |
|
| 50 |
def test_idr_find_sparse(self): |
| 51 |
root = self.prog["drgn_test_idr_sparse"].address_of_() |
| 52 |
self.assertIdentical(idr_find(root, 0), NULL(self.prog, "void *")) |
| 53 |
self.assertIdentical(idr_find(root, 1), Object(self.prog, "void *", 0x1234)) |
| 54 |
self.assertIdentical(idr_find(root, 2), NULL(self.prog, "void *")) |
| 55 |
self.assertIdentical(idr_find(root, 0x40), NULL(self.prog, "void *")) |
| 56 |
self.assertIdentical(idr_find(root, 0x70), NULL(self.prog, "void *")) |
| 57 |
self.assertIdentical(idr_find(root, 0x7F), NULL(self.prog, "void *")) |
| 58 |
self.assertIdentical(idr_find(root, 0x80), Object(self.prog, "void *", 0x5678)) |
| 59 |
self.assertIdentical(idr_find(root, 0xEE), Object(self.prog, "void *", 0x9ABC)) |
| 60 |
self.assertIdentical(idr_find(root, 0xEF), NULL(self.prog, "void *")) |
| 61 |
|
| 62 |
def test_idr_for_each_sparse(self): |
| 63 |
root = self.prog["drgn_test_idr_sparse"].address_of_() |
| 64 |
self.assertIdentical( |
| 65 |
list(idr_for_each(root)), |
| 66 |
[ |
| 67 |
(1, Object(self.prog, "void *", 0x1234)), |
| 68 |
(0x80, Object(self.prog, "void *", 0x5678)), |
| 69 |
(0xEE, Object(self.prog, "void *", 0x9ABC)), |
| 70 |
], |
| 71 |
) |
| 72 |
|
| 73 |
def test_idr_for_each_entry(self): |
| 74 |
root = self.prog["drgn_test_idr_sparse"].address_of_() |
| 75 |
self.assertIdentical( |
| 76 |
list(idr_for_each_entry(root, "int")), |
| 77 |
[ |
| 78 |
(1, Object(self.prog, "int *", 0x1234)), |
| 79 |
(0x80, Object(self.prog, "int *", 0x5678)), |
| 80 |
(0xEE, Object(self.prog, "int *", 0x9ABC)), |
| 81 |
], |
| 82 |
) |