Unit testing and mocking with cmocka Unit testing and mocking with - - PowerPoint PPT Presentation

unit testing and mocking with cmocka unit testing and
SMART_READER_LITE
LIVE PREVIEW

Unit testing and mocking with cmocka Unit testing and mocking with - - PowerPoint PPT Presentation

Unit testing and mocking with cmocka Unit testing and mocking with cmocka SambaXP 2018 SambaXP 2018 Andreas Schneider Red Hat Samba Maintainer About me About me Free and Open Source Software Developer: cmocka - a unit testing framework for


slide-1
SLIDE 1

Unit testing and mocking with cmocka Unit testing and mocking with cmocka

SambaXP 2018 SambaXP 2018

Andreas Schneider

Red Hat Samba Maintainer

slide-2
SLIDE 2

About me About me

Free and Open Source Software Developer: cmocka - a unit testing framework for C Samba - The domain controller and file server libssh - The SSH Library cwrap - Client/Server testing made easy LineageOS - Android with Privacy Features

slide-3
SLIDE 3

1 What is this talk about? What is this talk about?

slide-4
SLIDE 4

What is this talk about? What is this talk about?

The talk will answer the following questions: What is cmocka? What featues does cmocka provide? What is mocking? How to write a mocking test?

slide-5
SLIDE 5

2 What is cmocka? What is cmocka?

slide-6
SLIDE 6

cmocka ... cmocka ...

is an elegant unit testing framework for C

  • ffers support for mock objects.

it only requires the standard C library

slide-7
SLIDE 7

cmocka ... cmocka ...

works on a range of computing platforms (including embedded) and works with different compilers. Linux/BSD/Windows - GCC/Clang/MSVC

slide-8
SLIDE 8

Mission Statement Mission Statement

The goal of this project is to provide a powerful testing framework for C, on different platforms and operating systems, which only requires the standard C library.

slide-9
SLIDE 9

Website Website

https:/ /cmocka.org/

slide-10
SLIDE 10

3 Features of cmocka Features of cmocka

slide-11
SLIDE 11

Test Fixtures and groups Test Fixtures and groups

Test fixtures are setup and teardown functions that can be shared across multiple test cases to provide common functions that prepare the test environment and destroy it afterwards. This is also supported for groups.

slide-12
SLIDE 12

Exception handling Exception handling

cmocka is able to recover the test state if there are exceptions like a segfault. Handling for SIGSEGV, SIGILL, etc. An attached debugger will stop when the segfault

  • ccurs
slide-13
SLIDE 13

Exception handling Exception handling

cmocka doesn't use fork() for exception handling in test cases!

fork() is not available on all platforms fork() is implemented diffrently on some OSes

(Linux vs. MacOSX)

slide-14
SLIDE 14

Output formats Output formats

cmocka has it's own console output format, but supports additional message formats like: Test Anything Protocol Subunit (used by Samba) xUnit XML (parsed by Jenkins)

slide-15
SLIDE 15

API Documentation API Documentation

https:/ /api.cmocka.org/

slide-16
SLIDE 16

A cmocka test A cmocka test

#include <stdarg.h> #include <stddef.h> #include <setjmp.h> #include <cmocka.h>

slide-17
SLIDE 17

A cmocka test A cmocka test

/* A test case that does nothing and succeeds. */ static void null_test_success(void **state) { (void) state; /* unused */ }

slide-18
SLIDE 18

A cmocka test A cmocka test

int main(void) { const struct CMUnitTest tests[] = { cmocka_unit_test(null_test_success), }; return cmocka_run_group_tests(tests, NULL, NULL); }

slide-19
SLIDE 19

Assert functions Assert functions

What you will mostly use are assert functions. Examples:

assert_true(x) assert_false(x) assert_non_null(x) assert_null(x) assert_int_equal(a, b) assert_int_not_equal(a, b) ...

slide-20
SLIDE 20

Assert functions Assert functions

There are functions for comparing strings, memory, pointers and sets. You can also check ranges and return codes.

slide-21
SLIDE 21

A cmocka test with assert function A cmocka test with assert function

/* A test case that compare intetergers and fails. */ static void integer_failure(void **state) { int i = 4; assert_int_equal(i, 5); }

slide-22
SLIDE 22

Extending assert functions Extending assert functions

You can also easily extend cmocka writing special assert functions for your project. Example: socket_wrapper tests offer:

assert_sockaddr_equal(ss, a) and assert_sockaddr_port_equal(ss, a, prt)

slide-23
SLIDE 23

assert() assert()

If you test code which use assert(), you could redefine assert() and test it!

#define assert mock_assert void showmessage(const char *message) { assert(message); } int main(void) { expect_assert_failure(show_message(NULL)); printf("succeeded\n"); return 0; }

slide-24
SLIDE 24

4 Mocking in unit tests Mocking in unit tests

slide-25
SLIDE 25

What is mocking? What is mocking?

Mocking is a way to create instrumented objects that simulate the behavior of real objects.

slide-26
SLIDE 26

What is mocking? What is mocking?

to mock = to imitate something Mocking in unit testing is a way to isolate behaviour of complex algorithms. This is useful if some functions are impractical to incorporate into the unit test.

slide-27
SLIDE 27

Lets test 'uptime' Lets test 'uptime'

./example/uptime/uptime up 3 days, 24 minutes

slide-28
SLIDE 28

Standard unit test Standard unit test

slide-29
SLIDE 29

Mocking test Mocking test

slide-30
SLIDE 30

GNU linker magic GNU linker magic

Use a wrapper function for a symbol. Supported by ld.bfd, ld.gold and llvm-ld

ld --wrap=<symbol>

slide-31
SLIDE 31

Linker function wrapping Linker function wrapping

If the function prototype is: We implement in the the mock function called:

int uptime(double *uptime_secs, double *idle_secs) int __wrap_uptime(double *uptime_secs, double *idle_sec { ... }

slide-32
SLIDE 32

Linker function wrapping Linker function wrapping

The symbol to the origial uptime() function will be renamed to: __real_uptime The uptime symbol is rebound to __wrap_uptime This way we can still call the original function if needed!

slide-33
SLIDE 33

Smybol binding order! Smybol binding order!

Symbols are searched and bound by the linker in the follow order:

  • 1. The executable itself
  • 2. Preloaded libraries
  • 3. Libraries in linking order
slide-34
SLIDE 34

Debug symbol binding Debug symbol binding

With GNU ld.so .. See 'man ld.so'

LD_DEBUG=symbols ./examples/uptime/uptime

slide-35
SLIDE 35

Parameter checking Parameter checking

  • > Checking Parameters

int __wrap_mock(char *name) { check_expected(name); } void test_foo(void **state) { expect_string(__wrap_mock, name, "wurst"); foo("wurstbrot"); }

https:/ /api.cmocka.org/

slide-36
SLIDE 36

Mocks Mocks

  • > Mock Objects

int __wrap_mock(char *name) { return mock_type(int); } void test_foo(void **state) { int rc; will_return(__wrap_mock, 0); rc = foo("wurstbrot"); assert_return_code(rc, errno); }

https:/ /api.cmocka.org/

slide-37
SLIDE 37

Call ordering Call ordering

Allows you to check that mock functions are called in the right order!

  • > Call Ordering

https:/ /api.cmocka.org/

slide-38
SLIDE 38

5 How to write a mocking test? How to write a mocking test?

slide-39
SLIDE 39

6 How to write a cmocka test in Samba? How to write a cmocka test in Samba?

slide-40
SLIDE 40

GAME OVER GAME OVER