Skip to contentSkip to conversation

libsemanage: add tests

PATCH In review Jan Zarsky 2 participants 14 messages Jun 12, 2019 – Jun 19, 2019 SELinux security module

lore
Share & tools
lore

Jan Zarsky

author
#0 lore raw

cover libsemanage: add tests

This series adds new test suites for libsemanage. In the first patch, common helper functions are added. The other patches are independent and each adds a new test suite.

The test suites are testing only the public API and aim for line coverage. They test only correct usage of the functions. Together with valgrind, the test suites can be used for detecting memory leaks.

The tests run fine in Travis CI and do not require an SELinux-enabled system.

I welcome any suggestions.

Jan Zarsky (11):
libsemanage: add helper functions to tests
libsemanage: test semanage_handle_* functions
libsemanage: test semanage_bool_* functions
libsemanage: test semanage_fcontext functions
libsemanage: test semanage_iface_* functions
libsemanage: test semanage_ibendport_* functions
libsemanage: test semanage_node_* functions
libsemanage: test semanage_port_* functions
libsemanage: test semanage_user_* functions
libsemanage: test semanage_context_* functions
libsemanage: test semanage_msg_default_handler

 libsemanage/tests/.gitignore            |    1 +
 libsemanage/tests/Makefile              |   11 +-
 libsemanage/tests/libsemanage-tests.c   |   18 +
 libsemanage/tests/test_bool.c           |  932 +++++++++++++++++++++++++++
 libsemanage/tests/test_bool.cil         |   24 +
 libsemanage/tests/test_bool.h           |   31 +
 libsemanage/tests/test_fcontext.c       | 1045 +++++++++++++++++++++++++++++++
 libsemanage/tests/test_fcontext.cil     |   25 +
 libsemanage/tests/test_fcontext.h       |   30 +
 libsemanage/tests/test_handle.c         |  329 ++++++++++
 libsemanage/tests/test_handle.cil       |   21 +
 libsemanage/tests/test_handle.h         |   30 +
 libsemanage/tests/test_ibendport.c      |  525 ++++++++++++++++
 libsemanage/tests/test_ibendport.cil    |   28 +
 libsemanage/tests/test_ibendport.h      |   30 +
 libsemanage/tests/test_iface.c          |  666 ++++++++++++++++++++
 libsemanage/tests/test_iface.cil        |   28 +
 libsemanage/tests/test_iface.h          |   30 +
 libsemanage/tests/test_node.c           |  807 ++++++++++++++++++++++++
 libsemanage/tests/test_node.cil         |   28 +
 libsemanage/tests/test_node.h           |   30 +
 libsemanage/tests/test_other.c          |  120 ++++
 libsemanage/tests/test_other.h          |   30 +
 libsemanage/tests/test_port.c           |  909 +++++++++++++++++++++++++++
 libsemanage/tests/test_port.cil         |   27 +
 libsemanage/tests/test_port.h           |   30 +
 libsemanage/tests/test_semanage_store.c |    2 +-
 libsemanage/tests/test_user.c           |  690 ++++++++++++++++++++
 libsemanage/tests/test_user.cil         |   27 +
 libsemanage/tests/test_user.h           |   30 +
 libsemanage/tests/utilities.c           |  254 +++++++-
 libsemanage/tests/utilities.h           |   53 +-
 32 files changed, 6831 insertions(+), 10 deletions(-)
author
#1 lore raw

PATCH 1/11 libsemanage: add helper functions to tests

- Add functions for creating and destroying test semanage store.
- Add functions for writing SELinux policy to the test store.
- Add functions for creating semanage handle, connecting to the store and for beginning a transaction.
- Update Makefile to compile test policies from CIL source.

---
 libsemanage/tests/.gitignore            |   1 +
 libsemanage/tests/Makefile              |  11 +-
 libsemanage/tests/test_semanage_store.c |   2 +-
 libsemanage/tests/utilities.c           | 254 +++++++++++++++++++++++-
 libsemanage/tests/utilities.h           |  53 ++++-
 5 files changed, 311 insertions(+), 10 deletions(-)
libsemanage/tests/.gitignore +1 -0
index f07111db..8a2a866a 100644
@@ -1 +1,2 @@
 libsemanage-tests
+*.policy
libsemanage/tests/Makefile +8 -3
index 324766a0..69f49a36 100644
@@ -1,5 +1,6 @@
 # Add your test source files here:
 SOURCES = $(sort $(wildcard *.c))
+CILS = $(sort $(wildcard *.cil))
 
 ###########################################################################
 
@@ -8,15 +9,19 @@ CFLAGS += -g -O0 -Wall -W -Wundef -Wmissing-noreturn -Wmissing-format-attribute
 override CFLAGS += -I../src -I../include
 override LDLIBS += -lcunit -lbz2 -laudit -lselinux -lsepol
 
-OBJECTS = $(SOURCES:.c=.o) 
+OBJECTS = $(SOURCES:.c=.o)
+POLICIES = $(CILS:.cil=.policy)
 
-all: $(EXECUTABLE) 
+all: $(EXECUTABLE) $(POLICIES)
 
 $(EXECUTABLE): $(OBJECTS) ../src/libsemanage.a
 	$(CC) $(LDFLAGS) -o $@ $^ $(LDLIBS)
 
+%.policy: %.cil
+	../../secilc/secilc $*.cil -o $*.policy -f /dev/null
+
 clean distclean: 
