summaryrefslogtreecommitdiff
path: root/tests/fs-tests/simple/test_1.c
blob: 874e4b28ec147d56d6b763247f7a88288af7ca3e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
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"

static 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", (unsigned long long)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", (unsigned long long)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 */

static const char *test_1_get_title(void)
{
	return "Fill file system while holding deleted big file descriptor";
}

/* Description of this test */

static 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;
}