* [LTP] [PATCH] read/read02.c: cleanup
@ 2014-03-28 7:20 Zeng Linggang
2014-03-28 7:22 ` [LTP] [PATCH] read/read02.c: add new error number tests Zeng Linggang
2014-04-29 16:10 ` [LTP] [PATCH] read/read02.c: cleanup chrubis
0 siblings, 2 replies; 10+ messages in thread
From: Zeng Linggang @ 2014-03-28 7:20 UTC (permalink / raw)
To: ltp-list
* Delete some useless commtents and variable.
* Use SAFE_* macros.
* Some cleanup.
Signed-off-by: Zeng Linggang <zenglg.jy@cn.fujitsu.com>
---
testcases/kernel/syscalls/read/read02.c | 217 +++++++++++---------------------
1 file changed, 74 insertions(+), 143 deletions(-)
diff --git a/testcases/kernel/syscalls/read/read02.c b/testcases/kernel/syscalls/read/read02.c
index 8681662..8aabe93 100644
--- a/testcases/kernel/syscalls/read/read02.c
+++ b/testcases/kernel/syscalls/read/read02.c
@@ -1,63 +1,36 @@
/*
+ * Copyright (c) International Business Machines Corp., 2001
+ * 07/2001 Ported by Wayne Boyer
*
- * Copyright (c) International Business Machines Corp., 2001
- *
- * 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 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.
+ * 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, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
-
/*
- * NAME
- * read02.c
- *
* DESCRIPTION
- * test 1:
- * Does read return -1 if file descriptor is not valid, check for EBADF
+ * test 1:
+ * Read with an invalid file descriptor, and expect an EBADF.
*
* test 2:
- * Check if read sets EISDIR, if the fd refers to a directory
- *
- * test 3:
- * Check if read sets EFAULT, if buf is -1.
- *$
- * ALGORITHM
- * test 1:
- * Read with an invalid file descriptor, and expect an EBADF
+ * The parameter passed to read is a directory, check if the errno is
+ * set to EISDIR.
*
- * test 2:
- * The parameter passed to read is a directory, check if the errno is
- * set to EISDIR.
- *
- * test 3:
- * Pass buf = -1 as a parmeter to read, expect an EFAULT.
- *$
- * USAGE: <for command-line>
- * read02 [-c n] [-e] [-i n] [-I x] [-P x] [-t]
- * where, -c n : Run n copies concurrently.
- * -e : Turn on errno logging.
- * -i n : Execute test n times.
- * -I x : Execute test for x seconds.
- * -P x : Pause for x seconds between iterations.
- * -t : Turn on syscall timing.
- *
- * HISTORY
- * 07/2001 Ported by Wayne Boyer
- *
- * RESTRICTIONS
- * None
+ * test 3:
+ * Pass buf = -1 as a parmeter to read, expect an EFAULT.
*/
-#define _GNU_SOURCE /* for O_DIRECTORY */
+
+#define _GNU_SOURCE
+
#include <stdio.h>
#include <errno.h>
#include <unistd.h>
@@ -65,43 +38,31 @@
#include <sys/mman.h>
#include "test.h"
#include "usctest.h"
+#include "safe_macros.h"
-void cleanup(void);
-void setup(void);
+#define TEST_FILE "test_file"
char *TCID = "read02";
-char file[BUFSIZ];
-char fname[100] = "/tmp/tstfile";
-
-int exp_enos[] = { EBADF, EISDIR, EFAULT, 0 };
-
-int badfd = -1;
-int fd2, fd3;
-char buf[BUFSIZ];
+static int badfd = -1;
+static int fd2, fd3;
+static char buf[BUFSIZ];
+static int exp_enos[] = { EBADF, EISDIR, EFAULT, 0 };
-struct test_case_t {
+static struct test_case_t {
int *fd;
void *buf;
- int error;
+ int exp_error;
} TC[] = {
- /* the file descriptor is invalid - EBADF */
- {
- &badfd, buf, EBADF},
- /* the file descriptor is a directory - EISDIR */
- {
- &fd2, buf, EISDIR,},
-#ifndef UCLINUX
- /* Skip since uClinux does not implement memory protection */
- /* the buffer is invalid - EFAULT */
- {
- &fd3, (void *)-1, EFAULT}
-#endif
+ {&badfd, buf, EBADF},
+ {&fd2, buf, EISDIR},
+ {&fd3, (void *)-1, EFAULT},
};
-int TST_TOTAL = sizeof(TC) / sizeof(*TC);
-
-char *bad_addr = 0;
+int TST_TOTAL = ARRAY_SIZE(TC);
+static void setup(void);
+static void cleanup(void);
+static void read_verify(const struct test_case_t *);
int main(int ac, char **av)
{
@@ -109,98 +70,68 @@ int main(int ac, char **av)
int lc;
char *msg;
- if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) {
+ msg = parse_opts(ac, av, NULL, NULL);
+ if (msg != NULL)
tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
- }
setup();
- /* set up the expected errnos */
TEST_EXP_ENOS(exp_enos);
- /*
- * The following loop checks looping state if -i option given
- */
for (lc = 0; TEST_LOOPING(lc); lc++) {
- /* reset tst_count in case we are looping */
tst_count = 0;
-
- /* loop through the test cases */
- for (i = 0; i < TST_TOTAL; i++) {
-
- TEST(read(*TC[i].fd, TC[i].buf, 1));
-
- if (TEST_RETURN != -1) {
- tst_resm(TFAIL, "call succeeded unexpectedly");
- continue;
- }
-
- TEST_ERROR_LOG(TEST_ERRNO);
-
- if (TEST_ERRNO == TC[i].error) {
- tst_resm(TPASS, "expected failure - "
- "errno = %d : %s", TEST_ERRNO,
- strerror(TEST_ERRNO));
- } else {
- tst_resm(TFAIL, "unexpected error - %d : %s - "
- "expected %d", TEST_ERRNO,
- strerror(TEST_ERRNO), TC[i].error);
- }
- }
+ for (i = 0; i < TST_TOTAL; i++)
+ read_verify(&TC[i]);
}
cleanup();
tst_exit();
}
-/*
- * setup() - performs all ONE TIME setup for this test
- */
-void setup(void)
+static void setup(void)
{
-
tst_sig(NOFORK, DEF_HANDLER, cleanup);
- /* create a temporary filename */
- sprintf(fname, "%s.%d", fname, getpid());
-
TEST_PAUSE;
- if ((fd2 = open("/tmp", O_DIRECTORY)) == -1) {
- tst_brkm(TBROK, cleanup, "open of fd2 failed");
- }
+ tst_tmpdir();
- if ((fd3 = open(fname, O_RDWR | O_CREAT, 0666)) == -1) {
- tst_brkm(TBROK, cleanup, "open of fd3 (temp file) failed");
- }
+ fd2 = SAFE_OPEN(cleanup, "/tmp", O_DIRECTORY);
- if (write(fd3, "A", 1) != 1) {
- tst_brkm(TBROK, cleanup, "can't write to fd3");
- }
- close(fd3);
- if ((fd3 = open(fname, O_RDWR | O_CREAT, 0666)) == -1) {
- tst_brkm(TBROK, cleanup, "open of fd3 (temp file) failed");
+ SAFE_FILE_PRINTF(cleanup, TEST_FILE, "A");
+
+ fd3 = SAFE_OPEN(cleanup, TEST_FILE, O_RDWR | O_CREAT, 0666);
+}
+
+static void read_verify(const struct test_case_t *test)
+{
+ TEST(read(*test->fd, test->buf, 1));
+
+ if (TEST_RETURN != -1) {
+ tst_resm(TFAIL, "call succeeded unexpectedly");
+ return;
}
-#if !defined(UCLINUX)
- bad_addr = mmap(0, 1, PROT_NONE,
- MAP_PRIVATE_EXCEPT_UCLINUX | MAP_ANONYMOUS, 0, 0);
- if (bad_addr == MAP_FAILED) {
- tst_brkm(TBROK, cleanup, "mmap failed");
+
+ TEST_ERROR_LOG(TEST_ERRNO);
+
+ if (TEST_ERRNO == test->exp_error) {
+ tst_resm(TPASS | TTERRNO, "expected failure - errno = %d : %s",
+ TEST_ERRNO, strerror(TEST_ERRNO));
+ } else {
+ tst_resm(TFAIL | TTERRNO,
+ "unexpected error - %d : %s - expected %d",
+ TEST_ERRNO, strerror(TEST_ERRNO), test->exp_error);
}
- TC[2].buf = bad_addr;
-#endif
}
-/*
- * cleanup() - performs all the ONE TIME cleanup for this test at completion
- * or premature exit.
- */
-void cleanup(void)
+static void cleanup(void)
{
- /*
- * print timing status if that option was specified.
- * print errno log if that option was specified.
- */
TEST_CLEANUP;
- unlink(fname);
+ if (fd3 > 0)
+ close(fd3);
+
+ if (fd2 > 0)
+ close(fd2);
+
+ tst_rmdir();
}
--
1.8.4.2
------------------------------------------------------------------------------
_______________________________________________
Ltp-list mailing list
Ltp-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ltp-list
^ permalink raw reply related [flat|nested] 10+ messages in thread* [LTP] [PATCH] read/read02.c: add new error number tests
2014-03-28 7:20 [LTP] [PATCH] read/read02.c: cleanup Zeng Linggang
@ 2014-03-28 7:22 ` Zeng Linggang
2014-04-29 16:31 ` chrubis
2014-04-29 16:10 ` [LTP] [PATCH] read/read02.c: cleanup chrubis
1 sibling, 1 reply; 10+ messages in thread
From: Zeng Linggang @ 2014-03-28 7:22 UTC (permalink / raw)
To: ltp-list
Add EINVAL errnor nuber tests for read(2):
* The file was opened with the O_DIRECT flag, and the alignment of the user
buffer was not multiples of the logical block size of the file system,
expect an EINVAL.
* The file was opened with the O_DIRECT flag, and transfer sizes was not
multiples of the logical block size of the file system, expect an EINVAL.
Signed-off-by: Zeng Linggang <zenglg.jy@cn.fujitsu.com>
---
testcases/kernel/syscalls/read/read02.c | 53 ++++++++++++++++++++++++++++-----
1 file changed, 46 insertions(+), 7 deletions(-)
diff --git a/testcases/kernel/syscalls/read/read02.c b/testcases/kernel/syscalls/read/read02.c
index 8aabe93..0e227fe 100644
--- a/testcases/kernel/syscalls/read/read02.c
+++ b/testcases/kernel/syscalls/read/read02.c
@@ -27,6 +27,16 @@
*
* test 3:
* Pass buf = -1 as a parmeter to read, expect an EFAULT.
+ *
+ * test 4:
+ * The file was opened with the O_DIRECT flag, and the alignment of the
+ * user buffer was not multiples of the logical block size of the file
+ * system, expect an EINVAL.
+ *
+ * test 5:
+ * The file was opened with the O_DIRECT flag, and transfer sizes was not
+ * multiples of the logical block size of the file system, expect an
+ * EINVAL.
*/
#define _GNU_SOURCE
@@ -41,22 +51,30 @@
#include "safe_macros.h"
#define TEST_FILE "test_file"
+#define TEST_FILE4 "/root/test_file4"
char *TCID = "read02";
static int badfd = -1;
-static int fd2, fd3;
+static int fd2, fd3, fd4;
static char buf[BUFSIZ];
-static int exp_enos[] = { EBADF, EISDIR, EFAULT, 0 };
+static void *outside_buf = (void *)-1;
+static void *temp;
+static void *addr4;
+static void *addr5;
+static int exp_enos[] = { EBADF, EISDIR, EFAULT, EINVAL, 0 };
static struct test_case_t {
int *fd;
- void *buf;
+ void **buf;
+ size_t count;
int exp_error;
} TC[] = {
- {&badfd, buf, EBADF},
- {&fd2, buf, EISDIR},
- {&fd3, (void *)-1, EFAULT},
+ {&badfd, (void **)&buf, 1, EBADF},
+ {&fd2, (void **)&buf, 1, EISDIR},
+ {&fd3, &outside_buf, 1, EFAULT},
+ {&fd4, &addr4, 4096, EINVAL},
+ {&fd4, &addr5, 1, EINVAL},
};
int TST_TOTAL = ARRAY_SIZE(TC);
@@ -89,6 +107,11 @@ int main(int ac, char **av)
static void setup(void)
{
+ if ((tst_kvercmp(2, 4, 10)) < 0)
+ tst_brkm(TCONF, NULL, "This test needs kernel 2.4.10 or newer");
+
+ tst_require_root(NULL);
+
tst_sig(NOFORK, DEF_HANDLER, cleanup);
TEST_PAUSE;
@@ -100,11 +123,19 @@ static void setup(void)
SAFE_FILE_PRINTF(cleanup, TEST_FILE, "A");
fd3 = SAFE_OPEN(cleanup, TEST_FILE, O_RDWR | O_CREAT, 0666);
+
+ temp = SAFE_MALLOC(cleanup, 4096*10);
+
+ addr4 = (char *)(((long)buf & (~4095)) + 1);
+ addr5 = (char *)((long)buf & (~4095));
+
+ SAFE_FILE_PRINTF(cleanup, TEST_FILE4, "A");
+ fd4 = SAFE_OPEN(cleanup, TEST_FILE4, O_RDWR | O_DIRECT, 0777);
}
static void read_verify(const struct test_case_t *test)
{
- TEST(read(*test->fd, test->buf, 1));
+ TEST(read(*test->fd, *test->buf, test->count));
if (TEST_RETURN != -1) {
tst_resm(TFAIL, "call succeeded unexpectedly");
@@ -127,6 +158,14 @@ static void cleanup(void)
{
TEST_CLEANUP;
+ free(temp);
+
+ if (fd4 > 0)
+ close(fd4);
+
+ if (unlink(TEST_FILE4) < 0)
+ tst_resm(TWARN | TERRNO, "unlink(\"%s\") failed", TEST_FILE4);
+
if (fd3 > 0)
close(fd3);
--
1.8.4.2
------------------------------------------------------------------------------
_______________________________________________
Ltp-list mailing list
Ltp-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ltp-list
^ permalink raw reply related [flat|nested] 10+ messages in thread* Re: [LTP] [PATCH] read/read02.c: add new error number tests
2014-03-28 7:22 ` [LTP] [PATCH] read/read02.c: add new error number tests Zeng Linggang
@ 2014-04-29 16:31 ` chrubis
[not found] ` <1399262735.3177.14.camel@G08JYZSD130126>
0 siblings, 1 reply; 10+ messages in thread
From: chrubis @ 2014-04-29 16:31 UTC (permalink / raw)
To: Zeng Linggang; +Cc: ltp-list
Hi!
> static struct test_case_t {
> int *fd;
> - void *buf;
> + void **buf;
> + size_t count;
> int exp_error;
> } TC[] = {
> - {&badfd, buf, EBADF},
> - {&fd2, buf, EISDIR},
> - {&fd3, (void *)-1, EFAULT},
> + {&badfd, (void **)&buf, 1, EBADF},
> + {&fd2, (void **)&buf, 1, EISDIR},
> + {&fd3, &outside_buf, 1, EFAULT},
> + {&fd4, &addr4, 4096, EINVAL},
> + {&fd4, &addr5, 1, EINVAL},
> };
>
> int TST_TOTAL = ARRAY_SIZE(TC);
> @@ -89,6 +107,11 @@ int main(int ac, char **av)
>
> static void setup(void)
> {
> + if ((tst_kvercmp(2, 4, 10)) < 0)
> + tst_brkm(TCONF, NULL, "This test needs kernel 2.4.10 or newer");
Linux 2.4.10 is from 2001 I would say that it's old enough so we can
expect that O_DIRECT is generally available.
> + tst_require_root(NULL);
Does O_DIRECT require root?
(I haven't found anything in read man page that would suggests so)
> tst_sig(NOFORK, DEF_HANDLER, cleanup);
>
> TEST_PAUSE;
> @@ -100,11 +123,19 @@ static void setup(void)
> SAFE_FILE_PRINTF(cleanup, TEST_FILE, "A");
>
> fd3 = SAFE_OPEN(cleanup, TEST_FILE, O_RDWR | O_CREAT, 0666);
> +
> + temp = SAFE_MALLOC(cleanup, 4096*10);
> +
> + addr4 = (char *)(((long)buf & (~4095)) + 1);
> + addr5 = (char *)((long)buf & (~4095));
You can allocate aligned buffer with memalign() instead.
> + SAFE_FILE_PRINTF(cleanup, TEST_FILE4, "A");
> + fd4 = SAFE_OPEN(cleanup, TEST_FILE4, O_RDWR | O_DIRECT, 0777);
> }
>
> static void read_verify(const struct test_case_t *test)
> {
> - TEST(read(*test->fd, test->buf, 1));
> + TEST(read(*test->fd, *test->buf, test->count));
>
> if (TEST_RETURN != -1) {
> tst_resm(TFAIL, "call succeeded unexpectedly");
> @@ -127,6 +158,14 @@ static void cleanup(void)
> {
> TEST_CLEANUP;
>
> + free(temp);
> +
> + if (fd4 > 0)
> + close(fd4);
> +
> + if (unlink(TEST_FILE4) < 0)
> + tst_resm(TWARN | TERRNO, "unlink(\"%s\") failed", TEST_FILE4);
There is no need to unlink files if you call tst_rmdir(). You only need
to close filedescriptors.
> if (fd3 > 0)
> close(fd3);
>
--
Cyril Hrubis
chrubis@suse.cz
------------------------------------------------------------------------------
"Accelerate Dev Cycles with Automated Cross-Browser Testing - For FREE
Instantly run your Selenium tests across 300+ browser/OS combos. Get
unparalleled scalability from the best Selenium testing platform available.
Simple to use. Nothing to install. Get started now for free."
http://p.sf.net/sfu/SauceLabs
_______________________________________________
Ltp-list mailing list
Ltp-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ltp-list
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [LTP] [PATCH] read/read02.c: cleanup
2014-03-28 7:20 [LTP] [PATCH] read/read02.c: cleanup Zeng Linggang
2014-03-28 7:22 ` [LTP] [PATCH] read/read02.c: add new error number tests Zeng Linggang
@ 2014-04-29 16:10 ` chrubis
1 sibling, 0 replies; 10+ messages in thread
From: chrubis @ 2014-04-29 16:10 UTC (permalink / raw)
To: Zeng Linggang; +Cc: ltp-list
Hi!
> -#ifndef UCLINUX
> - /* Skip since uClinux does not implement memory protection */
> - /* the buffer is invalid - EFAULT */
> - {
> - &fd3, (void *)-1, EFAULT}
> -#endif
> + {&badfd, buf, EBADF},
> + {&fd2, buf, EISDIR},
> + {&fd3, (void *)-1, EFAULT},
> };
Removing the ifdefs breaks the testcases on uClinux, please keep them
there.
> -#if !defined(UCLINUX)
> - bad_addr = mmap(0, 1, PROT_NONE,
> - MAP_PRIVATE_EXCEPT_UCLINUX | MAP_ANONYMOUS, 0, 0);
> - if (bad_addr == MAP_FAILED) {
> - tst_brkm(TBROK, cleanup, "mmap failed");
> +
> + TEST_ERROR_LOG(TEST_ERRNO);
> +
> + if (TEST_ERRNO == test->exp_error) {
> + tst_resm(TPASS | TTERRNO, "expected failure - errno = %d : %s",
> + TEST_ERRNO, strerror(TEST_ERRNO));
> + } else {
> + tst_resm(TFAIL | TTERRNO,
> + "unexpected error - %d : %s - expected %d",
> + TEST_ERRNO, strerror(TEST_ERRNO), test->exp_error);
> }
> - TC[2].buf = bad_addr;
> -#endif
And removing the mmap() possibly breaks the testcases on platforms where
there is something mapped on address (void*)-1. Looking at the git log,
this may be case for some 64 bit machines (at least this is what the
patch from 2002 says).
Given that we have quite a lot of testcases that passes addresses that
are expected to generate EFAULT to various syscalls we should unify the
way these are generated.
I would go for creating tst_bad_addr() function that would replace all
the adhoc (and posibly wrong) definitions. I will look into that asap.
--
Cyril Hrubis
chrubis@suse.cz
------------------------------------------------------------------------------
"Accelerate Dev Cycles with Automated Cross-Browser Testing - For FREE
Instantly run your Selenium tests across 300+ browser/OS combos. Get
unparalleled scalability from the best Selenium testing platform available.
Simple to use. Nothing to install. Get started now for free."
http://p.sf.net/sfu/SauceLabs
_______________________________________________
Ltp-list mailing list
Ltp-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ltp-list
^ permalink raw reply [flat|nested] 10+ messages in thread
end of thread, other threads:[~2014-05-27 16:41 UTC | newest]
Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-03-28 7:20 [LTP] [PATCH] read/read02.c: cleanup Zeng Linggang
2014-03-28 7:22 ` [LTP] [PATCH] read/read02.c: add new error number tests Zeng Linggang
2014-04-29 16:31 ` chrubis
[not found] ` <1399262735.3177.14.camel@G08JYZSD130126>
2014-05-05 11:53 ` chrubis
[not found] ` <1399362493.3177.40.camel@G08JYZSD130126>
[not found] ` <1399362582.3177.41.camel@G08JYZSD130126>
2014-05-26 13:37 ` [LTP] [PATCH v2 1/3] SAFE_MACROS: Add memalign() chrubis
[not found] ` <1399362661.3177.43.camel@G08JYZSD130126>
2014-05-26 13:37 ` [LTP] [PATCH v2 2/3] read/read02.c: cleanup chrubis
[not found] ` <1399362721.3177.44.camel@G08JYZSD130126>
2014-05-26 13:39 ` [LTP] [PATCH v2 3/3] read/read02.c: add new error number tests chrubis
2014-05-26 13:44 ` chrubis
[not found] ` <1401171983.23078.0.camel@G08JYZSD130126>
2014-05-27 16:41 ` [LTP] [PATCH] read/read02.c: fix skipping all testes on tmpfs chrubis
2014-04-29 16:10 ` [LTP] [PATCH] read/read02.c: cleanup chrubis
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox