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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
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
use alloc::string::{String, ToString};
use core::fmt::Display;
use spacepackets::cfdp::ChecksumType;
use spacepackets::ByteConversionError;
#[cfg(feature = "std")]
use std::error::Error;
use std::path::Path;
#[cfg(feature = "std")]
pub use std_mod::*;

#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[non_exhaustive]
pub enum FilestoreError {
    FileDoesNotExist,
    FileAlreadyExists,
    DirDoesNotExist,
    Permission,
    IsNotFile,
    IsNotDirectory,
    ByteConversion(ByteConversionError),
    Io {
        raw_errno: Option<i32>,
        string: String,
    },
    ChecksumTypeNotImplemented(ChecksumType),
    Utf8Error,
    Other,
}

impl From<ByteConversionError> for FilestoreError {
    fn from(value: ByteConversionError) -> Self {
        Self::ByteConversion(value)
    }
}

impl Display for FilestoreError {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        match self {
            FilestoreError::FileDoesNotExist => {
                write!(f, "file does not exist")
            }
            FilestoreError::FileAlreadyExists => {
                write!(f, "file already exists")
            }
            FilestoreError::DirDoesNotExist => {
                write!(f, "directory does not exist")
            }
            FilestoreError::Permission => {
                write!(f, "permission error")
            }
            FilestoreError::IsNotFile => {
                write!(f, "is not a file")
            }
            FilestoreError::IsNotDirectory => {
                write!(f, "is not a directory")
            }
            FilestoreError::ByteConversion(e) => {
                write!(f, "filestore error: {e}")
            }
            FilestoreError::Io { raw_errno, string } => {
                write!(
                    f,
                    "filestore generic IO error with raw errno {:?}: {}",
                    raw_errno, string
                )
            }
            FilestoreError::ChecksumTypeNotImplemented(checksum_type) => {
                write!(f, "checksum {:?} not implemented", checksum_type)
            }
            FilestoreError::Utf8Error => {
                write!(f, "utf8 error")
            }
            FilestoreError::Other => {
                write!(f, "some filestore error occured")
            }
        }
    }
}

impl Error for FilestoreError {
    fn source(&self) -> Option<&(dyn Error + 'static)> {
        match self {
            FilestoreError::ByteConversion(e) => Some(e),
            _ => None,
        }
    }
}

#[cfg(feature = "std")]
impl From<std::io::Error> for FilestoreError {
    fn from(value: std::io::Error) -> Self {
        Self::Io {
            raw_errno: value.raw_os_error(),
            string: value.to_string(),
        }
    }
}

pub trait VirtualFilestore {
    fn create_file(&self, file_path: &str) -> Result<(), FilestoreError>;

    fn remove_file(&self, file_path: &str) -> Result<(), FilestoreError>;

    /// Truncating a file means deleting all its data so the resulting file is empty.
    /// This can be more efficient than removing and re-creating a file.
    fn truncate_file(&self, file_path: &str) -> Result<(), FilestoreError>;

    fn remove_dir(&self, dir_path: &str, all: bool) -> Result<(), FilestoreError>;
    fn create_dir(&self, dir_path: &str) -> Result<(), FilestoreError>;

    fn read_data(
        &self,
        file_path: &str,
        offset: u64,
        read_len: u64,
        buf: &mut [u8],
    ) -> Result<(), FilestoreError>;

    fn write_data(&self, file: &str, offset: u64, buf: &[u8]) -> Result<(), FilestoreError>;

    fn filename_from_full_path(path: &str) -> Option<&str>
    where
        Self: Sized,
    {
        // Convert the path string to a Path
        let path = Path::new(path);

        // Extract the file name using the file_name() method
        path.file_name().and_then(|name| name.to_str())
    }

    fn is_file(&self, path: &str) -> Result<bool, FilestoreError>;

    fn is_dir(&self, path: &str) -> Result<bool, FilestoreError> {
        Ok(!self.is_file(path)?)
    }

    fn exists(&self, path: &str) -> Result<bool, FilestoreError>;

