| 1 | n/a | import unittest |
|---|
| 2 | n/a | import os.path |
|---|
| 3 | n/a | import sys |
|---|
| 4 | n/a | import test.support |
|---|
| 5 | n/a | from ctypes import * |
|---|
| 6 | n/a | from ctypes.util import find_library |
|---|
| 7 | n/a | |
|---|
| 8 | n/a | # On some systems, loading the OpenGL libraries needs the RTLD_GLOBAL mode. |
|---|
| 9 | n/a | class Test_OpenGL_libs(unittest.TestCase): |
|---|
| 10 | n/a | @classmethod |
|---|
| 11 | n/a | def setUpClass(cls): |
|---|
| 12 | n/a | lib_gl = lib_glu = lib_gle = None |
|---|
| 13 | n/a | if sys.platform == "win32": |
|---|
| 14 | n/a | lib_gl = find_library("OpenGL32") |
|---|
| 15 | n/a | lib_glu = find_library("Glu32") |
|---|
| 16 | n/a | elif sys.platform == "darwin": |
|---|
| 17 | n/a | lib_gl = lib_glu = find_library("OpenGL") |
|---|
| 18 | n/a | else: |
|---|
| 19 | n/a | lib_gl = find_library("GL") |
|---|
| 20 | n/a | lib_glu = find_library("GLU") |
|---|
| 21 | n/a | lib_gle = find_library("gle") |
|---|
| 22 | n/a | |
|---|
| 23 | n/a | ## print, for debugging |
|---|
| 24 | n/a | if test.support.verbose: |
|---|
| 25 | n/a | print("OpenGL libraries:") |
|---|
| 26 | n/a | for item in (("GL", lib_gl), |
|---|
| 27 | n/a | ("GLU", lib_glu), |
|---|
| 28 | n/a | ("gle", lib_gle)): |
|---|
| 29 | n/a | print("\t", item) |
|---|
| 30 | n/a | |
|---|
| 31 | n/a | cls.gl = cls.glu = cls.gle = None |
|---|
| 32 | n/a | if lib_gl: |
|---|
| 33 | n/a | try: |
|---|
| 34 | n/a | cls.gl = CDLL(lib_gl, mode=RTLD_GLOBAL) |
|---|
| 35 | n/a | except OSError: |
|---|
| 36 | n/a | pass |
|---|
| 37 | n/a | if lib_glu: |
|---|
| 38 | n/a | try: |
|---|
| 39 | n/a | cls.glu = CDLL(lib_glu, RTLD_GLOBAL) |
|---|
| 40 | n/a | except OSError: |
|---|
| 41 | n/a | pass |
|---|
| 42 | n/a | if lib_gle: |
|---|
| 43 | n/a | try: |
|---|
| 44 | n/a | cls.gle = CDLL(lib_gle) |
|---|
| 45 | n/a | except OSError: |
|---|
| 46 | n/a | pass |
|---|
| 47 | n/a | |
|---|
| 48 | n/a | @classmethod |
|---|
| 49 | n/a | def tearDownClass(cls): |
|---|
| 50 | n/a | cls.gl = cls.glu = cls.gle = None |
|---|
| 51 | n/a | |
|---|
| 52 | n/a | def test_gl(self): |
|---|
| 53 | n/a | if self.gl is None: |
|---|
| 54 | n/a | self.skipTest('lib_gl not available') |
|---|
| 55 | n/a | self.gl.glClearIndex |
|---|
| 56 | n/a | |
|---|
| 57 | n/a | def test_glu(self): |
|---|
| 58 | n/a | if self.glu is None: |
|---|
| 59 | n/a | self.skipTest('lib_glu not available') |
|---|
| 60 | n/a | self.glu.gluBeginCurve |
|---|
| 61 | n/a | |
|---|
| 62 | n/a | def test_gle(self): |
|---|
| 63 | n/a | if self.gle is None: |
|---|
| 64 | n/a | self.skipTest('lib_gle not available') |
|---|
| 65 | n/a | self.gle.gleGetJoinStyle |
|---|
| 66 | n/a | |
|---|
| 67 | n/a | def test_shell_injection(self): |
|---|
| 68 | n/a | result = find_library('; echo Hello shell > ' + test.support.TESTFN) |
|---|
| 69 | n/a | self.assertFalse(os.path.lexists(test.support.TESTFN)) |
|---|
| 70 | n/a | self.assertIsNone(result) |
|---|
| 71 | n/a | |
|---|
| 72 | n/a | |
|---|
| 73 | n/a | @unittest.skipUnless(sys.platform.startswith('linux'), |
|---|
| 74 | n/a | 'Test only valid for Linux') |
|---|
| 75 | n/a | class LibPathFindTest(unittest.TestCase): |
|---|
| 76 | n/a | def test_find_on_libpath(self): |
|---|
| 77 | n/a | import subprocess |
|---|
| 78 | n/a | import tempfile |
|---|
| 79 | n/a | |
|---|
| 80 | n/a | try: |
|---|
| 81 | n/a | p = subprocess.Popen(['gcc', '--version'], stdout=subprocess.PIPE, |
|---|
| 82 | n/a | stderr=subprocess.DEVNULL) |
|---|
| 83 | n/a | out, _ = p.communicate() |
|---|
| 84 | n/a | except OSError: |
|---|
| 85 | n/a | raise unittest.SkipTest('gcc, needed for test, not available') |
|---|
| 86 | n/a | with tempfile.TemporaryDirectory() as d: |
|---|
| 87 | n/a | # create an empty temporary file |
|---|
| 88 | n/a | srcname = os.path.join(d, 'dummy.c') |
|---|
| 89 | n/a | libname = 'py_ctypes_test_dummy' |
|---|
| 90 | n/a | dstname = os.path.join(d, 'lib%s.so' % libname) |
|---|
| 91 | n/a | with open(srcname, 'w') as f: |
|---|
| 92 | n/a | pass |
|---|
| 93 | n/a | self.assertTrue(os.path.exists(srcname)) |
|---|
| 94 | n/a | # compile the file to a shared library |
|---|
| 95 | n/a | cmd = ['gcc', '-o', dstname, '--shared', |
|---|
| 96 | n/a | '-Wl,-soname,lib%s.so' % libname, srcname] |
|---|
| 97 | n/a | out = subprocess.check_output(cmd) |
|---|
| 98 | n/a | self.assertTrue(os.path.exists(dstname)) |
|---|
| 99 | n/a | # now check that the .so can't be found (since not in |
|---|
| 100 | n/a | # LD_LIBRARY_PATH) |
|---|
| 101 | n/a | self.assertIsNone(find_library(libname)) |
|---|
| 102 | n/a | # now add the location to LD_LIBRARY_PATH |
|---|
| 103 | n/a | with test.support.EnvironmentVarGuard() as env: |
|---|
| 104 | n/a | KEY = 'LD_LIBRARY_PATH' |
|---|
| 105 | n/a | if KEY not in env: |
|---|
| 106 | n/a | v = d |
|---|
| 107 | n/a | else: |
|---|
| 108 | n/a | v = '%s:%s' % (env[KEY], d) |
|---|
| 109 | n/a | env.set(KEY, v) |
|---|
| 110 | n/a | # now check that the .so can be found (since in |
|---|
| 111 | n/a | # LD_LIBRARY_PATH) |
|---|
| 112 | n/a | self.assertEqual(find_library(libname), 'lib%s.so' % libname) |
|---|
| 113 | n/a | |
|---|
| 114 | n/a | |
|---|
| 115 | n/a | if __name__ == "__main__": |
|---|
| 116 | n/a | unittest.main() |
|---|