-	rm -rf $(OBJECTS) $(EXECUTABLE)
+	rm -rf $(OBJECTS) $(POLICIES) $(EXECUTABLE)
 
 test: all 
 	./$(EXECUTABLE)
libsemanage/tests/test_semanage_store.c +1 -1
index b324d502..92085361 100644
@@ -43,7 +43,7 @@
 #include <unistd.h>
 #include <CUnit/Basic.h>
 
-semanage_handle_t *sh = NULL;
+extern semanage_handle_t *sh;
 const char *rootpath = "./test-policy";
 const char *polpath = "./test-policy/store/";
 const char *readlockpath = "./test-policy/store/semanage.read.LOCK";
libsemanage/tests/utilities.c +250 -4

272 lines not on this page · raw message

libsemanage/tests/utilities.h +51 -2

64 lines not on this page · raw message

Signature · 1 line
2.20.1
author
#2 lore raw

PATCH 2/11 libsemanage: test semanage_handle_* functions

In reply to Jan Zarsky

Add new test suite for semanage_handle_* functions. The test suite aims for line coverage and covers expected usage of functions. The test suite uses custom semanage store and policy written in CIL, it does not require running on SELinux enabled system.

---
 libsemanage/tests/libsemanage-tests.c |   2 +
 libsemanage/tests/test_handle.c       | 329 ++++++++++++++++++++++++++
 libsemanage/tests/test_handle.cil     |  21 ++
 libsemanage/tests/test_handle.h       |  30 +++
 4 files changed, 382 insertions(+)
 create mode 100644 libsemanage/tests/test_handle.c
 create mode 100644 libsemanage/tests/test_handle.cil
 create mode 100644 libsemanage/tests/test_handle.h
libsemanage/tests/libsemanage-tests.c +2 -0
index 048751b8..0fb3991b 100644
@@ -21,6 +21,7 @@
 
 #include "test_semanage_store.h"
 #include "test_utilities.h"
+#include "test_handle.h"
 
 #include <CUnit/Basic.h>
 #include <CUnit/Console.h>
@@ -59,6 +60,7 @@ static bool do_tests(int interactive, int verbose)
 
 	DECLARE_SUITE(semanage_store);
 	DECLARE_SUITE(semanage_utilities);
+	DECLARE_SUITE(handle);
 
 	if (verbose)
 		CU_basic_set_mode(CU_BRM_VERBOSE);
libsemanage/tests/test_handle.c new file +329 -0

329 lines not on this page · raw message

