From: jbrindle@tresys.com
To: selinux@tycho.nsa.gov
Subject: [PATCH 15/33] libsemanage: context serialization
Date: Mon, 23 Apr 2007 17:35:10 -0400 [thread overview]
Message-ID: <20070423213735.016593000@tresys.com> (raw)
In-Reply-To: 20070423213455.741326000@tresys.com
---
libsemanage/src/context_internal.h | 38 +++++++
libsemanage/src/context_record.c | 48 +++++++++
libsemanage/tests/libsemanage-tests.c | 2
libsemanage/tests/test_context_record.c | 154 ++++++++++++++++++++++++++++++++
libsemanage/tests/test_context_record.h | 32 ++++++
5 files changed, 272 insertions(+), 2 deletions(-)
Index: selinux-pms-support/libsemanage/src/context_internal.h
===================================================================
--- selinux-pms-support.orig/libsemanage/src/context_internal.h
+++ selinux-pms-support/libsemanage/src/context_internal.h
@@ -1,6 +1,29 @@
+/* Authors: Ivan Gyurdiev <ivg2@cornell.edu>
+ * Christopher Ashworth <cashworth@tresys.com>
+ * Caleb Case <ccase@tresys.com>
+ *
+ * Copyright (C) 2005 Red Hat, Inc.
+ * 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 _SEMANAGE_CONTEXT_INTERNAL_H_
#define _SEMANAGE_CONTEXT_INTERNAL_H_
+#include <inttypes.h>
#include <semanage/context_record.h>
#include "dso.h"
@@ -8,4 +31,19 @@ hidden_proto(semanage_context_clone)
hidden_proto(semanage_context_free)
hidden_proto(semanage_context_from_string)
hidden_proto(semanage_context_to_string)
+
+/*======== Internal API: Serialize/Unserialize ========== */
+hidden int semanage_context_calculate_serialized_size(semanage_handle_t *
+ handle,
+ const semanage_context_t *
+ context, uint64_t * size);
+
+hidden int semanage_context_serialize(semanage_handle_t * handle,
+ const semanage_context_t * context,
+ char **data, uint64_t * size);
+
+hidden int semanage_context_unserialize(semanage_handle_t * handle,
+ char **data, uint64_t * size,
+ semanage_context_t ** context);
+
#endif
Index: selinux-pms-support/libsemanage/src/context_record.c
===================================================================
--- selinux-pms-support.orig/libsemanage/src/context_record.c
+++ selinux-pms-support/libsemanage/src/context_record.c
@@ -1,12 +1,35 @@
-/* Copyright (C) 2005 Red Hat, Inc. */
+/* Authors: Ivan Gyurdiev <ivg2@cornell.edu>
+ * Christopher Ashworth <cashworth@tresys.com>
+ * Caleb Case <ccase@tresys.com>
+ *
+ * Copyright (C) 2005 Red Hat, Inc.
+ * 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
+ */
+#include <stdlib.h>
+#include <string.h>
#include <sepol/context_record.h>
-#include "handle.h"
typedef sepol_context_t semanage_context_t;
#define _SEMANAGE_CONTEXT_DEFINED_
#include "context_internal.h"
+#include "handle.h"
+#include "debug.h"
/* User */
const char *semanage_context_get_user(const semanage_context_t * con)
@@ -90,6 +113,27 @@ void semanage_context_free(semanage_cont
hidden_def(semanage_context_free)
+/* Serialize/Unserialize */
+/** Destructively modifies data and size.
+ * Caller must pre-allocate space for data.
+ * Use semanage_context_calculate_serialized_size(). */
+hidden int semanage_context_serialize(semanage_handle_t * handle,
+ const semanage_context_t * context,
+ char **data, uint64_t * size)
+{
+ return sepol_context_serialize(handle->sepolh, context, data, size);
+}
+
+/** Destructively modifies context, data and size.
+ * Allocates space for context.
+ * Caller must free. */
+hidden int semanage_context_unserialize(semanage_handle_t * handle,
+ char **data, uint64_t * size,
+ semanage_context_t ** context)
+{
+ return sepol_context_unserialize(handle->sepolh, data, size, context);
+}
+
/* Parse to/from string */
int semanage_context_from_string(semanage_handle_t * handle,
const char *str, semanage_context_t ** con)
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
@@ -22,6 +22,7 @@
#include "test_semanage_store.h"
#include "test_boolean_record.h"
+#include "test_context_record.h"
#include <CUnit/Basic.h>
#include <CUnit/Console.h>
@@ -59,6 +60,7 @@ static int do_tests(int interactive, int
DECLARE_SUITE(semanage_store);
DECLARE_SUITE(boolean_record);
+ DECLARE_SUITE(context_record);
if (verbose)
CU_basic_set_mode(CU_BRM_VERBOSE);
Index: selinux-pms-support/libsemanage/tests/test_context_record.c
===================================================================
--- /dev/null
+++ selinux-pms-support/libsemanage/tests/test_context_record.c
@@ -0,0 +1,154 @@
+/* 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/semanage_store.c
+ *
+ */
+
+#include "context_internal.h"
+#include "context_record.h"
+
+#include "globals.h"
+#include "utilities.h"
+#include "test_context_record.h"
+
+#include <libgen.h>
+#include <limits.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <CUnit/Basic.h>
+
+/* The suite initialization function.
+ * Returns zero on success, non-zero otherwise.
+ */
+int context_record_test_init(void)
+{
+ /* initialize the handle */
+ sh = semanage_handle_create();
+ if (sh == NULL)
+ return -1;
+
+ /* hide error messages */
+ sh->msg_callback = test_msg_handler;
+
+ return 0;
+}
+
+/* The suite cleanup function.
+ * Returns zero on success, non-zero otherwise.
+ */
+int context_record_test_cleanup(void)
+{
+ semanage_handle_destroy(sh);
+ return 0;
+}
+
+/* Adds all the tests needed for this suite.
+ */
+int context_record_add_tests(CU_pSuite suite)
+{
+ if (NULL ==
+ CU_add_test(suite, "semanage_context_serialize",
+ test_semanage_context_serialize)) {
+ CU_cleanup_registry();
+ return CU_get_error();
+ }
+
+ return 0;
+}
+
+/* Tests the semanage_context_serialize function in context_record.c
+ */
+void test_semanage_context_serialize(void)
+{
+ int status;
+ char *data;
+ uint64_t data_length = 0;
+
+ /* serialize */
+
+ semanage_context_t *context;
+ status = semanage_context_create(sh, &context);
+
+ /* setup test fields */
+ const char *user = "testuser";
+ const char *role = "testrole";
+ const char *type = "testtype";
+
+ status = semanage_context_set_user(sh, context, user);
+ CU_ASSERT(status == 0);
+ status = semanage_context_set_role(sh, context, role);
+ CU_ASSERT(status == 0);
+ status = semanage_context_set_type(sh, context, type);
+ CU_ASSERT(status == 0);
+
+ /* set aside enough space... */
+ status = semanage_context_serialize(sh, context, NULL, &data_length);
+ CU_ASSERT(status == 0);
+ data = calloc(data_length, sizeof(char));
+
+ char *data2;
+ data2 = data;
+ status = semanage_context_serialize(sh, context, &data2, NULL);
+ CU_ASSERT(status == 0);
+ /* iterator/destructive effect check */
+ CU_ASSERT((unsigned)(data2 - data) == data_length);
+
+ /* unserialize */
+ semanage_context_t *context2;
+ data2 = data;
+ uint64_t data_length2 = data_length;
+ status =
+ semanage_context_unserialize(sh, &data2, &data_length2, &context2);
+ CU_ASSERT(status == 0);
+ /* iterator/destructive effect check */
+ CU_ASSERT((unsigned)(data2 - data) == data_length);
+ /* unserialize should create space */
+ CU_ASSERT(context2 != NULL);
+
+ /* get results */
+ const char *user2;
+ const char *role2;
+ const char *type2;
+
+ user2 = semanage_context_get_user(context2);
+ role2 = semanage_context_get_role(context2);
+ type2 = semanage_context_get_type(context2);
+
+ /* compare */
+ status = strcmp(user, user2);
+ CU_ASSERT(status == 0);
+
+ status = strcmp(role, role2);
+ CU_ASSERT(status == 0);
+
+ status = strcmp(type, type2);
+ CU_ASSERT(status == 0);
+
+ /* cleanup */
+ semanage_context_free(context);
+ semanage_context_free(context2);
+ free(data);
+
+}
Index: selinux-pms-support/libsemanage/tests/test_context_record.h
===================================================================
--- /dev/null
+++ selinux-pms-support/libsemanage/tests/test_context_record.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_CONTEXT_RECORD_H__
+#define __TEST_CONTEXT_RECORD_H__
+
+#include <CUnit/Basic.h>
+
+int context_record_test_init(void);
+int context_record_test_cleanup(void);
+int context_record_add_tests(CU_pSuite suite);
+
+void test_semanage_context_serialize(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.
next prev parent reply other threads:[~2007-04-24 18:30 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 ` jbrindle [this message]
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 ` [PATCH 30/33] libsemanage: node " jbrindle
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=20070423213735.016593000@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.