-
Notifications
You must be signed in to change notification settings - Fork 57
/
p-testwritefs.cc
153 lines (107 loc) · 3.51 KB
/
p-testwritefs.cc
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
151
152
153
#include "u-lib.hh"
void process_main() {
printf("Starting testwritefs (assuming clean file system)...\n");
// read file
printf("%s:%d: read...\n", __FILE__, __LINE__);
int f = sys_open("emerson.txt", OF_READ);
assert_gt(f, 2);
char buf[200];
memset(buf, 0, sizeof(buf));
ssize_t n = sys_read(f, buf, 200);
assert_eq(n, 130);
assert_memeq(buf, "When piped a tiny voice hard by,\n"
"Gay and polite, a cheerful cry,\n"
"Chic-chicadeedee", 81);
sys_close(f);
// overwrite start of file
printf("%s:%d: overwrite start...\n", __FILE__, __LINE__);
f = sys_open("emerson.txt", OF_WRITE);
assert_gt(f, 2);
n = sys_write(f, "OLEK WAS HERE", 13);
assert_eq(n, 13);
sys_close(f);
f = sys_open("emerson.txt", OF_READ);
assert_gt(f, 2);
memset(buf, 0, sizeof(buf));
n = sys_read(f, buf, 200);
assert_eq(n, 130);
assert_memeq(buf, "OLEK WAS HEREtiny voice hard by,\n"
"Gay and polite, a cheerful cry,\n"
"Chic-chicadeedee", 81);
sys_close(f);
// read & write same file
printf("%s:%d: read and write...\n", __FILE__, __LINE__);
f = sys_open("emerson.txt", OF_READ);
assert_gt(f, 2);
int wf = sys_open("emerson.txt", OF_WRITE);
assert_gt(wf, 2);
assert_ne(f, wf);
memset(buf, 0, sizeof(buf));
n = sys_read(f, buf, 8);
assert_eq(n, 8);
assert_memeq(buf, "OLEK WAS", 8);
n = sys_write(wf, "CLARE ROJAS WAS HERE", 20);
assert_eq(n, 20);
memset(buf, 0, sizeof(buf));
n = sys_read(f, buf, 20);
assert_eq(n, 20);
assert_memeq(buf, "JAS WAS HEREice hard", 20);
n = sys_write(wf, "!", 1);
assert_eq(n, 1);
memset(buf, 0, sizeof(buf));
n = sys_read(f, buf, 4);
assert_eq(n, 4);
assert_memeq(buf, " by,", 4);
// cannot write fd open only for reading
n = sys_write(f, "!", 1);
assert_eq(n, E_BADF);
// cannot read fd open only for writing
n = sys_read(wf, buf, 1);
assert_eq(n, E_BADF);
sys_close(f);
sys_close(wf);
// read & write same file with combination flags
printf("%s:%d: read|write...\n", __FILE__, __LINE__);
f = sys_open("emerson.txt", OF_READ);
assert_gt(f, 2);
wf = sys_open("emerson.txt", OF_READ | OF_WRITE);
assert_gt(wf, 2);
assert_ne(f, wf);
memset(buf, 0, sizeof(buf));
n = sys_read(f, buf, 8);
assert_eq(n, 8);
assert_memeq(buf, "CLARE RO", 8);
memset(buf, 0, sizeof(buf));
n = sys_read(wf, buf, 8);
assert_eq(n, 8);
assert_memeq(buf, "CLARE RO", 8);
n = sys_write(wf, "OT Vegetables", 13);
assert_eq(n, 13);
memset(buf, 0, sizeof(buf));
n = sys_read(wf, buf, 7);
assert_eq(n, 7);
assert_memeq(buf, "ce hard", 7);
memset(buf, 0, sizeof(buf));
n = sys_read(f, buf, 13);
assert_eq(n, 13);
assert_memeq(buf, "OT Vegetables", 13);
sys_close(f);
sys_close(wf);
// synchronize disk
printf("%s:%d: sync disk...\n", __FILE__, __LINE__);
int r = sys_sync(2);
assert_ge(r, 0);
// read again, this time from disk
printf("%s:%d: read...\n", __FILE__, __LINE__);
f = sys_open("emerson.txt", OF_READ);
assert_gt(f, 2);
memset(buf, 0, sizeof(buf));
n = sys_read(f, buf, 200);
assert_eq(n, 130);
assert_memeq(buf, "CLARE ROOT Vegetablesce hard by,\n"
"Gay and polite, a cheerful cry,\n"
"Chic-chicadeedee", 81);
sys_close(f);
printf("testwritefs succeeded.\n");
sys_exit(0);
}