libsemanage/tests/test_handle.cil new file +21 -0
new file mode 100644
index 00000000..81690b88
@@ -0,0 +1,21 @@
+(typeattribute cil_gen_require)
+(roleattribute cil_gen_require)
+(handleunknown allow)
+(mls true)
+(policycap network_peer_controls)
+(policycap open_perms)
+(sid security)
+(sidorder (security))
+(sensitivity s0)
+(sensitivityorder (s0))
+(user system_u)
+(userrole system_u object_r)
+(userlevel system_u (s0))
+(userrange system_u ((s0) (s0)))
+(role object_r)
+(roletype object_r test_t)
+(type test_t)
+(sidcontext security (system_u object_r test_t ((s0) (s0))))
+(class test_class (test_perm))
+(classorder (test_class))
+(allow test_t self (test_class (test_perm)))
libsemanage/tests/test_handle.h new file +30 -0
new file mode 100644
index 00000000..f927bd6a
@@ -0,0 +1,30 @@
+/*
+ * Authors: Jan Zarsky <[email protected]>
+ *
+ * Copyright (C) 2019 Red Hat, Inc.
+ *
+ * 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_HANDLE_H__
+#define __TEST_HANDLE_H__
+
+#include <CUnit/Basic.h>
+
+int handle_test_init(void);
+int handle_test_cleanup(void);
+int handle_add_tests(CU_pSuite suite);
+
+#endif
Signature · 1 line
2.20.1
author
#3 lore raw

PATCH 3/11 libsemanage: test semanage_bool_* functions

In reply to Jan Zarsky

Add new test suite for semanage_bool_* functions. The test suite aims for line coverage and covers expected usage of functions. The test suite uses custom semanage store and policy written in CIL, it does not require running on SELinux enabled system.

---
 libsemanage/tests/libsemanage-tests.c |   2 +
 libsemanage/tests/test_bool.c         | 932 ++++++++++++++++++++++++++
 libsemanage/tests/test_bool.cil       |  24 +
 libsemanage/tests/test_bool.h         |  31 +
 4 files changed, 989 insertions(+)
 create mode 100644 libsemanage/tests/test_bool.c
 create mode 100644 libsemanage/tests/test_bool.cil
 create mode 100644 libsemanage/tests/test_bool.h
libsemanage/tests/libsemanage-tests.c +2 -0
index 0fb3991b..83754fe4 100644
@@ -22,6 +22,7 @@
 #include "test_semanage_store.h"
 #include "test_utilities.h"
 #include "test_handle.h"
+#include "test_bool.h"
 
 #include <CUnit/Basic.h>
 #include <CUnit/Console.h>
@@ -61,6 +62,7 @@ static bool do_tests(int interactive, int verbose)
 	DECLARE_SUITE(semanage_store);
 	DECLARE_SUITE(semanage_utilities);
 	DECLARE_SUITE(handle);
+	DECLARE_SUITE(bool);
 
 	if (verbose)
 		CU_basic_set_mode(CU_BRM_VERBOSE);
libsemanage/tests/test_bool.c new file +932 -0

932 lines not on this page · raw message

libsemanage/tests/test_bool.cil new file +24 -0
new file mode 100644
index 00000000..4174751c
@@ -0,0 +1,24 @@
+(typeattribute cil_gen_require)
+(roleattribute cil_gen_require)
+(handleunknown allow)
+(mls true)
+(policycap network_peer_controls)
+(policycap open_perms)
+(sid security)
+(sidorder (security))
+(sensitivity s0)
+(sensitivityorder (s0))
+(user system_u)
+(userrole system_u object_r)
+(userlevel system_u (s0))
+(userrange system_u ((s0) (s0)))
+(role object_r)
+(roletype object_r test_t)
+(type test_t)
+(sidcontext security (system_u object_r test_t ((s0) (s0))))
+(class test_class (test_perm))
+(classorder (test_class))
+(allow test_t self (test_class (test_perm)))
+(boolean first_bool true)
+(boolean second_bool false)
+(boolean third_bool false)
libsemanage/tests/test_bool.h new file +31 -0
new file mode 100644
index 00000000..b5b5a603
@@ -0,0 +1,31 @@
+/*
+ * Authors: Jan Zarsky <[email protected]>
+ *
+ * Copyright (C) 2019 Red Hat, Inc.
+ *
+ * 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_BOOL_H__
+#define __TEST_BOOL_H__
+
+#include <CUnit/Basic.h>
+#include "semanage/semanage.h"
+
+int bool_test_init(void);
+int bool_test_cleanup(void);
+int bool_add_tests(CU_pSuite suite);
+
+#endif
Signature · 1 line
2.20.1
author
#4 lore raw

PATCH 4/11 libsemanage: test semanage_fcontext functions

In reply to Jan Zarsky

Add new test suite for semanage_fcontext_* functions. The test suite aims for line coverage and covers expected usage of functions. The test suite uses custom semanage store and policy written in CIL, it does not require running on SELinux enabled system.

---
 libsemanage/tests/libsemanage-tests.c |    2 +
 libsemanage/tests/test_fcontext.c     | 1045 +++++++++++++++++++++++++
 libsemanage/tests/test_fcontext.cil   |   25 +
 libsemanage/tests/test_fcontext.h     |   30 +
 4 files changed, 1102 insertions(+)
 create mode 100644 libsemanage/tests/test_fcontext.c
 create mode 100644 libsemanage/tests/test_fcontext.cil
 create mode 100644 libsemanage/tests/test_fcontext.h
libsemanage/tests/libsemanage-tests.c +2 -0
index 83754fe4..c5f42b9e 100644
@@ -23,6 +23,7 @@
 #include "test_utilities.h"
 #include "test_handle.h"
 #include "test_bool.h"
+#include "test_fcontext.h"
 
 #include <CUnit/Basic.h>
 #include <CUnit/Console.h>
@@ -63,6 +64,7 @@ static bool do_tests(int interactive, int verbose)
 	DECLARE_SUITE(semanage_utilities);
 	DECLARE_SUITE(handle);
 	DECLARE_SUITE(bool);
+	DECLARE_SUITE(fcontext);
 
 	if (verbose)
 		CU_basic_set_mode(CU_BRM_VERBOSE);
libsemanage/tests/test_fcontext.c new file +1045 -0

1,045 lines not on this page · raw message

libsemanage/tests/test_fcontext.cil new file +25 -0
new file mode 100644
index 00000000..1c62b893
@@ -0,0 +1,25 @@
+(typeattribute cil_gen_require)
+(roleattribute cil_gen_require)
+(handleunknown allow)
+(mls true)
+(policycap network_peer_controls)
+(policycap open_perms)
+(sid security)
+(sidorder (security))
+(sensitivity s0)
+(sensitivityorder (s0))
+(user system_u)
+(userrole system_u object_r)
+(userlevel system_u (s0))
+(userrange system_u ((s0) (s0)))
+(role object_r)
+(roletype object_r first_t)
+(roletype object_r second_t)
+(roletype object_r third_t)
+(type first_t)
+(type second_t)
+(type third_t)
+(sidcontext security (system_u object_r first_t ((s0) (s0))))
+(class test_class (test_perm))
+(classorder (test_class))
+(allow first_t self (test_class (test_perm)))
libsemanage/tests/test_fcontext.h new file +30 -0
new file mode 100644
index 00000000..64aba991
@@ -0,0 +1,30 @@
+/*
+ * Authors: Jan Zarsky <[email protected]>
+ *
+ * Copyright (C) 2019 Red Hat, Inc.
+ *
+ * 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_FCONTEXT_H__
+#define __TEST_FCONTEXT_H__
+
+#include <CUnit/Basic.h>
+
+int fcontext_test_init(void);
+int fcontext_test_cleanup(void);
+int fcontext_add_tests(CU_pSuite suite);
+
+#endif
Signature · 1 line
2.20.1
author
#5 lore raw

PATCH 5/11 libsemanage: test semanage_iface_* functions

In reply to Jan Zarsky

Add new test suite for semanage_iface_* functions. The test suite aims for line coverage and covers expected usage of functions. The test suite uses custom semanage store and policy written in CIL, it does not require running on SELinux enabled system.

---
 libsemanage/tests/libsemanage-tests.c |   2 +
 libsemanage/tests/test_iface.c        | 666 ++++++++++++++++++++++++++
 libsemanage/tests/test_iface.cil      |  28 ++
 libsemanage/tests/test_iface.h        |  30 ++
 4 files changed, 726 insertions(+)
 create mode 100644 libsemanage/tests/test_iface.c
 create mode 100644 libsemanage/tests/test_iface.cil
 create mode 100644 libsemanage/tests/test_iface.h
libsemanage/tests/libsemanage-tests.c +2 -0
index c5f42b9e..e590f7bd 100644
@@ -24,6 +24,7 @@
 #include "test_handle.h"
 #include "test_bool.h"
 #include "test_fcontext.h"
+#include "test_iface.h"
 
 #include <CUnit/Basic.h>
 #include <CUnit/Console.h>
@@ -65,6 +66,7 @@ static bool do_tests(int interactive, int verbose)
 	DECLARE_SUITE(handle);
 	DECLARE_SUITE(bool);
 	DECLARE_SUITE(fcontext);
+	DECLARE_SUITE(iface);
 
 	if (verbose)
 		CU_basic_set_mode(CU_BRM_VERBOSE);
libsemanage/tests/test_iface.c new file +666 -0

666 lines not on this page · raw message

libsemanage/tests/test_iface.cil new file +28 -0
new file mode 100644
index 00000000..13fd8f68
@@ -0,0 +1,28 @@
+(typeattribute cil_gen_require)
+(roleattribute cil_gen_require)
+(handleunknown allow)
+(mls true)
+(policycap network_peer_controls)
+(policycap open_perms)
+(sid security)
+(sidorder (security))
+(sensitivity s0)
+(sensitivityorder (s0))
+(user system_u)
+(userrole system_u object_r)
+(userlevel system_u (s0))
+(userrange system_u ((s0) (s0)))
+(role object_r)
+(roletype object_r first_netif_t)
+(roletype object_r second_netif_t)
+(roletype object_r third_netif_t)
+(type first_netif_t)
+(type second_netif_t)
+(type third_netif_t)
+(sidcontext security (system_u object_r first_netif_t ((s0) (s0))))
+(class netif (tcp_recv))
+(classorder (netif))
+(allow first_netif_t self (netif (tcp_recv)))
+(netifcon eth0 (system_u object_r first_netif_t ((s0) (s0))) (system_u object_r first_netif_t ((s0) (s0))))
+(netifcon eth1 (system_u object_r second_netif_t ((s0) (s0))) (system_u object_r second_netif_t ((s0) (s0))))
+(netifcon eth2 (system_u object_r third_netif_t ((s0) (s0))) (system_u object_r third_netif_t ((s0) (s0))))
libsemanage/tests/test_iface.h new file +30 -0
new file mode 100644
index 00000000..5953e9c0
@@ -0,0 +1,30 @@
+/*
+ * Authors: Jan Zarsky <[email protected]>
+ *
+ * Copyright (C) 2019 Red Hat, Inc.
+ *
+ * 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_IFACE_H__
+#define __TEST_IFACE_H__
+
+#include <CUnit/Basic.h>
+
+int iface_test_init(void);
+int iface_test_cleanup(void);
+int iface_add_tests(CU_pSuite suite);
+
+#endif
Signature · 1 line
2.20.1
author
#6 lore raw

PATCH 6/11 libsemanage: test semanage_ibendport_* functions

In reply to Jan Zarsky

Add new test suite for semanage_ibendport_* functions. The test suite aims for line coverage and covers expected usage of functions. The test suite uses custom semanage store and policy written in CIL, it does not require running on SELinux enabled system.

---
 libsemanage/tests/libsemanage-tests.c |   2 +
 libsemanage/tests/test_ibendport.c    | 525 ++++++++++++++++++++++++++
 libsemanage/tests/test_ibendport.cil  |  28 ++
 libsemanage/tests/test_ibendport.h    |  30 ++
 4 files changed, 585 insertions(+)
 create mode 100644 libsemanage/tests/test_ibendport.c
 create mode 100644 libsemanage/tests/test_ibendport.cil
 create mode 100644 libsemanage/tests/test_ibendport.h
libsemanage/tests/libsemanage-tests.c +2 -0
index e590f7bd..d395a9fd 100644
@@ -25,6 +25,7 @@
 #include "test_bool.h"
 #include "test_fcontext.h"
 #include "test_iface.h"
+#include "test_ibendport.h"
 
 #include <CUnit/Basic.h>
 #include <CUnit/Console.h>
@@ -67,6 +68,7 @@ static bool do_tests(int interactive, int verbose)
 	DECLARE_SUITE(bool);
 	DECLARE_SUITE(fcontext);
 	DECLARE_SUITE(iface);
+	DECLARE_SUITE(ibendport);
 
 	if (verbose)
 		CU_basic_set_mode(CU_BRM_VERBOSE);
libsemanage/tests/test_ibendport.c new file +525 -0

525 lines not on this page · raw message

libsemanage/tests/test_ibendport.cil new file +28 -0
new file mode 100644
index 00000000..b786b913
@@ -0,0 +1,28 @@
+(typeattribute cil_gen_require)
+(roleattribute cil_gen_require)
+(handleunknown allow)
+(mls true)
+(policycap network_peer_controls)
+(policycap open_perms)
+(sid security)
+(sidorder (security))
+(sensitivity s0)
+(sensitivityorder (s0))
+(user system_u)
+(userrole system_u object_r)
+(userlevel system_u (s0))
+(userrange system_u ((s0) (s0)))
+(role object_r)
+(roletype object_r first_ibendport_t)
+(roletype object_r second_ibendport_t)
+(roletype object_r third_ibendport_t)
+(type first_ibendport_t)
+(type second_ibendport_t)
+(type third_ibendport_t)
+(sidcontext security (system_u object_r first_ibendport_t ((s0) (s0))))
+(class test_class (test_perm))
+(classorder (test_class))
+(allow first_ibendport_t self (test_class (test_perm)))
+(ibendportcon mlx4_0 1 (system_u object_r first_ibendport_t ((s0) (s0))))
+(ibendportcon mlx4_1 2 (system_u object_r second_ibendport_t ((s0) (s0))))
+(ibendportcon mlx4_1 3 (system_u object_r third_ibendport_t ((s0) (s0))))
libsemanage/tests/test_ibendport.h new file +30 -0
new file mode 100644
index 00000000..33d7fbd0
@@ -0,0 +1,30 @@
+/*
+ * Authors: Jan Zarsky <[email protected]>
+ *
+ * Copyright (C) 2019 Red Hat, Inc.
+ *
+ * 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_IBENDPORT_H__
+#define __TEST_IBENDPORT_H__
+
+#include <CUnit/Basic.h>
+
+int ibendport_test_init(void);
+int ibendport_test_cleanup(void);
+int ibendport_add_tests(CU_pSuite suite);
+
+#endif
Signature · 1 line
2.20.1
author
#7 lore raw

PATCH 7/11 libsemanage: test semanage_node_* functions

In reply to Jan Zarsky

Add new test suite for semanage_node_* functions. The test suite aims for line coverage and covers expected usage of functions. The test suite uses custom semanage store and policy written in CIL, it does not require running on SELinux enabled system.

---
 libsemanage/tests/libsemanage-tests.c |   2 +
 libsemanage/tests/test_node.c         | 807 ++++++++++++++++++++++++++
 libsemanage/tests/test_node.cil       |  28 +
 libsemanage/tests/test_node.h         |  30 +
 4 files changed, 867 insertions(+)
 create mode 100644 libsemanage/tests/test_node.c
 create mode 100644 libsemanage/tests/test_node.cil
 create mode 100644 libsemanage/tests/test_node.h
libsemanage/tests/libsemanage-tests.c +2 -0
index d395a9fd..ba24421d 100644
@@ -26,6 +26,7 @@
 #include "test_fcontext.h"
 #include "test_iface.h"
 #include "test_ibendport.h"
+#include "test_node.h"
 
 #include <CUnit/Basic.h>
 #include <CUnit/Console.h>
@@ -69,6 +70,7 @@ static bool do_tests(int interactive, int verbose)
 	DECLARE_SUITE(fcontext);
 	DECLARE_SUITE(iface);
 	DECLARE_SUITE(ibendport);
+	DECLARE_SUITE(node);
 
 	if (verbose)
 		CU_basic_set_mode(CU_BRM_VERBOSE);
libsemanage/tests/test_node.c new file +807 -0

807 lines not on this page · raw message

libsemanage/tests/test_node.cil new file +28 -0
new file mode 100644
index 00000000..1638cd1e
@@ -0,0 +1,28 @@
+(typeattribute cil_gen_require)
+(roleattribute cil_gen_require)
+(handleunknown allow)
+(mls true)
+(policycap network_peer_controls)
+(policycap open_perms)
+(sid security)
+(sidorder (security))
+(sensitivity s0)
+(sensitivityorder (s0))
+(user system_u)
+(userrole system_u object_r)
+(userlevel system_u (s0))
+(userrange system_u ((s0) (s0)))
+(role object_r)
+(roletype object_r first_node_t)
+(roletype object_r second_node_t)
+(roletype object_r third_node_t)
+(type first_node_t)
+(type second_node_t)
+(type third_node_t)
+(sidcontext security (system_u object_r first_node_t ((s0) (s0))))
+(class node (tcp_recv))
+(classorder (node))
+(allow first_node_t self (node (tcp_recv)))
+(nodecon (192.168.0.0) (255.255.255.0) (system_u object_r first_node_t ((s0) (s0))))
+(nodecon (2001:db8:85a3::8a2e:370:7334) (2001:db8:85a3::8a2e:370:7334) (system_u object_r second_node_t ((s0) (s0))))
+(nodecon (127.0.0.1) (255.255.0.0) (system_u object_r third_node_t ((s0) (s0))))
libsemanage/tests/test_node.h new file +30 -0
new file mode 100644
index 00000000..5b329406
@@ -0,0 +1,30 @@
+/*
+ * Authors: Jan Zarsky <[email protected]>
+ *
+ * Copyright (C) 2019 Red Hat, Inc.
+ *
+ * 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_NODE_H__
+#define __TEST_NODE_H__
+
+#include <CUnit/Basic.h>
+
+int node_test_init(void);
+int node_test_cleanup(void);
+int node_add_tests(CU_pSuite suite);
+
+#endif
Signature · 1 line
2.20.1
author
#8 lore raw

PATCH 8/11 libsemanage: test semanage_port_* functions

In reply to Jan Zarsky

Add new test suite for semanage_port_* functions. The test suite aims for line coverage and covers expected usage of functions. The test suite uses custom semanage store and policy written in CIL, it does not require running on SELinux enabled system.

---
 libsemanage/tests/libsemanage-tests.c |   2 +
 libsemanage/tests/test_port.c         | 909 ++++++++++++++++++++++++++
 libsemanage/tests/test_port.cil       |  27 +
 libsemanage/tests/test_port.h         |  30 +
 4 files changed, 968 insertions(+)
 create mode 100644 libsemanage/tests/test_port.c
 create mode 100644 libsemanage/tests/test_port.cil
 create mode 100644 libsemanage/tests/test_port.h
libsemanage/tests/libsemanage-tests.c +2 -0
index ba24421d..b4a8c122 100644
@@ -27,6 +27,7 @@
 #include "test_iface.h"
 #include "test_ibendport.h"
 #include "test_node.h"
+#include "test_port.h"
 
 #include <CUnit/Basic.h>
 #include <CUnit/Console.h>
@@ -71,6 +72,7 @@ static bool do_tests(int interactive, int verbose)
 	DECLARE_SUITE(iface);
 	DECLARE_SUITE(ibendport);
 	DECLARE_SUITE(node);
+	DECLARE_SUITE(port);
 
 	if (verbose)
 		CU_basic_set_mode(CU_BRM_VERBOSE);
libsemanage/tests/test_port.c new file +909 -0

909 lines not on this page · raw message

libsemanage/tests/test_port.cil new file +27 -0
new file mode 100644
index 00000000..7e07a61c
@@ -0,0 +1,27 @@
+(typeattribute cil_gen_require)
+(roleattribute cil_gen_require)
+(handleunknown allow)
+(mls true)
+(policycap open_perms)
+(sid security)
+(sidorder (security))
+(sensitivity s0)
+(sensitivityorder (s0))
+(user system_u)
+(userrole system_u object_r)
+(userlevel system_u (s0))
+(userrange system_u ((s0) (s0)))
+(role object_r)
+(roletype object_r first_port_t)
+(roletype object_r second_port_t)
+(roletype object_r third_port_t)
+(type first_port_t)
+(type second_port_t)
+(type third_port_t)
+(sidcontext security (system_u object_r first_port_t ((s0) (s0))))
+(class file (open))
+(classorder (file))
+(allow first_port_t self (file (open)))
+(portcon tcp 80 (system_u object_r first_port_t ((s0) (s0))))
+(portcon udp (1 1023) (system_u object_r second_port_t ((s0) (s0))))
+(portcon tcp 12345 (system_u object_r third_port_t ((s0) (s0))))
libsemanage/tests/test_port.h new file +30 -0
new file mode 100644
index 00000000..ad26f90b
@@ -0,0 +1,30 @@
+/*
+ * Authors: Jan Zarsky <[email protected]>
+ *
+ * Copyright (C) 2019 Red Hat, Inc.
+ *
+ * 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_PORT_H__
+#define __TEST_PORT_H__
+
+#include <CUnit/Basic.h>
+
+int port_test_init(void);
+int port_test_cleanup(void);
+int port_add_tests(CU_pSuite suite);
+
+#endif
Signature · 1 line
2.20.1
author
#9 lore raw

PATCH 9/11 libsemanage: test semanage_user_* functions

In reply to Jan Zarsky

Add new test suite for semanage_user_* functions. The test suite aims for line coverage and covers expected usage of functions. The test suite uses custom semanage store and policy written in CIL, it does not require running on SELinux enabled system.

---
 libsemanage/tests/libsemanage-tests.c |   2 +
 libsemanage/tests/test_user.c         | 690 ++++++++++++++++++++++++++
 libsemanage/tests/test_user.cil       |  27 +
 libsemanage/tests/test_user.h         |  30 ++
 4 files changed, 749 insertions(+)
 create mode 100644 libsemanage/tests/test_user.c
 create mode 100644 libsemanage/tests/test_user.cil
 create mode 100644 libsemanage/tests/test_user.h
libsemanage/tests/libsemanage-tests.c +2 -0
index b4a8c122..64039b5d 100644
@@ -28,6 +28,7 @@
 #include "test_ibendport.h"
 #include "test_node.h"
 #include "test_port.h"
+#include "test_user.h"
 
 #include <CUnit/Basic.h>
 #include <CUnit/Console.h>
@@ -73,6 +74,7 @@ static bool do_tests(int interactive, int verbose)
 	DECLARE_SUITE(ibendport);
 	DECLARE_SUITE(node);
 	DECLARE_SUITE(port);
+	DECLARE_SUITE(user);
 
 	if (verbose)
 		CU_basic_set_mode(CU_BRM_VERBOSE);
libsemanage/tests/test_user.c new file +690 -0

690 lines not on this page · raw message

libsemanage/tests/test_user.cil new file +27 -0
new file mode 100644
index 00000000..1c65b9fc
@@ -0,0 +1,27 @@
+(typeattribute cil_gen_require)
+(roleattribute cil_gen_require)
+(handleunknown allow)
+(mls true)
+(policycap network_peer_controls)
+(policycap open_perms)
+(sid security)
+(sidorder (security))
+(sensitivity s0)
+(sensitivityorder (s0))
+(user first_u)
+(user second_u)
+(user third_u)
+(userrole first_u object_r)
+(userlevel first_u (s0))
+(userlevel second_u (s0))
+(userlevel third_u (s0))
+(userrange first_u ((s0) (s0)))
+(userrange second_u ((s0) (s0)))
+(userrange third_u ((s0) (s0)))
+(role object_r)
+(roletype object_r test_t)
+(type test_t)
+(sidcontext security (first_u object_r test_t ((s0) (s0))))
+(class test_class (test_perm))
+(classorder (test_class))
+(allow test_t self (test_class (test_perm)))
libsemanage/tests/test_user.h new file +30 -0
new file mode 100644
index 00000000..014a84aa
@@ -0,0 +1,30 @@
+/*
+ * Authors: Jan Zarsky <[email protected]>
+ *
+ * Copyright (C) 2019 Red Hat, Inc.
+ *
+ * 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_USER_H__
+#define __TEST_USER_H__
+
+#include <CUnit/Basic.h>
+
+int user_test_init(void);
+int user_test_cleanup(void);
+int user_add_tests(CU_pSuite suite);
+
+#endif
Signature · 1 line
2.20.1
author
#10 lore raw

PATCH 10/11 libsemanage: test semanage_context_* functions

In reply to Jan Zarsky

Add new test suite for other libsemanage functions. Add tests for semanage_context_* functions.

---
 libsemanage/tests/libsemanage-tests.c |  2 +
 libsemanage/tests/test_other.c        | 97 +++++++++++++++++++++++++++
 libsemanage/tests/test_other.h        | 30 +++++++++
 3 files changed, 129 insertions(+)
 create mode 100644 libsemanage/tests/test_other.c
 create mode 100644 libsemanage/tests/test_other.h
libsemanage/tests/libsemanage-tests.c +2 -0
index 64039b5d..2ae4a21b 100644
@@ -29,6 +29,7 @@
 #include "test_node.h"
 #include "test_port.h"
 #include "test_user.h"
+#include "test_other.h"
 
 #include <CUnit/Basic.h>
 #include <CUnit/Console.h>
@@ -75,6 +76,7 @@ static bool do_tests(int interactive, int verbose)
 	DECLARE_SUITE(node);
 	DECLARE_SUITE(port);
 	DECLARE_SUITE(user);
+	DECLARE_SUITE(other);
 
 	if (verbose)
 		CU_basic_set_mode(CU_BRM_VERBOSE);
libsemanage/tests/test_other.c new file +97 -0

97 lines not on this page · raw message

libsemanage/tests/test_other.h new file +30 -0
new file mode 100644
index 00000000..40d2dcf8
@@ -0,0 +1,30 @@
+/*
+ * Authors: Jan Zarsky <[email protected]>
+ *
+ * Copyright (C) 2019 Red Hat, Inc.
+ *
+ * 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_OTHER_H__
+#define __TEST_OTHER_H__
+
+#include <CUnit/Basic.h>
+
+int other_test_init(void);
+int other_test_cleanup(void);
+int other_add_tests(CU_pSuite suite);
+
+#endif
Signature · 1 line
2.20.1
author
#11 lore raw

PATCH 11/11 libsemanage: test semanage_msg_default_handler

In reply to Jan Zarsky

Add test for semanage_msg_default_handler.

---
 libsemanage/tests/test_other.c | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)
libsemanage/tests/test_other.c +23 -0
index 437064d2..c4ee0ed8 100644
@@ -24,6 +24,9 @@
 /* context_record.h */
 void test_semanage_context(void);
 