    /// Extract the file name part of a full path.
    ///
    /// This method should behave similarly to the [std::path::Path::file_name] method.
    fn file_name<'a>(&self, full_path: &'a str) -> Result<Option<&'a str>, FilestoreError>;

    fn file_size(&self, path: &str) -> Result<u64, FilestoreError>;

    /// This special function is the CFDP specific abstraction to calculate the checksum of a file.
    /// This allows to keep OS specific details like reading the whole file in the most efficient
    /// manner inside the file system abstraction.
    ///
    /// The passed verification buffer argument will be used by the specific implementation as
    /// a buffer to read the file into. It is recommended to use common buffer sizes like
    /// 4096 or 8192 bytes.
    fn calculate_checksum(
        &self,
        file_path: &str,
        checksum_type: ChecksumType,
        size_to_verify: u64,
        verification_buf: &mut [u8],
    ) -> Result<u32, FilestoreError>;

    /// This special function is the CFDP specific abstraction to verify the checksum of a file.
    /// This allows to keep OS specific details like reading the whole file in the most efficient
    /// manner inside the file system abstraction.
    ///
    /// The passed verification buffer argument will be used by the specific implementation as
    /// a buffer to read the file into. It is recommended to use common buffer sizes like
    /// 4096 or 8192 bytes.
    fn checksum_verify(
        &self,
        expected_checksum: u32,
        file_path: &str,
        checksum_type: ChecksumType,
        size_to_verify: u64,
        verification_buf: &mut [u8],
    ) -> Result<bool, FilestoreError> {
        Ok(
            self.calculate_checksum(file_path, checksum_type, size_to_verify, verification_buf)?
                == expected_checksum,
        )
    }
}

#[cfg(feature = "std")]
pub mod std_mod {

    use crc::Crc;

    use crate::{CRC_32, CRC_32C};

    use super::*;
    use std::{
        fs::{self, File, OpenOptions},
        io::{BufReader, Read, Seek, SeekFrom, Write},
    };

    #[derive(Default)]
    pub struct NativeFilestore {}

    impl VirtualFilestore for NativeFilestore {
        fn create_file(&self, file_path: &str) -> Result<(), FilestoreError> {
            if self.exists(file_path)? {
                return Err(FilestoreError::FileAlreadyExists);
            }
            File::create(file_path)?;
            Ok(())
        }

        fn remove_file(&self, file_path: &str) -> Result<(), FilestoreError> {
            if !self.exists(file_path)? {
                return Err(FilestoreError::FileDoesNotExist);
            }
            if !self.is_file(file_path)? {
                return Err(FilestoreError::IsNotFile);
            }
            fs::remove_file(file_path)?;
            Ok(())
        }

