selinux/libsemanage/tests
Christian Göttsche 0bafe764aa libsemanage/tests: misc cleanup
* Explicitly mark unused function parameters and drop compiler warning
  override
* Move declaration of `semanage_handle_t *sh` from individual source
  files to utilities.h, since it is defined in utilities.c
* Declare file local variables static
* Drop unused macros
* Avoid casts dropping const qualifier
* Avoid usage of reserved identifier names (leading underscores)
* Silence UBSAN underflow warning by adding explicit cast

Signed-off-by: Christian Göttsche <cgzones@googlemail.com>
Acked-by: James Carter <jwcart2@gmail.com>
2024-11-21 15:32:22 -05:00
..
.gitignore libsemanage: add helper functions to tests 2019-06-19 09:01:48 -07:00
libsemanage-tests.c libsemanage/tests: rename bool identifiers 2023-05-03 09:15:26 -04:00
Makefile libsemanage/tests: misc cleanup 2024-11-21 15:32:22 -05:00
nc_sort_malformed initial import from svn trunk revision 2950 2008-08-19 15:30:36 -04:00
nc_sort_sorted initial import from svn trunk revision 2950 2008-08-19 15:30:36 -04:00
nc_sort_unsorted initial import from svn trunk revision 2950 2008-08-19 15:30:36 -04:00
README libsemanage: white space cleanup 2024-11-21 15:28:48 -05:00
test_bool.c libsemanage/tests: misc cleanup 2024-11-21 15:32:22 -05:00
test_bool.cil libsemanage: test semanage_bool_* functions 2019-06-19 09:01:48 -07:00
test_bool.h libsemanage/tests: rename bool identifiers 2023-05-03 09:15:26 -04:00
test_fcontext.c libsemanage/tests: misc cleanup 2024-11-21 15:32:22 -05:00
test_fcontext.cil libsemanage: test semanage_fcontext functions 2019-06-19 09:01:48 -07:00
test_fcontext.h libsemanage: test semanage_fcontext functions 2019-06-19 09:01:48 -07:00
test_handle.c libsemanage/tests: misc cleanup 2024-11-21 15:32:22 -05:00
test_handle.cil libsemanage: test semanage_handle_* functions 2019-06-19 09:01:48 -07:00
test_handle.h libsemanage: test semanage_handle_* functions 2019-06-19 09:01:48 -07:00
test_ibendport.c libsemanage/tests: misc cleanup 2024-11-21 15:32:22 -05:00
test_ibendport.cil libsemanage: test semanage_ibendport_* functions 2019-06-19 09:01:48 -07:00
test_ibendport.h libsemanage: test semanage_ibendport_* functions 2019-06-19 09:01:48 -07:00
test_iface.c libsemanage/tests: misc cleanup 2024-11-21 15:32:22 -05:00
test_iface.cil libsemanage: test semanage_iface_* functions 2019-06-19 09:01:48 -07:00
test_iface.h libsemanage: test semanage_iface_* functions 2019-06-19 09:01:48 -07:00
test_node.c libsemanage/tests: misc cleanup 2024-11-21 15:32:22 -05:00
test_node.cil libsemanage: test semanage_node_* functions 2019-06-19 09:01:48 -07:00
test_node.h libsemanage: test semanage_node_* functions 2019-06-19 09:01:48 -07:00
test_other.c libsemanage/tests: misc cleanup 2024-11-21 15:32:22 -05:00
test_other.h libsemanage: test semanage_context_* functions 2019-06-19 09:01:48 -07:00
test_port.c libsemanage/tests: misc cleanup 2024-11-21 15:32:22 -05:00
test_port.cil libsemanage: test semanage_port_* functions 2019-06-19 09:01:48 -07:00
test_port.h libsemanage: test semanage_port_* functions 2019-06-19 09:01:48 -07:00
test_semanage_store.c libsemanage/tests: misc cleanup 2024-11-21 15:32:22 -05:00
test_semanage_store.h initial import from svn trunk revision 2950 2008-08-19 15:30:36 -04:00
test_user.c libsemanage/tests: misc cleanup 2024-11-21 15:32:22 -05:00
test_user.cil libsemanage: test semanage_user_* functions 2019-06-19 09:01:48 -07:00
test_user.h libsemanage: test semanage_user_* functions 2019-06-19 09:01:48 -07:00
test_utilities.c libsemanage/tests: misc cleanup 2024-11-21 15:32:22 -05:00
test_utilities.h initial import from svn trunk revision 2950 2008-08-19 15:30:36 -04:00
utilities.c libsemanage/tests: misc cleanup 2024-11-21 15:32:22 -05:00
utilities.h libsemanage/tests: misc cleanup 2024-11-21 15:32:22 -05:00

Notes on tests
============================
The semanage_access_check test in the semanage_store suite simulates a
read-only filesystem by using DAC permissions. Consequently, these tests
will fail if run as root, as root can override DAC permissions.


How to add and use unit tests
=============================

We are using the CUnit unit testing framework.  This framework--and the
official documentation of the framework--may be found here:

http://cunit.sourceforge.net/

If you have not yet installed CUnit, first do that.  (There is an RPM,
or you can compile from source.)  Once installed, follow these steps to
add unit tests for your code:

1. Create a .h and .c file corresponding to the .c file you want to test.
   For example, test_semanage_store.c provides tests of the functions in
   semanage_store.c.   Your new .h/.c files represent a suite of related
   tests.

2. Write or add new tests to a suite.  Tests are simply functions that
   take the form:

	void test_my_function(void)

   These tests are where you will make calls to the CUnit assertions.

   If you are making a new test suite, also add the suite init/cleanup
   functions.  These take the form:

   	int <suite_name>_test_init(void)
    int <suite_name>_cleanup(void)

   These functions will be called before and after the test functions
   in your suite, respectively.  They return 0 on success, 1 on failure.

3. Update libsemanage-tests.c to add your new suite and/or your new tests
   using the DECLARE_SUITE macro in do_tests().

4. Update the Makefile:
     + Make sure that the TESTSRC variable is set to the location
	   of the libsemanage source code you want to test.

5. Compile the libsemanage source code you will be testing, to ensure
   the object files are available and up to date.

6. Run your tests.  Rejoice or despair, as appropriate.


A note on the the utilities.c: Add functions that can be commonly used
here.  For example, it is handy to have a dummy message callback
function to silence error messages produced by libsemanage and keep
your output pretty.  To do this, include utilities.h and specify the
callback like so:

        semanage_handle_t *sh;
        sh = semanage_handle_create();
        sh->msg_callback = test_msg_handler;

Feel free to add other such functions here as well.