~mrooney/ecryptfs/nautilus-integration

« back to all changes in this revision

Viewing changes to src/testcases/llseek.c

  • Committer: mhalcrow@us.ibm.com
  • Date: 2007-11-06 22:56:01 UTC
  • Revision ID: git-v1:f8357de9d554b274497b5cce9db4347254b7e7eb
Initial import of eCryptfs filesystem userspace utilities (mount helper, daemon component,
etc.)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <sys/types.h>
 
2
#include <sys/stat.h>
 
3
#include <fcntl.h>
 
4
#include <unistd.h>
 
5
#include <stdio.h>
 
6
#include <string.h>
 
7
#include <stdint.h>
 
8
 
 
9
#define FILENAME "test.dat"
 
10
 
 
11
int main()
 
12
{
 
13
        off_t size;
 
14
        int fd;
 
15
        struct stat s;
 
16
        uint32_t deadbeef = 0xdeadbeef;
 
17
        uint32_t baadf00d = 0xbaadf00d;
 
18
        char buf[4096];
 
19
        int i;
 
20
        int rc;
 
21
 
 
22
        printf("Verifying that lseek() doesn't change the file size\n");
 
23
        unlink(FILENAME);
 
24
        fd = open(FILENAME, (O_CREAT | O_EXCL| O_WRONLY));
 
25
        if (fd == -1) {
 
26
                printf("Error attempting to create new file [%s]\n", FILENAME);
 
27
                rc = 1;
 
28
                goto out;
 
29
        }
 
30
        size = lseek(fd, 4096, SEEK_END);
 
31
        if (size != 4096) {
 
32
                printf("Expected 4096 from lseek; got [%lld]\n", size);
 
33
                rc = 1;
 
34
                goto out;
 
35
        }
 
36
        close(fd);
 
37
        rc = stat(FILENAME, &s);
 
38
        if (rc == -1) {
 
39
                printf("Error attempting to stat file [%s]\n", FILENAME);
 
40
                rc = 1;
 
41
                goto out;
 
42
        }
 
43
        if (s.st_size != 0) {
 
44
                printf("Filesize is [%lld]; expected 0\n", s.st_size);
 
45
                rc = 1;
 
46
                goto out;
 
47
        }
 
48
        unlink(FILENAME);
 
49
 
 
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);
 
53
        if (fd == -1) {
 
54
                printf("Error attempting to create new file [%s]\n", FILENAME);
 
55
                rc = 1;
 
56
                goto out;
 
57
        }
 
58
        if ((size = lseek(fd, 4096, SEEK_END)) != 4096) {
 
59
                printf("Expected 4096 from lseek; got [%lld]\n", size);
 
60
                rc = 1;
 
61
                goto out;
 
62
        }
 
63
        if ((size = write(fd, (char *)&deadbeef, 4)) != 4) {
 
64
                printf("Expected a write of 4 bytes; got [%lld] instead\n",
 
65
                       size);
 
66
                rc = 1;
 
67
                goto out;
 
68
        }
 
69
        if ((size = lseek(fd, 5120, SEEK_SET)) != 5120) {
 
70
                printf("Expected 5120 from lseek; got [%lld]\n", size);
 
71
                rc = 1;
 
72
                goto out;
 
73
        }
 
74
        if ((size = write(fd, (char *)&baadf00d, 4)) != 4) {
 
75
                printf("Expected a write of 4 bytes; got [%lld] instead\n",
 
76
                       size);
 
77
                rc = 1;
 
78
                goto out;
 
79
        }
 
80
        if ((size = lseek(fd, 4096, SEEK_SET)) != 4096) {
 
81
                printf("Expected 4096 from lseek; got [%lld]\n", size);
 
82
                rc = 1;
 
83
                goto out;
 
84
        }
 
85
        if ((size = read(fd, buf, 4)) != 4) {
 
86
                printf("Error attempting to read data. Expected [%lld] bytes; "
 
87
                       "read [%lld] instead\n", 4, size);
 
88
                rc = 1;
 
89
                goto out;
 
90
        }
 
91
        if (memcmp((char *)&deadbeef, buf, 4) != 0) {
 
92
                printf("deadbeef data mismatch on initial write\n");
 
93
                rc = 1;
 
94
                goto out;
 
95
        }
 
96
        if ((size = read(fd, buf, 1020)) != 1020) {
 
97
                printf("Error attempting to read data. Expected [%lld] bytes; "
 
98
                       "read [%lld] instead\n", 1020, size);
 
99
                rc = 1;
 
100
                goto out;
 
101
        }
 
102
        for (i = 0; i < 1020; i++)
 
103
                if (buf[i] != 0x00) {
 
104
                        printf("Byte [%d] is [0x%.2x]; expected [0x00]\n", i,
 
105
                                buf[i]);
 
106
                        rc = 1;
 
107
                        goto out;
 
108
                }
 