        fn file_name<'a>(&self, full_path: &'a str) -> Result<Option<&'a str>, FilestoreError> {
            let path = Path::new(full_path);
            path.file_name()
                .map(|s| s.to_str())
                .ok_or(FilestoreError::Utf8Error)
        }

        fn truncate_file(&self, file_path: &str) -> Result<(), FilestoreError> {
            if !self.exists(file_path)? {
                return Err(FilestoreError::FileDoesNotExist);
            }
            if !self.is_file(file_path)? {
                return Err(FilestoreError::IsNotFile);
            }
            OpenOptions::new()
                .write(true)
                .truncate(true)
                .open(file_path)?;
            Ok(())
        }

        fn create_dir(&self, dir_path: &str) -> Result<(), FilestoreError> {
            fs::create_dir(dir_path).map_err(|e| FilestoreError::Io {
                raw_errno: e.raw_os_error(),
                string: e.to_string(),
            })?;
            Ok(())
        }

        fn remove_dir(&self, dir_path: &str, all: bool) -> Result<(), FilestoreError> {
            if !self.exists(dir_path)? {
                return Err(FilestoreError::DirDoesNotExist);
            }
            if !self.is_dir(dir_path)? {
                return Err(FilestoreError::IsNotDirectory);
            }
            if !all {
                fs::remove_dir(dir_path)?;
                return Ok(());
            }
            fs::remove_dir_all(dir_path)?;
            Ok(())
        }

        fn read_data(
            &self,
            file_name: &str,
            offset: u64,
            read_len: u64,
            buf: &mut [u8],
        ) -> Result<(), FilestoreError> {
            if buf.len() < read_len as usize {
                return Err(ByteConversionError::ToSliceTooSmall {
                    found: buf.len(),
                    expected: read_len as usize,
                }
                .into());
            }
            if !self.exists(file_name)? {
                return Err(FilestoreError::FileDoesNotExist);
            }
            if !self.is_file(file_name)? {
                return Err(FilestoreError::IsNotFile);
            }
            let mut file = File::open(file_name)?;
            file.seek(SeekFrom::Start(offset))?;
            file.read_exact(&mut buf[0..read_len as usize])?;
            Ok(())
        }

        fn write_data(&self, file: &str, offset: u64, buf: &[u8]) -> Result<(), FilestoreError> {
            if !self.exists(file)? {
                return Err(FilestoreError::FileDoesNotExist);
            }
            if !self.is_file(file)? {
                return Err(FilestoreError::IsNotFile);
            }
            let mut file = OpenOptions::new().write(true).open(file)?;
            file.seek(SeekFrom::Start(offset))?;
            file.write_all(buf)?;
            Ok(())
        }

        fn is_file(&self, str_path: &str) -> Result<bool, FilestoreError> {
            let path = Path::new(str_path);
            if !self.exists(str_path)? {
                return Err(FilestoreError::FileDoesNotExist);
            }
            Ok(path.is_file())
        }

        fn exists(&self, path: &str) -> Result<bool, FilestoreError> {
            let path = Path::new(path);
            Ok(self.exists_internal(path))
        }

        fn file_size(&self, str_path: &str) -> Result<u64, FilestoreError> {
            let path = Path::new(str_path);
            if !self.exists_internal(path) {
                return Err(FilestoreError::FileDoesNotExist);
            }
            if !path.is_file() {
                return Err(FilestoreError::IsNotFile);
            }
            Ok(path.metadata()?.len())
        }

        fn calculate_checksum(
            &self,
            file_path: &str,
            checksum_type: ChecksumType,
            size_to_verify: u64,
            verification_buf: &mut [u8],
        ) -> Result<u32, FilestoreError> {
            let mut calc_with_crc_lib = |crc: Crc<u32>| -> Result<u32, FilestoreError> {
                let mut digest = crc.digest();
                let mut buf_reader = BufReader::new(File::open(file_path)?);
                let mut remaining_bytes = size_to_verify;
                while remaining_bytes > 0 {
                    // Read the smaller of the remaining bytes or the buffer size
                    let bytes_to_read = remaining_bytes.min(verification_buf.len() as u64) as usize;
                    let bytes_read = buf_reader.read(&mut verification_buf[0..bytes_to_read])?;

                    if bytes_read == 0 {
                        break; // Reached end of file
                    }
                    digest.update(&verification_buf[0..bytes_read]);
                    remaining_bytes -= bytes_read as u64;
                }
                Ok(digest.finalize())
            };
            match checksum_type {
                ChecksumType::Modular => self.calc_modular_checksum(file_path),
                ChecksumType::Crc32 => calc_with_crc_lib(CRC_32),
                ChecksumType::Crc32C => calc_with_crc_lib(CRC_32C),
                ChecksumType::NullChecksum => Ok(0),
                _ => Err(FilestoreError::ChecksumTypeNotImplemented(checksum_type)),
            }
        }
    }

    impl NativeFilestore {
        pub fn calc_modular_checksum(&self, file_path: &str) -> Result<u32, FilestoreError> {
            let mut checksum: u32 = 0;
            let file = File::open(file_path)?;
            let mut buf_reader = BufReader::new(file);
            let mut buffer = [0; 4];

            loop {
                let bytes_read = buf_reader.read(&mut buffer)?;
                if bytes_read == 0 {
                    break;
                }
                // Perform padding directly in the buffer
                (bytes_read..4).for_each(|i| {
                    buffer[i] = 0;
                });

                checksum = checksum.wrapping_add(u32::from_be_bytes(buffer));
            }
            Ok(checksum)
        }

        fn exists_internal(&self, path: &Path) -> bool {
            if !path.exists() {
                return false;
            }
            true
        }
    }
}

#[cfg(test)]
mod tests {
    use std::{fs, path::Path, println};

    use super::*;
    use alloc::format;
    use tempfile::tempdir;

    const EXAMPLE_DATA_CFDP: [u8; 15] = [
        0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E,
    ];

    const NATIVE_FS: NativeFilestore = NativeFilestore {};

