Skip to contentSkip to conversation

libsepol unit test infrastructure

PATCH In review Joshua Brindle 1 participant 1 message Aug 3, 2006 SELinux security module

lore
Share & tools
lore
#0 lore raw

PATCH 1/5 libsepol unit test infrastructure

This patch adds the new infrastructure that makes adding new tests much easier and fixes the existing tests to use it.

libsepol/tests/helpers.c +81 -0
diff -pruN -x policies trunk.old/libsepol/tests/helpers.c trunk/libsepol/tests/helpers.c
@@ -0,0 +1,81 @@
+/*
+ * Author: Joshua Brindle <[email protected]>
+ *         Chad Sellers <[email protected]>
+ *
+ * Copyright (C) 2006 Tresys Technology, LLC
+ *
+ *  This library is free software; you can redistribute it and/or
+ *  modify it under the terms of the GNU Lesser General Public
+ *  License as published by the Free Software Foundation; either
+ *  version 2.1 of the License, or (at your option) any later version.
+ *
+ *  This library is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ *  Lesser General Public License for more details.
+ *
+ *  You should have received a copy of the GNU Lesser General Public
+ *  License along with this library; if not, write to the Free Software
+ *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ */
+
+/* This has helper functions that are common between tests */
+
+#include "helpers.h"
+#include "parse_util.h"
+
+#include <sepol/policydb/expand.h>
+#include <sepol/policydb/avrule_block.h>
+
+#include <CUnit/Basic.h>
+
+#include <stdlib.h>
+#include <limits.h>
+
+int test_load_policy(policydb_t * p, int policy_type, int mls, const char *test_name, const char *policy_name)
+{
+	char filename[PATH_MAX];
+
+	if (mls) {
+		if (snprintf(filename, PATH_MAX, "policies/%s/%s.mls", test_name, policy_name) < 0) {
+			return -1;
+		}
+	} else {
+		if (snprintf(filename, PATH_MAX, "policies/%s/%s.std", test_name, policy_name) < 0) {
+			return -1;
+		}
+	}
+
+	if (policydb_init(p)) {
+		fprintf(stderr, "Out of memory");
+		return -1;
+	}
+
+	p->policy_type = policy_type;
+	p->mls = mls;
+
+	if (read_source_policy(p, filename, test_name)) {
+		fprintf(stderr, "failed to read policy %s\n", filename);
+		policydb_destroy(p);
+		return -1;
+	}
+
+	return 0;
+}
+
+avrule_decl_t *test_find_decl_by_sym(policydb_t * p, int symtab, char *sym)
+{
+	scope_datum_t *scope = (scope_datum_t *) hashtab_search(p->scope[symtab].table, sym);
+
+	if (scope == NULL) {
+		return NULL;
+	}
+	if (scope->scope != SCOPE_DECL) {
+		return NULL;
+	}
+	if (scope->decl_ids_len != 1) {
+		return NULL;
+	}
+
+	return p->decl_val_to_struct[scope->decl_ids[0] - 1];
+}
libsepol/tests/helpers.h +59 -0

59 lines not on this page · raw message

libsepol/tests/libsepol-tests.c +75 -13

103 lines not on this page · raw message

libsepol/tests/Makefile +34 -13

62 lines not on this page · raw message

libsepol/tests/test-cond.c +11 -14
diff -pruN -x policies trunk.old/libsepol/tests/test-cond.c trunk/libsepol/tests/test-cond.c
@@ -20,6 +20,7 @@
 
 #include "test-cond.h"
 #include "parse_util.h"
+#include "helpers.h"
 
 #include <sepol/policydb/policydb.h>
 #include <sepol/policydb/link.h>
@@ -31,34 +32,31 @@ static policydb_t base_expanded;
 
 int cond_test_init(void)
 {
-	if (policydb_init(&basemod)) {
-		fprintf(stderr, "out of memory!\n");
-		return -1;
-	}
-
 	if (policydb_init(&base_expanded)) {
 		fprintf(stderr, "out of memory!\n");
+		policydb_destroy(&basemod);
 		return -1;
 	}
 
-	basemod.policy_type = POLICY_BASE;
-	basemod.mls = 1;
-
-	if (read_source_policy(&basemod, "policies/refpolicy-base.conf",
-			       "test-cond"))
-		return -1;
+	if (test_load_policy(&basemod, POLICY_BASE, 1, "test-cond", "refpolicy-base.conf"))
+		goto cleanup;
 
 	if (link_modules(NULL, &basemod, NULL, 0, 0)) {
 		fprintf(stderr, "link modules failed\n");
-		return -1;
+		goto cleanup;
 	}
 
 	if (expand_module(NULL, &basemod, &base_expanded, 0, 1)) {
 		fprintf(stderr, "expand module failed\n");
-		return -1;
+		goto cleanup;
 	}
 
 	return 0;
+
+      cleanup:
+	policydb_destroy(&basemod);
+	policydb_destroy(&base_expanded);
+	return -1;
 }
 
 int cond_test_cleanup(void)
@@ -91,7 +89,6 @@ static void test_cond_expr_equal(void)
 int cond_add_tests(CU_pSuite suite)
 {
 	if (NULL == CU_add_test(suite, "cond_expr_equal", test_cond_expr_equal)) {
-		CU_cleanup_registry();
 		return CU_get_error();
 	}
 	return 0;

--
This message was distributed to subscribers of the selinux mailing list. If you no longer wish to subscribe, send mail to [email protected] with the words "unsubscribe selinux" without quotes as the message.