Diff
Not logged in

Differences From Artifact [10cdd58e03]:

To Artifact [459fe796ba]:


304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
...
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367

368
369
370
371
372
373
374
375
...
377
378
379
380
381
382
383


384
385
386
387
388
389
390
391
...
412
413
414
415
416
417
418


419
420
421
422
423
424
425
426
...
767
768
769
770
771
772
773
774

775
776
777
778
779
780
781
782
...
795
796
797
798
799
800
801

802
803
804
805
806
807
808
809
...
811
812
813
814
815
816
817

818

819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844

845
846
847
848
849
850
851
852
...
853
854
855
856
857
858
859
860

861
862
863
864
865
866
867
868
869
870
871
872

873
874
875
876
877
878
879
880
881
882
883
884
...
887
888
889
890
891
892
893

894
895
896
897
898
899
900
901
902
903
904
905
906
907

908
909
910
911
912
913
914
915
...
918
919
920
921
922
923
924
925

926
927
928
929
930
931
932
933
934
935
936
937
938

939
940
941
942
943
944
945
946
...
949
950
951
952
953
954
955
956

957
958
959
960
961
962
963
964
965
966
967
968
969

970
971
972
973
974
975
976
977
...
982
983
984
985
986
987
988
989

990
991
992
993
994
995
996
997
998
999
1000
1001
1002

1003
1004
1005
1006
1007
1008
1009
1010
....
1013
1014
1015
1016
1017
1018
1019
1020

1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034

1035
1036
1037
1038
1039
1040
1041
1042
....
1045
1046
1047
1048
1049
1050
1051
1052

1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066

1067
1068
1069
1070
1071
1072
1073
1074
....
1077
1078
1079
1080
1081
1082
1083
1084

1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099

1100
1101
1102
1103
1104
1105
1106
1107
....
1112
1113
1114
1115
1116
1117
1118
1119

1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132

1133
1134
1135
1136
1137
1138
1139
1140
....
1155
1156
1157
1158
1159
1160
1161

1162
1163

1164
1165
1166
1167
1168
1169
1170
1171
....
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194

1195
1196

1197
1198
1199
1200
1201
1202
1203

1204

1205
1206
1207
1208
1209
1210
1211
1212
....
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234

1235
1236

1237
1238
1239
1240
1241
1242
1243

1244
1245
1246

1247
1248
1249
1250
1251
1252
1253
1254
....
1256
1257
1258
1259
1260
1261
1262

1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277

1278
1279

1280
1281
1282
1283
1284
1285
1286

1287

1288
1289
1290
1291
1292
1293
1294
1295
....
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319

1320
1321
1322
1323
1324
1325
1326
1327
....
1330
1331
1332
1333
1334
1335
1336
1337

1338
1339

1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350

1351
1352

1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365

1366
1367
1368
1369
1370
1371

1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385

1386
1387

1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399

1400
1401
1402
1403
1404
1405
1406
1407
....
1410
1411
1412
1413
1414
1415
1416

1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430

1431
1432

1433
1434
1435
1436
1437
1438
1439
1440
1441
1442

1443

1444
1445
1446
1447
1448
1449

1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461


1462
1463


1464
1465
1466
1467
1468
1469
1470
1471

1472
1473
1474
1475
1476
1477
1478
1479
        for &i in [chunks_start, chunks_end - 1].iter().chain(&next_two) {
            if self.chunks[i].in_sequencer {
                continue;
            }
            self.chunks[i].in_sequencer = true;
            let pos = get_start_end_positions(self.map_size, i as u32).0 as usize;
            let vec = self.decrypt_chunk(i as u32)?;
            for (p, byte) in self.sequencer
                    .iter_mut()
                    .skip(pos)
                    .zip(vec) {
                *p = byte;
            }
        }
        for chunk in &mut self.chunks[chunks_start..chunks_end] {
            chunk.status = ChunkStatus::ToBeHashed;
            chunk.in_sequencer = true;
        }
................................................................................
        for i in chunks_start..chunks_end {
            if self.chunks[i].in_sequencer {
                continue;
            }
            self.chunks[i].in_sequencer = true;
            let pos = get_start_end_positions(self.map_size, i as u32).0 as usize;
            let vec = self.decrypt_chunk(i as u32)?;
            for (p, byte) in self.sequencer
                    .iter_mut()
                    .skip(pos)
                    .zip(vec) {
                *p = byte
            }
        }
        Ok(())
    }

    fn extend_sequencer_up_to(&mut self, new_len: u64) -> Result<(), SelfEncryptionError<E>> {
        let old_len = self.sequencer.len() as u64;
        if new_len > old_len {
            if new_len > MAX_IN_MEMORY_SIZE as u64 {
                self.sequencer.create_mapping()?;
            } else {

                self.sequencer.extend(iter::repeat(0).take((new_len - old_len) as usize));
            }
        }
        Ok(())
    }

    fn decrypt_chunk(&self, chunk_number: u32) -> Result<Vec<u8>, SelfEncryptionError<E>> {
        let name = &self.sorted_map[chunk_number as usize].hash;
................................................................................
        let (pad, key, iv) = get_pad_key_and_iv(chunk_number, &self.sorted_map, self.map_size);
        let xor_result = xor(&content, &pad);
        let decrypted = encryption::decrypt(&xor_result, &key, &iv)?;
        let mut decompressor = BrotliDecoder::new(vec![]);
        if decompressor.write_all(&decrypted).is_err() {
            return Err(SelfEncryptionError::Compression);
        }


        decompressor.finish().map_err(|_| SelfEncryptionError::Compression)
    }
}