    #[test]
    fn test_basic_native_filestore_create() {
        let tmpdir = tempdir().expect("creating tmpdir failed");
        let file_path = tmpdir.path().join("test.txt");
        let result =
            NATIVE_FS.create_file(file_path.to_str().expect("getting str for file failed"));
        assert!(result.is_ok());
        let path = Path::new(&file_path);
        assert!(path.exists());
        assert!(NATIVE_FS.exists(file_path.to_str().unwrap()).unwrap());
        assert!(NATIVE_FS.is_file(file_path.to_str().unwrap()).unwrap());
    }

    #[test]
    fn test_basic_native_fs_file_exists() {
        let tmpdir = tempdir().expect("creating tmpdir failed");
        let file_path = tmpdir.path().join("test.txt");
        assert!(!NATIVE_FS.exists(file_path.to_str().unwrap()).unwrap());
        NATIVE_FS
            .create_file(file_path.to_str().expect("getting str for file failed"))
            .unwrap();
        assert!(NATIVE_FS.exists(file_path.to_str().unwrap()).unwrap());
        assert!(NATIVE_FS.is_file(file_path.to_str().unwrap()).unwrap());
    }

    #[test]
    fn test_basic_native_fs_dir_exists() {
        let tmpdir = tempdir().expect("creating tmpdir failed");
        let dir_path = tmpdir.path().join("testdir");
        assert!(!NATIVE_FS.exists(dir_path.to_str().unwrap()).unwrap());
        NATIVE_FS
            .create_dir(dir_path.to_str().expect("getting str for file failed"))
            .unwrap();
        assert!(NATIVE_FS.exists(dir_path.to_str().unwrap()).unwrap());
        assert!(NATIVE_FS
            .is_dir(dir_path.as_path().to_str().unwrap())
            .unwrap());
    }

    #[test]
    fn test_basic_native_fs_remove_file() {
        let tmpdir = tempdir().expect("creating tmpdir failed");
        let file_path = tmpdir.path().join("test.txt");
        NATIVE_FS
            .create_file(file_path.to_str().expect("getting str for file failed"))
            .expect("creating file failed");
        assert!(NATIVE_FS.exists(file_path.to_str().unwrap()).unwrap());
        NATIVE_FS
            .remove_file(file_path.to_str().unwrap())
            .expect("removing file failed");
        assert!(!NATIVE_FS.exists(file_path.to_str().unwrap()).unwrap());
    }

    #[test]
    fn test_basic_native_fs_write() {
        let tmpdir = tempdir().expect("creating tmpdir failed");
        let file_path = tmpdir.path().join("test.txt");
        assert!(!NATIVE_FS.exists(file_path.to_str().unwrap()).unwrap());
        NATIVE_FS
            .create_file(file_path.to_str().expect("getting str for file failed"))
            .unwrap();
        assert!(NATIVE_FS.exists(file_path.to_str().unwrap()).unwrap());
        assert!(NATIVE_FS.is_file(file_path.to_str().unwrap()).unwrap());
        println!("{}", file_path.to_str().unwrap());
        let write_data = "hello world\n";
        NATIVE_FS
            .write_data(file_path.to_str().unwrap(), 0, write_data.as_bytes())
            .expect("writing to file failed");
        let read_back = fs::read_to_string(file_path).expect("reading back data failed");
        assert_eq!(read_back, write_data);
    }

    #[test]
    fn test_basic_native_fs_read() {
        let tmpdir = tempdir().expect("creating tmpdir failed");
        let file_path = tmpdir.path().join("test.txt");
        assert!(!NATIVE_FS.exists(file_path.to_str().unwrap()).unwrap());
        NATIVE_FS
            .create_file(file_path.to_str().expect("getting str for file failed"))
            .unwrap();
        assert!(NATIVE_FS.exists(file_path.to_str().unwrap()).unwrap());
        assert!(NATIVE_FS.is_file(file_path.to_str().unwrap()).unwrap());
        println!("{}", file_path.to_str().unwrap());
        let write_data = "hello world\n";
        NATIVE_FS
            .write_data(file_path.to_str().unwrap(), 0, write_data.as_bytes())
            .expect("writing to file failed");
        let read_back = fs::read_to_string(file_path).expect("reading back data failed");
        assert_eq!(read_back, write_data);
    }

