summaryrefslogtreecommitdiffstats
path: root/vendor/tempfile/tests/spooled.rs
blob: a3bcc03d8fe3f0aeea19d26a469381f0fe6fef17 (plain)
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
#![deny(rust_2018_idioms)]

use std::io::{Read, Seek, SeekFrom, Write};

use tempfile::{spooled_tempfile, SpooledTempFile};

#[test]
fn test_automatic_rollover() {
    let mut t = spooled_tempfile(10);
    let mut buf = Vec::new();

    assert!(!t.is_rolled());
    assert_eq!(t.stream_position().unwrap(), 0);
    assert_eq!(t.read_to_end(&mut buf).unwrap(), 0);
    assert_eq!(buf.as_slice(), b"");
    buf.clear();

    assert_eq!(t.write(b"abcde").unwrap(), 5);

    assert!(!t.is_rolled());
    assert_eq!(t.seek(SeekFrom::Start(0)).unwrap(), 0);
    assert_eq!(t.read_to_end(&mut buf).unwrap(), 5);
    assert_eq!(buf.as_slice(), b"abcde");

    assert_eq!(t.write(b"fghijklmno").unwrap(), 10);

    assert_eq!(t.stream_position().unwrap(), 15);
    assert!(t.is_rolled());
}

#[test]
fn test_explicit_rollover() {
    let mut t = SpooledTempFile::new(100);
    assert_eq!(t.write(b"abcdefghijklmnopqrstuvwxyz").unwrap(), 26);
    assert_eq!(t.stream_position().unwrap(), 26);
    assert!(!t.is_rolled());

    // roll over explicitly
    assert!(t.roll().is_ok());
    assert!(t.is_rolled());
    assert_eq!(t.stream_position().unwrap(), 26);

    let mut buf = Vec::new();
    assert_eq!(t.read_to_end(&mut buf).unwrap(), 0);
    assert_eq!(buf.as_slice(), b"");
    buf.clear();

    assert_eq!(t.seek(SeekFrom::Start(0)).unwrap(), 0);
    assert_eq!(t.read_to_end(&mut buf).unwrap(), 26);
    assert_eq!(buf.as_slice(), b"abcdefghijklmnopqrstuvwxyz");
    assert_eq!(t.stream_position().unwrap(), 26);
}

// called by test_seek_{buffer, file}
// assumes t is empty and offset is 0 to start
fn test_seek(t: &mut SpooledTempFile) {
    assert_eq!(t.write(b"abcdefghijklmnopqrstuvwxyz").unwrap(), 26);

    assert_eq!(t.stream_position().unwrap(), 26); // tell()
    assert_eq!(t.seek(SeekFrom::Current(-1)).unwrap(), 25);
    assert_eq!(t.seek(SeekFrom::Current(1)).unwrap(), 26);
    assert_eq!(t.seek(SeekFrom::Current(1)).unwrap(), 27);
    assert_eq!(t.seek(SeekFrom::Current(-27)).unwrap(), 0);
    assert!(t.seek(SeekFrom::Current(-1)).is_err());
    assert!(t.seek(SeekFrom::Current(-1245)).is_err());

    assert_eq!(t.seek(SeekFrom::Start(0)).unwrap(), 0);
    assert_eq!(t.seek(SeekFrom::Start(1)).unwrap(), 1);
    assert_eq!(t.seek(SeekFrom::Start(26)).unwrap(), 26);
    assert_eq!(t.seek(SeekFrom::Start(27)).unwrap(), 27);
    // // these are build errors
    // assert!(t.seek(SeekFrom::Start(-1)).is_err());
    // assert!(t.seek(SeekFrom::Start(-1000)).is_err());

    assert_eq!(t.seek(SeekFrom::End(0)).unwrap(), 26);
    assert_eq!(t.seek(SeekFrom::End(-1)).unwrap(), 25);
    assert_eq!(t.seek(SeekFrom::End(-26)).unwrap(), 0);
    assert!(t.seek(SeekFrom::End(-27)).is_err());
    assert!(t.seek(SeekFrom::End(-99)).is_err());
    assert_eq!(t.seek(SeekFrom::End(1)).unwrap(), 27);
    assert_eq!(t.seek(SeekFrom::End(1)).unwrap(), 27);
}

#[test]
fn test_seek_buffer() {
    let mut t = spooled_tempfile(100);
    test_seek(&mut t);
}

#[test]
fn test_seek_file() {
    let mut t = SpooledTempFile::new(10);
    test_seek(&mut t);
}

