Skip to contentSkip to conversation

Libsepol test framework

PATCH In review Karl MacMillan 2 participants 3 messages May 2, 2006 – May 8, 2006

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Libsepol test framework

Steve,

Attached are two patches to add a simple test framework to libsepol and, in the future, to the other libraries. It depends CUnit
(http://cunit.sourceforge.net/), a workable if not exciting unit testing framework for C.

The compiler-cleanup patch is needed to make it easier for the unit tests to load source policies. I think that using source policies for unit testing libsepol will be more maintainable over the long run. It also removes some code duplication between checkpolicy.c and checkmodule.c.

The test-framework patch, which depends upon the computer-cleanup patch, adds a simple test to libsepol and the makefile changes
to introduce a top-level make target "test". Example output from running the test target is included below.

The test policy is in a separate email to you because it is large (a complete base policy.conf from reference policy).

More patches with additional tests will be submitted over time.

Please apply.

Thanks,

Karl

Signature · 20 lines
Karl MacMillan
Tresys Technology
www.tresys.com

Test example:

./libsepol-tests


     CUnit - A Unit testing framework for C - Version 2.1-0
     http://cunit.sourceforge.net/


Suite: cond
  Test: cond_expr_equal ... passed

--Run Summary: Type      Total     Ran  Passed  Failed
               suites        1       1     n/a       0
               tests         1       1       1       0
               asserts   19044   19044   19044       0
checkmodule.c renamed from ../trunk/checkpolicy/checkmodule.c +20 -62
diff -ruN --exclude=.svn ../../trunk/checkpolicy/checkmodule.c checkpolicy/checkmodule.c
@@ -30,24 +30,15 @@
 
 #include "queue.h"
 #include "checkpolicy.h"
+#include "parse_util.h"
 
 extern char *optarg;
 extern int optind;
 
 static sidtab_t sidtab;
 
-extern policydb_t *policydbp;
-extern queue_t id_queue;
-extern unsigned int policydb_errors;
-extern unsigned long policydb_lineno;
-extern char source_file[];
 extern int mlspol;
 
-extern FILE *yyin;
-extern void init_parser(int);
-extern int yyparse(void);
-extern void yyrestart(FILE *);
-
 static char *txtfile = "policy.conf";
 static char *binfile = "policy";
 
@@ -109,50 +100,6 @@
         return 0;
 }
 
