Debian Codegraph @

tests/linux_kernel/helpers/test_user.py

raw
1 # Copyright (c) Meta Platforms, Inc. and affiliates.
2 # SPDX-License-Identifier: LGPL-2.1-or-later
3
4 import contextlib
5 import os
6
7 from drgn import Object
8 from drgn.helpers.linux.user import find_user, for_each_user, kuid_val
9 from tests.linux_kernel import LinuxKernelTestCase, fork_and_stop
10
11
12 class TestUser(LinuxKernelTestCase):
13 # Try a few UIDs in case the the hash function changes in the future.
14 UIDS = frozenset({0, 430, 1000, 65537})
15
16 def test_kuid_val(self):
17 self.assertIdentical(kuid_val(self.prog["init_cred"].uid), 0)
18 self.assertIdentical(kuid_val(Object(self.prog, "uid_t", 0)), 0)
19 self.assertIdentical(kuid_val(0), 0)
20
21 def test_find_user(self):
22 for uid in self.UIDS:
23 with fork_and_stop(os.setuid, uid):
24 found_uid = find_user(self.prog, uid).uid.val.value_()
25 self.assertEqual(found_uid, uid)
26
27 def test_for_each_user(self):
28 with contextlib.ExitStack() as stack:
29 for uid in self.UIDS:
30 stack.enter_context(fork_and_stop(os.setuid, uid))
31 found_uids = {user.uid.val.value_() for user in for_each_user(self.prog)}
32 self.assertTrue(self.UIDS.issubset(found_uids))