+/* debug.h */
+void test_debug(void);
+
 extern semanage_handle_t *sh;
 
 int other_test_init(void)
@@ -39,6 +42,7 @@ int other_test_cleanup(void)
 int other_add_tests(CU_pSuite suite)
 {
 	CU_add_test(suite, "semanage_context", test_semanage_context);
+	CU_add_test(suite, "debug", test_debug);
 
 	return 0;
 }
@@ -95,3 +99,22 @@ void test_semanage_context(void)
 	semanage_context_free(con_clone);
 	cleanup_handle(SH_CONNECT);
 }
+
+/* Function semanage_msg_default_handler */
+void test_debug(void)
+{
+	semanage_module_info_t *modinfo = NULL;
+
+	/* setup */
+	sh = semanage_handle_create();
+	CU_ASSERT_PTR_NOT_NULL(sh);
+	CU_ASSERT(semanage_connect(sh) >= 0);
+	CU_ASSERT(semanage_module_info_create(sh, &modinfo) >= 0);
+
+	/* test */
+	CU_ASSERT(semanage_module_info_set_priority(sh, modinfo, -42) < 0);
+
+	/* cleanup */
+	CU_ASSERT(semanage_disconnect(sh) >= 0);
+	semanage_handle_destroy(sh);
+}
Signature · 1 line
2.20.1
#12 lore raw
In reply to Jan Zarsky
Quoted 51 lines · Jan Zarsky

