#2 Replace 2 of libsemanage tests with one sanity test
Closed 6 years ago by plautrba. Opened 6 years ago by plautrba.
tests/ plautrba/selinux jzarsky-sanity-tests  into  master

libsemanage/sanity-tests/Makefile libsemanage/semanage-handle-functions/Makefile
file renamed
+7 -7
@@ -1,7 +1,7 @@ 

  # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

  #

- #   Makefile of /CoreOS/libsemanage/Sanity/semanage-handle-functions

- #   Description: Test functions from handle.h

+ #   Makefile of /CoreOS/libsemanage/Sanity/sanity-tests

+ #   Description: Test libsemanage functions

  #   Author: Jan Zarsky <jzarsky@redhat.com>

  #

  # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -23,12 +23,12 @@ 

  #

  # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

  

- export TEST=/CoreOS/libsemanage/Sanity/semanage-handle-functions

+ export TEST=/CoreOS/libsemanage/Sanity/sanity-tests

  export TESTVERSION=1.0

  

  BUILT_FILES=

  

- FILES=$(METADATA) runtest.sh Makefile PURPOSE functions.c test_*.c

+ FILES=$(METADATA) runtest.sh Makefile PURPOSE tests/

  

  .PHONY: all install download clean

  
@@ -49,11 +49,11 @@ 

  	@echo "Name:            $(TEST)" >> $(METADATA)

  	@echo "TestVersion:     $(TESTVERSION)" >> $(METADATA)

  	@echo "Path:            $(TEST_DIR)" >> $(METADATA)

- 	@echo "Description:     Test functions from handle.h" >> $(METADATA)

+ 	@echo "Description:     Test libsemanage functions" >> $(METADATA)

  	@echo "Type:            Sanity" >> $(METADATA)

- 	@echo "TestTime:        5m" >> $(METADATA)

+ 	@echo "TestTime:        60m" >> $(METADATA)

  	@echo "RunFor:          libsemanage" >> $(METADATA)

- 	@echo "Requires:        libsemanage libsemanage-devel glibc gcc" >> $(METADATA)

+ 	@echo "Requires:        libsemanage libsemanage-devel gcc CUnit CUnit-devel checkpolicy policycoreutils-python3" >> $(METADATA)

  	@echo "Priority:        Normal" >> $(METADATA)

  	@echo "License:         GPLv2+" >> $(METADATA)

  	@echo "Confidential:    no" >> $(METADATA)

@@ -0,0 +1,3 @@ 

+ PURPOSE of /CoreOS/libsemanage/Sanity/sanity-tests

+ Description: Test libsemanage functions

+ Author: Jan Zarsky <jzarsky@redhat.com>

@@ -0,0 +1,71 @@ 

+ #!/bin/bash

+ # vim: dict+=/usr/share/beakerlib/dictionary.vim cpt=.,w,b,u,t,i,k

+ # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

+ #

+ #   runtest.sh of /CoreOS/libsemanage/Sanity/sanity-tests

+ #   Description: Test libsemanage functions

+ #   Author: Jan Zarsky <jzarsky@redhat.com>

+ #

+ # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

+ #

+ #   Copyright (c) 2017 Red Hat, Inc.

+ #

+ #   This program is free software: you can redistribute it and/or

+ #   modify it under the terms of the GNU General Public License as

+ #   published by the Free Software Foundation, either version 2 of

+ #   the License, or (at your option) any later version.

+ #

+ #   This program 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 General Public License for more details.

+ #

+ #   You should have received a copy of the GNU General Public License

+ #   along with this program. If not, see http://www.gnu.org/licenses/.

+ #

+ # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

+ 

+ # Include Beaker environment

+ . /usr/bin/rhts-environment.sh || exit 1

+ . /usr/share/beakerlib/beakerlib.sh || exit 1

+ 

+ PACKAGE="libsemanage"

+ 

+ rlJournalStart

+     rlPhaseStartSetup

+         rlRun "rlImport --all" || rlDie

+ 

+         rlRun "rlCheckMakefileRequires"

+ 

+         VERS=$(rpm -q $PACKAGE | cut -f 2 -d '-')

+         VERS_MAJOR=$(echo $VERS | cut -f 1 -d '.')

+         VERS_MINOR=$(echo $VERS | cut -f 2 -d '.')

+         VERS_RELEASE=$(rpm -q $PACKAGE | cut -f 3 -d '-' | cut -f 1 -d '.')

+ 

+         rlRun "cd tests"

+ 

+         rlRun "make VERS_MAJOR=$VERS_MAJOR VERS_MINOR=$VERS_MINOR VERS_RELEASE=$VERS_RELEASE"

+ 

+         rlRun "./libsemanage-tests list > list"

+     rlPhaseEnd

+ 

+     for suite in $(cat list); do

+         rlPhaseStartTest "$suite"

+             rlRun "./libsemanage-tests suite $suite >rawoutput 2>erroutput" 0 "Run test suite $suite"

+ 

+             rlRun "cat rawoutput | sed 1,5d" 0 "stdout"

+             rlRun "cat erroutput" 0 "stderr"

+ 

+             rlRun "grep -v 'Run Summary:' rawoutput > output"

+ 

+             rlRun "! grep -i failed output" 0 "Fails"

+             rlRun "! grep -i error output" 0 "Errors"

+         rlPhaseEnd

+     done;

+ 

+     rlPhaseStartCleanup

+         rlRun "make clean"

+         rlRun "rm -f output rawoutput erroutput list"

+     rlPhaseEnd

+ rlJournalPrintText

+ rlJournalEnd

@@ -0,0 +1,1 @@ 

+ libsemanage-tests

@@ -0,0 +1,35 @@ 

+ SOURCES = $(wildcard *.c)

+ 

+ ###########################################################################

+ 

+ EXECUTABLE = libsemanage-tests

+ CFLAGS += -g -Wall -Werror -std=gnu99

+ LDFLAGS += -lcunit -lsemanage -lselinux

+ OBJECTS = $(SOURCES:.c=.o) 

+ 

+ ifdef VERS_MAJOR

+ DEFINES += -D VERS_MAJOR=$(VERS_MAJOR)

+ endif

+ 

+ ifdef VERS_MINOR

+ DEFINES += -D VERS_MINOR=$(VERS_MINOR)

+ endif

+ 

+ ifdef VERS_RELEASE

+ DEFINES += -D VERS_RELEASE=$(VERS_RELEASE)

+ endif

+ 

+ all: $(EXECUTABLE) 

+ 

+ $(EXECUTABLE): $(OBJECTS)

+ 	$(CC) $(OBJECTS) $(LIBS) $(LDFLAGS) -o $@

+ 

+ %.o: %.c

+ 	$(CC) $(CFLAGS) $(DEFINES) -c $*.c -o $*.o

+ 

+ clean distclean: 

+ 	rm -rf $(OBJECTS) $(EXECUTABLE)

+ 

+ test: all 

+ 	./$(EXECUTABLE)

+ 

@@ -0,0 +1,771 @@ 

+ #include <sys/types.h>

+ #include <sys/stat.h>

+ #include <sys/wait.h>

+ #include <unistd.h>

+ #include <stdlib.h>

+ #include <stdarg.h>

+ 

+ #include <semanage/handle.h>

+ #include <semanage/semanage.h>

+ 

+ #include <CUnit/Basic.h>

+ 

+ #include "functions.h"

+ 

+ int result;

+ 

+ semanage_handle_t *sh = NULL;

+ 

+ void stderr_callback(void *varg, semanage_handle_t *sh, const char *fmt, ...) {

+     va_list args;

+     va_start(args, fmt);

+ 

+     vfprintf(stderr, fmt, args);

+ 

+     fprintf(stderr, "\n");

+ 

+     va_end(args);

+ }

+ 

+ void helper_handle_create(void) {

+     sh = semanage_handle_create();

+     

+     CU_ASSERT_PTR_NOT_NULL(sh);

+ 

+     semanage_msg_set_callback(sh, stderr_callback, NULL);

+ }

+ 

+ void helper_handle_destroy(void) {

+     semanage_handle_destroy(sh);

+ }

+ 

+ void helper_connect(void) {

+     CU_ASSERT(semanage_connect(sh) >= 0);

+ }

+ 

+ void helper_disconnect(void) {

+     CU_ASSERT(semanage_disconnect(sh) >= 0);

+ }

+ 

+ void helper_begin_transaction(void) {

+     CU_ASSERT(semanage_begin_transaction(sh) >= 0);

+ }

+ 

+ void helper_commit(void) {

+     CU_ASSERT(semanage_commit(sh) >= 0);

+ }

+ 

+ void setup_handle(level_t level) {

+     if (level >= SH_NULL)

+         sh = NULL;

+ 

+     if (level >= SH_HANDLE)

+         helper_handle_create();

+ 

+     if (level >= SH_CONNECT)

+         helper_connect();

+ 

+     if (level >= SH_TRANS)

+         helper_begin_transaction();

+ }

+ 

+ void cleanup_handle(level_t level) {

+     if (level >= SH_TRANS)

+         helper_commit();

+ 

+     if (level >= SH_CONNECT)

+         helper_disconnect();

+ 

+     if (level >= SH_HANDLE)

+         helper_handle_destroy();

+ 

+     if (level >= SH_NULL)

+         sh = NULL;

+ }

+ 

+ void setup_handle_invalid_store(level_t level) {

+     CU_ASSERT(level >= SH_HANDLE);

+ 

+     helper_handle_create();

+     

+     semanage_select_store(sh, "", SEMANAGE_CON_INVALID);

+ 

+     if (level >= SH_CONNECT)

+         helper_connect();

+ 

+     if (level >= SH_TRANS)

+         helper_begin_transaction();

+ }

+ 

+ int context_compare(semanage_context_t *con, const char *str) {

+     char *con_str = NULL;

+     int rc;

+ 

+     CU_ASSERT_PTR_NOT_NULL_FATAL(con);

+ 

+     CU_ASSERT(semanage_context_to_string(sh, con, &con_str) >= 0);

+     CU_ASSERT_PTR_NOT_NULL_FATAL(con_str);

+ 

+     rc = strcmp(con_str, str);

+ 

+     free(con_str);

+     return rc;

+ }

+ 

+ semanage_seuser_t *get_seuser_nth(int index) {

+     int result;

+     semanage_seuser_t **records;

+     semanage_seuser_t *seuser;

+     unsigned int count;

+ 

+     if (index == I_NULL)

+         return NULL;

+ 

+     result = semanage_seuser_list(sh, &records, &count);

+     

+     CU_ASSERT_FATAL(result >= 0);

+     CU_ASSERT_FATAL(count >= index + 1);

+ 

+     seuser = records[index];

+ 

+     for (unsigned int i = 0; i < count; i++) {

+         if (i != index)

+             semanage_seuser_free(records[i]);

+     }

+ 

+     return seuser;

+ }

+ 

+ semanage_seuser_t *get_seuser_new() {

+     int result;

+     semanage_seuser_t *seuser;

+ 

+     result = semanage_seuser_create(sh, &seuser);

+     

+     CU_ASSERT_FATAL(result >= 0);

+ 

+     return seuser;

+ }

+ 

+ semanage_seuser_key_t *get_seuser_key_nth(int index) {

+     semanage_seuser_key_t *key;

+     semanage_seuser_t *seuser;

+     int result;

+ 

+     if (index == I_NULL)

+         return NULL;

+ 

+     seuser = get_seuser_nth(index);

+ 

+     result = semanage_seuser_key_extract(sh, seuser, &key);

+ 

+     CU_ASSERT_FATAL(result >= 0);

+     CU_ASSERT_PTR_NOT_NULL_FATAL(key);

+ 

+     return key;

+ }

+ 

+ semanage_seuser_key_t *get_seuser_key_from_str(const char *str) {

+     semanage_seuser_key_t *key;

+     int result;

+ 

+     if (str == NULL)

+         return NULL;

+ 

+     result = semanage_seuser_key_create(sh, str, &key);

+ 

+     CU_ASSERT_FATAL(result >= 0);

+     CU_ASSERT_PTR_NOT_NULL_FATAL(key);

+ 

+     return key;

+ }

+ 

+ void add_local_seuser(int seuser_index) {

+     semanage_seuser_t *seuser;

+     semanage_seuser_key_t *key = NULL;

+ 

+     CU_ASSERT_FATAL(seuser_index != I_NULL);

+ 

+     seuser = get_seuser_nth(seuser_index);

+ 

+     CU_ASSERT_FATAL(semanage_seuser_key_extract(sh, seuser, &key) >= 0);

+     CU_ASSERT_PTR_NOT_NULL_FATAL(key);

+ 

+     CU_ASSERT_FATAL(semanage_seuser_modify_local(sh, key, seuser) >= 0);

+ 

+     semanage_seuser_key_free(key);

+     semanage_seuser_free(seuser);

+ }

+ 

+ void delete_local_seuser(int seuser_index) {

+     semanage_seuser_key_t *key = NULL;

+ 

+     CU_ASSERT_FATAL(seuser_index != I_NULL);

+ 

+     key = get_seuser_key_nth(seuser_index);

+ 

+     CU_ASSERT_FATAL(semanage_seuser_del_local(sh, key) >= 0);

+ 

+     semanage_seuser_key_free(key);

+ }

+ 

+ semanage_bool_t *get_bool_nth(int index) {

+     int result;

+     semanage_bool_t **records;

+     semanage_bool_t *boolean;

+     unsigned int count;

+ 

+     if (index == I_NULL)

+         return NULL;

+ 

+     result = semanage_bool_list(sh, &records, &count);

+     

+     CU_ASSERT_FATAL(result >= 0);

+     CU_ASSERT_FATAL(count >= index + 1);

+ 

+     boolean = records[index];

+ 

+     for (unsigned int i = 0; i < count; i++) {

+         if (i != index)

+             semanage_bool_free(records[i]);

+     }

+ 

+     return boolean;

+ }

+ 

+ semanage_bool_t *get_bool_new() {

+     int result;

+     semanage_bool_t *boolean;

+ 

+     result = semanage_bool_create(sh, &boolean);

+     

+     CU_ASSERT_FATAL(result >= 0);

+ 

+     return boolean;

+ }

+ 

+ semanage_bool_key_t *get_bool_key_nth(int index) {

+     semanage_bool_key_t *key;

+     semanage_bool_t *boolean;

+     int result;

+ 

+     if (index == I_NULL)

+         return NULL;

+ 

+     boolean = get_bool_nth(index);

+ 

+     result = semanage_bool_key_extract(sh, boolean, &key);

+ 

+     CU_ASSERT_FATAL(result >= 0);

+     CU_ASSERT_PTR_NOT_NULL_FATAL(key);

+ 

+     return key;

+ }

+ 

+ semanage_bool_key_t *get_bool_key_from_str(const char *str) {

+     semanage_bool_key_t *key;

+     int result;

+ 

+     if (str == NULL)

+         return NULL;

+ 

+     result = semanage_bool_key_create(sh, str, &key);

+ 

+     CU_ASSERT_FATAL(result >= 0);

+     CU_ASSERT_PTR_NOT_NULL_FATAL(key);

+ 

+     return key;

+ }

+ 

+ void add_local_bool(int bool_index) {

+     semanage_bool_t *boolean;

+     semanage_bool_key_t *key = NULL;

+ 

+     CU_ASSERT_FATAL(bool_index != I_NULL);

+ 

+     boolean = get_bool_nth(bool_index);

+ 

+     CU_ASSERT_FATAL(semanage_bool_key_extract(sh, boolean, &key) >= 0);

+     CU_ASSERT_PTR_NOT_NULL_FATAL(key);

+ 

+     CU_ASSERT_FATAL(semanage_bool_modify_local(sh, key, boolean) >= 0);

+ }

+ 

+ void delete_local_bool(int bool_index) {

+     semanage_bool_key_t *key = NULL;

+ 

+     CU_ASSERT_FATAL(bool_index != I_NULL);

+ 

+     key = get_bool_key_nth(bool_index);

+ 

+     CU_ASSERT_FATAL(semanage_bool_del_local(sh, key) >= 0);

+ }

+ 

+ semanage_fcontext_t *get_fcontext_new() {

+     int result;

+     semanage_fcontext_t *fcontext;

+ 

+     result = semanage_fcontext_create(sh, &fcontext);

+     

+     CU_ASSERT_FATAL(result >= 0);

+ 

+     return fcontext;

+ }

+ 

+ semanage_fcontext_t *get_fcontext_nth(int index) {

+     int result;

+     semanage_fcontext_t **records;

+     semanage_fcontext_t *fcontext;

+     unsigned int count;

+ 

+     if (index == I_NULL)

+         return NULL;

+ 

+     result = semanage_fcontext_list(sh, &records, &count);

+     

+     CU_ASSERT_FATAL(result >= 0);

+     CU_ASSERT_FATAL(count >= index + 1);

+ 

+     fcontext = records[index];

+ 

+     for (unsigned int i = 0; i < count; i++) {

+         if (i != index)

+             semanage_fcontext_free(records[i]);

+     }

+ 

+     return fcontext;

+ }

+ 

+ semanage_fcontext_key_t *get_fcontext_key_nth(int index) {

+     semanage_fcontext_key_t *key;

+     semanage_fcontext_t *fcontext;

+     int result;

+ 

+     if (index == I_NULL)

+         return NULL;

+ 

+     fcontext = get_fcontext_nth(index);

+ 

+     result = semanage_fcontext_key_extract(sh, fcontext, &key);

+ 

+     CU_ASSERT_FATAL(result >= 0);

+     CU_ASSERT_PTR_NOT_NULL_FATAL(key);

+ 

+     return key;

+ }

+ 

+ semanage_fcontext_key_t *get_fcontext_key_from_str(const char *str, int type) {

+     semanage_fcontext_key_t *key;

+     int result;

+ 

+     if (str == NULL)

+         return NULL;

+ 

+     result = semanage_fcontext_key_create(sh, str, type, &key);

+ 

+     CU_ASSERT_FATAL(result >= 0);

+     CU_ASSERT_PTR_NOT_NULL_FATAL(key);

+ 

+     return key;

+ }

+ 

+ void add_local_fcontext(int fcontext_index) {

+     semanage_fcontext_t *fcontext;

+     semanage_fcontext_key_t *key = NULL;

+ 

+     CU_ASSERT_FATAL(fcontext_index != I_NULL);

+ 

+     fcontext = get_fcontext_nth(fcontext_index);

+ 

+     CU_ASSERT_FATAL(semanage_fcontext_key_extract(sh, fcontext, &key) >= 0);

+     CU_ASSERT_PTR_NOT_NULL_FATAL(key);

+ 

+     CU_ASSERT_FATAL(semanage_fcontext_modify_local(sh, key, fcontext) >= 0);

+ }

+ 

+ void delete_local_fcontext(int fcontext_index) {

+     semanage_fcontext_key_t *key = NULL;

+ 

+     CU_ASSERT_FATAL(fcontext_index != I_NULL);

+ 

+     key = get_fcontext_key_nth(fcontext_index);

+ 

+     CU_ASSERT_FATAL(semanage_fcontext_del_local(sh, key) >= 0);

+ }

+ 

+ #if VERS_CHECK(2, 5)

+ 

+ semanage_module_info_t *get_module_info_nth(int index, semanage_module_info_t **modinfo_free) {

+     int result;

+     semanage_module_info_t *records = NULL;

+     semanage_module_info_t *modinfo;

+     int count;

+ 

+     if (index == I_NULL)

+         return NULL;

+ 

+     result = semanage_module_list_all(sh, &records, &count);

+     

+     CU_ASSERT_FATAL(result >= 0);

+     CU_ASSERT_FATAL(records != NULL);

+     CU_ASSERT_FATAL(count >= index + 1);

+ 

+     modinfo = semanage_module_list_nth(records, index);

+ 

+     CU_ASSERT_FATAL(modinfo != NULL);

+ 

+     for (unsigned int i = 0; i < count; i++) {

+         if (i != index) {

+             semanage_module_info_t *tmp = semanage_module_list_nth(records, i);

+             semanage_module_info_destroy(sh, tmp);

+         }

+     }

+ 

+     *modinfo_free = records;

+ 

+     return modinfo;

+ }

+ 

+ semanage_module_key_t *get_module_key_nth(int index) {

+     semanage_module_key_t *modkey = NULL;

+     semanage_module_info_t *modinfo = NULL;

+     semanage_module_info_t *modinfo_free = NULL;

+     const char *name = NULL;

+     uint16_t priority = 0;

+ 

+     CU_ASSERT(semanage_module_key_create(sh, &modkey) >= 0);

+ 

+     modinfo = get_module_info_nth(index, &modinfo_free);

+ 

+     CU_ASSERT(semanage_module_info_get_name(sh, modinfo, &name) >= 0);

+     CU_ASSERT(semanage_module_key_set_name(sh, modkey, name) >= 0);

+ 

+     CU_ASSERT(semanage_module_info_get_priority(sh, modinfo, &priority) >= 0);

+     CU_ASSERT(semanage_module_key_set_priority(sh, modkey, priority) >= 0);

+ 

+     free(modinfo_free);

+ 

+     return modkey;

+ }

+ 

+ int module_exists(const char *name) {

+     const char *bools_path = "/sys/fs/selinux/booleans/";

+     char *path = NULL;

+     struct stat buf;

+ 

+     path = malloc(strlen(bools_path) + strlen(name) + 1);

+ 

+     CU_ASSERT_PTR_NOT_NULL(path);

+ 

+     strcpy(path, bools_path);

+     strcat(path, name);

+ 

+     if (stat(path, &buf) != 0) {

+         CU_ASSERT(errno == ENOENT);

+ 

+         return 0;

+     }

+     else {

+         return 1;

+     }

+ }

+ 

+ #endif

+ 

+ semanage_port_t *get_port_nth(int index) {

+     int result;

+     semanage_port_t **records;

+     semanage_port_t *port;

+     unsigned int count;

+ 

+     if (index == I_NULL)

+         return NULL;

+ 

+     result = semanage_port_list(sh, &records, &count);

+     

+     CU_ASSERT_FATAL(result >= 0);

+     CU_ASSERT_FATAL(count >= index + 1);

+ 

+     port = records[index];

+ 

+     for (unsigned int i = 0; i < count; i++) {

+         if (i != index)

+             semanage_port_free(records[i]);

+     }

+ 

+     return port;

+ }

+ 

+ semanage_port_key_t *get_port_key_nth(int index) {

+     semanage_port_key_t *key;

+     semanage_port_t *port;

+     int result;

+ 

+     if (index == I_NULL)

+         return NULL;

+ 

+     port = get_port_nth(index);

+ 

+     result = semanage_port_key_extract(sh, port, &key);

+ 

+     CU_ASSERT_FATAL(result >= 0);

+     CU_ASSERT_PTR_NOT_NULL_FATAL(key);

+ 

+     return key;

+ }

+ 

+ void add_local_port(int port_index) {

+     semanage_port_t *port;

+     semanage_port_key_t *key = NULL;

+ 

+     CU_ASSERT_FATAL(port_index != I_NULL);

+ 

+     port = get_port_nth(port_index);

+ 

+     CU_ASSERT_FATAL(semanage_port_key_extract(sh, port, &key) >= 0);

+     CU_ASSERT_PTR_NOT_NULL_FATAL(key);

+ 

+     CU_ASSERT_FATAL(semanage_port_modify_local(sh, key, port) >= 0);

+ }

+ 

+ void delete_local_port(int port_index) {

+     semanage_port_key_t *key = NULL;

+ 

+     CU_ASSERT_FATAL(port_index != I_NULL);

+ 

+     key = get_port_key_nth(port_index);

+ 

+     CU_ASSERT_FATAL(semanage_port_del_local(sh, key) >= 0);

+ }

+ 

+ semanage_iface_t *get_iface() {

+     semanage_iface_t *iface = NULL;

+     semanage_context_t *ifcon = NULL;

+     semanage_context_t *msgcon = NULL;

+ 

+     CU_ASSERT_FATAL(semanage_iface_create(sh, &iface) >= 0);

+ 

+     CU_ASSERT_FATAL(semanage_iface_set_name(sh, iface, "asdf") >= 0);

+ 

+     CU_ASSERT_FATAL(semanage_context_from_string(sh, "system_u:object_r:tmp_t:s0", &ifcon) >= 0);

+     CU_ASSERT_FATAL(semanage_iface_set_ifcon(sh, iface, ifcon) >= 0);

+ 

+     CU_ASSERT_FATAL(semanage_context_from_string(sh, "system_u:object_r:tmp_t:s0", &msgcon) >= 0);

+     CU_ASSERT_FATAL(semanage_iface_set_msgcon(sh, iface, msgcon) >= 0);

+ 

+     return iface;

+ }

+ 

+ semanage_iface_key_t *get_iface_key() {

+     semanage_iface_key_t *key;

+ 

+     CU_ASSERT_FATAL(semanage_iface_key_create(sh, "asdf", &key) >= 0);

+ 

+     return key;

+ }

+ 

+ void add_local_iface(int index) {

+     semanage_iface_t *iface;

+     semanage_iface_key_t *key = NULL;

+     char name[50] = {};

+ 

+     snprintf(name, 50, "asdf%d", index);

+ 

+     iface = get_iface();

+ 

+     CU_ASSERT_FATAL(semanage_iface_set_name(sh, iface, name) >= 0);

+ 

+     CU_ASSERT_FATAL(semanage_iface_key_extract(sh, iface, &key) >= 0);

+     CU_ASSERT_PTR_NOT_NULL_FATAL(key);

+ 

+     CU_ASSERT_FATAL(semanage_iface_modify_local(sh, key, iface) >= 0);

+ }

+ 

+ void delete_local_iface(int index) {

+     semanage_iface_key_t *key = NULL;

+     char name[50] = {};

+ 

+     snprintf(name, 50, "asdf%d", index);

+ 

+     CU_ASSERT_FATAL(semanage_iface_key_create(sh, name, &key) >= 0);

+ 

+     CU_ASSERT_FATAL(semanage_iface_del_local(sh, key) >= 0);

+ }

+ 

+ semanage_node_t *get_node() {

+     semanage_node_t *node = NULL;

+     semanage_context_t *con = NULL;

+ 

+     CU_ASSERT_FATAL(semanage_node_create(sh, &node) >= 0);

+ 

+     CU_ASSERT_FATAL(semanage_node_set_addr(sh, node, SEMANAGE_PROTO_IP4,

+                                            "127.0.0.1") >= 0);

+ 

+     CU_ASSERT_FATAL(semanage_node_set_mask(sh, node, SEMANAGE_PROTO_IP4,

+                                            "255.255.255.0") >= 0);

+ 

+     semanage_node_set_proto(node, SEMANAGE_PROTO_IP4);

+ 

+     CU_ASSERT_FATAL(semanage_context_from_string(sh,

+                                       "system_u:object_r:tmp_t:s0", &con) >= 0);

+     CU_ASSERT_FATAL(semanage_node_set_con(sh, node, con) >= 0);

+ 

+     return node;

+ }

+ 

+ semanage_node_key_t *get_node_key() {

+     semanage_node_key_t *key;

+ 

+     CU_ASSERT_FATAL(semanage_node_key_create(sh, "127.0.0.1", "255.255.255.0",

+                                              SEMANAGE_PROTO_IP4, &key) >= 0);

+ 

+     return key;

+ }

+ 

+ void add_local_node(int index) {

+     semanage_node_t *node;

+     semanage_node_key_t *key = NULL;

+     char addr[50] = {};

+ 

+     CU_ASSERT(index >= 0 && index <= 255);

+ 

+     snprintf(addr, 50, "192.168.0.%d", index);

+ 

+     node = get_node();

+ 

+     CU_ASSERT_FATAL(semanage_node_set_addr(sh, node, SEMANAGE_PROTO_IP4,

+                                            addr) >= 0);

+     CU_ASSERT_FATAL(semanage_node_set_mask(sh, node, SEMANAGE_PROTO_IP4,

+                                            "255.255.0.0") >= 0);

+ 

+     CU_ASSERT_FATAL(semanage_node_key_extract(sh, node, &key) >= 0);

+     CU_ASSERT_PTR_NOT_NULL_FATAL(key);

+ 

+     CU_ASSERT_FATAL(semanage_node_modify_local(sh, key, node) >= 0);

+ }

+ 

+ void delete_local_node(int index) {

+     semanage_node_key_t *key = NULL;

+     char addr[50] = {};

+ 

+     CU_ASSERT(index >= 0 && index <= 255);

+ 

+     snprintf(addr, 50, "192.168.0.%d", index);

+ 

+     CU_ASSERT_FATAL(semanage_node_key_create(sh, addr, "255.255.0.0",

+                                              SEMANAGE_PROTO_IP4, &key) >= 0);

+ 

+     CU_ASSERT_FATAL(semanage_node_del_local(sh, key) >= 0);

+ }

+ 

+ semanage_user_t *get_user_nth(int index) {

+     int result;

+     semanage_user_t **records;

+     semanage_user_t *user;

+     unsigned int count;

+ 

+     if (index == I_NULL)

+         return NULL;

+ 

+     result = semanage_user_list(sh, &records, &count);

+     

+     CU_ASSERT_FATAL(result >= 0);

+     CU_ASSERT_FATAL(count >= index + 1);

+ 

+     user = records[index];

+ 

+     for (unsigned int i = 0; i < count; i++) {

+         if (i != index)

+             semanage_user_free(records[i]);

+     }

+ 

+     return user;

+ }

+ 

+ semanage_user_key_t *get_user_key_nth(int index) {

+     semanage_user_key_t *key;

+     semanage_user_t *user;

+     int result;

+ 

+     if (index == I_NULL)

+         return NULL;

+ 

+     user = get_user_nth(index);

+ 

+     result = semanage_user_key_extract(sh, user, &key);

+ 

+     CU_ASSERT_FATAL(result >= 0);

+     CU_ASSERT_PTR_NOT_NULL_FATAL(key);

+ 

+     return key;

+ }

+ 

+ void add_local_user(int user_index) {

+     semanage_user_t *user;

+     semanage_user_key_t *key = NULL;

+ 

+     CU_ASSERT_FATAL(user_index != I_NULL);

+ 

+     user = get_user_nth(user_index);

+ 

+     CU_ASSERT_FATAL(semanage_user_key_extract(sh, user, &key) >= 0);

+     CU_ASSERT_PTR_NOT_NULL_FATAL(key);

+ 

+     CU_ASSERT_FATAL(semanage_user_modify_local(sh, key, user) >= 0);

+ }

+ 

+ void delete_local_user(int user_index) {

+     semanage_user_key_t *key = NULL;

+ 

+     CU_ASSERT_FATAL(user_index != I_NULL);

+ 

+     key = get_user_key_nth(user_index);

+ 

+     CU_ASSERT_FATAL(semanage_user_del_local(sh, key) >= 0);

+ }

+ 

+ #ifdef FEATURE_INFINIBAND

+ 

+ void add_local_ibendport(int index) {

+     semanage_ibendport_t *ibendport;

+     semanage_ibendport_key_t *key = NULL;

+     semanage_context_t *con = NULL;

+     char name[50];

+ 

+     CU_ASSERT(index >= 0 && index <= 255);

+ 

+     snprintf(name, 50, "ibendport%d", index);

+ 

+     CU_ASSERT_FATAL(semanage_ibendport_key_create(sh, name, 0, &key) >= 0);

+ 

+     CU_ASSERT_FATAL(semanage_ibendport_create(sh, &ibendport) >= 0);

+     CU_ASSERT_FATAL(semanage_ibendport_set_ibdev_name(sh, ibendport,

+                                                       name) >= 0);

+     semanage_ibendport_set_port(ibendport, 0);

+     CU_ASSERT_FATAL(semanage_context_from_string(sh,

+                                       "system_u:object_r:bin_t:s0", &con) >= 0);

+     CU_ASSERT_FATAL(semanage_ibendport_set_con(sh, ibendport, con) >= 0);

+ 

+     CU_ASSERT_FATAL(semanage_ibendport_modify_local(sh, key, ibendport) >= 0);

+ 

+     semanage_context_free(con);

+     semanage_ibendport_key_free(key);

+     semanage_ibendport_free(ibendport);

+ }

+ 

+ void delete_local_ibendport(int index) {

+     semanage_ibendport_key_t *key = NULL;

+     char name[50];

+ 

+     CU_ASSERT(index >= 0 && index <= 255);

+ 

+     snprintf(name, 50, "ibendport%d", index);

+ 

+     CU_ASSERT_FATAL(semanage_ibendport_key_create(sh, name, 0, &key) >= 0);

+ 

+     CU_ASSERT_FATAL(semanage_ibendport_del_local(sh, key) >= 0);

+ 

+     semanage_ibendport_key_free(key);

+ }

+ 

+ #endif

@@ -0,0 +1,163 @@ 

+ #include <sys/types.h>

+ #include <sys/wait.h>

+ #include <unistd.h>

+ #include <stdlib.h>

+ #include <semanage/handle.h>

+ #include <semanage/semanage.h>

+ 

+ #ifndef __FUNCTIONS_H__

+ #define __FUNCTIONS_H__

+ 

+ #if defined(VERS_MAJOR) && defined(VERS_MINOR)

+ 

+ #define VERS_CHECK(major, minor) \

+     ((VERS_MAJOR > major) || ((VERS_MAJOR == major) && (VERS_MINOR >= minor)))