fn test_seek_read(t: &mut SpooledTempFile) {
    assert_eq!(t.write(b"abcdefghijklmnopqrstuvwxyz").unwrap(), 26);

    let mut buf = Vec::new();

    // we're at the end
    assert_eq!(t.read_to_end(&mut buf).unwrap(), 0);
    assert_eq!(buf.as_slice(), b"");
    buf.clear();

    // seek to start, read whole thing
    assert_eq!(t.seek(SeekFrom::Start(0)).unwrap(), 0);
    assert_eq!(t.read_to_end(&mut buf).unwrap(), 26);
    assert_eq!(buf.as_slice(), b"abcdefghijklmnopqrstuvwxyz");
    buf.clear();

    // now we're at the end again
    assert_eq!(t.stream_position().unwrap(), 26); // tell()
    assert_eq!(t.read_to_end(&mut buf).unwrap(), 0);
    assert_eq!(buf.as_slice(), b"");
    buf.clear();

    // seek to somewhere in the middle, read a bit
    assert_eq!(t.seek(SeekFrom::Start(5)).unwrap(), 5);
    let mut buf = [0; 5];
    assert!(t.read_exact(&mut buf).is_ok());
    assert_eq!(buf, *b"fghij");

    // read again from current spot
    assert_eq!(t.stream_position().unwrap(), 10); // tell()
    assert!(t.read_exact(&mut buf).is_ok());
    assert_eq!(buf, *b"klmno");

    let mut buf = [0; 15];
    // partial read
    assert_eq!(t.read(&mut buf).unwrap(), 11);
    assert_eq!(buf[0..11], *b"pqrstuvwxyz");

    // try to read off the end: UnexpectedEof
    assert!(t.read_exact(&mut buf).is_err());
}

#[test]
fn test_seek_read_buffer() {
    let mut t = spooled_tempfile(100);
    test_seek_read(&mut t);
}

#[test]
fn test_seek_read_file() {
    let mut t = SpooledTempFile::new(10);
    test_seek_read(&mut t);
}

fn test_overwrite_middle(t: &mut SpooledTempFile) {
    assert_eq!(t.write(b"abcdefghijklmnopqrstuvwxyz").unwrap(), 26);

    assert_eq!(t.seek(SeekFrom::Start(10)).unwrap(), 10);
    assert_eq!(t.write(b"0123456789").unwrap(), 10);
    assert_eq!(t.seek(SeekFrom::Start(0)).unwrap(), 0);

    let mut buf = Vec::new();
    assert_eq!(t.read_to_end(&mut buf).unwrap(), 26);
    assert_eq!(buf.as_slice(), b"abcdefghij0123456789uvwxyz");
}

#[test]
fn test_overwrite_middle_of_buffer() {
    let mut t = spooled_tempfile(100);
    test_overwrite_middle(&mut t);
}

#[test]
fn test_overwrite_middle_of_file() {
    let mut t = SpooledTempFile::new(10);
    test_overwrite_middle(&mut t);
}

#[test]
fn test_overwrite_and_extend_buffer() {
    let mut t = spooled_tempfile(100);
    assert_eq!(t.write(b"abcdefghijklmnopqrstuvwxyz").unwrap(), 26);
    assert_eq!(t.seek(SeekFrom::End(-5)).unwrap(), 21);
    assert_eq!(t.write(b"0123456789").unwrap(), 10);
    assert_eq!(t.seek(SeekFrom::Start(0)).unwrap(), 0);
    let mut buf = Vec::new();
    assert_eq!(t.read_to_end(&mut buf).unwrap(), 31);
    assert_eq!(buf.as_slice(), b"abcdefghijklmnopqrstu0123456789");
    assert!(!t.is_rolled());
}

#[test]
fn test_overwrite_and_extend_rollover() {
    let mut t = SpooledTempFile::new(20);
    assert_eq!(t.write(b"abcdefghijklmno").unwrap(), 15);
    assert!(!t.is_rolled());
    assert_eq!(t.seek(SeekFrom::End(-5)).unwrap(), 10);
    assert_eq!(t.stream_position().unwrap(), 10); // tell()
    assert!(!t.is_rolled());
    assert_eq!(t.write(b"0123456789)!@#$%^&*(").unwrap(), 20);
    assert!(t.is_rolled());
    assert_eq!(t.stream_position().unwrap(), 30); // tell()
    let mut buf = Vec::new();
    assert_eq!(t.seek(SeekFrom::Start(0)).unwrap(), 0);
    assert_eq!(t.read_to_end(&mut buf).unwrap(), 30);
    assert_eq!(buf.as_slice(), b"abcdefghij0123456789)!@#$%^&*(");
}