fn encrypt_chunk<E: StorageError>(content: &[u8],
                                  pki: (Pad, Key, Iv))
                                  -> Result<Vec<u8>, SelfEncryptionError<E>> {
    let (pad, key, iv) = pki;
................................................................................
    let n_2_pre_hash = &sorted_map[n_2 as usize].pre_hash;

    let mut pad = [0u8; PAD_SIZE];
    let mut key = [0u8; KEY_SIZE];
    let mut iv = [0u8; IV_SIZE];

    for (pad_iv_el, element) in


        pad.iter_mut().chain(iv.iter_mut()).zip(this_pre_hash.iter().chain(n_2_pre_hash.iter())) {
        *pad_iv_el = *element;
    }

    for (key_el, element) in key.iter_mut().zip(n_1_pre_hash.iter()) {
        *key_el = *element;
    }

................................................................................
                   ((number_of_chunks * MAX_CHUNK_SIZE) as u64));
    }

    fn check_file_size<E: StorageError, S: Storage<E>>(se: &SelfEncryptor<E, S>,
                                                       expected_file_size: u64) {
        assert_eq!(se.file_size, expected_file_size);
        if !se.sorted_map.is_empty() {
            let chunks_cumulated_size =

                se.sorted_map.iter().fold(0u64, |acc, chunk| acc + chunk.source_size);
            assert_eq!(chunks_cumulated_size, expected_file_size);
        }
    }

    #[test]
    fn xor() {
        let mut data: Vec<u8> = vec![];
................................................................................
    fn write() {
        let mut storage = SimpleStorage::new();
        let mut se = SelfEncryptor::new(&mut storage, DataMap::None)
            .expect("Encryptor construction shouldn't fail.");
        let size = 3;
        let offset = 5u32;
        let the_bytes = random_bytes(size);

        se.write(&the_bytes, offset as u64).expect("Writing to encryptor shouldn't fail.");
        check_file_size(&se, (size + offset as usize) as u64);
    }

    #[test]
    fn multiple_writes() {
        let mut storage = SimpleStorage::new();
        let size1 = 3;
................................................................................
        let part1 = random_bytes(size1);
        let part2 = random_bytes(size2);
        let data_map;
        {
            let mut se = SelfEncryptor::new(&mut storage, DataMap::None)
                .expect("First encryptor construction shouldn't fail.");
            // Just testing multiple subsequent write calls

            se.write(&part1, 0).expect("Writing part one to encryptor shouldn't fail.");

            se.write(&part2, size1 as u64).expect("Writing part two to encryptor shouldn't fail.");
            // Let's also test an overwrite.. over middle bytes of part2
            se.write(&[4u8, 2], size1 as u64 + 1)
                .expect("Overwriting on encryptor shouldn't fail.");
            check_file_size(&se, (size1 + size2) as u64);
            data_map = se.close().expect("Closing encryptor shouldn't fail.");
        }
        let mut se = SelfEncryptor::new(&mut storage, data_map)
            .expect("Second encryptor construction shouldn't fail.");
        let fetched =
            se.read(0, (size1 + size2) as u64).expect("Reading from encryptor shouldn't fail.");
        assert!(&fetched[..size1] == &part1[..]);
        assert_eq!(fetched[size1], part2[0]);
        assert!(&fetched[size1 + 1..size1 + 3] == &[4u8, 2][..]);
        assert!(&fetched[size1 + 3..] == &part2[3..]);
    }

    #[test]
    fn three_min_chunks_minus_one() {
        let mut storage = SimpleStorage::new();
        let data_map: DataMap;
        let bytes_len = (MIN_CHUNK_SIZE * 3) - 1;
        let the_bytes = random_bytes(bytes_len as usize);
        {
            let mut se = SelfEncryptor::new(&mut storage, DataMap::None)
                .expect("First encryptor construction shouldn't fail.");

            se.write(&the_bytes, 0).expect("Writing to encryptor shouldn't fail.");
            assert_eq!(se.sorted_map.len(), 0);
            assert_eq!(se.sequencer.len(), bytes_len as usize);
            check_file_size(&se, bytes_len as u64);
            // check close
            data_map = se.close().expect("Closing encryptor shouldn't fail.");
        }
        match data_map {
................................................................................
            DataMap::Chunks(_) => panic!("shall not return DataMap::Chunks"),
            DataMap::Content(ref content) => assert_eq!(content.len(), bytes_len as usize),
            DataMap::None => panic!("shall not return DataMap::None"),
        }
        // check read, write
        let mut new_se = SelfEncryptor::new(&mut storage, data_map)
            .expect("Second encryptor construction shouldn't fail.");
        let fetched =

            new_se.read(0, bytes_len as u64).expect("Reading from encryptor shouldn't fail.");
        assert!(fetched == the_bytes);
    }

    #[test]
    fn three_min_chunks() {
        let mut storage = SimpleStorage::new();
        let data_map: DataMap;
        let the_bytes = random_bytes(MIN_CHUNK_SIZE as usize * 3);
        {
            let mut se = SelfEncryptor::new(&mut storage, DataMap::None)
                .expect("First encryptor construction shouldn't fail.");

            se.write(&the_bytes, 0).expect("Writing to encryptor shouldn't fail.");
            check_file_size(&se, MIN_CHUNK_SIZE as u64 * 3);
            let fetched = se.read(0, MIN_CHUNK_SIZE as u64 * 3)
                .expect("Reading from encryptor shouldn't fail.");
            assert!(fetched == the_bytes);
            data_map = se.close().expect("Closing encryptor shouldn't fail.");
        }
        match data_map {
            DataMap::Chunks(ref chunks) => {
                assert_eq!(chunks.len(), 3);
                assert_eq!(storage.num_entries(), 3);
                for chunk_detail in chunks.iter() {
................................................................................
            }
            DataMap::Content(_) => panic!("shall not return DataMap::Content"),
            DataMap::None => panic!("shall not return DataMap::None"),
        }
        // check read, write
        let mut new_se = SelfEncryptor::new(&mut storage, data_map)
            .expect("Second encryptor construction shouldn't fail.");

        let fetched = new_se.read(0, MIN_CHUNK_SIZE as u64 * 3)
            .expect("Reading again from encryptor shouldn't fail.");
        assert!(fetched == the_bytes);
    }

    #[test]
    fn three_min_chunks_plus_one() {
        let mut storage = SimpleStorage::new();
        let data_map: DataMap;
        let bytes_len = (MIN_CHUNK_SIZE * 3) + 1;
        let the_bytes = random_bytes(bytes_len as usize);
        {
            let mut se = SelfEncryptor::new(&mut storage, DataMap::None)
                .expect("First encryptor construction shouldn't fail.");

            se.write(&the_bytes, 0).expect("Writing to encryptor shouldn't fail.");
            check_file_size(&se, bytes_len as u64);
            data_map = se.close().expect("Closing encryptor shouldn't fail.");
        }
        match data_map {
            DataMap::Chunks(ref chunks) => {
                assert_eq!(chunks.len(), 3);
                assert_eq!(storage.num_entries(), 3);
................................................................................
                }
            }
            DataMap::Content(_) => panic!("shall not return DataMap::Content"),
            DataMap::None => panic!("shall not return DataMap::None"),
        }
        let mut new_se = SelfEncryptor::new(&mut storage, data_map)
            .expect("Second encryptor construction shouldn't fail.");
        let fetched =

            new_se.read(0, bytes_len as u64).expect("Reading from encryptor shouldn't fail.");
        assert!(fetched == the_bytes);
    }

    #[test]
    fn three_max_chunks() {
        let mut storage = SimpleStorage::new();
        let data_map: DataMap;
        let bytes_len = MAX_CHUNK_SIZE * 3;
        let the_bytes = random_bytes(bytes_len as usize);
        {
            let mut se = SelfEncryptor::new(&mut storage, DataMap::None)
                .expect("First encryptor construction shouldn't fail.");

            se.write(&the_bytes, 0).expect("Writing to encryptor shouldn't fail.");
            check_file_size(&se, bytes_len as u64);
            data_map = se.close().expect("Closing encryptor shouldn't fail.");
        }
        match data_map {
            DataMap::Chunks(ref chunks) => {
                assert_eq!(chunks.len(), 3);
                assert_eq!(storage.num_entries(), 3);
................................................................................
                }
            }
            DataMap::Content(_) => panic!("shall not return DataMap::Content"),
            DataMap::None => panic!("shall not return DataMap::None"),
        }
        let mut new_se = SelfEncryptor::new(&mut storage, data_map)
            .expect("Second encryptor construction shouldn't fail.");
        let fetched =

            new_se.read(0, bytes_len as u64).expect("Reading from encryptor shouldn't fail.");
        assert!(fetched == the_bytes);
    }

    #[test]
    fn three_max_chunks_plus_one() {
        let mut storage = SimpleStorage::new();
        let data_map: DataMap;
        let bytes_len = (MAX_CHUNK_SIZE * 3) + 1;
        let the_bytes = random_bytes(bytes_len as usize);
        {
            let mut se = SelfEncryptor::new(&mut storage, DataMap::None)
                .expect("First encryptor construction shouldn't fail.");

            se.write(&the_bytes, 0).expect("Writing to encryptor shouldn't fail.");
            check_file_size(&se, bytes_len as u64);
            // check close
            data_map = se.close().expect("Closing encryptor shouldn't fail.");
        }
        match data_map {
            DataMap::Chunks(ref chunks) => {
                assert_eq!(chunks.len(), 4);
................................................................................
            }
            DataMap::Content(_) => panic!("shall not return DataMap::Content"),
            DataMap::None => panic!("shall not return DataMap::None"),
        }
        // check read and write
        let mut new_se = SelfEncryptor::new(&mut storage, data_map)
            .expect("Second encryptor construction shouldn't fail.");
        let fetched =

            new_se.read(0, bytes_len as u64).expect("Reading from encryptor shouldn't fail.");
        assert!(fetched == the_bytes);
    }

    #[test]
    fn seven_and_a_bit_max_chunks() {
        let mut storage = SimpleStorage::new();
        let data_map: DataMap;
        let bytes_len = (MAX_CHUNK_SIZE * 7) + 1024;
        let the_bytes = random_bytes(bytes_len as usize);
        {
            let mut se = SelfEncryptor::new(&mut storage, DataMap::None)
                .expect("First encryptor construction shouldn't fail.");

            se.write(&the_bytes, 0).expect("Writing to encryptor shouldn't fail.");
            check_file_size(&se, bytes_len as u64);
            data_map = se.close().expect("Closing encryptor shouldn't fail.");
        }
        match data_map {
            DataMap::Chunks(ref chunks) => {
                assert_eq!(chunks.len(), 8);
                assert_eq!(storage.num_entries(), 8);
................................................................................
                }
            }
            DataMap::Content(_) => panic!("shall not return DataMap::Content"),
            DataMap::None => panic!("shall not return DataMap::None"),
        }
        let mut new_se = SelfEncryptor::new(&mut storage, data_map)
            .expect("Second encryptor construction shouldn't fail.");
        let fetched =

            new_se.read(0, bytes_len as u64).expect("Reading from encryptor shouldn't fail.");
        assert!(fetched == the_bytes);
    }

    #[test]
    fn large_file_one_byte_under_eleven_chunks() {
        let mut storage = SimpleStorage::new();
        let data_map: DataMap;
        let number_of_chunks: u32 = 11;
        let bytes_len = (MAX_CHUNK_SIZE as usize * number_of_chunks as usize) - 1;
        let the_bytes = random_bytes(bytes_len);
        {
            let mut se = SelfEncryptor::new(&mut storage, DataMap::None)
                .expect("First encryptor construction shouldn't fail.");

            se.write(&the_bytes, 0).expect("Writing to encryptor shouldn't fail.");
            check_file_size(&se, bytes_len as u64);
            data_map = se.close().expect("Closing encryptor shouldn't fail.");
        }
        match data_map {
            DataMap::Chunks(ref chunks) => {
                assert_eq!(chunks.len(), number_of_chunks as usize);
                assert_eq!(storage.num_entries(), number_of_chunks as usize);
................................................................................
                }
            }
            DataMap::Content(_) => panic!("shall not return DataMap::Content"),
            DataMap::None => panic!("shall not return DataMap::None"),
        }
        let mut new_se = SelfEncryptor::new(&mut storage, data_map)
            .expect("Second encryptor construction shouldn't fail.");
        let fetched =

            new_se.read(0, bytes_len as u64).expect("Reading from encryptor shouldn't fail.");
        assert!(fetched == the_bytes);
    }

    #[test]
    fn large_file_one_byte_over_eleven_chunks() {
        let mut storage = SimpleStorage::new();
        let data_map: DataMap;
        let number_of_chunks: u32 = 11;
        let bytes_len = (MAX_CHUNK_SIZE as usize * number_of_chunks as usize) + 1;
        let the_bytes = random_bytes(bytes_len);
        {
            let mut se = SelfEncryptor::new(&mut storage, DataMap::None)
                .expect("First encryptor construction shouldn't fail.");

            se.write(&the_bytes, 0).expect("Writing to encryptor shouldn't fail.");
            check_file_size(&se, bytes_len as u64);
            data_map = se.close().expect("Closing encryptor shouldn't fail.");
        }
        match data_map {
            DataMap::Chunks(ref chunks) => {
                assert_eq!(chunks.len(), number_of_chunks as usize + 1);
                assert_eq!(storage.num_entries(), number_of_chunks as usize + 1);
................................................................................
                }
            }
            DataMap::Content(_) => panic!("shall not return DataMap::Content"),
            DataMap::None => panic!("shall not return DataMap::None"),
        }
        let mut new_se = SelfEncryptor::new(&mut storage, data_map)
            .expect("Second encryptor construction shouldn't fail.");
        let fetched =

            new_se.read(0, bytes_len as u64).expect("Reading from encryptor shouldn't fail.");
        assert!(fetched == the_bytes);
    }

    #[test]
    fn large_file_size_1024_over_eleven_chunks() {
        // has been tested for 50 chunks
        let mut storage = SimpleStorage::new();
        let data_map: DataMap;
        let number_of_chunks: u32 = 11;
        let bytes_len = (MAX_CHUNK_SIZE as usize * number_of_chunks as usize) + 1024;
        let the_bytes = random_bytes(bytes_len);
        {
            let mut se = SelfEncryptor::new(&mut storage, DataMap::None)
                .expect("First encryptor construction shouldn't fail.");

            se.write(&the_bytes, 0).expect("Writing to encryptor shouldn't fail.");
            check_file_size(&se, bytes_len as u64);
            // check close
            data_map = se.close().expect("Closing encryptor shouldn't fail.");
        }
        match data_map {
            DataMap::Chunks(ref chunks) => {
                assert_eq!(chunks.len(), number_of_chunks as usize + 1);
................................................................................
            }
            DataMap::Content(_) => panic!("shall not return DataMap::Content"),
            DataMap::None => panic!("shall not return DataMap::None"),
        }
        // check read and write
        let mut new_se = SelfEncryptor::new(&mut storage, data_map)
            .expect("Second encryptor construction shouldn't fail.");
        let fetched =

            new_se.read(0, bytes_len as u64).expect("Reading from encryptor shouldn't fail.");
        assert!(fetched == the_bytes);
    }

    #[test]
    fn five_and_extend_to_seven_plus_one() {
        let mut storage = SimpleStorage::new();
        let data_map: DataMap;
        let bytes_len = MAX_CHUNK_SIZE * 5;
        let the_bytes = random_bytes(bytes_len as usize);
        {
            let mut se = SelfEncryptor::new(&mut storage, DataMap::None)
                .expect("Encryptor construction shouldn't fail.");

            se.write(&the_bytes, 0).expect("Writing to encryptor shouldn't fail.");
            check_file_size(&se, bytes_len as u64);
            se.truncate((7 * MAX_CHUNK_SIZE + 1) as u64)
                .expect("Truncating encryptor shouldn't fail.");
            check_file_size(&se, (7 * MAX_CHUNK_SIZE + 1) as u64);
            data_map = se.close().expect("Closing encryptor shouldn't fail.");
        }
        match data_map {
................................................................................
        let mut storage = SimpleStorage::new();
        let data_map: DataMap;
        let bytes_len = MAX_CHUNK_SIZE * 3;
        let bytes = random_bytes(bytes_len as usize);
        {
            let mut se = SelfEncryptor::new(&mut storage, DataMap::None)
                .expect("First encryptor construction shouldn't fail.");

            se.write(&bytes, 0).expect("Writing to encryptor shouldn't fail.");
            check_file_size(&se, bytes_len as u64);

            se.truncate(bytes_len as u64 - 24).expect("Truncating encryptor shouldn't fail.");
            check_file_size(&se, bytes_len as u64 - 24);
            data_map = se.close().expect("Closing encryptor shouldn't fail.");
        }
        assert_eq!(data_map.len(), bytes_len as u64 - 24);
        match data_map {
            DataMap::Chunks(ref chunks) => {
                assert_eq!(chunks.len(), 3);
................................................................................
                    assert!(storage.has_chunk(&chunk_detail.hash));
                }
            }
            _ => panic!("data_map should be DataMap::Chunks"),
        }
        let mut se = SelfEncryptor::new(&mut storage, data_map)
            .expect("Second encryptor construction shouldn't fail.");
        let fetched =
            se.read(0, bytes_len as u64 - 24).expect("Reading from encryptor shouldn't fail.");
        assert!(&fetched[..] == &bytes[..(bytes_len - 24) as usize]);
    }

    #[test]
    fn truncate_from_data_map() {
        let mut storage = SimpleStorage::new();
        let bytes_len = MAX_CHUNK_SIZE * 3;
        let bytes = random_bytes(bytes_len as usize);
        let data_map: DataMap;
        {
            let mut se = SelfEncryptor::new(&mut storage, DataMap::None)
                .expect("First encryptor construction shouldn't fail.");

            se.write(&bytes, 0).expect("Writing to encryptor shouldn't fail.");
            check_file_size(&se, bytes_len as u64);

            data_map = se.close().expect("Closing first encryptor shouldn't fail.");
        }
        let data_map2: DataMap;
        {
            // Start with an existing data_map.
            let mut se = SelfEncryptor::new(&mut storage, data_map)
                .expect("Second encryptor construction shouldn't fail.");

            se.truncate(bytes_len as u64 - 24).expect("Truncating encryptor shouldn't fail.");

            data_map2 = se.close().expect("Closing second encryptor shouldn't fail.");
        }
        assert_eq!(data_map2.len(), bytes_len as u64 - 24);
        match data_map2 {
            DataMap::Chunks(ref chunks) => {
                assert_eq!(chunks.len(), 3);
                // old ones + new ones
                assert_eq!(storage.num_entries(), 6);
................................................................................
                    assert!(storage.has_chunk(&chunk_detail.hash));
                }
            }
            _ => panic!("data_map should be DataMap::Chunks"),
        }
        let mut se = SelfEncryptor::new(&mut storage, data_map2)
            .expect("Third encryptor construction shouldn't fail.");
        let fetched =
            se.read(0, bytes_len as u64 - 24).expect("Reading from encryptor shouldn't fail.");
        assert!(&fetched[..] == &bytes[..(bytes_len - 24) as usize]);
    }

    #[test]
    fn truncate_from_data_map2() {
        let mut storage = SimpleStorage::new();
        let bytes_len = MAX_CHUNK_SIZE * 3;
        let bytes = random_bytes(bytes_len as usize);
        let data_map: DataMap;
        {
            let mut se = SelfEncryptor::new(&mut storage, DataMap::None)
                .expect("First encryptor construction shouldn't fail.");

            se.write(&bytes, 0).expect("Writing to encryptor shouldn't fail.");
            check_file_size(&se, bytes_len as u64);

            data_map = se.close().expect("Closing first encryptor shouldn't fail.");
        }
        let data_map2: DataMap;
        {
            // Start with an existing data_map.
            let mut se = SelfEncryptor::new(&mut storage, data_map)
                .expect("Second encryptor construction shouldn't fail.");

            se.truncate(bytes_len as u64 - 1).expect("Truncating encryptor once shouldn't fail.");
            se.truncate(bytes_len as u64)
                .expect("Truncating encryptor a second time shouldn't fail.");

            data_map2 = se.close().expect("Closing second encryptor shouldn't fail.");
        }
        assert_eq!(data_map2.len(), bytes_len as u64);
        match data_map2 {
            DataMap::Chunks(ref chunks) => {
                assert_eq!(chunks.len(), 3);
                // old ones + new ones
                assert_eq!(storage.num_entries(), 6);
................................................................................
                    assert!(storage.has_chunk(&chunk_detail.hash));
                }
            }
            _ => panic!("data_map should be DataMap::Chunks"),
        }
        let mut se = SelfEncryptor::new(&mut storage, data_map2)
            .expect("Third encryptor construction shouldn't fail.");

        let fetched = se.read(0, bytes_len as u64).expect("Reading from encryptor shouldn't fail.");
        let matching_bytes = bytes_len as usize - 1;
        assert!(&fetched[..matching_bytes] == &bytes[..matching_bytes]);
        assert_eq!(fetched[matching_bytes], 0u8);
    }

    #[test]
    fn truncate_to_extend_from_data_map() {
        let mut storage = SimpleStorage::new();
        let bytes_len = MAX_CHUNK_SIZE * 3 - 24;
        let bytes = random_bytes(bytes_len as usize);
        let data_map: DataMap;
        {
            let mut se = SelfEncryptor::new(&mut storage, DataMap::None)
                .expect("First encryptor construction shouldn't fail.");

            se.write(&bytes, 0).expect("Writing to encryptor shouldn't fail.");
            check_file_size(&se, bytes_len as u64);

            data_map = se.close().expect("Closing first encryptor shouldn't fail.");
        }
        let data_map2: DataMap;
        {
            // Start with an existing data_map.
            let mut se = SelfEncryptor::new(&mut storage, data_map)
                .expect("Second encryptor construction shouldn't fail.");

            se.truncate(bytes_len as u64 + 24).expect("Truncating encryptor shouldn't fail.");

            data_map2 = se.close().expect("Closing second encryptor shouldn't fail.");
        }
        assert_eq!(data_map2.len(), bytes_len as u64 + 24);
        match data_map2 {
            DataMap::Chunks(ref chunks) => {
                assert_eq!(chunks.len(), 3);
                // old ones + new ones
                assert_eq!(storage.num_entries(), 6);
................................................................................
                    assert!(storage.has_chunk(&chunk_detail.hash));
                }
            }
            _ => panic!("data_map should be DataMap::Chunks"),
        }
        let mut se = SelfEncryptor::new(&mut storage, data_map2)
            .expect("Third encryptor construction shouldn't fail.");
        let fetched =
            se.read(0, bytes_len as u64 + 24).expect("Reading from encryptor shouldn't fail.");
        assert!(&fetched[..bytes_len as usize] == &bytes[..]);
        assert!(&fetched[bytes_len as usize..] == &[0u8; 24]);
    }

    #[test]
    fn large_100mb_file() {
        let mut storage = SimpleStorage::new();
        let data_map: DataMap;
        let number_of_chunks: u32 = 100;
        let bytes_len = MAX_CHUNK_SIZE as usize * number_of_chunks as usize;
        let bytes = random_bytes(bytes_len);
        {
            let mut se = SelfEncryptor::new(&mut storage, DataMap::None)
                .expect("First encryptor construction shouldn't fail.");

            se.write(&bytes, 0).expect("Writing to encryptor shouldn't fail.");
            check_file_size(&se, bytes_len as u64);
            data_map = se.close().expect("Closing encryptor shouldn't fail.");
        }
        match data_map {
            DataMap::Chunks(ref chunks) => {
                assert_eq!(chunks.len(), number_of_chunks as usize);
                assert_eq!(storage.num_entries(), number_of_chunks as usize);
................................................................................
                }
            }
            DataMap::Content(_) => panic!("shall not return DataMap::Content"),
            DataMap::None => panic!("shall not return DataMap::None"),
        }
        let mut new_se = SelfEncryptor::new(&mut storage, data_map)
            .expect("Second encryptor construction shouldn't fail.");
        let fetched =

            new_se.read(0, bytes_len as u64).expect("Reading from encryptor shouldn't fail.");
        assert!(fetched == bytes);

    }

    #[test]
    fn write_starting_with_existing_data_map() {
        let mut storage = SimpleStorage::new();
        let part1_len = MIN_CHUNK_SIZE * 3;
        let part1_bytes = random_bytes(part1_len as usize);
        let data_map: DataMap;
        {
            let mut se = SelfEncryptor::new(&mut storage, DataMap::None)
                .expect("First encryptor construction shouldn't fail.");

            se.write(&part1_bytes, 0).expect("Writing part one to encryptor shouldn't fail.");
            check_file_size(&se, part1_len as u64);

            data_map = se.close().expect("Closing first encryptor shouldn't fail.");
        }
        let part2_len = 1024;
        let part2_bytes = random_bytes(part2_len as usize);
        let full_len = part1_len + part2_len;
        let data_map2: DataMap;
        {
            // Start with an existing data_map.
            let mut se = SelfEncryptor::new(&mut storage, data_map)
                .expect("Second encryptor construction shouldn't fail.");
            se.write(&part2_bytes, part1_len as u64)
                .expect("Writing part two to encryptor shouldn't fail.");
            // check_file_size(&se, full_len);

            data_map2 = se.close().expect("Closing second encryptor shouldn't fail.");
        }
        assert_eq!(data_map2.len(), full_len as u64);

        let mut se = SelfEncryptor::new(&mut storage, data_map2)
            .expect("Third encryptor construction shouldn't fail.");

        let fetched = se.read(0, full_len as u64).expect("Reading from encryptor shouldn't fail.");
        assert!(&part1_bytes[..] == &fetched[..part1_len as usize]);
        assert!(&part2_bytes[..] == &fetched[part1_len as usize..]);
    }

    #[test]
    fn write_starting_with_existing_data_map2() {
        let mut storage = SimpleStorage::new();
        let part1_len = MAX_CHUNK_SIZE * 3 - 24;
        let part1_bytes = random_bytes(part1_len as usize);
        let data_map: DataMap;
        {
            let mut se = SelfEncryptor::new(&mut storage, DataMap::None)
                .expect("First encryptor construction shouldn't fail.");

            se.write(&part1_bytes, 0).expect("Writing part one to encryptor shouldn't fail.");
            check_file_size(&se, part1_len as u64);

            data_map = se.close().expect("Closing first encryptor shouldn't fail.");
        }
        let part2_len = 1024;
        let part2_bytes = random_bytes(part2_len as usize);
        let full_len = part1_len + part2_len;
        let data_map2: DataMap;
        {
            // Start with an existing data_map.
            let mut se = SelfEncryptor::new(&mut storage, data_map)
                .expect("Second encryptor construction shouldn't fail.");
            se.write(&part2_bytes, part1_len as u64)
                .expect("Writing part two to encryptor shouldn't fail.");

            data_map2 = se.close().expect("Closing second encryptor shouldn't fail.");
        }
        assert_eq!(data_map2.len(), full_len as u64);
        match data_map2 {
            DataMap::Chunks(ref chunks) => {
                assert_eq!(chunks.len(), 4);
                // old ones + new ones
                assert_eq!(storage.num_entries(), 7);
................................................................................
                }
            }
            _ => panic!("data_map should be DataMap::Chunks"),
        }

        let mut se = SelfEncryptor::new(&mut storage, data_map2)
            .expect("Third encryptor construction shouldn't fail.");

        let fetched = se.read(0, full_len as u64).expect("Reading from encryptor shouldn't fail.");
        assert!(&part1_bytes[..] == &fetched[..part1_len as usize]);
        assert!(&part2_bytes[..] == &fetched[part1_len as usize..]);
    }

    #[test]
    fn overwrite_starting_with_existing_data_map() {
        let mut storage = SimpleStorage::new();
        let part1_len = MAX_CHUNK_SIZE * 4;
        let part1_bytes = random_bytes(part1_len as usize);
        let data_map: DataMap;
        {
            let mut se = SelfEncryptor::new(&mut storage, DataMap::None)
                .expect("First encryptor construction shouldn't fail.");

            se.write(&part1_bytes, 0).expect("Writing part one to encryptor shouldn't fail.");
            check_file_size(&se, part1_len as u64);

            data_map = se.close().expect("Closing first encryptor shouldn't fail.");
        }
        let part2_len = 2;
        let part2_bytes = random_bytes(part2_len);
        let data_map2: DataMap;
        {
            // Start with an existing data_map.
            let mut se = SelfEncryptor::new(&mut storage, data_map)
                .expect("Second encryptor construction shouldn't fail.");
            // Overwrite. This and next two chunks will have to be re-encrypted.

            se.write(&part2_bytes, 2).expect("Writing part two to encryptor shouldn't fail.");

            data_map2 = se.close().expect("Closing second encryptor shouldn't fail.");
        }
        assert_eq!(data_map2.len(), part1_len as u64);

        let mut se = SelfEncryptor::new(&mut storage, data_map2)
            .expect("Third encryptor construction shouldn't fail.");

        let fetched = se.read(0, part1_len as u64).expect("Reading from encryptor shouldn't fail.");
        assert!(&part1_bytes[..2] == &fetched[..2]);
        assert!(&part2_bytes[..] == &fetched[2..2 + part2_len]);
        assert!(&part1_bytes[2 + part2_len..] == &fetched[2 + part2_len..]);
    }

    fn create_vector_data_map(storage: &mut SimpleStorage, vec_len: usize) -> DataMap {
        let data: Vec<usize> = (0..vec_len).collect();
        let serialised_data: Vec<u8> =
            serialisation::serialise(&data).expect("failed to serialise Vec<usize>");
        let mut self_encryptor = SelfEncryptor::new(storage, DataMap::None)
            .expect("Encryptor construction shouldn't fail.");


        self_encryptor.write(&serialised_data, 0).expect("Writing to encryptor shouldn't fail.");
        check_file_size(&self_encryptor, serialised_data.len() as u64);


        self_encryptor.close().expect("Closing encryptor shouldn't fail.")
    }

    fn check_vector_data_map(storage: &mut SimpleStorage, vec_len: usize, data_map: &DataMap) {
        let mut self_encryptor = SelfEncryptor::new(storage, data_map.clone())
            .expect("Encryptor construction shouldn't fail.");
        let length = self_encryptor.len();
        let data_to_deserialise: Vec<u8> =

            self_encryptor.read(0, length).expect("Reading from encryptor shouldn't fail.");
        let data: Vec<usize> = serialisation::deserialise(&data_to_deserialise)
            .expect("failed to deserialise Vec<usize>");
        assert_eq!(data.len(), vec_len);
        for (index, data_char) in data.iter().enumerate() {
            assert_eq!(*data_char, index);
        }
    }