    #[test]
    fn test_truncate_file() {
        let tmpdir = tempdir().expect("creating tmpdir failed");
        let file_path = tmpdir.path().join("test.txt");
        NATIVE_FS
            .create_file(file_path.to_str().expect("getting str for file failed"))
            .expect("creating file failed");
        fs::write(file_path.clone(), [1, 2, 3, 4]).unwrap();
        assert_eq!(fs::read(file_path.clone()).unwrap(), [1, 2, 3, 4]);
        NATIVE_FS
            .truncate_file(file_path.to_str().unwrap())
            .unwrap();
        assert_eq!(fs::read(file_path.clone()).unwrap(), []);
    }

    #[test]
    fn test_remove_dir() {
        let tmpdir = tempdir().expect("creating tmpdir failed");
        let dir_path = tmpdir.path().join("testdir");
        assert!(!NATIVE_FS.exists(dir_path.to_str().unwrap()).unwrap());
        NATIVE_FS
            .create_dir(dir_path.to_str().expect("getting str for file failed"))
            .unwrap();
        assert!(NATIVE_FS.exists(dir_path.to_str().unwrap()).unwrap());
        NATIVE_FS
            .remove_dir(dir_path.to_str().unwrap(), false)
            .unwrap();
        assert!(!NATIVE_FS.exists(dir_path.to_str().unwrap()).unwrap());
    }

    #[test]
    fn test_read_file() {
        let tmpdir = tempdir().expect("creating tmpdir failed");
        let file_path = tmpdir.path().join("test.txt");
        NATIVE_FS
            .create_file(file_path.to_str().expect("getting str for file failed"))
            .expect("creating file failed");
        fs::write(file_path.clone(), [1, 2, 3, 4]).unwrap();
        let read_buf: &mut [u8] = &mut [0; 4];
        NATIVE_FS
            .read_data(file_path.to_str().unwrap(), 0, 4, read_buf)
            .unwrap();
        assert_eq!([1, 2, 3, 4], read_buf);
        NATIVE_FS
            .write_data(file_path.to_str().unwrap(), 4, &[5, 6, 7, 8])
            .expect("writing to file failed");
        NATIVE_FS
            .read_data(file_path.to_str().unwrap(), 2, 4, read_buf)
            .unwrap();
        assert_eq!([3, 4, 5, 6], read_buf);
    }

    #[test]
    fn test_remove_which_does_not_exist() {
        let tmpdir = tempdir().expect("creating tmpdir failed");
        let file_path = tmpdir.path().join("test.txt");
        let result = NATIVE_FS.read_data(file_path.to_str().unwrap(), 0, 4, &mut [0; 4]);
        assert!(result.is_err());
        let error = result.unwrap_err();
        if let FilestoreError::FileDoesNotExist = error {
            assert_eq!(error.to_string(), "file does not exist");
        } else {
            panic!("unexpected error");
        }
    }

    #[test]
    fn test_file_already_exists() {
        let tmpdir = tempdir().expect("creating tmpdir failed");
        let file_path = tmpdir.path().join("test.txt");
        let result =
            NATIVE_FS.create_file(file_path.to_str().expect("getting str for file failed"));
        assert!(result.is_ok());
        let result =
            NATIVE_FS.create_file(file_path.to_str().expect("getting str for file failed"));
        assert!(result.is_err());
        let error = result.unwrap_err();
        if let FilestoreError::FileAlreadyExists = error {
            assert_eq!(error.to_string(), "file already exists");
        } else {
            panic!("unexpected error");
        }
    }

    #[test]
    fn test_remove_file_with_dir_api() {
        let tmpdir = tempdir().expect("creating tmpdir failed");
        let file_path = tmpdir.path().join("test.txt");
        NATIVE_FS
            .create_file(file_path.to_str().expect("getting str for file failed"))
            .unwrap();
        let result = NATIVE_FS.remove_dir(file_path.to_str().unwrap(), true);
        assert!(result.is_err());
        let error = result.unwrap_err();
        if let FilestoreError::IsNotDirectory = error {
            assert_eq!(error.to_string(), "is not a directory");
        } else {
            panic!("unexpected error");
        }
    }

    #[test]
    fn test_remove_dir_remove_all() {
        let tmpdir = tempdir().expect("creating tmpdir failed");
        let dir_path = tmpdir.path().join("test");
        NATIVE_FS
            .create_dir(dir_path.to_str().expect("getting str for file failed"))
            .unwrap();
        let file_path = dir_path.as_path().join("test.txt");
        NATIVE_FS
            .create_file(file_path.to_str().expect("getting str for file failed"))
            .unwrap();
        let result = NATIVE_FS.remove_dir(dir_path.to_str().unwrap(), true);
        assert!(result.is_ok());
        assert!(!NATIVE_FS.exists(dir_path.to_str().unwrap()).unwrap());
    }

