All of lore.kernel.org
 help / color / mirror / Atom feed
From: jbrindle@tresys.com
To: selinux@tycho.nsa.gov
Subject: [PATCH 30/33] libsemanage: node serialization tests
Date: Mon, 23 Apr 2007 17:35:25 -0400	[thread overview]
Message-ID: <20070423213750.314752000@tresys.com> (raw)
In-Reply-To: 20070423213455.741326000@tresys.com

---
 libsemanage/tests/libsemanage-tests.c |    6 
 libsemanage/tests/test_nodes_file.c   |  529 ++++++++++++++++++++++++++++++++++
 libsemanage/tests/test_nodes_file.h   |   32 ++
 libsemanage/tests/test_nodes_policy.c |  392 +++++++++++++++++++++++++
 libsemanage/tests/test_nodes_policy.h |   32 ++
 5 files changed, 991 insertions(+)

Index: selinux-pms-support/libsemanage/tests/libsemanage-tests.c
===================================================================
--- selinux-pms-support.orig/libsemanage/tests/libsemanage-tests.c
+++ selinux-pms-support/libsemanage/tests/libsemanage-tests.c
@@ -45,6 +45,9 @@
 #include "test_interfaces_file.h"
 #include "test_interfaces_policy.h"
 
+#include "test_nodes_file.h"
+#include "test_nodes_policy.h"
+
 #include <CUnit/Basic.h>
 #include <CUnit/Console.h>
 #include <CUnit/TestDB.h>
