| 1 |
Notes on tests |
| 2 |
============================ |
| 3 |
The semanage_access_check test in the semanage_store suite simulates a |
| 4 |
read-only filesystem by using DAC permissions. Consequently, these tests |
| 5 |
will fail if run as root, as root can override DAC permissions. |
| 6 |
|
| 7 |
|
| 8 |
How to add and use unit tests |
| 9 |
============================= |
| 10 |
|
| 11 |
We are using the CUnit unit testing framework. This framework--and the |
| 12 |
official documentation of the framework--may be found here: |
| 13 |
|
| 14 |
http://cunit.sourceforge.net/ |
| 15 |
|
| 16 |
If you have not yet installed CUnit, first do that. (There is an RPM, |
| 17 |
or you can compile from source.) Once installed, follow these steps to |
| 18 |
add unit tests for your code: |
| 19 |
|
| 20 |
1. Create a .h and .c file corresponding to the .c file you want to test. |
| 21 |
For example, test_semanage_store.c provides tests of the functions in |
| 22 |
semanage_store.c. Your new .h/.c files represent a suite of related |
| 23 |
tests. |
| 24 |
|
| 25 |
2. Write or add new tests to a suite. Tests are simply functions that |
| 26 |
take the form: |
| 27 |
|
| 28 |
void test_my_function(void) |
| 29 |
|
| 30 |
These tests are where you will make calls to the CUnit assertions. |
| 31 |
|
| 32 |
If you are making a new test suite, also add the suite init/cleanup |
| 33 |
functions. These take the form: |
| 34 |
|
| 35 |
int <suite_name>_test_init(void) |
| 36 |
int <suite_name>_cleanup(void) |
| 37 |
|
| 38 |
These functions will be called before and after the test functions |
| 39 |
in your suite, respectively. They return 0 on success, 1 on failure. |
| 40 |
|
| 41 |
3. Update libsemanage-tests.c to add your new suite and/or your new tests |
| 42 |
using the DECLARE_SUITE macro in do_tests(). |
| 43 |
|
| 44 |
4. Update the Makefile: |
| 45 |
+ Make sure that the TESTSRC variable is set to the location |
| 46 |
of the libsemanage source code you want to test. |
| 47 |
|
| 48 |
5. Compile the libsemanage source code you will be testing, to ensure |
| 49 |
the object files are available and up to date. |
| 50 |
|
| 51 |
6. Run your tests. Rejoice or despair, as appropriate. |
| 52 |
|
| 53 |
|
| 54 |
A note on the the utilities.c: Add functions that can be commonly used |
| 55 |
here. For example, it is handy to have a dummy message callback |
| 56 |
function to silence error messages produced by libsemanage and keep |
| 57 |
your output pretty. To do this, include utilities.h and specify the |
| 58 |
callback like so: |
| 59 |
|
| 60 |
semanage_handle_t *sh; |
| 61 |
sh = semanage_handle_create(); |
| 62 |
sh->msg_callback = test_msg_handler; |
| 63 |
|
| 64 |
Feel free to add other such functions here as well. |