From 55ea7048d0ebf0b9c8ed06ca6b9025a90ae15b16 Mon Sep 17 00:00:00 2001 From: Adrian Hunter Date: Fri, 20 Jul 2007 11:31:18 +0300 Subject: Add test program orph.c Signed-off-by: Adrian Hunter Signed-off-by: Artem Bityutskiy --- tests/fs-tests/lib/tests.c | 48 ++++++++++++++++++++++++++++++++++++++++++++-- tests/fs-tests/lib/tests.h | 14 ++++++++++++++ 2 files changed, 60 insertions(+), 2 deletions(-) (limited to 'tests/fs-tests/lib') diff --git a/tests/fs-tests/lib/tests.c b/tests/fs-tests/lib/tests.c index 8c34b00..e2a6d5b 100644 --- a/tests/fs-tests/lib/tests.c +++ b/tests/fs-tests/lib/tests.c @@ -506,9 +506,9 @@ void tests_check_filled_file_fd(int fd) char buf[WRITE_BUFFER_SIZE]; do { - sz = read(fd,buf,WRITE_BUFFER_SIZE); + sz = read(fd, buf, WRITE_BUFFER_SIZE); CHECK(sz >= 0); - CHECK(memcmp(buf,write_buffer,sz) == 0); + CHECK(memcmp(buf, write_buffer, sz) == 0); } while (sz); } @@ -585,6 +585,26 @@ uint64_t tests_get_big_file_size(unsigned numerator, unsigned denominator) return numerator * (tests_get_free_space() / denominator); } +/* Create file "fragment_n" where n is the file_number, and unlink it */ +int tests_create_orphan(unsigned file_number) +{ + int fd; + int flags; + mode_t mode; + char file_name[256]; + + sprintf(file_name, "fragment_%u", file_number); + flags = O_CREAT | O_TRUNC | O_RDWR | tests_maybe_sync_flag(); + mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH; + fd = open(file_name, flags, mode); + if (fd == -1 && (errno == ENOSPC || errno == EMFILE)) + return fd; /* File system full or too many open files */ + CHECK(fd != -1); + tests_sync_directory(file_name); + CHECK(unlink(file_name) != -1); + return fd; +} + /* Write size bytes at offset to the file "fragment_n" where n is the file_number and file_number also determines the random data written i.e. seed for random numbers */ @@ -709,6 +729,30 @@ void tests_delete_fragment_file(unsigned file_number) tests_delete_file(file_name); } +/* Check the random data in file "fragment_n" is what is expected */ +void tests_check_fragment_file_fd(unsigned file_number, int fd) +{ + ssize_t sz, i; + int d; + uint64_t u; + char buf[8192]; + + CHECK(lseek(fd, 0, SEEK_SET) == 0); + srand(file_number); + u = RAND_MAX; + u += 1; + u /= 256; + d = (int) u; + for (;;) { + sz = read(fd, buf, 8192); + if (sz == 0) + break; + CHECK(sz >= 0); + for (i = 0; i < sz; ++i) + CHECK(buf[i] == (char) (rand() / d)); + } +} + /* Check the random data in file "fragment_n" is what is expected */ void tests_check_fragment_file(unsigned file_number) { diff --git a/tests/fs-tests/lib/tests.h b/tests/fs-tests/lib/tests.h index 36ca310..db08628 100644 --- a/tests/fs-tests/lib/tests.h +++ b/tests/fs-tests/lib/tests.h @@ -85,6 +85,17 @@ uint64_t tests_create_file(const char *file_name, uint64_t file_size); /* Calculate: free_space * numerator / denominator */ uint64_t tests_get_big_file_size(unsigned numerator, unsigned denominator); +/* Create file "fragment_n" where n is the file_number, and unlink it */ +int tests_create_orphan(unsigned file_number); + +/* Write size bytes at offset to the file "fragment_n" where n is the + file_number and file_number also determines the random data written + i.e. seed for random numbers */ +unsigned tests_write_fragment_file(unsigned file_number, + int fd, + off_t offset, + unsigned size); + /* Write size bytes to the end of file descriptor fd using file_number to determine the random data written i.e. seed for random numbers */ unsigned tests_fill_fragment_file(unsigned file_number, @@ -108,6 +119,9 @@ unsigned tests_overwite_fragment_file( unsigned file_number, /* Delete file "fragment_n" where n is the file_number */ void tests_delete_fragment_file(unsigned file_number); +/* Check the random data in file "fragment_n" is what is expected */ +void tests_check_fragment_file_fd(unsigned file_number, int fd); + /* Check the random data in file "fragment_n" is what is expected */ void tests_check_fragment_file(unsigned file_number); -- cgit v1.2.3