+ 

+ #else

+ 

+ #define VERS_CHECK(major, minor) 1

+ 

+ #endif

+ 

+ #if defined(VERS_MAJOR) && defined(VERS_MINOR) && defined(VERS_RELEASE)

+ 

+ #define VERS_CHECK_REL(major, minor, release) \

+     ((VERS_MAJOR == major) && (VERS_MINOR == minor) && (VERS_RELEASE >= release))

+ 

+ #else

+ 

+ #define VERS_CHECK_REL(major, minor, release) 0

+ 

+ #endif

+ 

+ #if VERS_CHECK(2, 7) || VERS_CHECK_REL(2, 5, 9)

+ #define FEATURE_INFINIBAND

+ #endif

+ 

+ typedef enum { SH_NULL, SH_HANDLE, SH_CONNECT, SH_TRANS } level_t;

+ 

+ void setup_handle(level_t level);

+ void cleanup_handle(level_t level);

+ void setup_handle_invalid_store(level_t level);

+ 

+ void helper_handle_create(void);

+ void helper_handle_destroy(void);

+ void helper_connect(void);

+ void helper_disconnect(void);

+ void helper_begin_transaction(void);

+ void helper_commit(void);

+ 

+ int context_compare(semanage_context_t *con, const char *str);

+ 

+ // forks, runs code and checks signal returned by child process

+ #define CU_ASSERT_SIGNAL(CODE,SIGNAL) \

+     do { \

+         pid_t pid = fork(); \

+         if (pid == 0) { \

+             CODE; \

+             exit(0); \

+         } else { \

+             int stat_val; \

+             wait(&stat_val); \

+             CU_ASSERT_FALSE(WIFEXITED(stat_val)); \

+             CU_ASSERT_EQUAL(WTERMSIG(stat_val), SIGNAL); \

+         } \

+     } while(0)

+ 

+ #define CU_ASSERT_CONTEXT(CON1,CON2,EQUAL) \

+     do { \

+         char *str; \

+         char *str2; \

+         CU_ASSERT_PTR_NOT_NULL(CON1); \

+         CU_ASSERT_PTR_NOT_NULL(CON2); \

+         CU_ASSERT(semanage_context_to_string(sh, CON1, &str) >= 0); \

+         CU_ASSERT(semanage_context_to_string(sh, CON2, &str2) >= 0); \

+         if (EQUAL) {\

+             CU_ASSERT_STRING_EQUAL(str, str2); \

+         } else {\

+             CU_ASSERT_STRING_NOT_EQUAL(str, str2); \

+         } \

+     } while(0)

+ 

+ #define CU_ASSERT_CONTEXT_EQUAL(CON1,CON2) CU_ASSERT_CONTEXT(CON1,CON2,1)

+ #define CU_ASSERT_CONTEXT_NOT_EQUAL(CON1,CON2) CU_ASSERT_CONTEXT(CON1,CON2,0)

+ 

+ #define I_NULL  -1

+ #define I_FIRST  0

+ #define I_SECOND 1

+ #define I_THIRD  2

+ 

+ // seusers

+ semanage_seuser_t *get_seuser_nth(int index);

+ semanage_seuser_t *get_seuser_new();

+ 

+ semanage_seuser_key_t *get_seuser_key_nth(int index);

+ semanage_seuser_key_t *get_seuser_key_from_str(const char *str);

+ 

+ void add_local_seuser(int seuser_index);

+ void delete_local_seuser(int seuser_index);

+ 

+ // booleans

+ semanage_bool_t *get_bool_nth(int index);

+ semanage_bool_t *get_bool_new();

+ 

+ semanage_bool_key_t *get_bool_key_nth(int index);

+ semanage_bool_key_t *get_bool_key_from_str(const char *str);

+ 

+ void add_local_bool(int bool_index);

+ void delete_local_bool(int bool_index);

+ 

+ // fcontext

+ semanage_fcontext_t *get_fcontext_nth(int index);

+ semanage_fcontext_t *get_fcontext_new();

+ 

+ semanage_fcontext_key_t *get_fcontext_key_nth(int index);

+ semanage_fcontext_key_t *get_fcontext_key_from_str(const char *str, int type);

+ 

+ void add_local_fcontext(int fcontext_index);

+ void delete_local_fcontext(int fcontext_index);

+ 

+ // module

+ #if VERS_CHECK(2, 5)

+ semanage_module_info_t *get_module_info_nth(int index, semanage_module_info_t **modinfo_free);

+ semanage_module_key_t *get_module_key_nth(int index);

+ int module_exists(const char *name);

+ #endif

+ 

+ // port

+ semanage_port_t *get_port_nth(int index);

+ semanage_port_key_t *get_port_key_nth(int index);

+ 

+ void add_local_port(int port_index);

+ void delete_local_port(int port_index);

+ 

+ // iface

+ semanage_iface_t *get_iface();

+ semanage_iface_key_t *get_iface_key();

+ 

+ void add_local_iface(int index);

+ void delete_local_iface(int index);

+ 

+ // node

+ 

+ semanage_node_t *get_node();

+ semanage_node_key_t *get_node_key();

+ 

+ void add_local_node(int index);

+ void delete_local_node(int index);

+ 

+ // user

+ 

+ semanage_user_t *get_user_nth(int index);

+ semanage_user_key_t *get_user_key_nth(int index);

+ 

+ void add_local_user(int index);

+ void delete_local_user(int index);

+ 

+ // ibendport

+ #ifdef FEATURE_INFINIBAND

+ void add_local_ibendport(int index);

+ void delete_local_ibendport(int index);

+ #endif

+ 

+ #endif

@@ -0,0 +1,110 @@ 

+ /* Authors: Christopher Ashworth <cashworth@tresys.com>

+  *          Caleb Case <ccase@tresys.com>

+  *          Chad Sellers <csellers@tresys.com>

+  *

+  * Copyright (C) 2006 Tresys Technology, LLC

+  *

+  *  This library is free software; you can redistribute it and/or

+  *  modify it under the terms of the GNU Lesser General Public

+  *  License as published by the Free Software Foundation; either

+  *  version 2.1 of the License, or (at your option) any later version.

+  *

+  *  This library is distributed in the hope that it will be useful,

+  *  but WITHOUT ANY WARRANTY; without even the implied warranty of

+  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU

+  *  Lesser General Public License for more details.

+  *

+  *  You should have received a copy of the GNU Lesser General Public

+  *  License along with this library; if not, write to the Free Software

+  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

+  */

+ 

+ #include <stdio.h>

+ 

+ #include "test_handle.h"

+ #include "test_seuser.h"

+ #include "test_bool.h"

+ #include "test_fcontext.h"

+ #include "test_module.h"

+ #include "test_port.h"

+ #include "test_iface.h"

+ #include "test_node.h"

+ #include "test_other.h"

+ #include "test_user.h"

+ #include "test_ibendport.h"

+ 

+ #include <CUnit/Basic.h>

+ #include <CUnit/Console.h>

+ #include <CUnit/TestDB.h>

+ 

+ #define DECLARE_SUITE(name) \

