• Stars
    star
    148
  • Rank 249,983 (Top 5 %)
  • Language
    C
  • License
    MIT License
  • Created over 8 years ago
  • Updated 8 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A KISS, cross-platform C mocking library

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.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 βœ• βœ• βœ• βˆ„

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.

More Repositories

1

Criterion

A cross-platform C and C++ unit testing framework for the 21st century
C
1,977
star
2

libcsptr

Smart pointers for the (GNU) C programming language
CMake
1,478
star
3

wssdl

Wireshark-Specific Dissector Language
Lua
194
star
4

fmem

A cross-platform library for opening memory-backed libc streams.
C
59
star
5

dot-git

Managing your dotfiles the Git Wayβ„’
Shell
44
star
6

BoxFort

Convenient & cross-platform sandboxing C library
C
42
star
7

python-rst2ansi

A python utility for rendering RST in the terminal
Python
32
star
8

confloose

The best startup scripts to prank IT people with
22
star
9

table2ascii

A simple proof of concept for rendering tables in ascii
Python
21
star
10

malloc

Simple malloc implementation for POSIX systems w/ sbrk(2)
C
18
star
11

toy-containers

Fast, lightweight toy container system
Go
11
star
12

Tizzy

A C library dedicated to error handling and panicking.
C
9
star
13

dyncall

Git mirror of http://hg.dyncall.org/pub/dyncall/dyncall
C
8
star
14

Insight

A reflection library for C and C++
C++
5
star
15

lastpass-ssh-agent

A SSH agent that authenticates you from keys in your lastpass vault
C
5
star
16

gitlab-runner-kubevirt

A gitlab-runner custom executor driver for running jobs with Kubevirt
Go
4
star
17

ttysend

A simplistic C utility to send data to other tty/pts.
C
4
star
18

colorutils

A small linux (command-line) utility to make the framebuffer a splashscreen
C
3
star
19

Tequila

A Minecraft server manager
Python
3
star
20

x-nix

A linux compatibility layer for multiple platforms
C
3
star
21

go-htutil

Go HTTP utilities, with no dependencies
Go
2
star
22

arm-stm32l1

A sample project for the stm32l1 using libopencm3
C
2
star
23

.files

My dotfiles. Check https://github.com/Snaipe/dot-git for the workflow.
Python
2
star
24

go-pass

Prompting passwords securely in Go
Go
2
star
25

sweet

A sway-to-i3 IPC compatibility layer
Go
2
star
26

boa

Better configuration management in Go
Go
2
star
27

Thieves

A thief plugin for Bukkit
Java
2
star
28

articles

Articles on http://snaipe.me/
2
star
29

metaheuristic

Python
2
star
30

packages

Packaging sources for my software
Shell
1
star
31

go-pager

A Go writer that abstracts `$PAGER` in a POSIX-compliant way.
Go
1
star
32

BlackDog

A Bukkitdev-to-maven-repository mapper
Python
1
star
33

go-hterrors

Convenience library for handling non-2xx HTTP errors
Go
1
star
34

Vapor

A java library for p2p sharing
Java
1
star
35

docker-alpine-qemu

Foreign-arch docker images running on x86_64 hosts.
Shell
1
star
36

meson-check

A meson module to conveniently check for features
Python
1
star
37

Damon

Functional constructs for the D programming language
D
1
star
38

libdwarfplusplus

A C++ wrapper over libdwarf
C++
1
star