This series adds new test suites for libsemanage. In the first patch, common helper functions are added. The other patches are independent and each adds a new test suite.

The test suites are testing only the public API and aim for line coverage. They test only correct usage of the functions. Together with valgrind, the test suites can be used for detecting memory leaks.

The tests run fine in Travis CI and do not require an SELinux-enabled system.

I welcome any suggestions.

Jan Zarsky (11):
libsemanage: add helper functions to tests
libsemanage: test semanage_handle_* functions
libsemanage: test semanage_bool_* functions
libsemanage: test semanage_fcontext functions
libsemanage: test semanage_iface_* functions
libsemanage: test semanage_ibendport_* functions
libsemanage: test semanage_node_* functions
libsemanage: test semanage_port_* functions
libsemanage: test semanage_user_* functions
libsemanage: test semanage_context_* functions
libsemanage: test semanage_msg_default_handler

 libsemanage/tests/.gitignore            |    1 +
 libsemanage/tests/Makefile              |   11 +-
 libsemanage/tests/libsemanage-tests.c   |   18 +
 libsemanage/tests/test_bool.c           |  932 +++++++++++++++++++++++++++
 libsemanage/tests/test_bool.cil         |   24 +
 libsemanage/tests/test_bool.h           |   31 +
 libsemanage/tests/test_fcontext.c       | 1045 +++++++++++++++++++++++++++++++
 libsemanage/tests/test_fcontext.cil     |   25 +
 libsemanage/tests/test_fcontext.h       |   30 +
 libsemanage/tests/test_handle.c         |  329 ++++++++++
 libsemanage/tests/test_handle.cil       |   21 +
 libsemanage/tests/test_handle.h         |   30 +
 libsemanage/tests/test_ibendport.c      |  525 ++++++++++++++++
 libsemanage/tests/test_ibendport.cil    |   28 +
 libsemanage/tests/test_ibendport.h      |   30 +
 libsemanage/tests/test_iface.c          |  666 ++++++++++++++++++++
 libsemanage/tests/test_iface.cil        |   28 +
 libsemanage/tests/test_iface.h          |   30 +
 libsemanage/tests/test_node.c           |  807 ++++++++++++++++++++++++
 libsemanage/tests/test_node.cil         |   28 +
 libsemanage/tests/test_node.h           |   30 +
 libsemanage/tests/test_other.c          |  120 ++++
 libsemanage/tests/test_other.h          |   30 +
 libsemanage/tests/test_port.c           |  909 +++++++++++++++++++++++++++
 libsemanage/tests/test_port.cil         |   27 +
