summaryrefslogtreecommitdiff
path: root/tests/fs-tests/simple
diff options
context:
space:
mode:
Diffstat (limited to 'tests/fs-tests/simple')
-rw-r--r--tests/fs-tests/simple/Makefile26
-rw-r--r--tests/fs-tests/simple/ftrunc.c111
-rw-r--r--tests/fs-tests/simple/test_1.c150
-rw-r--r--tests/fs-tests/simple/test_2.c201
4 files changed, 488 insertions, 0 deletions
diff --git a/tests/fs-tests/simple/Makefile b/tests/fs-tests/simple/Makefile
new file mode 100644
index 0000000..07ddf65
--- /dev/null
+++ b/tests/fs-tests/simple/Makefile
@@ -0,0 +1,26 @@
+
+ifeq ($(origin CC),default)
+CC = gcc
+endif
+
+CFLAGS := $(CFLAGS) -Wall -g -O2 -I../lib
+
+LDFLAGS := $(LDFLAGS)
+
+TARGETS = test_1 \
+ test_2 \
+ ftrunc
+
+all: $(TARGETS)
+
+$(TARGETS): ../lib/tests.o
+
+../lib/tests.o: ../lib/tests.h
+
+clean:
+ rm -f *.o $(TARGETS)
+
+tests: all
+ ./test_1 --sync
+ ./test_2 --sync
+ ./ftrunc
diff --git a/tests/fs-tests/simple/ftrunc.c b/tests/fs-tests/simple/ftrunc.c
new file mode 100644
index 0000000..86edf65
--- /dev/null
+++ b/tests/fs-tests/simple/ftrunc.c
@@ -0,0 +1,111 @@
+/*
+ * Copyright (C) 2007 Nokia Corporation.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * version 2 as published by the Free Software Foundation.
+ *
+ * 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 St, Fifth Floor, Boston, MA
+ * 02110-1301 USA
+ *
+ * Author: Adrian Hunter
+ */
+
+#include <unistd.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <stdint.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <errno.h>
+
+#include "tests.h"
+
+#define WRITE_BUFFER_SIZE 32768
+
+void ftrunc(void)
+{
+ int fd, i;
+ pid_t pid;
+ ssize_t written;
+ int64_t remains;
+ size_t block;
+ char *file_name;
+ off_t actual;
+ char buf[WRITE_BUFFER_SIZE];
+
+ file_name = "ftrunc_test_file";
+ fd = open(file_name, O_CREAT | O_WRONLY,
+ S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH);
+ CHECK(fd != -1);
+ pid = getpid();
+ srand(pid);
+ for (i = 0; i < WRITE_BUFFER_SIZE;++i)
+ buf[i] = rand();
+ remains = tests_size_parameter;
+ actual = 0;
+ while (remains > 0) {
+ if (remains > WRITE_BUFFER_SIZE)
+ block = WRITE_BUFFER_SIZE;
+ else
+ block = remains;
+ written = write(fd, buf, block);
+ if (written <= 0) {
+ CHECK(errno == ENOSPC); /* File system full */
+ errno = 0;
+ break;
+ }
+ remains -= written;
+ actual += written;
+ }
+ CHECK(ftruncate(fd, (actual ? actual - 1 : actual)) != -1);
+ CHECK(close(fd) != -1);
+ CHECK(unlink(file_name) != -1);
+}
+
+/* Title of this test */
+
+const char *ftrunc_get_title(void)
+{
+ return "Truncate a large test file";
+}
+
+/* Description of this test */
+
+const char *ftrunc_get_description(void)
+{
+ return
+ "Create a file named ftrunc_test_file. " \
+ "Truncate the file to reduce its length by 1. " \
+ "Then remove the truncated file. "
+ "The size is given by the -z or --size option, " \
+ "otherwise it defaults to 1000000.";
+}
+
+int main(int argc, char *argv[])
+{
+ int run_test;
+
+ /* Set default test file size */
+ tests_size_parameter = 1000000;
+
+ /* Handle common arguments */
+ run_test = tests_get_args(argc, argv, ftrunc_get_title(),
+ ftrunc_get_description(), "z");
+ if (!run_test)
+ return 1;
+ /* Change directory to the file system and check it is ok for testing */
+ tests_check_test_file_system();
+ /* Do the actual test */
+ ftrunc();
+ return 0;
+}
diff --git a/tests/fs-tests/simple/test_1.c b/tests/fs-tests/simple/test_1.c
new file mode 100644
index 0000000..69eafe4
--- /dev/null
+++ b/tests/fs-tests/simple/test_1.c
@@ -0,0 +1,150 @@
+/*
+ * Copyright (C) 2007 Nokia Corporation.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * version 2 as published by the Free Software Foundation.
+ *
+ * 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 St, Fifth Floor, Boston, MA
+ * 02110-1301 USA
+ *
+ * Author: Adrian Hunter
+ */
+
+#include <unistd.h>
+#include <stdio.h>
+#include <string.h>
+#include <stdint.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+
+#include "tests.h"
+
+void test_1(void)
+{
+ int fd;
+ pid_t pid;
+ uint64_t i;
+ uint64_t block;
+ uint64_t actual_size;
+ char name[256];
+ char old[16];
+ char buf[16];
+ off_t old_len;
+ char dir_name[256];
+
+ /* Create a directory to test in */
+ pid = getpid();
+ tests_cat_pid(dir_name, "test_1_test_dir_", pid);
+ if (chdir(dir_name) == -1)
+ CHECK(mkdir(dir_name, 0777) != -1);
+ CHECK(chdir(dir_name) != -1);
+ /* Create a file that fills half the free space on the file system */
+ tests_create_file("big_file", tests_get_big_file_size(1,2));
+ CHECK(tests_count_files_in_dir(".") == 1);
+ fd = open("big_file", O_RDWR | tests_maybe_sync_flag());
+ CHECK(fd != -1);
+ CHECK(read(fd, old, 5) == 5);
+ CHECK(lseek(fd, 0, SEEK_SET) != (off_t) -1);
+ CHECK(write(fd,"start", 5) == 5);
+ CHECK(lseek(fd,0,SEEK_END) != (off_t) -1);
+ CHECK(write(fd, "end", 3) == 3);
+ tests_maybe_sync(fd);
+ /* Delete the file while it is still open */
+ tests_delete_file("big_file");
+ CHECK(tests_count_files_in_dir(".") == 0);
+ /* Create files to file up the file system */
+ for (block = 1000000, i = 1; ; block /= 10) {
+ while (i != 0) {
+ sprintf(name, "fill_up_%llu", i);
+ actual_size = tests_create_file(name, block);
+ if (actual_size != 0)
+ ++i;
+ if (actual_size != block)
+ break;
+ }
+ if (block == 1)
+ break;
+ }
+ /* Check the big file */
+ CHECK(lseek(fd, 0, SEEK_SET) != (off_t) -1);
+ CHECK(read(fd, buf, 5) == 5);
+ CHECK(strncmp(buf, "start", 5) == 0);
+ CHECK(lseek(fd, -3, SEEK_END) != (off_t) -1);
+ CHECK(read(fd, buf, 3) == 3);
+ CHECK(strncmp(buf, "end", 3) == 0);
+ /* Check the other files and delete them */
+ i -= 1;
+ CHECK(tests_count_files_in_dir(".") == i);
+ for (; i > 0; --i) {
+ sprintf(name, "fill_up_%llu", i);
+ tests_check_filled_file(name);
+ tests_delete_file(name);
+ }
+ CHECK(tests_count_files_in_dir(".") == 0);
+ /* Check the big file again */
+ CHECK(lseek(fd, 0, SEEK_SET) != (off_t) -1);
+ CHECK(read(fd, buf, 5) == 5);
+ CHECK(strncmp(buf, "start", 5) == 0);
+ CHECK(lseek(fd, -3, SEEK_END) != (off_t) -1);
+ CHECK(read(fd, buf, 3) == 3);
+ CHECK(strncmp(buf, "end", 3) == 0);
+ CHECK(lseek(fd, 0, SEEK_SET) != (off_t) -1);
+ CHECK(write(fd,old, 5) == 5);
+ old_len = lseek(fd, -3, SEEK_END);
+ CHECK(old_len != (off_t) -1);
+ CHECK(ftruncate(fd,old_len) != -1);
+ tests_check_filled_file_fd(fd);
+ /* Close the big file*/
+ CHECK(close(fd) != -1);
+ CHECK(tests_count_files_in_dir(".") == 0);
+ CHECK(chdir("..") != -1);
+ CHECK(rmdir(dir_name) != -1);
+}
+
+/* Title of this test */
+
+const char *test_1_get_title(void)
+{
+ return "Fill file system while holding deleted big file descriptor";
+}
+
+/* Description of this test */
+
+const char *test_1_get_description(void)
+{
+ return
+ "Create a directory named test_1_test_dir_pid, where " \
+ "pid is the process id. Within that directory, " \
+ "create a big file (approx. half the file system in size), " \
+ "open it, and unlink it. " \
+ "Create many smaller files until the file system is full. " \
+ "Check the big file is ok. " \
+ "Delete all the smaller files. " \
+ "Check the big file again. " \
+ "Finally delete the big file and directory.";
+}
+
+int main(int argc, char *argv[])
+{
+ int run_test;
+
+ /* Handle common arguments */
+ run_test = tests_get_args(argc, argv, test_1_get_title(),
+ test_1_get_description(), "s");
+ if (!run_test)
+ return 1;
+ /* Change directory to the file system and check it is ok for testing */
+ tests_check_test_file_system();
+ /* Do the actual test */
+ test_1();
+ return 0;
+}
diff --git a/tests/fs-tests/simple/test_2.c b/tests/fs-tests/simple/test_2.c
new file mode 100644
index 0000000..2094460
--- /dev/null
+++ b/tests/fs-tests/simple/test_2.c
@@ -0,0 +1,201 @@
+/*
+ * Copyright (C) 2007 Nokia Corporation.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * version 2 as published by the Free Software Foundation.
+ *
+ * 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 St, Fifth Floor, Boston, MA
+ * 02110-1301 USA
+ *
+ * Author: Adrian Hunter
+ */
+
+#include <unistd.h>
+#include <stdio.h>
+#include <string.h>
+#include <stdint.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+
+#include "tests.h"
+
+void test_2(void)
+{
+ pid_t pid;
+ int create, full;
+ unsigned i, number_of_files;
+ unsigned growth;
+ unsigned size;
+ uint64_t big_file_size;
+ int fd;
+ off_t offset;
+ char dir_name[256];
+
+ /* Create a directory to test in */
+ pid = getpid();
+ tests_cat_pid(dir_name, "test_2_test_dir_", pid);
+ if (chdir(dir_name) == -1)
+ CHECK(mkdir(dir_name, 0777) != -1);
+ CHECK(chdir(dir_name) != -1);
+ /* Create up to 1000 files appending 400 bytes at a time to each file */
+ /* until the file system is full.*/
+ create = 1;
+ full = 0;
+ number_of_files = 1000;
+ while (!full) {
+ for (i = 0; i < number_of_files; ++i) {
+ growth = tests_append_to_fragment_file(i, 400, create);
+ if (!growth) {
+ full = 1;
+ if (create)
+ number_of_files = i;
+ break;
+ }
+ }
+ create = 0;
+ }
+ /* Check the files */
+ CHECK(tests_count_files_in_dir(".") == number_of_files);
+ for (i = 0; i < number_of_files; ++i)
+ tests_check_fragment_file(i);
+ /* Delete half of them */
+ for (i = 1; i < number_of_files; i += 2)
+ tests_delete_fragment_file(i);
+ /* Check them again */
+ CHECK(tests_count_files_in_dir(".") == (number_of_files + 1) / 2);
+ for (i = 0; i < number_of_files; i += 2)
+ tests_check_fragment_file(i);
+ CHECK(tests_count_files_in_dir(".") == (number_of_files + 1) / 2);
+ /* Create a big file that fills two thirds of the free space */
+ big_file_size = tests_get_big_file_size(2,3);
+ /* Check the big file */
+ tests_create_file("big_file", big_file_size);
+ CHECK(tests_count_files_in_dir(".") == 1 + (number_of_files + 1) / 2);
+ tests_check_filled_file("big_file");
+ /* Open the big file */
+ fd = open("big_file",O_RDWR | tests_maybe_sync_flag());
+ CHECK(fd != -1);
+ /* Delete the big file while it is still open */
+ tests_delete_file("big_file");
+ /* Check the big file again */
+ CHECK(tests_count_files_in_dir(".") == (number_of_files + 1) / 2);
+ tests_check_filled_file_fd(fd);
+
+ /* Write parts of the files and check them */
+
+ offset = 100; /* Offset to write at, in the small files */
+ size = 200; /* Number of bytes to write at the offset */
+
+ for (i = 0; i < number_of_files; i += 2)
+ tests_overwite_fragment_file(i, offset, size);
+ /* Rewrite the big file entirely */
+ tests_write_filled_file(fd, 0, big_file_size);
+ for (i = 0; i < number_of_files; i += 2)
+ tests_check_fragment_file(i);
+ tests_check_filled_file_fd(fd);
+
+ offset = 300; /* Offset to write at, in the small files */
+ size = 400; /* Number of bytes to write at the offset */
+
+ for (i = 0; i < number_of_files; i += 2)
+ tests_overwite_fragment_file(i, offset, size);
+ /* Rewrite the big file entirely */
+ tests_write_filled_file(fd, 0, big_file_size);
+ for (i = 0; i < number_of_files; i += 2)
+ tests_check_fragment_file(i);
+ tests_check_filled_file_fd(fd);
+
+ offset = 110; /* Offset to write at, in the small files */
+ size = 10; /* Number of bytes to write at the offset */
+
+ for (i = 0; i < number_of_files; i += 2)
+ tests_overwite_fragment_file(i, offset, size);
+ /* Rewrite the big file entirely */
+ tests_write_filled_file(fd, 0, big_file_size);
+ for (i = 0; i < number_of_files; i += 2)
+ tests_check_fragment_file(i);
+ tests_check_filled_file_fd(fd);
+
+ offset = 10; /* Offset to write at, in the small files */
+ size = 1000; /* Number of bytes to write at the offset */
+
+ for (i = 0; i < number_of_files; i += 2)
+ tests_overwite_fragment_file(i, offset, size);
+ /* Rewrite the big file entirely */
+ tests_write_filled_file(fd, 0, big_file_size);
+ for (i = 0; i < number_of_files; i += 2)
+ tests_check_fragment_file(i);
+ tests_check_filled_file_fd(fd);
+
+ offset = 0; /* Offset to write at, in the small files */
+ size = 100000; /* Number of bytes to write at the offset */
+
+ for (i = 0; i < number_of_files; i += 2)
+ tests_overwite_fragment_file(i, offset, size);
+ /* Rewrite the big file entirely */
+ tests_write_filled_file(fd, 0, big_file_size);
+ for (i = 0; i < number_of_files; i += 2)
+ tests_check_fragment_file(i);
+ tests_check_filled_file_fd(fd);
+
+ /* Close the big file*/
+ CHECK(close(fd) != -1);
+ /* Check the small files */
+ CHECK(tests_count_files_in_dir(".") == (number_of_files + 1) / 2);
+ for (i = 0; i < number_of_files; i += 2)
+ tests_check_fragment_file(i);
+ /* Delete the small files */
+ for (i = 0; i < number_of_files; i += 2)
+ tests_delete_fragment_file(i);
+ CHECK(tests_count_files_in_dir(".") == 0);
+ CHECK(chdir("..") != -1);
+ CHECK(rmdir(dir_name) != -1);
+}
+
+/* Title of this test */
+
+const char *test_2_get_title(void)
+{
+ return "Repeated write many small files and one big deleted file";
+}
+
+/* Description of this test */
+
+const char *test_2_get_description(void)
+{
+ return
+ "Create a directory named test_2_test_dir_pid, where " \
+ "pid is the process id. Within that directory, " \
+ "create about 1000 files. Append 400 bytes to each until " \
+ "the file system is full. Then delete half of them. Then " \
+ "create a big file that uses about 2/3 of the remaining free " \
+ "space. Get a file descriptor for the big file, and delete " \
+ "the big file. Then repeatedly write to the small files " \
+ "and the big file. " \
+ "Finally delete the big file and directory.";
+}
+
+int main(int argc, char *argv[])
+{
+ int run_test;
+
+ /* Handle common arguments */
+ run_test = tests_get_args(argc, argv, test_2_get_title(),
+ test_2_get_description(), "s");
+ if (!run_test)
+ return 1;
+ /* Change directory to the file system and check it is ok for testing */
+ tests_check_test_file_system();
+ /* Do the actual test */
+ test_2();
+ return 0;
+}