|
<
<
<







 







|
<
<
<












>
|







 







>
>
|







 







>
>
|







 







|
>
|







 







>
|







 







>
|
>
|








|
|
|

|
|











>
|







 







|
>
|
|










>
|



|







 







>
|

|











>
|







 







|
>
|
|











>
|







 







|
>
|
|











>
|







 







|
>
|
|











>
|







 







|
>
|
|












>
|







 







|
>
|
|












>
|







 







|
>
|
|













>
|







 







|
>
|
|











>
|







 







>
|

>
|







 







|
|
|











>
|

>
|






>
|
>
|







 







|
|
|











>
|

>
|






>
|


>
|







 







>
|

|












>
|

>
|






>
|
>
|







 







|
|
|
|












>
|







 







|
>
|
<
>











>
|

>
|












>
|





>
|
|
|











>
|

>
|











>
|







 







>
|
|
|











>
|

>
|









>
|
>
|





>
|
|
|
|








>
>
|

>
>
|






|
>
|







304
305
306
307
308
309
310
311



312
313
314
315
316
317
318
...
342
343
344
345
346
347
348
349



350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
...
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
...
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
...
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
...
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
...
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
...
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
...
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
...
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
...
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
...
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
....
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
....
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
....
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
....
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
....
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
....
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
....
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
....
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
....
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
....
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377