109
        if ((size = read(fd, buf, 4)) != 4) {
 
110
                printf("Error attempting to read data. Expected [%lld] bytes; "
 
111
                       "read [%lld] instead\n", 4, size);
 
112
                rc = 1;
 
113
                goto out;
 
114
        }
 
115
        if (memcmp((char *)&baadf00d, buf, 4) != 0) {
 
116
                printf("baadf00d data mismatch on initial write\n");
 
117
                rc = 1;
 
118
                goto out;
 
119
        }
 
120
        if ((size = read(fd, buf, 1)) != 0) {
 
121
                printf("Error attempting to read data. Expected [%lld] bytes; "
 
122
                       "read [%lld] instead\n", 0, size);
 
123
                rc = 1;
 
124
                goto out;
 
125
        }
 
126
        if ((size = lseek(fd, 0, SEEK_SET)) != 0) {
 
127
                printf("Expected 0 from lseek; got [%lld]\n", size);
 
128
                rc = 1;
 
129
                goto out;
 
130
        }
 
131
        if ((size = read(fd, buf, 4096)) != 4096) {
 
132
                printf("Error attempting to read data. Expected [%lld] bytes; "
 
133
                       "read [%lld] instead\n", 4096, size);
 
134
                rc = 1;
 
135
                goto out;
 
136
        }
 
137
        for (i = 0; i < 4096; i++)
 
138
                if (buf[i] != 0x00) {
 
139
                        printf("Byte [%d] is [0x%.2x]; expected [0x00]\n", i,
 
140
                                buf[i]);
 
141
                        rc = 1;
 
142
                        goto out;
 
143
                }
 
144
        close(fd);
 
145
        rc = stat(FILENAME, &s);
 
146
        if (rc == -1) {
 
147
                printf("Error attempting to stat file [%s]\n", FILENAME);
 
148
                rc = 1;
 
149
                goto out;
 
150
        }
 
151
        if (s.st_size != 5124) {
 
152
                printf("Filesize is [%lld]; expected 5124\n", s.st_size);
 
153
                rc = 1;
 
154
                goto out;
 
155
        }
 
156
        fd = open(FILENAME, (O_RDONLY));
 
157
        if (fd == -1) {
 
158
                printf("Error attempting to create new file [%s]\n", FILENAME);
 
159
                rc = 1;
 
160
                goto out;
 
161
        }
 
162
        if ((size = read(fd, buf, 4096)) != 4096) {
 
163
                printf("Error attempting to read data. Expected [%lld] bytes; "
 
164
                       "read [%lld] instead\n", 4096, size);
 
165
                rc = 1;
 
166
                goto out;
 
167
        }
 
168
        for (i = 0; i < 4096; i++)
 
169
                if (buf[i] != 0x00) {
 
170
                        printf("Byte [%d] is [0x%.2x]; expected [0x00]\n", i,
 
171
                                buf[i]);
 
172
                        rc = 1;
 
173
                        goto out;
 
174
                }
 
175
        if ((size = read(fd, buf, 4)) != 4) {
 
176
                printf("Error attempting to read data. Expected [%lld] bytes; "
 
177
                       "read [%lld] instead\n", 4, size);
 
178
                rc = 1;
 
179
                goto out;
 
180
        }
 
181
        if (memcmp((char *)&deadbeef, buf, 4) != 0) {
 
182
                printf("deadbeef data mismatch after initial write\n");
 
183
                rc = 1;
 
184
                goto out;
 
185
        }
 
186
        if ((size = read(fd, buf, 1020)) != 1020) {
 
187
                printf("Error attempting to read data. Expected [%lld] bytes; "
 
188
                       "read [%lld] instead\n", 1020, size);
 
189
                rc = 1;
 
190
                goto out;
 
191
        }
 
192
        for (i = 0; i < 1020; i++)
 
193
                if (buf[i] != 0x00) {
 
194
                        printf("Byte [%d] is [0x%.2x]; expected [0x00]\n", i,
 
195
                                buf[i]);
 
196
                        rc = 1;
 
197
                        goto out;
 
198
                }
 
199
        if ((size = read(fd, buf, 4)) != 4) {
 
200
                printf("Error attempting to read data. Expected [%lld] bytes; "
 
201
                       "read [%lld] instead\n", 4, size);
 
202
                rc = 1;
 
203
                goto out;
 
204
        }
 
205
        if (memcmp((char *)&baadf00d, buf, 4) != 0) {
 
206
                printf("baadf00d data mismatch after initial write\n");
 
207
                rc = 1;
 
208
                goto out;
 
209
        }
 
210
        if ((size = read(fd, buf, 1)) != 0) {
 
211
                printf("Error attempting to read data. Expected [%lld] bytes; "
 
212
                       "read [%lld] instead\n", 0, size);
 
213
                rc = 1;
 
214
                goto out;
 
215
        }
 
216
        close(fd);
 
217
        unlink(FILENAME);
 
218
        rc = 0;
 
219
out:
 
220
        return rc;
 
221
}