-static int read_source_policy(policydb_t *p, char *file, char *progname)
-{
-        yyin = fopen(file, "r");
-        if (!yyin) {
-                fprintf(stderr, "%s:  unable to open %s\n", progname, 
-                        file);
-                return -1;
-        }
-        
-        if (policydb_init(p) ||
-            ((id_queue = queue_create()) == NULL)) {
-                fprintf(stderr, "%s: out of memory!\n", progname);
-                return -1;
-        }
-
-	p->policy_type = policy_type;
-	p->mls = mlspol;
-
-        init_parser(1);
-        if (yyparse() || policydb_errors) {
-                fprintf(stderr, "%s:  error(s) encountered while parsing configuration\n", progname);
-                return -1;
-        }
-        rewind(yyin);
-        init_parser(2);
-        source_file[0] = '\0';
-        yyrestart(yyin);
-        if (yyparse() || policydb_errors) {
-                fprintf(stderr, "%s:  error(s) encountered while parsing configuration\n", progname);
-                return -1;
-        }
-        queue_destroy(id_queue);
-
-        if (hierarchy_check_constraints(NULL, p)) {
-                return -1;
-        }
-        
-        if (policydb_errors) 
-                return -1;
-
-        fclose(yyin);
-        return 0;
-}
-
 static int write_binary_policy(policydb_t *p, char *file, char *progname)
 {
 	FILE *outfp = NULL;
@@ -253,18 +200,29 @@
         sepol_set_policydb(&modpolicydb);
         sepol_set_sidtab(&sidtab);
 
-        policydbp = &modpolicydb;
 	if (binary) {
-                if (read_binary_policy(policydbp, file, argv[0]) == -1) {
+                if (read_binary_policy(&modpolicydb, file, argv[0]) == -1) {
                         exit(1);
                 }
 	} else {
-                if (read_source_policy(policydbp, file, argv[0]) == -1) {
+		if (policydb_init(&modpolicydb)) {
+			fprintf(stderr, "%s: out of memory!\n", argv[0]);
+			return -1;
+		}
+
+		modpolicydb.policy_type = policy_type;
+		modpolicydb.mls = mlspol;
+
+                if (read_source_policy(&modpolicydb, file, argv[0]) == -1) {
                         exit(1);
                 }
+
+		if (hierarchy_check_constraints(NULL, &modpolicydb)) {
+			return -1;
+		}
         }
 
-	if (policydbp->policy_type == POLICY_BASE) {
+	if (modpolicydb.policy_type == POLICY_BASE) {
 		/* Verify that we can successfully expand the base module. */
 		policydb_t kernpolicydb;
 
@@ -272,24 +230,24 @@
 			fprintf(stderr, "%s:  policydb_init failed\n", argv[0]);
 			exit(1);
 		}
-		if (link_modules(NULL, policydbp, NULL, 0, 0)) {
+		if (link_modules(NULL, &modpolicydb, NULL, 0, 0)) {
 			fprintf(stderr, "%s:  link modules failed\n", argv[0]);
 			exit(1);
 		}
-		if (expand_module(NULL, policydbp, &kernpolicydb, 0, 1)) {
+		if (expand_module(NULL, &modpolicydb, &kernpolicydb, 0, 1)) {
 			fprintf(stderr, "%s:  expand module failed\n", argv[0]);
 			exit(1);
 		}
 		policydb_destroy(&kernpolicydb);
 	}
 
-	if (policydb_load_isids(policydbp, &sidtab))
+	if (policydb_load_isids(&modpolicydb, &sidtab))
 		exit(1);
 
 	printf("%s:  policy configuration loaded\n", argv[0]);
 
 	if (outfile &&
-            write_binary_policy(policydbp, outfile, argv[0]) == -1) {
+            write_binary_policy(&modpolicydb, outfile, argv[0]) == -1) {
                 exit(1);
         }
 	return 0;
checkpolicy.c renamed from ../trunk/checkpolicy/checkpolicy.c +2 -37
diff -ruN --exclude=.svn ../../trunk/checkpolicy/checkpolicy.c checkpolicy/checkpolicy.c
@@ -80,6 +80,7 @@
 
 #include "queue.h"
 #include "checkpolicy.h"
+#include "parse_util.h"
 
 extern char *optarg;
 extern int optind;
@@ -88,17 +89,8 @@
 static sidtab_t sidtab;
 
 extern policydb_t *policydbp;
-extern queue_t id_queue;
-extern unsigned int policydb_errors;
-extern unsigned long policydb_lineno;
-extern char source_file[];
 extern int mlspol;
 
-extern FILE *yyin;
-extern void init_parser(int);
-extern int yyparse(void);
-extern void yyrestart(FILE *);
-
 static char *txtfile = "policy.conf";
 static char *binfile = "policy";
 
@@ -498,13 +490,6 @@
 	} else {
                 policydb_t parse_policy;
 
-                yyin = fopen(file, "r");
-		if (!yyin) {
-			fprintf(stderr, "%s:  unable to open %s\n", argv[0], 
-				file);
-			exit(1);
-		}
-
 		if (policydb_init(&parse_policy))
 			exit(1);
 		/* We build this as a base policy first since that is all the parser understands */
@@ -513,28 +498,9 @@
 		/* Let sepol know if we are dealing with MLS support */
 		parse_policy.mls = mlspol;
 
-		id_queue = queue_create();
-		if (!id_queue) {
-			fprintf(stderr, "%s:  out of memory\n", argv[0]);
-			exit(1);
-		}
 		policydbp = &parse_policy;
-                init_parser(1);
-		if (yyparse() || policydb_errors) {
-			fprintf(stderr, "%s:  error(s) encountered while parsing configuration\n", argv[0]);
-			exit(1);
-		}
-		rewind(yyin);
-                init_parser(2);
-		source_file[0] = '\0';
-		yyrestart(yyin);
-		if (yyparse() || policydb_errors) {
-			fprintf(stderr, "%s:  error(s) encountered while parsing configuration\n", argv[0]);
-			exit(1);
-		}
-		queue_destroy(id_queue);
 
-		if (policydb_errors) 
+		if (read_source_policy(policydbp, file, "checkpolicy") < 0)
 			exit(1);
 
 		if (hashtab_map(policydbp->p_levels.table, check_level, NULL))
@@ -557,7 +523,6 @@
                 }
 		policydb_destroy(&parse_policy);
 		policydbp = &policydb;
-		fclose(yyin);
 	}
 
 	if (policydb_load_isids(&policydb, &sidtab))
Makefile renamed from ../trunk/checkpolicy/Makefile +1 -1
diff -ruN --exclude=.svn ../../trunk/checkpolicy/Makefile checkpolicy/Makefile
@@ -12,7 +12,7 @@
 
 override CFLAGS += -I. -I${INCLUDEDIR}
 
-CHECKOBJS = y.tab.o lex.yy.o queue.o module_compiler.o
+CHECKOBJS = y.tab.o lex.yy.o queue.o module_compiler.o parse_util.o
 CHECKPOLOBJS = $(CHECKOBJS) checkpolicy.o
 CHECKMODOBJS = $(CHECKOBJS) checkmodule.o
 
parse_util.c renamed from ../trunk/checkpolicy/parse_util.c +74 -0

74 lines not on this page · raw message

parse_util.h renamed from ../trunk/checkpolicy/parse_util.h +35 -0
diff -ruN --exclude=.svn ../../trunk/checkpolicy/parse_util.h checkpolicy/parse_util.h
@@ -0,0 +1,35 @@
+/*
+ * Author: Karl MacMillan <[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
+ */
+
+/* Utility functions shared by checkpolicy and checkmodule */
+
+#ifndef __PARSE_UTIL_H__
+#define __PARSE_UTIL_H__
+
+#include <sepol/policydb/policydb.h>
+
+/* Read a source policy and populate the policydb passed in. The
+ * policydb must already have been created and configured (e.g.,
+ * expected policy type set. The string progname is used for
+ * error messages. No checking of assertions, hierarchy, etc.
+ * is done. */
+int read_source_policy(policydb_t *p, char *file, char *progname);
+
+#endif
libselinux/Makefile renamed from ../trunk/libselinux/Makefile +1 -0
diff --exclude=.svn -ruN --exclude=checkpolicy --exclude=refpolicy-base.conf ../../trunk/libselinux/Makefile ./libselinux/Makefile
@@ -21,3 +21,4 @@
 	$(MAKE) -C src $@
 	$(MAKE) -C utils clean
 
+test:
\ No newline at end of file
libsemanage/Makefile renamed from ../trunk/libsemanage/Makefile +1 -0
diff --exclude=.svn -ruN --exclude=checkpolicy --exclude=refpolicy-base.conf ../../trunk/libsemanage/Makefile ./libsemanage/Makefile
@@ -18,3 +18,4 @@
 clean distclean:
 	$(MAKE) -C src $@
 
+test:
\ No newline at end of file
libsepol/include/sepol/policydb/conditional.h renamed from ../trunk/libsepol/include/sepol/policydb/conditional.h +1 -0
diff --exclude=.svn -ruN --exclude=checkpolicy --exclude=refpolicy-base.conf ../../trunk/libsepol/include/sepol/policydb/conditional.h ./libsepol/include/sepol/policydb/conditional.h
@@ -91,6 +91,7 @@
 extern int cond_evaluate_expr(policydb_t *p, cond_expr_t *expr);
 extern cond_expr_t *cond_copy_expr(cond_expr_t *expr);
 
+extern int cond_expr_equal(cond_node_t *a, cond_node_t *b);
 extern int cond_normalize_expr(policydb_t *p, cond_node_t *cn);
 extern void cond_node_destroy(cond_node_t *node);
 extern void cond_expr_destroy(cond_expr_t *expr);
libsepol/Makefile renamed from ../trunk/libsepol/Makefile +5 -0
diff --exclude=.svn -ruN --exclude=checkpolicy --exclude=refpolicy-base.conf ../../trunk/libsepol/Makefile ./libsepol/Makefile
@@ -1,6 +1,7 @@
 all: 
 	$(MAKE) -C src 
 	$(MAKE) -C utils
+	$(MAKE) -C tests
 
 install: 
 	$(MAKE) -C include install
@@ -14,4 +15,8 @@
 clean:
 	$(MAKE) -C src clean
 	$(MAKE) -C utils clean
+	$(MAKE) -C tests clean
+
+test:
+	$(MAKE) -C tests test
 
libsepol/src/conditional.c renamed from ../trunk/libsepol/src/conditional.c +1 -1
diff --exclude=.svn -ruN --exclude=checkpolicy --exclude=refpolicy-base.conf ../../trunk/libsepol/src/conditional.c ./libsepol/src/conditional.c
@@ -86,7 +86,7 @@
 /*
  * Determine if two conditional expressions are equal. 
  */
-static int cond_expr_equal(cond_node_t *a, cond_node_t *b)
+int cond_expr_equal(cond_node_t *a, cond_node_t *b)
 {
 	cond_expr_t *cur_a, *cur_b;
 		
libsepol/tests/libsepol-tests.c renamed from ../trunk/libsepol/tests/libsepol-tests.c +49 -0

49 lines not on this page · raw message

libsepol/tests/Makefile renamed from ../trunk/libsepol/tests/Makefile +28 -0
diff --exclude=.svn -ruN --exclude=checkpolicy --exclude=refpolicy-base.conf ../../trunk/libsepol/tests/Makefile ./libsepol/tests/Makefile
@@ -0,0 +1,28 @@
+# Statically link libsepol on the assumption that we are going to
+# be testing internal functions.
+LIBSEPOL = -L../src/ -static -lsepol
+
+# In order to load source policies we need to link in the checkpolicy/checkmodule parser and util code.
+# This is less than ideal, but it makes the tests easier to maintain by allowing source policies
+# to be loaded directly.
+CHECKPOLICY = ../../checkpolicy/
+PARSEROBJS = $(CHECKPOLICY)queue.o $(CHECKPOLICY)y.tab.o $(CHECKPOLICY)parse_util.o $(CHECKPOLICY)lex.yy.o \
+	$(CHECKPOLICY)module_compiler.o
+INCLUDE = -I../include/ -I$(CHECKPOLICY)
+
+OBJS= $(patsubst %.c,%.o,$(wildcard *.c))
+CFLAGS = -g -o0 -Wall -W -Wundef -Wmissing-noreturn -Wmissing-format-attribute -Wno-unused-parameter
+
+EXE = libsepol-tests
+
+all: $(OBJS)
+	$(CC) $(CFLAGS) $(INCLUDE) $(OBJS) $(PARSEROBJS) -lfl -lcunit -lcurses $(LIBSEPOL) -o $(EXE)
+
+%.o: %.c
+	$(CC) $(CFLAGS) $(INCLUDE) -c $*.c -o $*.o
+
+clean: 
+	rm -rf $(OBJS) $(EXE)
+
+test: all
+	./$(EXE)
libsepol/tests/test-cond.c renamed from ../trunk/libsepol/tests/test-cond.c +99 -0

99 lines not on this page · raw message

libsepol/tests/test-cond.h renamed from ../trunk/libsepol/tests/test-cond.h +30 -0
diff --exclude=.svn -ruN --exclude=checkpolicy --exclude=refpolicy-base.conf ../../trunk/libsepol/tests/test-cond.h ./libsepol/tests/test-cond.h
@@ -0,0 +1,30 @@
+/*
+ * Author: Karl MacMillan <[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
+ */
+
+#ifndef __TEST_COND_H__
+#define __TEST_COND_H__
+
+#include <CUnit/Basic.h>
+
+int cond_test_init(void);
+int cond_test_cleanup(void);
+int cond_add_tests(CU_pSuite suite);
+
+#endif
Makefile renamed from ../trunk/Makefile +6 -1
diff --exclude=.svn -ruN --exclude=checkpolicy --exclude=refpolicy-base.conf ../../trunk/Makefile ./Makefile
@@ -6,7 +6,7 @@
 	export LDFLAGS = -g
 endif
 
-install relabel: 
+install relabel:
 	@for subdir in $(SUBDIRS); do \
 		(cd $$subdir && $(MAKE) $@) || exit 1; \
 	done
@@ -20,3 +20,8 @@
 	@for subdir in $(SUBDIRS); do \
 		(cd $$subdir && $(MAKE) $@) || exit 1; \
 	done
+
+test:
+	@for subdir in $(SUBDIRS); do \
+		(cd $$subdir && $(MAKE) $@) || exit 1; \
+	done
policycoreutils/Makefile renamed from ../trunk/policycoreutils/Makefile +1 -0
diff --exclude=.svn -ruN --exclude=checkpolicy --exclude=refpolicy-base.conf ../../trunk/policycoreutils/Makefile ./policycoreutils/Makefile
@@ -5,3 +5,4 @@
 		(cd $$subdir && $(MAKE) $@) || exit 1; \
 	done
 
+test:
#1 lore raw
Quoted 18 lines · Karl MacMillan

Steve,

Attached are two patches to add a simple test framework to libsepol and, in the future, to the other libraries. It depends CUnit
(http://cunit.sourceforge.net/), a workable if not exciting unit testing framework for C.

The compiler-cleanup patch is needed to make it easier for the unit tests to load source policies. I think that using source policies for unit testing libsepol will be more maintainable over the long run. It also removes some code duplication between checkpolicy.c and checkmodule.c.

The test-framework patch, which depends upon the computer-cleanup patch, adds a simple test to libsepol and the makefile changes
to introduce a top-level make target "test". Example output from running the test target is included below.

Quoted 6 lines

The test policy is in a separate email to you because it is large (a complete base policy.conf from reference policy).

More patches with additional tests will be submitted over time.

Please apply.

Merged. However, at present, this requires not only having a copy of checkpolicy sources but also having them prebuilt (make test in libsepol with checkpolicy sources present but not built will fail), which seems undesirable. Actual output that I see has only 36 asserts vs the output you posted (19044?).

Signature · 8 lines
Stephen Smalley
National Security Agency


--
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.
#2 lore raw
Quoted 22 lines · Stephen Smalley
Quoted 21 lines · Karl MacMillan

Steve,

Attached are two patches to add a simple test framework to libsepol and, in the future, to the other libraries. It depends CUnit
(http://cunit.sourceforge.net/), a workable if not exciting unit testing framework for C.

The compiler-cleanup patch is needed to make it easier for the unit tests to load source policies. I think that using source policies for unit testing libsepol will be more maintainable over the long run. It also removes some code duplication between checkpolicy.c and checkmodule.c.

The test-framework patch, which depends upon the computer-cleanup patch, adds a simple test to libsepol and the makefile changes
to introduce a top-level make target "test". Example output from running the test target is included below.

The test policy is in a separate email to you because it is large (a complete base policy.conf from reference policy).

Quoted 8 lines
Quoted 3 lines

More patches with additional tests will be submitted over time.

Please apply.

Merged. However, at present, this requires not only having a copy of checkpolicy sources but also having them prebuilt (make test in libsepol with checkpolicy sources present but not built will fail), which seems undesirable.

I agree - requiring the sources for checkpolicy is not desirable, but it is only way to use source policies for the tests, which I believe is worthwhile. I can probably make the targets handle the dependencies, though all of these would be easier if it were possible to build everything without installation.

Quoted 3 lines

Actual output that I see has only 36 asserts vs the output
you posted (19044?).

This is because the policy that I used for the example was much larger than what I actually submitted.

Karl

Signature · 8 lines
Karl MacMillan
Tresys Technology
www.tresys.com

--
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.