Giter VIP home page Giter VIP logo

narmock's People

Contributors

actions-user avatar dependabot-preview[bot] avatar vberlier avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

munken

narmock's Issues

Function declaration parsing fails

Hello,

Narmock fails to mock the function ml_mount() with error: 'ml_mount' undeclared.

I added debug prints to the narmock source code, and it seems the function declaration parser fails to parse a typedef.

The commands and files I used to get the error:

$ gcc -E main.c > main.pp.c
$ cat main.pp.c | PYTHONPATH=../../../../narmock python3 -m narmock -g 
code:
typedef int __builtin_va_list;
typedef long int ptrdiff_t;
typedef long unsigned int size_t;
typedef int wchar_t;
typedef struct {
  long long __max_align_ll __attribute__((__aligned__(__alignof__(long long))));
  long double __max_align_ld __attribute__((__aligned__(__alignof__(long double))));
# 437 "/usr/lib/gcc/x86_64-linux-gnu/8/include/stddef.h" 3 4
} max_align_t;
int ml_mount(const char *source_p,
             const char *target_p,
             const char *type_p);
parse error :6:28: before: __attribute__
error: 'ml_mount' undeclared

main.c

#include "__mocks__.h"

int ml_mount(const char *source_p,
             const char *target_p,
             const char *type_p);

int main(void)
{ 
    MOCK(ml_mount)->mock_return(0);

    return (0);
}

main.pp.c

# 1 "main.c"
# 1 "<built-in>"
# 1 "<command-line>"
# 31 "<command-line>"
# 1 "/usr/include/stdc-predef.h" 1 3 4
# 32 "<command-line>" 2
# 1 "main.c"
# 1 "__mocks__.h" 1
# 15 "__mocks__.h"
# 1 "/usr/lib/gcc/x86_64-linux-gnu/8/include/stddef.h" 1 3 4
# 149 "/usr/lib/gcc/x86_64-linux-gnu/8/include/stddef.h" 3 4

# 149 "/usr/lib/gcc/x86_64-linux-gnu/8/include/stddef.h" 3 4
typedef long int ptrdiff_t;
# 216 "/usr/lib/gcc/x86_64-linux-gnu/8/include/stddef.h" 3 4
typedef long unsigned int size_t;
# 328 "/usr/lib/gcc/x86_64-linux-gnu/8/include/stddef.h" 3 4
typedef int wchar_t;
# 426 "/usr/lib/gcc/x86_64-linux-gnu/8/include/stddef.h" 3 4
typedef struct {
  long long __max_align_ll __attribute__((__aligned__(__alignof__(long long))));
  long double __max_align_ld __attribute__((__aligned__(__alignof__(long double))));
# 437 "/usr/lib/gcc/x86_64-linux-gnu/8/include/stddef.h" 3 4
} max_align_t;
# 16 "__mocks__.h" 2
# 2 "main.c" 2


# 3 "main.c"
int ml_mount(const char *source_p,
             const char *target_p,
             const char *type_p);

int main(void)
{
    (_narmock_get_mock_for_ml_mount((void *)&ml_mount))->mock_return(0);

    return (0);
}

variadic an idea

Hi

I've been toying around with how to get variadic functions working.
It uses the gcc builtin in functions (https://gcc.gnu.org/onlinedocs/gcc-4.1.2/gcc/Constructing-Calls.html) to forward the call.

A hacked together example for ioctl can be seen here: https://gist.github.com/Munken/df6828e275e128a8174c6e5851776a4d

This could be combined with something like:

int ioctl_(int fd, unsigned long req, int a) {
  return a;
}

MOCK(ioctl)->capture_variadic("%d");
MOCK(ioctl)->mock_implementation(ioctl_);

ioctl(1, 8, 123);

ASSERT_EQ((int) MOCK(ioctl)->get_vararg(0), 123);

The __wrap_xxx would then use va_start, va_arg and va_end to capture the varargs into an array.

argument names in last_call

Hello,

why are the arguments called arg1, arg2, ... in last_call instead of the names in the function declaration?

Suggestion: call_count as int

This is not a bug, but a suggestion.

I usually pass -Wextra to gcc, which makes

ASSERT_EQ(MOCK(mount)->call_count, 1);

give the compile error

main.c: In function ‘_narwhal_test_function_ml_mount_ok’:
/home/erik/workspace/monolinux/ml/tst/utils/narwhal.h:263:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     default: (left) == (right))

