Debian Codegraph @

Lib/test/test_ctypes/test_find.py

raw
1 import os.path
2 import sys
3 import test.support
4 import unittest
5 import unittest.mock
6 from ctypes import CDLL, RTLD_GLOBAL
7 from ctypes.util import find_library
8 from test.support import os_helper
9
10
11 # On some systems, loading the OpenGL libraries needs the RTLD_GLOBAL mode.
12 class Test_OpenGL_libs(unittest.TestCase):
13 @classmethod
14 def setUpClass(cls):
15 lib_gl = lib_glu = lib_gle = None
16 if sys.platform == "win32":
17 lib_gl = find_library("OpenGL32")
18 lib_glu = find_library("Glu32")
19 elif sys.platform == "darwin":
20 lib_gl = lib_glu = find_library("OpenGL")
21 else:
22 lib_gl = find_library("GL")
23 lib_glu = find_library("GLU")
24 lib_gle = find_library("gle")
25
26 # print, for debugging
27 if test.support.verbose:
28 print("OpenGL libraries:")
29 for item in (("GL", lib_gl),
30 ("GLU", lib_glu),
31 ("gle", lib_gle)):
32 print("\t", item)
33
34 cls.gl = cls.glu = cls.gle = None
35 if lib_gl:
36 try:
37 cls.gl = CDLL(lib_gl, mode=RTLD_GLOBAL)
38 except OSError:
39 pass
40
41 if lib_glu:
42 try:
43 cls.glu = CDLL(lib_glu, RTLD_GLOBAL)
44 except OSError:
45 pass
46
47 if lib_gle:
48 try:
49 cls.gle = CDLL(lib_gle)
50 except OSError:
51 pass
52
53 @classmethod
54 def tearDownClass(cls):
55 cls.gl = cls.glu = cls.gle = None
56
57 def test_gl(self):
58 if self.gl is None:
59 self.skipTest('lib_gl not available')
60 self.gl.glClearIndex
61
62 def test_glu(self):
63 if self.glu is None:
64 self.skipTest('lib_glu not available')
65 self.glu.gluBeginCurve
66
67 def test_gle(self):
68 if self.gle is None:
69 self.skipTest('lib_gle not available')
70 self.gle.gleGetJoinStyle
71
72 def test_shell_injection(self):
73 result = find_library('; echo Hello shell > ' + os_helper.TESTFN)
74 self.assertFalse(os.path.lexists(os_helper.TESTFN))
75 self.assertIsNone(result)
76
77
78 @unittest.skipUnless(sys.platform.startswith('linux'),
79 'Test only valid for Linux')
80 class FindLibraryLinux(unittest.TestCase):
81 def test_find_on_libpath(self):
82 import subprocess
83 import tempfile
84
85 try:
86 p = subprocess.Popen(['gcc', '--version'], stdout=subprocess.PIPE,
87 stderr=subprocess.DEVNULL)
88 out, _ = p.communicate()
89 except OSError:
90 raise unittest.SkipTest('gcc, needed for test, not available')
91 with tempfile.TemporaryDirectory() as d:
92 # create an empty temporary file
93 srcname = os.path.join(d, 'dummy.c')
94 libname = 'py_ctypes_test_dummy'
95 dstname = os.path.join(d, 'lib%s.so' % libname)
96 with open(srcname, 'wb') as f:
97 pass
98 self.assertTrue(os.path.exists(srcname))
99 # compile the file to a shared library
100 cmd = ['gcc', '-o', dstname, '--shared',
101 '-Wl,-soname,lib%s.so' % libname, srcname]
102 out = subprocess.check_output(cmd)
103 self.assertTrue(os.path.exists(dstname))
104 # now check that the .so can't be found (since not in
105 # LD_LIBRARY_PATH)
106 self.assertIsNone(find_library(libname))
107 # now add the location to LD_LIBRARY_PATH
108 with os_helper.EnvironmentVarGuard() as env:
109 KEY = 'LD_LIBRARY_PATH'
110 if KEY not in env:
111 v = d
112 else:
113 v = '%s:%s' % (env[KEY], d)
114 env.set(KEY, v)
115 # now check that the .so can be found (since in
116 # LD_LIBRARY_PATH)
117 self.assertEqual(find_library(libname), 'lib%s.so' % libname)
118
119 def test_find_library_with_gcc(self):
120 with unittest.mock.patch("ctypes.util._findSoname_ldconfig", lambda *args: None):
121 self.assertNotEqual(find_library('c'), None)
122
123 def test_find_library_with_ld(self):
124 with unittest.mock.patch("ctypes.util._findSoname_ldconfig", lambda *args: None), \
125 unittest.mock.patch("ctypes.util._findLib_gcc", lambda *args: None):
126 self.assertNotEqual(find_library('c'), None)
127
128 def test_gh114257(self):
129 self.assertIsNone(find_library("libc"))
130
131
132 @unittest.skipUnless(sys.platform == 'android', 'Test only valid for Android')
133 class FindLibraryAndroid(unittest.TestCase):
134 def test_find(self):
135 for name in [
136 "c", "m", # POSIX
137 "z", # Non-POSIX, but present on Linux
138 "log", # Not present on Linux
139 ]:
140 with self.subTest(name=name):
141 path = find_library(name)
142 self.assertIsInstance(path, str)
143 self.assertEqual(
144 os.path.dirname(path),
145 "/system/lib64" if "64" in os.uname().machine
146 else "/system/lib")
147 self.assertEqual(os.path.basename(path), f"lib{name}.so")
148 self.assertTrue(os.path.isfile(path), path)
149
150 for name in ["libc", "nonexistent"]:
151 with self.subTest(name=name):
152 self.assertIsNone(find_library(name))
153
154
155 if __name__ == "__main__":
156 unittest.main()