~ecryptfs/ecryptfs/trunk

« back to all changes in this revision

Viewing changes to src/testcases/llseek.c

  • Committer: Dustin Kirkland
  • Date: 2009-02-13 15:57:24 UTC
  • Revision ID: kirkland@canonical.com-20090213155724-1q3qz2o0cbyimu9x
debian/ubuntu packaging

Initial checkin of the Debian/Ubuntu packaging

Signed-off-by: Dustin Kirkland <kirkland@canonical.com>

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/**
2
 
 * Author: Michael Halcrow
3
 
 *
4
 
 * Copyright (C) IBM
5
 
 *
6
 
 * Modified by Tyler Hicks <tyhicks@canonical.com> to fit into the eCryptfs
7
 
 * test modern framework.
8
 
 */
9
1
#include <sys/types.h>
10
2
#include <sys/stat.h>
11
3
#include <fcntl.h>
12
4
#include <unistd.h>
13
5
#include <stdio.h>
14
 
#include <stdlib.h>
15
6
#include <string.h>
16
7
#include <stdint.h>
17
 
#include <inttypes.h>
18
 
 
19
 
int main(int argc, char *argv[])
 
8
 
 
9
#define FILENAME "test.dat"
 
10
 
 
11
int main()
20
12
{
21
13
        off_t size;
22
14
        int fd;
23
15
        struct stat s;
24
16
        uint32_t deadbeef = 0xdeadbeef;
25
17
        uint32_t baadf00d = 0xbaadf00d;
26
 
        char *path;
27
18
        char buf[4096];
28
19
        int i;
29
20
        int rc;
30
21
 
31
 
        if (argc != 2) {
32
 
                fprintf(stderr, "Usage: %s path\n", argv[0]);
33
 
                exit(1);
34
 
        }
35
 
        path = argv[1];
36
 
 
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");
 
23
        unlink(FILENAME);
 
24
        fd = open(FILENAME, (O_CREAT | O_EXCL| O_WRONLY));
39
25
        if (fd == -1) {
40
 
                fprintf(stderr, "Error attempting to create new file [%s]\n",
41
 
                        path);
 
26
                printf("Error attempting to create new file [%s]\n", FILENAME);
42
27
                rc = 1;
43
28
                goto out;
44
29
        }
45
30
        size = lseek(fd, 4096, SEEK_END);
46
31
        if (size != 4096) {
47
 
                fprintf(stderr, "Expected 4096 from lseek; got [%jd]\n",
48
 
                        (intmax_t)size);
 
32
                printf("Expected 4096 from lseek; got [%lld]\n", size);
49
33
                rc = 1;
50
34
                goto out;
51
35
        }
52
36
        close(fd);
53
 
        rc = stat(path, &s);
 
37
        rc = stat(FILENAME, &s);
54
38
        if (rc == -1) {
55
 
                fprintf(stderr, "Error attempting to stat file [%s]\n",
56
 
                        path);
 
39
                printf("Error attempting to stat file [%s]\n", FILENAME);
57
40
                rc = 1;
58
41
                goto out;
59
42
        }
60
43
        if (s.st_size != 0) {
61
 
                fprintf(stderr, "Filesize is [%jd]; expected 0\n",
62
 
                        (intmax_t)s.st_size);
 
44
                printf("Filesize is [%lld]; expected 0\n", s.st_size);
63
45
                rc = 1;
64
46
                goto out;
65
47
        }
66
 
        unlink(path);
 
48
        unlink(FILENAME);
67
49
 
68
 
        /**
69
 
         * Verifying that intermediate regions of the file are initialized to 0
70
 
         * on lseek() and write() events\n;
71
 
         */
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);
73
53
        if (fd == -1) {
74
 
                fprintf(stderr, "Error attempting to create new file [%s]\n",
75
 
                        path);
 
54
                printf("Error attempting to create new file [%s]\n", FILENAME);
76
55
                rc = 1;
77
56
                goto out;
78
57
        }
79
58
        if ((size = lseek(fd, 4096, SEEK_END)) != 4096) {
80
 
                fprintf(stderr, "Expected 4096 from lseek; got [%jd]\n",
81
 
                        (intmax_t)size);
 
59
                printf("Expected 4096 from lseek; got [%lld]\n", size);
82
60
                rc = 1;
83
61
                goto out;
84
62
        }
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",
 
65
                       size);
88
66
                rc = 1;
89
67
                goto out;
90
68
        }