@@ -110,6 +113,9 @@ static int do_tests(int interactive, int
 	DECLARE_SUITE(interfaces_file);
 	DECLARE_SUITE(interfaces_policy);
 
+	DECLARE_SUITE(nodes_file);
+	DECLARE_SUITE(nodes_policy);
+
 	/* The ps_api_disconnect test 'unforks'. */
 	DECLARE_SUITE(ps_api_disconnect);
 
Index: selinux-pms-support/libsemanage/tests/test_nodes_file.c
===================================================================
--- /dev/null
+++ selinux-pms-support/libsemanage/tests/test_nodes_file.c
@@ -0,0 +1,529 @@
+/* Authors: Christopher Ashworth <cashworth@tresys.com>
+ *          Caleb Case <ccase@tresys.com>
+ *
+ * Copyright (C)  2007 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
+ */
+
+/*  The purpose of this file is to provide unit tests of the functions in:
+ *
+ *  libsemanage/src/nodes_file.c
+ *
+ */
+
+#include "globals.h"
+#include "utilities.h"
+#include "test_nodes_file.h"
+
+#include <libgen.h>
+#include <limits.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/wait.h>
+#include <unistd.h>
+#include <CUnit/Basic.h>
+
+#include <assert.h>
+#include <ctype.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <limits.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <arpa/inet.h>
+#include <netinet/in.h>
+#include <sys/socket.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <sys/un.h>
+#include <selinux/selinux.h>
+
+#include "debug.h"
+#include "byteswap.h"
+#include "handle.h"
+#include "modules.h"
+#include "policy.h"
+#include "nodes_local.h"
+#include "messages_internal.h"
+#include "semanage_store.h"
+#include "database_file.h"
+#include "user_internal.h"
+#include "seuser_internal.h"
+#include "port_internal.h"
+#include "iface_internal.h"
+#include "boolean_internal.h"
+#include "fcontext_internal.h"
+#include "node_internal.h"
+
+#include "database_llist.h"
+
+/* Server table setup. */
+
+/* Record functions. */
+
+static record_table_t test_node_rtable = {
+	.create = (create_f)semanage_node_create,
+	.key_extract = (key_extract_f)semanage_node_key_extract,
+	.key_free = (key_free_f)semanage_node_key_free,
+	.clone = (clone_f)semanage_node_clone,
+	.compare = (compare_f)semanage_node_compare,
+	.compare2 = (compare2_f)semanage_node_compare2,
+	.compare2_qsort = NULL,
+	.free = (free_f)semanage_node_free,
+	.serialize = (serialize_f)semanage_node_serialize,
+	.unserialize = (unserialize_f)semanage_node_unserialize,
+};
+
+/* Table functions. */
+
+/* Test cache function. */
+static int test_dbase_file_cache(
+	semanage_handle_t *handle,
+	dbase_config_t *dconfig)
+{
+	int status = 0;
+
+	/* Add some test entries to the list. */
+	record_t *node = NULL;
+	record_key_t *nodekey = NULL;
+
+	record_table_t *rtable = dconfig->dtable->get_rtable(dconfig);
+
+	/* Create record. */
+	status = rtable->create(sh, &node);
+	if (status) goto cleanup;
+
+	/* Populate record. */
+	semanage_context_t *context;
+	int proto = SEMANAGE_PROTO_IP4;
+
+	status = semanage_node_set_addr(sh, (semanage_node_t *)node, proto, "10.10.10.10");
+	if (status) goto cleanup;
+	status = semanage_node_set_mask(sh, (semanage_node_t *)node, proto, "255.255.255.255");
+
+	status = semanage_context_from_string(sh, "testuser:testrole:testtype", &context);
+	if (status) goto cleanup;
+	status = semanage_node_set_con(sh, (semanage_node_t *)node, context);
+	if (status) goto cleanup;
+
+	status = rtable->key_extract(sh, node, &nodekey);
+	if (status) goto cleanup;
+
+	/* Add record. */
+	status = dconfig->dtable->add(
+		sh,
+		dconfig,
+		nodekey,
+		node);
+	if (status) goto cleanup;
+
+	/* Cleanup. */
+	semanage_context_free(context);
+	context = NULL;
+	rtable->free(node);
+	node = NULL;
+	rtable->key_free(nodekey);
+	nodekey = NULL;
+
+	/* Create record. */
+	status = rtable->create(sh, &node);
+	if (status) goto cleanup;
+
+	/* Populate record. */
+	status = semanage_node_set_addr(sh, (semanage_node_t *)node, proto, "10.10.10.1");
+	if (status) goto cleanup;
+	status = semanage_node_set_mask(sh, (semanage_node_t *)node, proto, "255.255.255.1");
+
+	status = semanage_context_from_string(sh, "testuser2:testrole2:testtype2", &context);
+	if (status) goto cleanup;
+	status = semanage_node_set_con(sh, (semanage_node_t *)node, context);
+	if (status) goto cleanup;
+
+	status = rtable->key_extract(sh, node, &nodekey);
+	if (status) goto cleanup;
+
+	/* Add record. */
+	status = dconfig->dtable->add(
+		sh,
+		dconfig,
+		nodekey,
+		node);
+	if (status) goto cleanup;
+
+	/* Cleanup. */
+cleanup:
+	semanage_context_free(context);
+	rtable->free(node);
+	rtable->key_free(nodekey);
+
+	CU_ASSERT( status == 0 );
+	return status;
+}
+
+/* Database callback table. */
+static dbase_table_t test_file_dtable = {
+
+	/* Cache/Transactions */
+	.cache = test_dbase_file_cache,
+	.drop_cache = (void *)dbase_llist_drop_cache,
+	.flush = NULL,
+	.is_modified = (void *)dbase_llist_is_modified,
+
+	/* Database API */
+	.iterate = (void *)dbase_llist_iterate,
+	.exists = (void *)dbase_llist_exists,
+	.list = (void *)dbase_llist_list,
+	.add = (void *)dbase_llist_add,
+	.set = (void *)dbase_llist_set,
+	.del = (void *)dbase_llist_del,
+	.clear = (void *)dbase_llist_clear,
+	.modify = (void *)dbase_llist_modify,
+	.query = (void *)dbase_llist_query,
+	.count = (void *)dbase_llist_count,
+
+	/* Polymorphism */
+	.get_rtable = (void *)dbase_llist_get_rtable
+};
+
+/* The suite initialization function.
+ * Returns zero on success, non-zero otherwise.
+ */
+int nodes_file_test_init(void)
+{
+	int status = 0;
+
+	if (ps_pid == 0) {
+		/* Server code. */
+
+		/* Reset locks status. */
+		sh->u.ps_handle.socket_fd = 0;
+
+		/* Store setup. */
+		dbase_config_t *dconfig = semanage_node_dbase_local(sh);
+
+		if ((status =
+			dbase_file_init(
+				sh,
+				"fakesuffix",
+				&test_node_rtable,
+				NULL,
+				(dbase_file_t **)&dconfig->dbase)))
+			goto cleanup;
+
+		dconfig->dtable = &test_file_dtable;
+
+	}
+
+	/* Cleanup. */
+cleanup:
+	return status;
+}
+
+/* The suite cleanup function.
+ * Returns zero on success, non-zero otherwise.
+ */
+int nodes_file_test_cleanup(void)
+{
+	return 0;
+}
+
+/* Adds all the tests needed for this suite.
+*/
+int nodes_file_add_tests(CU_pSuite suite)
+{
+	CU_ErrorCode status;
+
+	if (NULL == CU_add_test(suite, "semanage_node_serialize_local", test_semanage_node_serialize_local))
+		goto cleanup;
+
+cleanup:
+	if (CUE_SUCCESS != (status = CU_get_error()))
+		CU_cleanup_registry();
+	return status;
+}
+
+/* Tests the semanage_node_serialize_local function in nodes_file.c
+ */
+void test_semanage_node_serialize_local(void) {
+	int status;
+
+	uint32_t message_type;
+	uint64_t data_length;
+	char *data = NULL;
+	char *ptr = NULL;
+	int timeout = 0;
+	uint32_t database_type = 0;
+
+	semanage_node_t **records = NULL;
+	unsigned int records_size;
+
+	dbase_config_t *dconfig = semanage_node_dbase_local(sh);
+	record_table_t *rtable = dconfig->dtable->get_rtable(dconfig);
+
+	char *addr = NULL;
+	char *mask = NULL;
+	char *context_str = NULL;
+
+	int commit_number = 7;
+
+	if (ps_pid == 0) {
+		/* Server code. */
+
+		/* On caching the client will ask for the commit number. */
+		status = test_semanage_ps_handle_get_commit_number(sh, commit_number);
+		CU_ASSERT( status == STATUS_SUCCESS );
+		if (status != STATUS_SUCCESS)
+			goto cleanup;
+
+		/* Wait for message. */
+		if ((status = read_msg(sh, client_socket_fd, timeout, &message_type, &data_length, &data)))
+			goto cleanup;
+
+		CU_ASSERT( message_type == PS_GET_DATABASE );
+		if (message_type != PS_GET_DATABASE) {
+			status = -1;
+			goto cleanup;
+		}
+
+		/* Get and serialize the database. */
+		free(data);
+		data = NULL;
+
+		if ((status = semanage_node_serialize_local(sh, &data, &data_length)))
+			goto cleanup;
+
+		/* Send back the database. */
+		if ((status = write_msg(sh, client_socket_fd, PS_OK, data_length, data)))
+			goto cleanup;
+
+		/* On caching the client will ask for the commit number. */
+		status = test_semanage_ps_handle_get_commit_number(sh, commit_number);
+		CU_ASSERT( status == STATUS_SUCCESS );
+		if (status != STATUS_SUCCESS)
+			goto cleanup;
+
+		/* On flushing the client will ask for the commit number. */
+		//FIXME: Why does the client ask for the commit number so many times here?
+		status = test_semanage_ps_handle_get_commit_number(sh, commit_number);
+		CU_ASSERT( status == STATUS_SUCCESS );
+		if (status != STATUS_SUCCESS)
+			goto cleanup;
+
+		status = test_semanage_ps_handle_get_commit_number(sh, commit_number);
+		CU_ASSERT( status == STATUS_SUCCESS );
+		if (status != STATUS_SUCCESS)
+			goto cleanup;
+
+		/* Receive changed database. */
+		free(data);
+		data = NULL;
+
+		if ((status = read_msg(sh, client_socket_fd, timeout, &message_type, &data_length, &data)))
+			goto cleanup;
+
+		CU_ASSERT( message_type == PS_PUT_DATABASE );
+		if (message_type != PS_PUT_DATABASE) {
+			status = -1;
+			goto cleanup;
+		}
+
+		/* Say we got it ok. */
+		if ((status = write_msg(sh, client_socket_fd, PS_OK, 0, NULL)))
+			goto cleanup;
+
+		/* Unserialize the database type */
+		ptr = data;
+		if(semanage_dbase_ps_database_type_unserialize(sh, &ptr, &data_length, &database_type));
+			goto cleanup;
+
+		/* Unserialize the database. and don't move the data pointer.*/
+		if ((status = semanage_node_unserialize_local(sh, ptr, data_length)))
+			goto cleanup;
+
+		/* Gather the database records. */
+		status = dconfig->dtable->list(sh, dconfig, (record_t ***)&records, &records_size);
+		CU_ASSERT( status == 0 );
+		CU_ASSERT( records != NULL );
+		CU_ASSERT( records_size == 3 );
+
+		/* Verify data. */
+		int proto = semanage_node_get_proto(records[0]);
+		status = semanage_node_get_addr(sh, records[0], &addr);
+		status = semanage_node_get_mask(sh, records[0], &mask);
+
+		semanage_context_t *context = semanage_node_get_con(records[0]);
+		status = semanage_context_to_string(sh, context, &context_str);
+
+		CU_ASSERT( proto == SEMANAGE_PROTO_IP4 );
+		CU_ASSERT( strcmp(addr, "10.10.10.10") == 0 );
+		CU_ASSERT( strcmp(mask, "255.255.255.255") == 0 );
+		CU_ASSERT( strcmp(context_str, "testuser:testrole:testtype") == 0 );
+
+		/* Cleanup. */
+		free(addr);
+		free(mask);
+		free(context_str);
+
+		proto = semanage_node_get_proto(records[1]);
+		status = semanage_node_get_addr(sh, records[1], &addr);
+		status = semanage_node_get_mask(sh, records[1], &mask);
+
+		context = semanage_node_get_con(records[1]);
+		status = semanage_context_to_string(sh, context, &context_str);
+
+		CU_ASSERT( proto == SEMANAGE_PROTO_IP4 );
+		CU_ASSERT( strcmp(addr, "10.10.10.1") == 0 );
+		CU_ASSERT( strcmp(mask, "255.255.255.1") == 0 );
+		CU_ASSERT( strcmp(context_str, "testuser2:testrole2:testtype2") == 0 );
+
+		/* Cleanup. */
+		free(addr);
+		free(mask);
+		free(context_str);
+
+		proto = semanage_node_get_proto(records[2]);
+		status = semanage_node_get_addr(sh, records[2], &addr);
+		status = semanage_node_get_mask(sh, records[2], &mask);
+
+		context = semanage_node_get_con(records[2]);
+		status = semanage_context_to_string(sh, context, &context_str);
+
+		CU_ASSERT( proto == SEMANAGE_PROTO_IP4 );
+		CU_ASSERT( strcmp(addr, "10.10.10.2") == 0 );
+		CU_ASSERT( strcmp(mask, "255.255.255.2") == 0 );
+		CU_ASSERT( strcmp(context_str, "testuser3:testrole3:testtype3") == 0 );
+	}
+
+	if (ps_pid > 0) {
+		/* Client code. */
+
+		/* Cache database.
+		 * This sends a message to the server requesting the database.
+		 * The server serializes the database and sends it to us (the client).
+		 * Internally the database is unserialized and loaded.
+		 */
+		status = dconfig->dtable->cache(sh, dconfig);
+		CU_ASSERT( status == 0 );
+
+		/* Gather the database records. */
+		status = dconfig->dtable->list(sh, dconfig, (record_t ***)&records, &records_size);
+		CU_ASSERT( status == 0 );
+		CU_ASSERT( records != NULL );
+		CU_ASSERT( records_size == 2 );
+
+		/* Verify data was sent correctly. */
+		int proto = semanage_node_get_proto(records[0]);
+		status = semanage_node_get_addr(sh, records[0], &addr);
+		status = semanage_node_get_mask(sh, records[0], &mask);
+
+		semanage_context_t *context = semanage_node_get_con(records[0]);
+		status = semanage_context_to_string(sh, context, &context_str);
+
+		CU_ASSERT( proto == SEMANAGE_PROTO_IP4 );
+		CU_ASSERT( strcmp(addr, "10.10.10.10") == 0 );
+		CU_ASSERT( strcmp(mask, "255.255.255.255") == 0 );
+		CU_ASSERT( strcmp(context_str, "testuser:testrole:testtype") == 0 );
+
+		/* Cleanup. */
+		free(addr);
+		free(mask);
+		free(context_str);
+
+		proto = semanage_node_get_proto(records[1]);
+		status = semanage_node_get_addr(sh, records[1], &addr);
+		status = semanage_node_get_mask(sh, records[1], &mask);
+
+		context = semanage_node_get_con(records[1]);
+		status = semanage_context_to_string(sh, context, &context_str);
+
+		CU_ASSERT( proto == SEMANAGE_PROTO_IP4 );
+		CU_ASSERT( strcmp(addr, "10.10.10.1") == 0 );
+		CU_ASSERT( strcmp(mask, "255.255.255.1") == 0 );
+		CU_ASSERT( strcmp(context_str, "testuser2:testrole2:testtype2") == 0 );
+
+		/* Change something. */
+
+		/* Add some test entries to the list. */
+		record_t *node = NULL;
+		record_key_t *nodekey = NULL;
+
+		/* Create record. */
+		status = rtable->create(sh, &node);
+		if (status) goto cleanup;
+
+		/* Populate record. */
+		status = semanage_node_set_addr(sh, (semanage_node_t *)node, proto, "10.10.10.2");
+		if (status) goto cleanup;
+		status = semanage_node_set_mask(sh, (semanage_node_t *)node, proto, "255.255.255.2");
+
+		status = semanage_context_from_string(sh, "testuser3:testrole3:testtype3", &context);
+		if (status) goto cleanup;
+		status = semanage_node_set_con(sh, (semanage_node_t *)node, context);
+		if (status) goto cleanup;
+
+		status = rtable->key_extract(sh, node, &nodekey);
+		if (status) goto cleanup;
+
+		/* Add record. */
+		status = dconfig->dtable->add(
+				sh,
+				dconfig,
+				nodekey,
+				node);
+		if (status) goto cleanup;
+
+		/* Cleanup. */
+		semanage_context_free(context);
+		rtable->free(node);
+		rtable->key_free(nodekey);
+
+		/* Flush database.
+		 * This sends a message to the server to replace its database
+		 * with the one we are sending.  Internally the flush serializes
+		 * the database and sends it.  The server then unserializes it,
+		 * clears its old copy, and replaces it with the new one.
+		 */
+		status = dconfig->dtable->flush(sh, dconfig);
+		CU_ASSERT( status == 0 );
+	}
+
+	/* Cleanup. */
+cleanup:
+	if (records != NULL) {
+		unsigned int i;
+		for (i = 0; i < records_size; i++) {
+			if (!records[i])
+				break;
+			rtable->free((record_t *)records[i]);
+		}
+	}
+
+	free(records);
+	free(data);
+	free(addr);
+	free(mask);
+	free(context_str);
+
+	CU_ASSERT( status == 0 );
+	if (status != 0) {
+		printf("\n\nReceived error code in %s: %d | %s\n", (ps_pid == 0) ? "server" : "client", status, strerror(status));
+	}
+}
+
Index: selinux-pms-support/libsemanage/tests/test_nodes_file.h
===================================================================
--- /dev/null
+++ selinux-pms-support/libsemanage/tests/test_nodes_file.h
@@ -0,0 +1,32 @@
+/* Authors: Christopher Ashworth <cashworth@tresys.com>
+ *          Caleb Case <ccase@tresys.com>
+ *
+ * Copyright (C)  2007 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_NODES_FILE_H__
+#define __TEST_NODES_FILE_H__
+
+#include <CUnit/Basic.h>
+
+int nodes_file_test_init(void);
+int nodes_file_test_cleanup(void);
+int nodes_file_add_tests(CU_pSuite suite);
+
+void test_semanage_node_serialize_local(void);
+
+#endif
Index: selinux-pms-support/libsemanage/tests/test_nodes_policy.c
===================================================================
--- /dev/null
+++ selinux-pms-support/libsemanage/tests/test_nodes_policy.c
@@ -0,0 +1,392 @@
+/* Authors: Christopher Ashworth <cashworth@tresys.com>
+ *          Caleb Case <ccase@tresys.com>
+ *
+ * Copyright (C)  2007 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
+ */
+
+/*  The purpose of this file is to provide unit tests of the functions in:
+ *
+ *  libsemanage/src/nodes_policy.c
+ *
+ */
+
+#include "globals.h"
+#include "utilities.h"
+#include "test_nodes_policy.h"
+
+#include <libgen.h>
+#include <limits.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/wait.h>
+#include <unistd.h>
+#include <CUnit/Basic.h>
+
+#include <assert.h>
+#include <ctype.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <limits.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <arpa/inet.h>
+#include <netinet/in.h>
+#include <sys/socket.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <sys/un.h>
+#include <selinux/selinux.h>
+
+#include "debug.h"
+#include "byteswap.h"
+#include "handle.h"
+#include "modules.h"
+#include "policy.h"
+#include "nodes_policy.h"
+#include "messages_internal.h"
+#include "semanage_store.h"
+#include "database_policydb.h"
+#include "user_internal.h"
+#include "seuser_internal.h"
+#include "port_internal.h"
+#include "iface_internal.h"
+#include "boolean_internal.h"
+#include "fcontext_internal.h"
+#include "node_internal.h"
+
+#include "database_llist.h"
+
+/* Server table setup. */
+
+/* Record functions. */
+
+static record_table_t test_node_rtable = {
+	.create = (create_f)semanage_node_create,
+	.key_extract = (key_extract_f)semanage_node_key_extract,
+	.key_free = (key_free_f)semanage_node_key_free,
+	.clone = (clone_f)semanage_node_clone,
+	.compare = (compare_f)semanage_node_compare,
+	.compare2 = (compare2_f)semanage_node_compare2,
+	.compare2_qsort = NULL,
+	.free = (free_f)semanage_node_free,
+	.serialize = (serialize_f)semanage_node_serialize,
+	.unserialize = (unserialize_f)semanage_node_unserialize,
+};
+
+/* Table functions. */
+
+/* Test cache function. */
+static int test_dbase_policydb_cache(
+	semanage_handle_t *handle,
+	dbase_config_t *dconfig)
+{
+	int status = 0;
+
+	/* Add some test entries to the list. */
+	record_t *node = NULL;
+	record_key_t *nodekey = NULL;
+
+	record_table_t *rtable = dconfig->dtable->get_rtable(dconfig);
+
+	/* Create record. */
+	status = rtable->create(sh, &node);
+	if (status) goto cleanup;
+
+	/* Populate record. */
+	semanage_context_t *context;
+	int proto = SEMANAGE_PROTO_IP4;
+
+	status = semanage_node_set_addr(sh, (semanage_node_t *)node, proto, "10.10.10.10");
+	if (status) goto cleanup;
+	status = semanage_node_set_mask(sh, (semanage_node_t *)node, proto, "255.255.255.255");
+
+	status = semanage_context_from_string(sh, "testuser:testrole:testtype", &context);
+	if (status) goto cleanup;
+	status = semanage_node_set_con(sh, (semanage_node_t *)node, context);
+	if (status) goto cleanup;
+
+	status = rtable->key_extract(sh, node, &nodekey);
+	if (status) goto cleanup;
+
+	/* Add record. */
+	status = dconfig->dtable->add(
+		sh,
+		dconfig,
+		nodekey,
+		node);
+	if (status) goto cleanup;
+
+	/* Cleanup. */
+	semanage_context_free(context);
+	context = NULL;
+	rtable->free(node);
+	node = NULL;
+	rtable->key_free(nodekey);
+	nodekey = NULL;
+
+	/* Create record. */
+	status = rtable->create(sh, &node);
+	if (status) goto cleanup;
+
+	/* Populate record. */
+	status = semanage_node_set_addr(sh, (semanage_node_t *)node, proto, "10.10.10.1");
+	if (status) goto cleanup;
+	status = semanage_node_set_mask(sh, (semanage_node_t *)node, proto, "255.255.255.1");
+
+	status = semanage_context_from_string(sh, "testuser2:testrole2:testtype2", &context);
+	if (status) goto cleanup;
+	status = semanage_node_set_con(sh, (semanage_node_t *)node, context);
+	if (status) goto cleanup;
+
+	status = rtable->key_extract(sh, node, &nodekey);
+	if (status) goto cleanup;
+
+	/* Add record. */
+	status = dconfig->dtable->add(
+		sh,
+		dconfig,
+		nodekey,
+		node);
+	if (status) goto cleanup;
+
+	/* Cleanup. */
+cleanup:
+	semanage_context_free(context);
+	rtable->free(node);
+	rtable->key_free(nodekey);
+
+	CU_ASSERT( status == 0 );
+	return status;
+}
+
+/* Database callback table. */
+static dbase_table_t test_policydb_dtable = {
+
+	/* Cache/Transactions */
+	.cache = test_dbase_policydb_cache,
+	.drop_cache = (void *)dbase_llist_drop_cache,
+	.flush = NULL,
+	.is_modified = (void *)dbase_llist_is_modified,
+
+	/* Database API */
+	.iterate = (void *)dbase_llist_iterate,
+	.exists = (void *)dbase_llist_exists,
+	.list = (void *)dbase_llist_list,
+	.add = (void *)dbase_llist_add,
+	.set = (void *)dbase_llist_set,
+	.del = (void *)dbase_llist_del,
+	.clear = (void *)dbase_llist_clear,
+	.modify = (void *)dbase_llist_modify,
+	.query = (void *)dbase_llist_query,
+	.count = (void *)dbase_llist_count,
+
+	/* Polymorphism */
+	.get_rtable = (void *)dbase_llist_get_rtable
+};
+
+/* The suite initialization function.
+ * Returns zero on success, non-zero otherwise.
+ */
+int nodes_policy_test_init(void)
+{
+	int status = 0;
+
+	if (ps_pid == 0) {
+		/* Server code. */
+
+		/* Reset locks status. */
+		sh->u.ps_handle.socket_fd = 0;
+
+		/* Store setup. */
+		dbase_config_t *dconfig = semanage_node_dbase_policy(sh);
+
+		if ((status =
+			dbase_policydb_init(
+				sh,
+				"fakesuffix",
+				&test_node_rtable,
+				NULL,
+				(dbase_policydb_t **)&dconfig->dbase)))
+			goto cleanup;
+
+		dconfig->dtable = &test_policydb_dtable;
+
+	}
+
+	/* Cleanup. */
+cleanup:
+	return status;
+}
+
+/* The suite cleanup function.
+ * Returns zero on success, non-zero otherwise.
+ */
+int nodes_policy_test_cleanup(void)
+{
+	return 0;
+}
+
+/* Adds all the tests needed for this suite.
+*/
+int nodes_policy_add_tests(CU_pSuite suite)
+{
+	CU_ErrorCode status;
+
+	if (NULL == CU_add_test(suite, "semanage_node_serialize_policy", test_semanage_node_serialize_policy))
+		goto cleanup;
+
+cleanup:
+	if (CUE_SUCCESS != (status = CU_get_error()))
+		CU_cleanup_registry();
+	return status;
+}
+
+/* Tests the semanage_node_serialize_policy function in nodes_policy.c
+ */
+void test_semanage_node_serialize_policy(void) {
+	int status;
+
+	uint32_t message_type;
+	uint64_t data_length;
+	char *data = NULL;
+	int timeout = 0;
+
+	semanage_node_t **records = NULL;
+	unsigned int records_size;
+
+	dbase_config_t *dconfig = semanage_node_dbase_policy(sh);
+	record_table_t *rtable = dconfig->dtable->get_rtable(dconfig);
+
+	char *addr = NULL;
+	char *mask = NULL;
+	char *context_str = NULL;
+
+	int commit_number = 7;
+
+	if (ps_pid == 0) {
+		/* Server code. */
+
+		/* On caching the client will ask for the commit number. */
+		status = test_semanage_ps_handle_get_commit_number(sh, commit_number);
+		CU_ASSERT( status == STATUS_SUCCESS );
+		if (status != STATUS_SUCCESS)
+			goto cleanup;
+
+		/* Wait for message. */
+		if ((status = read_msg(sh, client_socket_fd, timeout, &message_type, &data_length, &data)))
+			goto cleanup;
+
+		CU_ASSERT( message_type == PS_GET_DATABASE );
+		if (message_type != PS_GET_DATABASE) {
+			status = -1;
+			goto cleanup;
+		}
+
+		/* Get and serialize the database. */
+		free(data);
+		data = NULL;
+
+		if ((status = semanage_node_serialize_policy(sh, &data, &data_length)))
+			goto cleanup;
+
+		/* Send back the database. */
+		if ((status = write_msg(sh, client_socket_fd, PS_OK, data_length, data)))
+			goto cleanup;
+
+		/* On caching the client will ask for the commit number. */
+		status = test_semanage_ps_handle_get_commit_number(sh, commit_number);
+		CU_ASSERT( status == STATUS_SUCCESS );
+		if (status != STATUS_SUCCESS)
+			goto cleanup;
+
+	}
+
+	if (ps_pid > 0) {
+		/* Client code. */
+
+		/* Cache database.
+		 * This sends a message to the server requesting the database.
+		 * The server serializes the database and sends it to us (the client).
+		 * Internally the database is unserialized and loaded.
+		 */
+		status = dconfig->dtable->cache(sh, dconfig);
+		CU_ASSERT( status == 0 );
+
+		/* Gather the database records. */
+		status = dconfig->dtable->list(sh, dconfig, (record_t ***)&records, &records_size);
+		CU_ASSERT( status == 0 );
+		CU_ASSERT( records != NULL );
+		CU_ASSERT( records_size == 2 );
+
+		/* Verify data was sent correctly. */
+		int proto = semanage_node_get_proto(records[0]);
+		status = semanage_node_get_addr(sh, records[0], &addr);
+		status = semanage_node_get_mask(sh, records[0], &mask);
+
+		semanage_context_t *context = semanage_node_get_con(records[0]);
+		status = semanage_context_to_string(sh, context, &context_str);
+
+		CU_ASSERT( proto == SEMANAGE_PROTO_IP4 );
+		CU_ASSERT( strcmp(addr, "10.10.10.10") == 0 );
+		CU_ASSERT( strcmp(mask, "255.255.255.255") == 0 );
+		CU_ASSERT( strcmp(context_str, "testuser:testrole:testtype") == 0 );
+
+		/* Cleanup. */
+		free(addr);
+		free(mask);
+		free(context_str);
+
+		proto = semanage_node_get_proto(records[1]);
+		status = semanage_node_get_addr(sh, records[1], &addr);
+		status = semanage_node_get_mask(sh, records[1], &mask);
+
+		context = semanage_node_get_con(records[1]);
+		status = semanage_context_to_string(sh, context, &context_str);
+
+		CU_ASSERT( proto == SEMANAGE_PROTO_IP4 );
+		CU_ASSERT( strcmp(addr, "10.10.10.1") == 0 );
+		CU_ASSERT( strcmp(mask, "255.255.255.1") == 0 );
+		CU_ASSERT( strcmp(context_str, "testuser2:testrole2:testtype2") == 0 );
+	}
+
+	/* Cleanup. */
+cleanup:
+	if (records != NULL) {
+		unsigned int i;
+		for (i = 0; i < records_size; i++) {
+			if (!records[i])
+				break;
+			rtable->free((record_t *)records[i]);
+		}
+	}
+
+	free(records);
+	free(data);
+	free(addr);
+	free(mask);
+	free(context_str);
+
+	CU_ASSERT( status == 0 );
+	if (status != 0) {
+		printf("\n\nReceived error code in %s: %d | %s\n", (ps_pid == 0) ? "server" : "client", status, strerror(status));
+	}
+}
+
Index: selinux-pms-support/libsemanage/tests/test_nodes_policy.h
===================================================================
--- /dev/null
+++ selinux-pms-support/libsemanage/tests/test_nodes_policy.h
@@ -0,0 +1,32 @@
+/* Authors: Christopher Ashworth <cashworth@tresys.com>
+ *          Caleb Case <ccase@tresys.com>
+ *
+ * Copyright (C)  2007 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_NODES_POLICY_H__
+#define __TEST_NODES_POLICY_H__
+
+#include <CUnit/Basic.h>
+
+int nodes_policy_test_init(void);
+int nodes_policy_test_cleanup(void);
+int nodes_policy_add_tests(CU_pSuite suite);
+
+void test_semanage_node_serialize_policy(void);
+
+#endif

-- 

--
This message was distributed to subscribers of the selinux mailing list.
If you no longer wish to subscribe, send mail to majordomo@tycho.nsa.gov with
the words "unsubscribe selinux" without quotes as the message.

  parent reply	other threads:[~2007-04-24 18:31 UTC|newest]

Thread overview: 58+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-04-23 21:34 [PATCH 00/33] libsemanage/libsepol object serialization and ps-api jbrindle
2007-04-23 21:34 ` [PATCH 01/33] libsepol: basic serilization support jbrindle
2007-04-24 20:00   ` Karl MacMillan
2007-04-24 22:29     ` Joshua Brindle
2007-04-25  4:49       ` Karl MacMillan
2007-04-25 14:14         ` Joshua Brindle
2007-04-25 15:16           ` Karl MacMillan
2007-04-25 15:21             ` Joshua Brindle
2007-04-25 15:40               ` Karl MacMillan
2007-04-25 15:52                 ` Joshua Brindle
2007-04-25 16:00                   ` Karl MacMillan
2007-04-25 16:25                     ` Joshua Brindle
2007-04-25 17:11                       ` James Antill
2007-04-25 18:08                         ` Karl MacMillan
2007-04-23 21:34 ` [PATCH 02/33] libsepol: boolean serialization jbrindle
2007-04-25  4:56   ` Karl MacMillan
2007-04-23 21:34 ` [PATCH 03/33] libsepol: context serialization jbrindle
2007-04-23 21:34 ` [PATCH 04/33] libsepol: interface serialization jbrindle
2007-04-23 21:35 ` [PATCH 05/33] libsepol: node serialization jbrindle
2007-04-23 21:35 ` [PATCH 06/33] libsepol: port serialization jbrindle
2007-04-23 21:35 ` [PATCH 07/33] libsepol: user serialization jbrindle
2007-04-23 21:35 ` [PATCH 08/33] libsemanage: DESTDIR support in INCLUDE and safe test target jbrindle
2007-04-23 21:35 ` [PATCH 09/33] libsemanage: dbase/dconfig cleanup jbrindle
2007-04-23 21:35 ` [PATCH 10/33] libsemanage: database serialization jbrindle
2007-04-23 21:35 ` [PATCH 11/33] libsemanage: endianness macros jbrindle
2007-04-23 21:35 ` [PATCH 12/33] libsemanage: basic serialization jbrindle
2007-04-24 21:16   ` Karl MacMillan
2007-04-24 22:31     ` Joshua Brindle
2007-04-24 22:39       ` Karl MacMillan
2007-04-23 21:35 ` [PATCH 13/33] libsemanage: testing infrastructure jbrindle
2007-04-23 21:35 ` [PATCH 14/33] libsemanage: boolean serialization jbrindle
2007-04-23 21:35 ` [PATCH 15/33] libsemanage: context serialization jbrindle
2007-04-23 21:35 ` [PATCH 16/33] libsemanage: fcontext serialization jbrindle
2007-04-23 21:35 ` [PATCH 17/33] libsemanage: interface serialization jbrindle
2007-04-23 21:35 ` [PATCH 18/33] libsemanage: node serialization jbrindle
2007-04-23 21:35 ` [PATCH 19/33] libsemanage: port serialization jbrindle
2007-04-23 21:35 ` [PATCH 20/33] libsemanage: seuser serialization jbrindle
2007-04-23 21:35 ` [PATCH 21/33] libsemanage: user serialization jbrindle
2007-04-23 21:35 ` [PATCH 22/33] libsemanage: module serialization jbrindle
2007-04-23 21:35 ` [PATCH 23/33] libsemanage: commit number serialization jbrindle
2007-04-23 21:35 ` [PATCH 24/33] libsemanage: networking support jbrindle
2007-04-23 21:35 ` [PATCH 25/33] libsemanage: policy server database hooks jbrindle
2007-04-24 21:39   ` Karl MacMillan
2007-04-24 22:39     ` Joshua Brindle
2007-04-24 23:20       ` Karl MacMillan
2007-04-24 23:57         ` Joshua Brindle
2007-04-25  4:42           ` Karl MacMillan
2007-04-23 21:35 ` [PATCH 26/33] libsemanage: module serialization tests jbrindle
2007-04-23 21:35 ` [PATCH 27/33] libsemanage: booleans " jbrindle
2007-04-23 21:35 ` [PATCH 28/33] libsemanage: fcontexts " jbrindle
2007-04-23 21:35 ` [PATCH 29/33] libsemanage: interface " jbrindle
2007-04-23 21:35 ` jbrindle [this message]
2007-04-23 21:35 ` [PATCH 31/33] libsemanage: port " jbrindle
2007-04-23 21:35 ` [PATCH 32/33] libsemanage: seuser " jbrindle
2007-04-23 21:35 ` [PATCH 33/33] libsemanage: user " jbrindle
2007-04-24 19:48 ` [PATCH 00/33] libsemanage/libsepol object serialization and ps-api Joshua Brindle
2007-04-24 23:12 ` James Antill
2007-04-25  4:46   ` James Antill

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20070423213750.314752000@tresys.com \
    --to=jbrindle@tresys.com \
    --cc=selinux@tycho.nsa.gov \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.