2
* Author: Michael Halcrow
6
* Modified by Tyler Hicks <tyhicks@canonical.com> to fit into the eCryptfs
7
* test modern framework.
9
1
#include <sys/types.h>
10
2
#include <sys/stat.h>
19
int main(int argc, char *argv[])
9
#define FILENAME "test.dat"
24
16
uint32_t deadbeef = 0xdeadbeef;
25
17
uint32_t baadf00d = 0xbaadf00d;
32
fprintf(stderr, "Usage: %s path\n", argv[0]);
37
/* Verifying that lseek() doesn't change the file size */
38
fd = open(path, (O_CREAT | O_EXCL| O_WRONLY), S_IRUSR | S_IWUSR);
22
printf("Verifying that lseek() doesn't change the file size\n");
24
fd = open(FILENAME, (O_CREAT | O_EXCL| O_WRONLY));
40
fprintf(stderr, "Error attempting to create new file [%s]\n",
26
printf("Error attempting to create new file [%s]\n", FILENAME);
45
30
size = lseek(fd, 4096, SEEK_END);
46
31
if (size != 4096) {
47
fprintf(stderr, "Expected 4096 from lseek; got [%jd]\n",
32
printf("Expected 4096 from lseek; got [%lld]\n", size);
37
rc = stat(FILENAME, &s);
55
fprintf(stderr, "Error attempting to stat file [%s]\n",
39
printf("Error attempting to stat file [%s]\n", FILENAME);
60
43
if (s.st_size != 0) {
61
fprintf(stderr, "Filesize is [%jd]; expected 0\n",
44
printf("Filesize is [%lld]; expected 0\n", s.st_size);
69
* Verifying that intermediate regions of the file are initialized to 0
70
* on lseek() and write() events\n;
72
fd = open(path, (O_CREAT | O_EXCL| O_RDWR), S_IRWXU);
50
printf("Verifying that intermediate regions of the file are "
51
"initialized to 0 on lseek() and write() events\n");
52
fd = open(FILENAME, (O_CREAT | O_EXCL| O_RDWR), S_IRWXU);
74
fprintf(stderr, "Error attempting to create new file [%s]\n",
54
printf("Error attempting to create new file [%s]\n", FILENAME);
79
58
if ((size = lseek(fd, 4096, SEEK_END)) != 4096) {
80
fprintf(stderr, "Expected 4096 from lseek; got [%jd]\n",
59
printf("Expected 4096 from lseek; got [%lld]\n", size);
85
63
if ((size = write(fd, (char *)&deadbeef, 4)) != 4) {
86
fprintf(stderr, "Expected a write of 4 bytes; got [%jd] "
87
"instead\n", (intmax_t)size);
64
printf("Expected a write of 4 bytes; got [%lld] instead\n",
91
69
if ((size = lseek(fd, 5120, SEEK_SET)) != 5120) {
92
fprintf(stderr, "Expected 5120 from lseek; got [%jd]\n",
70
printf("Expected 5120 from lseek; got [%lld]\n", size);
97
74
if ((size = write(fd, (char *)&baadf00d, 4)) != 4) {
98
fprintf(stderr, "Expected a write of 4 bytes; got [%jd] "
99
"instead\n", (intmax_t)size);
75
printf("Expected a write of 4 bytes; got [%lld] instead\n",
103
80
if ((size = lseek(fd, 4096, SEEK_SET)) != 4096) {
104
fprintf(stderr, "Expected 4096 from lseek; got [%jd]\n",
81
printf("Expected 4096 from lseek; got [%lld]\n", size);
109
85
if ((size = read(fd, buf, 4)) != 4) {
110
fprintf(stderr, "Error attempting to read data. Expected "
111
"[%d] bytes; read [%jd] instead\n", 4, (intmax_t)size);
86
printf("Error attempting to read data. Expected [%lld] bytes; "
87
"read [%lld] instead\n", 4, size);
115
91
if (memcmp((char *)&deadbeef, buf, 4) != 0) {
116
fprintf(stderr, "deadbeef data mismatch on initial write\n");
92
printf("deadbeef data mismatch on initial write\n");
120
96
if ((size = read(fd, buf, 1020)) != 1020) {
121
fprintf(stderr, "Error attempting to read data. Expected "
122
"[%d] bytes; read [%jd] instead\n", 1020,
97
printf("Error attempting to read data. Expected [%lld] bytes; "
98
"read [%lld] instead\n", 1020, size);
127
102
for (i = 0; i < 1020; i++)
128
103
if (buf[i] != 0x00) {
129
fprintf(stderr, "Byte [%d] is [0x%.2x]; expected "
130
"[0x00]\n", i, buf[i]);
104
printf("Byte [%d] is [0x%.2x]; expected [0x00]\n", i,
134
109
if ((size = read(fd, buf, 4)) != 4) {
135
fprintf(stderr, "Error attempting to read data. Expected "
136
"[%d] bytes; read [%jd] instead\n", 4, (intmax_t)size);
110
printf("Error attempting to read data. Expected [%lld] bytes; "
111
"read [%lld] instead\n", 4, size);
140
115
if (memcmp((char *)&baadf00d, buf, 4) != 0) {
141
fprintf(stderr, "baadf00d data mismatch on initial write\n");
116
printf("baadf00d data mismatch on initial write\n");
145
120
if ((size = read(fd, buf, 1)) != 0) {
146
fprintf(stderr, "Error attempting to read data. Expected "
147
"[%d] bytes; read [%jd] instead\n", 0, (intmax_t)size);
121
printf("Error attempting to read data. Expected [%lld] bytes; "
122
"read [%lld] instead\n", 0, size);
151
126
if ((size = lseek(fd, 0, SEEK_SET)) != 0) {
152
fprintf(stderr, "Expected 0 from lseek; got [%jd]\n",
127
printf("Expected 0 from lseek; got [%lld]\n", size);
157
131
if ((size = read(fd, buf, 4096)) != 4096) {
158
fprintf(stderr, "Error attempting to read data. Expected "
159
"[%d] bytes; read [%jd] instead\n", 4096,
132
printf("Error attempting to read data. Expected [%lld] bytes; "
133
"read [%lld] instead\n", 4096, size);
164
137
for (i = 0; i < 4096; i++)
165
138
if (buf[i] != 0x00) {
166
fprintf(stderr, "Byte [%d] is [0x%.2x]; expected "
167
"[0x00]\n", i, buf[i]);
139
printf("Byte [%d] is [0x%.2x]; expected [0x00]\n", i,
145
rc = stat(FILENAME, &s);
174
fprintf(stderr, "Error attempting to stat file [%s]\n",
147
printf("Error attempting to stat file [%s]\n", FILENAME);
179
151
if (s.st_size != 5124) {
180
fprintf(stderr, "Filesize is [%jd]; expected 5124\n",
181
(intmax_t)s.st_size);
152
printf("Filesize is [%lld]; expected 5124\n", s.st_size);
185
fd = open(path, (O_RDONLY));
156
fd = open(FILENAME, (O_RDONLY));
187
fprintf(stderr, "Error attempting to create new file [%s]\n",
158
printf("Error attempting to create new file [%s]\n", FILENAME);
192
162
if ((size = read(fd, buf, 4096)) != 4096) {
193
fprintf(stderr, "Error attempting to read data. Expected "
194
"[%d] bytes; read [%jd] instead\n", 4096,
163
printf("Error attempting to read data. Expected [%lld] bytes; "
164
"read [%lld] instead\n", 4096, size);
199
168
for (i = 0; i < 4096; i++)
200
169
if (buf[i] != 0x00) {
201
fprintf(stderr, "Byte [%d] is [0x%.2x]; expected "
202
"[0x00]\n", i, buf[i]);
170
printf("Byte [%d] is [0x%.2x]; expected [0x00]\n", i,
206
175
if ((size = read(fd, buf, 4)) != 4) {
207
fprintf(stderr, "Error attempting to read data. Expected "
208
"[%d] bytes; read [%jd] instead\n", 4, (intmax_t)size);
176
printf("Error attempting to read data. Expected [%lld] bytes; "
177
"read [%lld] instead\n", 4, size);
212
181
if (memcmp((char *)&deadbeef, buf, 4) != 0) {
213
fprintf(stderr, "deadbeef data mismatch after initial write\n");
182
printf("deadbeef data mismatch after initial write\n");
217
186
if ((size = read(fd, buf, 1020)) != 1020) {
218
fprintf(stderr, "Error attempting to read data. Expected "
219
"[%d] bytes; read [%jd] instead\n", 1020,
187
printf("Error attempting to read data. Expected [%lld] bytes; "
188
"read [%lld] instead\n", 1020, size);
224
192
for (i = 0; i < 1020; i++)
225
193
if (buf[i] != 0x00) {
226
fprintf(stderr, "Byte [%d] is [0x%.2x]; expected "
227
"[0x00]\n", i, buf[i]);
194
printf("Byte [%d] is [0x%.2x]; expected [0x00]\n", i,
231
199
if ((size = read(fd, buf, 4)) != 4) {
232
fprintf(stderr, "Error attempting to read data. Expected "
233
"[%d] bytes; read [%jd] instead\n", 4, (intmax_t)size);
200
printf("Error attempting to read data. Expected [%lld] bytes; "
201
"read [%lld] instead\n", 4, size);
237
205
if (memcmp((char *)&baadf00d, buf, 4) != 0) {
238
fprintf(stderr, "baadf00d data mismatch after initial write\n");
206
printf("baadf00d data mismatch after initial write\n");
242
210
if ((size = read(fd, buf, 1)) != 0) {
243
fprintf(stderr, "Error attempting to read data. Expected "
244
"[%d] bytes; read [%jd] instead\n", 0, (intmax_t)size);
211
printf("Error attempting to read data. Expected [%lld] bytes; "
212
"read [%lld] instead\n", 0, size);