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