libpoldiff-tests.c

Go to the documentation of this file.
00001 /**
00002  *  @file
00003  *
00004  *  CUnit testing framework for libpoldiff's correctness.
00005  *
00006  *  @author Paul Rosenfeld prosenfeld@tresys.com
00007  *
00008  *  Copyright (C) 2007 Tresys Technology, LLC
00009  *
00010  *  This library is free software; you can redistribute it and/or
00011  *  modify it under the terms of the GNU Lesser General Public
00012  *  License as published by the Free Software Foundation; either
00013  *  version 2.1 of the License, or (at your option) any later version.
00014  *
00015  *  This library is distributed in the hope that it will be useful,
00016  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018  *  Lesser General Public License for more details.
00019  *
00020  *  You should have received a copy of the GNU Lesser General Public
00021  *  License along with this library; if not, write to the Free Software
00022  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00023  */
00024 
00025 #include <config.h>
00026 
00027 #include "libpoldiff-tests.h"
00028 
00029 #include <CUnit/Basic.h>
00030 #include <CUnit/TestDB.h>
00031 
00032 #include <apol/util.h>
00033 #include <apol/vector.h>
00034 #include <stdio.h>
00035 #include <errno.h>
00036 #include <stdint.h>
00037 #include <stdlib.h>
00038 #include <string.h>
00039 
00040 #include "components-tests.h"
00041 #include "rules-tests.h"
00042 #include "mls-tests.h"
00043 #include "nomls-tests.h"
00044 
00045 apol_vector_t *string_array_to_vector(char *arr[])
00046 {
00047         apol_vector_t *v = apol_vector_create(free);
00048         int i;
00049         for (i = 0; arr[i] != NULL; ++i) {
00050                 apol_vector_append(v, strdup(arr[i]));
00051         }
00052         return v;
00053 }
00054 
00055 char *vector_to_string(const apol_vector_t * v, const char *pre, const char *sep)
00056 {
00057         char *item = NULL, *str = NULL, *tmp = NULL;
00058         size_t i = 0, str_len = 0, tmp_len = 0;
00059         size_t num_elements = apol_vector_get_size(v);
00060         for (i = 0; v && i < num_elements; i++) {
00061                 item = apol_vector_get_element(v, i);
00062                 if (apol_str_appendf(&tmp, &tmp_len, "%s%s", sep, item) < 0) {
00063                         return NULL;
00064                 }
00065         }
00066         apol_str_trim(tmp);
00067         if (tmp) {
00068                 apol_str_appendf(&str, &str_len, "%s%s", pre, tmp);
00069         } else {
00070                 str = strdup("");
00071         }
00072         free(tmp);
00073         return str;
00074 }
00075 
00076 apol_vector_t *shallow_copy_str_vec_and_sort(const apol_vector_t * v)
00077 {
00078         apol_vector_t *copy = apol_vector_create_from_vector(v, NULL, NULL, NULL);
00079         apol_vector_sort(copy, apol_str_strcmp, NULL);
00080         return copy;
00081 }
00082 
00083 void run_test(component_funcs_t * component_funcs, poldiff_test_answers_t * poldiff_test_answers, test_numbers_e test_num)
00084 {
00085         added_v = apol_vector_create(free);
00086         removed_v = apol_vector_create(free);
00087         modified_v = apol_vector_create(free);
00088         modified_name_only_v = apol_vector_create(free);
00089         switch (test_num) {
00090         case COMPONENT:
00091                 build_component_vecs(component_funcs);
00092                 break;
00093         case RULES_AVRULE:
00094                 build_avrule_vecs();
00095                 break;
00096         case RULES_TERULE:
00097                 build_terule_vecs();
00098                 break;
00099         case RULES_ROLEALLOW:
00100                 build_roleallow_vecs();
00101                 break;
00102         case RULES_ROLETRANS:
00103                 build_roletrans_vecs();
00104                 break;
00105         case MLS_CATEGORY:
00106                 build_category_vecs();
00107                 break;
00108         case MLS_LEVEL:
00109                 build_level_vecs();
00110                 break;
00111         case MLS_RANGETRANS:
00112                 build_rangetrans_vecs();
00113                 break;
00114         case MLS_USER:
00115                 build_user_vecs();
00116                 break;
00117         }
00118         size_t first_diff;
00119         apol_vector_t *intersect = NULL, *all_changes = NULL;
00120         if (!(all_changes = apol_vector_create(NULL))) {
00121                 goto err;
00122         }
00123         apol_vector_cat(all_changes, added_v);
00124         apol_vector_cat(all_changes, removed_v);
00125         apol_vector_cat(all_changes, modified_name_only_v);
00126         if (!
00127             (intersect =
00128              apol_vector_create_from_intersection(all_changes, poldiff_test_answers->correct_unchanged_v, compare_str, NULL))) {
00129                 goto err;
00130         }
00131         /* unchanged */
00132         CU_ASSERT_EQUAL(apol_vector_get_size(intersect), 0);
00133         /* added */
00134         apol_vector_sort(added_v, compare_str, NULL);
00135         apol_vector_sort(poldiff_test_answers->correct_added_v, compare_str, NULL);
00136         int test_result;
00137         CU_ASSERT_FALSE(test_result =
00138                         apol_vector_compare(added_v, poldiff_test_answers->correct_added_v, compare_str, NULL, &first_diff));
00139         if (test_result) {
00140                 print_test_failure(added_v, poldiff_test_answers->correct_added_v, first_diff, "Added");
00141         }
00142         /* removed */
00143         apol_vector_sort(removed_v, compare_str, NULL);
00144         apol_vector_sort(poldiff_test_answers->correct_removed_v, compare_str, NULL);
00145         CU_ASSERT_FALSE(test_result =
00146                         apol_vector_compare(removed_v, poldiff_test_answers->correct_removed_v, compare_str, NULL, &first_diff));
00147         if (test_result) {
00148                 print_test_failure(removed_v, poldiff_test_answers->correct_removed_v, first_diff, "Removed");
00149         }
00150         /* modified */
00151         apol_vector_sort(modified_v, compare_str, NULL);
00152         apol_vector_sort(poldiff_test_answers->correct_modified_v, compare_str, NULL);
00153         CU_ASSERT_FALSE(test_result =
00154                         apol_vector_compare(modified_v, poldiff_test_answers->correct_modified_v, compare_str, NULL, &first_diff));
00155         if (test_result) {
00156                 print_test_failure(modified_v, poldiff_test_answers->correct_modified_v, first_diff, "Modified");
00157         }
00158 
00159         apol_vector_destroy(&intersect);
00160         apol_vector_destroy(&added_v);
00161         apol_vector_destroy(&removed_v);
00162         apol_vector_destroy(&modified_name_only_v);
00163         apol_vector_destroy(&modified_v);
00164         apol_vector_destroy(&all_changes);
00165         return;
00166       err:
00167         apol_vector_destroy(&intersect);
00168         apol_vector_destroy(&added_v);
00169         apol_vector_destroy(&removed_v);
00170         apol_vector_destroy(&modified_name_only_v);
00171         apol_vector_destroy(&modified_v);
00172         apol_vector_destroy(&all_changes);
00173         CU_FAIL_FATAL("Could not initialize vectors for test");
00174 }
00175 
00176 void print_test_failure(apol_vector_t * actual, apol_vector_t * expected, size_t first_diff, const char *test_name)
00177 {
00178         printf("\nTEST FAILED\n");
00179         size_t i;
00180         printf("--- ACTUAL RESULT (%s) -----\n", test_name);
00181         for (i = first_diff; i < apol_vector_get_size(actual); ++i) {
00182                 char *item = (char *)apol_vector_get_element(actual, i);
00183                 printf("\t%3d. %s\n", (int)i, item);
00184         }
00185         printf("--- EXPECTED RESULT (%s) ---\n", test_name);
00186         for (i = first_diff; i < apol_vector_get_size(expected); ++i) {
00187                 char *item = (char *)apol_vector_get_element(expected, i);
00188                 printf("\t%3d. %s\n", (int)i, item);
00189         }
00190 }
00191 
00192 int compare_str(const void *s1, const void *s2, void *debug)
00193 {
00194         char *str1 = strdup((char *)s1);
00195         char *str2 = strdup((char *)s2);
00196         apol_str_trim(str1);
00197         apol_str_trim(str2);
00198         int result = strcmp(str1, str2);
00199         free(str1);
00200         free(str2);
00201         return result;
00202 }
00203 
00204 poldiff_test_answers_t *init_answer_vectors(char *added_arr[], char *removed_arr[], char *unchanged_arr[], char *modified_arr[])
00205 {
00206         poldiff_test_answers_t *answers = (poldiff_test_answers_t *) malloc(sizeof(poldiff_test_answers_t));
00207         answers->correct_added_v = string_array_to_vector(added_arr);
00208         answers->correct_removed_v = string_array_to_vector(removed_arr);
00209         answers->correct_unchanged_v = string_array_to_vector(unchanged_arr);
00210         answers->correct_modified_v = string_array_to_vector(modified_arr);
00211         return answers;
00212 }
00213 
00214 component_funcs_t *init_test_funcs(poldiff_get_diff_vector get_diff_vector, poldiff_get_name get_name, poldiff_get_form get_form,
00215                                    poldiff_get_added get_added, poldiff_get_removed get_removed)
00216 {
00217         component_funcs_t *funcs = (component_funcs_t *) malloc(sizeof(component_funcs_t));
00218         funcs->get_diff_vector = get_diff_vector;
00219         funcs->get_name = get_name;
00220         funcs->get_form = get_form;
00221         funcs->get_added = get_added;
00222         funcs->get_removed = get_removed;
00223         return funcs;
00224 }
00225 
00226 poldiff_t *init_poldiff(char *orig_base_path, char *mod_base_path)
00227 {
00228         poldiff_t *return_diff = NULL;
00229         uint32_t flags = POLDIFF_DIFF_ALL;
00230         apol_policy_path_t *mod_pol_path = NULL;
00231         apol_policy_path_t *orig_pol_path = NULL;
00232 
00233         orig_pol_path = apol_policy_path_create(APOL_POLICY_PATH_TYPE_MONOLITHIC, orig_base_path, NULL);
00234         if (!orig_pol_path) {
00235                 ERR(NULL, "%s", strerror(errno));
00236                 goto err;
00237         }
00238 
00239         mod_pol_path = apol_policy_path_create(APOL_POLICY_PATH_TYPE_MONOLITHIC, mod_base_path, NULL);
00240         if (!mod_pol_path) {
00241                 ERR(NULL, "%s", strerror(errno));
00242                 goto err;
00243         }
00244 
00245         orig_policy = apol_policy_create_from_policy_path(orig_pol_path, 0, NULL, NULL);
00246         if (!orig_policy) {
00247                 ERR(NULL, "%s", strerror(errno));
00248                 goto err;
00249         }
00250 
00251         mod_policy = apol_policy_create_from_policy_path(mod_pol_path, 0, NULL, NULL);
00252         if (!mod_policy) {
00253                 ERR(NULL, "%s", strerror(errno));
00254                 goto err;
00255         }
00256 
00257         if (!(return_diff = poldiff_create(orig_policy, mod_policy, NULL, NULL))) {
00258                 ERR(NULL, "%s", strerror(errno));
00259                 goto err;
00260         }
00261         if (poldiff_run(return_diff, flags)) {
00262                 goto err;
00263         }
00264         apol_policy_path_destroy(&orig_pol_path);
00265         apol_policy_path_destroy(&mod_pol_path);
00266         return return_diff;
00267       err:
00268         apol_policy_destroy(&orig_policy);
00269         apol_policy_destroy(&mod_policy);
00270         apol_policy_path_destroy(&orig_pol_path);
00271         apol_policy_path_destroy(&mod_pol_path);
00272         poldiff_destroy(&return_diff);
00273         return NULL;
00274 }
00275 
00276 void cleanup_test(poldiff_test_answers_t * answers)
00277 {
00278         if (answers != NULL) {
00279                 apol_vector_destroy(&answers->correct_added_v);
00280                 apol_vector_destroy(&answers->correct_unchanged_v);
00281                 apol_vector_destroy(&answers->correct_removed_v);
00282                 apol_vector_destroy(&answers->correct_modified_v);
00283                 free(answers);
00284         }
00285 }
00286 
00287 int poldiff_cleanup()
00288 {
00289         poldiff_destroy(&diff);
00290         return 0;
00291 }
00292 
00293 int main()
00294 {
00295         if (CU_initialize_registry() != CUE_SUCCESS) {
00296                 return CU_get_error();
00297         }
00298 
00299         CU_TestInfo components_tests_arr[] = {
00300                 {"Attributes", components_attributes_tests}
00301                 ,
00302                 {"Classes", components_class_tests}
00303                 ,
00304                 {"Commons", components_commons_tests}
00305                 ,
00306                 {"Roles", components_roles_tests}
00307                 ,
00308                 {"Users", components_users_tests}
00309                 ,
00310                 {"Bools", components_bools_tests}
00311                 ,
00312                 {"Types", components_types_tests}
00313                 ,
00314                 CU_TEST_INFO_NULL
00315         };
00316 
00317         CU_TestInfo rules_tests_arr[] = {
00318                 {"AV Rules", rules_avrules_tests}
00319                 ,
00320                 {"TE Rules", rules_terules_tests}
00321                 ,
00322                 {"Role Allow Rules", rules_roleallow_tests}
00323                 ,
00324                 {"Role Transition Rules", rules_roletrans_tests}
00325                 ,
00326                 CU_TEST_INFO_NULL
00327         };
00328 
00329         CU_TestInfo mls_tests_arr[] = {
00330                 {"Categories", mls_category_tests}
00331                 ,
00332                 {"Levels", mls_level_tests}
00333                 ,
00334                 {"Range Transitions", mls_rangetrans_tests}
00335                 ,
00336                 {"Users (MLS)", mls_user_tests}
00337                 ,
00338                 CU_TEST_INFO_NULL
00339         };
00340         CU_TestInfo nomls_tests_arr[] = {
00341                 {"Changed & Unchanged Users", nomls_tests}
00342                 ,
00343                 CU_TEST_INFO_NULL
00344         };
00345 
00346         CU_SuiteInfo suites[] = {
00347                 {"Components", components_test_init, poldiff_cleanup, components_tests_arr}
00348                 ,
00349                 {"Rules", rules_test_init, poldiff_cleanup, rules_tests_arr}
00350                 ,
00351                 {"MLS", mls_test_init, poldiff_cleanup, mls_tests_arr}
00352                 ,
00353                 {"Non-MLS vs. MLS Users", nomls_test_init, poldiff_cleanup, nomls_tests_arr}
00354                 ,
00355                 CU_SUITE_INFO_NULL
00356         };
00357 
00358         CU_register_suites(suites);
00359         CU_basic_set_mode(CU_BRM_VERBOSE);
00360         CU_basic_run_tests();
00361         unsigned int num_failures = CU_get_number_of_failure_records();
00362         CU_cleanup_registry();
00363         return (int)num_failures;
00364 }