Пример #1
0
from __future__ import absolute_import, division, print_function

import sys
import ctypes
from CTypeGen import generate
import CMock
import gc

if len( sys.argv ) >= 2:
   mocklib = sys.argv[ 1 ]
else:
   mocklib = "libMockTest.so"

# Generate type info for "f", and "entry" so we can call them.
module, resolver = generate( mocklib,
                             "proggen.py",
                             [],
                             [ "f", "g", "entry", "entry_g" ] )

# Load the DLL, and decorate the functions with their prototypes.
# We use RTLD_GLOBAL so the mocking framework does not need to be passed the
# handle to the dlopen'd library to find the symbol for the named function
lib = ctypes.CDLL( mocklib, ctypes.RTLD_GLOBAL )
module.decorateFunctions( lib )

# if f is not mocked, we expect the behaviour from the C function
def checkNotMocked():
   lib.entry( 1, 2 )

# if f is mocked, we expect the behaviour from the Python function
def checkMocked():
   lib.entry( 100, 101 )
Пример #2
0
#     limitations under the License.
from __future__ import absolute_import, division, print_function

import sys
from ctypes import CDLL
from CTypeGen import generate
import CMock

if len(sys.argv) >= 2:
    mocklib = sys.argv[1]
else:
    mocklib = "libPreMockTest.so"

# Generate type info for "preF", and "preEntry" so we can call them.
module, resolver = generate(
    mocklib, "premock.py", [],
    ["preEntry", "preF", "preRecurse", "preRecurseEntry"])

# Load the DLL, and decorate the functions with their prototypes.
lib = CDLL(mocklib)
module.decorateFunctions(lib)

# Test our pre-style mocks work. These will be called before the
# original C function, and control will then pass to the original
# function
# We create a mock function that sets a global python variable,
# and modifies an argument to the original.
# The real C function asserts the value is that received from
# The python mock function, and the python code checks the global
# was set.
Пример #3
0
#!/usr/bin/env python
from CTypeGen import generate, PythonType

generate(["libcallback.so"], "callbackgen.py", [PythonType("Callback")],
         ["callme"])
Пример #4
0
from CTypeGen import generate, PythonType

generate(["libbasic.so"], "basic.py", [PythonType("SomeStructure")],
         ["someFunction"])
Пример #5
0
    warnCount = 0
    warnings = []


def testwarning(txt):
    global warnCount
    print("warning: %s" % txt)
    warnCount += 1
    warnings.append(txt)


# test some common error cases:
# Test non-existent file
module, generator = generate("/nosuchfile",
                             "CTypeSanity.py",
                             types,
                             functions,
                             errorfunc=testwarning,
                             globalVars=globalVars)

assert warnCount == 1
assert "No such file" in warnings[0]
clearWarnings()

# Test filename not a string.
module, generator = generate(42,
                             "CTypeSanity.py",
                             types,
                             functions,
                             errorfunc=testwarning,
                             globalVars=globalVars)
Пример #6
0
from __future__ import absolute_import, division, print_function
from CTypeGen import generate
import CMock
import CMock.helpers
import ctypes
import sys

#  Get our libFOpenTest library with the fopen_test function to call
if len(sys.argv) >= 2:
    mocklib = sys.argv[1]
else:
    mocklib = "libFOpenTest.so"

# Generate type info for "fopen_test"
module, resolver = generate(mocklib, "ptrgen.py", [], [
    "fopen_test",
])

dll = ctypes.CDLL(mocklib)
module.decorateFunctions(dll)

# We'll mock fopen/fopen64 from libc.
libc, _ = CMock.helpers.getLibc()
openFiles = []


# redirect all fopen and fopen64 calls to open /dev/zero
def impl(name, mode, func):
    global openFiles
    openFiles.append(name)
    return func(b"/dev/zero", mode)
Пример #7
0
    "in6addrinfo",
    "_IO_FILE_complete",
    "_IO_FILE_complete_plus",
    "_IO_lock_t",
    "printf_info",
    "printf_spec",
    "pthread",
    "raise",  # python keyword.
    "stackblock",
    "timex",
    "_Unwind_Exception",
])


def haveDyn(die):
    ''' Filter for functions that are in the .dynsym section - we can't call
   other functions anyway, and CTypeGen will just generate a warning if they
   appear. '''
    obj = die.object()
    name = die.DW_AT_linkage_name
    if name is None:
        name = die.name()
    return name in obj.dynnames()


generate(
    ["./libdbghelper.so", sys.argv[1]],
    sys.argv[2],
    types=lambda name, space, die: name not in broken,
    functions=lambda name, space, die: name not in broken and haveDyn(die))
Пример #8
0
    warnings = []


def testwarning(txt):
    global warnCount
    print("warning: %s" % txt)
    warnCount += 1
    warnings.append(txt)


# test some common error cases:

# Test filename not a string.
module, generator = generate(42,
                             "CTypeSanity.py",
                             types,
                             functions,
                             errorfunc=testwarning,
                             globalVars=globalVars)

assert warnCount == 1
assert "requires a list of ELF images" in warnings[0]
clearWarnings()

# generate the module, complete with warnings
module, generator = generate([sanitylib],
                             "CTypeSanity.py",
                             types,
                             functions,
                             errorfunc=testwarning,
                             globalVars=globalVars)
Пример #9
0
#! /usr/bin/env python
from CTypeGen import generate
import paths

generate([ paths.libc ], "libcgen.py", [], ["_IO_fgets", "_IO_puts"])
generate([ paths.testme ], "testmegen.py", [], ["functionToTest"])

Пример #10
0
from __future__ import print_function
from ctypes import CDLL, POINTER
import sys
from CTypeGen import generate

if len(sys.argv) >= 2:
    sanitylib = sys.argv[1]
else:
    sanitylib = "./libGreedyTest.so"

warnCount = 0
warnings = []

module, generator = generate(sanitylib,
                             "GreedyTest.py",
                             types=lambda name, space, die: True,
                             functions=lambda name, space, die: True,
                             globalVars=lambda name, space, die: True)

dll = CDLL(sanitylib)
module.decorateFunctions(dll)

f = dll.create_f(42)
assert isinstance(f, POINTER(module.f))
assert f[0].input == 42
assert f[0].inputx2 == 42 * 2
assert isinstance(f[0].g, POINTER(module.g))
assert f[0].g[0].inputx3 == 42 * 3
assert f[0].g[0].inputx4 == 42 * 4
assert module.Globals(dll).global42.value == 42