1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
....
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
        for &i in [chunks_start, chunks_end - 1].iter().chain(&next_two) {
            if self.chunks[i].in_sequencer {
                continue;
            }
            self.chunks[i].in_sequencer = true;
            let pos = get_start_end_positions(self.map_size, i as u32).0 as usize;
            let vec = self.decrypt_chunk(i as u32)?;
            for (p, byte) in self.sequencer.iter_mut().skip(pos).zip(vec) {



                *p = byte;
            }
        }
        for chunk in &mut self.chunks[chunks_start..chunks_end] {
            chunk.status = ChunkStatus::ToBeHashed;
            chunk.in_sequencer = true;
        }
................................................................................
        for i in chunks_start..chunks_end {
            if self.chunks[i].in_sequencer {
                continue;
            }
            self.chunks[i].in_sequencer = true;
            let pos = get_start_end_positions(self.map_size, i as u32).0 as usize;
            let vec = self.decrypt_chunk(i as u32)?;
            for (p, byte) in self.sequencer.iter_mut().skip(pos).zip(vec) {



                *p = byte
            }
        }
        Ok(())
    }

    fn extend_sequencer_up_to(&mut self, new_len: u64) -> Result<(), SelfEncryptionError<E>> {
        let old_len = self.sequencer.len() as u64;
        if new_len > old_len {
            if new_len > MAX_IN_MEMORY_SIZE as u64 {
                self.sequencer.create_mapping()?;
            } else {
                self.sequencer
                    .extend(iter::repeat(0).take((new_len - old_len) as usize));
            }
        }
        Ok(())
    }

    fn decrypt_chunk(&self, chunk_number: u32) -> Result<Vec<u8>, SelfEncryptionError<E>> {
        let name = &self.sorted_map[chunk_number as usize].hash;
................................................................................
        let (pad, key, iv) = get_pad_key_and_iv(chunk_number, &self.sorted_map, self.map_size);
        let xor_result = xor(&content, &pad);
        let decrypted = encryption::decrypt(&xor_result, &key, &iv)?;
        let mut decompressor = BrotliDecoder::new(vec![]);
        if decompressor.write_all(&decrypted).is_err() {
            return Err(SelfEncryptionError::Compression);
        }
        decompressor
            .finish()
            .map_err(|_| SelfEncryptionError::Compression)
    }
}