    #[test]
    fn test_remove_dir_with_file_api() {
        let tmpdir = tempdir().expect("creating tmpdir failed");
        let file_path = tmpdir.path().join("test");
        NATIVE_FS
            .create_dir(file_path.to_str().expect("getting str for file failed"))
            .unwrap();
        let result = NATIVE_FS.remove_file(file_path.to_str().unwrap());
        assert!(result.is_err());
        let error = result.unwrap_err();
        if let FilestoreError::IsNotFile = error {
            assert_eq!(error.to_string(), "is not a file");
        } else {
            panic!("unexpected error");
        }
    }

    #[test]
    fn test_remove_dir_which_does_not_exist() {
        let tmpdir = tempdir().expect("creating tmpdir failed");
        let file_path = tmpdir.path().join("test");
        let result = NATIVE_FS.remove_dir(file_path.to_str().unwrap(), true);
        assert!(result.is_err());
        let error = result.unwrap_err();
        if let FilestoreError::DirDoesNotExist = error {
            assert_eq!(error.to_string(), "directory does not exist");
        } else {
            panic!("unexpected error");
        }
    }

    #[test]
    fn test_remove_file_which_does_not_exist() {
        let tmpdir = tempdir().expect("creating tmpdir failed");
        let file_path = tmpdir.path().join("test.txt");
        let result = NATIVE_FS.remove_file(file_path.to_str().unwrap());
        assert!(result.is_err());
        let error = result.unwrap_err();
        if let FilestoreError::FileDoesNotExist = error {
            assert_eq!(error.to_string(), "file does not exist");
        } else {
            panic!("unexpected error");
        }
    }

    #[test]
    fn test_truncate_file_which_does_not_exist() {
        let tmpdir = tempdir().expect("creating tmpdir failed");
        let file_path = tmpdir.path().join("test.txt");
        let result = NATIVE_FS.truncate_file(file_path.to_str().unwrap());
        assert!(result.is_err());
        let error = result.unwrap_err();
        if let FilestoreError::FileDoesNotExist = error {
            assert_eq!(error.to_string(), "file does not exist");
        } else {
            panic!("unexpected error");
        }
    }

    #[test]
    fn test_truncate_file_on_directory() {
        let tmpdir = tempdir().expect("creating tmpdir failed");
        let file_path = tmpdir.path().join("test");
        NATIVE_FS.create_dir(file_path.to_str().unwrap()).unwrap();
        let result = NATIVE_FS.truncate_file(file_path.to_str().unwrap());
        assert!(result.is_err());
        let error = result.unwrap_err();
        if let FilestoreError::IsNotFile = error {
            assert_eq!(error.to_string(), "is not a file");
        } else {
            panic!("unexpected error");
        }
    }

    #[test]
    fn test_byte_conversion_error_when_reading() {
        let tmpdir = tempdir().expect("creating tmpdir failed");
        let file_path = tmpdir.path().join("test.txt");
        NATIVE_FS
            .create_file(file_path.to_str().expect("getting str for file failed"))
            .unwrap();
        let result = NATIVE_FS.read_data(file_path.to_str().unwrap(), 0, 2, &mut []);
        assert!(result.is_err());
        let error = result.unwrap_err();
        if let FilestoreError::ByteConversion(byte_conv_error) = error {
            if let ByteConversionError::ToSliceTooSmall { found, expected } = byte_conv_error {
                assert_eq!(found, 0);
                assert_eq!(expected, 2);
            } else {
                panic!("unexpected error");
            }
            assert_eq!(
                error.to_string(),
                format!("filestore error: {}", byte_conv_error)
            );
        } else {
            panic!("unexpected error");
        }
    }

    #[test]
    fn test_read_file_on_dir() {
        let tmpdir = tempdir().expect("creating tmpdir failed");
        let dir_path = tmpdir.path().join("test");
        NATIVE_FS
            .create_dir(dir_path.to_str().expect("getting str for file failed"))
            .unwrap();
        let result = NATIVE_FS.read_data(dir_path.to_str().unwrap(), 0, 4, &mut [0; 4]);
        assert!(result.is_err());
        let error = result.unwrap_err();
        if let FilestoreError::IsNotFile = error {
            assert_eq!(error.to_string(), "is not a file");
        } else {
            panic!("unexpected error");
        }
    }

