| 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, thread_unsafe |
| 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 |
@thread_unsafe('uses setenv') |
| 82 |
def test_find_on_libpath(self): |
| 83 |
import subprocess |
| 84 |
import tempfile |
| 85 |
|
| 86 |
try: |
| 87 |
p = subprocess.Popen(['gcc', '--version'], stdout=subprocess.PIPE, |
| 88 |
stderr=subprocess.DEVNULL) |
| 89 |
out, _ = p.communicate() |
| 90 |
except OSError: |
| 91 |
raise unittest.SkipTest('gcc, needed for test, not available') |
| 92 |
with tempfile.TemporaryDirectory() as d: |
| 93 |
# create an empty temporary file |
| 94 |
srcname = os.path.join(d, 'dummy.c') |
| 95 |
libname = 'py_ctypes_test_dummy' |
| 96 |
dstname = os.path.join(d, 'lib%s.so' % libname) |
| 97 |
with open(srcname, 'wb') as f: |
| 98 |
pass |
| 99 |
self.assertTrue(os.path.exists(srcname)) |
| 100 |
# compile the file to a shared library |
| 101 |
cmd = ['gcc', '-o', dstname, '--shared', |
| 102 |
'-Wl,-soname,lib%s.so' % libname, srcname] |
| 103 |
out = subprocess.check_output(cmd) |
| 104 |
self.assertTrue(os.path.exists(dstname)) |
| 105 |
# now check that the .so can't be found (since not in |
| 106 |
# LD_LIBRARY_PATH) |
| 107 |
self.assertIsNone(find_library(libname)) |
| 108 |
# now add the location to LD_LIBRARY_PATH |
| 109 |
with os_helper.EnvironmentVarGuard() as env: |
| 110 |
KEY = 'LD_LIBRARY_PATH' |
| 111 |
if KEY not in env: |
| 112 |
v = d |
| 113 |
else: |
| 114 |
v = '%s:%s' % (env[KEY], d) |
| 115 |
env.set(KEY, v) |
| 116 |
# now check that the .so can be found (since in |
| 117 |
# LD_LIBRARY_PATH) |
| 118 |
self.assertEqual(find_library(libname), 'lib%s.so' % libname) |
| 119 |
|
| 120 |
def test_find_library_with_gcc(self): |
| 121 |
with unittest.mock.patch("ctypes.util._findSoname_ldconfig", lambda *args: None): |
| 122 |
self.assertNotEqual(find_library('c'), None) |
| 123 |
|
| 124 |
def test_find_library_with_ld(self): |
| 125 |
with unittest.mock.patch("ctypes.util._findSoname_ldconfig", lambda *args: None), \ |
| 126 |
unittest.mock.patch("ctypes.util._findLib_gcc", lambda *args: None): |
| 127 |
self.assertNotEqual(find_library('c'), None) |
| 128 |
|
| 129 |
def test_gh114257(self): |
| 130 |
self.assertIsNone(find_library("libc")) |
| 131 |
|
| 132 |
|
| 133 |
@unittest.skipUnless(sys.platform == 'android', 'Test only valid for Android') |
| 134 |
class FindLibraryAndroid(unittest.TestCase): |
| 135 |
def test_find(self): |
| 136 |
for name in [ |
| 137 |
"c", "m", # POSIX |
| 138 |
"z", # Non-POSIX, but present on Linux |
| 139 |
"log", # Not present on Linux |
| 140 |
]: |
| 141 |
with self.subTest(name=name): |
| 142 |
path = find_library(name) |
| 143 |
self.assertIsInstance(path, str) |
| 144 |
self.assertEqual( |
| 145 |
os.path.dirname(path), |
| 146 |
"/system/lib64" if "64" in os.uname().machine |
| 147 |
else "/system/lib") |
| 148 |
self.assertEqual(os.path.basename(path), f"lib{name}.so") |
| 149 |
self.assertTrue(os.path.isfile(path), path) |
| 150 |
|
| 151 |
for name in ["libc", "nonexistent"]: |
| 152 |
with self.subTest(name=name): |
| 153 |
self.assertIsNone(find_library(name)) |
| 154 |
|
| 155 |
|
| 156 |
@unittest.skipUnless(test.support.is_emscripten, |
| 157 |
'Test only valid for Emscripten') |
| 158 |
class FindLibraryEmscripten(unittest.TestCase): |
| 159 |
@classmethod |
| 160 |
def setUpClass(cls): |
| 161 |
import tempfile |
| 162 |
|
| 163 |
# A very simple wasm module |
| 164 |
# In WAT format: (module) |
| 165 |
cls.wasm_module = b'\x00asm\x01\x00\x00\x00\x00\x08\x04name\x02\x01\x00' |
| 166 |
|
| 167 |
cls.non_wasm_content = b'This is not a WASM file' |
| 168 |
|
| 169 |
cls.temp_dir = tempfile.mkdtemp() |
| 170 |
cls.libdummy_so_path = os.path.join(cls.temp_dir, 'libdummy.so') |
| 171 |
with open(cls.libdummy_so_path, 'wb') as f: |
| 172 |
f.write(cls.wasm_module) |
| 173 |
|
| 174 |
cls.libother_wasm_path = os.path.join(cls.temp_dir, 'libother.wasm') |
| 175 |
with open(cls.libother_wasm_path, 'wb') as f: |
| 176 |
f.write(cls.wasm_module) |
| 177 |
|
| 178 |
cls.libnowasm_so_path = os.path.join(cls.temp_dir, 'libnowasm.so') |
| 179 |
with open(cls.libnowasm_so_path, 'wb') as f: |
| 180 |
f.write(cls.non_wasm_content) |
| 181 |
|
| 182 |
@classmethod |
| 183 |
def tearDownClass(cls): |
| 184 |
import shutil |
| 185 |
shutil.rmtree(cls.temp_dir) |
| 186 |
|
| 187 |
def test_find_wasm_file_with_so_extension(self): |
| 188 |
with os_helper.EnvironmentVarGuard() as env: |
| 189 |
env.set('LD_LIBRARY_PATH', self.temp_dir) |
| 190 |
result = find_library('dummy') |
| 191 |
self.assertEqual(result, self.libdummy_so_path) |
| 192 |
def test_find_wasm_file_with_wasm_extension(self): |
| 193 |
with os_helper.EnvironmentVarGuard() as env: |
| 194 |
env.set('LD_LIBRARY_PATH', self.temp_dir) |
| 195 |
result = find_library('other') |
| 196 |
self.assertEqual(result, self.libother_wasm_path) |
| 197 |
|
| 198 |
def test_ignore_non_wasm_file(self): |
| 199 |
with os_helper.EnvironmentVarGuard() as env: |
| 200 |
env.set('LD_LIBRARY_PATH', self.temp_dir) |
| 201 |
result = find_library('nowasm') |
| 202 |
self.assertIsNone(result) |
| 203 |
|
| 204 |
def test_find_nothing_without_ld_library_path(self): |
| 205 |
with os_helper.EnvironmentVarGuard() as env: |
| 206 |
if 'LD_LIBRARY_PATH' in env: |
| 207 |
del env['LD_LIBRARY_PATH'] |
| 208 |
result = find_library('dummy') |
| 209 |
self.assertIsNone(result) |
| 210 |
result = find_library('other') |
| 211 |
self.assertIsNone(result) |
| 212 |
|
| 213 |
def test_find_nothing_with_wrong_ld_library_path(self): |
| 214 |
import tempfile |
| 215 |
with tempfile.TemporaryDirectory() as empty_dir: |
| 216 |
with os_helper.EnvironmentVarGuard() as env: |
| 217 |
env.set('LD_LIBRARY_PATH', empty_dir) |
| 218 |
result = find_library('dummy') |
| 219 |
self.assertIsNone(result) |
| 220 |
result = find_library('other') |
| 221 |
self.assertIsNone(result) |
| 222 |
|
| 223 |
|
| 224 |
if __name__ == "__main__": |
| 225 |
unittest.main() |