91
69
        if ((size = lseek(fd, 5120, SEEK_SET)) != 5120) {
92
 
                fprintf(stderr, "Expected 5120 from lseek; got [%jd]\n",
93
 
                        (intmax_t)size);
 
70
                printf("Expected 5120 from lseek; got [%lld]\n", size);
94
71
                rc = 1;
95
72
                goto out;
96
73
        }
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",
 
76
                       size);
100
77
                rc = 1;
101
78
                goto out;
102
79
        }
103
80
        if ((size = lseek(fd, 4096, SEEK_SET)) != 4096) {
104
 
                fprintf(stderr, "Expected 4096 from lseek; got [%jd]\n",
105
 
                        (intmax_t)size);
 
81
                printf("Expected 4096 from lseek; got [%lld]\n", size);
106
82
                rc = 1;
107
83
                goto out;
108
84
        }
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);
112
88
                rc = 1;
113
89
                goto out;
114
90
        }
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");
117
93
                rc = 1;
118
94
                goto out;
119
95
        }
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,
123
 
                        (intmax_t)size);
 
97
                printf("Error attempting to read data. Expected [%lld] bytes; "
 
98
                       "read [%lld] instead\n", 1020, size);
124
99
                rc = 1;
125
100
                goto out;
126
101
        }
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,
 
105
                                buf[i]);
131
106
                        rc = 1;
132
107
                        goto out;
133
108
                }
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);
137
112
                rc = 1;
138
113
                goto out;
139
114
        }
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");
142
117
                rc = 1;
143
118
                goto out;
144
119
        }
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);
148
123
                rc = 1;
149
124
                goto out;
150
125
        }
151
126
        if ((size = lseek(fd, 0, SEEK_SET)) != 0) {
152
 
                fprintf(stderr, "Expected 0 from lseek; got [%jd]\n",
153
 
                        (intmax_t)size);
 
127
                printf("Expected 0 from lseek; got [%lld]\n", size);
154
128
                rc = 1;
155
129
                goto out;
156
130
        }
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,
160
 
                        (intmax_t)size);
 
132
                printf("Error attempting to read data. Expected [%lld] bytes; "
 
133
                       "read [%lld] instead\n", 4096, size);
161
134
                rc = 1;
162
135
                goto out;
163
136
        }
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,
 
140
                                buf[i]);
168
141
                        rc = 1;
169
142
                        goto out;
170
143
                }
171
144
        close(fd);
172
 
        rc = stat(path, &s);
 
145
        rc = stat(FILENAME, &s);
173
146
        if (rc == -1) {
174
 
                fprintf(stderr, "Error attempting to stat file [%s]\n",
175
 
                        path);
 
147
                printf("Error attempting to stat file [%s]\n", FILENAME);
176
148
                rc = 1;
177
149
                goto out;
178
150
        }
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);
182
153
                rc = 1;
183
154
                goto out;
184
155
        }
185
 
        fd = open(path, (O_RDONLY));
 
156
        fd = open(FILENAME, (O_RDONLY));
186
157
        if (fd == -1) {
187
 
                fprintf(stderr, "Error attempting to create new file [%s]\n",
188
 
                        path);
 
158
                printf("Error attempting to create new file [%s]\n", FILENAME);
189
159
                rc = 1;
190
160
                goto out;
191
161
        }
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,
195
 
                        (intmax_t)size);
 
163
                printf("Error attempting to read data. Expected [%lld] bytes; "
 
164
                       "read [%lld] instead\n", 4096, size);
196
165
                rc = 1;
197
166
                goto out;
198
167
        }
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,
 
171
                                buf[i]);
203
172
                        rc = 1;
204
173
                        goto out;
205
174
                }
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);
209
178
                rc = 1;
210
179
                goto out;
211
180
        }
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");
214
183
                rc = 1;
215
184
                goto out;
216
185
        }
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,
220
 
                        (intmax_t)size);
 
187
                printf("Error attempting to read data. Expected [%lld] bytes; "
 
188
                       "read [%lld] instead\n", 1020, size);
221
189
                rc = 1;
222
190
                goto out;
223
191
        }
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,
 
195
                                buf[i]);
228
196
                        rc = 1;
229
197
                        goto out;
230
198
                }
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);
234
202
                rc = 1;
235
203
                goto out;
236
204
        }
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");
239
207
                rc = 1;
240
208
                goto out;
241
209
        }
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);
245
213
                rc = 1;
246
214
                goto out;
247
215
        }
248
216
        close(fd);
249
 
        unlink(path);
 
217
        unlink(FILENAME);
250
218
        rc = 0;
251
219
out:
252
220
        return rc;