@@ -264,140 +264,77 @@ mod tests {
264
264
) ;
265
265
}
266
266
267
- // #[test]
268
- // fn test_bad_snapshot_size() {
269
- // let snapshot_data = vec![0u8; 1];
270
-
271
- // let snapshot = SnapshotHdr::new(Version::new(1, 6, 1));
272
- // assert!(matches!(
273
- // snapshot.load_with_version_check::<_, u8>(
274
- // &mut snapshot_data.as_slice(),
275
- // snapshot_data.len()
276
- // ),
277
- // Err(SnapshotError::InvalidSnapshotSize)
278
- // ));
279
- // }
280
-
281
- // #[test]
282
- // fn test_bad_reader() {
283
- // #[derive(Debug)]
284
- // struct BadReader;
285
-
286
- // impl Read for BadReader {
287
- // fn read(&mut self, _buf: &mut [u8]) -> std::io::Result<usize> {
288
- // Err(std::io::ErrorKind::InvalidInput.into())
289
- // }
290
- // }
291
-
292
- // let mut reader = BadReader {};
293
-
294
- // let snapshot = Snapshot::new(Version::new(42, 27, 18));
295
- // assert!(matches!(
296
- // snapshot.load_with_version_check::<_, u8>(&mut reader, 1024),
297
- // Err(SnapshotError::Io(_))
298
- // ));
299
- // }
300
-
301
- // #[test]
302
- // fn test_bad_magic() {
303
- // let mut data = vec![0u8; 100];
304
-
305
- // let snapshot = Snapshot::new(Version::new(24, 16, 1));
306
- // snapshot.save(&mut data.as_mut_slice(), &42u8).unwrap();
307
-
308
- // // Writing dummy values in the first bytes of the snapshot data (we are on little-endian
309
- // // machines) should trigger an `Error::InvalidMagic` error.
310
- // data[0] = 0x01;
311
- // data[1] = 0x02;
312
- // data[2] = 0x03;
313
- // data[3] = 0x04;
314
- // data[4] = 0x42;
315
- // data[5] = 0x43;
316
- // data[6] = 0x44;
317
- // data[7] = 0x45;
318
- // assert!(matches!(
319
- // Snapshot::unchecked_load::<_, u8>(&mut data.as_slice()),
320
- // Err(SnapshotError::InvalidMagic(0x4544_4342_0403_0201u64))
321
- // ));
322
- // }
323
-
324
- // #[test]
325
- // fn test_bad_crc() {
326
- // let mut data = vec![0u8; 100];
327
-
328
- // let snapshot = Snapshot::new(Version::new(12, 1, 3));
329
- // snapshot.save(&mut data.as_mut_slice(), &42u8).unwrap();
330
-
331
- // // Tamper the bytes written, without touching the previously CRC.
332
- // snapshot
333
- // .save_without_crc(&mut data.as_mut_slice(), &43u8)
334
- // .unwrap();
335
-
336
- // assert!(matches!(
337
- // snapshot.load_with_version_check::<_, u8>(&mut data.as_slice(), data.len()),
338
- // Err(SnapshotError::Crc64(_))
339
- // ));
340
- // }
341
-
342
- // #[test]
343
- // fn test_bad_version() {
344
- // let mut data = vec![0u8; 100];
345
-
346
- // // We write a snapshot with version "v1.3.12"
347
- // let snapshot = Snapshot::new(Version::new(1, 3, 12));
348
- // snapshot.save(&mut data.as_mut_slice(), &42u8).unwrap();
349
-
350
- // // Different major versions should not work
351
- // let snapshot = Snapshot::new(Version::new(2, 3, 12));
352
- // assert!(matches!(
353
- // snapshot.load_with_version_check::<_, u8>(&mut data.as_slice(), data.len()),
354
- // Err(SnapshotError::InvalidFormatVersion(Version {
355
- // major: 1,
356
- // minor: 3,
357
- // patch: 12,
358
- // ..
359
- // }))
360
- // ));
361
- // let snapshot = Snapshot::new(Version::new(0, 3, 12));
362
- // assert!(matches!(
363
- // snapshot.load_with_version_check::<_, u8>(&mut data.as_slice(), data.len()),
364
- // Err(SnapshotError::InvalidFormatVersion(Version {
365
- // major: 1,
366
- // minor: 3,
367
- // patch: 12,
368
- // ..
369
- // }))
370
- // ));
371
-
372
- // // We can't support minor versions bigger than ours
373
- // let snapshot = Snapshot::new(Version::new(1, 2, 12));
374
- // assert!(matches!(
375
- // snapshot.load_with_version_check::<_, u8>(&mut data.as_slice(), data.len()),
376
- // Err(SnapshotError::InvalidFormatVersion(Version {
377
- // major: 1,
378
- // minor: 3,
379
- // patch: 12,
380
- // ..
381
- // }))
382
- // ));
383
-
384
- // // But we can support minor versions smaller or equeal to ours. We also support
385
- // // all patch versions within our supported major.minor version.
386
- // let snapshot = Snapshot::new(Version::new(1, 4, 12));
387
- // snapshot
388
- // .load_with_version_check::<_, u8>(&mut data.as_slice(), data.len())
389
- // .unwrap();
390
- // let snapshot = Snapshot::new(Version::new(1, 3, 0));
391
- // snapshot
392
- // .load_with_version_check::<_, u8>(&mut data.as_slice(), data.len())
393
- // .unwrap();
394
- // let snapshot = Snapshot::new(Version::new(1, 3, 12));
395
- // snapshot
396
- // .load_with_version_check::<_, u8>(&mut data.as_slice(), data.len())
397
- // .unwrap();
398
- // let snapshot = Snapshot::new(Version::new(1, 3, 1024));
399
- // snapshot
400
- // .load_with_version_check::<_, u8>(&mut data.as_slice(), data.len())
401
- // .unwrap();
402
- // }
267
+ #[ test]
268
+ fn test_bad_snapshot_size ( ) {
269
+ let snapshot_data = vec ! [ 0u8 ; 1 ] ;
270
+
271
+ let snapshot = SnapshotHdr :: new ( Version :: new ( 1 , 6 , 1 ) ) ;
272
+ assert ! ( matches!(
273
+ Snapshot :: load:: <_, u8 >(
274
+ & mut snapshot_data. as_slice( ) ,
275
+ ) ,
276
+ Err ( SnapshotError :: InvalidSnapshotSize )
277
+ ) ) ;
278
+ }
279
+
280
+ #[ test]
281
+ fn test_bad_reader ( ) {
282
+ #[ derive( Debug ) ]
283
+ struct BadReader ;
284
+
285
+ impl Read for BadReader {
286
+ fn read ( & mut self , _buf : & mut [ u8 ] ) -> std:: io:: Result < usize > {
287
+ Err ( std:: io:: ErrorKind :: InvalidInput . into ( ) )
288
+ }
289
+ }
290
+
291
+ let mut reader = BadReader { } ;
292
+
293
+ assert ! ( matches!(
294
+ Snapshot :: load:: <_, u8 >( & mut reader) ,
295
+ Err ( SnapshotError :: Io ( _) )
296
+ ) ) ;
297
+ }
298
+
299
+ #[ test]
300
+ fn test_bad_magic ( ) {
301
+ let mut data = vec ! [ 0u8 ; 100 ] ;
302
+
303
+ let snapshot = Snapshot :: new ( Version :: new ( 24 , 16 , 1 ) , & 42u8 ) ;
304
+ snapshot. save ( & mut data. as_mut_slice ( ) ) . unwrap ( ) ;
305
+
306
+ // Writing dummy values in the first bytes of the snapshot data (we are on little-endian
307
+ // machines) should trigger an `Error::InvalidMagic` error.
308
+ data[ 0 ] = 0x01 ;
309
+ data[ 1 ] = 0x02 ;
310
+ data[ 2 ] = 0x03 ;
311
+ data[ 3 ] = 0x04 ;
312
+ data[ 4 ] = 0x42 ;
313
+ data[ 5 ] = 0x43 ;
314
+ data[ 6 ] = 0x44 ;
315
+ data[ 7 ] = 0x45 ;
316
+ assert ! ( matches!(
317
+ Snapshot :: unchecked_load:: <_, u8 >( & mut data. as_slice( ) ) ,
318
+ Err ( SnapshotError :: InvalidMagic ( 0x4544_4342_0403_0201u64 ) )
319
+ ) ) ;
320
+ }
321
+
322
+ #[ test]
323
+ fn test_bad_crc ( ) {
324
+ let mut data = vec ! [ 0u8 ; 100 ] ;
325
+
326
+ let snapshot = Snapshot :: new ( Version :: new ( 12 , 1 , 3 ) , & 42u8 ) ;
327
+ snapshot. save ( & mut data. as_mut_slice ( ) ) . unwrap ( ) ;
328
+
329
+ // Tamper the bytes written, without touching the previously CRC.
330
+ let snapshot2 = Snapshot :: new ( Version :: new ( 12 , 1 , 3 ) , & 43u8 ) ;
331
+ snapshot2
332
+ . save_without_crc ( & mut data. as_mut_slice ( ) )
333
+ . unwrap ( ) ;
334
+
335
+ assert ! ( matches!(
336
+ Snapshot :: load:: <_, u8 >( & mut data. as_slice( ) ) ,
337
+ Err ( SnapshotError :: Crc64 ( _) )
338
+ ) ) ;
339
+ }
403
340
}
0 commit comments