    #[test]
    fn test_write_file_non_existing() {
        let tmpdir = tempdir().expect("creating tmpdir failed");
        let file_path = tmpdir.path().join("test.txt");
        let result = NATIVE_FS.write_data(file_path.to_str().unwrap(), 0, &[]);
        assert!(result.is_err());
        let error = result.unwrap_err();
        if let FilestoreError::FileDoesNotExist = error {
        } else {
            panic!("unexpected error");
        }
    }

    #[test]
    fn test_write_file_on_dir() {
        let tmpdir = tempdir().expect("creating tmpdir failed");
        let file_path = tmpdir.path().join("test");
        NATIVE_FS.create_dir(file_path.to_str().unwrap()).unwrap();
        let result = NATIVE_FS.write_data(file_path.to_str().unwrap(), 0, &[]);
        assert!(result.is_err());
        let error = result.unwrap_err();
        if let FilestoreError::IsNotFile = error {
        } else {
            panic!("unexpected error");
        }
    }

    #[test]
    fn test_filename_extraction() {
        let tmpdir = tempdir().expect("creating tmpdir failed");
        let file_path = tmpdir.path().join("test.txt");
        NATIVE_FS
            .create_file(file_path.to_str().expect("getting str for file failed"))
            .unwrap();
        NativeFilestore::filename_from_full_path(file_path.to_str().unwrap());
    }

    #[test]
    fn test_modular_checksum() {
        let tmpdir = tempdir().expect("creating tmpdir failed");
        let file_path = tmpdir.path().join("mod-crc.bin");
        fs::write(file_path.as_path(), EXAMPLE_DATA_CFDP).expect("writing test file failed");
        // Kind of re-writing the modular checksum impl here which we are trying to test, but the
        // numbers/correctness were verified manually using calculators, so this is okay.
        let mut checksum: u32 = 0;
        let mut buffer: [u8; 4] = [0; 4];
        for i in 0..3 {
            buffer = EXAMPLE_DATA_CFDP[i * 4..(i + 1) * 4].try_into().unwrap();
            checksum = checksum.wrapping_add(u32::from_be_bytes(buffer));
        }
        buffer[0..3].copy_from_slice(&EXAMPLE_DATA_CFDP[12..15]);
        buffer[3] = 0;
        checksum = checksum.wrapping_add(u32::from_be_bytes(buffer));
        let mut verif_buf: [u8; 32] = [0; 32];
        let result = NATIVE_FS.checksum_verify(
            checksum,
            file_path.to_str().unwrap(),
            ChecksumType::Modular,
            EXAMPLE_DATA_CFDP.len() as u64,
            &mut verif_buf,
        );
        assert!(result.is_ok());
    }

    #[test]
    fn test_null_checksum_impl() {
        let tmpdir = tempdir().expect("creating tmpdir failed");
        let file_path = tmpdir.path().join("mod-crc.bin");
        // The file to check does not even need to exist, and the verification buffer can be
        // empty: the null checksum is always yields the same result.
        let result = NATIVE_FS.checksum_verify(
            0,
            file_path.to_str().unwrap(),
            ChecksumType::NullChecksum,
            0,
            &mut [],
        );
        assert!(result.is_ok());
        assert!(result.unwrap());
    }

    #[test]
    fn test_checksum_not_implemented() {
        let tmpdir = tempdir().expect("creating tmpdir failed");
        let file_path = tmpdir.path().join("mod-crc.bin");
        // The file to check does not even need to exist, and the verification buffer can be
        // empty: the null checksum is always yields the same result.
        let result = NATIVE_FS.checksum_verify(
            0,
            file_path.to_str().unwrap(),
            ChecksumType::Crc32Proximity1,
            0,
            &mut [],
        );
        assert!(result.is_err());
        let error = result.unwrap_err();
        if let FilestoreError::ChecksumTypeNotImplemented(cksum_type) = error {
            assert_eq!(
                error.to_string(),
                format!("checksum {:?} not implemented", cksum_type)
            );
        } else {
            panic!("unexpected error");
        }
    }
}