fn encrypt_chunk<E: StorageError>(content: &[u8],
                                  pki: (Pad, Key, Iv))
                                  -> Result<Vec<u8>, SelfEncryptionError<E>> {
    let (pad, key, iv) = pki;
................................................................................
    let n_2_pre_hash = &sorted_map[n_2 as usize].pre_hash;

    let mut pad = [0u8; PAD_SIZE];
    let mut key = [0u8; KEY_SIZE];
    let mut iv = [0u8; IV_SIZE];

    for (pad_iv_el, element) in
        pad.iter_mut()
            .chain(iv.iter_mut())
            .zip(this_pre_hash.iter().chain(n_2_pre_hash.iter())) {
        *pad_iv_el = *element;
    }

    for (key_el, element) in key.iter_mut().zip(n_1_pre_hash.iter()) {
        *key_el = *element;
    }

................................................................................
                   ((number_of_chunks * MAX_CHUNK_SIZE) as u64));
    }

    fn check_file_size<E: StorageError, S: Storage<E>>(se: &SelfEncryptor<E, S>,
                                                       expected_file_size: u64) {
        assert_eq!(se.file_size, expected_file_size);
        if !se.sorted_map.is_empty() {
            let chunks_cumulated_size = se.sorted_map
                .iter()
                .fold(0u64, |acc, chunk| acc + chunk.source_size);
            assert_eq!(chunks_cumulated_size, expected_file_size);
        }
    }

    #[test]
    fn xor() {
        let mut data: Vec<u8> = vec![];
................................................................................
    fn write() {
        let mut storage = SimpleStorage::new();
        let mut se = SelfEncryptor::new(&mut storage, DataMap::None)
            .expect("Encryptor construction shouldn't fail.");
        let size = 3;
        let offset = 5u32;
        let the_bytes = random_bytes(size);
        se.write(&the_bytes, offset as u64)
            .expect("Writing to encryptor shouldn't fail.");
        check_file_size(&se, (size + offset as usize) as u64);
    }

    #[test]
    fn multiple_writes() {
        let mut storage = SimpleStorage::new();
        let size1 = 3;
................................................................................
        let part1 = random_bytes(size1);
        let part2 = random_bytes(size2);
        let data_map;
        {
            let mut se = SelfEncryptor::new(&mut storage, DataMap::None)
                .expect("First encryptor construction shouldn't fail.");
            // Just testing multiple subsequent write calls
            se.write(&part1, 0)
                .expect("Writing part one to encryptor shouldn't fail.");
            se.write(&part2, size1 as u64)
                .expect("Writing part two to encryptor shouldn't fail.");
            // Let's also test an overwrite.. over middle bytes of part2
            se.write(&[4u8, 2], size1 as u64 + 1)
                .expect("Overwriting on encryptor shouldn't fail.");
            check_file_size(&se, (size1 + size2) as u64);
            data_map = se.close().expect("Closing encryptor shouldn't fail.");
        }
        let mut se = SelfEncryptor::new(&mut storage, data_map)
            .expect("Second encryptor construction shouldn't fail.");
        let fetched = se.read(0, (size1 + size2) as u64)
            .expect("Reading from encryptor shouldn't fail.");
        assert_eq!(&fetched[..size1], &part1[..]);
        assert_eq!(fetched[size1], part2[0]);
        assert_eq!(&fetched[size1 + 1..size1 + 3], &[4u8, 2][..]);
        assert_eq!(&fetched[size1 + 3..], &part2[3..]);
    }

    #[test]
    fn three_min_chunks_minus_one() {
        let mut storage = SimpleStorage::new();
        let data_map: DataMap;
        let bytes_len = (MIN_CHUNK_SIZE * 3) - 1;
        let the_bytes = random_bytes(bytes_len as usize);
        {
            let mut se = SelfEncryptor::new(&mut storage, DataMap::None)
                .expect("First encryptor construction shouldn't fail.");
            se.write(&the_bytes, 0)
                .expect("Writing to encryptor shouldn't fail.");
            assert_eq!(se.sorted_map.len(), 0);
            assert_eq!(se.sequencer.len(), bytes_len as usize);
            check_file_size(&se, bytes_len as u64);
            // check close
            data_map = se.close().expect("Closing encryptor shouldn't fail.");
        }
        match data_map {
................................................................................
            DataMap::Chunks(_) => panic!("shall not return DataMap::Chunks"),
            DataMap::Content(ref content) => assert_eq!(content.len(), bytes_len as usize),
            DataMap::None => panic!("shall not return DataMap::None"),
        }
        // check read, write
        let mut new_se = SelfEncryptor::new(&mut storage, data_map)
            .expect("Second encryptor construction shouldn't fail.");
        let fetched = new_se
            .read(0, bytes_len as u64)
            .expect("Reading from encryptor shouldn't fail.");
        assert_eq!(fetched, the_bytes);
    }

    #[test]
    fn three_min_chunks() {
        let mut storage = SimpleStorage::new();
        let data_map: DataMap;
        let the_bytes = random_bytes(MIN_CHUNK_SIZE as usize * 3);
        {
            let mut se = SelfEncryptor::new(&mut storage, DataMap::None)
                .expect("First encryptor construction shouldn't fail.");
            se.write(&the_bytes, 0)
                .expect("Writing to encryptor shouldn't fail.");
            check_file_size(&se, MIN_CHUNK_SIZE as u64 * 3);
            let fetched = se.read(0, MIN_CHUNK_SIZE as u64 * 3)
                .expect("Reading from encryptor shouldn't fail.");
            assert_eq!(fetched, the_bytes);
            data_map = se.close().expect("Closing encryptor shouldn't fail.");
        }
        match data_map {
            DataMap::Chunks(ref chunks) => {
                assert_eq!(chunks.len(), 3);
                assert_eq!(storage.num_entries(), 3);
                for chunk_detail in chunks.iter() {
................................................................................
            }
            DataMap::Content(_) => panic!("shall not return DataMap::Content"),
            DataMap::None => panic!("shall not return DataMap::None"),
        }
        // check read, write
        let mut new_se = SelfEncryptor::new(&mut storage, data_map)
            .expect("Second encryptor construction shouldn't fail.");
        let fetched = new_se
            .read(0, MIN_CHUNK_SIZE as u64 * 3)
            .expect("Reading again from encryptor shouldn't fail.");
        assert_eq!(fetched, the_bytes);
    }

    #[test]
    fn three_min_chunks_plus_one() {
        let mut storage = SimpleStorage::new();
        let data_map: DataMap;
        let bytes_len = (MIN_CHUNK_SIZE * 3) + 1;
        let the_bytes = random_bytes(bytes_len as usize);
        {
            let mut se = SelfEncryptor::new(&mut storage, DataMap::None)
                .expect("First encryptor construction shouldn't fail.");
            se.write(&the_bytes, 0)
                .expect("Writing to encryptor shouldn't fail.");
            check_file_size(&se, bytes_len as u64);
            data_map = se.close().expect("Closing encryptor shouldn't fail.");
        }
        match data_map {
            DataMap::Chunks(ref chunks) => {
                assert_eq!(chunks.len(), 3);
                assert_eq!(storage.num_entries(), 3);
................................................................................
                }
            }
            DataMap::Content(_) => panic!("shall not return DataMap::Content"),
            DataMap::None => panic!("shall not return DataMap::None"),
        }
        let mut new_se = SelfEncryptor::new(&mut storage, data_map)
            .expect("Second encryptor construction shouldn't fail.");
        let fetched = new_se
            .read(0, bytes_len as u64)
            .expect("Reading from encryptor shouldn't fail.");
        assert_eq!(fetched, the_bytes);
    }

    #[test]
    fn three_max_chunks() {
        let mut storage = SimpleStorage::new();
        let data_map: DataMap;
        let bytes_len = MAX_CHUNK_SIZE * 3;
        let the_bytes = random_bytes(bytes_len as usize);
        {
            let mut se = SelfEncryptor::new(&mut storage, DataMap::None)
                .expect("First encryptor construction shouldn't fail.");
            se.write(&the_bytes, 0)
                .expect("Writing to encryptor shouldn't fail.");
            check_file_size(&se, bytes_len as u64);
            data_map = se.close().expect("Closing encryptor shouldn't fail.");
        }
        match data_map {
            DataMap::Chunks(ref chunks) => {
                assert_eq!(chunks.len(), 3);
                assert_eq!(storage.num_entries(), 3);
................................................................................
                }
            }
            DataMap::Content(_) => panic!("shall not return DataMap::Content"),
            DataMap::None => panic!("shall not return DataMap::None"),
        }
        let mut new_se = SelfEncryptor::new(&mut storage, data_map)
            .expect("Second encryptor construction shouldn't fail.");
        let fetched = new_se
            .read(0, bytes_len as u64)
            .expect("Reading from encryptor shouldn't fail.");
        assert_eq!(fetched, the_bytes);
    }

    #[test]
    fn three_max_chunks_plus_one() {
        let mut storage = SimpleStorage::new();
        let data_map: DataMap;
        let bytes_len = (MAX_CHUNK_SIZE * 3) + 1;
        let the_bytes = random_bytes(bytes_len as usize);
        {
            let mut se = SelfEncryptor::new(&mut storage, DataMap::None)
                .expect("First encryptor construction shouldn't fail.");
            se.write(&the_bytes, 0)
                .expect("Writing to encryptor shouldn't fail.");
            check_file_size(&se, bytes_len as u64);
            // check close
            data_map = se.close().expect("Closing encryptor shouldn't fail.");
        }
        match data_map {
            DataMap::Chunks(ref chunks) => {
                assert_eq!(chunks.len(), 4);
................................................................................
            }
            DataMap::Content(_) => panic!("shall not return DataMap::Content"),
            DataMap::None => panic!("shall not return DataMap::None"),
        }
        // check read and write
        let mut new_se = SelfEncryptor::new(&mut storage, data_map)
            .expect("Second encryptor construction shouldn't fail.");
        let fetched = new_se
            .read(0, bytes_len as u64)
            .expect("Reading from encryptor shouldn't fail.");
        assert_eq!(fetched, the_bytes);
    }

    #[test]
    fn seven_and_a_bit_max_chunks() {
        let mut storage = SimpleStorage::new();
        let data_map: DataMap;
        let bytes_len = (MAX_CHUNK_SIZE * 7) + 1024;
        let the_bytes = random_bytes(bytes_len as usize);
        {
            let mut se = SelfEncryptor::new(&mut storage, DataMap::None)
                .expect("First encryptor construction shouldn't fail.");
            se.write(&the_bytes, 0)
                .expect("Writing to encryptor shouldn't fail.");
            check_file_size(&se, bytes_len as u64);
            data_map = se.close().expect("Closing encryptor shouldn't fail.");
        }
        match data_map {
            DataMap::Chunks(ref chunks) => {
                assert_eq!(chunks.len(), 8);
                assert_eq!(storage.num_entries(), 8);
................................................................................
                }
            }
            DataMap::Content(_) => panic!("shall not return DataMap::Content"),
            DataMap::None => panic!("shall not return DataMap::None"),
        }
        let mut new_se = SelfEncryptor::new(&mut storage, data_map)
            .expect("Second encryptor construction shouldn't fail.");
        let fetched = new_se
            .read(0, bytes_len as u64)
            .expect("Reading from encryptor shouldn't fail.");
        assert_eq!(fetched, the_bytes);
    }

    #[test]
    fn large_file_one_byte_under_eleven_chunks() {
        let mut storage = SimpleStorage::new();
        let data_map: DataMap;
        let number_of_chunks: u32 = 11;
        let bytes_len = (MAX_CHUNK_SIZE as usize * number_of_chunks as usize) - 1;
        let the_bytes = random_bytes(bytes_len);
        {
            let mut se = SelfEncryptor::new(&mut storage, DataMap::None)
                .expect("First encryptor construction shouldn't fail.");
            se.write(&the_bytes, 0)
                .expect("Writing to encryptor shouldn't fail.");
            check_file_size(&se, bytes_len as u64);
            data_map = se.close().expect("Closing encryptor shouldn't fail.");
        }
        match data_map {
            DataMap::Chunks(ref chunks) => {
                assert_eq!(chunks.len(), number_of_chunks as usize);
                assert_eq!(storage.num_entries(), number_of_chunks as usize);
................................................................................
                }
            }
            DataMap::Content(_) => panic!("shall not return DataMap::Content"),
            DataMap::None => panic!("shall not return DataMap::None"),
        }
        let mut new_se = SelfEncryptor::new(&mut storage, data_map)
            .expect("Second encryptor construction shouldn't fail.");
        let fetched = new_se
            .read(0, bytes_len as u64)
            .expect("Reading from encryptor shouldn't fail.");
        assert_eq!(fetched, the_bytes);
    }

    #[test]
    fn large_file_one_byte_over_eleven_chunks() {
        let mut storage = SimpleStorage::new();
        let data_map: DataMap;
        let number_of_chunks: u32 = 11;
        let bytes_len = (MAX_CHUNK_SIZE as usize * number_of_chunks as usize) + 1;
        let the_bytes = random_bytes(bytes_len);
        {
            let mut se = SelfEncryptor::new(&mut storage, DataMap::None)
                .expect("First encryptor construction shouldn't fail.");
            se.write(&the_bytes, 0)
                .expect("Writing to encryptor shouldn't fail.");
            check_file_size(&se, bytes_len as u64);
            data_map = se.close().expect("Closing encryptor shouldn't fail.");
        }
        match data_map {
            DataMap::Chunks(ref chunks) => {
                assert_eq!(chunks.len(), number_of_chunks as usize + 1);
                assert_eq!(storage.num_entries(), number_of_chunks as usize + 1);
................................................................................
                }
            }
            DataMap::Content(_) => panic!("shall not return DataMap::Content"),
            DataMap::None => panic!("shall not return DataMap::None"),
        }
        let mut new_se = SelfEncryptor::new(&mut storage, data_map)
            .expect("Second encryptor construction shouldn't fail.");
        let fetched = new_se
            .read(0, bytes_len as u64)
            .expect("Reading from encryptor shouldn't fail.");
        assert_eq!(fetched, the_bytes);
    }

    #[test]
    fn large_file_size_1024_over_eleven_chunks() {
        // has been tested for 50 chunks
        let mut storage = SimpleStorage::new();
        let data_map: DataMap;
        let number_of_chunks: u32 = 11;
        let bytes_len = (MAX_CHUNK_SIZE as usize * number_of_chunks as usize) + 1024;
        let the_bytes = random_bytes(bytes_len);
        {
            let mut se = SelfEncryptor::new(&mut storage, DataMap::None)
                .expect("First encryptor construction shouldn't fail.");
            se.write(&the_bytes, 0)
                .expect("Writing to encryptor shouldn't fail.");
            check_file_size(&se, bytes_len as u64);
            // check close
            data_map = se.close().expect("Closing encryptor shouldn't fail.");
        }
        match data_map {
            DataMap::Chunks(ref chunks) => {
                assert_eq!(chunks.len(), number_of_chunks as usize + 1);
................................................................................
            }
            DataMap::Content(_) => panic!("shall not return DataMap::Content"),
            DataMap::None => panic!("shall not return DataMap::None"),
        }
        // check read and write
        let mut new_se = SelfEncryptor::new(&mut storage, data_map)
            .expect("Second encryptor construction shouldn't fail.");
        let fetched = new_se
            .read(0, bytes_len as u64)
            .expect("Reading from encryptor shouldn't fail.");
        assert_eq!(fetched, the_bytes);
    }

    #[test]
    fn five_and_extend_to_seven_plus_one() {
        let mut storage = SimpleStorage::new();
        let data_map: DataMap;
        let bytes_len = MAX_CHUNK_SIZE * 5;
        let the_bytes = random_bytes(bytes_len as usize);
        {
            let mut se = SelfEncryptor::new(&mut storage, DataMap::None)
                .expect("Encryptor construction shouldn't fail.");
            se.write(&the_bytes, 0)
                .expect("Writing to encryptor shouldn't fail.");
            check_file_size(&se, bytes_len as u64);
            se.truncate((7 * MAX_CHUNK_SIZE + 1) as u64)
                .expect("Truncating encryptor shouldn't fail.");
            check_file_size(&se, (7 * MAX_CHUNK_SIZE + 1) as u64);
            data_map = se.close().expect("Closing encryptor shouldn't fail.");
        }
        match data_map {
................................................................................
        let mut storage = SimpleStorage::new();
        let data_map: DataMap;
        let bytes_len = MAX_CHUNK_SIZE * 3;
        let bytes = random_bytes(bytes_len as usize);
        {
            let mut se = SelfEncryptor::new(&mut storage, DataMap::None)
                .expect("First encryptor construction shouldn't fail.");
            se.write(&bytes, 0)
                .expect("Writing to encryptor shouldn't fail.");
            check_file_size(&se, bytes_len as u64);
            se.truncate(bytes_len as u64 - 24)
                .expect("Truncating encryptor shouldn't fail.");
            check_file_size(&se, bytes_len as u64 - 24);
            data_map = se.close().expect("Closing encryptor shouldn't fail.");
        }
        assert_eq!(data_map.len(), bytes_len as u64 - 24);
        match data_map {
            DataMap::Chunks(ref chunks) => {
                assert_eq!(chunks.len(), 3);
................................................................................
                    assert!(storage.has_chunk(&chunk_detail.hash));
                }
            }
            _ => panic!("data_map should be DataMap::Chunks"),
        }
        let mut se = SelfEncryptor::new(&mut storage, data_map)
            .expect("Second encryptor construction shouldn't fail.");
        let fetched = se.read(0, bytes_len as u64 - 24)
            .expect("Reading from encryptor shouldn't fail.");
        assert_eq!(&fetched[..], &bytes[..(bytes_len - 24) as usize]);
    }

    #[test]
    fn truncate_from_data_map() {
        let mut storage = SimpleStorage::new();
        let bytes_len = MAX_CHUNK_SIZE * 3;
        let bytes = random_bytes(bytes_len as usize);
        let data_map: DataMap;
        {
            let mut se = SelfEncryptor::new(&mut storage, DataMap::None)
                .expect("First encryptor construction shouldn't fail.");
            se.write(&bytes, 0)
                .expect("Writing to encryptor shouldn't fail.");
            check_file_size(&se, bytes_len as u64);
            data_map = se.close()
                .expect("Closing first encryptor shouldn't fail.");
        }
        let data_map2: DataMap;
        {
            // Start with an existing data_map.
            let mut se = SelfEncryptor::new(&mut storage, data_map)
                .expect("Second encryptor construction shouldn't fail.");
            se.truncate(bytes_len as u64 - 24)
                .expect("Truncating encryptor shouldn't fail.");
            data_map2 = se.close()
                .expect("Closing second encryptor shouldn't fail.");
        }
        assert_eq!(data_map2.len(), bytes_len as u64 - 24);
        match data_map2 {
            DataMap::Chunks(ref chunks) => {
                assert_eq!(chunks.len(), 3);
                // old ones + new ones
                assert_eq!(storage.num_entries(), 6);
................................................................................
                    assert!(storage.has_chunk(&chunk_detail.hash));
                }
            }
            _ => panic!("data_map should be DataMap::Chunks"),
        }
        let mut se = SelfEncryptor::new(&mut storage, data_map2)
            .expect("Third encryptor construction shouldn't fail.");
        let fetched = se.read(0, bytes_len as u64 - 24)
            .expect("Reading from encryptor shouldn't fail.");
        assert_eq!(&fetched[..], &bytes[..(bytes_len - 24) as usize]);
    }

    #[test]
    fn truncate_from_data_map2() {
        let mut storage = SimpleStorage::new();
        let bytes_len = MAX_CHUNK_SIZE * 3;
        let bytes = random_bytes(bytes_len as usize);
        let data_map: DataMap;
        {
            let mut se = SelfEncryptor::new(&mut storage, DataMap::None)
                .expect("First encryptor construction shouldn't fail.");
            se.write(&bytes, 0)
                .expect("Writing to encryptor shouldn't fail.");
            check_file_size(&se, bytes_len as u64);
            data_map = se.close()
                .expect("Closing first encryptor shouldn't fail.");
        }
        let data_map2: DataMap;
        {
            // Start with an existing data_map.
            let mut se = SelfEncryptor::new(&mut storage, data_map)
                .expect("Second encryptor construction shouldn't fail.");
            se.truncate(bytes_len as u64 - 1)
                .expect("Truncating encryptor once shouldn't fail.");
            se.truncate(bytes_len as u64)
                .expect("Truncating encryptor a second time shouldn't fail.");
            data_map2 = se.close()
                .expect("Closing second encryptor shouldn't fail.");
        }
        assert_eq!(data_map2.len(), bytes_len as u64);
        match data_map2 {
            DataMap::Chunks(ref chunks) => {
                assert_eq!(chunks.len(), 3);
                // old ones + new ones
                assert_eq!(storage.num_entries(), 6);
................................................................................
                    assert!(storage.has_chunk(&chunk_detail.hash));
                }
            }
            _ => panic!("data_map should be DataMap::Chunks"),
        }
        let mut se = SelfEncryptor::new(&mut storage, data_map2)
            .expect("Third encryptor construction shouldn't fail.");
        let fetched = se.read(0, bytes_len as u64)
            .expect("Reading from encryptor shouldn't fail.");
        let matching_bytes = bytes_len as usize - 1;
        assert_eq!(&fetched[..matching_bytes], &bytes[..matching_bytes]);
        assert_eq!(fetched[matching_bytes], 0u8);
    }

    #[test]
    fn truncate_to_extend_from_data_map() {
        let mut storage = SimpleStorage::new();
        let bytes_len = MAX_CHUNK_SIZE * 3 - 24;
        let bytes = random_bytes(bytes_len as usize);
        let data_map: DataMap;
        {
            let mut se = SelfEncryptor::new(&mut storage, DataMap::None)
                .expect("First encryptor construction shouldn't fail.");
            se.write(&bytes, 0)
                .expect("Writing to encryptor shouldn't fail.");
            check_file_size(&se, bytes_len as u64);
            data_map = se.close()
                .expect("Closing first encryptor shouldn't fail.");
        }
        let data_map2: DataMap;
        {
            // Start with an existing data_map.
            let mut se = SelfEncryptor::new(&mut storage, data_map)
                .expect("Second encryptor construction shouldn't fail.");
            se.truncate(bytes_len as u64 + 24)
                .expect("Truncating encryptor shouldn't fail.");
            data_map2 = se.close()
                .expect("Closing second encryptor shouldn't fail.");
        }
        assert_eq!(data_map2.len(), bytes_len as u64 + 24);
        match data_map2 {
            DataMap::Chunks(ref chunks) => {
                assert_eq!(chunks.len(), 3);
                // old ones + new ones
                assert_eq!(storage.num_entries(), 6);
................................................................................
                    assert!(storage.has_chunk(&chunk_detail.hash));
                }
            }
            _ => panic!("data_map should be DataMap::Chunks"),
        }
        let mut se = SelfEncryptor::new(&mut storage, data_map2)
            .expect("Third encryptor construction shouldn't fail.");
        let fetched = se.read(0, bytes_len as u64 + 24)
            .expect("Reading from encryptor shouldn't fail.");
        assert_eq!(&fetched[..bytes_len as usize], &bytes[..]);
        assert_eq!(&fetched[bytes_len as usize..], &[0u8; 24]);
    }

    #[test]
    fn large_100mb_file() {
        let mut storage = SimpleStorage::new();
        let data_map: DataMap;
        let number_of_chunks: u32 = 100;
        let bytes_len = MAX_CHUNK_SIZE as usize * number_of_chunks as usize;
        let bytes = random_bytes(bytes_len);
        {
            let mut se = SelfEncryptor::new(&mut storage, DataMap::None)
                .expect("First encryptor construction shouldn't fail.");
            se.write(&bytes, 0)
                .expect("Writing to encryptor shouldn't fail.");
            check_file_size(&se, bytes_len as u64);
            data_map = se.close().expect("Closing encryptor shouldn't fail.");
        }
        match data_map {
            DataMap::Chunks(ref chunks) => {
                assert_eq!(chunks.len(), number_of_chunks as usize);
                assert_eq!(storage.num_entries(), number_of_chunks as usize);
................................................................................
                }
            }
            DataMap::Content(_) => panic!("shall not return DataMap::Content"),
            DataMap::None => panic!("shall not return DataMap::None"),
        }
        let mut new_se = SelfEncryptor::new(&mut storage, data_map)
            .expect("Second encryptor construction shouldn't fail.");
        let fetched = new_se
            .read(0, bytes_len as u64)
            .expect("Reading from encryptor shouldn't fail.");

        assert_eq!(fetched, bytes);
    }

    #[test]
    fn write_starting_with_existing_data_map() {
        let mut storage = SimpleStorage::new();
        let part1_len = MIN_CHUNK_SIZE * 3;
        let part1_bytes = random_bytes(part1_len as usize);
        let data_map: DataMap;
        {
            let mut se = SelfEncryptor::new(&mut storage, DataMap::None)
                .expect("First encryptor construction shouldn't fail.");
            se.write(&part1_bytes, 0)
                .expect("Writing part one to encryptor shouldn't fail.");
            check_file_size(&se, part1_len as u64);
            data_map = se.close()
                .expect("Closing first encryptor shouldn't fail.");
        }
        let part2_len = 1024;
        let part2_bytes = random_bytes(part2_len as usize);
        let full_len = part1_len + part2_len;
        let data_map2: DataMap;
        {
            // Start with an existing data_map.
            let mut se = SelfEncryptor::new(&mut storage, data_map)
                .expect("Second encryptor construction shouldn't fail.");
            se.write(&part2_bytes, part1_len as u64)
                .expect("Writing part two to encryptor shouldn't fail.");
            // check_file_size(&se, full_len);
            data_map2 = se.close()
                .expect("Closing second encryptor shouldn't fail.");
        }
        assert_eq!(data_map2.len(), full_len as u64);

        let mut se = SelfEncryptor::new(&mut storage, data_map2)
            .expect("Third encryptor construction shouldn't fail.");
        let fetched = se.read(0, full_len as u64)
            .expect("Reading from encryptor shouldn't fail.");
        assert_eq!(&part1_bytes[..], &fetched[..part1_len as usize]);
        assert_eq!(&part2_bytes[..], &fetched[part1_len as usize..]);
    }

    #[test]
    fn write_starting_with_existing_data_map2() {
        let mut storage = SimpleStorage::new();
        let part1_len = MAX_CHUNK_SIZE * 3 - 24;
        let part1_bytes = random_bytes(part1_len as usize);
        let data_map: DataMap;
        {
            let mut se = SelfEncryptor::new(&mut storage, DataMap::None)
                .expect("First encryptor construction shouldn't fail.");
            se.write(&part1_bytes, 0)
                .expect("Writing part one to encryptor shouldn't fail.");
            check_file_size(&se, part1_len as u64);
            data_map = se.close()
                .expect("Closing first encryptor shouldn't fail.");
        }
        let part2_len = 1024;
        let part2_bytes = random_bytes(part2_len as usize);
        let full_len = part1_len + part2_len;
        let data_map2: DataMap;
        {
            // Start with an existing data_map.
            let mut se = SelfEncryptor::new(&mut storage, data_map)
                .expect("Second encryptor construction shouldn't fail.");
            se.write(&part2_bytes, part1_len as u64)
                .expect("Writing part two to encryptor shouldn't fail.");
            data_map2 = se.close()
                .expect("Closing second encryptor shouldn't fail.");
        }
        assert_eq!(data_map2.len(), full_len as u64);
        match data_map2 {
            DataMap::Chunks(ref chunks) => {
                assert_eq!(chunks.len(), 4);
                // old ones + new ones
                assert_eq!(storage.num_entries(), 7);
................................................................................
                }
            }
            _ => panic!("data_map should be DataMap::Chunks"),
        }

        let mut se = SelfEncryptor::new(&mut storage, data_map2)
            .expect("Third encryptor construction shouldn't fail.");
        let fetched = se.read(0, full_len as u64)
            .expect("Reading from encryptor shouldn't fail.");
        assert_eq!(&part1_bytes[..], &fetched[..part1_len as usize]);
        assert_eq!(&part2_bytes[..], &fetched[part1_len as usize..]);
    }

    #[test]
    fn overwrite_starting_with_existing_data_map() {
        let mut storage = SimpleStorage::new();
        let part1_len = MAX_CHUNK_SIZE * 4;
        let part1_bytes = random_bytes(part1_len as usize);
        let data_map: DataMap;
        {
            let mut se = SelfEncryptor::new(&mut storage, DataMap::None)
                .expect("First encryptor construction shouldn't fail.");
            se.write(&part1_bytes, 0)
                .expect("Writing part one to encryptor shouldn't fail.");
            check_file_size(&se, part1_len as u64);
            data_map = se.close()
                .expect("Closing first encryptor shouldn't fail.");
        }
        let part2_len = 2;
        let part2_bytes = random_bytes(part2_len);
        let data_map2: DataMap;
        {
            // Start with an existing data_map.
            let mut se = SelfEncryptor::new(&mut storage, data_map)
                .expect("Second encryptor construction shouldn't fail.");
            // Overwrite. This and next two chunks will have to be re-encrypted.
            se.write(&part2_bytes, 2)
                .expect("Writing part two to encryptor shouldn't fail.");
            data_map2 = se.close()
                .expect("Closing second encryptor shouldn't fail.");
        }
        assert_eq!(data_map2.len(), part1_len as u64);

        let mut se = SelfEncryptor::new(&mut storage, data_map2)
            .expect("Third encryptor construction shouldn't fail.");
        let fetched = se.read(0, part1_len as u64)
            .expect("Reading from encryptor shouldn't fail.");
        assert_eq!(&part1_bytes[..2], &fetched[..2]);
        assert_eq!(&part2_bytes[..], &fetched[2..2 + part2_len]);
        assert_eq!(&part1_bytes[2 + part2_len..], &fetched[2 + part2_len..]);
    }

    fn create_vector_data_map(storage: &mut SimpleStorage, vec_len: usize) -> DataMap {
        let data: Vec<usize> = (0..vec_len).collect();
        let serialised_data: Vec<u8> =
            serialisation::serialise(&data).expect("failed to serialise Vec<usize>");
        let mut self_encryptor = SelfEncryptor::new(storage, DataMap::None)
            .expect("Encryptor construction shouldn't fail.");
        self_encryptor
            .write(&serialised_data, 0)
            .expect("Writing to encryptor shouldn't fail.");
        check_file_size(&self_encryptor, serialised_data.len() as u64);
        self_encryptor
            .close()
            .expect("Closing encryptor shouldn't fail.")
    }

    fn check_vector_data_map(storage: &mut SimpleStorage, vec_len: usize, data_map: &DataMap) {
        let mut self_encryptor = SelfEncryptor::new(storage, data_map.clone())
            .expect("Encryptor construction shouldn't fail.");
        let length = self_encryptor.len();
        let data_to_deserialise: Vec<u8> = self_encryptor
            .read(0, length)
            .expect("Reading from encryptor shouldn't fail.");
        let data: Vec<usize> = serialisation::deserialise(&data_to_deserialise)
            .expect("failed to deserialise Vec<usize>");
        assert_eq!(data.len(), vec_len);
        for (index, data_char) in data.iter().enumerate() {
            assert_eq!(*data_char, index);
        }
    }