xref: /aosp_15_r20/external/libcap-ng/bindings/python3/test/capng-test.py (revision 8dd5e09d5faf27a871e8654ddaa2d2af7c696578)
1#!/usr/bin/env python3
2
3import os
4import sys
5import time
6load_path = '../'
7if False:
8    sys.path.insert(0, load_path)
9
10import capng
11
12last = capng.CAP_LAST_CAP
13try:
14	with open('/proc/sys/kernel/cap_last_cap', 'r') as f:
15		last = int(f.readline())
16except IOError as e:
17	print("Error opening /proc/sys/kernel/cap_last_cap: {0}".format(e.strerror))
18
19print("Doing basic bit tests...")
20capng.capng_clear(capng.CAPNG_SELECT_BOTH)
21if capng.capng_have_capabilities(capng.CAPNG_SELECT_BOTH) != capng.CAPNG_NONE:
22	print("Failed clearing capabilities\n")
23	sys.exit(1)
24
25capng.capng_fill(capng.CAPNG_SELECT_BOTH)
26if capng.capng_have_capabilities(capng.CAPNG_SELECT_BOTH) != capng.CAPNG_FULL:
27	print("Failed filling capabilities")
28	sys.exit(1)
29
30text = capng.capng_print_caps_numeric(capng.CAPNG_PRINT_BUFFER, capng.CAPNG_SELECT_CAPS)
31len = len(text)
32if len < 80 and last > 30:
33	last = 30
34
35print("Doing advanced bit tests for %d capabilities...\n" % (last))
36for i in range(last+1):
37	capng.capng_clear(capng.CAPNG_SELECT_BOTH)
38	rc = capng.capng_update(capng.CAPNG_ADD, capng.CAPNG_EFFECTIVE, i)
39	if rc:
40		print("Failed update test 1")
41		sys.exit(1)
42
43	rc = capng.capng_have_capability(capng.CAPNG_EFFECTIVE, int(i))
44	if rc <= capng.CAPNG_NONE:
45		print("Failed have capability test 1")
46		capng.capng_print_caps_numeric(capng.CAPNG_PRINT_STDOUT, capng.CAPNG_SELECT_CAPS)
47		sys.exit(1)
48
49	if capng.capng_have_capabilities(capng.CAPNG_SELECT_CAPS) != capng.CAPNG_PARTIAL:
50		print("Failed have capabilities test 1")
51		sys.exit(1)
52
53	capng.capng_fill(capng.CAPNG_SELECT_BOTH)
54	rc = capng.capng_update(capng.CAPNG_DROP, capng.CAPNG_EFFECTIVE, i)
55	if rc:
56		print("Failed update test 3")
57		sys.exit(1)
58
59	if capng.capng_have_capabilities(capng.CAPNG_SELECT_CAPS)!=capng.CAPNG_PARTIAL:
60		print("Failed have capabilities test 3")
61		capng.capng_print_caps_numeric(capng.CAPNG_PRINT_STDOUT, capng.CAPNG_SELECT_CAPS)
62		sys.exit(1)
63
64	rc = capng.capng_update(capng.CAPNG_ADD, capng.CAPNG_EFFECTIVE, i)
65	if rc:
66		print("Failed update test 4")
67		sys.exit(1)
68
69	if capng.capng_have_capabilities(capng.CAPNG_SELECT_CAPS) != capng.CAPNG_FULL:
70		print("Failed have capabilities test 4")
71		capng.capng_print_caps_numeric(capng.CAPNG_PRINT_STDOUT, capng.CAPNG_SELECT_CAPS)
72		sys.exit(1)
73
74sys.exit(0)
75