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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
|
/*
stream tests
todo: do we need a seek test that seeks beyond the eos, writes,
then reads and checks for 0's in the space?
*/
#include <string.h>
#include "camel/camel-stream.h"
#include "camel/camel-seekable-stream.h"
#include "camel/camel-seekable-substream.h"
#include "streams.h"
#include "camel-test.h"
static char teststring[] = "\xaa\x55\xc0\x0c\xff\x00";
static char testbuf[10240];
/* pass in an empty read/write stream */
void
test_stream_seekable_writepart(CamelSeekableStream *s)
{
off_t end;
int i;
push("seekable stream test, writing ");
check(camel_seekable_stream_tell(s) == 0);
check(camel_seekable_stream_seek(s, 0, CAMEL_STREAM_SET) == 0);
check(camel_seekable_stream_tell(s) == 0);
check(camel_stream_write(CAMEL_STREAM(s), "", 0) == 0);
check(camel_seekable_stream_tell(s) == 0);
check(camel_stream_write(CAMEL_STREAM(s), "\n", 1) == 1);
check(camel_seekable_stream_tell(s) == 1);
for (i=0;i<10240;i++) {
check(camel_stream_write(CAMEL_STREAM(s), teststring, sizeof(teststring)) == sizeof(teststring));
check(camel_seekable_stream_tell(s) == 1 + (i+1)*sizeof(teststring));
}
end = 10240*sizeof(teststring)+1;
check_msg(camel_seekable_stream_seek(s, 0, CAMEL_STREAM_END) == end, "seek =%d end = %d",
camel_seekable_stream_seek(s, 0, CAMEL_STREAM_END), end);
check(camel_seekable_stream_seek(s, 0, CAMEL_STREAM_END) == end);
check(camel_seekable_stream_tell(s) == end);
/* need to read 0 first to set eos */
check(camel_stream_read(CAMEL_STREAM(s), testbuf, 10240) == 0);
check(camel_stream_eos(CAMEL_STREAM(s)));
pull();
}
void
test_stream_seekable_readpart(CamelSeekableStream *s)
{
off_t off, new, end;
int i, j;
push("seekable stream test, re-reading");
end = 10240*sizeof(teststring)+1;
check(camel_seekable_stream_seek(s, 0, CAMEL_STREAM_SET) == 0);
check(camel_seekable_stream_tell(s) == 0);
check(!camel_stream_eos(CAMEL_STREAM(s)));
off = 0;
for (i=0;i<1024;i++) {
new = i*3;
/* exercise all seek methods */
switch(i % 3) {
case 0:
check(camel_seekable_stream_seek(s, new, CAMEL_STREAM_SET) == new);
break;
case 1:
check(camel_seekable_stream_seek(s, new-off, CAMEL_STREAM_CUR) == new);
break;
case 2:
check(camel_seekable_stream_seek(s, new-end, CAMEL_STREAM_END) == new);
break;
}
check(camel_seekable_stream_tell(s) == new);
check(camel_stream_read(CAMEL_STREAM(s), testbuf, i*3) == i*3);
for (j=0;j<i*3;j++) {
int k = new + j;
if (k==0) {
check(testbuf[j] == '\n');
} else {
check(testbuf[j] == teststring[(k-1) % sizeof(teststring)]);
}
}
off = new+i*3;
}
/* verify end-of-file behaviour */
check(camel_seekable_stream_seek(s, -1, CAMEL_STREAM_END) == end-1);
check(camel_seekable_stream_tell(s) == end-1);
check(camel_stream_read(CAMEL_STREAM(s), testbuf, 10240) == 1);
check(testbuf[0] == teststring[sizeof(teststring)-1]);
check(camel_stream_read(CAMEL_STREAM(s), testbuf, 10240) == 0);
check(camel_seekable_stream_seek(s, 0, CAMEL_STREAM_CUR) == end);
check(camel_seekable_stream_tell(s) == end);
check(camel_stream_eos(CAMEL_STREAM(s)));
pull();
}
/*
0 = write to the substream
1 = write to the parent stream at the right spot
*/
void
test_seekable_substream_writepart(CamelStream *s, int type)
{
CamelSeekableStream *ss = (CamelSeekableStream *)s;
CamelSeekableSubstream *sus = (CamelSeekableSubstream *)s;
CamelSeekableStream *sp = sus->parent_stream;
int i, len;
off_t end;
push("writing substream, type %d", type);
if (type == 1) {
check(camel_seekable_stream_seek(sp, ss->bound_start, CAMEL_STREAM_SET) == ss->bound_start);
s = (CamelStream *)sp;
} else {
check(camel_seekable_stream_tell(ss) == ss->bound_start);
check(camel_seekable_stream_seek(ss, 0, CAMEL_STREAM_SET) == ss->bound_start);
}
check(camel_seekable_stream_tell(CAMEL_SEEKABLE_STREAM(s)) == ss->bound_start);
check(camel_stream_write(s, "", 0) == 0);
check(camel_seekable_stream_tell(CAMEL_SEEKABLE_STREAM(s)) == ss->bound_start);
/* fill up the bounds with writes */
if (ss->bound_end != CAMEL_STREAM_UNBOUND) {
for (i=0;i<(ss->bound_end-ss->bound_start)/sizeof(teststring);i++) {
check(camel_stream_write(s, teststring, sizeof(teststring)) == sizeof(teststring));
check(camel_seekable_stream_tell(CAMEL_SEEKABLE_STREAM(s)) == ss->bound_start + (i+1)*sizeof(teststring));
}
len = (ss->bound_end-ss->bound_start) % sizeof(teststring);
check(camel_stream_write(s, teststring, len) == len);
check(camel_seekable_stream_tell(CAMEL_SEEKABLE_STREAM(s)) == ss->bound_end);
if (type == 0) {
check(camel_stream_write(s, teststring, sizeof(teststring)) == 0);
check(camel_stream_eos(s));
check(camel_seekable_stream_tell(CAMEL_SEEKABLE_STREAM(s)) == ss->bound_end);
}
} else {
/* just 10K */
for (i=0;i<10240;i++) {
check(camel_stream_write(CAMEL_STREAM(s), teststring, sizeof(teststring)) == sizeof(teststring));
check(camel_seekable_stream_tell(CAMEL_SEEKABLE_STREAM(s)) == ss->bound_start + (i+1)*sizeof(teststring));
}
/* we can't really verify any end length here */
}
pull();
}
void
test_seekable_substream_readpart(CamelStream *s)
{
CamelSeekableStream *ss = (CamelSeekableStream *)s;
CamelSeekableSubstream *sus = (CamelSeekableSubstream *)s;
CamelSeekableStream *sp = sus->parent_stream;
int i, len;
off_t end;
push("reading substream");
check(camel_seekable_stream_seek(ss, 0, CAMEL_STREAM_SET) == ss->bound_start);
check(camel_seekable_stream_tell(ss) == ss->bound_start);
check(camel_seekable_stream_seek(sp, ss->bound_start, CAMEL_STREAM_SET) == ss->bound_start);
check(camel_seekable_stream_tell(sp) == ss->bound_start);
/* check writes, cross check with parent stream */
if (ss->bound_end != CAMEL_STREAM_UNBOUND) {
for (i=0;i<(ss->bound_end-ss->bound_start)/sizeof(teststring);i++) {
check(camel_stream_read(s, testbuf, sizeof(teststring)) == sizeof(teststring));
check(memcmp(testbuf, teststring, sizeof(teststring)) == 0);
check(camel_seekable_stream_tell(ss) == ss->bound_start + (i+1)*sizeof(teststring));
/* yeah great, the substreams affect the seek ... */
check(camel_seekable_stream_seek(sp, ss->bound_start + (i)*sizeof(teststring), CAMEL_STREAM_SET) == ss->bound_start + i*sizeof(teststring));
check(camel_stream_read(CAMEL_STREAM(sp), testbuf, sizeof(teststring)) == sizeof(teststring));
check(memcmp(testbuf, teststring, sizeof(teststring)) == 0);
check(camel_seekable_stream_tell(sp) == ss->bound_start + (i+1)*sizeof(teststring));
}
len = (ss->bound_end-ss->bound_start) % sizeof(teststring);
check(camel_stream_read(s, testbuf, len) == len);
check(memcmp(testbuf, teststring, len) == 0);
check(camel_seekable_stream_seek(sp, ss->bound_end - len, CAMEL_STREAM_SET) == ss->bound_end - len);
check(camel_stream_read(CAMEL_STREAM(sp), testbuf, len) == len);
check(memcmp(testbuf, teststring, len) == 0);
check(camel_stream_eos(s));
check(camel_seekable_stream_tell(ss) == ss->bound_end);
check(camel_seekable_stream_tell(sp) == ss->bound_end);
check(camel_stream_read(s, testbuf, 1024) == 0);
check(camel_seekable_stream_tell(ss) == ss->bound_end);
check(camel_seekable_stream_tell(sp) == ss->bound_end);
check(camel_stream_eos(s));
} else {
/* just 10K */
for (i=0;i<10240;i++) {
check(camel_stream_read(s, testbuf, sizeof(teststring)) == sizeof(teststring));
check(memcmp(testbuf, teststring, sizeof(teststring)) == 0);
check(camel_seekable_stream_tell(ss) == ss->bound_start + (i+1)*sizeof(teststring));
check(camel_seekable_stream_seek(sp, ss->bound_start + (i)*sizeof(teststring), CAMEL_STREAM_SET) == ss->bound_start + i*sizeof(teststring));
check(camel_stream_read(CAMEL_STREAM(sp), testbuf, sizeof(teststring)) == sizeof(teststring));
check(memcmp(testbuf, teststring, sizeof(teststring)) == 0);
check(camel_seekable_stream_tell(sp) == ss->bound_start + (i+1)*sizeof(teststring));
}
/* unbound - we dont know the real length */
#if 0
end = 10240*sizeof(teststring)+ss->bound_start;
check(camel_seekable_stream_seek(ss, 0, CAMEL_STREAM_END) == end);
check(camel_seekable_stream_tell(ss) == end);
/* need to read 0 first to set eos */
check(camel_stream_read(s, testbuf, 10240) == 0);
check(camel_stream_eos(s));
#endif
}
pull();
}
|