Replacing 1 with 1u obviously solves the problem, but I would prefer declaring call_count as int instead as it makes the code slightly easier to read and write.

pycparser 2.21 breaks generation

The example/basic breaks with the latest pycparser version:

  File "/home/munken/.local/bin//narmock", line 8, in <module>
    sys.exit(main())
  File "/home/munken/.local/lib/python3.8/site-packages/narmock/cli.py", line 44, in main
    generate_mocks(expanded_code=args.g.read(), directory=args.d, keep_args=args.k)
  File "/home/munken/.local/lib/python3.8/site-packages/narmock/api.py", line 16, in generate_mocks
    generator.add_mock(function)
  File "/home/munken/.local/lib/python3.8/site-packages/narmock/generator.py", line 184, in add_mock
    self.mocks.append(GeneratedMock(mocked_function))
  File "/home/munken/.local/lib/python3.8/site-packages/narmock/generator.py", line 113, in __init__
    self.return_value_decl = decl(
  File "/home/munken/.local/lib/python3.8/site-packages/narmock/generator.py", line 19, in decl
    return node.Decl(name, [], [], [], type, None, None)
TypeError: __init__() missing 1 required positional argument: 'bitsize'

This is caused by pycparser adding another argument to Decl:
eliben/pycparser#435

Steps to reproduce

 % git clone https://github.com/vberlier/narmock.git              /tmp/bla P14s
Cloning into 'narmock'...
remote: Enumerating objects: 835, done.
remote: Counting objects: 100% (69/69), done.
remote: Compressing objects: 100% (69/69), done.
remote: Total 835 (delta 35), reused 0 (delta 0), pack-reused 766
Receiving objects: 100% (835/835), 188.08 KiB | 6.27 MiB/s, done.
Resolving deltas: 100% (579/579), done.
 % cd narmock/examples                                            /tmp/bla P14s
 % cd basic                                      narmock/examples (master) P14s
 % python3 -m venv venv                          examples/basic (master) P14s
 % source venv/bin/activate
 % pip install narmock                             examples/basic (master) P14s
Requirement already satisfied: narmock in /home/munken/.local/lib/python3.8/site-packages (0.2.12)
Requirement already satisfied: pycparser<3.0,>=2.19 in /home/munken/.local/lib/python3.8/site-packages (from narmock) (2.21)
Requirement already satisfied: jinja2<3.0,>=2.10 in /usr/lib/python3/dist-packages (from narmock) (2.10.1)
 % touch main.c                                    examples/basic (master) P14s
 % make                                            examples/basic (master) P14s
cc -E main.c | narmock -g
Traceback (most recent call last):
  File "/home/munken/.local/bin//narmock", line 8, in <module>
    sys.exit(main())
  File "/home/munken/.local/lib/python3.8/site-packages/narmock/cli.py", line 44, in main
    generate_mocks(expanded_code=args.g.read(), directory=args.d, keep_args=args.k)
  File "/home/munken/.local/lib/python3.8/site-packages/narmock/api.py", line 16, in generate_mocks
    generator.add_mock(function)
  File "/home/munken/.local/lib/python3.8/site-packages/narmock/generator.py", line 184, in add_mock
    self.mocks.append(GeneratedMock(mocked_function))
  File "/home/munken/.local/lib/python3.8/site-packages/narmock/generator.py", line 113, in __init__
    self.return_value_decl = decl(
  File "/home/munken/.local/lib/python3.8/site-packages/narmock/generator.py", line 19, in decl
    return node.Decl(name, [], [], [], type, None, None)
TypeError: __init__() missing 1 required positional argument: 'bitsize'
make: *** [Makefile:16: __mocks__.h] Error 1

mocking open fails

When trying to mock the open system call it fails with:

Traceback (most recent call last):
  File "/home/munk/src/libvmemock/test/venv/bin/narmock", line 8, in <module>
    sys.exit(main())
  File "/home/munk/src/libvmemock/test/venv/lib/python3.8/site-packages/narmock/cli.py", line 44, in main
    generate_mocks(expanded_code=args.g.read(), directory=args.d, keep_args=args.k)
  File "/home/munk/src/libvmemock/test/venv/lib/python3.8/site-packages/narmock/api.py", line 15, in generate_mocks
    for function in collect_mocked_functions(expanded_code, keep_args):
  File "/home/munk/src/libvmemock/test/venv/lib/python3.8/site-packages/narmock/inspect.py", line 40, in collect_mocked_functions
    yield from ForgivingDeclarationParser(expanded_source_code, functions, keep_args)
  File "/home/munk/src/libvmemock/test/venv/lib/python3.8/site-packages/narmock/inspect.py", line 192, in __iter__
    function = self.parse_function_declaration()
  File "/home/munk/src/libvmemock/test/venv/lib/python3.8/site-packages/narmock/inspect.py", line 314, in parse_function_declaration
    else rename_arguments(file_ast.ext[-1]),
  File "/home/munk/src/libvmemock/test/venv/lib/python3.8/site-packages/narmock/inspect.py", line 53, in rename_arguments
    param_type = param.type
AttributeError: 'EllipsisParam' object has no attribute 'type'

Code to reproduce:

#include "narwhal.h"
#include "__mocks__.h"

#include <fcntl.h>

TEST(example)
{
  MOCK(open)->mock_return(1);

  ASSERT_EQ(open("a", 0), 1);
}

Version of narmock:

Name: narmock
Version: 0.2.14
Summary: A minimal mocking utility for C projects.
Home-page: https://github.com/vberlier/narmock
Author: Valentin Berlier
Author-email: [email protected]
License: MIT
Location: /home/munk/src/libvmemock/test/venv/lib/python3.8/site-packages
Requires: pycparser, Jinja2
Required-by: 

Mocking a function taking void as its only argument does not work

Hello!

Mocking a function taking void as its only argument does not work (or explicitly no arguments really).

int func(void);

#include <narwhal/narwhal.h>

TEST(foo)
{
    MOCK(func)->disable_mock();
    func();
}

int main()
{
    return RUN_TESTS(foo);
}

Generate the mock.

gcc -E main.c | narmock -g mocks.c

The generated functions has one arguemnt, arg1, of the invalid type void.

...
// NARMOCK_IMPLEMENTATION func

int __real_func(void arg1);
...

Compiler error messages with and without function pointers in MOCK()

Hello!

I replaced the MOCK() macro and its function pointers with plain functions in my fork for better error messags from the compiler when the wrong arguments are given. See below for compiler error messages with MOCK() and plain functions.

MOCK()

poetry run make -C tests
make: Entering directory '/home/erik/workspace/narmock/tests'
cc -E dummy_functions.c main.c narwhal.c | narmock -g
cc -Wall -Wextra -Wconversion -std=c11 -g -Og -fsanitize=address -fno-omit-frame-pointer $(narmock -f) *.c -o run_tests
In file included from main.c:7:
main.c: In function ‘_narwhal_test_function_pipe_function’:
__mocks__.h:19:25: error: too many arguments to function ‘_narmock_get_mock_for_pipe((const void *)&pipe)->mock_once’
 #define MOCK(function) (_narmock_get_mock_for_##function((void *)&function))
                        ~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
main.c:95:5: note: in expansion of macro ‘MOCK’
     MOCK(pipe)
     ^~~~
make: *** [Makefile:22: run_tests] Error 1
make: Leaving directory '/home/erik/workspace/narmock/tests'

Plain function

poetry run make -C tests
make: Entering directory '/home/erik/workspace/narmock/tests'
cc -E dummy_functions.c main.c narwhal.c | narmock -g
cc -Wall -Wextra -Wconversion -std=c11 -g -Og -fsanitize=address -fno-omit-frame-pointer $(narmock -f) *.c -o run_tests
main.c: In function ‘_narwhal_test_function_pipe_function_set_out’:
main.c:93:5: error: too many arguments to function ‘pipe_mock_once’
     pipe_mock_once(42, 1);
     ^~~~~~~~~~~~~~
In file included from main.c:7:
__mocks__.h:123:6: note: declared here
 void pipe_mock_once(int return_value);
      ^~~~~~~~~~~~~~
make: *** [Makefile:22: run_tests] Error 1
make: Leaving directory '/home/erik/workspace/narmock/tests'

Reset all the mocks

Previously discussed here.

Adding a function to reset all the mocks would make it possible for Narwhal to avoid unexpected behavior when a function mocked by the user is also used internally.

narmock_reset_all_mocks();

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.