-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest.c
executable file
·189 lines (139 loc) · 4.85 KB
/
test.c
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <assert.h>
#include <stdlib.h>
#include <openssl/evp.h>
#include <openssl/rand.h>
#include "csfio.h"
int main(int argc, char **argv) {
int iter = 100;
int buffer_sz_max = 100*1024;
int i,j,k;
CSF_CTX *csf_ctx;
unsigned char *key;
int key_len;
int fd0, fd1;
ssize_t sz0, sz1;
ssize_t tmp_sz0, tmp_sz1;
ssize_t pos0, pos1;
unsigned char *buffer0, *buffer1;
char *test_buffer = "1";
printf("RAND_MAX = %u\n", RAND_MAX);
printf("preparing random key\n");
key_len = 256;
key = calloc(key_len, 1);
RAND_pseudo_bytes(key, key_len);
printf("testing seek past end of file\n");
fd0 = open("./testfile.raw", O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
fd1 = open("./testfile.csf", O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
csf_ctx_init(&csf_ctx, &fd1, key, key_len, 512);
printf("testing 0, SEEK_SET\n");
lseek(fd0, 0, SEEK_SET);
write(fd0, test_buffer, 1);
assert(lseek(fd0, 0, SEEK_END) == 1);
csf_seek(csf_ctx, 0, SEEK_SET);
csf_write(csf_ctx, test_buffer, 1);
assert(csf_seek(csf_ctx, 0, SEEK_END) == 1);
printf("testing seek past EOF with SEEK_SET\n");
lseek(fd0, 6000, SEEK_SET);
write(fd0, test_buffer, 1);
assert(lseek(fd0, 0, SEEK_END) == 6001);
csf_seek(csf_ctx, 6000, SEEK_SET);
csf_write(csf_ctx, test_buffer, 1);
assert(csf_seek(csf_ctx, 0, SEEK_END) == 6001);
printf("testing seek past EOF with SEEK_CUR\n");
lseek(fd0, 6000, SEEK_CUR);
write(fd0, test_buffer, 1);
assert(lseek(fd0, 0, SEEK_END) == 12002);
csf_seek(csf_ctx, 6000, SEEK_CUR);
csf_write(csf_ctx, test_buffer, 1);
assert(csf_seek(csf_ctx, 0, SEEK_END) == 12002);
printf("rewind with with SEEK_SET\n");
assert(lseek(fd0, 4000, SEEK_SET) == 4000);
assert(csf_seek(csf_ctx, 4000, SEEK_SET) == 4000);
printf("testing seek past EOF with SEEK_END\n");
lseek(fd0, 3000, SEEK_END);
write(fd0, test_buffer, 1);
printf("lseek = %d\n", lseek(fd0, 0, SEEK_END) );
assert(lseek(fd0, 0, SEEK_END) == 15003);
csf_seek(csf_ctx, 3000, SEEK_END);
csf_write(csf_ctx, test_buffer, 1);
assert(csf_seek(csf_ctx, 0, SEEK_END) == 15003);
close(fd0);
close(fd1);
csf_ctx_destroy(csf_ctx);
printf("testing randomized file writes\n");
for(i = 0; i < iter; i++) {
fd0 = open("./testfile.raw", O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
fd1 = open("./testfile.csf", O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
csf_ctx_init(&csf_ctx, &fd1, key, key_len, 512);
srand(i);
sz0 = sz1 = rand() % buffer_sz_max;
buffer0 = calloc(sz0, 1);
RAND_pseudo_bytes(buffer0, sz0);
buffer1 = calloc(sz1, 1);
memcpy(buffer1, buffer0, sz1);
tmp_sz0 = write(fd0, buffer0, sz0);
//tmp_sz1 = write(fd1, buffer1, sz1);
tmp_sz1 = csf_write(csf_ctx, buffer1, sz1);
assert(tmp_sz0 == tmp_sz1);
for(j = 0; j < iter; j++) {
int offset = rand() % sz0;
int len = rand() % (sz0 - offset);
RAND_pseudo_bytes(buffer0, len);
//memset(buffer0, 0, sz0);
memcpy(buffer1, buffer0, len);
lseek(fd0, offset, SEEK_SET);
//lseek(fd1, offset, SEEK_SET);
csf_seek(csf_ctx, offset, SEEK_SET);
tmp_sz0 = write(fd0, buffer0, len);
//tmp_sz1 = write(fd1, buffer1, len);
tmp_sz1 = csf_write(csf_ctx, buffer1, len);
assert(tmp_sz0 == tmp_sz1);
assert(tmp_sz0 == len);
/* read current location, verify */
pos0 = lseek(fd0, 0, SEEK_CUR);
//pos1 = lseek(fd1, 0, SEEK_CUR);
pos1 = csf_seek(csf_ctx, 0, SEEK_CUR);
assert(pos0 == pos1);
/* read back and verify write */
lseek(fd0, offset, SEEK_SET);
//lseek(fd1, offset, SEEK_SET);
csf_seek(csf_ctx, offset, SEEK_SET);
tmp_sz0 = read(fd0, buffer0, len);
//tmp_sz1 = read(fd1, buffer1, len);
tmp_sz1 = csf_read(csf_ctx, buffer1, len);
assert(tmp_sz0 == tmp_sz1);
assert(tmp_sz0 == len);
assert(memcmp(buffer0, buffer1, len) == 0);
}
/* read back the entire file and verify front to back */
lseek(fd0, 0, SEEK_SET);
//lseek(fd1, 0, SEEK_SET);
csf_seek(csf_ctx, 0, SEEK_SET);
tmp_sz0 = read(fd0, buffer0, sz0);
//tmp_sz1 = read(fd1, buffer1, sz1);
tmp_sz1 = csf_read(csf_ctx, buffer1, sz1);
assert(tmp_sz0 == sz0);
assert(tmp_sz1 == sz1);
assert(tmp_sz0 == tmp_sz1);
assert(memcmp(buffer0, buffer1, sz0) == 0);
/* read file size, verify */
pos0 = lseek(fd0, 0, SEEK_END);
//pos1 = lseek(fd1, 0, SEEK_END);
pos1 = csf_seek(csf_ctx, 0, SEEK_END);
assert(pos0 == pos1);
assert(pos0 == sz0);
printf("iteraton %d: wrote %d bytes, read %d bytes\n", i, sz0, sz0);
free(buffer0);
free(buffer1);
csf_ctx_destroy(csf_ctx);
memset(key, 0, key_len);
close(fd0);
close(fd1);
}
free(key);
}