Return-Path: Received: from sog-mx-1.v43.ch3.sourceforge.com ([172.29.43.191] helo=mx.sourceforge.net) by sfs-ml-2.v29.ch3.sourceforge.com with esmtp (Exim 4.76) (envelope-from ) id 1RoBCZ-0006j4-Iq for ltp-list@lists.sourceforge.net; Fri, 20 Jan 2012 09:56:07 +0000 Received: from mx1.redhat.com ([209.132.183.28]) by sog-mx-1.v43.ch3.sourceforge.com with esmtp (Exim 4.76) id 1RoBCT-0005Xt-Vz for ltp-list@lists.sourceforge.net; Fri, 20 Jan 2012 09:56:07 +0000 Received: from int-mx11.intmail.prod.int.phx2.redhat.com (int-mx11.intmail.prod.int.phx2.redhat.com [10.5.11.24]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id q0K9tuLQ027830 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Fri, 20 Jan 2012 04:55:56 -0500 From: Eryu Guan Date: Fri, 20 Jan 2012 17:54:08 +0800 Message-Id: <1327053248-18415-2-git-send-email-eguan@redhat.com> In-Reply-To: <1327053248-18415-1-git-send-email-eguan@redhat.com> References: <1327053248-18415-1-git-send-email-eguan@redhat.com> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="------------1.7.7.5" Subject: [LTP] [PATCH v2 2/2] syscalls/setxattr03: new testcase to test setxattr(2) List-Id: Linux Test Project General Discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: ltp-list-bounces@lists.sourceforge.net To: ltp-list@lists.sourceforge.net This is a multi-part message in MIME format. --------------1.7.7.5 Content-Type: text/plain; charset=UTF-8; format=fixed Content-Transfer-Encoding: 8bit setxattr(2) to immutable and append-only files should get EPERM There are 2 test cases: 1. Set attribute to a immutable file, setxattr(2) should return -1 and set errno to EPERM 2. Set attribute to a append-only file, setxattr(2) should return -1 and set errno to EPERM Signed-off-by: Eryu Guan --- v1->v2: 1. Use sizeof() instead of strlen to compute length of string at compile time 2. Use better described log message runtest/syscalls | 1 + testcases/kernel/syscalls/setxattr/setxattr03.c | 222 +++++++++++++++++++++++ 2 files changed, 223 insertions(+), 0 deletions(-) create mode 100644 testcases/kernel/syscalls/setxattr/setxattr03.c --------------1.7.7.5 Content-Type: text/x-patch; name="0002-syscalls-setxattr03-new-testcase-to-test-setxattr-2.patch" Content-Transfer-Encoding: 8bit Content-Disposition: attachment; filename="0002-syscalls-setxattr03-new-testcase-to-test-setxattr-2.patch" diff --git a/runtest/syscalls b/runtest/syscalls index d786746..37dfc7e 100644 --- a/runtest/syscalls +++ b/runtest/syscalls @@ -1027,6 +1027,7 @@ setuid04_16 setuid04_16 setxattr01 setxattr01 setxattr02 setxattr02 +setxattr03 setxattr03 shmat01 shmat01 shmat02 shmat02 diff --git a/testcases/kernel/syscalls/setxattr/setxattr03.c b/testcases/kernel/syscalls/setxattr/setxattr03.c new file mode 100644 index 0000000..268e24e --- /dev/null +++ b/testcases/kernel/syscalls/setxattr/setxattr03.c @@ -0,0 +1,222 @@ +/* + * Copyright (C) 2012 Red Hat, Inc. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of version 2 of the GNU General Public + * License as published by the Free Software Foundation. + * + * This program is distributed in the hope that it would be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * + * Further, this software is distributed without any warranty that it + * is free of the rightful claim of any third person regarding + * infringement or the like. Any license provided herein, whether + * implied or otherwise, applies only to this software file. Patent + * licenses, if any, provided herein do not apply to combinations of + * this program with other software, or any other product whatsoever. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301, USA. + */ + +/* + * setxattr(2) to immutable and append-only files should get EPERM + * + * There are 2 test cases: + * 1. Set attribute to a immutable file, setxattr(2) should return -1 + * and set errno to EPERM + * 2. Set attribute to a append-only file, setxattr(2) should return + * -1 and set errno to EPERM + */ + +#include "config.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#ifdef HAVE_ATTR_XATTR_H +#include +#endif +#include + +#include "test.h" +#include "usctest.h" + +char *TCID = "setxattr03"; + +#ifdef HAVE_ATTR_XATTR_H +#define XATTR_TEST_KEY "user.testkey" +#define XATTR_TEST_VALUE "this is a test value" +#define XATTR_TEST_VALUE_SIZE (sizeof(XATTR_TEST_VALUE) - 1) + +#define IMMU_FILE "setxattr03immutable" +#define APPEND_FILE "setxattr03appendonly" + +#define set_immutable_on(fd) fsetflag(fd, 1, 1) +#define set_immutable_off(fd) fsetflag(fd, 0, 1) +#define set_append_on(fd) fsetflag(fd, 1, 0) +#define set_append_off(fd) fsetflag(fd, 0, 0) + +struct test_case { + char *desc; + char *fname; + char *key; + char *value; + size_t size; + int flags; + int exp_err; +}; +static struct test_case tc[] = { + { /* case 00, set attr to immutable file */ + .desc = "Set attr to immutable file", + .fname = IMMU_FILE, + .key = XATTR_TEST_KEY, + .value = XATTR_TEST_VALUE, + .size = XATTR_TEST_VALUE_SIZE, + .flags = XATTR_CREATE, + .exp_err = EPERM, + }, + { /* case 01, set attr to append-only file */ + .desc = "Set attr to append-only file", + .fname = APPEND_FILE, + .key = XATTR_TEST_KEY, + .value = XATTR_TEST_VALUE, + .size = XATTR_TEST_VALUE_SIZE, + .flags = XATTR_CREATE, + .exp_err = EPERM, + }, +}; + +static void setup(void); +static void cleanup(void); + +static int immu_fd; +static int append_fd; + +int TST_TOTAL = sizeof(tc) / sizeof(tc[0]); + +int main(int argc, char *argv[]) +{ + int lc; + int i; + char *msg; + + msg = parse_opts(argc, argv, NULL, NULL); + if (msg != NULL) + tst_brkm(TBROK, tst_exit, "OPTION PARSING ERROR - %s", msg); + + setup(); + + for (lc = 0; TEST_LOOPING(lc); lc++) { + Tst_count = 0; + + for (i = 0; i < TST_TOTAL; i++) { + TEST(setxattr(tc[i].fname, tc[i].key, tc[i].value, + tc[i].size, tc[i].flags)); + + if (TEST_ERRNO == tc[i].exp_err) { + tst_resm(TPASS | TTERRNO, tc[i].desc); + } else { + tst_resm(TFAIL | TTERRNO, "%s - expected errno" + " %d - Got", tc[i].desc, tc[i].exp_err); + } + } + } + + cleanup(); + tst_exit(); +} + +static int fsetflag(int fd, int on, int immutable) +{ + int fsflags = 0; + int fsfl; + + if (ioctl(fd, FS_IOC_GETFLAGS, &fsflags) < 0) + return 1; + + if (immutable) + fsfl = FS_IMMUTABLE_FL; + else + fsfl = FS_APPEND_FL; + + if (on) + fsflags |= fsfl; + else + fsflags &= ~fsfl; + + if (ioctl(fd, FS_IOC_SETFLAGS, &fsflags) < 0) + return 1; + + return 0; +} + +static void setup(void) +{ + int fd; + + tst_require_root(NULL); + + tst_tmpdir(); + + /* Test for xattr support */ + fd = creat("testfile", 0644); + if (fd == -1) + tst_brkm(TBROK | TERRNO, cleanup, "Create testfile failed"); + close(fd); + if (setxattr("testfile", "user.test", "test", 4, XATTR_CREATE) == -1) + if (errno == ENOTSUP) + tst_brkm(TCONF, cleanup, "No xattr support in fs or " + "fs mounted without user_xattr option"); + unlink("testfile"); + + /* Create test files and set file immutable or append-only */ + immu_fd = creat(IMMU_FILE, 0644); + if (immu_fd == -1) + tst_brkm(TBROK | TERRNO, cleanup, "Create test file(%s) failed", + IMMU_FILE); + if (set_immutable_on(immu_fd)) + tst_brkm(TBROK | TERRNO, cleanup, "Set %s immutable failed", + IMMU_FILE); + + append_fd = creat(APPEND_FILE, 0644); + if (append_fd == -1) + tst_brkm(TBROK | TERRNO, cleanup, "Create test file(%s) failed", + APPEND_FILE); + if (set_append_on(append_fd)) + tst_brkm(TBROK | TERRNO, cleanup, "Set %s append-only failed", + APPEND_FILE); + + TEST_PAUSE; +} + +static void cleanup(void) +{ + if (set_immutable_off(immu_fd)) + tst_resm(TWARN | TERRNO, "Unset %s immutable failed", + IMMU_FILE); + if (set_append_off(append_fd)) + tst_resm(TWARN | TERRNO, "Unset %s append-only failed", + APPEND_FILE); + close(immu_fd); + close(append_fd); + + TEST_CLEANUP; + tst_rmdir(); +} +#else /* HAVE_ATTR_XATTR_H */ +int main(int argc, char *argv[]) +{ + tst_brkm(TCONF, NULL, " does not exist."); +} +#endif /* HAVE_ATTR_XATTR_H */ --------------1.7.7.5 Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline ------------------------------------------------------------------------------ Keep Your Developer Skills Current with LearnDevNow! The most comprehensive online learning library for Microsoft developers is just $99.99! Visual Studio, SharePoint, SQL - plus HTML5, CSS3, MVC3, Metro Style Apps, more. Free future releases when you subscribe now! http://p.sf.net/sfu/learndevnow-d2d --------------1.7.7.5 Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline _______________________________________________ Ltp-list mailing list Ltp-list@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/ltp-list --------------1.7.7.5--