From 526ee2b8adf7547497cbf9a4e662cf4ccaf85750 Mon Sep 17 00:00:00 2001 From: Jarkko Sakkinen Date: Mon, 15 Nov 2010 07:56:19 -0800 Subject: [PATCH] Separated tests into three suites. --- tests/Makefile.am | 19 +++- tests/{check_smack.c => check_rules.c} | 91 +---------------- tests/check_users.c | 122 +++++++++++++++++++++++ tests/check_xattr.c | 176 +++++++++++++++++++++++++++++++++ 4 files changed, 313 insertions(+), 95 deletions(-) rename tests/{check_smack.c => check_rules.c} (76%) create mode 100644 tests/check_users.c create mode 100644 tests/check_xattr.c diff --git a/tests/Makefile.am b/tests/Makefile.am index c3d07c2..4fd6816 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -1,5 +1,14 @@ -TESTS = check_smack -check_PROGRAMS = check_smack -check_smack_SOURCES = check_smack.c $(top_builddir)/src/smack.h -check_smack_CFLAGS = @CHECK_CFLAGS@ -check_smack_LDADD = $(top_builddir)/src/libsmack.la @CHECK_LIBS@ +TESTS = check_rules check_users check_xattr +check_PROGRAMS = check_rules check_users check_xattr + +check_rules_SOURCES = check_rules.c $(top_builddir)/src/smack.h +check_rules_CFLAGS = @CHECK_CFLAGS@ +check_rules_LDADD = $(top_builddir)/src/libsmack.la @CHECK_LIBS@ + +check_users_SOURCES = check_users.c $(top_builddir)/src/smack.h +check_users_CFLAGS = @CHECK_CFLAGS@ +check_users_LDADD = $(top_builddir)/src/libsmack.la @CHECK_LIBS@ + +check_xattr_SOURCES = check_xattr.c $(top_builddir)/src/smack.h +check_xattr_CFLAGS = @CHECK_CFLAGS@ +check_xattr_LDADD = $(top_builddir)/src/libsmack.la @CHECK_LIBS@ diff --git a/tests/check_smack.c b/tests/check_rules.c similarity index 76% rename from tests/check_smack.c rename to tests/check_rules.c index a70fa5d..5b13661 100644 --- a/tests/check_smack.c +++ b/tests/check_rules.c @@ -166,88 +166,6 @@ START_TEST(test_have_access_removed_rule) } END_TEST -START_TEST(test_rw_users) -{ - int rc; - smack_users_t users = smack_create_users(); - fail_unless(users != NULL, "Users creation failed"); - rc = smack_read_users_from_file(users, "data/rw_users-in.txt"); - fail_unless(rc == 0, "Failed to read users"); - rc = smack_write_users_to_file(users, "rw_users-result.txt"); - fail_unless(rc == 0, "Failed to write ruleset"); - rc = files_equal("rw_users-result.txt", "data/rw_users-excepted.txt"); - fail_unless(rc == 1, "Unexcepted result"); - smack_destroy_users(users); -} -END_TEST - -START_TEST(test_set_smack_to_file) -{ - FILE *file; - int rc; - char *smack; - - file = fopen("set_smack-dummy.txt", "w"); - fprintf(file, "dummy\n"); - fclose(file); - - rc = smack_set_smack_to_file("set_smack-dummy.txt", "Apple", 0); - fail_unless(rc == 0, "Failed to set SMACK64"); - - rc = smack_get_smack_from_file("set_smack-dummy.txt", &smack, 0); - fail_unless(rc == 0, "Failed to get SMACK64"); - - rc = strcmp(smack, "Apple"); - fail_unless(rc == 0, "smack %s not equal to Apple", smack); - - free(smack); -} -END_TEST - -START_TEST(test_set_smack_to_file_symlink) -{ - FILE *file; - int rc; - char *smack; - - symlink("unknown.txt", "set_smack-symlink.txt"); - - rc = smack_set_smack_to_file("set_smack-symlink.txt", "Apple", SMACK_XATTR_SYMLINK); - fail_unless(rc == 0, "Failed to set SMACK64"); - - rc = smack_get_smack_from_file("set_smack-symlink.txt", &smack, SMACK_XATTR_SYMLINK); - fail_unless(rc == 0, "Failed to get SMACK64"); - - rc = strcmp(smack, "Apple"); - fail_unless(rc == 0, "smack %s not equal to Apple", smack); - - free(smack); -} -END_TEST - -START_TEST(test_set_smackexec_to_file) -{ - FILE *file; - int rc; - char *smack; - - file = fopen("set_smack-dummy.txt", "w"); - fprintf(file, "dummy\n"); - fclose(file); - - rc = smack_set_smackexec_to_file("set_smack-dummy.txt", "Apple", 0); - fail_unless(rc == 0, "Failed to set SMACK64EXEC"); - - rc = smack_get_smackexec_from_file("set_smack-dummy.txt", &smack, 0); - fail_unless(rc == 0, "Failed to get SMACK64EXEC"); - - rc = strcmp(smack, "Apple"); - fail_unless(rc == 0, "smack %s not equal to Apple", smack); - - free(smack); -} -END_TEST - Suite *ruleset_suite (void) { Suite *s; @@ -265,13 +183,6 @@ Suite *ruleset_suite (void) tcase_add_test(tc_core, test_remove_rules_by_object); tcase_add_test(tc_core, test_have_access_rule); tcase_add_test(tc_core, test_have_access_removed_rule); - tcase_add_test(tc_core, test_rw_users); - suite_add_tcase(s, tc_core); - - tc_core = tcase_create("Security attributes"); - tcase_add_test(tc_core, test_set_smack_to_file); - tcase_add_test(tc_core, test_set_smack_to_file_symlink); - tcase_add_test(tc_core, test_set_smackexec_to_file); suite_add_tcase(s, tc_core); return s; @@ -282,7 +193,7 @@ int main(void) int nfailed; Suite *s = ruleset_suite(); SRunner *sr = srunner_create(s); - srunner_set_log(sr, "check_smack.log"); + srunner_set_log(sr, "check_rules.log"); srunner_run_all(sr, CK_ENV); nfailed = srunner_ntests_failed(sr); srunner_free(sr); diff --git a/tests/check_users.c b/tests/check_users.c new file mode 100644 index 0000000..cf829bb --- /dev/null +++ b/tests/check_users.c @@ -0,0 +1,122 @@ +/* + * This file is part of libsmack + * + * Copyright (C) 2010 Nokia Corporation + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License + * version 2.1 as published by the Free Software Foundation. + * + * 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 + * + * Author: Jarkko Sakkinen + */ + +#include +#include +#include +#include "../src/smack.h" + +static int files_equal(const char *filename1, const char *filename2); + +START_TEST(test_rw_users) +{ + int rc; + smack_users_t users = smack_create_users(); + fail_unless(users != NULL, "Users creation failed"); + rc = smack_read_users_from_file(users, "data/rw_users-in.txt"); + fail_unless(rc == 0, "Failed to read users"); + rc = smack_write_users_to_file(users, "rw_users-result.txt"); + fail_unless(rc == 0, "Failed to write ruleset"); + rc = files_equal("rw_users-result.txt", "data/rw_users-excepted.txt"); + fail_unless(rc == 1, "Unexcepted result"); + smack_destroy_users(users); +} +END_TEST + +Suite *ruleset_suite (void) +{ + Suite *s; + TCase *tc_core; + + s = suite_create("Smack"); + + tc_core = tcase_create("Users"); + tcase_add_test(tc_core, test_rw_users); + suite_add_tcase(s, tc_core); + + return s; +} + +int main(void) +{ + int nfailed; + Suite *s = ruleset_suite(); + SRunner *sr = srunner_create(s); + srunner_set_log(sr, "check_users.log"); + srunner_run_all(sr, CK_ENV); + nfailed = srunner_ntests_failed(sr); + srunner_free(sr); + return (nfailed == 0) ? EXIT_SUCCESS : EXIT_FAILURE; +} + +static int files_equal(const char *filename1, const char *filename2) +{ + FILE *fp1 = NULL; + FILE *fp2 = NULL; + char ch1, ch2; + int rc = 0; + + fp1 = fopen(filename1, "rb"); + if (fp1 == NULL) { + goto out; + } + + fp2 = fopen(filename2, "rb"); + if (fp2 == NULL) { + goto out; + } + + rc = 1; + for (;;) { + if (feof(fp1) && feof(fp2)) + break; + + if (feof(fp1) || feof(fp2)) { + rc = 0; + break; + } + + ch1 = fgetc(fp1); + if (ferror(fp1)) { + rc = 0; + break; + } + + ch2 = fgetc(fp2); + if (ferror(fp2)) { + rc = 0; + break; + } + + if (ch1 != ch2) { + rc = 0; + break; + } + } +out: + if (fp1 != NULL) + fclose(fp1); + if (fp2 != NULL) + fclose(fp2); + return rc; +} + diff --git a/tests/check_xattr.c b/tests/check_xattr.c new file mode 100644 index 0000000..d74eb9c --- /dev/null +++ b/tests/check_xattr.c @@ -0,0 +1,176 @@ +/* + * This file is part of libsmack + * + * Copyright (C) 2010 Nokia Corporation + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License + * version 2.1 as published by the Free Software Foundation. + * + * 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 + * + * Author: Jarkko Sakkinen + */ + +#include +#include +#include +#include "../src/smack.h" + +static int files_equal(const char *filename1, const char *filename2); + +START_TEST(test_set_smack_to_file) +{ + FILE *file; + int rc; + char *smack; + + file = fopen("set_smack-dummy.txt", "w"); + fprintf(file, "dummy\n"); + fclose(file); + + rc = smack_set_smack_to_file("set_smack-dummy.txt", "Apple", 0); + fail_unless(rc == 0, "Failed to set SMACK64"); + + rc = smack_get_smack_from_file("set_smack-dummy.txt", &smack, 0); + fail_unless(rc == 0, "Failed to get SMACK64"); + + rc = strcmp(smack, "Apple"); + fail_unless(rc == 0, "smack %s not equal to Apple", smack); + + free(smack); +} +END_TEST + +START_TEST(test_set_smack_to_file_symlink) +{ + FILE *file; + int rc; + char *smack; + + symlink("unknown.txt", "set_smack-symlink.txt"); + + rc = smack_set_smack_to_file("set_smack-symlink.txt", "Apple", SMACK_XATTR_SYMLINK); + fail_unless(rc == 0, "Failed to set SMACK64"); + + rc = smack_get_smack_from_file("set_smack-symlink.txt", &smack, SMACK_XATTR_SYMLINK); + fail_unless(rc == 0, "Failed to get SMACK64"); + + rc = strcmp(smack, "Apple"); + fail_unless(rc == 0, "smack %s not equal to Apple", smack); + + free(smack); +} +END_TEST + +START_TEST(test_set_smackexec_to_file) +{ + FILE *file; + int rc; + char *smack; + + file = fopen("set_smack-dummy.txt", "w"); + fprintf(file, "dummy\n"); + fclose(file); + + rc = smack_set_smackexec_to_file("set_smack-dummy.txt", "Apple", 0); + fail_unless(rc == 0, "Failed to set SMACK64EXEC"); + + rc = smack_get_smackexec_from_file("set_smack-dummy.txt", &smack, 0); + fail_unless(rc == 0, "Failed to get SMACK64EXEC"); + + rc = strcmp(smack, "Apple"); + fail_unless(rc == 0, "smack %s not equal to Apple", smack); + + free(smack); +} +END_TEST + +Suite *ruleset_suite (void) +{ + Suite *s; + TCase *tc_core; + + s = suite_create("Smack"); + + tc_core = tcase_create("Xattr"); + tcase_add_test(tc_core, test_set_smack_to_file); + tcase_add_test(tc_core, test_set_smack_to_file_symlink); + tcase_add_test(tc_core, test_set_smackexec_to_file); + suite_add_tcase(s, tc_core); + + return s; +} + +int main(void) +{ + int nfailed; + Suite *s = ruleset_suite(); + SRunner *sr = srunner_create(s); + srunner_set_log(sr, "check_xattr.log"); + srunner_run_all(sr, CK_ENV); + nfailed = srunner_ntests_failed(sr); + srunner_free(sr); + return (nfailed == 0) ? EXIT_SUCCESS : EXIT_FAILURE; +} + +static int files_equal(const char *filename1, const char *filename2) +{ + FILE *fp1 = NULL; + FILE *fp2 = NULL; + char ch1, ch2; + int rc = 0; + + fp1 = fopen(filename1, "rb"); + if (fp1 == NULL) { + goto out; + } + + fp2 = fopen(filename2, "rb"); + if (fp2 == NULL) { + goto out; + } + + rc = 1; + for (;;) { + if (feof(fp1) && feof(fp2)) + break; + + if (feof(fp1) || feof(fp2)) { + rc = 0; + break; + } + + ch1 = fgetc(fp1); + if (ferror(fp1)) { + rc = 0; + break; + } + + ch2 = fgetc(fp2); + if (ferror(fp2)) { + rc = 0; + break; + } + + if (ch1 != ch2) { + rc = 0; + break; + } + } +out: + if (fp1 != NULL) + fclose(fp1); + if (fp2 != NULL) + fclose(fp2); + return rc; +} + -- 2.7.4