| 1 |
# Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 |
# SPDX-License-Identifier: LGPL-2.1-or-later |
| 3 |
|
| 4 |
import collections |
| 5 |
|
| 6 |
from drgn import NULL |
| 7 |
from drgn.helpers import ValidationError |
| 8 |
from drgn.helpers.linux.list import ( |
| 9 |
hlist_empty, |
| 10 |
hlist_for_each, |
| 11 |
hlist_for_each_entry, |
| 12 |
list_count_nodes, |
| 13 |
list_empty, |
| 14 |
list_first_entry, |
| 15 |
list_first_entry_or_null, |
| 16 |
list_for_each, |
| 17 |
list_for_each_entry, |
| 18 |
list_for_each_entry_reverse, |
| 19 |
list_for_each_reverse, |
| 20 |
list_is_singular, |
| 21 |
list_last_entry, |
| 22 |
list_next_entry, |
| 23 |
list_prev_entry, |
| 24 |
validate_list, |
| 25 |
validate_list_count_nodes, |
| 26 |
validate_list_for_each, |
| 27 |
validate_list_for_each_entry, |
| 28 |
) |
| 29 |
from tests.linux_kernel import LinuxKernelTestCase, skip_unless_have_test_kmod |
| 30 |
|
| 31 |
|
| 32 |
@skip_unless_have_test_kmod |
| 33 |
class TestList(LinuxKernelTestCase): |
| 34 |
@classmethod |
| 35 |
def setUpClass(cls): |
| 36 |
super().setUpClass() |
| 37 |
cls.empty = cls.prog["drgn_test_empty_list"].address_of_() |
| 38 |
cls.full = cls.prog["drgn_test_full_list"].address_of_() |
| 39 |
cls.entries = cls.prog["drgn_test_list_entries"] |
| 40 |
cls.num_entries = 3 |
| 41 |
cls.singular = cls.prog["drgn_test_singular_list"].address_of_() |
| 42 |
cls.singular_entry = cls.prog["drgn_test_singular_list_entry"].address_of_() |
| 43 |
cls.singular_node = cls.singular_entry.node.address_of_() |
| 44 |
cls.corrupted = cls.prog["drgn_test_corrupted_list"].address_of_() |
| 45 |
|
| 46 |
def node(self, n): |
| 47 |
return self.entries[n].node.address_of_() |
| 48 |
|
| 49 |
def entry(self, n): |
| 50 |
return self.entries[n].address_of_() |
| 51 |
|
| 52 |
def test_list_empty(self): |
| 53 |
self.assertTrue(list_empty(self.empty)) |
| 54 |
self.assertFalse(list_empty(self.full)) |
| 55 |
self.assertFalse(list_empty(self.singular)) |
| 56 |
|
| 57 |
def test_list_is_singular(self): |
| 58 |
self.assertFalse(list_is_singular(self.empty)) |
| 59 |
self.assertFalse(list_is_singular(self.full)) |
| 60 |
self.assertTrue(list_is_singular(self.singular)) |
| 61 |
|
| 62 |
def test_list_count_nodes(self): |
| 63 |
self.assertEqual(list_count_nodes(self.empty), 0) |
| 64 |
self.assertEqual(list_count_nodes(self.full), self.num_entries) |
| 65 |
self.assertEqual(list_count_nodes(self.singular), 1) |
| 66 |
|
| 67 |
def test_list_first_entry(self): |
| 68 |
self.assertEqual( |
| 69 |
list_first_entry(self.full, "struct drgn_test_list_entry", "node"), |
| 70 |
self.entry(0), |
| 71 |
) |
| 72 |
self.assertEqual( |
| 73 |
list_first_entry(self.singular, "struct drgn_test_list_entry", "node"), |
| 74 |
self.singular_entry, |
| 75 |
) |
| 76 |
|
| 77 |
def test_list_first_entry_or_null(self): |
| 78 |
self.assertEqual( |
| 79 |
list_first_entry_or_null(self.empty, "struct drgn_test_list_entry", "node"), |
| 80 |
NULL(self.prog, "struct drgn_test_list_entry *"), |
| 81 |
) |
| 82 |
self.assertEqual( |
| 83 |
list_first_entry_or_null(self.full, "struct drgn_test_list_entry", "node"), |
| 84 |
self.entry(0), |
| 85 |
) |
| 86 |
self.assertEqual( |
| 87 |
list_first_entry_or_null( |
| 88 |
self.singular, "struct drgn_test_list_entry", "node" |
| 89 |
), |
| 90 |
self.singular_entry, |
| 91 |
) |
| 92 |
|
| 93 |
def test_list_last_entry(self): |
| 94 |
self.assertEqual( |
| 95 |
list_last_entry(self.full, "struct drgn_test_list_entry", "node"), |
| 96 |
self.entry(self.num_entries - 1), |
| 97 |
) |
| 98 |
self.assertEqual( |
| 99 |
list_last_entry(self.singular, "struct drgn_test_list_entry", "node"), |
| 100 |
self.singular_entry, |
| 101 |
) |
| 102 |
|
| 103 |
def test_list_next_entry(self): |
| 104 |
for i in range(self.num_entries - 1): |
| 105 |
self.assertEqual(list_next_entry(self.entry(i), "node"), self.entry(i + 1)) |
| 106 |
|
| 107 |
def test_list_prev_entry(self): |
| 108 |
for i in range(1, self.num_entries): |
| 109 |
self.assertEqual(list_prev_entry(self.entry(i), "node"), self.entry(i - 1)) |
| 110 |
|
| 111 |
def test_list_for_each(self): |
| 112 |
self.assertEqual(list(list_for_each(self.empty)), []) |
| 113 |
self.assertEqual( |
| 114 |
list(list_for_each(self.full)), |
| 115 |
[self.node(i) for i in range(self.num_entries)], |
| 116 |
) |
| 117 |
self.assertEqual(list(list_for_each(self.singular)), [self.singular_node]) |
| 118 |
|
| 119 |
def test_list_for_each_reverse(self): |
| 120 |
self.assertEqual(list(list_for_each_reverse(self.empty)), []) |
| 121 |
self.assertEqual( |
| 122 |
list(list_for_each_reverse(self.full)), |
| 123 |
[self.node(i) for i in range(self.num_entries - 1, -1, -1)], |
| 124 |
) |
| 125 |
self.assertEqual( |
| 126 |
list(list_for_each_reverse(self.singular)), [self.singular_node] |
| 127 |
) |
| 128 |
|
| 129 |
def test_list_for_each_entry(self): |
| 130 |
self.assertEqual( |
| 131 |
list( |
| 132 |
list_for_each_entry("struct drgn_test_list_entry", self.empty, "node") |
| 133 |
), |
| 134 |
[], |
| 135 |
) |
| 136 |
self.assertEqual( |
| 137 |
list(list_for_each_entry("struct drgn_test_list_entry", self.full, "node")), |
| 138 |
[self.entry(i) for i in range(self.num_entries)], |
| 139 |
) |
| 140 |
self.assertEqual( |
| 141 |
list( |
| 142 |
list_for_each_entry( |
| 143 |
"struct drgn_test_list_entry", self.singular, "node" |
| 144 |
) |
| 145 |
), |
| 146 |
[self.singular_entry], |
| 147 |
) |
| 148 |
|
| 149 |
def test_list_for_each_entry_reverse(self): |
| 150 |
self.assertEqual( |
| 151 |
list( |
| 152 |
list_for_each_entry_reverse( |
| 153 |
"struct drgn_test_list_entry", self.empty, "node" |
| 154 |
) |
| 155 |
), |
| 156 |
[], |
| 157 |
) |
| 158 |
self.assertEqual( |
| 159 |
list( |
| 160 |
list_for_each_entry_reverse( |
| 161 |
"struct drgn_test_list_entry", self.full, "node" |
| 162 |
) |
| 163 |
), |
| 164 |
[self.entry(i) for i in range(self.num_entries - 1, -1, -1)], |
| 165 |
) |
| 166 |
self.assertEqual( |
| 167 |
list( |
| 168 |
list_for_each_entry_reverse( |
| 169 |
"struct drgn_test_list_entry", self.singular, "node" |
| 170 |
) |
| 171 |
), |
| 172 |
[self.singular_entry], |
| 173 |
) |
| 174 |
|
| 175 |
def test_validate_list(self): |
| 176 |
for head in (self.empty, self.full, self.singular): |
| 177 |
validate_list(head) |
| 178 |
for head in ( |
| 179 |
self.corrupted, |
| 180 |
self.prog["drgn_test_list_cycle1"].address_of_(), |
| 181 |
self.prog["drgn_test_list_cycle2"].address_of_(), |
| 182 |
self.prog["drgn_test_list_cycle3"].address_of_(), |
| 183 |
self.prog["drgn_test_list_self_cycle"].address_of_(), |
| 184 |
): |
| 185 |
self.assertRaises(ValidationError, validate_list, head) |
| 186 |
|
| 187 |
def test_validate_list_count_nodes(self): |
| 188 |
self.assertEqual(validate_list_count_nodes(self.empty), 0) |
| 189 |
self.assertEqual(validate_list_count_nodes(self.full), self.num_entries) |
| 190 |
self.assertEqual(validate_list_count_nodes(self.singular), 1) |
| 191 |
self.assertRaises(ValidationError, validate_list_count_nodes, self.corrupted) |
| 192 |
|
| 193 |
def test_validate_list_for_each(self): |
| 194 |
for head in (self.empty, self.full, self.singular): |
| 195 |
self.assertEqual( |
| 196 |
list(validate_list_for_each(head)), list(list_for_each(head)) |
| 197 |
) |
| 198 |
self.assertRaises( |
| 199 |
ValidationError, |
| 200 |
collections.deque, |
| 201 |
validate_list_for_each(self.corrupted), |
| 202 |
0, |
| 203 |
) |
| 204 |
|
| 205 |
def test_validate_list_for_each_entry(self): |
| 206 |
for head in (self.empty, self.full, self.singular): |
| 207 |
self.assertEqual( |
| 208 |
list( |
| 209 |
validate_list_for_each_entry( |
| 210 |
"struct drgn_test_list_entry", head, "node" |
| 211 |
) |
| 212 |
), |
| 213 |
list(list_for_each_entry("struct drgn_test_list_entry", head, "node")), |
| 214 |
) |
| 215 |
self.assertRaises( |
| 216 |
ValidationError, |
| 217 |
collections.deque, |
| 218 |
validate_list_for_each_entry( |
| 219 |
"struct drgn_test_list_entry", self.corrupted, "node" |
| 220 |
), |
| 221 |
0, |
| 222 |
) |
| 223 |
|
| 224 |
|
| 225 |
@skip_unless_have_test_kmod |
| 226 |
class TestHlist(LinuxKernelTestCase): |
| 227 |
@classmethod |
| 228 |
def setUpClass(cls): |
| 229 |
super().setUpClass() |
| 230 |
cls.empty = cls.prog["drgn_test_empty_hlist"].address_of_() |
| 231 |
cls.full = cls.prog["drgn_test_full_hlist"].address_of_() |
| 232 |
cls.entries = cls.prog["drgn_test_hlist_entries"] |
| 233 |
cls.num_entries = 3 |
| 234 |
|
| 235 |
def node(self, n): |
| 236 |
return self.entries[n].node.address_of_() |
| 237 |
|
| 238 |
def entry(self, n): |
| 239 |
return self.entries[n].address_of_() |
| 240 |
|
| 241 |
def test_hlist_empty(self): |
| 242 |
self.assertTrue(hlist_empty(self.empty)) |
| 243 |
self.assertFalse(hlist_empty(self.full)) |
| 244 |
|
| 245 |
def test_hlist_for_each(self): |
| 246 |
self.assertEqual(list(hlist_for_each(self.empty)), []) |
| 247 |
self.assertEqual( |
| 248 |
list(hlist_for_each(self.full)), |
| 249 |
[self.node(i) for i in range(self.num_entries)], |
| 250 |
) |
| 251 |
|
| 252 |
def test_hlist_for_each_entry(self): |
| 253 |
self.assertEqual( |
| 254 |
list( |
| 255 |
hlist_for_each_entry("struct drgn_test_hlist_entry", self.empty, "node") |
| 256 |
), |
| 257 |
[], |
| 258 |
) |
| 259 |
self.assertEqual( |
| 260 |
list( |
| 261 |
hlist_for_each_entry("struct drgn_test_hlist_entry", self.full, "node") |
| 262 |
), |
| 263 |
[self.entry(i) for i in range(self.num_entries)], |
| 264 |
) |