fn test_sparse(t: &mut SpooledTempFile) {
    assert_eq!(t.write(b"abcde").unwrap(), 5);
    assert_eq!(t.seek(SeekFrom::Current(5)).unwrap(), 10);
    assert_eq!(t.write(b"klmno").unwrap(), 5);
    assert_eq!(t.seek(SeekFrom::Start(0)).unwrap(), 0);
    let mut buf = Vec::new();
    assert_eq!(t.read_to_end(&mut buf).unwrap(), 15);
    assert_eq!(buf.as_slice(), b"abcde\0\0\0\0\0klmno");
}

#[test]
fn test_sparse_buffer() {
    let mut t = spooled_tempfile(100);
    test_sparse(&mut t);
}

#[test]
fn test_sparse_file() {
    let mut t = SpooledTempFile::new(1);
    test_sparse(&mut t);
}

#[test]
fn test_sparse_write_rollover() {
    let mut t = spooled_tempfile(10);
    assert_eq!(t.write(b"abcde").unwrap(), 5);
    assert!(!t.is_rolled());
    assert_eq!(t.seek(SeekFrom::Current(5)).unwrap(), 10);
    assert!(!t.is_rolled());
    assert_eq!(t.write(b"klmno").unwrap(), 5);
    assert!(t.is_rolled());
    assert_eq!(t.seek(SeekFrom::Start(0)).unwrap(), 0);
    let mut buf = Vec::new();
    assert_eq!(t.read_to_end(&mut buf).unwrap(), 15);
    assert_eq!(buf.as_slice(), b"abcde\0\0\0\0\0klmno");
}

fn test_set_len(t: &mut SpooledTempFile) {
    let mut buf: Vec<u8> = Vec::new();

    assert_eq!(t.write(b"abcdefghijklmnopqrstuvwxyz").unwrap(), 26);

    // truncate to 10 bytes
    assert!(t.set_len(10).is_ok());

    // position should not have moved
    assert_eq!(t.stream_position().unwrap(), 26); // tell()

    assert_eq!(t.read_to_end(&mut buf).unwrap(), 0);
    assert_eq!(buf.as_slice(), b"");
    assert_eq!(t.stream_position().unwrap(), 26); // tell()
    buf.clear();

    // read whole thing
    assert_eq!(t.seek(SeekFrom::Start(0)).unwrap(), 0);
    assert_eq!(t.read_to_end(&mut buf).unwrap(), 10);
    assert_eq!(buf.as_slice(), b"abcdefghij");
    buf.clear();

    // set_len to expand beyond the end
    assert!(t.set_len(40).is_ok());
    assert_eq!(t.stream_position().unwrap(), 10); // tell()
    assert_eq!(t.seek(SeekFrom::Start(0)).unwrap(), 0);
    assert_eq!(t.read_to_end(&mut buf).unwrap(), 40);
    assert_eq!(
        buf.as_slice(),
        &b"abcdefghij\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"[..]
    );
}

#[test]
fn test_set_len_buffer() {
    let mut t = spooled_tempfile(100);
    test_set_len(&mut t);
}

#[test]
fn test_set_len_file() {
    let mut t = spooled_tempfile(100);
    test_set_len(&mut t);
}

#[test]
fn test_set_len_rollover() {
    let mut buf: Vec<u8> = Vec::new();

    let mut t = spooled_tempfile(10);
    assert_eq!(t.write(b"abcde").unwrap(), 5);
    assert!(!t.is_rolled());
    assert_eq!(t.stream_position().unwrap(), 5); // tell()

    assert_eq!(t.seek(SeekFrom::Start(0)).unwrap(), 0);
    assert_eq!(t.read_to_end(&mut buf).unwrap(), 5);
    assert_eq!(buf.as_slice(), b"abcde");
    assert_eq!(t.stream_position().unwrap(), 5); // tell()
    buf.clear();

    assert!(t.set_len(20).is_ok());
    assert!(t.is_rolled());
    assert_eq!(t.stream_position().unwrap(), 5); // tell()
    assert_eq!(t.seek(SeekFrom::Start(0)).unwrap(), 0);
    assert_eq!(t.read_to_end(&mut buf).unwrap(), 20);
    assert_eq!(buf.as_slice(), b"abcde\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0");
}