Quoted 10 lines
 libsemanage/tests/test_port.h           |   30 +
 libsemanage/tests/test_semanage_store.c |    2 +-
 libsemanage/tests/test_user.c           |  690 ++++++++++++++++++++
 libsemanage/tests/test_user.cil         |   27 +
 libsemanage/tests/test_user.h           |   30 +
 libsemanage/tests/utilities.c           |  254 +++++++-
 libsemanage/tests/utilities.h           |   53 +-
 32 files changed, 6831 insertions(+), 10 deletions(-)

Ack
Staged: https://github.com/SELinuxProject/selinux/pull/159

#13 lore raw

merged

Quoted 65 lines · William Roberts
Quoted 51 lines · Jan Zarsky

This series adds new test suites for libsemanage. In the first patch, common helper functions are added. The other patches are independent and each adds a new test suite.

The test suites are testing only the public API and aim for line coverage. They test only correct usage of the functions. Together with valgrind, the test suites can be used for detecting memory leaks.

The tests run fine in Travis CI and do not require an SELinux-enabled system.

I welcome any suggestions.

Jan Zarsky (11):
libsemanage: add helper functions to tests
libsemanage: test semanage_handle_* functions
libsemanage: test semanage_bool_* functions
libsemanage: test semanage_fcontext functions
libsemanage: test semanage_iface_* functions
libsemanage: test semanage_ibendport_* functions
libsemanage: test semanage_node_* functions
libsemanage: test semanage_port_* functions
libsemanage: test semanage_user_* functions
libsemanage: test semanage_context_* functions
libsemanage: test semanage_msg_default_handler

 libsemanage/tests/.gitignore            |    1 +
 libsemanage/tests/Makefile              |   11 +-
 libsemanage/tests/libsemanage-tests.c   |   18 +
 libsemanage/tests/test_bool.c           |  932 +++++++++++++++++++++++++++
 libsemanage/tests/test_bool.cil         |   24 +
 libsemanage/tests/test_bool.h           |   31 +
 libsemanage/tests/test_fcontext.c       | 1045 +++++++++++++++++++++++++++++++
 libsemanage/tests/test_fcontext.cil     |   25 +
 libsemanage/tests/test_fcontext.h       |   30 +
 libsemanage/tests/test_handle.c         |  329 ++++++++++
 libsemanage/tests/test_handle.cil       |   21 +
 libsemanage/tests/test_handle.h         |   30 +
 libsemanage/tests/test_ibendport.c      |  525 ++++++++++++++++
 libsemanage/tests/test_ibendport.cil    |   28 +
 libsemanage/tests/test_ibendport.h      |   30 +
 libsemanage/tests/test_iface.c          |  666 ++++++++++++++++++++
 libsemanage/tests/test_iface.cil        |   28 +
 libsemanage/tests/test_iface.h          |   30 +
 libsemanage/tests/test_node.c           |  807 ++++++++++++++++++++++++
 libsemanage/tests/test_node.cil         |   28 +
 libsemanage/tests/test_node.h           |   30 +
 libsemanage/tests/test_other.c          |  120 ++++
 libsemanage/tests/test_other.h          |   30 +
 libsemanage/tests/test_port.c           |  909 +++++++++++++++++++++++++++
 libsemanage/tests/test_port.cil         |   27 +
Quoted 10 lines
 libsemanage/tests/test_port.h           |   30 +
 libsemanage/tests/test_semanage_store.c |    2 +-
 libsemanage/tests/test_user.c           |  690 ++++++++++++++++++++
 libsemanage/tests/test_user.cil         |   27 +
 libsemanage/tests/test_user.h           |   30 +
 libsemanage/tests/utilities.c           |  254 +++++++-
 libsemanage/tests/utilities.h           |   53 +-
 32 files changed, 6831 insertions(+), 10 deletions(-)

Ack
Staged: https://github.com/SELinuxProject/selinux/pull/159