+     suites[suite_index] = CU_add_suite(#name, name##_test_init, name##_test_cleanup); \

+     if (NULL == suites[suite_index]) { \

+         CU_cleanup_registry(); \

+         return CU_get_error(); } \

+     if (name##_add_tests(suites[suite_index])) { \

+         CU_cleanup_registry(); \

+         return CU_get_error(); } \

+     suites_names[suite_index] = #name; \

+     suite_index++;

+ 

+ #define SUITE_NUM 11

+ 

+ CU_pSuite suites[SUITE_NUM] = { NULL };

+ const char *suites_names[SUITE_NUM] = { NULL };

+ 

+ int suite_index = 0;

+ 

+ int main(int argc, char **argv)

+ {

+     if (CUE_SUCCESS != CU_initialize_registry())

+         return CU_get_error();

+ 

+     // when adding new suite, update SUITE_NUM and include header file

+     DECLARE_SUITE(handle);

+     DECLARE_SUITE(seuser);

+     DECLARE_SUITE(fcontext);

+     DECLARE_SUITE(module);

+     DECLARE_SUITE(bool);

+     DECLARE_SUITE(port);

+     DECLARE_SUITE(iface);

+     DECLARE_SUITE(node);

+     DECLARE_SUITE(other);

+     DECLARE_SUITE(user);

+     DECLARE_SUITE(ibendport);

+ 

+     CU_basic_set_mode(CU_BRM_VERBOSE);

+ 

+     if (argc == 1) {

+         CU_basic_run_tests();

+     }

+     else if (argc == 2 && strcmp(argv[1], "list") == 0) {

+         for (int i = 0; i < suite_index; i++)

+             printf("%s ", suites_names[i]);

+     }

+     else if (argc == 3 && strcmp(argv[1], "suite") == 0) {

+         int i;

+ 

+         for (i = 0; i < suite_index; i++) {

+             if (strcmp(argv[2], suites_names[i]) == 0) {

+                 CU_basic_run_suite(suites[i]);

+                 break;

+             }

+         }

+         

+         if (i == suite_index) {

+             fprintf(stderr, "Invalid suite name\n");

+             CU_cleanup_registry();

+             return 1;

+         }

+     }

+     else {

+         fprintf(stderr, "Invalid arguments\n");

+         CU_cleanup_registry();

+         return 1;

+     }

+ 

+     CU_cleanup_registry();

+ 

+     return 0;

+ }

The added file is too large to be shown here, see it at: libsemanage/sanity-tests/tests/test_bool.c
@@ -0,0 +1,48 @@ 

+ #ifndef __TEST_BOOL_H__

+ #define __TEST_BOOL_H__

+ 

+ #include <CUnit/Basic.h>

+ 

+ int bool_test_init(void);

+ int bool_test_cleanup(void);

+ int bool_add_tests(CU_pSuite suite);

+ 

+ // boolean_record.h

+ 

+ void test_bool_key_create(void);

+ void test_bool_key_extract(void);

+ void test_bool_compare(void);

+ void test_bool_compare2(void);

+ void test_bool_get_set_name(void);

+ void test_bool_get_set_value(void);

+ void test_bool_create(void);

+ void test_bool_clone(void);

+ 

+ // booleans_policy.h

+ 

+ void test_bool_query(void);

+ void test_bool_exists(void);

+ void test_bool_count(void);

+ void test_bool_iterate(void);

+ void test_bool_list(void);

+ 

+ // booleans_local.h

+ 

+ void test_bool_modify_del_local(void);

+ void test_bool_query_local(void);

+ void test_bool_exists_local(void);

+ void test_bool_count_local(void);

+ void test_bool_iterate_local(void);

+ void test_bool_list_local(void);

+ 

+ // booleans_active.h

+ 

+ void test_bool_set_active(void);

+ void test_bool_query_active(void);

+ void test_bool_exists_active(void);

+ void test_bool_count_active(void);

+ void test_bool_iterate_active(void);

+ void test_bool_list_active(void);

+ 

+ #endif

+ 

The added file is too large to be shown here, see it at: libsemanage/sanity-tests/tests/test_fcontext.c
@@ -0,0 +1,41 @@ 

+ #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);

+ 

+ // fcontext_record.h

+ 

+ void test_fcontext_compare(void);

+ void test_fcontext_compare2(void);

+ void test_fcontext_key_create(void);

+ void test_fcontext_key_extract(void);

+ void test_fcontext_get_set_expr(void);

+ void test_fcontext_get_set_type(void);

+ void test_fcontext_get_type_str(void);

+ void test_fcontext_get_set_con(void);

+ void test_fcontext_create(void);

+ void test_fcontext_clone(void);

+ 

+ // fcontext_policy.h

+ 

+ void test_fcontext_query(void);

+ void test_fcontext_exists(void);

+ void test_fcontext_count(void);

+ void test_fcontext_iterate(void);

+ void test_fcontext_list(void);

+ 

+ // fcontext_local.h

+ 

+ void test_fcontext_modify_del_local(void);

+ void test_fcontext_query_local(void);

+ void test_fcontext_exists_local(void);

+ void test_fcontext_count_local(void);

+ void test_fcontext_iterate_local(void);

+ void test_fcontext_list_local(void);

+ 

+ #endif

+ 

@@ -0,0 +1,432 @@ 

+ #include <sys/wait.h>

+ 

+ #include <semanage/semanage.h>

+ #include <semanage/handle.h>

+ 

+ #include <CUnit/Basic.h>

+ 

+ #include "test_handle.h"

+ #include "functions.h"

+ 

+ extern semanage_handle_t *sh;

+ 

+ int handle_test_init(void)

+ {

+ 	return 0;

+ }

+ 

+ int handle_test_cleanup(void)

+ {

+ 	return 0;

+ }

+ 

+ int handle_add_tests(CU_pSuite suite)

+ {

+     CU_add_test(suite, "test_handle_create",    test_handle_create);

+     CU_add_test(suite, "test_connect",          test_connect);

+     CU_add_test(suite, "test_disconnect",       test_disconnect);

+     CU_add_test(suite, "test_transaction",      test_transaction);

+     CU_add_test(suite, "test_commit",           test_commit);

+     CU_add_test(suite, "test_is_connected",     test_is_connected);

+     CU_add_test(suite, "test_access_check",     test_access_check);

+     CU_add_test(suite, "test_is_managed",       test_is_managed);

+     CU_add_test(suite, "test_mls_enabled",      test_mls_enabled);

+     CU_add_test(suite, "msg_set_callback",      test_msg_set_callback);

+ 

+ #if VERS_CHECK(2, 5)

+     CU_add_test(suite, "test_root",         test_root);

+ #endif

+ 

+     CU_add_test(suite, "test_select_store", test_select_store);

+ 

+ 	return 0;

+ }

+ 

+ // Function semanage_handle_create

+ 

+ void test_handle_create(void) {

+     sh = semanage_handle_create();

+ 

+     CU_ASSERT_PTR_NOT_NULL(sh);

+ 

+     semanage_handle_destroy(sh);

+ }

+ 

+ // Function semanage_connect

+ // TODO: make semanage_direct_connect(sh) return error

+ // TODO: make semanage_check_init return error

+ // TODO: make semanage_create_store return error

+ // TODO: make other functions return error

+ // TODO: try all values of disable dontaudit (add file)

+ 

+ void test_connect(void) {

+     // test without handle

+     setup_handle(SH_NULL);

+ 

+     CU_ASSERT_SIGNAL(semanage_connect(sh), SIGABRT);

+ 

+     // test handle created

+     helper_handle_create();

+ 

+     CU_ASSERT(semanage_connect(sh) >= 0);

+     CU_ASSERT(semanage_disconnect(sh) >= 0);

+ 

+     cleanup_handle(SH_HANDLE);

+ 

+     // test invalid store

+     setup_handle_invalid_store(SH_HANDLE);

+ 

+     CU_ASSERT(semanage_connect(sh) < 0);

+ 

+     cleanup_handle(SH_HANDLE);

+ 

+     // test normal use

+     setup_handle(SH_HANDLE);

+ 

+     CU_ASSERT(semanage_connect(sh) >= 0);

+ 

+     // test connecting twice

+     // TODO: connecting twice creates memory leaks

+     //CU_ASSERT(semanage_connect(sh) < 0);

+     CU_ASSERT(semanage_connect(sh) >= 0);

+ 

+     CU_ASSERT(semanage_disconnect(sh) >= 0);

+ 

+     cleanup_handle(SH_HANDLE);

+ }

+ 

+ // Function semanage_disconnect

+ // TODO: disconnect when disconnect function not set

+ // TODO: make semanage_remove_directory return error (twice)

+ 

+ void test_disconnect(void) {

+     setup_handle(SH_NULL);

+ 

+     CU_ASSERT_SIGNAL(semanage_disconnect(sh), SIGABRT);

+ 

+     helper_handle_create();

+ 

+     CU_ASSERT_SIGNAL(semanage_disconnect(sh), SIGABRT);

+ 

+     helper_connect();

+ 

+     CU_ASSERT(semanage_disconnect(sh) >= 0);

+ 

+     cleanup_handle(SH_HANDLE);

+ }

+ 

+ // Function semanage_begin_transaction

+ // TODO: make semanage_get_trans_lock return error

+ // TODO: make semanage_make_sandbow return error

+ // TODO: make semanage_make_final return error

+ // TODO: there is a memory leak in semanage_begin_transaction()

+ /*

+ ==24810== 101 (80 direct, 21 indirect) bytes in 1 blocks are definitely lost in loss record 26 of 31

+ ==24810==    at 0x4C30A1E: calloc (vg_replace_malloc.c:711)

+ ==24810==    by 0x5062AF8: semanage_genhomedircon (genhomedircon.c:1351)

+ ==24810==    by 0x505FAF4: semanage_direct_commit (direct_api.c:1567)

+ ==24810==    by 0x5064C6C: semanage_commit (handle.c:426)

+ ==24810==    by 0x41032E: test_transaction (test_handle.c:146)

+ ==24810==    by 0x4E3F106: ??? (in /usr/lib64/libcunit.so.1.0.1)

+ ==24810==    by 0x4E3F33C: ??? (in /usr/lib64/libcunit.so.1.0.1)

+ ==24810==    by 0x4E3F855: CU_run_suite (in /usr/lib64/libcunit.so.1.0.1)

+ ==24810==    by 0x42085A: main (libsemanage-tests.c:90)

+ */

+ 

+ void test_transaction(void) {

+     // test without handle

+     setup_handle(SH_NULL);

+ 

+     CU_ASSERT_SIGNAL(semanage_begin_transaction(sh), SIGABRT);

+ 

+     // test with handle

+     helper_handle_create();

+ 

+     CU_ASSERT_SIGNAL(semanage_begin_transaction(sh), SIGABRT);

+ 

+     // test disconnected

+     helper_connect();

+     helper_disconnect();

+ 

+     CU_ASSERT(semanage_begin_transaction(sh) < 0);

+ 

+     cleanup_handle(SH_HANDLE);

+ 

+     // test normal use

+     setup_handle(SH_CONNECT);

+ 

+     CU_ASSERT(semanage_begin_transaction(sh) >= 0);

+     CU_ASSERT(semanage_commit(sh) >= 0);

+ 

+     // test beginning transaction twice

+     // FIXME

+     //CU_ASSERT(semanage_begin_transaction(sh) >= 0);

+     //CU_ASSERT(semanage_begin_transaction(sh) >= 0);

+     //CU_ASSERT(semanage_commit(sh) >= 0);

+ 

+     cleanup_handle(SH_CONNECT);

+ }

+ 

+ // Function semanage_commit

+ // TODO: try other values of disable dontaudit

+ // TODO: try other values of preserve tunables

+ // TODO: make users->dtable->is_modified return true

+ // TODO: try missing paths (lines 1236-1272)

+ // TODO: error gotos (lines 1299-1514)

+ // TODO: make sh->conf->disable_genhomedircon return true

+ // TODO: make semanage_genhomedircon return error

+ 

+ void test_commit(void) {

+     setup_handle(SH_NULL);

+ 

+     CU_ASSERT_SIGNAL(semanage_commit(sh), SIGABRT);

+ 

+     helper_handle_create();

+ 

+     CU_ASSERT_SIGNAL(semanage_commit(sh), SIGABRT);

+ 

+     helper_connect();

+ 

+     CU_ASSERT(semanage_commit(sh) < 0);

+ 

+     helper_begin_transaction();

+ 

+     // FIXME: memory leaks

+     CU_ASSERT(semanage_commit(sh) >= 0);

+ 

+     cleanup_handle(SH_CONNECT);

+ }

+ 

+ // Function semanage_is_connected

+ 

+ void test_is_connected(void) {

+     setup_handle(SH_NULL);

+ 

+     CU_ASSERT_SIGNAL(semanage_is_connected(sh), SIGABRT);

+ 

+     helper_handle_create();

+ 

+     CU_ASSERT(semanage_is_connected(sh) == 0);

+ 

+     helper_connect();

+ 

+     CU_ASSERT(semanage_is_connected(sh) == 1);

+ 

+     helper_begin_transaction();

+ 

+     CU_ASSERT(semanage_is_connected(sh) == 1);

+ 

+     // FIXME: memory leaks (semanage_commit)

+     cleanup_handle(SH_TRANS);

+ }

+ 

+ // Function semanage_access_check

+ 

+ void test_access_check(void) {

+     int result = 0;

+ 

+     // test without handle

+     setup_handle(SH_NULL);

+ 

+     CU_ASSERT_SIGNAL(semanage_access_check(sh), SIGABRT);

+ 

+     // test with handle

+     helper_handle_create();

+ 

+     result = semanage_access_check(sh);

+ 

+     CU_ASSERT(result == 0 || result == SEMANAGE_CAN_READ

+               || result == SEMANAGE_CAN_WRITE);

+ 

+     cleanup_handle(SH_HANDLE);

+     

+     // test with invalid store

+     setup_handle_invalid_store(SH_HANDLE);

+ 

+     CU_ASSERT(semanage_access_check(sh) < 0);

+ 

+     cleanup_handle(SH_HANDLE);

+     

+     // test normal use

+     setup_handle(SH_CONNECT);

+ 

+     result = semanage_access_check(sh);

+ 

+     CU_ASSERT(result == 0 || result == SEMANAGE_CAN_READ

+               || result == SEMANAGE_CAN_WRITE);

+ 

+     cleanup_handle(SH_CONNECT);

+ }

+ 

+ // Function semanage_is_managed

+ // TODO: make semanage_check_init return error

+ // TODO: make semanage_access_check return error

+ 

+ void test_is_managed(void) {

+     int result = 0;

+     

+     // test without handle

+     setup_handle(SH_NULL);

+ 

+     CU_ASSERT_SIGNAL(semanage_is_managed(sh), SIGABRT);

+ 

+     // test with handle

+     helper_handle_create();

+ 

+     result = semanage_is_managed(sh);

+ 

+     CU_ASSERT(result == 0 || result == 1);

+ 

+     // test connected

+     helper_connect();

+ 

+     result = semanage_is_managed(sh);

+ 

+     CU_ASSERT(result < 0);

+ 

+     cleanup_handle(SH_CONNECT);

+ }

+ 

+ // Function semanage_mls_enabled

+ 

+ void test_mls_enabled(void) {

+     int result = 0;

+ 

+     // test without handle

+     setup_handle(SH_NULL);

+ 

+     CU_ASSERT_SIGNAL(semanage_mls_enabled(sh), SIGABRT);

+ 

+     // test with handle

+     helper_handle_create();

+ 

+     result = semanage_mls_enabled(sh);

+ 

+     CU_ASSERT(result == 0 || result == 1);

+ 

+     cleanup_handle(SH_HANDLE);

+     

+     // test with invalid store

+     setup_handle_invalid_store(SH_HANDLE);

+ 

+     CU_ASSERT(semanage_mls_enabled(sh) < 0);

+ 

+     cleanup_handle(SH_HANDLE);

+ 

+     // test connected

+     setup_handle(SH_CONNECT);

+ 

+     result = semanage_mls_enabled(sh);

+ 

+     CU_ASSERT(result == 0 || result == 1);

+ 

+     cleanup_handle(SH_CONNECT);

+ }

+ 

+ // Function semanage_set_callback

+ 

+ int msg_set_callback_count = 0;

+ 

+ void helper_msg_set_callback(void *varg, semanage_handle_t *sh,

+                              const char *fmt, ...) {

+     msg_set_callback_count++;

+ }

+ 

+ void test_msg_set_callback(void) {

+     setup_handle(SH_CONNECT);

+ 

+     semanage_msg_set_callback(sh, helper_msg_set_callback, NULL);

+ 

+     // produce error message

+     semanage_commit(sh);

+ 

+     CU_ASSERT(msg_set_callback_count == 1);

+ 

+     semanage_msg_set_callback(sh, NULL, NULL);

+ 

+     // produce error message

+     semanage_commit(sh);

+ 

+     CU_ASSERT(msg_set_callback_count == 1);

+ 

+     cleanup_handle(SH_CONNECT);

+ }

+ 

+ // Function semanage_root, semanage_set_root

+ 

+ #if VERS_CHECK(2, 5)

+ 

+ void helper_root(void) {

+     const char *root = semanage_root();

+ 

+     CU_ASSERT_STRING_EQUAL(root, "");

+ 

+     CU_ASSERT(semanage_set_root("asdf") >= 0);

+ 

+     root = semanage_root();

+ 

+     CU_ASSERT_STRING_EQUAL(root, "asdf");

+ 

+     CU_ASSERT(semanage_set_root("") >= 0);

+ 

+     root = semanage_root();

+ 

+     CU_ASSERT_STRING_EQUAL(root, "");

+ }

+ 

+ void test_root(void) {

+     // test without handle

+     setup_handle(SH_NULL);

+ 

+     helper_root();

+ 

+     // test with handle

+     helper_handle_create();

+ 

+     helper_root();

+ 

+     // test connected

+     helper_connect();

+ 

+     helper_root();

+ 

+     cleanup_handle(SH_CONNECT);

+ }

+ 

+ #endif

+ 

+ // Function semanage_select_store

+ 

+ void helper_select_store(char *name, enum semanage_connect_type type,

+                          int exp_result) {

+     setup_handle(SH_HANDLE);

+ 

+     semanage_select_store(sh, name, type);

+ 

+     int result = semanage_connect(sh);

+ 

+     if (exp_result < 0) {

+         CU_ASSERT(result < 0);

+     }

+     else {

+         CU_ASSERT(result >= 0);

+     }

+ 

+     if (result >= 0)

+         cleanup_handle(SH_CONNECT);

+     else

+         cleanup_handle(SH_HANDLE);

+ }

+ 

+ void test_select_store(void) {

+     helper_select_store("asdf", SEMANAGE_CON_INVALID - 1, -1);

+     helper_select_store("asdf", SEMANAGE_CON_POLSERV_REMOTE + 1, -1);

+     helper_select_store("", SEMANAGE_CON_DIRECT, 0);

+     CU_ASSERT_SIGNAL(helper_select_store(NULL, SEMANAGE_CON_DIRECT, -1),

+                      SIGSEGV);

+ 

+     helper_select_store("asdf", SEMANAGE_CON_INVALID, -1);

+     helper_select_store("asdf", SEMANAGE_CON_DIRECT, 0);

+     helper_select_store("asdf", SEMANAGE_CON_POLSERV_LOCAL, -1);

+     helper_select_store("asdf", SEMANAGE_CON_POLSERV_REMOTE, -1);

+ }

@@ -0,0 +1,30 @@ 

+ #ifndef __TEST_HANDLE_H__

+ #define __TEST_HANDLE_H__

+ 

+ #include <CUnit/Basic.h>

+ 

+ #include "functions.h"

+ 

+ int handle_test_init(void);

+ int handle_test_cleanup(void);

+ int handle_add_tests(CU_pSuite suite);

+ 

+ void test_handle_create(void);

+ void test_connect(void);

+ void test_disconnect(void);

+ void test_transaction(void);

+ void test_commit(void);

+ void test_is_connected(void);

+ void test_access_check(void);

+ void test_is_managed(void);

+ void test_mls_enabled(void);

+ void test_msg_set_callback(void);

+ 

+ #if VERS_CHECK(2, 5)

+ void test_root(void);

+ #endif

+ 

+ void test_select_store(void);

+ 

+ #endif

+ 

@@ -0,0 +1,542 @@ 

+ #include <stdio.h>

+ #include <string.h>

+ #include <semanage/semanage.h>

+ #include <semanage/handle.h>

+ #include <CUnit/Basic.h>

+ 

+ #include "test_ibendport.h"

+ #include "test_ibendport_module.h"

+ #include "functions.h"

+ 

+ extern semanage_handle_t *sh;

+ 

+ int ibendport_test_init(void) {

+ #ifdef FEATURE_INFINIBAND

+ 

+     semanage_handle_t *sh;

+ 

+     sh = semanage_handle_create();

+ 

+     if (semanage_connect(sh) < 0) {

+         semanage_handle_destroy(sh);

+         return -1;

+     }

+ 

+     if (semanage_begin_transaction(sh) < 0)

+         goto err;

+ 

+     if (semanage_module_install(sh, (char *) test_ibendport_module,

+                                 test_ibendport_module_len, "test_ibendport",

+                                 "cil") < 0) {

+         semanage_disconnect(sh);

+         semanage_handle_destroy(sh);

+         return -1;

+     }

+ 

+     if (semanage_commit(sh) < 0) {

+         semanage_disconnect(sh);

+         semanage_handle_destroy(sh);

+         return -1;

+     }

+ 

+     return 0;

+ 

+ err:

+     semanage_disconnect(sh);

+     semanage_handle_destroy(sh);

+     return -1;

+ 

+ #else

+ 

+     return 0;

+ 

+ #endif

+ }

+ 

+ int ibendport_test_cleanup(void) {

+ #ifdef FEATURE_INFINIBAND

+ 

+     semanage_handle_t *sh;

+ 

+     sh = semanage_handle_create();

+ 

+     if (semanage_connect(sh) < 0)

+         return -1;

+ 

+     if (semanage_begin_transaction(sh) < 0) {

+         semanage_disconnect(sh);

+         semanage_handle_destroy(sh);

+         return -1;

+     }

+ 

+     if (semanage_module_remove(sh, "test_ibendport") < 0) {

+         semanage_disconnect(sh);

+         semanage_handle_destroy(sh);

+         return -1;

+     }

+ 

+     if (semanage_commit(sh) < 0) {

+         semanage_disconnect(sh);

+         semanage_handle_destroy(sh);

+         return -1;

+     }

+ 

+     semanage_disconnect(sh);

+     semanage_handle_destroy(sh);

+ 

+ #endif

+ 

+     return 0;

+ }

+ 

+ int ibendport_add_tests(CU_pSuite suite) {

+ #ifdef FEATURE_INFINIBAND

+     CU_add_test(suite, "ibendport_query", test_ibendport_query);

+     CU_add_test(suite, "ibendport_exists", test_ibendport_exists);

+     CU_add_test(suite, "ibendport_count", test_ibendport_count);

+     CU_add_test(suite, "ibendport_iterate", test_ibendport_iterate);

+     CU_add_test(suite, "ibendport_list", test_ibendport_list);

+ 

+     CU_add_test(suite, "ibendport_modify_del_query_local",

+                 test_ibendport_modify_del_query_local);

+     CU_add_test(suite, "ibendport_exists_local", test_ibendport_exists_local);

+     CU_add_test(suite, "ibendport_count_local", test_ibendport_count_local);

+     CU_add_test(suite, "ibendport_iterate_local", test_ibendport_iterate_local);

+     CU_add_test(suite, "ibendport_list_local", test_ibendport_list_local);

+ #endif

+ 

+     return 0;

+ }

+ 

+ #ifdef FEATURE_INFINIBAND

+ 

+ // Function semanage_ibendport_query

+ 

+ void test_ibendport_query(void) {

+     semanage_ibendport_key_t *key1 = NULL;

+     semanage_ibendport_key_t *key2 = NULL;

+     semanage_ibendport_t *response1 = NULL;

+     semanage_ibendport_t *response2 = NULL;

+     char *name = NULL;

+     semanage_context_t *con = NULL;

+ 

+     // setup

+     setup_handle(SH_CONNECT);

+ 

+     CU_ASSERT(semanage_ibendport_key_create(sh, IBENDPORT_INV_NAME,

+                                             IBENDPORT_INV_PORT, &key1) >= 0);

+ 

+     CU_ASSERT(semanage_ibendport_key_create(sh, IBENDPORT_1_NAME,

+                                             IBENDPORT_1_PORT, &key2) >= 0);

+ 

+     // test nonexisting ibendport

+     CU_ASSERT(semanage_ibendport_query(sh, key1, &response1) >= 0);

+     CU_ASSERT(response1 == NULL);

+ 

+     // test existing ibendport

+     CU_ASSERT(semanage_ibendport_query(sh, key2, &response2) >= 0);

+     CU_ASSERT_PTR_NOT_NULL_FATAL(response2);

+ 

+     CU_ASSERT(semanage_ibendport_get_ibdev_name(sh, response2, &name) >= 0);

+     CU_ASSERT_PTR_NOT_NULL_FATAL(name);

+     CU_ASSERT_STRING_EQUAL(name, IBENDPORT_1_NAME);

+ 

+     CU_ASSERT(semanage_ibendport_get_port(response2) == IBENDPORT_1_PORT);

+ 

+     con = semanage_ibendport_get_con(response2);

+     CU_ASSERT_PTR_NOT_NULL_FATAL(con);

+     CU_ASSERT(context_compare(con, IBENDPORT_1_CON) == 0);

+ 

+     // cleanup

+     free(name);

+     semanage_ibendport_key_free(key1);

+     semanage_ibendport_key_free(key2);

+     semanage_ibendport_free(response1);

+     semanage_ibendport_free(response2);

+     cleanup_handle(SH_CONNECT);

+ }

+ 

+ // Function semanage_ibendport_exists

+ 

+ void test_ibendport_exists(void) {

+     semanage_ibendport_key_t *key1 = NULL;

+     semanage_ibendport_key_t *key2 = NULL;

+     int response = 42;

+ 

+     // setup

+     setup_handle(SH_CONNECT);

+ 

+     CU_ASSERT(semanage_ibendport_key_create(sh, IBENDPORT_INV_NAME,

+                                             IBENDPORT_INV_PORT, &key1) >= 0);

+ 

+     CU_ASSERT(semanage_ibendport_key_create(sh, IBENDPORT_1_NAME,

+                                             IBENDPORT_1_PORT, &key2) >= 0);

+ 

+     // test nonexisting ibendport

+     CU_ASSERT(semanage_ibendport_exists(sh, key1, &response) >= 0);

+     CU_ASSERT(!response);

+ 

+     // test existing ibendport

+     CU_ASSERT(semanage_ibendport_exists(sh, key2, &response) >= 0);

+     CU_ASSERT(response);

+ 

+     // cleanup

+     semanage_ibendport_key_free(key1);

+     semanage_ibendport_key_free(key2);

+     cleanup_handle(SH_CONNECT);

+ }

+ 

+ // Function semanage_ibendport_count

+ 

+ void test_ibendport_count(void) {

+     unsigned int count = 42;

+ 

+     // setup

+     setup_handle(SH_CONNECT);

+ 

+     // test

+     CU_ASSERT(semanage_ibendport_count(sh, &count) >= 0);

+ 

+     CU_ASSERT(count == IBENDPORT_COUNT);

+ 

+     // cleanup

+     cleanup_handle(SH_CONNECT);

+ }

+ 

+ // Function semanage_ibendport_iterate

+ 

+ unsigned int helper_ibendport_iterate_counter = 0;

+ 

+ int helper_ibendport_iterate(const semanage_ibendport_t *ibendport,

+                              void *fn_arg) {

+     CU_ASSERT(fn_arg == (void *) 42);

+ 

+     helper_ibendport_iterate_counter++;

+ 

+     return 0;

+ }

+ 

+ int helper_ibendport_iterate_error(const semanage_ibendport_t *ibendport,

+                                    void *fn_arg) {

+     CU_ASSERT(fn_arg == (void *) 42);

+ 

+     helper_ibendport_iterate_counter++;

+ 

+     return -1;

+ }

+ 

+ int helper_ibendport_iterate_break(const semanage_ibendport_t *ibendport,

+                                    void *fn_arg) {

+     CU_ASSERT(fn_arg == (void *) 42);

+ 

+     helper_ibendport_iterate_counter++;

+ 

+     return 1;

+ }

+ 

+ void test_ibendport_iterate(void) {

+     // setup

+     setup_handle(SH_CONNECT);

+ 

+     // test

+     helper_ibendport_iterate_counter = 0;

+ 

+     CU_ASSERT(semanage_ibendport_iterate(sh, helper_ibendport_iterate,

+                                          (void *) 42) >= 0);

+     CU_ASSERT(helper_ibendport_iterate_counter == IBENDPORT_COUNT);

+ 

+     // test function which returns error

+     helper_ibendport_iterate_counter = 0;

+ 

+     CU_ASSERT(semanage_ibendport_iterate(sh, helper_ibendport_iterate_error,

+                                          (void *) 42) < 0);

+     CU_ASSERT(helper_ibendport_iterate_counter == 1);

+ 

+     // test function which requests break

+     helper_ibendport_iterate_counter = 0;

+ 

+     CU_ASSERT(semanage_ibendport_iterate(sh, helper_ibendport_iterate_break,

+                                          (void *) 42) >= 0);

+     CU_ASSERT(helper_ibendport_iterate_counter == 1);

+ 

+     // cleanup

+     cleanup_handle(SH_CONNECT);

+ }

+ 

+ // Function semanage_ibendport_list

+ 

+ void test_ibendport_list(void) {

+     semanage_ibendport_t **records = NULL;

+     unsigned int count = 42;

+     char *name = NULL;

+     semanage_context_t *con = NULL;

+ 

+     // setup

+     setup_handle(SH_CONNECT);

+ 

+     // test

+     CU_ASSERT(semanage_ibendport_list(sh, &records, &count) >= 0);

+ 

+     CU_ASSERT_PTR_NOT_NULL_FATAL(records);

+     CU_ASSERT(count == IBENDPORT_COUNT);

+ 

+     for (unsigned int i = 0; i < count; i++) {

+         CU_ASSERT_PTR_NOT_NULL_FATAL(records[i]);

+ 

+         CU_ASSERT(semanage_ibendport_get_ibdev_name(sh, records[i],

+                                                     &name) >= 0);

+ 

+         con = semanage_ibendport_get_con(records[i]);

+         CU_ASSERT_PTR_NOT_NULL_FATAL(con);

+ 

+         free(name);

+     }

+ 

+     // cleanup

+     for (unsigned int i = 0; i < count; i++) {

+         semanage_ibendport_free(records[i]);

+     }

+ 

+     free(records);

+     cleanup_handle(SH_CONNECT);

+ }

+ 

+ // Function semanage_ibendport_modify_local, semanage_ibendport_del_local,

+ // semanage_ibendport_query_local

+ 

+ void test_ibendport_modify_del_query_local(void) {

+     semanage_ibendport_t *ibendport;

+     semanage_ibendport_t *ibendport_local;

+     semanage_ibendport_t *ibendport_tmp;

+     semanage_ibendport_key_t *key = NULL;

+     semanage_ibendport_key_t *key_tmp = NULL;

+     semanage_context_t *con = NULL;

+     semanage_context_t *con_tmp = NULL;

+ 

+     // setup

+     setup_handle(SH_TRANS);

+ 

+     CU_ASSERT(semanage_ibendport_key_create(sh, "asdf", 1, &key) >= 0);

+ 

+     CU_ASSERT(semanage_ibendport_create(sh, &ibendport) >= 0);

+     CU_ASSERT(semanage_ibendport_set_ibdev_name(sh, ibendport, "asdf") >= 0);

+     semanage_ibendport_set_port(ibendport, 1);

+     CU_ASSERT(semanage_context_from_string(sh, "system_u:object_r:bin_t:s0",

+                                            &con) >= 0);

+     CU_ASSERT(semanage_ibendport_set_con(sh, ibendport, con) >= 0);

+ 

+     // add second record, so that semanage_ibendport_compare2_qsort will be

+     // called

+     CU_ASSERT(semanage_ibendport_key_create(sh, "qwerty", 2, &key_tmp) >= 0);

+ 

+     CU_ASSERT(semanage_ibendport_create(sh, &ibendport_tmp) >= 0);

+     CU_ASSERT(semanage_ibendport_set_ibdev_name(sh, ibendport_tmp,

+                                                 "qwerty") >= 0);

+     semanage_ibendport_set_port(ibendport_tmp, 2);

+     CU_ASSERT(semanage_context_from_string(sh, "system_u:object_r:bin_t:s0",

+                                            &con_tmp) >= 0);

+     CU_ASSERT(semanage_ibendport_set_con(sh, ibendport_tmp, con_tmp) >= 0);

+ 

+     // test

+     CU_ASSERT(semanage_ibendport_modify_local(sh, key, ibendport) >= 0);

+     CU_ASSERT(semanage_ibendport_modify_local(sh, key_tmp, ibendport_tmp) >= 0);

+ 

+     // write changes to file

+     helper_commit();

+     helper_begin_transaction();

+ 

+     CU_ASSERT(semanage_ibendport_query_local(sh, key, &ibendport_local) >= 0);

+     CU_ASSERT_PTR_NOT_NULL_FATAL(ibendport_local);

+ 

+     CU_ASSERT(semanage_ibendport_del_local(sh, key) >= 0);

+     CU_ASSERT(semanage_ibendport_del_local(sh, key_tmp) >= 0);

+ 

+     CU_ASSERT(semanage_ibendport_query_local(sh, key, &ibendport_local) < 0);

+ 

+     // cleanup

+     semanage_context_free(con);

+     semanage_context_free(con_tmp);

+     semanage_ibendport_key_free(key);

+     semanage_ibendport_key_free(key_tmp);

+     semanage_ibendport_free(ibendport);

+     semanage_ibendport_free(ibendport_local);

+     semanage_ibendport_free(ibendport_tmp);

+     cleanup_handle(SH_TRANS);

+ }

+ 

+ // Function semanage_ibendport_exists_local

+ 

+ void test_ibendport_exists_local(void) {

+     semanage_ibendport_key_t *key1 = NULL;

+     semanage_ibendport_key_t *key2 = NULL;

+     int response = 42;

+ 

+     // setup

+     setup_handle(SH_TRANS);

+ 

+     add_local_ibendport(I_FIRST);

+ 

+     CU_ASSERT(semanage_ibendport_key_create(sh, IBENDPORT_INV_NAME,

+                                             IBENDPORT_INV_PORT, &key1) >= 0);

+ 

+     CU_ASSERT(semanage_ibendport_key_create(sh, "ibendport0", 0, &key2) >= 0);

+ 

+     // test nonexisting ibendport

+     CU_ASSERT(semanage_ibendport_exists_local(sh, key1, &response) >= 0);

+     CU_ASSERT(!response);

+ 

+     // test existing ibendport

+     CU_ASSERT(semanage_ibendport_exists_local(sh, key2, &response) >= 0);

+     CU_ASSERT(response);

+ 

+     // cleanup

+     

+     delete_local_ibendport(I_FIRST);

+ 

+     semanage_ibendport_key_free(key1);

+     semanage_ibendport_key_free(key2);

+     cleanup_handle(SH_TRANS);

+ }

+ 

+ // Function semanage_ibendport_count_local

+ 

+ void test_ibendport_count_local(void) {

+     unsigned int count = 42;

+ 

+     // setup

+     setup_handle(SH_TRANS);

+ 

+     add_local_ibendport(I_FIRST);

+     add_local_ibendport(I_SECOND);

+     add_local_ibendport(I_THIRD);

+ 

+     // test

+     CU_ASSERT(semanage_ibendport_count_local(sh, &count) >= 0);

+ 

+     CU_ASSERT(count == 3);

+ 

+     // cleanup

+     delete_local_ibendport(I_FIRST);

+     delete_local_ibendport(I_SECOND);

+     delete_local_ibendport(I_THIRD);

+ 

+     cleanup_handle(SH_TRANS);

+ }

+ 

+ // Function semanage_ibendport_iterate_local

+ 

+ unsigned int helper_ibendport_iterate_local_counter = 0;

+ 

+ int helper_ibendport_iterate_local(const semanage_ibendport_t *ibendport,

+                                    void *fn_arg) {

+     CU_ASSERT(fn_arg == (void *) 42);

+ 

+     helper_ibendport_iterate_local_counter++;

+ 

+     return 0;

+ }

+ 

+ int helper_ibendport_iterate_local_error(const semanage_ibendport_t *ibendport,

+                                          void *fn_arg) {

+     CU_ASSERT(fn_arg == (void *) 42);

+ 

+     helper_ibendport_iterate_local_counter++;

+ 

+     return -1;

+ }

+ 

+ int helper_ibendport_iterate_local_break(const semanage_ibendport_t *ibendport,

+                                          void *fn_arg) {

+     CU_ASSERT(fn_arg == (void *) 42);

+ 

+     helper_ibendport_iterate_local_counter++;

+ 

+     return 1;

+ }

+ 

+ void test_ibendport_iterate_local(void) {

+     // setup

+     setup_handle(SH_TRANS);

+ 

+     add_local_ibendport(I_FIRST);

+     add_local_ibendport(I_SECOND);

+     add_local_ibendport(I_THIRD);

+ 

+     // test

+     helper_ibendport_iterate_local_counter = 0;

+ 

+     CU_ASSERT(semanage_ibendport_iterate_local(sh,

+                              helper_ibendport_iterate_local, (void *) 42) >= 0);

+     CU_ASSERT(helper_ibendport_iterate_local_counter == 3);

+ 

+     // test function which returns error

+     helper_ibendport_iterate_local_counter = 0;

+ 

+     CU_ASSERT(semanage_ibendport_iterate_local(sh,

+                         helper_ibendport_iterate_local_error, (void *) 42) < 0);

+     CU_ASSERT(helper_ibendport_iterate_local_counter == 1);

+ 

+     // test function which requests break

+     helper_ibendport_iterate_local_counter = 0;

+ 

+     CU_ASSERT(semanage_ibendport_iterate_local(sh,

+                        helper_ibendport_iterate_local_break, (void *) 42) >= 0);

+     // TODO: there is a bug

+     //CU_ASSERT(helper_ibendport_iterate_local_counter == 1);

+ 

+     // cleanup

+     delete_local_ibendport(I_FIRST);

+     delete_local_ibendport(I_SECOND);

+     delete_local_ibendport(I_THIRD);

+ 

+     cleanup_handle(SH_TRANS);

+ }

+ 

+ // Function semanage_ibendport_list_local

+ 

+ void test_ibendport_list_local(void) {

+     semanage_ibendport_t **records = NULL;

+     unsigned int count = 42;

+     char *name = NULL;

+     semanage_context_t *con = NULL;

+ 

+     // setup

+     setup_handle(SH_TRANS);

+ 

+     add_local_ibendport(I_FIRST);

+     add_local_ibendport(I_SECOND);

+     add_local_ibendport(I_THIRD);

+ 

+     // test

+     CU_ASSERT(semanage_ibendport_list_local(sh, &records, &count) >= 0);

+ 

+     CU_ASSERT_PTR_NOT_NULL_FATAL(records);

+     CU_ASSERT(count == 3);

+ 

+     for (unsigned int i = 0; i < count; i++) {

+         CU_ASSERT_PTR_NOT_NULL_FATAL(records[i]);

+ 

+         CU_ASSERT(semanage_ibendport_get_ibdev_name(sh, records[i],

+                                                     &name) >= 0);

+ 

+         con = semanage_ibendport_get_con(records[i]);

+         CU_ASSERT_PTR_NOT_NULL_FATAL(con);

+ 

+         free(name);

+     }

+ 

+     // cleanup

+     for (unsigned int i = 0; i < count; i++) {

+         semanage_ibendport_free(records[i]);

+     }

+ 

+     free(records);

+ 

+     delete_local_ibendport(I_FIRST);

+     delete_local_ibendport(I_SECOND);

+     delete_local_ibendport(I_THIRD);

+ 

+     cleanup_handle(SH_TRANS);

+ }

+ 

+ #endif

@@ -0,0 +1,32 @@ 

+ #ifndef __TEST_IBENDPORT_H__

+ #define __TEST_IBENDPORT_H__

+ 

+ #include <CUnit/Basic.h>

+ 

+ #include "functions.h"

+ 

+ int ibendport_test_init(void);

+ int ibendport_test_cleanup(void);

+ int ibendport_add_tests(CU_pSuite suite);

+ 

+ #if VERS_CHECK(2, 5)

+ 

+ // ibendports_policy.h

+ 

+ void test_ibendport_query(void);

+ void test_ibendport_exists(void);

+ void test_ibendport_count(void);

+ void test_ibendport_iterate(void);

+ void test_ibendport_list(void);

+ 

+ // ibendports_local.h

+ 

+ void test_ibendport_modify_del_query_local(void);

+ void test_ibendport_exists_local(void);

+ void test_ibendport_count_local(void);

+ void test_ibendport_iterate_local(void);

+ void test_ibendport_list_local(void);

+ 

+ #endif

+ 

+ #endif

@@ -0,0 +1,34 @@ 

+ policy_module(test_ibendport,1.0)

+ 

+ gen_require(`

+         type bin_t;

+         type infiniband_mgmt_device_t;

+ ')

+ 

+ attribute ibendportdomain;

+ 

+ # Domain for process.

+ type test_ibendport_manage_subnet_t;

+ domain_type(test_ibendport_manage_subnet_t)

+ typeattribute test_ibendport_manage_subnet_t ibendportdomain;

+ 

+ type test_ibendport_t;

+ ifdef(`corenet_ib_endport',`

+ corenet_ib_endport(test_ibendport_t)

+ ')

+ 

+ dev_rw_infiniband_dev(test_ibendport_manage_subnet_t)

+ dev_rw_sysfs(test_ibendport_manage_subnet_t)

+ 

+ corecmd_bin_entry_type(test_ibendport_manage_subnet_t)

+ 

+ allow test_ibendport_manage_subnet_t infiniband_mgmt_device_t:chr_file { read write open ioctl};

+ 

+ ifdef(`corenet_ib_access_unlabeled_pkeys',`

+ corenet_ib_access_unlabeled_pkeys(test_ibendport_manage_subnet_t)

+ ')

+ 

+ allow test_ibendport_manage_subnet_t test_ibendport_t:infiniband_endport manage_subnet;

+ 

+ # Allow all of these domains to be entered from the sysadm domain.

+ miscfiles_domain_entry_test_files(ibendportdomain)

@@ -0,0 +1,23 @@ 

+ #define IBENDPORT_COUNT 2

+ 

+ #define IBENDPORT_MODULE_SOURCE \

+     "(type test_ibendport_t)\n" \

+     "(type test_ibendport_second_t)\n" \

+     "(roletype object_r test_ibendport_t)\n" \

+     "(roletype object_r test_ibendport_second_t)\n" \

+     "(ibendportcon mlx4_0 1 (system_u object_r test_ibendport_t ((s0) (s0))))\n" \

+     "(ibendportcon mlx4_1 2 (system_u object_r test_ibendport_second_t ((s0) (s0))))\n"

+ 

+ #define IBENDPORT_INV_NAME "mlx4_42"

+ #define IBENDPORT_INV_PORT 42

+ 

+ #define IBENDPORT_1_NAME "mlx4_0"

+ #define IBENDPORT_1_PORT 1

+ #define IBENDPORT_1_CON "system_u:object_r:test_ibendport_t:s0"

+ 

+ #define IBENDPORT_2_NAME "mlx4_1"

+ #define IBENDPORT_2_PORT 2

+ #define IBENDPORT_2_CON "system_u:object_r:test_ibendport_second_t:s0"

+ 

+ const char *test_ibendport_module = IBENDPORT_MODULE_SOURCE;

+ size_t test_ibendport_module_len = (sizeof IBENDPORT_MODULE_SOURCE) - 1;

@@ -0,0 +1,566 @@ 

+ #include <stdio.h>

+ #include <semanage/semanage.h>

+ #include <semanage/handle.h>

+ #include <CUnit/Basic.h>

+ 

+ #include "test_iface.h"

+ #include "functions.h"

+ 

+ extern semanage_handle_t *sh;

+ 

+ // TODO: current policies does not have any netifcon rules

+ #define IFACE_COUNT 0

+ 

+ int iface_test_init(void) {

+ 	return 0;

+ }

+ 

+ int iface_test_cleanup(void) {

+ 	return 0;

+ }

+ 

+ int iface_add_tests(CU_pSuite suite) {

+     CU_add_test(suite, "iface_compare", test_iface_compare);

+     CU_add_test(suite, "iface_compare2", test_iface_compare2);

+     CU_add_test(suite, "iface_key_create", test_iface_key_create);

+     CU_add_test(suite, "iface_key_extract", test_iface_key_extract);

+     CU_add_test(suite, "iface_get_set_name", test_iface_get_set_name);

+     CU_add_test(suite, "iface_get_set_ifcon", test_iface_get_set_ifcon);

+     CU_add_test(suite, "iface_get_set_msgcon", test_iface_get_set_msgcon);

+     CU_add_test(suite, "iface_create)", test_iface_create);

+     CU_add_test(suite, "iface_clone);", test_iface_clone);

+ 

+     CU_add_test(suite, "iface_query", test_iface_query);

+     CU_add_test(suite, "iface_exists", test_iface_exists);

+     CU_add_test(suite, "iface_count", test_iface_count);

+     CU_add_test(suite, "iface_iterate", test_iface_iterate);

+     CU_add_test(suite, "iface_list", test_iface_list);

+ 

+     CU_add_test(suite, "iface_modify_del_query_local",

+                 test_iface_modify_del_query_local);

+     CU_add_test(suite, "iface_exists_local", test_iface_exists_local);

+     CU_add_test(suite, "iface_count_local", test_iface_count_local);

+     CU_add_test(suite, "iface_iterate_local", test_iface_iterate_local);

+     CU_add_test(suite, "iface_list_local", test_iface_list_local);

+ 

+ 	return 0;

+ }

+ 

+ // Function semanage_iface_compare

+ 

+ void test_iface_compare(void) {

+     semanage_iface_t *iface = NULL;

+     semanage_iface_key_t *key1 = NULL;

+     semanage_iface_key_t *key2 = NULL;

+     int result = 42;

+ 

+     // setup

+     setup_handle(SH_CONNECT);

+ 

+     iface = get_iface();

+ 

+     key1 = get_iface_key();

+ 

+     CU_ASSERT(semanage_iface_key_create(sh, "qwerty", &key2) >= 0);

+     CU_ASSERT_PTR_NOT_NULL(key2);

+ 

+     // test

+     result = semanage_iface_compare(iface, key1);

+ 

+     CU_ASSERT(result == 0);

+ 

+     result = semanage_iface_compare(iface, key2);

+ 

+     CU_ASSERT(result != 0);

+ 

+     // cleanup

+     semanage_iface_free(iface);

+     semanage_iface_key_free(key1);

+     semanage_iface_key_free(key2);

+     cleanup_handle(SH_CONNECT);

+ }

+ 

+ // Function semanage_iface_compare2

+ 

+ void test_iface_compare2(void) {

+     semanage_iface_t *iface1 = NULL;

+     semanage_iface_t *iface2 = NULL;

+     semanage_iface_t *iface3 = NULL;

+     int result = 42;

+ 

+     // setup

+     setup_handle(SH_CONNECT);

+ 

+     iface1 = get_iface();

+     iface2 = get_iface();

+ 

+     iface3 = get_iface();

+     CU_ASSERT(semanage_iface_set_name(sh, iface3, "qwerty") >= 0);

+ 

+     // test

+     result = semanage_iface_compare2(iface1, iface2);

+ 

+     CU_ASSERT(result == 0);

+ 

+     result = semanage_iface_compare2(iface1, iface3);

+ 

+     CU_ASSERT(result != 0);

+ 

+     // cleanup

+     semanage_iface_free(iface1);

+     semanage_iface_free(iface2);

+     semanage_iface_free(iface3);

+     cleanup_handle(SH_CONNECT);

+ }

+ 

+ // Function semanage_iface_create

+ 

+ void test_iface_key_create(void) {

+     semanage_iface_key_t *key = NULL;

+ 

+     setup_handle(SH_CONNECT);

+ 

+     CU_ASSERT(semanage_iface_key_create(sh, "asdf", &key) >= 0);

+     CU_ASSERT_PTR_NOT_NULL(key);

+ 

+     semanage_iface_key_free(key);

+     cleanup_handle(SH_CONNECT);

+ }

+ 

+ // Function semanage_iface_extract

+ 

+ void test_iface_key_extract(void) {

+     semanage_iface_t *iface = NULL;

+     semanage_iface_key_t *key = NULL;

+ 

+     // setup

+     setup_handle(SH_CONNECT);

+ 

+     iface = get_iface();

+ 

+     // test

+     CU_ASSERT(semanage_iface_key_extract(sh, iface, &key) >= 0);

+     CU_ASSERT_PTR_NOT_NULL(key);

+ 

+     // cleanup

+     semanage_iface_free(iface);

+     semanage_iface_key_free(key);

+     cleanup_handle(SH_CONNECT);

+ }

+ 

+ // Function semanage_iface_get_name, semanage_iface_set_name

+ 

+ void test_iface_get_set_name(void) {

+     semanage_iface_t *iface = NULL;

+ 

+     // setup

+     setup_handle(SH_CONNECT);

+ 

+     iface = get_iface();

+ 

+     // test

+     CU_ASSERT(semanage_iface_set_name(sh, iface, "my_asdf") == 0);

+ 

+     CU_ASSERT_STRING_EQUAL(semanage_iface_get_name(iface), "my_asdf");

+ 

+     // cleanup

+     semanage_iface_free(iface);

+     cleanup_handle(SH_CONNECT);

+ }

+ 

+ // Function semanage_iface_get_ifcon, semanage_iface_set_ifcon

+ 

+ void test_iface_get_set_ifcon(void) {

+     semanage_iface_t *iface = NULL;

+     semanage_context_t *con1 = NULL;

+     semanage_context_t *con2 = NULL;

+ 

+     // setup

+     setup_handle(SH_CONNECT);

+ 

+     iface = get_iface();

+ 

+     CU_ASSERT(semanage_context_from_string(sh,

+                                "my_user_u:my_role_r:my_type_t:s0", &con1) >= 0);

+ 

+     // test

+     CU_ASSERT(semanage_iface_set_ifcon(sh, iface, con1) == 0);

+ 

+     con2 = semanage_iface_get_ifcon(iface);

+ 

+     CU_ASSERT_CONTEXT_EQUAL(con1, con2);

+ 

+     // cleanup

+     semanage_iface_free(iface);

+     cleanup_handle(SH_CONNECT);

+ }

+ 

+ // Function semanage_iface_get_msgcon, semanage_iface_set_msgcon

+ 

+ void test_iface_get_set_msgcon(void) {

+     semanage_iface_t *iface = NULL;

+     semanage_context_t *con1 = NULL;

+     semanage_context_t *con2 = NULL;

+ 

+     // setup

+     setup_handle(SH_CONNECT);

+ 

+     iface = get_iface();

+ 

+     CU_ASSERT(semanage_context_from_string(sh,

+                               "my_user_u:my_role_r:my_type_t:s0", &con1) >= 0);

+ 

+     // test

+     CU_ASSERT(semanage_iface_set_msgcon(sh, iface, con1) == 0);

+ 

+     con2 = semanage_iface_get_msgcon(iface);

+ 

+     CU_ASSERT_CONTEXT_EQUAL(con1, con2);

+ 

+     // cleanup

+     semanage_iface_free(iface);

+     cleanup_handle(SH_CONNECT);

+ }

+ 

+ // Function semanage_iface_create

+ 

+ void test_iface_create(void) {

+     semanage_iface_t *iface = NULL;

+     semanage_context_t *ifcon = NULL;

+     semanage_context_t *msgcon = NULL;

+ 

+     // setup

+     setup_handle(SH_CONNECT);

+ 

+     // test

+     CU_ASSERT(semanage_iface_create(sh, &iface) >= 0);

+ 

+     CU_ASSERT(semanage_iface_set_name(sh, iface, "asdf") >= 0);

+ 

+     CU_ASSERT(semanage_context_from_string(sh, "user_u:role_r:type_t:s0",

+                                            &ifcon) >= 0);

+     CU_ASSERT(semanage_iface_set_ifcon(sh, iface, ifcon) >= 0);

+ 

+     CU_ASSERT(semanage_context_from_string(sh, "user_u:role_r:type_t:s0",

+                                            &msgcon) >= 0);

+     CU_ASSERT(semanage_iface_set_msgcon(sh, iface, msgcon) >= 0);

+ 

+     // cleanup

+     semanage_iface_free(iface);

+     cleanup_handle(SH_CONNECT);

+ }

+ 

+ // Function semanage_iface_clone

+ 

+ void test_iface_clone(void) {

+     semanage_iface_t *iface = NULL;

+     semanage_iface_t *iface_clone = NULL;

+     semanage_context_t *ifcon = NULL;

+     semanage_context_t *ifcon2 = NULL;

+     semanage_context_t *msgcon = NULL;

+     semanage_context_t *msgcon2 = NULL;

+ 

+     // setup

+     setup_handle(SH_CONNECT);

+ 

+     CU_ASSERT(semanage_iface_create(sh, &iface) >= 0);

+ 

+     CU_ASSERT(semanage_iface_set_name(sh, iface, "asdf") >= 0);

+ 

+     CU_ASSERT(semanage_context_from_string(sh, "user_u:role_r:if_type_t:s0",

+                                            &ifcon) >= 0);

+     CU_ASSERT(semanage_iface_set_ifcon(sh, iface, ifcon) >= 0);

+ 

+     CU_ASSERT(semanage_context_from_string(sh, "user_u:role_r:msg_type_t:s0",

+                                            &msgcon) >= 0);

+     CU_ASSERT(semanage_iface_set_msgcon(sh, iface, msgcon) >= 0);

+ 

+     // test

+     CU_ASSERT(semanage_iface_clone(sh, iface, &iface_clone) >= 0);

+ 

+     CU_ASSERT_STRING_EQUAL(semanage_iface_get_name(iface_clone), "asdf");

+ 

+     ifcon2 = semanage_iface_get_ifcon(iface_clone);

+     

+     CU_ASSERT_CONTEXT_EQUAL(ifcon, ifcon2);

+ 

+     msgcon2 = semanage_iface_get_msgcon(iface_clone);

+ 

+     CU_ASSERT_CONTEXT_EQUAL(msgcon, msgcon2);

+ 

+     // cleanup

+     semanage_iface_free(iface);

+     semanage_iface_free(iface_clone);

+     cleanup_handle(SH_CONNECT);

+ }

+ 

+ // Function semanage_iface_query

+ 

+ void test_iface_query(void) {

+     semanage_iface_t *iface = NULL;

+     semanage_iface_key_t *key = NULL;

+ 

+     // setup

+     setup_handle(SH_CONNECT);

+ 

+     key = get_iface_key();

+ 

+     // test

+     CU_ASSERT(semanage_iface_query(sh, key, &iface) >= 0);

+ 

+     CU_ASSERT(iface == NULL);

+ 

+     // cleanup

+     semanage_iface_free(iface);

+     cleanup_handle(SH_CONNECT);

+ }

+ 

+ // Function semanage_iface_exists

+ 

+ void test_iface_exists(void) {

+     semanage_iface_key_t *key = NULL;

+     int response = 42;

+ 

+     // setup

+     setup_handle(SH_CONNECT);

+ 

+     key = get_iface_key();

+ 

+     // test

+     CU_ASSERT(semanage_iface_exists(sh, key, &response) >= 0);

+ 

+     CU_ASSERT(!response);

+ 

+     // cleanup

+     semanage_iface_key_free(key);

+     cleanup_handle(SH_CONNECT);

+ }

+ 

+ // Function semanage_iface_count

+ 

+ void test_iface_count(void) {

+     unsigned int count = 42;

+ 

+     // setup

+     setup_handle(SH_CONNECT);

+ 

+     // test

+     CU_ASSERT(semanage_iface_count(sh, &count) >= 0);

+ 

+     CU_ASSERT(count == IFACE_COUNT);

+ 

+     // cleanup

+     cleanup_handle(SH_CONNECT);

+ }

+ 

+ // Function semanage_iface_iterate

+ 

+ unsigned int counter_iface_iterate = 0;

+ 

+ int handler_iface_iterate(const semanage_iface_t *record, void *varg) {

+     counter_iface_iterate++;

+     return 0;

+ }

+ 

+ void test_iface_iterate(void) {

+     // setup

+     setup_handle(SH_CONNECT);

+ 

+     // test

+     semanage_iface_iterate(sh, handler_iface_iterate, NULL);

+ 

+     CU_ASSERT(counter_iface_iterate == IFACE_COUNT);

+ 

+     // cleanup

+     cleanup_handle(SH_CONNECT);

+ }

+ 

+ // Function semanage_iface_list

+ 

+ void test_iface_list(void) {

+     semanage_iface_t **records = NULL;

+     unsigned int count = 42;

+ 

+     // setup

+     setup_handle(SH_CONNECT);

+ 

+     // test

+     CU_ASSERT(semanage_iface_list(sh, &records, &count) >= 0);

+ 

+     CU_ASSERT(count == IFACE_COUNT);

+ 

+     for (unsigned int i = 0; i < count; i++) {

+         CU_ASSERT_PTR_NOT_NULL(records[i]);

+     }

+ 

+     for (unsigned int i = 0; i < count; i++) {

+         semanage_iface_free(records[i]);

+     }

+ 

+     // cleanup

+     cleanup_handle(SH_CONNECT);

+ }

+ 

+ // Function semanage_iface_modify_local, semanage_iface_del_local,

+ // semanage_iface_query_local

+ 

+ void test_iface_modify_del_query_local(void) {

+     semanage_iface_t *iface;

+     semanage_iface_t *iface_local;

+     semanage_iface_key_t *key = NULL;

+ 

+     // setup

+     setup_handle(SH_TRANS);

+ 

+     iface = get_iface();

+ 

+     CU_ASSERT(semanage_iface_key_extract(sh, iface, &key) >= 0);

+     CU_ASSERT_PTR_NOT_NULL(key);

+ 

+     // test

+     CU_ASSERT(semanage_iface_modify_local(sh, key, iface) >= 0);

+ 

+     // write changes to file

+     helper_commit();

+     helper_begin_transaction();

+ 

+     CU_ASSERT(semanage_iface_query_local(sh, key, &iface_local) >= 0);

+     CU_ASSERT_PTR_NOT_NULL_FATAL(iface_local);

+ 

+     CU_ASSERT(semanage_iface_del_local(sh, key) >= 0);

+ 

+     CU_ASSERT(semanage_iface_query_local(sh, key, &iface_local) < 0);

+ 

+     // cleanup

+     semanage_iface_free(iface);

+     cleanup_handle(SH_TRANS);

+ }

+ 

+ // Function semanage_iface_exists_local

+ 

+ void test_iface_exists_local(void) {

+     semanage_iface_t *iface1 = NULL;

+     semanage_iface_key_t *key1 = NULL;

+     semanage_iface_key_t *key2 = NULL;

+     int response = 42;

+ 

+     // setup

+     setup_handle(SH_TRANS);

+ 

+     CU_ASSERT(semanage_iface_create(sh, &iface1) >= 0);

+     CU_ASSERT(semanage_iface_set_name(sh, iface1, "asdf") >= 0);

+ 

+     CU_ASSERT(semanage_iface_key_extract(sh, iface1, &key1) >= 0);

+     CU_ASSERT_PTR_NOT_NULL(key1);

+ 

+     CU_ASSERT(semanage_iface_modify_local(sh, key1, iface1) >= 0);

+ 

+     CU_ASSERT(semanage_iface_key_create(sh, "qwerty", &key2) >= 0);

+     CU_ASSERT_PTR_NOT_NULL(key2);

+ 

+     // test existing

+     CU_ASSERT(semanage_iface_exists_local(sh, key1, &response) >= 0);

+ 

+     CU_ASSERT(response);

+ 

+     // test nonexistent

+     CU_ASSERT(semanage_iface_exists_local(sh, key2, &response) >= 0);

+ 

+     CU_ASSERT(!response);

+ 

+     // cleanup

+     CU_ASSERT(semanage_iface_del_local(sh, key1) >= 0);

+     semanage_iface_free(iface1);

+     semanage_iface_key_free(key1);

+     semanage_iface_key_free(key2);

+     cleanup_handle(SH_TRANS);

+ }

+ 

+ // Function semanage_iface_count_local

+ 

+ void test_iface_count_local(void) {

+     unsigned int count = 42;

+ 

+     // setup

+     setup_handle(SH_TRANS);

+ 

+     add_local_iface(I_FIRST);

+     add_local_iface(I_SECOND);

+     add_local_iface(I_THIRD);

+ 

+     // test

+     CU_ASSERT(semanage_iface_count_local(sh, &count) >= 0);

+ 

+     CU_ASSERT(count == 3);

+ 

+     // cleanup

+     delete_local_iface(I_FIRST);

+     delete_local_iface(I_SECOND);

+     delete_local_iface(I_THIRD);

+ 

+     cleanup_handle(SH_TRANS);

+ }

+ 

+ // Function semanage_iface_iterate_local

+ 

+ unsigned int counter_iface_iterate_local = 0;

+ 

+ int handler_iface_iterate_local(const semanage_iface_t *record, void *varg) {

+     counter_iface_iterate_local++;

+     return 0;

+ }

+ 

+ void test_iface_iterate_local(void) {

+     // setup

+     setup_handle(SH_TRANS);

+ 

+     add_local_iface(I_FIRST);

+     add_local_iface(I_SECOND);

+     add_local_iface(I_THIRD);

+ 

+     // test

+     semanage_iface_iterate_local(sh, handler_iface_iterate_local, NULL);

+ 

+     CU_ASSERT(counter_iface_iterate_local == 3);

+ 

+     // cleanup

+     delete_local_iface(I_FIRST);

+     delete_local_iface(I_SECOND);

+     delete_local_iface(I_THIRD);

+ 

+     cleanup_handle(SH_TRANS);

+ }

+ 

+ // Function semanage_iface_list_local

+ 

+ void test_iface_list_local(void) {

+     semanage_iface_t **records = NULL;

+     unsigned int count = 42;

+ 

+     // setup

+     setup_handle(SH_TRANS);

+ 

+     add_local_iface(I_FIRST);

+     add_local_iface(I_SECOND);

+     add_local_iface(I_THIRD);

+ 

+     // test

+     CU_ASSERT(semanage_iface_list_local(sh, &records, &count) >= 0);

+ 

+     CU_ASSERT(count == 3);

+ 

+     for (unsigned int i = 0; i < count; i++) {

+         CU_ASSERT_PTR_NOT_NULL(records[i]);

+     }

+ 

+     // cleanup

+     for (unsigned int i = 0; i < count; i++) {

+         semanage_iface_free(records[i]);

+     }

+ 

+     delete_local_iface(I_FIRST);

+     delete_local_iface(I_SECOND);

+     delete_local_iface(I_THIRD);

+ 

+     cleanup_handle(SH_TRANS);

+ }

@@ -0,0 +1,39 @@ 

+ #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);

+ 

+ // iface_record.h

+ 

+ void test_iface_compare(void);

+ void test_iface_compare2(void);

+ void test_iface_key_create(void);

+ void test_iface_key_extract(void);

+ void test_iface_get_set_name(void);

+ void test_iface_get_set_ifcon(void);

+ void test_iface_get_set_msgcon(void);

+ void test_iface_create(void);

+ void test_iface_clone(void);

+ 

+ // iterfaces_policy.h

+ 

+ void test_iface_query(void);

+ void test_iface_exists(void);

+ void test_iface_count(void);

+ void test_iface_iterate(void);

+ void test_iface_list(void);

+ 

+ // interfaces_local.h

+ 

+ void test_iface_modify_del_query_local(void);

+ void test_iface_exists_local(void);

+ void test_iface_count_local(void);

+ void test_iface_iterate_local(void);

+ void test_iface_list_local(void);

+ 

+ #endif

+ 

The added file is too large to be shown here, see it at: libsemanage/sanity-tests/tests/test_module.c
@@ -0,0 +1,42 @@ 

+ #ifndef __TEST_MODULE_H__

+ #define __TEST_MODULE_H__

+ 

+ #include <CUnit/Basic.h>

+ 

+ #include "functions.h"

+ 

+ int module_test_init(void);

+ int module_test_cleanup(void);

+ int module_add_tests(CU_pSuite suite);

+ 

+ // modules.h

+ 

+ void test_module_list(void);

+ 

+ #if VERS_CHECK(2, 5)

+ void test_module_install(void);

+ void test_module_install_file(void);

+ void test_module_install_info(void);

+ void test_module_remove(void);

+ void test_module_remove_key(void);

+ 

+ void test_module_extract(void);

+ void test_module_list_all(void);

+ 

+ void test_module_get_name(void);

+ void test_module_get_module_info(void);

+ 

+ void test_module_info_create(void);

+ void test_module_info_get_set_priority(void);

+ void test_module_info_get_set_name(void);

+ void test_module_info_get_set_lang_ext(void);

+ void test_module_info_get_set_enabled(void);

+ 

+ void test_module_key_create(void);

+ void test_module_key_get_set_name(void);

+ void test_module_key_get_set_priority(void);

+ void test_module_get_set_enabled(void);

+ #endif

+ 

+ #endif

+ 

@@ -0,0 +1,697 @@ 

+ #include <stdio.h>

+ #include <semanage/semanage.h>

+ #include <semanage/handle.h>

+ #include <CUnit/Basic.h>

+ 

+ #include "test_node.h"

+ #include "functions.h"

+ 

+ extern semanage_handle_t *sh;

+ 

+ // TODO: current policies does not have any nodecon rules

+ #define NODE_COUNT 0

+ 

+ int node_test_init(void) {

+     return 0;

+ }

+ 

+ int node_test_cleanup(void) {

+     return 0;

+ }

+ 

+ int node_add_tests(CU_pSuite suite) {

+     CU_add_test(suite, "node_compare", test_node_compare);

+     CU_add_test(suite, "node_compare2", test_node_compare2);

+     CU_add_test(suite, "node_key_create", test_node_key_create);

+     CU_add_test(suite, "node_key_extract", test_node_key_extract);

+     CU_add_test(suite, "node_get_set_addr", test_node_get_set_addr);

+     CU_add_test(suite, "node_get_set_addr_bytes", test_node_get_set_addr_bytes);

+     CU_add_test(suite, "node_get_set_mask", test_node_get_set_mask);

+     CU_add_test(suite, "node_get_set_mask_bytes", test_node_get_set_mask_bytes);

+     CU_add_test(suite, "node_get_set_proto", test_node_get_set_proto);

+     CU_add_test(suite, "node_get_proto_str", test_node_get_proto_str);

+     CU_add_test(suite, "node_get_set_con", test_node_get_set_con);

+     CU_add_test(suite, "node_create", test_node_create);

+     CU_add_test(suite, "node_clone", test_node_clone);

+ 

+     CU_add_test(suite, "node_query", test_node_query);

+     CU_add_test(suite, "node_exists", test_node_exists);

+     CU_add_test(suite, "node_count", test_node_count);

+     CU_add_test(suite, "node_iterate", test_node_iterate);

+     CU_add_test(suite, "node_list", test_node_list);

+ 

+     CU_add_test(suite, "node_modify_del_query_local",

+                 test_node_modify_del_query_local);

+     CU_add_test(suite, "node_exists_local", test_node_exists_local);

+     CU_add_test(suite, "node_count_local", test_node_count_local);

+     CU_add_test(suite, "node_iterate_local", test_node_iterate_local);

+     CU_add_test(suite, "node_list_local", test_node_list_local);

+ 

+     return 0;

+ }

+ 

+ // Function semanage_node_compare

+ 

+ void test_node_compare(void) {

+     semanage_node_t *node = NULL;

+     semanage_node_key_t *key1 = NULL;

+     semanage_node_key_t *key2 = NULL;

+     int result = 42;

+ 

+     // setup

+     setup_handle(SH_CONNECT);

+ 

+     node = get_node();

+ 

+     key1 = get_node_key();

+ 

+     CU_ASSERT(semanage_node_key_create(sh, "192.168.0.1", "255.255.0.0",

+                                        SEMANAGE_PROTO_IP4, &key2) >= 0);

+     CU_ASSERT_PTR_NOT_NULL(key2);

+ 

+     // test

+     result = semanage_node_compare(node, key1);

+ 

+     CU_ASSERT(result == 0);

+ 

+     result = semanage_node_compare(node, key2);

+ 

+     CU_ASSERT(result != 0);

+ 

+     // cleanup

+     semanage_node_free(node);

+     semanage_node_key_free(key1);

+     semanage_node_key_free(key2);

+     cleanup_handle(SH_CONNECT);

+ }

+ 

+ // Function semanage_node_compare2

+ 

+ void test_node_compare2(void) {

+     semanage_node_t *node1 = NULL;

+     semanage_node_t *node2 = NULL;

+     semanage_node_t *node3 = NULL;

+     int result = 42;

+ 

+     // setup

+     setup_handle(SH_CONNECT);

+ 

+     node1 = get_node();

+     node2 = get_node();

+ 

+     node3 = get_node();

+     CU_ASSERT(semanage_node_set_addr(sh, node3, SEMANAGE_PROTO_IP4,

+                                      "192.168.0.1") >= 0);

+ 

+     // test

+     result = semanage_node_compare2(node1, node2);

+ 

+     CU_ASSERT(result == 0);

+ 

+     result = semanage_node_compare2(node1, node3);

+ 

+     CU_ASSERT(result != 0);

+ 

+     // cleanup

+     semanage_node_free(node1);

+     semanage_node_free(node2);

+     semanage_node_free(node3);

+     cleanup_handle(SH_CONNECT);

+ }

+ 

+ // Function semanage_node_key_create

+ 

+ void test_node_key_create(void) {

+     semanage_node_key_t *key = NULL;

+ 

+     setup_handle(SH_CONNECT);

+ 

+     CU_ASSERT(semanage_node_key_create(sh, "127.0.0.1", "255.255.255.255",

+                                        SEMANAGE_PROTO_IP4, &key) >= 0);

+     CU_ASSERT_PTR_NOT_NULL(key);

+ 

+     semanage_node_key_free(key);

+     cleanup_handle(SH_CONNECT);

+ }

+ 

+ // Function semanage_node_key_extract

+ 

+ void test_node_key_extract(void) {

+     semanage_node_t *node = NULL;

+     semanage_node_key_t *key = NULL;

+ 

+     // setup

+     setup_handle(SH_CONNECT);

+ 

+     node = get_node();

+ 

+     // test

+     CU_ASSERT(semanage_node_key_extract(sh, node, &key) >= 0);

+     CU_ASSERT_PTR_NOT_NULL(key);

+ 

+     // cleanup

+     semanage_node_free(node);

+     semanage_node_key_free(key);

+     cleanup_handle(SH_CONNECT);

+ }

+ 

+ // Function semanage_node_get_addr, semanage_node_set_addr

+ 

+ void test_node_get_set_addr(void) {

+     semanage_node_t *node = NULL;

+     char *addr = NULL;

+ 

+     // setup

+     setup_handle(SH_CONNECT);

+ 

+     CU_ASSERT(semanage_node_create(sh, &node) >= 0);

+ 

+     // test

+     CU_ASSERT(semanage_node_set_addr(sh, node, SEMANAGE_PROTO_IP4,

+                                      "192.168.0.1") == 0);

+ 

+     CU_ASSERT(semanage_node_get_addr(sh, node, &addr) >= 0);

+     CU_ASSERT_PTR_NOT_NULL(addr);

+     CU_ASSERT_STRING_EQUAL(addr, "192.168.0.1");

+ 

+     // cleanup

+     semanage_node_free(node);

+     cleanup_handle(SH_CONNECT);

+ }

+ 

+ // Function semanage_node_get_addr_bytes, semanage_node_set_addr_bytes

+ 

+ void test_node_get_set_addr_bytes(void) {

+     semanage_node_t *node = NULL;

+     char addr1[] = { 192, 168, 0, 1 };

+     size_t addr1_size = sizeof(addr1);

+     char *addr2 = NULL;

+     size_t addr2_size = 0;

+ 

+     // setup

+     setup_handle(SH_CONNECT);

+ 

+     CU_ASSERT(semanage_node_create(sh, &node) >= 0);

+ 

+     // test

+     CU_ASSERT(semanage_node_set_addr_bytes(sh, node, addr1, addr1_size) == 0);

+ 

+     CU_ASSERT(semanage_node_get_addr_bytes(sh, node, &addr2, &addr2_size) >= 0);

+     CU_ASSERT_PTR_NOT_NULL(addr2);

+ 

+     for (size_t i = 0; i < addr2_size; i++) {

+         CU_ASSERT(addr1[i] == addr2[i]);

+     }

+ 

+     // cleanup

+     semanage_node_free(node);

+     cleanup_handle(SH_CONNECT);

+ }

+ 

+ // Function semanage_node_get_mask, semanage_node_set_mask

+ 

+ void test_node_get_set_mask(void) {

+     semanage_node_t *node = NULL;

+     char *mask = NULL;

+ 

+     // setup

+     setup_handle(SH_CONNECT);

+ 

+     CU_ASSERT(semanage_node_create(sh, &node) >= 0);

+ 

+     // test

+     CU_ASSERT(semanage_node_set_mask(sh, node, SEMANAGE_PROTO_IP4,

+                                      "255.255.255.0") == 0);

+ 

+     CU_ASSERT(semanage_node_get_mask(sh, node, &mask) >= 0);

+     CU_ASSERT_PTR_NOT_NULL(mask);

+     CU_ASSERT_STRING_EQUAL(mask, "255.255.255.0");

+ 

+     // cleanup

+     semanage_node_free(node);

+     cleanup_handle(SH_CONNECT);

+ }

+ 

+ // Function semanage_node_get_mask_bytes, semanage_node_set_mask_bytes

+ 

+ void test_node_get_set_mask_bytes(void) {

+     semanage_node_t *node = NULL;

+     char mask1[] = { 255, 255, 255, 0 };

+     size_t mask1_size = sizeof(mask1);

+     char *mask2 = NULL;

+     size_t mask2_size = 0;

+ 

+     // setup

+     setup_handle(SH_CONNECT);

+ 

+     CU_ASSERT(semanage_node_create(sh, &node) >= 0);

+ 

+     // test

+     CU_ASSERT(semanage_node_set_mask_bytes(sh, node, mask1, mask1_size) == 0);

+ 

+     CU_ASSERT(semanage_node_get_mask_bytes(sh, node, &mask2, &mask2_size) >= 0);

+     CU_ASSERT_PTR_NOT_NULL(mask2);

+ 

+     for (size_t i = 0; i < mask2_size; i++) {

+         CU_ASSERT(mask1[i] == mask2[i]);

+     }

+ 

+     // cleanup

+     semanage_node_free(node);

+     cleanup_handle(SH_CONNECT);

+ }

+ 

+ // Function semanage_node_get_proto, semanage_node_set_proto

+ 

+ void test_node_get_set_proto(void) {

+     semanage_node_t *node = NULL;

+ 

+     // setup

+     setup_handle(SH_CONNECT);

+ 

+     CU_ASSERT(semanage_node_create(sh, &node) >= 0);

+ 

+     // test

+     semanage_node_set_proto(node, SEMANAGE_PROTO_IP4);

+ 

+     CU_ASSERT(semanage_node_get_proto(node) == SEMANAGE_PROTO_IP4);

+ 

+     // cleanup

+     semanage_node_free(node);

+     cleanup_handle(SH_CONNECT);

+ }

+ 

+ // Function semanage_node_get_proto_str

+ 

+ void test_node_get_proto_str(void) {

+     CU_ASSERT_STRING_EQUAL(semanage_node_get_proto_str(SEMANAGE_PROTO_IP4),

+                            "ipv4");

+     CU_ASSERT_STRING_EQUAL(semanage_node_get_proto_str(SEMANAGE_PROTO_IP6),

+                            "ipv6");

+ }

+ 

+ // Function semanage_node_get_con, semanage_node_set_con

+ 

+ void test_node_get_set_con(void) {

+     semanage_node_t *node = NULL;

+     semanage_context_t *con1 = NULL;

+     semanage_context_t *con2 = NULL;

+ 

+     // setup

+     setup_handle(SH_CONNECT);

+ 

+     CU_ASSERT(semanage_node_create(sh, &node) >= 0);

+ 

+     CU_ASSERT(semanage_context_from_string(sh,

+                                "my_user_u:my_role_r:my_type_t:s0", &con1) >= 0);

+ 

+     // test

+     CU_ASSERT(semanage_node_set_con(sh, node, con1) == 0);

+ 

+     con2 = semanage_node_get_con(node);

+ 

+     CU_ASSERT_CONTEXT_EQUAL(con1, con2);

+ 

+     // cleanup

+     semanage_node_free(node);

+     cleanup_handle(SH_CONNECT);

+ }

+ 

+ // Function semanage_node_create

+ 

+ void test_node_create(void) {

+     semanage_node_t *node = NULL;

+     semanage_context_t *con = NULL;

+ 

+     // setup

+     setup_handle(SH_CONNECT);

+ 

+     // test

+     CU_ASSERT(semanage_node_create(sh, &node) >= 0);

+ 

+     CU_ASSERT(semanage_node_set_addr(sh, node, SEMANAGE_PROTO_IP4,

+                                      "127.0.0.1") >= 0);

+ 

+     CU_ASSERT(semanage_node_set_mask(sh, node, SEMANAGE_PROTO_IP4,

+                                      "255.255.255.0") >= 0);

+ 

+     semanage_node_set_proto(node, SEMANAGE_PROTO_IP4);

+ 

+     CU_ASSERT(semanage_context_from_string(sh, "user_u:role_r:type_t:s0",

+                                            &con) >= 0);

+     CU_ASSERT(semanage_node_set_con(sh, node, con) >= 0);

+ 

+     // cleanup

+     semanage_node_free(node);

+     cleanup_handle(SH_CONNECT);

+ }

+ 

+ // Function semanage_node_clone

+ 

+ void test_node_clone(void) {

+     semanage_node_t *node = NULL;

+     semanage_node_t *node_clone = NULL;

+     semanage_context_t *con = NULL;

+     semanage_context_t *con2 = NULL;

+     const char *addr1 = "127.0.0.1";

+     char *addr2 = NULL;

+     const char *mask1 = "255.255.255.0";

+     char *mask2 = NULL;

+ 

+     // setup

+     setup_handle(SH_CONNECT);

+ 

+     CU_ASSERT(semanage_node_create(sh, &node) >= 0);

+ 

+     CU_ASSERT(semanage_node_set_addr(sh, node, SEMANAGE_PROTO_IP4,

+                                      addr1) >= 0);

+ 

+     CU_ASSERT(semanage_node_set_mask(sh, node, SEMANAGE_PROTO_IP4,

+                                      mask1) >= 0);

+ 

+     semanage_node_set_proto(node, SEMANAGE_PROTO_IP4);

+ 

+     CU_ASSERT(semanage_context_from_string(sh, "user_u:role_r:type_t:s0",

+                                            &con) >= 0);

+     CU_ASSERT(semanage_node_set_con(sh, node, con) >= 0);

+ 

+     // test

+     CU_ASSERT(semanage_node_clone(sh, node, &node_clone) >= 0);

+ 

+     CU_ASSERT(semanage_node_get_addr(sh, node_clone, &addr2) >= 0);

+     CU_ASSERT_PTR_NOT_NULL(addr2);

+     CU_ASSERT_STRING_EQUAL(addr1, addr2);

+ 

+     CU_ASSERT(semanage_node_get_mask(sh, node_clone, &mask2) >= 0);

+     CU_ASSERT_PTR_NOT_NULL(mask2);

+     CU_ASSERT_STRING_EQUAL(mask1, mask2);

+ 

+     CU_ASSERT(semanage_node_get_proto(node_clone) == SEMANAGE_PROTO_IP4);

+ 

+     con2 = semanage_node_get_con(node_clone);

+     

+     CU_ASSERT_CONTEXT_EQUAL(con, con2);

+ 

+     // cleanup

+     semanage_node_free(node);

+     semanage_node_free(node_clone);

+     cleanup_handle(SH_CONNECT);

+ }

+ 

+ // Function semanage_node_query

+ 

+ void test_node_query(void) {

+     semanage_node_t *node = NULL;

+     semanage_node_key_t *key = NULL;

+ 

+     // setup

+     setup_handle(SH_CONNECT);

+ 

+     key = get_node_key();

+ 

+     // test

+     CU_ASSERT(semanage_node_query(sh, key, &node) >= 0);

+ 

+     CU_ASSERT(node == NULL);

+ 

+     // cleanup

+     semanage_node_free(node);

+     cleanup_handle(SH_CONNECT);

+ }

+ 

+ // Function semanage_node_exists

+ 

+ void test_node_exists(void) {

+     semanage_node_key_t *key = NULL;

+     int response = 42;

+ 

+     // setup

+     setup_handle(SH_CONNECT);

+ 

+     key = get_node_key();

+ 

+     // test

+     CU_ASSERT(semanage_node_exists(sh, key, &response) >= 0);

+ 

+     CU_ASSERT(!response);

+ 

+     // cleanup

+     semanage_node_key_free(key);

+     cleanup_handle(SH_CONNECT);

+ }

+ 

+ // Function semanage_node_count

+ 

+ void test_node_count(void) {

+     unsigned int count = 42;

+ 

+     // setup

+     setup_handle(SH_CONNECT);

+ 

+     // test

+     CU_ASSERT(semanage_node_count(sh, &count) >= 0);

+ 

+     CU_ASSERT(count == NODE_COUNT);

+ 

+     // cleanup

+     cleanup_handle(SH_CONNECT);

+ }

+ 

+ // Function semanage_node_iterate

+ 

+ unsigned int counter_node_iterate = 0;

+ 

+ int handler_node_iterate(const semanage_node_t *record, void *varg) {

+     counter_node_iterate++;

+     return 0;

+ }

+ 

+ void test_node_iterate(void) {

+     // setup

+     setup_handle(SH_CONNECT);

+ 

+     // test

+     semanage_node_iterate(sh, handler_node_iterate, NULL);

+ 

+     CU_ASSERT(counter_node_iterate == NODE_COUNT);

+ 

+     // cleanup

+     cleanup_handle(SH_CONNECT);

+ }

+ 

+ // Function semanage_node_list

+ 

+ void test_node_list(void) {

+     semanage_node_t **records = NULL;

+     unsigned int count = 42;

+ 

+     // setup

+     setup_handle(SH_CONNECT);

+ 

+     // test

+     CU_ASSERT(semanage_node_list(sh, &records, &count) >= 0);

+ 

+     CU_ASSERT(count == NODE_COUNT);

+ 

+     for (unsigned int i = 0; i < count; i++) {

+         CU_ASSERT_PTR_NOT_NULL(records[i]);

+     }

+ 

+     for (unsigned int i = 0; i < count; i++) {

+         semanage_node_free(records[i]);

+     }

+ 

+     // cleanup

+     cleanup_handle(SH_CONNECT);

+ }

+ 

+ // Function semanage_node_modify_local, semanage_node_del_local,

+ // semanage_node_query_local

+ 

+ void test_node_modify_del_query_local(void) {

+     semanage_node_t *node;

+     semanage_node_t *node_local;

+     semanage_node_t *node_tmp;

+     semanage_node_key_t *key = NULL;

+     semanage_node_key_t *key_tmp = NULL;

+ 

+     // setup

+     setup_handle(SH_TRANS);

+ 

+     node = get_node();

+ 

+     CU_ASSERT(semanage_node_key_extract(sh, node, &key) >= 0);

+     CU_ASSERT_PTR_NOT_NULL(key);

+ 

+     // add second record, so that semanage_node_compare2_qsort will be called

+     node_tmp = get_node();

+ 

+     CU_ASSERT(semanage_node_set_addr(sh, node_tmp, SEMANAGE_PROTO_IP4,

+                                      "10.0.0.1") >= 0);

+ 

+     CU_ASSERT(semanage_node_key_extract(sh, node_tmp, &key_tmp) >= 0);

+     CU_ASSERT_PTR_NOT_NULL(key_tmp);

+ 

+     // test

+     CU_ASSERT(semanage_node_modify_local(sh, key, node) >= 0);

+     CU_ASSERT(semanage_node_modify_local(sh, key_tmp, node_tmp) >= 0);

+ 

+     // write changes to file

+     helper_commit();

+     helper_begin_transaction();

+ 

+     CU_ASSERT(semanage_node_query_local(sh, key, &node_local) >= 0);

+     CU_ASSERT_PTR_NOT_NULL_FATAL(node_local);

+ 

+     CU_ASSERT(semanage_node_del_local(sh, key) >= 0);

+     CU_ASSERT(semanage_node_del_local(sh, key_tmp) >= 0);

+ 

+     CU_ASSERT(semanage_node_query_local(sh, key, &node_local) < 0);

+ 

+     // cleanup

+     semanage_node_free(node);

+     semanage_node_free(node_tmp);

+     cleanup_handle(SH_TRANS);

+ }

+ 

+ // Function semanage_node_exists_local

+ 

+ void test_node_exists_local(void) {

+     semanage_node_t *node = NULL;

+     semanage_node_key_t *key1 = NULL;

+     semanage_node_key_t *key2 = NULL;

+     semanage_context_t *con = NULL;

+     int response = 42;

+ 

+     // setup

+     setup_handle(SH_TRANS);

+ 

+     CU_ASSERT(semanage_node_create(sh, &node) >= 0);

+ 

+     CU_ASSERT(semanage_node_set_addr(sh, node, SEMANAGE_PROTO_IP4,

+                                      "127.0.0.1") >= 0);

+ 

+     CU_ASSERT(semanage_node_set_mask(sh, node, SEMANAGE_PROTO_IP4,

+                                      "255.255.255.0") >= 0);

+ 

+     semanage_node_set_proto(node, SEMANAGE_PROTO_IP4);

+ 

+     CU_ASSERT(semanage_context_from_string(sh, "user_u:role_r:type_t:s0",

+                                            &con) >= 0);

+     CU_ASSERT(semanage_node_set_con(sh, node, con) >= 0);

+ 

+     CU_ASSERT(semanage_node_key_extract(sh, node, &key1) >= 0);

+     CU_ASSERT_PTR_NOT_NULL(key1);

+ 

+     CU_ASSERT(semanage_node_modify_local(sh, key1, node) >= 0);

+ 

+     CU_ASSERT(semanage_node_key_create(sh, "192.168.0.1", "255.255.0.0",

+                                        SEMANAGE_PROTO_IP4, &key2) >= 0);

+     CU_ASSERT_PTR_NOT_NULL(key2);

+ 

+     // test existing

+     CU_ASSERT(semanage_node_exists_local(sh, key1, &response) >= 0);

+ 

+     CU_ASSERT(response);

+ 

+     // test nonexistent

+     CU_ASSERT(semanage_node_exists_local(sh, key2, &response) >= 0);

+ 

+     CU_ASSERT(!response);

+ 

+     // cleanup

+     CU_ASSERT(semanage_node_del_local(sh, key1) >= 0);

+     semanage_node_free(node);

+     semanage_node_key_free(key1);

+     semanage_node_key_free(key2);

+     cleanup_handle(SH_TRANS);

+ }

+ 

+ // Function semanage_node_count_local

+ 

+ void test_node_count_local(void) {

+     unsigned int count = 42;

+ 

+     // setup

+     setup_handle(SH_TRANS);

+ 

+     add_local_node(I_FIRST);

+     add_local_node(I_SECOND);

+     add_local_node(I_THIRD);

+ 

+     // test

+     CU_ASSERT(semanage_node_count_local(sh, &count) >= 0);

+ 

+     CU_ASSERT(count == 3);

+ 

+     // cleanup

+     delete_local_node(I_FIRST);

+     delete_local_node(I_SECOND);

+     delete_local_node(I_THIRD);

+ 

+     cleanup_handle(SH_TRANS);

+ }

+ 

+ // Function semanage_node_iterate_local

+ 

+ unsigned int counter_node_iterate_local = 0;

+ 

+ int handler_node_iterate_local(const semanage_node_t *record, void *varg) {

+     counter_node_iterate_local++;

+     return 0;

+ }

+ 

+ void test_node_iterate_local(void) {

+     // setup

+     setup_handle(SH_TRANS);

+ 

+     add_local_node(I_FIRST);

+     add_local_node(I_SECOND);

+     add_local_node(I_THIRD);

+ 

+     // test

+     semanage_node_iterate_local(sh, handler_node_iterate_local, NULL);

+ 

+     CU_ASSERT(counter_node_iterate_local == 3);

+ 

+     // cleanup

+     delete_local_node(I_FIRST);

+     delete_local_node(I_SECOND);

+     delete_local_node(I_THIRD);

+ 

+     cleanup_handle(SH_TRANS);

+ }

+ 

+ // Function semanage_node_list_local

+ 

+ void test_node_list_local(void) {

+     semanage_node_t **records = NULL;

+     unsigned int count = 42;

+ 

+     // setup

+     setup_handle(SH_TRANS);

+ 

+     add_local_node(I_FIRST);

+     add_local_node(I_SECOND);

+     add_local_node(I_THIRD);

+ 

+     // test

+     CU_ASSERT(semanage_node_list_local(sh, &records, &count) >= 0);

+ 

+     CU_ASSERT(count == 3);

+ 

+     for (unsigned int i = 0; i < count; i++) {

+         CU_ASSERT_PTR_NOT_NULL(records[i]);

+     }

+ 

+     // cleanup

+     for (unsigned int i = 0; i < count; i++) {

+         semanage_node_free(records[i]);

+     }

+ 

+     delete_local_node(I_FIRST);

+     delete_local_node(I_SECOND);

+     delete_local_node(I_THIRD);

+ 

+     cleanup_handle(SH_TRANS);

+ }

@@ -0,0 +1,43 @@ 

+ #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);

+ 

+ // node_record.h

+ 

+ void test_node_compare(void);

+ void test_node_compare2(void);

+ void test_node_key_create(void);

+ void test_node_key_extract(void);

+ void test_node_get_set_addr(void);

+ void test_node_get_set_addr_bytes(void);

+ void test_node_get_set_mask(void);

+ void test_node_get_set_mask_bytes(void);

+ void test_node_get_set_proto(void);

+ void test_node_get_proto_str(void);

+ void test_node_get_set_con(void);

+ void test_node_create(void);

+ void test_node_clone(void);

+ 

+ // nodes_policy.h

+ 

+ void test_node_query(void);

+ void test_node_exists(void);

+ void test_node_count(void);

+ void test_node_iterate(void);

+ void test_node_list(void);

+ 

+ // nodes_local.h

+ 

+ void test_node_modify_del_query_local(void);

+ void test_node_exists_local(void);

+ void test_node_count_local(void);

+ void test_node_iterate_local(void);

+ void test_node_list_local(void);

+ 

+ #endif

+ 

@@ -0,0 +1,108 @@ 

+ #include <stdio.h>

+ #include <semanage/semanage.h>

+ #include <semanage/handle.h>

+ #include <CUnit/Basic.h>

+ 

+ #include "test_other.h"

+ #include "functions.h"

+ 

+ extern semanage_handle_t *sh;

+ 

+ int other_test_init(void) {

+ 	return 0;

+ }

+ 

+ int other_test_cleanup(void) {

+ 	return 0;

+ }

+ 

+ int other_add_tests(CU_pSuite suite) {

+     CU_add_test(suite, "semanage_context", test_semanage_context);

+ 

+ #if VERS_CHECK(2, 5)

+     CU_add_test(suite, "debug", test_debug);

+ #endif

+ 

+ 	return 0;

+ }

+ 

+ // Function semanage_context_get_user, semanage_context_set_user,

+ // semanage_context_get_role, semanage_context_set_role,

+ // semanage_context_get_type, semanage_context_set_type,

+ // semanage_context_get_mls, semanage_context_set_mls,

+ // semanage_context_create, semanage_context_clone,

+ // semanage_context_free, semanage_context_from_string

+ // semanage_context_to_string

+ 

+ void test_semanage_context(void) {

+     semanage_context_t *con = NULL;

+     semanage_context_t *con_clone = NULL;

+     char *str = NULL;

+ 

+     // setup

+     setup_handle(SH_CONNECT);

+ 

+     // test

+     CU_ASSERT(semanage_context_create(sh, &con) >= 0);

+ 

+     CU_ASSERT(semanage_context_set_user(sh, con, "user_u") >= 0);

+     CU_ASSERT_STRING_EQUAL(semanage_context_get_user(con), "user_u");

+ 

+     CU_ASSERT(semanage_context_set_role(sh, con, "role_r") >= 0);

+     CU_ASSERT_STRING_EQUAL(semanage_context_get_role(con), "role_r");

+     

+     CU_ASSERT(semanage_context_set_type(sh, con, "type_t") >= 0);

+     CU_ASSERT_STRING_EQUAL(semanage_context_get_type(con), "type_t");

+     

+     CU_ASSERT(semanage_context_set_mls(sh, con, "s0") >= 0);

+     CU_ASSERT_STRING_EQUAL(semanage_context_get_mls(con), "s0");

+ 

+     CU_ASSERT(semanage_context_to_string(sh, con, &str) >= 0);

+     CU_ASSERT_PTR_NOT_NULL(str);

+     CU_ASSERT_STRING_EQUAL(str, "user_u:role_r:type_t:s0");

+ 

+     CU_ASSERT(semanage_context_from_string(sh, "my_u:my_r:my_t:s0",

+                                            &con) >= 0);

+     

+     CU_ASSERT_STRING_EQUAL(semanage_context_get_user(con), "my_u");

+     CU_ASSERT_STRING_EQUAL(semanage_context_get_role(con), "my_r");

+     CU_ASSERT_STRING_EQUAL(semanage_context_get_type(con), "my_t");

+     CU_ASSERT_STRING_EQUAL(semanage_context_get_mls(con), "s0");

+ 

+     CU_ASSERT(semanage_context_clone(sh, con, &con_clone) >= 0);

+ 

+     CU_ASSERT_STRING_EQUAL(semanage_context_get_user(con_clone), "my_u");

+     CU_ASSERT_STRING_EQUAL(semanage_context_get_role(con_clone), "my_r");

+     CU_ASSERT_STRING_EQUAL(semanage_context_get_type(con_clone), "my_t");

+     CU_ASSERT_STRING_EQUAL(semanage_context_get_mls(con_clone), "s0");

+ 

+     // cleanup

+     semanage_context_free(con);

+     semanage_context_free(con_clone);

+     cleanup_handle(SH_CONNECT);

+ }

+ 

+ // Function semanage_msg_default_handler

+ 

+ #if VERS_CHECK(2, 5)

+ 

+ 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);

+ }

+ 

+ #endif

@@ -0,0 +1,25 @@ 

+ #ifndef __TEST_OTHER_H__

+ #define __TEST_OTHER_H__

+ 

+ #include <CUnit/Basic.h>

+ 

+ #include "functions.h"

+ 

+ int other_test_init(void);

+ int other_test_cleanup(void);

+ int other_add_tests(CU_pSuite suite);

+ 

+ // context_record.h

+ 

+ void test_semanage_context(void);

+ 

+ // debug.h

+ 

+ #if VERS_CHECK(2, 5)

+ 

+ void test_debug(void);

+ 

+ #endif

+ 

+ #endif

+ 

@@ -0,0 +1,834 @@ 

+ #include <stdio.h>

+ #include <semanage/semanage.h>

+ #include <semanage/handle.h>

+ #include <CUnit/Basic.h>

+ 

+ #include "test_port.h"

+ #include "functions.h"

+ 

+ extern semanage_handle_t *sh;

+ 

+ int port_test_init(void) {

+ 	return 0;

+ }

+ 

+ int port_test_cleanup(void) {

+ 	return 0;

+ }

+ 

+ int port_add_tests(CU_pSuite suite) {

+     CU_add_test(suite, "port_compare", test_port_compare);

+     CU_add_test(suite, "port_compare2", test_port_compare2);

+     CU_add_test(suite, "port_key_create", test_port_key_create);

+     CU_add_test(suite, "port_key_extract", test_port_key_extract);

+     CU_add_test(suite, "port_get_set_proto", test_port_get_set_proto);

+     CU_add_test(suite, "port_get_proto_str", test_port_get_proto_str);

+     CU_add_test(suite, "port_get_set_port", test_port_get_set_port);

+     CU_add_test(suite, "port_get_set_con", test_port_get_set_con);

+     CU_add_test(suite, "port_create", test_port_create);

+     CU_add_test(suite, "port_clone", test_port_clone);

+ 

+     CU_add_test(suite, "port_query", test_port_query);

+     CU_add_test(suite, "port_exists", test_port_exists);

+     CU_add_test(suite, "port_count", test_port_count);

+     CU_add_test(suite, "port_iterate", test_port_iterate);

+     CU_add_test(suite, "port_list", test_port_list);

+ 

+     CU_add_test(suite, "port_modify_del_local", test_port_modify_del_local);

+     CU_add_test(suite, "port_query_local", test_port_query_local);

+     CU_add_test(suite, "port_exists_local", test_port_exists_local);

+     CU_add_test(suite, "port_count_local", test_port_count_local);

+     CU_add_test(suite, "port_iterate_local", test_port_iterate_local);

+     CU_add_test(suite, "port_list_local", test_port_list_local);

+ 

+     CU_add_test(suite, "port_validate_local", test_port_validate_local);

+ 

+ 	return 0;

+ }

+ 

+ // Function semanage_port_compare

+ 

+ void helper_port_compare(int index1, int index2) {

+     semanage_port_t *port = NULL;

+     semanage_port_key_t *key = NULL;

+     int result = 42;

+ 

+     // setup

+     setup_handle(SH_CONNECT);

+ 

+     port = get_port_nth(index1);

+     key = get_port_key_nth(index2);

+ 

+     // test

+     result = semanage_port_compare(port, key);

+ 

+     if (index1 == index2) {

+         CU_ASSERT(result == 0);

+     }

+     else {

+         CU_ASSERT(result != 0);

+     }

+ 

+     // cleanup

+     semanage_port_free(port);

+     semanage_port_key_free(key);

+     cleanup_handle(SH_CONNECT);

+ }

+ 

+ void test_port_compare(void) {

+     helper_port_compare(I_FIRST,  I_FIRST);

+     helper_port_compare(I_FIRST,  I_SECOND);

+     helper_port_compare(I_SECOND, I_FIRST);

+     helper_port_compare(I_SECOND, I_SECOND);

+ 

+     CU_ASSERT_SIGNAL(helper_port_compare(I_NULL,  I_FIRST), SIGSEGV);

+     CU_ASSERT_SIGNAL(helper_port_compare(I_FIRST, I_NULL), SIGSEGV);

+     CU_ASSERT_SIGNAL(helper_port_compare(I_NULL,  I_NULL), SIGSEGV);

+ }

+ 

+ // Function semanage_port_compare2

+ 

+ void helper_port_compare2(int index1, int index2) {

+     semanage_port_t *port1 = NULL;

+     semanage_port_t *port2 = NULL;

+     int result = 42;

+ 

+     // setup

+     setup_handle(SH_CONNECT);

+ 

+     port1 = get_port_nth(index1);

+     port2 = get_port_nth(index2);

+ 

+     // test

+     result = semanage_port_compare2(port1, port2);

+ 

+     if (index1 == index2) {

+         CU_ASSERT(result == 0);

+     }

+     else {

+         CU_ASSERT(result != 0);

+     }

+ 

+     // cleanup

+     semanage_port_free(port1);

+     semanage_port_free(port2);

+     cleanup_handle(SH_CONNECT);

+ }

+ 

+ void test_port_compare2(void) {

+     helper_port_compare2(I_FIRST,  I_FIRST);

+     helper_port_compare2(I_FIRST,  I_SECOND);

+     helper_port_compare2(I_SECOND, I_FIRST);

+     helper_port_compare2(I_SECOND, I_SECOND);

+ 

+     CU_ASSERT_SIGNAL(helper_port_compare2(I_NULL,  I_FIRST), SIGSEGV);

+     CU_ASSERT_SIGNAL(helper_port_compare2(I_FIRST, I_NULL), SIGSEGV);

+     CU_ASSERT_SIGNAL(helper_port_compare2(I_NULL,  I_NULL), SIGSEGV);

+ }

+ 

+ // Function semanage_port_create

+ 

+ void test_port_key_create(void) {

+     semanage_port_key_t *key = NULL;

+ 

+     setup_handle(SH_CONNECT);

+ 

+     CU_ASSERT(semanage_port_key_create(sh, 1000, 1200, 0, &key) >= 0);

+     CU_ASSERT_PTR_NOT_NULL(key);

+ 

+     semanage_port_key_free(key);

+     cleanup_handle(SH_CONNECT);

+ }

+ 

+ // Function semanage_port_extract

+ 

+ void test_port_key_extract(void) {

+     semanage_port_t *port = NULL;

+     semanage_port_key_t *key = NULL;

+ 

+     // setup

+     setup_handle(SH_CONNECT);

+ 

+     port = get_port_nth(I_FIRST);

+ 

+     // test

+     CU_ASSERT(semanage_port_key_extract(sh, port, &key) >= 0);

+     CU_ASSERT_PTR_NOT_NULL(key);

+ 

+     // cleanup

+     semanage_port_free(port);

+     semanage_port_key_free(key);

+     cleanup_handle(SH_CONNECT);

+ }

+ 

+ // Function semanage_port_get_proto, semanage_port_set_proto

+ 

+ void helper_port_get_set_proto(int index) {

+     semanage_port_t *port = NULL;

+ 

+     // setup

+     setup_handle(SH_CONNECT);

+ 

+     port = get_port_nth(index);

+ 

+     // test

+     semanage_port_set_proto(port, 0);

+ 

+     CU_ASSERT(semanage_port_get_proto(port) == 0);

+ 

+     semanage_port_set_proto(port, 1);

+ 

+     CU_ASSERT(semanage_port_get_proto(port) == 1);

+ 

+     // cleanup

+     semanage_port_free(port);

+     cleanup_handle(SH_CONNECT);

+ }

+ 

+ void test_port_get_set_proto(void) {

+     helper_port_get_set_proto(I_FIRST);

+     helper_port_get_set_proto(I_SECOND);

+ }

+ 

+ // Function semanage_port_get_proto_str

+ 

+ void test_port_get_proto_str(void) {

+     const char *str = NULL;

+ 

+     str = semanage_port_get_proto_str(-1);

+     CU_ASSERT_STRING_EQUAL(str, "???");

+ 

+     str = semanage_port_get_proto_str(0);

+     CU_ASSERT_STRING_EQUAL(str, "udp");

+ 

+     str = semanage_port_get_proto_str(1);

+     CU_ASSERT_STRING_EQUAL(str, "tcp");

+ 

+ #if VERS_CHECK(2, 5)

+     str = semanage_port_get_proto_str(2);

+     CU_ASSERT_STRING_EQUAL(str, "dccp");

+ #endif

+ 

+     str = semanage_port_get_proto_str(3);

+     CU_ASSERT_STRING_EQUAL(str, "???");

+ }

+ 

+ // Function semanage_port_get_low, semanage_port_get_high,

+ // semanage_port_set_port, semanage_port_set_range

+ 

+ void test_port_get_set_port(void) {

+     semanage_port_t *port = NULL;

+ 

+     // setup

+     setup_handle(SH_CONNECT);

+ 

+     port = get_port_nth(I_FIRST);

+ 

+     // test

+     semanage_port_set_port(port, 1000);

+ 

+     CU_ASSERT(semanage_port_get_low(port) == 1000);

+     CU_ASSERT(semanage_port_get_high(port) == 1000);

+ 

+     semanage_port_set_range(port, 1000, 1200);

+ 

+     CU_ASSERT(semanage_port_get_low(port) == 1000);

+     CU_ASSERT(semanage_port_get_high(port) == 1200);

+     

+     // cleanup

+     semanage_port_free(port);

+     cleanup_handle(SH_CONNECT);

+ }

+ 

+ // Function semanage_port_get_con, semanage_port_set_con

+ 

+ void test_port_get_set_con(void) {

+     semanage_port_t *port = NULL;

+     semanage_port_t *port_tmp = NULL;

+     semanage_context_t *con = NULL;

+ 

+     // setup

+     setup_handle(SH_CONNECT);

+ 

+     port = get_port_nth(I_FIRST);

+     port_tmp = get_port_nth(I_SECOND);

+ 

+     con = semanage_port_get_con(port_tmp);

+ 

+     // test

+     CU_ASSERT(semanage_port_set_con(sh, port, con) >= 0);

+ 

+     CU_ASSERT(semanage_port_get_con(port) != NULL);

+ 

+     // TODO: check the context

+ 

+     // cleanup

+     semanage_port_free(port);

+     semanage_port_free(port_tmp);

+     cleanup_handle(SH_CONNECT);

+ }

+ 

+ // Function semanage_port_create

+ 

+ void test_port_create(void) {

+     semanage_port_t *port = NULL;

+ 

+     // setup

+     setup_handle(SH_CONNECT);

+ 

+     // test

+     CU_ASSERT(semanage_port_create(sh, &port) >= 0);

+ 

+     CU_ASSERT(semanage_port_get_low(port) == 0);

+     CU_ASSERT(semanage_port_get_high(port) == 0);

+     CU_ASSERT(semanage_port_get_con(port) == NULL);

+     CU_ASSERT(semanage_port_get_proto(port) == 0);

+ 

+     // cleanup

+     semanage_port_free(port);

+     cleanup_handle(SH_CONNECT);

+ }

+ 

+ // Function semanage_port_clone

+ 

+ void test_port_clone(void) {

+     semanage_port_t *port = NULL;

+     semanage_port_t *port_clone = NULL;

+     semanage_context_t *con = NULL;

+     semanage_context_t *con2 = NULL;

+ 

+     // setup

+     setup_handle(SH_CONNECT);

+ 

+     CU_ASSERT(semanage_port_create(sh, &port) >= 0);

+ 

+     semanage_port_set_range(port, 1000, 1200);

+     semanage_port_set_proto(port, 1);

+ 

+     semanage_context_from_string(sh, "user_u:role_r:type_t:s0", &con);

+     semanage_port_set_con(sh, port, con);

+ 

+     // test

+     CU_ASSERT(semanage_port_clone(sh, port, &port_clone) >= 0);

+ 

+     CU_ASSERT(semanage_port_get_low(port_clone) == 1000);

+     CU_ASSERT(semanage_port_get_high(port_clone) == 1200);

+     CU_ASSERT(semanage_port_get_proto(port_clone) == 1);

+ 

+     con2 = semanage_port_get_con(port_clone);

+     CU_ASSERT_CONTEXT_EQUAL(con, con2);

+ 

+     // cleanup

+     semanage_port_free(port);

+     semanage_port_free(port_clone);

+     cleanup_handle(SH_CONNECT);

+ }

+ 

+ // Function semanage_port_query

+ 

+ void test_port_query(void) {

+     semanage_port_t *port = NULL;

+     semanage_port_t *port_exp = NULL;

+     semanage_port_key_t *key = NULL;

+     semanage_context_t *con = NULL;

+     semanage_context_t *con_exp = NULL;

+ 

+     // setup

+     setup_handle(SH_CONNECT);

+ 

+     key = get_port_key_nth(I_FIRST);

+ 

+     port_exp = get_port_nth(I_FIRST);

+ 

+     // test

+     CU_ASSERT(semanage_port_query(sh, key, &port) >= 0);

+ 

+     CU_ASSERT(semanage_port_get_low(port) ==

+               semanage_port_get_low(port_exp));

+     CU_ASSERT(semanage_port_get_high(port) ==

+               semanage_port_get_high(port_exp));

+     CU_ASSERT(semanage_port_get_proto(port) ==

+               semanage_port_get_proto(port_exp));

+ 

+     con = semanage_port_get_con(port);

+     con_exp = semanage_port_get_con(port_exp);

+ 

+     CU_ASSERT_CONTEXT_EQUAL(con, con_exp);

+ 

+     // cleanup

+     semanage_port_free(port);

+     semanage_port_free(port_exp);

+     cleanup_handle(SH_CONNECT);

+ }

+ 

+ // Function semanage_port_exists

+ 

+ void test_port_exists(void) {

+     semanage_port_key_t *key1 = NULL;

+     semanage_port_key_t *key2 = NULL;

+     int response = 42;

+ 

+     // setup

+     setup_handle(SH_CONNECT);

+ 

+     key1 = get_port_key_nth(I_FIRST);

+     

+     CU_ASSERT(semanage_port_key_create(sh, 123, 456, 0, &key2) >= 0);

+ 

+     // test existing

+     CU_ASSERT(semanage_port_exists(sh, key1, &response) >= 0);

+ 

+     CU_ASSERT(response);

+ 

+     // test nonexistent

+     CU_ASSERT(semanage_port_exists(sh, key2, &response) >= 0);

+ 

+     CU_ASSERT(!response);

+ 

+     // cleanup

+     semanage_port_key_free(key1);

+     semanage_port_key_free(key2);

+     cleanup_handle(SH_CONNECT);

+ }

+ 

+ // Function semanage_port_count

+ 

+ void test_port_count(void) {

+     unsigned int count = 42;

+ 

+     // setup

+     setup_handle(SH_CONNECT);

+ 

+     // test

+     CU_ASSERT(semanage_port_count(sh, &count) >= 0);

+ 

+     // TODO: check real count

+     CU_ASSERT(count > 0);

+ 

+     // cleanup

+     cleanup_handle(SH_CONNECT);

+ }

+ 

+ // Function semanage_port_iterate

+ 

+ unsigned int counter_port_iterate = 0;

+ 

+ int handler_port_iterate(const semanage_port_t *record, void *varg) {

+     counter_port_iterate++;

+     return 0;

+ }

+ 

+ void test_port_iterate(void) {

+     // setup

+     setup_handle(SH_CONNECT);

+ 

+     // test

+     semanage_port_iterate(sh, handler_port_iterate, NULL);

+ 

+     // TODO: check real count

+     CU_ASSERT(counter_port_iterate > 0);

+ 

+     // cleanup

+     cleanup_handle(SH_CONNECT);

+ }

+ 

+ // Function semanage_port_list

+ 

+ void test_port_list(void) {

+     semanage_port_t **records = NULL;

+     unsigned int count = 42;

+ 

+     // setup

+     setup_handle(SH_CONNECT);

+ 

+     // test

+     CU_ASSERT(semanage_port_list(sh, &records, &count) >= 0);

+ 

+     CU_ASSERT(count > 0);

+ 

+     for (unsigned int i = 0; i < count; i++) {

+         CU_ASSERT_PTR_NOT_NULL(records[i]);

+     }

+ 

+     // cleanup

+     for (unsigned int i = 0; i < count; i++) {

+         semanage_port_free(records[i]);

+     }

+ 

+     cleanup_handle(SH_CONNECT);

+ }

+ 

+ // Function semanage_port_modify_local, semanage_port_del_local

+ 

+ void test_port_modify_del_local(void) {

+     semanage_port_t *port;

+     semanage_port_t *port_local;

+     semanage_port_key_t *key = NULL;

+     semanage_context_t *con = NULL;

+     semanage_context_t *con_local = NULL;

+ 

+     // setup

+     setup_handle(SH_TRANS);

+ 

+     port = get_port_nth(I_FIRST);

+ 

+     semanage_context_from_string(sh, "user_u:role_r:type_t:s0", &con);

+     semanage_port_set_con(sh, port, con);

+ 

+     CU_ASSERT(semanage_port_key_extract(sh, port, &key) >= 0);

+     CU_ASSERT_PTR_NOT_NULL(key);

+ 

+     // test

+     CU_ASSERT(semanage_port_modify_local(sh, key, port) >= 0);

+ 

+     CU_ASSERT(semanage_port_query_local(sh, key, &port_local) >= 0);

+     CU_ASSERT_PTR_NOT_NULL_FATAL(port_local);

+ 

+     con_local = semanage_port_get_con(port_local);

+ 

+     CU_ASSERT_CONTEXT_EQUAL(con, con_local);

+ 

+     CU_ASSERT(semanage_port_del_local(sh, key) >= 0);

+ 

+     CU_ASSERT(semanage_port_query_local(sh, key, &port_local) < 0);

+ 

+     // cleanup

+     semanage_port_free(port);

+     cleanup_handle(SH_TRANS);

+ }

+ 

+ // Function semanage_port_query_local

+ 

+ void test_port_query_local(void) {

+     semanage_port_t *port = NULL;

+     semanage_port_t *port_exp = NULL;

+     semanage_port_key_t *key = NULL;

+     semanage_context_t *con = NULL;

+     semanage_context_t *con_exp = NULL;

+ 

+     // setup

+     setup_handle(SH_TRANS);

+ 

+     add_local_port(I_FIRST);

+ 

+     key = get_port_key_nth(I_FIRST);

+ 

+     port_exp = get_port_nth(I_FIRST);

+ 

+     // test

+     CU_ASSERT(semanage_port_query_local(sh, key, &port) >= 0);

+ 

+     CU_ASSERT(semanage_port_get_low(port) ==

+               semanage_port_get_low(port_exp));

+     CU_ASSERT(semanage_port_get_high(port) ==

+               semanage_port_get_high(port_exp));

+     CU_ASSERT(semanage_port_get_proto(port) ==

+               semanage_port_get_proto(port_exp));

+ 

+     con = semanage_port_get_con(port);

+     con_exp = semanage_port_get_con(port_exp);

+ 

+     CU_ASSERT_CONTEXT_EQUAL(con, con_exp);

+ 

+     // cleanup

+     delete_local_port(I_FIRST);

+     semanage_port_free(port);

+     semanage_port_free(port_exp);

+     cleanup_handle(SH_TRANS);

+ }

+ 

+ // Function semanage_port_exists_local

+ 

+ void test_port_exists_local(void) {

+     semanage_port_key_t *key1 = NULL;

+     semanage_port_key_t *key2 = NULL;

+     int response = 42;

+ 

+     // setup

+     setup_handle(SH_TRANS);

+ 

+     add_local_port(I_FIRST);

+ 

+     key1 = get_port_key_nth(I_FIRST);

+     key2 = get_port_key_nth(I_SECOND);

+ 

+     // test existing

+     CU_ASSERT(semanage_port_exists_local(sh, key1, &response) >= 0);

+ 

+     CU_ASSERT(response);

+ 

+     // test nonexistent

+     CU_ASSERT(semanage_port_exists_local(sh, key2, &response) >= 0);

+ 

+     CU_ASSERT(!response);

+ 

+     // cleanup

+     delete_local_port(I_FIRST);

+     semanage_port_key_free(key1);

+     semanage_port_key_free(key2);

+     cleanup_handle(SH_TRANS);

+ }

+ 

+ // Function semanage_port_count_local

+ 

+ void test_port_count_local(void) {

+     unsigned int count = 42;

+ 

+     // setup

+     setup_handle(SH_TRANS);

+ 

+     // test 0 local ports

+     CU_ASSERT(semanage_port_count_local(sh, &count) >= 0);

+     CU_ASSERT(count == 0);

+ 

+     // test 1 local port

+     add_local_port(I_FIRST);

+ 

+     CU_ASSERT(semanage_port_count_local(sh, &count) >= 0);

+     CU_ASSERT(count == 1);

+ 

+     // test 2 local ports

+     add_local_port(I_SECOND);

+ 

+     CU_ASSERT(semanage_port_count_local(sh, &count) >= 0);

+     CU_ASSERT(count == 2);

+ 

+     // test 1 local port

+     delete_local_port(I_SECOND);

+ 

+     CU_ASSERT(semanage_port_count_local(sh, &count) >= 0);

+     CU_ASSERT(count == 1);

+ 

+     // test 0 local ports

+     delete_local_port(I_FIRST);

+ 

+     CU_ASSERT(semanage_port_count_local(sh, &count) >= 0);

+     CU_ASSERT(count == 0);

+ 

+     // cleanup

+     cleanup_handle(SH_TRANS);

+ }

+ 

+ // Function semanage_port_iterate_local

+ 

+ unsigned int counter_port_iterate_local = 0;

+ 

+ int handler_port_iterate_local(const semanage_port_t *record, void *varg) {

+     counter_port_iterate_local++;

+     return 0;

+ }

+ 

+ void test_port_iterate_local(void) {

+     // setup

+     setup_handle(SH_TRANS);

+ 

+     add_local_port(I_FIRST);

+     add_local_port(I_SECOND);

+     add_local_port(I_THIRD);

+ 

+     // test

+     semanage_port_iterate_local(sh, handler_port_iterate_local, NULL);

+ 

+     CU_ASSERT(counter_port_iterate_local == 3);

+ 

+     // cleanup

+     delete_local_port(I_FIRST);

+     delete_local_port(I_SECOND);

+     delete_local_port(I_THIRD);

+ 

+     cleanup_handle(SH_TRANS);

+ }

+ 

+ // Function semanage_port_list_local

+ 

+ void test_port_list_local(void) {

+     semanage_port_t **records = NULL;

+     unsigned int count = 42;

+     // setup

+     setup_handle(SH_TRANS);

+ 

+     add_local_port(I_FIRST);

+     add_local_port(I_SECOND);

+     add_local_port(I_THIRD);

+ 

+     // test

+     CU_ASSERT(semanage_port_list_local(sh, &records, &count) >= 0);

+ 

+     CU_ASSERT(count == 3);

+ 

+     for (unsigned int i = 0; i < count; i++) {

+         CU_ASSERT_PTR_NOT_NULL(records[i]);

+     }

+ 

+     // cleanup

+     for (unsigned int i = 0; i < count; i++) {

+         semanage_port_free(records[i]);

+     }

+ 

+     delete_local_port(I_FIRST);

+     delete_local_port(I_SECOND);

+     delete_local_port(I_THIRD);

+ 

+     cleanup_handle(SH_TRANS);

+ }

+ 

+ // Internal function semanage_port_validate_local

+ // TODO: test all corner cases

+ 

+ void helper_port_validate_local_noport(void) {

+     semanage_port_key_t *key = NULL;

+     int response = 42;

+ 

+     // setup

+     setup_handle(SH_TRANS);

+ 

+     add_local_port(I_FIRST);

+ 

+     helper_commit();

+ 

+     key = get_port_key_nth(I_FIRST);

+ 

+     CU_ASSERT(semanage_port_exists_local(sh, key, &response) >= 0);

+     CU_ASSERT(response);

+ 

+     // test

+     helper_begin_transaction();

+ 

+     delete_local_port(I_FIRST);

+ 

+     helper_commit();

+ 

+     // cleanup

+     helper_begin_transaction();

+     delete_local_port(I_FIRST);

+     cleanup_handle(SH_TRANS);

+ }

+ 

+ void helper_port_validate_local_oneport(void) {

+     // setup

+     setup_handle(SH_TRANS);

+ 

+     add_local_port(I_FIRST);

+ 

+     // test

+     helper_commit();

+ 

+     // cleanup

+     helper_begin_transaction();

+     delete_local_port(I_FIRST);

+     cleanup_handle(SH_TRANS);

+ }

+ 

+ void helper_port_validate_local_twoports(void) {

+     semanage_port_key_t *key1 = NULL;

+     semanage_port_key_t *key2 = NULL;

+     semanage_port_t *port1 = NULL;

+     semanage_port_t *port2 = NULL;

+     semanage_context_t *con1 = NULL;

+     semanage_context_t *con2 = NULL;

+ 

+     // setup

+     setup_handle(SH_TRANS);

+ 

+     CU_ASSERT(semanage_port_key_create(sh, 101, 200, 0, &key1) >= 0);

+     CU_ASSERT(semanage_port_key_create(sh, 201, 300, 0, &key2) >= 0);

+ 

+     CU_ASSERT(semanage_port_create(sh, &port1) >= 0);

+     CU_ASSERT(semanage_port_create(sh, &port2) >= 0);

+ 

+     semanage_port_set_range(port1, 101, 200);

+     semanage_port_set_range(port2, 201, 300);

+     semanage_port_set_proto(port1, 0);

+     semanage_port_set_proto(port2, 0);

+ 

+     CU_ASSERT(semanage_context_from_string(sh,

+                 "system_u:object_r:user_home_t:s0", &con1) >= 0);

+     CU_ASSERT(semanage_context_from_string(sh,

+                 "system_u:object_r:user_tmp_t:s0", &con2) >= 0);

+ 

+     semanage_port_set_con(sh, port1, con1);

+     semanage_port_set_con(sh, port2, con2);

+ 

+     CU_ASSERT(semanage_port_modify_local(sh, key1, port1) >= 0);

+     CU_ASSERT(semanage_port_modify_local(sh, key2, port2) >= 0);

+ 

+     // test

+     helper_commit();

+ 

+     // cleanup

+     helper_begin_transaction();

+     CU_ASSERT(semanage_port_del_local(sh, key1) >= 0);

+     CU_ASSERT(semanage_port_del_local(sh, key2) >= 0);

+     semanage_port_key_free(key1);

+     semanage_port_key_free(key2);

+     semanage_port_free(port1);

+     semanage_port_free(port2);

+     cleanup_handle(SH_TRANS);

+ }

+ 

+ void helper_port_validate_local_proto(void) {

+     semanage_port_key_t *key1 = NULL;

+     semanage_port_key_t *key2 = NULL;

+     semanage_port_key_t *key3 = NULL;

+     semanage_port_t *port1 = NULL;

+     semanage_port_t *port2 = NULL;

+     semanage_port_t *port3 = NULL;

+     semanage_context_t *con1 = NULL;

+     semanage_context_t *con2 = NULL;

+     semanage_context_t *con3 = NULL;

+ 

+     // setup

+     setup_handle(SH_TRANS);

+ 

+     CU_ASSERT(semanage_port_key_create(sh, 101, 200, 0, &key1) >= 0);

+     CU_ASSERT(semanage_port_key_create(sh,  51, 250, 1, &key2) >= 0);

+     CU_ASSERT(semanage_port_key_create(sh, 201, 300, 0, &key3) >= 0);

+ 

+     CU_ASSERT(semanage_port_create(sh, &port1) >= 0);

+     CU_ASSERT(semanage_port_create(sh, &port2) >= 0);

+     CU_ASSERT(semanage_port_create(sh, &port3) >= 0);

+ 

+     semanage_port_set_range(port1, 101, 200);

+     semanage_port_set_range(port2,  51, 250);

+     semanage_port_set_range(port3, 201, 300);

+     

+     semanage_port_set_proto(port1, 0);

+     semanage_port_set_proto(port2, 0);

+     semanage_port_set_proto(port3, 0);

+ 

+     CU_ASSERT(semanage_context_from_string(sh,

+                 "system_u:object_r:user_home_t:s0", &con1) >= 0);

+     CU_ASSERT(semanage_context_from_string(sh,

+                 "system_u:object_r:user_home_t:s0", &con2) >= 0);

+     CU_ASSERT(semanage_context_from_string(sh,

+                 "system_u:object_r:user_tmp_t:s0", &con3) >= 0);

+ 

+     semanage_port_set_con(sh, port1, con1);

+     semanage_port_set_con(sh, port2, con2);

+     semanage_port_set_con(sh, port3, con3);

+ 

+     CU_ASSERT(semanage_port_modify_local(sh, key1, port1) >= 0);

+     CU_ASSERT(semanage_port_modify_local(sh, key2, port2) >= 0);

+     CU_ASSERT(semanage_port_modify_local(sh, key3, port3) >= 0);

+ 

+     // test

+     helper_commit();

+ 

+     // cleanup

+     CU_ASSERT(semanage_port_del_local(sh, key1) >= 0);

+     CU_ASSERT(semanage_port_del_local(sh, key2) >= 0);

+     CU_ASSERT(semanage_port_del_local(sh, key3) >= 0);

+     semanage_port_key_free(key1);

+     semanage_port_key_free(key2);

+     semanage_port_key_free(key3);

+     semanage_port_free(port1);

+     semanage_port_free(port2);

+     semanage_port_free(port3);

+     cleanup_handle(SH_TRANS);

+ }

+ 

+ void test_port_validate_local(void) {

+     helper_port_validate_local_noport();

+     helper_port_validate_local_oneport();

+     helper_port_validate_local_twoports();

+ }

@@ -0,0 +1,43 @@ 

+ #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);

+ 

+ // port_record.h

+ 

+ void test_port_compare(void);

+ void test_port_compare2(void);

+ void test_port_key_create(void);

+ void test_port_key_extract(void);

+ void test_port_get_set_proto(void);

+ void test_port_get_proto_str(void);

+ void test_port_get_set_port(void);

+ void test_port_get_set_con(void);

+ void test_port_create(void);

+ void test_port_clone(void);

+ 

+ // ports_policy.h

+ 

+ void test_port_query(void);

+ void test_port_exists(void);

+ void test_port_count(void);

+ void test_port_iterate(void);

+ void test_port_list(void);

+ 

+ // ports_local.h

+ 

+ void test_port_modify_del_local(void);

+ void test_port_query_local(void);

+ void test_port_exists_local(void);

+ void test_port_count_local(void);

+ void test_port_iterate_local(void);

+ void test_port_list_local(void);

+ // internal

+ void test_port_validate_local(void);

+ 

+ #endif

+ 

The added file is too large to be shown here, see it at: libsemanage/sanity-tests/tests/test_seuser.c
@@ -0,0 +1,40 @@ 

+ #ifndef __TEST_SEUSER_H__

+ #define __TEST_SEUSER_H__

+ 

+ #include <CUnit/Basic.h>

+ 

+ int seuser_test_init(void);

+ int seuser_test_cleanup(void);

+ int seuser_add_tests(CU_pSuite suite);

+ 

+ // seuser_record.h

+ 

+ void test_seuser_key_create(void);

+ void test_seuser_key_extract(void);

+ void test_seuser_compare(void);

+ void test_seuser_compare2(void);

+ void test_seuser_get_set_name(void);

+ void test_seuser_get_set_sename(void);

+ void test_seuser_get_set_mlsrange(void);

+ void test_seuser_clone(void);

+ void test_seuser_create(void);

+ 

+ // seusers_policy.h

+ 

+ void test_seuser_query(void);

+ void test_seuser_exists(void);

+ void test_seuser_count(void);

+ void test_seuser_iterate(void);

+ void test_seuser_list(void);

+ 

+ // seusers_local.h

+ 

+ void test_seuser_modify_del_local(void);

+ void test_seuser_query_local(void);

+ void test_seuser_exists_local(void);

+ void test_seuser_count_local(void);

+ void test_seuser_iterate_local(void);

+ void test_seuser_list_local(void);

+ 

+ #endif

+ 

@@ -0,0 +1,604 @@ 

+ #include <stdio.h>

+ #include <semanage/semanage.h>

+ #include <semanage/handle.h>

+ #include <CUnit/Basic.h>

+ 

+ #include "test_user.h"

+ #include "functions.h"

+ 

+ extern semanage_handle_t *sh;

+ 

+ int user_test_init(void) {

+     return 0;

+ }

+ 

+ int user_test_cleanup(void) {

+     return 0;

+ }

+ 

+ int user_add_tests(CU_pSuite suite) {

+     CU_add_test(suite, "user_compare", test_user_compare);

+     CU_add_test(suite, "user_compare2", test_user_compare2);

+     CU_add_test(suite, "user_key_create", test_user_key_create);

+     CU_add_test(suite, "user_key_extract", test_user_key_extract);

+     CU_add_test(suite, "user_get_set_name", test_user_get_set_name);

+     CU_add_test(suite, "user_get_set_prefix", test_user_get_set_prefix);

+     CU_add_test(suite, "user_get_set_mlslevel", test_user_get_set_mlslevel);

+     CU_add_test(suite, "user_get_set_mlsrange", test_user_get_set_mlsrange);

+     CU_add_test(suite, "user_roles", test_user_roles);

+     CU_add_test(suite, "user_create", test_user_create);

+     CU_add_test(suite, "user_clone", test_user_clone);

+ 

+     CU_add_test(suite, "user_query", test_user_query);

+     CU_add_test(suite, "user_exists", test_user_exists);

+     CU_add_test(suite, "user_count", test_user_count);

+     CU_add_test(suite, "user_iterate", test_user_iterate);

+     CU_add_test(suite, "user_list", test_user_list);

+ 

+     CU_add_test(suite, "user_modify_del_query_local",

+                 test_user_modify_del_query_local);

+     CU_add_test(suite, "user_exists_local", test_user_exists_local);

+     CU_add_test(suite, "user_count_local", test_user_count_local);

+     CU_add_test(suite, "user_iterate_local", test_user_iterate_local);

+     CU_add_test(suite, "user_list_local", test_user_list_local);

+ 

+     return 0;

+ }

+ 

+ // Function semanage_user_compare

+ 

+ void test_user_compare(void) {

+     semanage_user_t *user = NULL;

+     semanage_user_key_t *key1 = NULL;

+     semanage_user_key_t *key2 = NULL;

+     int result = 42;

+ 

+     // setup

+     setup_handle(SH_CONNECT);

+ 

+     user = get_user_nth(I_FIRST);

+     key1 = get_user_key_nth(I_FIRST);

+     key2 = get_user_key_nth(I_SECOND);

+ 

+     // test

+     result = semanage_user_compare(user, key1);

+ 

+     CU_ASSERT(result == 0);

+ 

+     result = semanage_user_compare(user, key2);

+ 

+     CU_ASSERT(result != 0);

+ 

+     // cleanup

+     semanage_user_free(user);

+     semanage_user_key_free(key1);

+     semanage_user_key_free(key2);

+     cleanup_handle(SH_CONNECT);

+ }

+ 

+ // Function semanage_user_compare2

+ 

+ void test_user_compare2(void) {

+     semanage_user_t *user1 = NULL;

+     semanage_user_t *user2 = NULL;

+     semanage_user_t *user3 = NULL;

+     int result = 42;

+ 

+     // setup

+     setup_handle(SH_CONNECT);

+ 

+     user1 = get_user_nth(I_FIRST);

+     user2 = get_user_nth(I_FIRST);

+     user3 = get_user_nth(I_SECOND);

+ 

+     // test

+     result = semanage_user_compare2(user1, user2);

+ 

+     CU_ASSERT(result == 0);

+ 

+     result = semanage_user_compare2(user1, user3);

+ 

+     CU_ASSERT(result != 0);

+ 

+     // cleanup

+     semanage_user_free(user1);

+     semanage_user_free(user2);

+     semanage_user_free(user3);

+     cleanup_handle(SH_CONNECT);

+ }

+ 

+ // Function semanage_user_key_create

+ 

+ void test_user_key_create(void) {

+     semanage_user_key_t *key = NULL;

+ 

+     setup_handle(SH_CONNECT);

+ 

+     CU_ASSERT(semanage_user_key_create(sh, "asdf", &key) >= 0);

+     CU_ASSERT_PTR_NOT_NULL(key);

+ 

+     semanage_user_key_free(key);

+     cleanup_handle(SH_CONNECT);

+ }

+ 

+ // Function semanage_user_key_extract

+ 

+ void test_user_key_extract(void) {

+     semanage_user_t *user = NULL;

+     semanage_user_key_t *key = NULL;

+ 

+     // setup

+     setup_handle(SH_CONNECT);

+ 

+     user = get_user_nth(I_FIRST);

+ 

+     // test

+     CU_ASSERT(semanage_user_key_extract(sh, user, &key) >= 0);

+     CU_ASSERT_PTR_NOT_NULL(key);

+ 

+     // cleanup

+     semanage_user_free(user);

+     semanage_user_key_free(key);

+     cleanup_handle(SH_CONNECT);

+ }

+ 

+ // Function semanage_user_get_name, semanage_user_set_name

+ 

+ void test_user_get_set_name(void) {

+     semanage_user_t *user = NULL;

+ 

+     // setup

+     setup_handle(SH_CONNECT);

+ 

+     CU_ASSERT(semanage_user_create(sh, &user) >= 0);

+ 

+     // test

+     CU_ASSERT(semanage_user_set_name(sh, user, "user_u") == 0);

+     CU_ASSERT_STRING_EQUAL(semanage_user_get_name(user), "user_u");

+ 

+     // cleanup

+     semanage_user_free(user);

+     cleanup_handle(SH_CONNECT);

+ }

+ 

+ // Function semanage_user_get_prefix, semanage_user_set_prefix

+ 

+ void test_user_get_set_prefix(void) {

+     semanage_user_t *user = NULL;

+ 

+     // setup

+     setup_handle(SH_CONNECT);

+ 

+     CU_ASSERT(semanage_user_create(sh, &user) >= 0);

+ 

+     // test

+     CU_ASSERT(semanage_user_set_prefix(sh, user, "user") == 0);

+     CU_ASSERT_STRING_EQUAL(semanage_user_get_prefix(user), "user");

+ 

+     // cleanup

+     semanage_user_free(user);

+     cleanup_handle(SH_CONNECT);

+ }

+ 

+ // Function semanage_user_get_mlslevel, semanage_user_set_mlslevel

+ 

+ void test_user_get_set_mlslevel(void) {

+     semanage_user_t *user = NULL;

+ 

+     // setup

+     setup_handle(SH_CONNECT);

+ 

+     CU_ASSERT(semanage_user_create(sh, &user) >= 0);

+ 

+     // test

+     CU_ASSERT(semanage_user_set_mlslevel(sh, user, "s0") == 0);

+     CU_ASSERT_STRING_EQUAL(semanage_user_get_mlslevel(user), "s0");

+ 

+     // cleanup

+     semanage_user_free(user);

+     cleanup_handle(SH_CONNECT);

+ }

+ 

+ // Function semanage_user_get_mlsrange, semanage_user_set_mlsrange

+ 

+ void test_user_get_set_mlsrange(void) {

+     semanage_user_t *user = NULL;

+ 

+     // setup

+     setup_handle(SH_CONNECT);

+ 

+     CU_ASSERT(semanage_user_create(sh, &user) >= 0);

+ 

+     // test

+     CU_ASSERT(semanage_user_set_mlsrange(sh, user, "s0-s15") == 0);

+     CU_ASSERT_STRING_EQUAL(semanage_user_get_mlsrange(user), "s0-s15");

+ 

+     // cleanup

+     semanage_user_free(user);

+     cleanup_handle(SH_CONNECT);

+ }

+ 

+ // Function semanage_user_get_num_roles, semanage_user_add_role,

+ // semanage_user_del_role, semanage_user_has_role, semanage_user_get_roles

+ // semanage_user_set_roles

+ 

+ void test_user_roles(void) {

+     semanage_user_t *user = NULL;

+     const char **roles_arr = NULL;

+     unsigned int num_roles = 42;

+     const char *new_roles_arr[] = { "new_role_r", "new_my_role_r" };

+     unsigned int new_num_roles = 2;

+ 

+     // setup

+     setup_handle(SH_CONNECT);

+ 

+     CU_ASSERT(semanage_user_create(sh, &user) >= 0);

+ 

+     // test

+     CU_ASSERT(semanage_user_get_num_roles(user) == 0);

+ 

+     CU_ASSERT(semanage_user_add_role(sh, user, "role_r") == 0);

+     CU_ASSERT(semanage_user_get_num_roles(user) == 1);

+ 

+     CU_ASSERT(semanage_user_has_role(user, "role_r"));

+     CU_ASSERT(!semanage_user_has_role(user, "my_role_r"));

+ 

+     CU_ASSERT(semanage_user_add_role(sh, user, "my_role_r") == 0);

+     CU_ASSERT(semanage_user_get_num_roles(user) == 2);

+ 

+     CU_ASSERT(semanage_user_get_roles(sh, user, &roles_arr, &num_roles) >= 0);

+     CU_ASSERT(num_roles == 2);

+     CU_ASSERT_STRING_EQUAL(roles_arr[0], "role_r");

+     CU_ASSERT_STRING_EQUAL(roles_arr[1], "my_role_r");

+ 

+     CU_ASSERT(semanage_user_set_roles(sh, user, new_roles_arr,

+                                       new_num_roles) >= 0);

+ 

+     CU_ASSERT(semanage_user_has_role(user, "new_role_r"));

+     CU_ASSERT(semanage_user_has_role(user, "new_my_role_r"));

+ 

+     CU_ASSERT(!semanage_user_has_role(user, "role_r"));

+     CU_ASSERT(!semanage_user_has_role(user, "my_role_r"));

+ 

+     semanage_user_del_role(user, "new_my_role_r");

+     CU_ASSERT(semanage_user_get_num_roles(user) == 1);

+ 

+     semanage_user_del_role(user, "new_role_r");

+     CU_ASSERT(semanage_user_get_num_roles(user) == 0);

+ 

+     // cleanup

+     semanage_user_free(user);

+     cleanup_handle(SH_CONNECT);

+ }

+ 

+ // Function semanage_user_create

+ 

+ void test_user_create(void) {

+     semanage_user_t *user = NULL;

+ 

+     // setup

+     setup_handle(SH_CONNECT);

+ 

+     // test

+     CU_ASSERT(semanage_user_create(sh, &user) >= 0);

+ 

+     CU_ASSERT(semanage_user_set_name(sh, user, "user_u") >= 0);

+     CU_ASSERT(semanage_user_set_prefix(sh, user, "user") >= 0);

+     CU_ASSERT(semanage_user_set_mlslevel(sh, user, "s0") >= 0);

+     CU_ASSERT(semanage_user_set_mlsrange(sh, user, "s0-s15") >= 0);

+ 

+     // cleanup

+     semanage_user_free(user);

+     cleanup_handle(SH_CONNECT);

+ }

+ 

+ // Function semanage_user_clone

+ 

+ void test_user_clone(void) {

+     semanage_user_t *user = NULL;

+     semanage_user_t *user_clone = NULL;

+ 

+     // setup

+     setup_handle(SH_CONNECT);

+ 

+     CU_ASSERT(semanage_user_create(sh, &user) >= 0);

+ 

+     CU_ASSERT(semanage_user_set_name(sh, user, "user_u") >= 0);

+     CU_ASSERT(semanage_user_set_prefix(sh, user, "user") >= 0);

+     CU_ASSERT(semanage_user_set_mlslevel(sh, user, "s0") >= 0);

+     CU_ASSERT(semanage_user_set_mlsrange(sh, user, "s0-s15") >= 0);

+ 

+     // test

+     CU_ASSERT(semanage_user_clone(sh, user, &user_clone) >= 0);

+ 

+     CU_ASSERT_STRING_EQUAL(semanage_user_get_name(user), "user_u");

+     CU_ASSERT_STRING_EQUAL(semanage_user_get_prefix(user), "user");

+     CU_ASSERT_STRING_EQUAL(semanage_user_get_mlslevel(user), "s0");

+     CU_ASSERT_STRING_EQUAL(semanage_user_get_mlsrange(user), "s0-s15");

+ 

+     // cleanup

+     semanage_user_free(user);

+     semanage_user_free(user_clone);

+     cleanup_handle(SH_CONNECT);

+ }

+ 

+ // Function semanage_user_query

+ 

+ void test_user_query(void) {

+     semanage_user_t *user = NULL;

+     semanage_user_key_t *key = NULL;

+ 

+     // setup

+     setup_handle(SH_CONNECT);

+ 

+     key = get_user_key_nth(I_FIRST);

+ 

+     // test

+     CU_ASSERT(semanage_user_query(sh, key, &user) >= 0);

+ 

+     // TODO: test values

+     CU_ASSERT_PTR_NOT_NULL(user);

+ 

+     // cleanup

+     semanage_user_free(user);

+     cleanup_handle(SH_CONNECT);

+ }

+ 

+ // Function semanage_user_exists

+ 

+ void test_user_exists(void) {

+     semanage_user_key_t *key1 = NULL;

+     semanage_user_key_t *key2 = NULL;

+     int response = 42;

+ 

+     // setup

+     setup_handle(SH_CONNECT);

+ 

+     key1 = get_user_key_nth(I_FIRST);

+     

+     CU_ASSERT(semanage_user_key_create(sh, "asdf", &key2) >= 0);

+ 

+     // test

+     CU_ASSERT(semanage_user_exists(sh, key1, &response) >= 0);

+ 

+     CU_ASSERT(response);

+ 

+     CU_ASSERT(semanage_user_exists(sh, key2, &response) >= 0);

+ 

+     CU_ASSERT(!response);

+ 

+     // cleanup

+     semanage_user_key_free(key1);

+     semanage_user_key_free(key2);

+     cleanup_handle(SH_CONNECT);

+ }

+ 

+ // Function semanage_user_count

+ 

+ void test_user_count(void) {

+     unsigned int count = 42;

+ 

+     // setup

+     setup_handle(SH_CONNECT);

+ 

+     // test

+     CU_ASSERT(semanage_user_count(sh, &count) >= 0);

+     

+     // TODO: check real count

+     CU_ASSERT(count > 0);

+ 

+     // cleanup

+     cleanup_handle(SH_CONNECT);

+ }

+ 

+ // Function semanage_user_iterate

+ 

+ unsigned int counter_user_iterate = 0;

+ 

+ int handler_user_iterate(const semanage_user_t *record, void *varg) {

+     counter_user_iterate++;

+     return 0;

+ }

+ 

+ void test_user_iterate(void) {

+     // setup

+     setup_handle(SH_CONNECT);

+ 

+     // test

+     semanage_user_iterate(sh, handler_user_iterate, NULL);

+ 

+     // TODO: check real count

+     CU_ASSERT(counter_user_iterate > 0);

+ 

+     // cleanup

+     cleanup_handle(SH_CONNECT);

+ }

+ 

+ // Function semanage_user_list

+ 

+ void test_user_list(void) {

+     semanage_user_t **records = NULL;

+     unsigned int count = 42;

+ 

+     // setup

+     setup_handle(SH_CONNECT);

+ 

+     // test

+     CU_ASSERT(semanage_user_list(sh, &records, &count) >= 0);

+ 

+     // TODO: check real count

+     CU_ASSERT(count > 0);

+ 

+     // TODO: check real values

+     for (unsigned int i = 0; i < count; i++) {

+         CU_ASSERT_PTR_NOT_NULL(records[i]);

+     }

+ 

+     for (unsigned int i = 0; i < count; i++) {

+         semanage_user_free(records[i]);

+     }

+ 

+     // cleanup

+     cleanup_handle(SH_CONNECT);

+ }

+ 

+ // Function semanage_user_modify_local, semanage_user_del_local,

+ // semanage_user_query_local

+ 

+ void test_user_modify_del_query_local(void) {

+     semanage_user_t *user;

+     semanage_user_t *user_local;

+     semanage_user_key_t *key = NULL;

+ 

+     // setup

+     setup_handle(SH_TRANS);

+ 

+     user = get_user_nth(I_FIRST);

+ 

+     CU_ASSERT(semanage_user_key_extract(sh, user, &key) >= 0);

+     CU_ASSERT_PTR_NOT_NULL(key);

+ 

+     // test

+     CU_ASSERT(semanage_user_modify_local(sh, key, user) >= 0);

+ 

+     // write changes to file

+     helper_commit();

+     helper_begin_transaction();

+ 

+     CU_ASSERT(semanage_user_query_local(sh, key, &user_local) >= 0);

+     CU_ASSERT_PTR_NOT_NULL_FATAL(user_local);

+ 

+     CU_ASSERT(semanage_user_del_local(sh, key) >= 0);

+ 

+     CU_ASSERT(semanage_user_query_local(sh, key, &user_local) < 0);

+ 

+     // cleanup

+     semanage_user_free(user);

+     cleanup_handle(SH_TRANS);

+ }

+ 

+ // Function semanage_user_exists_local

+ 

+ void test_user_exists_local(void) {

+     semanage_user_t *user = NULL;

+     semanage_user_key_t *key1 = NULL;

+     semanage_user_key_t *key2 = NULL;

+     int response = 42;

+ 

+     // setup

+     setup_handle(SH_TRANS);

+ 

+     add_local_user(I_FIRST);

+ 

+     key1 = get_user_key_nth(I_FIRST);

+ 

+     CU_ASSERT(semanage_user_key_create(sh, "asdf", &key2) >= 0);

+     CU_ASSERT_PTR_NOT_NULL(key2);

+ 

+     // test existing

+     CU_ASSERT(semanage_user_exists_local(sh, key1, &response) >= 0);

+ 

+     CU_ASSERT(response);

+ 

+     // test nonexistent

+     CU_ASSERT(semanage_user_exists_local(sh, key2, &response) >= 0);

+ 

+     CU_ASSERT(!response);

+ 

+     // cleanup

+     CU_ASSERT(semanage_user_del_local(sh, key1) >= 0);

+     semanage_user_free(user);

+     semanage_user_key_free(key1);

+     semanage_user_key_free(key2);

+     cleanup_handle(SH_TRANS);

+ }

+ 

+ // Function semanage_user_count_local

+ 

+ void test_user_count_local(void) {

+     unsigned int count = 42;

+ 

+     // setup

+     setup_handle(SH_TRANS);

+ 

+     add_local_user(I_FIRST);

+     add_local_user(I_SECOND);

+     add_local_user(I_THIRD);

+ 

+     // test

+     CU_ASSERT(semanage_user_count_local(sh, &count) >= 0);

+ 

+     CU_ASSERT(count == 3);

+ 

+     // cleanup

+     delete_local_user(I_FIRST);

+     delete_local_user(I_SECOND);

+     delete_local_user(I_THIRD);

+ 

+     cleanup_handle(SH_TRANS);

+ }

+ 

+ // Function semanage_user_iterate_local

+ 

+ unsigned int counter_user_iterate_local = 0;

+ 

+ int handler_user_iterate_local(const semanage_user_t *record, void *varg) {

+     counter_user_iterate_local++;

+     return 0;

+ }

+ 

+ void test_user_iterate_local(void) {

+     // setup

+     setup_handle(SH_TRANS);

+ 

+     add_local_user(I_FIRST);

+     add_local_user(I_SECOND);

+     add_local_user(I_THIRD);

+ 

+     // test

+     semanage_user_iterate_local(sh, handler_user_iterate_local, NULL);

+ 

+     CU_ASSERT(counter_user_iterate_local == 3);

+ 

+     // cleanup

+     delete_local_user(I_FIRST);

+     delete_local_user(I_SECOND);

+     delete_local_user(I_THIRD);

+ 

+     cleanup_handle(SH_TRANS);

+ }

+ 

+ // Function semanage_user_list_local

+ 

+ void test_user_list_local(void) {

+     semanage_user_t **records = NULL;

+     unsigned int count = 42;

+ 

+     // setup

+     setup_handle(SH_TRANS);

+ 

+     add_local_user(I_FIRST);

+     add_local_user(I_SECOND);

+     add_local_user(I_THIRD);

+ 

+     // test

+     CU_ASSERT(semanage_user_list_local(sh, &records, &count) >= 0);

+ 

+     CU_ASSERT(count == 3);

+ 

+     for (unsigned int i = 0; i < count; i++) {

+         CU_ASSERT_PTR_NOT_NULL(records[i]);

+     }

+ 

+     // cleanup

+     for (unsigned int i = 0; i < count; i++) {

+         semanage_user_free(records[i]);

+     }

+ 

+     delete_local_user(I_FIRST);

+     delete_local_user(I_SECOND);

+     delete_local_user(I_THIRD);

+ 

+     cleanup_handle(SH_TRANS);

+ }

+ 

@@ -0,0 +1,41 @@ 

+ #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);

+ 

+ // user_record.h

+ 

+ void test_user_compare(void);

+ void test_user_compare2(void);

+ void test_user_key_create(void);

+ void test_user_key_extract(void);

+ void test_user_get_set_name(void);

+ void test_user_get_set_prefix(void);

+ void test_user_get_set_mlslevel(void);

+ void test_user_get_set_mlsrange(void);

+ void test_user_roles(void);

+ void test_user_create(void);

+ void test_user_clone(void);

+ 

+ // users_policy.h

+ 

+ void test_user_query(void);

+ void test_user_exists(void);

+ void test_user_count(void);

+ void test_user_iterate(void);

+ void test_user_list(void);

+ 

+ // users_local.h

+ 

+ void test_user_modify_del_query_local(void);

+ void test_user_exists_local(void);

+ void test_user_count_local(void);

+ void test_user_iterate_local(void);

+ void test_user_list_local(void);

+ 

+ #endif

+ 

@@ -1,3 +0,0 @@ 

- PURPOSE of /CoreOS/libsemanage/Sanity/semanage-handle-functions

- Description: Test functions from handle.h

- Author: Jan Zarsky <jzarsky@redhat.com>

@@ -1,132 +0,0 @@ 

- #include <stdio.h>

- #include <stdlib.h>

- #include <string.h>

- #include <errno.h>

- #include <semanage/semanage.h>

- 

- void check_result_int(const char *expected, int real) {

-     int exp = strtol(expected, NULL, 10);

- 

-     if (exp != real) {

-         fprintf(stderr, "Expected %d but got %d\n", exp, real);

-         exit(1);

-     }

- }

- 

- semanage_handle_t *test_handle_create() {

-     semanage_handle_t *sh = NULL;

- 

-     sh = semanage_handle_create();

-     printf("semanage_handle_create(): %p\n", (void *) sh);

- 

-     if (sh == NULL) {

-         perror("semanage_handle_create");

-         exit(1);

-     }

- 

-     return sh;

- }

- 

- int test_connect(semanage_handle_t *sh) {

-     int result = semanage_connect(sh);

-     printf("semanage_connect(%p): %d\n", (void *) sh, result);

- 

-     if (result != 0) {

-         perror("semanage_connect");

-         exit(1);

-     }

- 

-     return result;

- }

- 

- int test_disconnect(semanage_handle_t *sh) {

-     int result = semanage_disconnect(sh);

-     printf("semanage_disconnect(%p): %d\n", (void *) sh, result);

- 

-     if (result != 0) {

-         perror("semanage_disconnect");

-         exit(1);

-     }

- 

-     return result;

- }

- 

- int test_begin_transaction(semanage_handle_t *sh) {

-     int result = semanage_begin_transaction(sh);

-     printf("semanage_begin_transaction(%p): %d\n", (void *) sh, result);

- 

-     if (result != 0) {

-         perror("semanage_begin_transaction");

-         exit(1);

-     }

- 

-     return result;

- }

- 

- int test_commit(semanage_handle_t *sh) {

-     int result = semanage_commit(sh);

-     printf("semanage_commit(%p): %d\n", (void *) sh, result);

- 

-     if (result != 0) {

-         perror("semanage_commit");

-         exit(1);

-     }

- 

-     return result;

- }

- 

- #define STATE_INIT      1

- #define STATE_HANDLE    2

- #define STATE_CONN      3

- #define STATE_TRANS     4

- 

- int get_state(const char *state_str) {

-     if (strcmp(state_str, "init") == 0)

-         return STATE_INIT;

-     if (strcmp(state_str, "handle") == 0)

-         return STATE_HANDLE;

-     if (strcmp(state_str, "conn") == 0)

-         return STATE_CONN;

-     if (strcmp(state_str, "trans") == 0)

-         return STATE_TRANS;

- 

-     return 0;

- }

- 

- semanage_handle_t * get_handle(const char *state_str) {

-     int state;

-     semanage_handle_t *sh = NULL;

- 

-     state = get_state(state_str);

- 

-     if (state >= STATE_INIT)

-         sh = NULL;

- 

-     if (state >= STATE_HANDLE)

-         sh = test_handle_create();

- 

-     if (state >= STATE_CONN)

-         test_connect(sh);

- 

-     if (state >= STATE_TRANS)

-         test_begin_transaction(sh);

- 

-     return sh;

- }

- 

- void destroy_handle(semanage_handle_t *sh, const char *state_str) {

-     int state;

- 

-     state = get_state(state_str);

- 

-     if (state >= STATE_TRANS)

-         test_commit(sh);

- 

-     if (state >= STATE_CONN)

-         test_disconnect(sh);

- 

-     if (state >= STATE_HANDLE) {

-         semanage_handle_destroy(sh);

-         printf("semanage_handle_destroy(%p)\n", (void *) sh);

-     }

- }

@@ -1,29 +0,0 @@ 

-                                         init    handle  conn    trans

- semanage_set_root                   x   ok      ok      ok      -

- semanage_root                       x   ok      ok      ok      -

- semanage_handle_create              x   ok      -       -       -        

- semanage_set_rebuild                    fail    ok      ok      -

- semanage_set_reload                     fail    ok      ok      -

- semanage_get_hll_compiler_path          fail    ?       ?       -

- semanage_set_create_store               fail    ok      ok      -       should be called after connect

- semanage_get_disable_dontaudit          fail    ?       ?       -

- semanage_set_disable_dontaudit          fail    ?       ?       -

- semanage_get_preserve_tunables          fail    ?       ?       -

- semanage_set_preserve_tunables          fail    ?       ?       -

- semanage_get_ignore_module_cache        fail    ?       ?       -

- semanage_set_ignore_module_cache        fail    ?       ?       -

- semanage_set_check_contexts             fail    ok      ok      -

- semanage_get_default_priority           fail    ok      ok      -

- semanage_set_default_priority           fail    ok      ok      -

- semanage_is_connected               x   fail    ok      ok      -

- semanage_select_store                   fail    ok      ok      -       should be called before connect

- semanage_set_store_root                 fail    ok      ok      -

- semanage_is_managed                 x   fail    ok      fail    -

- semanage_mls_enabled                x   fail    ?       ok      -

- semanage_connect                    x   fail    ok      ?       -

- semanage_access_check               x   fail    ok      ?       -

- semanage_disconnect                 x   fail    fail    ok      -       ok when disconnected twice

- semanage_handle_destroy             x   fail    ok      ok      -

- semanage_begin_transaction          x   fail    fail    ok      ok      ok when begin twice

- semanage_commit                     x   fail    fail    fail    ok

- semanage_reload_policy                  fail    ?       ?       ?

@@ -1,122 +0,0 @@ 

- #!/bin/bash

- # vim: dict+=/usr/share/beakerlib/dictionary.vim cpt=.,w,b,u,t,i,k

- # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

- #

- #   runtest.sh of /CoreOS/libsemanage/Sanity/semanage-handle-functions

- #   Description: Test functions from handle.h

- #   Author: Jan Zarsky <jzarsky@redhat.com>

- #

- # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

- #

- #   Copyright (c) 2017 Red Hat, Inc.

- #

- #   This program is free software: you can redistribute it and/or

- #   modify it under the terms of the GNU General Public License as

- #   published by the Free Software Foundation, either version 2 of

- #   the License, or (at your option) any later version.

- #

- #   This program 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 General Public License for more details.

- #

- #   You should have received a copy of the GNU General Public License

- #   along with this program. If not, see http://www.gnu.org/licenses/.

- #

- # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

- 

- # Include Beaker environment

- . /usr/bin/rhts-environment.sh || exit 1

- . /usr/share/beakerlib/beakerlib.sh || exit 1

- 

- PACKAGE="libsemanage"

- 

- rlJournalStart

-     rlPhaseStartSetup

-         rlAssertRpm ${PACKAGE}

-         rlAssertRpm ${PACKAGE}-devel

-         rlAssertRpm "glibc"

-         rlAssertRpm "gcc"

- 

-         if rlIsRHEL ">=7" || rlIsFedora; then

-             rlRun -l "gcc test_root.c           -o test_root            -lsemanage -Wall -Wextra -std=c99"

-         fi

- 

-         rlRun -l "gcc test_handle_create.c  -o test_handle_create   -lsemanage -Wall -Wextra -Wno-unused-parameter -std=c99"

-         rlRun -l "gcc test_access_check.c   -o test_access_check    -lsemanage -Wall -Wextra -std=c99"

-         rlRun -l "gcc test_is_managed.c     -o test_is_managed      -lsemanage -Wall -Wextra -std=c99"

-         rlRun -l "gcc test_connect.c        -o test_connect         -lsemanage -Wall -Wextra -std=c99"

-         rlRun -l "gcc test_is_connected.c   -o test_is_connected    -lsemanage -Wall -Wextra -std=c99"

-         rlRun -l "gcc test_mls_enabled.c    -o test_mls_enabled     -lsemanage -Wall -Wextra -std=c99"

-         rlRun -l "gcc test_transaction.c    -o test_transaction     -lsemanage -Wall -Wextra -std=c99"

- 

-         ERR_FAIL=1

-         ERR_ABORT=134

-     rlPhaseEnd

- 

-     if rlIsRHEL ">=7" || rlIsFedora; then

-     rlPhaseStartTest "semanage_root, semanage_test_root"

-         rlRun "./test_root init"

-         rlRun "./test_root handle"

-         rlRun "./test_root conn"

-         rlRun "./test_root init /somepath"

-         rlRun "./test_root handle /somepath"

-         rlRun "./test_root conn /somepath"

-     rlPhaseEnd

-     fi

- 

-     rlPhaseStartTest "semanage_handle_create, semanage_handle_destroy"

-         rlRun "./test_handle_create init"

-     rlPhaseEnd

- 

-     rlPhaseStartTest "semanage_access_check"

-         rlRun "./test_access_check init" $ERR_ABORT

-         rlRun "./test_access_check handle 2"

-         rlRun "./test_access_check conn 2"

-     rlPhaseEnd

- 

-     rlPhaseStartTest "semanage_is_managed"

-         rlRun "./test_is_managed init" $ERR_ABORT

-         rlRun "./test_is_managed handle 1"

-         rlRun "./test_is_managed conn" $ERR_FAIL

-     rlPhaseEnd

- 

-     rlPhaseStartTest "semanage_connect, semanage_disconnect"

-         rlRun "./test_connect init" $ERR_ABORT

-         rlRun "./test_connect init reversed" $ERR_ABORT

-         rlRun "./test_connect handle"

-         rlRun "./test_connect handle twice"

-         rlRun "./test_connect handle reversed" $ERR_ABORT

-         # why does it work??

-         rlRun "./test_connect conn"

-     rlPhaseEnd

- 

-     rlPhaseStartTest "semanage_is_connected"

-         rlRun "./test_is_connected init" $ERR_ABORT

-         rlRun "./test_is_connected handle 0"

-         rlRun "./test_is_connected conn 1"

-     rlPhaseEnd

- 

-     rlPhaseStartTest "semanage_mls_enabled"

-         rlRun "./test_mls_enabled init" $ERR_ABORT

-         rlRun "./test_mls_enabled handle" $ERR_ABORT

-         rlRun "./test_mls_enabled conn 1"

-     rlPhaseEnd

- 

-     rlPhaseStartTest "semanage_begin_transaction, semanage_commit"

-         rlRun "./test_transaction init" $ERR_ABORT

-         rlRun "./test_transaction init reversed" $ERR_ABORT

-         rlRun "./test_transaction handle" $ERR_ABORT

-         rlRun "./test_transaction handle reversed" $ERR_ABORT

-         rlRun "./test_transaction conn"

-         rlRun "./test_transaction conn twice"

-         rlRun "./test_transaction conn reversed" $ERR_FAIL

-     rlPhaseEnd

- 

-     rlPhaseStartCleanup

-         rlRun "rm -f output test_root test_handle_create test_access_check \

-                test_is_managed test_connect test_is_connected \

-                test_mls_enabled test_transaction"

-     rlPhaseEnd

- rlJournalPrintText

- rlJournalEnd

@@ -1,32 +0,0 @@ 

- #include <stdio.h>

- #include <stdlib.h>

- #include <string.h>

- #include <errno.h>

- #include <semanage/semanage.h>

- 

- #include "functions.c"

- 

- int main (int argc, char **argv) {

-     semanage_handle_t *sh;

- 

-     if (argc < 2)

-         exit(1);

-     

-     sh = get_handle(argv[1]);

- 

-     int result = semanage_access_check(sh);

-     printf("semanage_access_check(%p): %d\n", (void *) sh, result);

-     

-     if (result < 0 || (result != 0 && result != SEMANAGE_CAN_READ

-                        && result != SEMANAGE_CAN_WRITE)) {

-         perror("semanage_access_check");

-         exit(1);

-     }

- 

-     if (argc >= 3)

-         check_result_int(argv[2], result);

- 

-     destroy_handle(sh, argv[1]);

- 

-     exit(0);

- }

@@ -1,33 +0,0 @@ 

- #include <stdio.h>

- #include <stdlib.h>

- #include <string.h>

- #include <errno.h>

- #include <semanage/semanage.h>

- 

- #include "functions.c"

- 

- int main (int argc, char **argv) {

-     semanage_handle_t *sh;

-     

-     if (argc < 2)

-         exit(1);

-     

-     sh = get_handle(argv[1]);

-     

-     if (argc >= 3 && strcmp(argv[2], "reversed") == 0) {

-         test_disconnect(sh);

-         test_connect(sh);

-     }

-     else {

-         test_connect(sh);

-         test_disconnect(sh);

-     }

- 

-     if (argc >= 3 && strcmp(argv[2], "twice") == 0) {

-         test_disconnect(sh);

-     }

- 

-     destroy_handle(sh, argv[1]);

- 

-     exit(0);

- }

@@ -1,15 +0,0 @@ 

- #include <stdio.h>

- #include <stdlib.h>

- #include <string.h>

- #include <errno.h>

- #include <semanage/semanage.h>

- 

- #include "functions.c"

- 

- int main (int argc, char **argv) {

-     semanage_handle_t *sh = test_handle_create();

- 

-     semanage_handle_destroy(sh);

- 

-     exit(0);

- }

@@ -1,32 +0,0 @@ 

- #include <stdio.h>

- #include <stdlib.h>

- #include <string.h>

- #include <errno.h>

- #include <semanage/semanage.h>

- 

- #include "functions.c"

- 

- int main (int argc, char **argv) {

-     semanage_handle_t *sh;

-     int result;

-     

-     if (argc < 2)

-         exit(1);

-     

-     sh = get_handle(argv[1]);

- 

-     result = semanage_is_connected(sh);

-     printf("semanage_is_connected(%p): %d\n", (void *) sh, result);

- 

-     if (result != 0 && result != 1) {

-         perror("semanage_is_connected");

-         exit(1);

-     }

- 

-     if (argc >= 3)

-         check_result_int(argv[2], result);

- 

-     destroy_handle(sh, argv[1]);

- 

-     exit(0);

- }

@@ -1,32 +0,0 @@ 

- #include <stdio.h>

- #include <stdlib.h>

- #include <string.h>

- #include <errno.h>

- #include <semanage/semanage.h>

- 

- #include "functions.c"

- 

- int main (int argc, char **argv) {

-     semanage_handle_t *sh;

-     int result;

- 

-     if (argc < 2)

-         exit(1);

-     

-     sh = get_handle(argv[1]);

-     

-     result = semanage_is_managed(sh);

-     printf("semanage_is_managed(%p): %d\n", (void *) sh, result);

- 

-     if (result != 0 && result != 1) {

-         perror("semanage_is_managed");

-         exit(1);

-     }

- 

-     if (argc >= 3)

-         check_result_int(argv[2], result);

- 

-     destroy_handle(sh, argv[1]);

- 

-     exit(0);

- }

@@ -1,32 +0,0 @@ 

- #include <stdio.h>

- #include <stdlib.h>

- #include <string.h>

- #include <errno.h>

- #include <semanage/semanage.h>

- 

- #include "functions.c"

- 

- int main (int argc, char **argv) {

-     semanage_handle_t *sh;

-     int result;

-     

-     if (argc < 2)

-         exit(1);

-     

-     sh = get_handle(argv[1]);

- 

-     result = semanage_mls_enabled(sh);

-     printf("semanage_mls_enabled(%p): %d\n", (void *) sh, result);

- 

-     if (result != 0 && result != 1) {

-         perror("semanage_mls_enabled");

-         exit(1);

-     }

- 

-     if (argc >= 4)

-         check_result_int(argv[3], result);

- 

-     destroy_handle(sh, argv[1]);

- 

-     exit(0);

- }

@@ -1,53 +0,0 @@ 

- #include <stdio.h>

- #include <stdlib.h>

- #include <string.h>

- #include <errno.h>

- #include <semanage/semanage.h>

- 

- #include "functions.c"

- 

- int main (int argc, char **argv) {

-     semanage_handle_t *sh;

-     const char *root;

-     int result;

- 

-     if (argc < 2)

-         exit(1);

-     

-     sh = get_handle(argv[1]);

- 

-     root = semanage_root();

-     printf("semanage_root(): %s\n", root);

- 

-     if (root == NULL) {

-         perror("semanage_root");

-         exit(1);

-     }

- 

-     if (argc >= 3) {

-         result = semanage_set_root(argv[2]);

-         printf("semanage_set_root(\"%s\"): %d\n", argv[2], result);

- 

-         if (root == NULL) {

-             perror("semanage_set_root");

-             exit(1);

-         }

- 

-         root = semanage_root();

-         printf("semanage_root(): %s\n", root);

- 

-         if (result != 0) {

-             perror("semanage_root");

-             exit(1);

-         }

- 

-         if (strcmp(root, argv[2]) != 0) {

-             fprintf(stderr, "Expected \"%s\" but got \"%s\"\n", argv[2], root);

-             exit(1);

-         }

-     }

- 

-     destroy_handle(sh, argv[1]);

- 

-     exit(0);

- }

@@ -1,34 +0,0 @@ 

- #include <stdio.h>

- #include <stdlib.h>

- #include <string.h>

- #include <errno.h>

- #include <semanage/semanage.h>

- 

- #include "functions.c"

- 

- int main (int argc, char **argv) {

-     semanage_handle_t *sh;

-     

-     if (argc < 2)

-         exit(1);

-     

-     sh = get_handle(argv[1]);

-     

-     if (argc >= 3 && strcmp(argv[2], "reversed") == 0) {

-         test_commit(sh);

-         test_begin_transaction(sh);

-     }

-     else if (argc >= 3 && strcmp(argv[2], "twice") == 0) {

-         test_begin_transaction(sh);

-         test_begin_transaction(sh);

-         test_commit(sh);

-     }

-     else {

-         test_begin_transaction(sh);

-         test_commit(sh);

-     }

- 

-     destroy_handle(sh, argv[1]);

- 

-     exit(0);

- }

@@ -1,63 +0,0 @@ 

- # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

- #

- #   Makefile of /CoreOS/libsemanage/Sanity/semanage-seuser-functions

- #   Description: Test semanage_seuser_* functions

- #   Author: Jan Zarsky <jzarsky@redhat.com>

- #

- # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

- #

- #   Copyright (c) 2017 Red Hat, Inc.

- #

- #   This program is free software: you can redistribute it and/or

- #   modify it under the terms of the GNU General Public License as

- #   published by the Free Software Foundation, either version 2 of

- #   the License, or (at your option) any later version.

- #

- #   This program 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 General Public License for more details.

- #

- #   You should have received a copy of the GNU General Public License

- #   along with this program. If not, see http://www.gnu.org/licenses/.

- #

- # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

- 

- export TEST=/CoreOS/libsemanage/Sanity/semanage-seuser-functions

- export TESTVERSION=1.0

- 

- BUILT_FILES=

- 

- FILES=$(METADATA) runtest.sh Makefile PURPOSE functions.c test_*.c

- 

- .PHONY: all install download clean

- 

- run: $(FILES) build

- 	./runtest.sh

- 

- build: $(BUILT_FILES)

- 	test -x runtest.sh || chmod a+x runtest.sh

- 

- clean:

- 	rm -f *~ $(BUILT_FILES)

- 

- 

- include /usr/share/rhts/lib/rhts-make.include

- 

- $(METADATA): Makefile

- 	@echo "Owner:           Jan Zarsky <jzarsky@redhat.com>" > $(METADATA)

- 	@echo "Name:            $(TEST)" >> $(METADATA)

- 	@echo "TestVersion:     $(TESTVERSION)" >> $(METADATA)

- 	@echo "Path:            $(TEST_DIR)" >> $(METADATA)

- 	@echo "Description:     Test semanage_seuser_* functions" >> $(METADATA)

- 	@echo "Type:            Sanity" >> $(METADATA)

- 	@echo "TestTime:        5m" >> $(METADATA)

- 	@echo "RunFor:          libsemanage" >> $(METADATA)

- 	@echo "Requires:        libsemanage libsemanage-devel glibc gcc" >> $(METADATA)

- 	@echo "Priority:        Normal" >> $(METADATA)

- 	@echo "License:         GPLv2+" >> $(METADATA)

- 	@echo "Confidential:    no" >> $(METADATA)

- 	@echo "Destructive:     no" >> $(METADATA)

- 	@echo "Releases:        -RHEL4 -RHELClient5 -RHELServer5" >> $(METADATA)

- 

- 	rhts-lint $(METADATA)

@@ -1,3 +0,0 @@ 

- PURPOSE of /CoreOS/libsemanage/Sanity/semanage-seuser-functions

- Description: Test semanage_seuser_* functions

- Author: Jan Zarsky <jzarsky@redhat.com>

@@ -1,263 +0,0 @@ 

- #include <stdio.h>

- #include <stdlib.h>

- #include <string.h>

- #include <errno.h>

- #include <semanage/semanage.h>

- 

- void check_result_int(const char *expected, int real) {

-     int exp = strtol(expected, NULL, 10);

- 

-     if (exp != real) {

-         fprintf(stderr, "Expected %d but got %d\n", exp, real);

-         exit(1);

-     }

- }

- 

- semanage_handle_t *test_handle_create() {

-     semanage_handle_t *sh = NULL;

- 

-     sh = semanage_handle_create();

-     printf("semanage_handle_create(): %p\n", (void *) sh);

- 

-     if (sh == NULL) {

-         perror("semanage_handle_create");

-         exit(2);

-     }

- 

-     return sh;

- }

- 

- int test_connect(semanage_handle_t *sh) {

-     int result = semanage_connect(sh);

-     printf("semanage_connect(%p): %d\n", (void *) sh, result);

- 

-     if (result != 0) {

-         perror("semanage_connect");

-         exit(2);

-     }

- 

-     return result;

- }

- 

- int test_disconnect(semanage_handle_t *sh) {

-     int result = semanage_disconnect(sh);

-     printf("semanage_disconnect(%p): %d\n", (void *) sh, result);

- 

-     if (result != 0) {

-         perror("semanage_disconnect");

-         exit(2);

-     }

- 

-     return result;

- }

- 

- int test_begin_transaction(semanage_handle_t *sh) {

-     int result = semanage_begin_transaction(sh);

-     printf("semanage_begin_transaction(%p): %d\n", (void *) sh, result);

- 

-     if (result != 0) {

-         perror("semanage_begin_transaction");

-         exit(2);

-     }

- 

-     return result;

- }

- 

- int test_commit(semanage_handle_t *sh) {

-     int result = semanage_commit(sh);

-     printf("semanage_commit(%p): %d\n", (void *) sh, result);

- 

-     if (result != 0) {

-         perror("semanage_commit");

-         exit(2);

-     }

- 

-     return result;

- }

- 

- semanage_seuser_key_t *test_get_key(semanage_handle_t *sh, const char *name) {

-     semanage_seuser_key_t *key;

-     int result = semanage_seuser_key_create(sh, name, &key);

-     printf("semanage_seuser_key_create(%p, %s, %p): %d\n",

-            (void *) sh, name, (void *) &key, result);

- 

-     if (key == NULL || result < 0) {

-         perror("semanage_seuser_key_create");

-         exit(2);

-     }

- 

-     return key;

- }

- 

- semanage_seuser_t *test_get_seuser_nth(semanage_handle_t *sh, unsigned int index) {

-     int result;

-     semanage_seuser_t **records;

-     unsigned int count;

- 

-     result = semanage_seuser_list(sh, &records, &count);

-     printf("semanage_seuser_list(%p, %p, %p): %d\n",

-            (void *) sh, (void *) &records, (void *) &count, result);

-     

-     if (result < 0) {

-         perror("semanage_seuser_list");

-         exit(2);

-     }

- 

-     if (count < index + 1)

-         exit(2);

- 

-     return records[index];

- }

- 

- semanage_seuser_t *test_get_seuser_new(semanage_handle_t *sh) {

-     int result;

-     semanage_seuser_t *seuser;

- 

-     result = semanage_seuser_create(sh, &seuser);

-     printf("semanage_seuser_create(%p, %p): %d\n",

-            (void *) sh, (void *) seuser, result);

-     

-     if (result < 0) {

-         perror("semanage_seuser_create");

-         exit(2);

-     }

- 

-     return seuser;

- }

- 

- semanage_seuser_t *test_get_seuser(semanage_handle_t *sh, const char *param) {

-     if (strcmp(param, "new") == 0)

-         return test_get_seuser_new(sh);

-     

-     if (strcmp(param, "first") == 0)

-         return test_get_seuser_nth(sh, 0);

- 

-     if (strcmp(param, "second") == 0)

-         return test_get_seuser_nth(sh, 1);

- 

-     fprintf(stderr, "Unknown seuser \"%s\" specified\n", param);

-     exit(2);

- }

- 

- void test_add_local_seuser(semanage_handle_t *sh, semanage_seuser_t *seuser) {

-     int result;

-     semanage_seuser_key_t *key;

- 

-     result = semanage_seuser_key_extract(sh, seuser, &key);

-     printf("semanage_seuser_key_extract(%p, %p, %p): %d\n",

-            (void *) sh, (void *) seuser, (void *) &key, result);

- 

-     if (result < 0) {

-         perror("semanage_seuser_key_extract");

-         exit(2);

-     }

- 

-     result = semanage_seuser_modify_local(sh, key, seuser);

-     printf("semanage_seuser_modify_local(%p, %p, %p): %d\n",

-            (void *) seuser, (void *) key, (void *) seuser, result);

- 

-     if (result < 0) {

-         perror("semanage_seuser_modify_local");

-         exit(2);

-     }

- }

- 

- void test_del_local_seuser(semanage_handle_t *sh, semanage_seuser_t *seuser) {

-     int result;

-     semanage_seuser_key_t *key;

- 

-     result = semanage_seuser_key_extract(sh, seuser, &key);

-     printf("semanage_seuser_key_extract(%p, %p, %p): %d\n",

-            (void *) sh, (void *) seuser, (void *) &key, result);

- 

-     if (result < 0) {

-         perror("semanage_seuser_key_extract");

-         exit(2);

-     }

- 

-     result = semanage_seuser_del_local(sh, key);

-     printf("semanage_seuser_del_local(%p, %p): %d\n",

-            (void *) seuser, (void *) key, result);

- 

-     if (result < 0) {

-         perror("semanage_seuser_del_local");

-         exit(2);

-     }

- }

- 

- #define STATE_INIT      1

- #define STATE_HANDLE    2

- #define STATE_CONN      3

- #define STATE_TRANS     4

- 

- int get_state(const char *state_str) {

-     if (strcmp(state_str, "init") == 0)

-         return STATE_INIT;

-     if (strcmp(state_str, "handle") == 0)

-         return STATE_HANDLE;

-     if (strcmp(state_str, "conn") == 0)

-         return STATE_CONN;

-     if (strcmp(state_str, "trans") == 0)

-         return STATE_TRANS;

- 

-     return 0;

- }

- 

- semanage_handle_t * get_handle(const char *state_str) {

-     int state;

-     semanage_handle_t *sh = NULL;

- 

-     state = get_state(state_str);

- 

-     if (state >= STATE_INIT)

-         sh = NULL;

- 

-     if (state >= STATE_HANDLE)

-         sh = test_handle_create();

- 

-     if (state >= STATE_CONN)

-         test_connect(sh);

- 

-     if (state >= STATE_TRANS)

-         test_begin_transaction(sh);

- 

-     return sh;

- }

- 

- void destroy_handle(semanage_handle_t *sh, const char *state_str) {

-     int state;

- 

-     state = get_state(state_str);

- 

-     if (state >= STATE_TRANS)

-         test_commit(sh);

- 

-     if (state >= STATE_CONN)

-         test_disconnect(sh);

- 

-     if (state >= STATE_HANDLE) {

-         semanage_handle_destroy(sh);

-         printf("semanage_handle_destroy(%p)\n", (void *) sh);

-     }

- }

- 

- int strcmp_null(const char *str1, const char *str2) {

-     if (str1 == NULL && str2 == NULL)

-         return 0;

- 

-     if (str1 == NULL) {

-         if (strcmp(str2, "NULL") == 0)

-             return 0;

-         else

-             return -1;

-     }

- 

-     if (str2 == NULL) {

-         if (strcmp(str1, "NULL") == 0)

-             return 0;

-         else

-             return 1;

-     }

- 

-     return strcmp(str1, str2);

- }

@@ -1,1 +0,0 @@ 

- tags: [Tier1]

@@ -1,255 +0,0 @@ 

- #!/bin/bash

- # vim: dict+=/usr/share/beakerlib/dictionary.vim cpt=.,w,b,u,t,i,k

- # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

- #

- #   runtest.sh of /CoreOS/libsemanage/Sanity/semanage-seuser-functions

- #   Description: Test semanage_seuser_* functions

- #   Author: Jan Zarsky <jzarsky@redhat.com>

- #

- # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

- #

- #   Copyright (c) 2017 Red Hat, Inc.

- #

- #   This program is free software: you can redistribute it and/or

- #   modify it under the terms of the GNU General Public License as

- #   published by the Free Software Foundation, either version 2 of

- #   the License, or (at your option) any later version.

- #

- #   This program 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 General Public License for more details.

- #

- #   You should have received a copy of the GNU General Public License

- #   along with this program. If not, see http://www.gnu.org/licenses/.

- #

- # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

- 

- # Include Beaker environment

- . /usr/bin/rhts-environment.sh || exit 1

- . /usr/share/beakerlib/beakerlib.sh || exit 1

- 

- PACKAGE="libsemanage"

- 

- rlJournalStart

-     rlPhaseStartSetup

-         rlAssertRpm ${PACKAGE}

-         rlAssertRpm ${PACKAGE}-devel

-         rlAssertRpm "glibc"

-         rlAssertRpm "gcc"

- 

-         for f in test_*.c ; do 

-             out=$(echo -n $f | cut -d'.' -f1)

-             rlRun "gcc $f -o $out -lsemanage -Wall -Wextra -Werror -std=c99"

-         done

- 

-         POLICY_TYPE="$(grep -E '^SELINUXTYPE=' /etc/selinux/config | cut -d'=' -f2 | tr '[:upper:]' '[:lower:]' | tr -d ' ')"

- 

-         if rlIsFedora; then

-             SEUSERS_PATH="/var/lib/selinux/$POLICY_TYPE/active/seusers"

-         elif rlIsRHEL '>=7'; then

-             SEUSERS_PATH="/etc/selinux/$POLICY_TYPE/active/seusers"

-         else

-             SEUSERS_PATH="/etc/selinux/$POLICY_TYPE/seusers"

-         fi

- 

-         rlRun "cat $SEUSERS_PATH"

- 

-         SEUSERS_COUNT="$(cat $SEUSERS_PATH | grep -vE '^#|^$' | wc -l)"

-         rlRun "[[ \"$SEUSERS_COUNT\" -gt 0 ]]"

- 

-         SEUSERS="$(cat $SEUSERS_PATH | grep -vE '^#|^$' | cut -d':' -f1 | tr '\n' ' ')"

-         rlRun "[[ -n \"$SEUSERS\" ]]"

- 

-         first_line="$(cat $SEUSERS_PATH | grep -vE '^#|^$' | head -n 1)"

-         SEUSER="$(echo -n $first_line | cut -d':' -f1)"

-         rlRun "[[ -n \"$SEUSER\" ]]"

-         SEUSER_SENAME="$(echo -n $first_line | cut -d':' -f2)"

-         rlRun "[[ -n \"$SEUSER_SENAME\" ]]"

-         SEUSER_MLSRANGE="$(echo -n $first_line | cut -d':' -f3-4)"

-         rlRun "[[ -n \"$SEUSER_MLSRANGE\" ]]"

- 

-         SEUSER_NONEXISTENT="nonuser"

-         SEUSER_DEFAULT="__default__"

- 

-         ERR_FAIL=1

-         ERR_ABORT=134

-         ERR_SEGFAULT=139

- 

-         # note: each test_*.c program takes first argument which specifies setup

-         #       before executing specified function

-         #       init      semanage handle == NULL

-         #       handle    semanage handle obtained via semanage_handle_create

-         #       conn      connected via semanage_connect

-         #       trans     inside transaction, via semanage_begin_transaction

-         # program returns 1 on error in function, 2 on error in setup

-     rlPhaseEnd

- 

-     rlPhaseStartTest "semanage_seuser_key_create, semanage_seuser_key_free"

-         # FIXME

-         # rlRun "./test_key_create init   $SEUSER" $ERR_ABORT,$ERR_SEGFAULT

-         # rlRun "./test_key_create handle $SEUSER" $ERR_FAIL

-         rlRun "./test_key_create conn   $SEUSER"

-         rlRun "./test_key_create trans  $SEUSER"

-     rlPhaseEnd

- 

-     rlPhaseStartTest "semanage_seuser_key_extract"

-         # FIXME

-         #rlRun "./test_key_extract conn  new"

-         rlRun "./test_key_extract conn  first"

-         # FIXME

-         #rlRun "./test_key_extract trans new"

-         rlRun "./test_key_extract trans first"

-     rlPhaseEnd

- 

-     rlPhaseStartTest "semanage_seuser_compare"

-         rlRun "./test_compare conn  $SEUSER             same"

-         rlRun "./test_compare conn  $SEUSER_NONEXISTENT different"

-         rlRun "./test_compare trans $SEUSER             same"

-         rlRun "./test_compare trans $SEUSER_NONEXISTENT different"

-     rlPhaseEnd

- 

-     rlPhaseStartTest "semanage_seuser_compare2"

-         rlRun "./test_compare2 conn  NULL 0"    $ERR_ABORT,$ERR_SEGFAULT

-         rlRun "./test_compare2 conn  0    NULL" $ERR_ABORT,$ERR_SEGFAULT

-         rlRun "./test_compare2 conn  NULL NULL" $ERR_ABORT,$ERR_SEGFAULT

-         rlRun "./test_compare2 conn  0    0"

-         rlRun "./test_compare2 conn  0    1"

-         rlRun "./test_compare2 trans NULL 0"    $ERR_ABORT,$ERR_SEGFAULT

-         rlRun "./test_compare2 trans 0    NULL" $ERR_ABORT,$ERR_SEGFAULT

-         rlRun "./test_compare2 trans NULL NULL" $ERR_ABORT,$ERR_SEGFAULT

-         rlRun "./test_compare2 trans 0    0"

-         rlRun "./test_compare2 trans 0    1"

-     rlPhaseEnd

- 

-     rlPhaseStartTest "semanage_seuser_count"

-         rlRun "./test_count init"   $ERR_ABORT,$ERR_SEGFAULT

-         rlRun "./test_count handle" $ERR_FAIL

-         rlRun "./test_count conn  $SEUSERS_COUNT"

-         rlRun "./test_count trans $SEUSERS_COUNT"

-     rlPhaseEnd

- 

-     rlPhaseStartTest "semanage_seuser_list"

-         rlRun "./test_list init"   $ERR_ABORT,$ERR_SEGFAULT

-         rlRun "./test_list handle" $ERR_FAIL

-         rlRun "./test_list conn  $SEUSERS_COUNT $SEUSERS"

-         rlRun "./test_list trans $SEUSERS_COUNT $SEUSERS"

-     rlPhaseEnd

- 

-     rlPhaseStartTest "semanage_seuser_iterate"

-         rlRun "./test_iterate init"   $ERR_ABORT,$ERR_SEGFAULT

-         rlRun "./test_iterate handle" $ERR_FAIL

-         rlRun "./test_iterate conn  $SEUSERS"

-         rlRun "./test_iterate trans $SEUSERS"

-     rlPhaseEnd

- 

-     rlPhaseStartTest "semanage_seuser_exists"

-         rlRun "./test_exists conn  $SEUSER_NONEXISTENT 0"

-         rlRun "./test_exists conn  $SEUSER_DEFAULT     1"

-         rlRun "./test_exists conn  $USER               1"

-         rlRun "./test_exists trans $SEUSER_NONEXISTENT 0"

-         rlRun "./test_exists trans $SEUSER_DEFAULT     1"

-         rlRun "./test_exists trans $SEUSER             1"

-     rlPhaseEnd

- 

-     rlPhaseStartTest "semanage_seuser_query"

-         rlRun "./test_query conn  $SEUSER_NONEXISTENT" $ERR_FAIL

-         rlRun "./test_query conn  $SEUSER_DEFAULT"

-         rlRun "./test_query conn  $SEUSER"

-         rlRun "./test_query trans $SEUSER_NONEXISTENT" $ERR_FAIL

-         rlRun "./test_query trans $SEUSER_DEFAULT"

-         rlRun "./test_query trans $SEUSER"

-     rlPhaseEnd

- 

-     rlPhaseStartTest "semanage_seuser_get_name"

-         rlRun "./test_get_name conn  new   NULL"

-         rlRun "./test_get_name conn  first $SEUSER"

-         rlRun "./test_get_name trans new   NULL"

-         rlRun "./test_get_name trans first $SEUSER"

-     rlPhaseEnd

- 

-     rlPhaseStartTest "semanage_seuser_set_name"

-         name="someuser"

-         rlRun "./test_set_name conn  $name"

-         rlRun "./test_set_name trans $name"

-     rlPhaseEnd

- 

-     rlPhaseStartTest "semanage_seuser_get_sename"

-         rlRun "./test_get_sename conn  new   NULL"

-         rlRun "./test_get_sename conn  first $SEUSER_SENAME"

-         rlRun "./test_get_sename trans new   NULL"

-         rlRun "./test_get_sename trans first $SEUSER_SENAME"

-     rlPhaseEnd

- 

-     rlPhaseStartTest "semanage_seuser_set_sename"

-         sename="someuser_u"

-         rlRun "./test_set_sename conn  $sename"

-         rlRun "./test_set_sename trans $sename"

-     rlPhaseEnd

- 

-     rlPhaseStartTest "semanage_seuser_get_mlsrange"

-         rlRun "./test_get_mlsrange conn  new   NULL"

-         rlRun "./test_get_mlsrange conn  first $SEUSER_MLSRANGE"

-         rlRun "./test_get_mlsrange trans new   NULL"

-         rlRun "./test_get_mlsrange trans first $SEUSER_MLSRANGE"

-     rlPhaseEnd

- 

-     rlPhaseStartTest "semanage_seuser_set_mlsrange"

-         mlsrange="c0-s1:c0.c42"

-         rlRun "./test_set_mlsrange conn  $mlsrange"

-         rlRun "./test_set_mlsrange trans $mlsrange"

-     rlPhaseEnd

- 

-     rlPhaseStartTest "semanage_seuser_clone"

-         # FIXME

-         #rlRun "./test_clone conn  new"

-         rlRun "./test_clone conn  first"

-         # FIXME

-         #rlRun "./test_clone trans new"

-         rlRun "./test_clone trans first"

-     rlPhaseEnd

- 

-     rlPhaseStartTest "semanage_seuser_create"

-         # FIXME

-         #rlRun "./test_create init" $ERR_ABORT,$ERR_SEGFAULT

-         #rlRun "./test_create handle" $ERR_ABORT,$ERR_SEGFAULT

-         rlRun "./test_create conn"

-         rlRun "./test_create trans"

-     rlPhaseEnd

- 

-     rlPhaseStartTest "semanage_seuser_modify_local"

-         # function requires transaction

-         #rlRun "./test_modify_local conn  new"   $ERR_FAIL

-         #rlRun "./test_modify_local conn  first" $ERR_FAIL

-         #rlRun "./test_modify_local trans new"   $ERR_FAIL

-         rlRun "./test_modify_local trans first"

-     rlPhaseEnd

-     

-     rlPhaseStartTest "semanage_seuser_del_local"

-         # adding local seuser requires transaction

-         # FIXME

-         #rlRun "./test_del_local trans first new"

-         #rlRun "./test_del_local trans first second"

-         rlRun "./test_del_local trans first first"

-     rlPhaseEnd

- 

-     rlPhaseStartTest "semanage_seuser_exists_local"

-         # adding local seuser requires transaction

-         rlRun "./test_exists_local trans first first  1"

-         rlRun "./test_exists_local trans first second 0"

-     rlPhaseEnd

- 

-     rlPhaseStartTest "semanage_seuser_count_local"

-         # adding local seuser requires transaction

-         # FIXME

-         #rlRun "./test_count_local trans 0"

-         rlRun "./test_count_local trans 1"

-         rlRun "./test_count_local trans 2"

-     rlPhaseEnd

- 

-     rlPhaseStartCleanup

-         testfiles="$(ls -1 test_* | grep -v '\.c' | tr '\n' ' ')"

-         rlRun "rm -f $testfiles"

-     rlPhaseEnd

- rlJournalPrintText

- rlJournalEnd

@@ -1,60 +0,0 @@ 

- #include <stdio.h>

- #include <stdlib.h>

- #include <string.h>

- #include <errno.h>

- #include <semanage/semanage.h>

- 

- #include "functions.c"

- 

- int main (int argc, char **argv) {

-     semanage_handle_t *sh;

-     semanage_seuser_t *seuser;

-     semanage_seuser_t *seuser_clone;

-     int result;

-     const char *str;

-     const char *str_clone;

-     

-     if (argc < 3)

-         exit(2);

-     

-     sh = get_handle(argv[1]);

- 

-     seuser = test_get_seuser(sh, argv[2]);

- 

-     result = semanage_seuser_clone(sh, seuser, &seuser_clone);

-     printf("semanage_seuser_clone(%p, %p): %d\n",

-            (void *) seuser, (void *) seuser_clone, result);

- 

-     if (result < 0) {

-         perror("semanage_seuser_clone");

-         exit(1);

-     }

- 

-     str = semanage_seuser_get_name(seuser);

-     str_clone = semanage_seuser_get_name(seuser_clone);

- 

-     if (strcmp(str, str_clone) != 0) {

-         fprintf(stderr, "Different in get_name\n");

-         exit(1);

-     }

- 

-     str = semanage_seuser_get_sename(seuser);

-     str_clone = semanage_seuser_get_sename(seuser_clone);

- 

-     if (strcmp(str, str_clone) != 0) {

-         fprintf(stderr, "Different in get_sename\n");

-         exit(1);

-     }

- 

-     str = semanage_seuser_get_mlsrange(seuser);

-     str_clone = semanage_seuser_get_mlsrange(seuser_clone);

- 

-     if (strcmp(str, str_clone) != 0) {

-         fprintf(stderr, "Different in get_mlsrange\n");

-         exit(1);

-     }

- 

-     destroy_handle(sh, argv[1]);

- 

-     exit(0);

- }

@@ -1,44 +0,0 @@ 

- #include <stdio.h>

- #include <stdlib.h>

- #include <string.h>

- #include <errno.h>

- #include <semanage/semanage.h>

- 

- #include "functions.c"

- 

- int main (int argc, char **argv) {

-     semanage_handle_t *sh;

-     semanage_seuser_t *seuser;

-     semanage_seuser_key_t *key;

-     int result;

-     

-     if (argc < 3)

-         exit(2);

-     

-     sh = get_handle(argv[1]);

- 

-     seuser = test_get_seuser(sh, "first");

- 

-     key = test_get_key(sh, argv[2]);

- 

-     result = semanage_seuser_compare(seuser, key);

-     printf("semanage_seuser_compare(%p, %p): %d\n",

-            (void *) seuser, (void *) key, result);

- 

-     if (argc >= 4) {

-         if (strcmp(argv[3], "same") == 0 && result != 0) {

-             fprintf(stderr, "Expected same but got different\n");

-             exit(1);

-         }

-         else if (strcmp(argv[3], "different") == 0 && result == 0) {

-             fprintf(stderr, "Expected different but got same\n");

-             exit(1);

-         }

-     }

- 

-     semanage_seuser_key_free(key);

- 

-     destroy_handle(sh, argv[1]);

- 

-     exit(0);

- }

@@ -1,54 +0,0 @@ 

- #include <stdio.h>

- #include <stdlib.h>

- #include <string.h>

- #include <errno.h>

- #include <semanage/semanage.h>

- 

- #include "functions.c"

- 

- int main (int argc, char **argv) {

-     semanage_handle_t *sh;

-     semanage_seuser_t *seuser;

-     semanage_seuser_t *seuser2;

-     int result;

-     int first = -1;

-     int second = -1;

-     

-     if (argc < 4)

-         exit(2);

-     

-     sh = get_handle(argv[1]);

- 

-     if (strcmp(argv[2], "NULL") == 0) {

-         seuser = NULL;

-     }

-     else {

-         first = strtol(argv[2], NULL, 10);

-         seuser = test_get_seuser_nth(sh, first);

-     }

- 

-     if (strcmp(argv[3], "NULL") == 0) {

-         seuser2 = NULL;

-     }

-     else {

-         second = strtol(argv[3], NULL, 10);

-         seuser2 = test_get_seuser_nth(sh, second);

-     }

- 

-     result = semanage_seuser_compare2(seuser, seuser2);

-     printf("semanage_seuser_compare(%p, %p): %d\n",

-            (void *) seuser, (void *) seuser2, result);

- 

-     if (first == second && result != 0) {

-         fprintf(stderr, "Expected same but got different\n");

-         exit(1);

-     }

-     else if (first != second && result == 0) {

-         fprintf(stderr, "Expected different but got same\n");

-         exit(1);

-     }

- 

-     destroy_handle(sh, argv[1]);

- 

-     exit(0);

- }

@@ -1,34 +0,0 @@ 

- #include <stdio.h>

- #include <stdlib.h>

- #include <string.h>

- #include <errno.h>

- #include <semanage/semanage.h>

- 

- #include "functions.c"

- 

- int main (int argc, char **argv) {

-     semanage_handle_t *sh;

-     unsigned int response;

-     int result;

-     

-     if (argc < 2)

-         exit(2);

-     

-     sh = get_handle(argv[1]);

- 

-     result = semanage_seuser_count(sh, &response);

-     printf("semanage_seuser_count(%p, %p): %d, response: %u\n",

-            (void *) sh, (void *) &response, result, response);

-     

-     if (result < 0) {

-         perror("semanage_seuser_count");

-         exit(1);

-     }

- 

-     if (argc >= 3)

-         check_result_int(argv[2], response);

- 

-     destroy_handle(sh, argv[1]);

- 

-     exit(0);

- }

@@ -1,46 +0,0 @@ 

- #include <stdio.h>

- #include <stdlib.h>

- #include <string.h>

- #include <errno.h>

- #include <semanage/semanage.h>

- 

- #include "functions.c"

- 

- int main (int argc, char **argv) {

-     semanage_handle_t *sh;

-     semanage_seuser_t *seuser;

-     int result;

-     unsigned int response;

-     int num;

-     

-     if (argc < 2)

-         exit(2);

-     

-     sh = get_handle(argv[1]);

- 

-     num = strtol(argv[2], NULL, 10);

- 

-     for (int i = 0; i < num; i++) {

-         seuser = test_get_seuser_nth(sh, i);

- 

-         test_add_local_seuser(sh, seuser);

-     }

- 

-     result = semanage_seuser_count_local(sh, &response);

-     printf("semanage_seuser_count_local(%p, %p): %d, response: %d\n",

-            (void *) sh, (void *) &response, result, response);

- 

-     if (result < 0) {

-         perror("semanage_seuser_count_local");

-         exit(1);

-     }

- 

-     if (argc >= 3)

-         check_result_int(argv[2], response);

- 

-     test_del_local_seuser(sh, seuser);

- 

-     destroy_handle(sh, argv[1]);

- 

-     exit(0);

- }

@@ -1,53 +0,0 @@ 

- #include <stdio.h>

- #include <stdlib.h>

- #include <string.h>

- #include <errno.h>

- #include <semanage/semanage.h>

- 

- #include "functions.c"

- 

- int main (int argc, char **argv) {

-     semanage_handle_t *sh;

-     semanage_seuser_t *seuser;

-     int result;

-     const char *str;

-     

-     if (argc < 2)

-         exit(2);

-     

-     sh = get_handle(argv[1]);

- 

-     result = semanage_seuser_create(sh, &seuser);

-     printf("semanage_seuser_create(%p, %p): %d\n",

-            (void *) sh, (void *) seuser, result);

- 

-     if (result < 0) {

-         perror("semanage_seuser_create");

-         exit(1);

-     }

- 

-     str = semanage_seuser_get_name(seuser);

- 

-     if (str != NULL) {

-         fprintf(stderr, "Expected name == NULL, got %s\n", str);

-         exit(1);

-     }

- 

-     str = semanage_seuser_get_sename(seuser);

- 

-     if (str != NULL) {

-         fprintf(stderr, "Expected sename == NULL, got %s\n", str);

-         exit(1);

-     }

- 

-     str = semanage_seuser_get_mlsrange(seuser);

- 

-     if (str != NULL) {

-         fprintf(stderr, "Expected mlsrange == NULL, got %s\n", str);

-         exit(1);

-     }

- 

-     destroy_handle(sh, argv[1]);

- 

-     exit(0);

- }

@@ -1,64 +0,0 @@ 

- #include <stdio.h>

- #include <stdlib.h>

- #include <string.h>

- #include <errno.h>

- #include <semanage/semanage.h>

- 

- #include "functions.c"

- 

- int main (int argc, char **argv) {

-     semanage_handle_t *sh;

-     semanage_seuser_t *seuser;

-     semanage_seuser_t *seuser_del;

-     semanage_seuser_key_t *key;

-     semanage_seuser_t **records;

-     int result;

-     unsigned int count;

-     

-     if (argc < 4)

-         exit(2);

-     

-     sh = get_handle(argv[1]);

- 

-     seuser = test_get_seuser(sh, argv[2]);

- 

-     test_add_local_seuser(sh, seuser);

- 

-     seuser_del = test_get_seuser(sh, argv[3]);

- 

-     result = semanage_seuser_key_extract(sh, seuser_del, &key);

-     printf("semanage_seuser_key_extract(%p, %p, %p): %d\n",

-            (void *) sh, (void *) seuser_del, (void *) &key, result);

- 

-     if (result < 0) {

-         perror("semanage_seuser_key_extract");

-         exit(2);

-     }

- 

-     result = semanage_seuser_del_local(sh, key);

-     printf("semanage_seuser_del_local(%p, %p): %d\n",

-            (void *) seuser, (void *) key, result);

- 

-     if (result < 0) {

-         perror("semanage_seuser_del_local");

-         exit(1);

-     }

- 

-     result = semanage_seuser_list_local(sh, &records, &count);

-     printf("semanage_seuser_list_local(%p, %p, %p): %d\n",

-            (void *) sh, (void *) &records, (void *) &count, result);

-     

-     if (result < 0) {

-         perror("semanage_seuser_list_local");

-         exit(2);

-     }

- 

-     if (count != 0) {

-         fprintf(stderr, "Number of local seusers is not 0!\n");

-         exit(1);

-     }

- 

-     destroy_handle(sh, argv[1]);

- 

-     exit(0);

- }

@@ -1,37 +0,0 @@ 

- #include <stdio.h>

- #include <stdlib.h>

- #include <string.h>

- #include <errno.h>

- #include <semanage/semanage.h>

- 

- #include "functions.c"

- 

- int main (int argc, char **argv) {

-     semanage_handle_t *sh;

-     semanage_seuser_key_t *key;

-     int result;

-     int response;

-     

-     if (argc < 3)

-         exit(2);

-     

-     sh = get_handle(argv[1]);

- 

-     key = test_get_key(sh, argv[2]);

- 

-     result = semanage_seuser_exists(sh, key, &response);

-     printf("semanage_seuser_exists(%p, %p, %p): %d, response: %d\n",

-            (void *) sh, (void *) key, (void *) &response, result, response);

-     

-     if (result < 0) {

-         perror("semanage_seuser_exists");

-         exit(1);

-     }

- 

-     if (argc >= 4)

-         check_result_int(argv[3], response);

- 

-     destroy_handle(sh, argv[1]);

- 

-     exit(0);

- }

@@ -1,59 +0,0 @@ 

- #include <stdio.h>

- #include <stdlib.h>

- #include <string.h>

- #include <errno.h>

- #include <semanage/semanage.h>

- 

- #include "functions.c"

- 

- int main (int argc, char **argv) {

-     semanage_handle_t *sh;

-     semanage_seuser_t *seuser;

-     semanage_seuser_t *seuser_exists;

-     semanage_seuser_key_t *key;

-     int result;

-     int response;

-     int exp;

-     

-     if (argc < 4)

-         exit(2);

-     

-     sh = get_handle(argv[1]);

- 

-     seuser = test_get_seuser(sh, argv[2]);

-     seuser_exists = test_get_seuser(sh, argv[3]);

- 

-     test_add_local_seuser(sh, seuser);

- 

-     result = semanage_seuser_key_extract(sh, seuser_exists, &key);

-     printf("semanage_seuser_key_extract(%p, %p, %p): %d\n",

-            (void *) sh, (void *) seuser_exists, (void *) &key, result); 

-     if (result < 0) {

-         perror("semanage_seuser_key_extract");

-         exit(2);

-     }

- 

-     result = semanage_seuser_exists_local(sh, key, &response);

-     printf("semanage_seuser_exists_local(%p, %p, %p): %d\n",

-            (void *) sh, (void *) key, (void *) &response, result);

- 

-     if (result < 0) {

-         perror("semanage_seuser_exists_local");

-         exit(1);

-     }

- 

-     if (argc >= 5) {

-         exp = strtol(argv[4], NULL, 10);

- 

-         if (response != exp) {

-             fprintf(stderr, "Expected %d but got %d\n", exp, response);

-             exit(1);

-         }

-     }

- 

-     test_del_local_seuser(sh, seuser);

- 

-     destroy_handle(sh, argv[1]);

- 

-     exit(0);

- }

@@ -1,32 +0,0 @@ 

- #include <stdio.h>

- #include <stdlib.h>

- #include <string.h>

- #include <errno.h>

- #include <semanage/semanage.h>

- 

- #include "functions.c"

- 

- int main (int argc, char **argv) {

-     semanage_handle_t *sh;

-     semanage_seuser_t *seuser;

-     

-     if (argc < 4)

-         exit(2);

-     

-     sh = get_handle(argv[1]);

- 

-     seuser = test_get_seuser(sh, argv[2]);

- 

-     const char *name = semanage_seuser_get_mlsrange(seuser);

-     printf("semanage_seuser_get_mlsrange(%p): %s\n",

-            (void *) seuser, name);

- 

-     if (strcmp_null(argv[3], name) != 0) {

-         fprintf(stderr, "Expected %s but got %s\n", argv[2], name);

-         exit(1);

-     }

- 

-     destroy_handle(sh, argv[1]);

- 

-     exit(0);

- }

@@ -1,32 +0,0 @@ 

- #include <stdio.h>

- #include <stdlib.h>

- #include <string.h>

- #include <errno.h>

- #include <semanage/semanage.h>

- 

- #include "functions.c"

- 

- int main (int argc, char **argv) {

-     semanage_handle_t *sh;

-     semanage_seuser_t *seuser;

-     

-     if (argc < 4)

-         exit(2);

-     

-     sh = get_handle(argv[1]);

- 

-     seuser = test_get_seuser(sh, argv[2]);

- 

-     const char *name = semanage_seuser_get_name(seuser);

-     printf("semanage_seuser_get_name(%p): %s\n",

-            (void *) seuser, name);

- 

-     if (strcmp_null(argv[3], name) != 0) {

-         fprintf(stderr, "Expected %s but got %s\n", argv[2], name);

-         exit(1);

-     }

- 

-     destroy_handle(sh, argv[1]);

- 

-     exit(0);

- }

@@ -1,32 +0,0 @@ 

- #include <stdio.h>

- #include <stdlib.h>

- #include <string.h>

- #include <errno.h>

- #include <semanage/semanage.h>

- 

- #include "functions.c"

- 

- int main (int argc, char **argv) {

-     semanage_handle_t *sh;

-     semanage_seuser_t *seuser;

-     

-     if (argc < 4)

-         exit(2);

-     

-     sh = get_handle(argv[1]);

- 

-     seuser = test_get_seuser(sh, argv[2]);

- 

-     const char *name = semanage_seuser_get_sename(seuser);

-     printf("semanage_seuser_get_sename(%p): %s\n",

-            (void *) seuser, name);

- 

-     if (strcmp_null(argv[3], name) != 0) {

-         fprintf(stderr, "Expected %s but got %s\n", argv[2], name);

-         exit(1);

-     }

- 

-     destroy_handle(sh, argv[1]);

- 

-     exit(0);

- }

@@ -1,49 +0,0 @@ 

- #include <stdio.h>

- #include <stdlib.h>

- #include <string.h>

- #include <errno.h>

- #include <semanage/semanage.h>

- 

- #include "functions.c"

- 

- int counter = 0;

- 

- int handler(const semanage_seuser_t *record, void *varg) {

-     char **args = (char **) varg;

-     

-     const char *name = semanage_seuser_get_name(record);

- 

-     if (strcmp(name, args[2 + counter++]) != 0)

-         return -1;

- 

-     return 0;

- }

- 

- int main (int argc, char **argv) {

-     semanage_handle_t *sh;

-     int result;

-     

-     if (argc < 2)

-         exit(2);

-     

-     sh = get_handle(argv[1]);

- 

-     char **param = NULL;

- 

-     if (argc >= 3) {

-         param = argv;

-     }

- 

-     result = semanage_seuser_iterate(sh, &handler, (void *) param);

-     printf("semanage_seuser_iterate(%p, %p, %p): %d\n",

-            (void *) sh, (void *) &handler, (void *) param, result);

-     

-     if (result < 0) {

-         perror("semanage_seuser_iterate");

-         exit(1);

-     }

- 

-     destroy_handle(sh, argv[1]);

- 

-     exit(0);

- }

@@ -1,39 +0,0 @@ 

- #include <stdio.h>

- #include <stdlib.h>

- #include <string.h>

- #include <errno.h>

- #include <semanage/semanage.h>

- 

- #include "functions.c"

- 

- int main (int argc, char **argv) {

-     semanage_handle_t *sh;

-     semanage_seuser_key_t *key;

-     const char *name;

-     int result;

-     

-     if (argc < 3)

-         exit(2);

-     

-     sh = get_handle(argv[1]);

- 

-     if (strcmp(argv[2], "NULL") == 0)

-         name = NULL;

-     else

-         name = argv[2];

- 

-     result = semanage_seuser_key_create(sh, name, &key);

-     printf("semanage_seuser_key_create(%p, %s, %p): %d\n",

-            (void *) sh, name, (void *) &key, result);

- 

-     if (result < 0 || key == NULL) {

-         perror("semanage_seuser_key_create");

-         exit(1);

-     }

- 

-     semanage_seuser_key_free(key);

- 

-     destroy_handle(sh, argv[1]);

- 

-     exit(0);

- }

@@ -1,45 +0,0 @@ 

- #include <stdio.h>

- #include <stdlib.h>

- #include <string.h>

- #include <errno.h>

- #include <semanage/semanage.h>

- 

- #include "functions.c"

- 

- int main (int argc, char **argv) {

-     semanage_handle_t *sh;

-     semanage_seuser_t *seuser;

-     semanage_seuser_key_t *key;

-     int result;

-     

-     if (argc < 3)

-         exit(2);

-     

-     sh = get_handle(argv[1]);

- 

-     seuser = test_get_seuser(sh, argv[2]);

- 

-     result = semanage_seuser_key_extract(sh, seuser, &key);

-     printf("semanage_seuser_key_extract(%p, %p, %p): %d\n",

-            (void *) sh, (void *) seuser, (void *) &key, result);

- 

-     if (result < 0) {

-         perror("semanage_seuser_key_extract");

-         exit(1);

-     }

- 

-     result = semanage_seuser_compare(seuser, key);

-     printf("semanage_seuser_compare(%p, %p): %d\n",

-            (void *) seuser, (void *) key, result);

- 

-     if (result != 0) {

-         perror("semanage_seuser_compare");

-         exit(1);

-     }

- 

-     semanage_seuser_key_free(key);

- 

-     destroy_handle(sh, argv[1]);

- 

-     exit(0);

- }

@@ -1,63 +0,0 @@ 

- #include <stdio.h>

- #include <stdlib.h>

- #include <string.h>

- #include <errno.h>

- #include <semanage/semanage.h>

- 

- #include "functions.c"

- 

- int main (int argc, char **argv) {

-     semanage_handle_t *sh;

-     semanage_seuser_t **records;

-     unsigned int count;

-     int result;

-     

-     if (argc < 2)

-         exit(2);

-     

-     sh = get_handle(argv[1]);

- 

-     result = semanage_seuser_list(sh, &records, &count);

-     printf("semanage_seuser_list(%p, %p, %p): %d",

-            (void *) sh, (void *) &records, (void *) &count, result);

-     

-     if (result < 0) {

-         perror("semanage_seuser_list");

-         exit(1);

-     }

- 

-     printf(", count: %u, records: ", count);

- 

-     const char *name;

- 

-     for (unsigned int i = 0; i < count; i++) {

-         name = semanage_seuser_get_name(records[i]);

-         printf("%p (%s), ", (void *) records[i], name);

-     }

- 

-     printf("\n");

- 

-     if (argc >= 3) {

-         unsigned int exp_count = strtoul(argv[2], NULL, 10);

-         

-         if (count != exp_count) {

-             printf("Expected %u but got %u\n", exp_count, count);

-             exit(1);

-         }

- 

-         const char *name;

- 

-         for (unsigned int i = 0; i < count; i++) {

-             name = semanage_seuser_get_name(records[i]);

- 

-             if (strcmp(name, argv[3 + i]) != 0) {

-                 printf("Expected %s but got %s\n", name, argv[3 + i]);

-                 exit(1);

-             }

-         }

-     }

- 

-     destroy_handle(sh, argv[1]);

- 

-     exit(0);

- }

@@ -1,64 +0,0 @@ 

- #include <stdio.h>

- #include <stdlib.h>

- #include <string.h>

- #include <errno.h>

- #include <semanage/semanage.h>

- 

- #include "functions.c"

- 

- int main (int argc, char **argv) {

-     semanage_handle_t *sh;

-     semanage_seuser_t *seuser;

-     semanage_seuser_key_t *key;

-     semanage_seuser_t **records;

-     int result;

-     unsigned int count;

-     

-     if (argc < 3)

-         exit(2);

-     

-     sh = get_handle(argv[1]);

- 

-     seuser = test_get_seuser(sh, argv[2]);

- 

-     result = semanage_seuser_key_extract(sh, seuser, &key);

-     printf("semanage_seuser_key_extract(%p, %p, %p): %d\n",

-            (void *) sh, (void *) seuser, (void *) &key, result);

- 

-     if (result < 0) {

-         perror("semanage_seuser_key_extract");

-         exit(2);

-     }

- 

-     result = semanage_seuser_modify_local(sh, key, seuser);

-     printf("semanage_seuser_modify_local(%p, %p, %p): %d\n",

-            (void *) seuser, (void *) key, (void *) seuser, result);

- 

-     if (result < 0) {

-         perror("semanage_seuser_modify_local");

-         exit(1);

-     }

- 

-     result = semanage_seuser_list_local(sh, &records, &count);

-     printf("semanage_seuser_list_local(%p, %p, %p): %d\n",

-            (void *) sh, (void *) &records, (void *) &count, result);

-     

-     if (result < 0) {

-         perror("semanage_seuser_list_local");

-         exit(2);

-     }

- 

-     if (count != 1) {

-         fprintf(stderr, "Number of local seusers is %u, expected 1!\n", count);

-         exit(1);

-     }

- 

-     if (semanage_seuser_compare(records[0], key) != 0) {

-         fprintf(stderr, "Local seuser is different!\n");

-         exit(1);

-     }

- 

-     destroy_handle(sh, argv[1]);

- 

-     exit(0);

- }

@@ -1,50 +0,0 @@ 

- #include <stdio.h>

- #include <stdlib.h>

- #include <string.h>

- #include <errno.h>

- #include <semanage/semanage.h>

- 

- #include "functions.c"

- 

- int main (int argc, char **argv) {

-     semanage_handle_t *sh;

-     semanage_seuser_key_t *key;

-     semanage_seuser_t *response;

-     int result;

-     

-     if (argc < 3)

-         exit(2);

-     

-     sh = get_handle(argv[1]);

- 

-     result = semanage_seuser_key_create(sh, argv[2], &key);

-     printf("semanage_seuser_key_create(%p, %s, %p): %d\n",

-            (void *) sh, argv[2], (void *) &key, result);

- 

-     if (result < 0 || key == NULL) {

-         perror("semanage_seuser_key_create");

-         exit(2);

-     }

- 

-     result = semanage_seuser_query(sh, key, &response);

-     printf("semanage_seuser_query(%p, %p, %p): %d, response: %p\n",

-            (void *) sh, (void *) key, (void *) &response, result, (void *) response);

-     

-     if (result < 0) {

-         perror("semanage_seuser_query");

-         exit(1);

-     }

- 

-     const char *name = semanage_seuser_get_name(response);

-     printf("semanage_seuser_get_name(%p): %s\n",

-            (void *) response, name);

- 

-     if (strcmp(argv[2], name) != 0) {

-         perror("semanage_seuser_get_name");

-         exit(2);

-     }

- 

-     destroy_handle(sh, argv[1]);

- 

-     exit(0);

- }

@@ -1,62 +0,0 @@ 

- #include <stdio.h>

- #include <stdlib.h>

- #include <string.h>

- #include <errno.h>

- #include <semanage/semanage.h>

- 

- #include "functions.c"

- 

- int main (int argc, char **argv) {

-     semanage_handle_t *sh;

-     semanage_seuser_t *user;

-     int result;

-     const char *mlsrange;

-     

-     if (argc < 3)

-         exit(2);

-     

-     sh = get_handle(argv[1]);

- 

-     user = test_get_seuser(sh, "first");

- 

-     if (strcmp(argv[2], "NULL") == 0)

-         mlsrange = NULL;

-     else 

-         mlsrange = argv[2];

- 

-     const char *old_mlsrange = semanage_seuser_get_mlsrange(user);

-     printf("semanage_seuser_get_mlsrange(%p): %s\n",

-            (void *) user, old_mlsrange);

- 

-     if (old_mlsrange == NULL) {

-         perror("semanage_seuser_get_mlsrange");

-         exit(2);

-     }

- 

-     if (strcmp(old_mlsrange, mlsrange) == 0) {

-         printf("New mlsrange is the same\n");

-         exit(2);

-     }

- 

-     result = semanage_seuser_set_mlsrange(sh, user, mlsrange);

-     printf("semanage_seuser_set_mlsrange(%p, %p, %s): %d\n",

-            (void *) sh, (void *) user, mlsrange, result);

- 

-     if (result < 0) {

-         perror("semanage_seuser_set_mlsrange");

-         exit(1);

-     }

- 

-     const char *new_mlsrange = semanage_seuser_get_mlsrange(user);

-     printf("semanage_seuser_get_mlsrange(%p): %s\n",

-            (void *) user, new_mlsrange);

- 

-     if (strcmp(new_mlsrange, mlsrange) != 0) {

-         perror("semanage_seuser_get_mlsrange");

-         exit(1);

-     }

- 

-     destroy_handle(sh, argv[1]);

- 

-     exit(0);

- }

@@ -1,62 +0,0 @@ 

- #include <stdio.h>

- #include <stdlib.h>

- #include <string.h>

- #include <errno.h>

- #include <semanage/semanage.h>

- 

- #include "functions.c"

- 

- int main (int argc, char **argv) {

-     semanage_handle_t *sh;

-     semanage_seuser_t *user;

-     int result;

-     const char *name;

-     

-     if (argc < 3)

-         exit(2);

-     

-     sh = get_handle(argv[1]);

- 

-     user = test_get_seuser(sh, "first");

- 

-     if (strcmp(argv[2], "NULL") == 0)

-         name = NULL;

-     else 

-         name = argv[2];

- 

-     const char *old_name = semanage_seuser_get_name(user);

-     printf("semanage_seuser_get_name(%p): %s\n",

-            (void *) user, old_name);

- 

-     if (old_name == NULL) {

-         perror("semanage_seuser_get_name");

-         exit(2);

-     }

- 

-     if (strcmp(old_name, name) == 0) {

-         printf("New name is the same\n");

-         exit(2);

-     }

- 

-     result = semanage_seuser_set_name(sh, user, name);

-     printf("semanage_seuser_set_name(%p, %p, %s): %d\n",

-            (void *) sh, (void *) user, name, result);

- 

-     if (result < 0) {

-         perror("semanage_seuser_set_name");

-         exit(1);

-     }

- 

-     const char *new_name = semanage_seuser_get_name(user);

-     printf("semanage_seuser_get_name(%p): %s\n",

-            (void *) user, new_name);

- 

-     if (strcmp(new_name, name) != 0) {

-         perror("semanage_seuser_get_name");

-         exit(1);

-     }

- 

-     destroy_handle(sh, argv[1]);

- 

-     exit(0);

- }

@@ -1,62 +0,0 @@ 

- #include <stdio.h>

- #include <stdlib.h>

- #include <string.h>

- #include <errno.h>

- #include <semanage/semanage.h>

- 

- #include "functions.c"

- 

- int main (int argc, char **argv) {

-     semanage_handle_t *sh;

-     semanage_seuser_t *user;

-     int result;

-     const char *name;

-     

-     if (argc < 3)

-         exit(2);

-     

-     sh = get_handle(argv[1]);

- 

-     user = test_get_seuser(sh, "first");

- 

-     if (strcmp(argv[2], "NULL") == 0)

-         name = NULL;

-     else 

-         name = argv[2];

- 

-     const char *old_name = semanage_seuser_get_sename(user);

-     printf("semanage_seuser_get_sename(%p): %s\n",

-            (void *) user, old_name);

- 

-     if (old_name == NULL) {

-         perror("semanage_seuser_get_sename");

-         exit(2);

-     }

- 

-     if (strcmp(old_name, name) == 0) {

-         printf("New name is the same\n");

-         exit(2);

-     }

- 

-     result = semanage_seuser_set_sename(sh, user, name);

-     printf("semanage_seuser_set_sename(%p, %p, %s): %d\n",

-            (void *) sh, (void *) user, name, result);

- 

-     if (result < 0) {

-         perror("semanage_seuser_set_sename");

-         exit(1);

-     }

- 

-     const char *new_name = semanage_seuser_get_sename(user);

-     printf("semanage_seuser_get_sename(%p): %s\n",

-            (void *) user, new_name);

- 

-     if (strcmp(new_name, name) != 0) {

-         perror("semanage_seuser_get_sename");

-         exit(1);

-     }

- 

-     destroy_handle(sh, argv[1]);

- 

-     exit(0);

- }

The current libsemanage/semanage-seuser-functions fails on Fedora 28 - http://plautrba.fedorapeople.org/test.selinux-libsemanage-semanage-seuser-functions.log

@janzarsky created a new sanity test which should cover semanage-handle-functions and semanage-seuser-functions - first two patches in this PR

The third patch is changes needed to run libsemanage/sanity-test. I'm not sure if this can be done in a better way, compatible with Red Hat Enterprise Linux test suite so it needs to be reviewed.

At the same time, libsemanage/sanity-tests fails on Fedora 28 - http://plautrba.fedorapeople.org/test.sanity-tests.log

1 new commit added

  • rename helper functions
6 years ago

Pull-Request has been closed by plautrba

6 years ago
Metadata
Changes Summary 70
+7 -7
file renamed
libsemanage/semanage-handle-functions/Makefile
libsemanage/sanity-tests/Makefile
+3
file added
libsemanage/sanity-tests/PURPOSE
+71
file added
libsemanage/sanity-tests/runtest.sh
+1
file added
libsemanage/sanity-tests/tests/.gitignore
+35
file added
libsemanage/sanity-tests/tests/Makefile
+771
file added
libsemanage/sanity-tests/tests/functions.c
+163
file added
libsemanage/sanity-tests/tests/functions.h
+110
file added
libsemanage/sanity-tests/tests/libsemanage-tests.c
+1227
file added
libsemanage/sanity-tests/tests/test_bool.c
+48
file added
libsemanage/sanity-tests/tests/test_bool.h
+1189
file added
libsemanage/sanity-tests/tests/test_fcontext.c
+41
file added
libsemanage/sanity-tests/tests/test_fcontext.h
+432
file added
libsemanage/sanity-tests/tests/test_handle.c
+30
file added
libsemanage/sanity-tests/tests/test_handle.h
+542
file added
libsemanage/sanity-tests/tests/test_ibendport.c
+32
file added
libsemanage/sanity-tests/tests/test_ibendport.h
+34
file added
libsemanage/sanity-tests/tests/test_ibendport.te
+23
file added
libsemanage/sanity-tests/tests/test_ibendport_module.h
+566
file added
libsemanage/sanity-tests/tests/test_iface.c
+39
file added
libsemanage/sanity-tests/tests/test_iface.h
+1347
file added
libsemanage/sanity-tests/tests/test_module.c
+42
file added
libsemanage/sanity-tests/tests/test_module.h
+697
file added
libsemanage/sanity-tests/tests/test_node.c
+43
file added
libsemanage/sanity-tests/tests/test_node.h
+108
file added
libsemanage/sanity-tests/tests/test_other.c
+25
file added
libsemanage/sanity-tests/tests/test_other.h
+834
file added
libsemanage/sanity-tests/tests/test_port.c
+43
file added
libsemanage/sanity-tests/tests/test_port.h
+1203
file added
libsemanage/sanity-tests/tests/test_seuser.c
+40
file added
libsemanage/sanity-tests/tests/test_seuser.h
+604
file added
libsemanage/sanity-tests/tests/test_user.c
+41
file added
libsemanage/sanity-tests/tests/test_user.h
-3
file removed
libsemanage/semanage-handle-functions/PURPOSE
-132
file removed
libsemanage/semanage-handle-functions/functions.c
-29
file removed
libsemanage/semanage-handle-functions/plan.txt
-122
file removed
libsemanage/semanage-handle-functions/runtest.sh
-32
file removed
libsemanage/semanage-handle-functions/test_access_check.c
-33
file removed
libsemanage/semanage-handle-functions/test_connect.c
-15
file removed
libsemanage/semanage-handle-functions/test_handle_create.c
-32
file removed
libsemanage/semanage-handle-functions/test_is_connected.c
-32
file removed
libsemanage/semanage-handle-functions/test_is_managed.c
-32
file removed
libsemanage/semanage-handle-functions/test_mls_enabled.c
-53
file removed
libsemanage/semanage-handle-functions/test_root.c
-34
file removed
libsemanage/semanage-handle-functions/test_transaction.c
-63
file removed
libsemanage/semanage-seuser-functions/Makefile
-3
file removed
libsemanage/semanage-seuser-functions/PURPOSE
-263
file removed
libsemanage/semanage-seuser-functions/functions.c
-1
file removed
libsemanage/semanage-seuser-functions/main.fmf
-255
file removed
libsemanage/semanage-seuser-functions/runtest.sh
-60
file removed
libsemanage/semanage-seuser-functions/test_clone.c
-44
file removed
libsemanage/semanage-seuser-functions/test_compare.c
-54
file removed
libsemanage/semanage-seuser-functions/test_compare2.c
-34
file removed
libsemanage/semanage-seuser-functions/test_count.c
-46
file removed
libsemanage/semanage-seuser-functions/test_count_local.c
-53
file removed
libsemanage/semanage-seuser-functions/test_create.c
-64
file removed
libsemanage/semanage-seuser-functions/test_del_local.c
-37
file removed
libsemanage/semanage-seuser-functions/test_exists.c
-59
file removed
libsemanage/semanage-seuser-functions/test_exists_local.c
-32
file removed
libsemanage/semanage-seuser-functions/test_get_mlsrange.c
-32
file removed
libsemanage/semanage-seuser-functions/test_get_name.c
-32
file removed
libsemanage/semanage-seuser-functions/test_get_sename.c
-49
file removed
libsemanage/semanage-seuser-functions/test_iterate.c
-39
file removed
libsemanage/semanage-seuser-functions/test_key_create.c
-45
file removed
libsemanage/semanage-seuser-functions/test_key_extract.c
-63
file removed
libsemanage/semanage-seuser-functions/test_list.c
-64
file removed
libsemanage/semanage-seuser-functions/test_modify_local.c
-50
file removed
libsemanage/semanage-seuser-functions/test_query.c
-62
file removed
libsemanage/semanage-seuser-functions/test_set_mlsrange.c
-62
file removed
libsemanage/semanage-seuser-functions/test_set_name.c
-62
file removed
libsemanage/semanage-seuser-functions/test_set_sename.c