Giter VIP home page Giter VIP logo

mimick's Introduction

Mimick

Unix Build Status Windows Build Status Coverity License Version Gitter Room

A KISS, cross-platform C Mocking library

Philosophy

Warning: this library is still experimental. APIs will change before the beta phase and things will break. Use at your own risk.

Mimick aims to be a simple of use and powerful mocking and stubbing library for C.

It doesn't rely on external code generation or compiler plugin to work -- simply link the library to your tests, and you're good to go!

Mimick works by poisoning the GOT of the target dynamic module at runtime, making any call to the target functions routed to one of its own stubs. The mock API is only sugar on top of the stub API, and provides a default stub suitable for testing interactions and specifying behaviour.

Samples

Here is a simple usage of Mimick to mock the malloc function:

#include <stdlib.h>
#include <assert.h>
#include <mimick/mimick.h>

/* Define the blueprint of a mock identified by `malloc_proto`
   that returns a `void *` and takes a `size_t` parameter. */
mmk_mock_define (malloc_mock, void *, size_t);

int main(void) {
    /* Mock the malloc function in the current module using 
       the `malloc_mock` blueprint. */
    mmk_mock("malloc@self", malloc_mock);

    /* Tell the mock to return NULL and set errno to ENOMEM
       whatever the given parameter is. */
    void *result = NULL;
    mmk_when(malloc(mmk_any(size_t)),
            .then_return = &result,
            .then_errno = ENOMEM);

    assert(malloc(42) == result && errno == ENOMEM);

    mmk_reset(malloc);
}

Or, alternatively:

malloc_mock mock = mmk_mock("malloc@self", malloc_mock);

void *result = NULL;
mmk_when(mock(mmk_any(size_t)),
        .then_return = &result,
        .then_errno = ENOMEM);

assert(malloc(42) == result && errno == ENOMEM);

mmk_reset(mock);

Other samples may be found in the samples directory.

Compatibility matrix

Supported Compilers: GCC 4.6+, Clang 3.5+, MSVC 14+

Legend:

: Supported.
: Unsupported.
?: Not Tested, but is expected to work.
: Does not exist / not applicable.
~: Works on some conditions.

Arch Linux OS X/iOS FreeBSD Windows
x86
x86_64
ARM ?
ARM64 ? ?
PPC
RISCV32
RISCV64 ?

F.A.Q.

Q. Can I mock/stub static functions?
A. No. Static functions are, by definition, private implementations details, and because they are not exported nor visible to the outside world they cannot (and should not) be mocked/stubbed.

Q. Can I mock/stub functions inside a static library?
A. Maybe. Functions inside a static library are moved inside the executable and are not called using the PLT by default. You need to build your library to use Position-Independent Code, otherwise the functions cannot be stubbed nor mocked.

Q. I am mocking a standard library function, but mmk_when and mmk_verify are not working, why is this happening?
A. It's very possible that your compiler is optimizing away your function call inside mmk_when or mmk_verify since it has determined that there are no visible side effects in not calling the function. Compile your tests without optimizations (with a compiler flag or the mmk_no_optimize function attribute if it is available to your compiler), or use the function pointer returned by mmk_mock rather than the function itself.

Q. Does this run on embedded systems / bare metal?
A. Yes, but on very specific conditions. Your code must be position-independent, call functions using a global offset table or similar mechanism, and use an executable format that Mimick can handle (ELF, PE, Mach-O).
You also need to provide definitions for some functions like malloc, realloc, and free.

mimick's People

Contributors

snaipe avatar hidmic avatar ziyao233 avatar brawner avatar audrow avatar brettrd avatar clalancette avatar blast545 avatar mjcarroll avatar phdittmann avatar traversaro avatar eboasson avatar jpace121 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.