d-engine-core 0.2.3

Pure Raft consensus algorithm - for building custom Raft-based systems
Documentation
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
use crate::Error;
use crate::FileError;
use crate::StorageError;
use crate::SystemError;
use crate::file_io;
use crate::file_io::convert_vec_checksum;
use crate::file_io::create_parent_dir_if_not_exist;
use crate::file_io::delete_file;
use nix::libc::LOCK_EX;
use nix::libc::flock;
use sha2::Digest;
use sha2::Sha256;
use std::io::Write;
use std::os::fd::AsRawFd;
use std::os::unix::fs::PermissionsExt;
use tempfile::NamedTempFile;
use tempfile::tempdir;
use tokio::fs;
use tracing_test::traced_test;

/// Passed: "/tmp/files/data.txt"
/// Expected: "/tmp/files" created
#[tokio::test]
#[traced_test]
async fn test_create_parent_dir_for_file() {
    let temp_dir = tempfile::tempdir().unwrap();
    let temp_path = temp_dir.path().join("test_create_parent_dir_for_file");

    // File path: create parent directory
    let file_path = temp_path.join("files").join("data.txt");
    create_parent_dir_if_not_exist(&file_path).unwrap();

    // Verify parent directory exists
    let parent_dir = file_path.parent().unwrap();
    assert!(file_io::is_dir(parent_dir).await.unwrap());
    // File itself should NOT be created
    assert!(parent_dir.exists());
    assert!(!file_path.exists());
}

/// Passed: "/tmp/dir/subdir"
/// Expected: "/tmp/dir/subdir" created
#[tokio::test]
#[traced_test]
async fn test_create_parent_dir_for_directory_without_trailing_separator() {
    let temp_dir = tempfile::tempdir().unwrap();
    let temp_path = temp_dir
        .path()
        .join("test_create_parent_dir_for_directory_without_trailing_separator");

    // Directory path (explicit trailing separator)
    let dir_path = temp_path.join("dir").join("subdir");
    create_parent_dir_if_not_exist(&dir_path).unwrap();

    // Verify parent directory exists
    let parent_dir = dir_path.parent().unwrap();
    assert!(parent_dir.exists());
    assert!(file_io::is_dir(parent_dir).await.unwrap());
}

/// Passed: "/tmp/dir/subdir/"
/// Expected: "/tmp/dir/subdir" created
#[tokio::test]
#[traced_test]
async fn test_create_parent_dir_for_directory_with_trailing_separator() {
    let temp_dir = tempfile::tempdir().unwrap();
    let temp_path = temp_dir
        .path()
        .join("test_create_parent_dir_for_directory_with_trailing_separator");

    // Directory path (explicit trailing separator)
    let dir_path = temp_path.join("dir").join("subdir").join(""); // Trailing separator
    create_parent_dir_if_not_exist(&dir_path).unwrap();

    // Verify directory itself exists
    assert!(dir_path.exists());
    assert!(file_io::is_dir(&dir_path).await.unwrap());
}

#[tokio::test]
#[traced_test]
async fn test_delete_file_success() {
    // Create temp file
    let mut file = NamedTempFile::new().unwrap();
    let path = file.path().to_owned();

    // Write test content
    writeln!(file, "test content").unwrap();

    // Delete the file
    let result = delete_file(&path).await;
    assert!(result.is_ok(), "Should successfully delete file");

    // Verify file no longer exists
    assert!(!path.exists(), "File should be deleted");
}

/// Test non-existent file path
#[tokio::test]
#[traced_test]
async fn test_delete_nonexistent_file() {
    let e = delete_file("nonexistent.txt").await.unwrap_err();
    assert!(
        matches!(
            e,
            Error::System(SystemError::Storage(StorageError::File(
                FileError::NotFound(_)
            )))
        ),
        "Should return NotFound error"
    );
}

/// Test directory deletion attempt
#[tokio::test]
#[traced_test]
async fn test_delete_directory() {
    // Create temp directory
    let dir = tempdir().unwrap();
    let dir_path = dir.path().to_owned();

    let e = delete_file(&dir_path).await.unwrap_err();
    assert!(
        matches!(
            e,
            Error::System(SystemError::Storage(StorageError::File(
                FileError::IsDirectory(_)
            )))
        ),
        "Should return IsDirectory error"
    );
}

/// Test busy file deletion (platform-specific)
#[tokio::test]
#[traced_test]
async fn test_delete_busy_file() {
    // Create temp file
    let temp_dir = tempfile::tempdir().unwrap();
    let temp_path = temp_dir.path().join("test_delete_busy_file");
    let dir_path = temp_path.to_owned();
    tokio::fs::create_dir_all(&dir_path).await.unwrap();

    // Create a test file
    let file_path = dir_path.join("test_file.txt");
    // Lock the file (platform-specific implementation)
    #[cfg(windows)]
    {
        // Keep file open to prevent deletion on Windows
        let _file_handle =
            std::fs::OpenOptions::new().write(true).create(true).open(&file_path).unwrap();
    }
    #[cfg(unix)]
    {
        // Open with exclusive lock on Unix-like systems
        use std::os::unix::fs::OpenOptionsExt;

        // Create a file and apply an exclusive lock
        let file = std::fs::OpenOptions::new()
            .write(true)
            .create(true)
            .truncate(true)
            .mode(0o644) // Set permissions
            .open(&file_path)
            .unwrap();

        // Apply file lock
        unsafe {
            flock(file.as_raw_fd(), LOCK_EX);
        }

        // Keep the file handle alive
        let _file_handle = file;
    }

    let e = delete_file(&file_path).await;

    #[cfg(unix)]
    assert!(e.is_ok());

    #[cfg(windows)]
    assert!(e.is_err());
}

/// Test permission error (if possible in test environment)
#[tokio::test]
#[cfg(unix)] // Unix-like systems have clearer permission semantics
async fn test_delete_permission_denied() {
    // Create temp file
    let temp_dir = tempfile::tempdir().unwrap();
    let temp_path = temp_dir.path().join("test_delete_permission_denied");
    let dir_path = temp_path.to_owned();
    tokio::fs::create_dir_all(&dir_path).await.unwrap();

    // Create a test file
    let file_path = dir_path.join("test_file.txt");
    fs::write(&file_path, b"test").await.unwrap();

    // Set read-only permissions
    let mut perms = fs::metadata(&dir_path).await.unwrap().permissions();
    perms.set_mode(0o444); // Read-only
    fs::set_permissions(&dir_path, perms.clone()).await.unwrap();

    let e = delete_file(&file_path).await.unwrap_err();
    println!("{:?}", &e);

    assert!(
        matches!(
            e,
            Error::System(SystemError::Storage(StorageError::File(
                FileError::PermissionDenied(_)
            )))
        ),
        "Should return PermissionDenied error"
    );

    // Cleanup permissions for temp file deletion
    perms.set_mode(0o700);
    fs::set_permissions(&dir_path, perms).await.unwrap();
}

#[test]
fn test_convert_vec_checksum_converts_valid_checksum() {
    let input = vec![1; 32];
    let result = convert_vec_checksum(input).unwrap();
    assert_eq!(result, [1; 32]);
}

#[test]
fn test_convert_vec_checksum_rejects_short_checksum() {
    let input = vec![0; 31];
    let result = convert_vec_checksum(input);
    assert!(result.is_err());
}

#[test]
fn test_convert_vec_checksum_rejects_long_checksum() {
    let input = vec![0; 33];
    let result = convert_vec_checksum(input);
    assert!(result.is_err());
}

#[test]
fn test_convert_vec_checksum_rejects_empty_checksum() {
    let input = vec![];
    let result = convert_vec_checksum(input);
    assert!(result.is_err());
}

#[test]
fn test_convert_vec_checksum_preserves_byte_order() {
    let mut input = vec![0; 32];
    input[31] = 0xFF;
    let result = convert_vec_checksum(input).unwrap();
    assert_eq!(result[31], 0xFF);
    assert_eq!(result[0], 0x00);
}

#[cfg(test)]
mod validate_compressed_format_tests {
    use std::fs::File;
    use std::io::Write;

    use tempfile::tempdir;
    use tracing::trace;

    use super::*;
    use crate::Result;
    use crate::file_io::validate_compressed_format;

    /// Test valid GZIP files with supported extensions
    #[tokio::test]
    async fn valid_compressed_files() -> Result<()> {
        let test_cases = &[
            ("valid.tar.gz", [0x1f, 0x8b]),
            ("archive.tgz", [0x1f, 0x8b]),
            ("data.snap", [0x1f, 0x8b]),
        ];

        for (filename, header) in test_cases {
            let dir = tempdir().unwrap();
            let path = dir.path().join(filename);

            // Create a file with proper GZIP header
            let mut file = File::create(&path).unwrap();
            file.write_all(header).unwrap();
            // Add some dummy content after the header
            file.write_all(b"dummy content").unwrap();

            // Execute validation
            let result = validate_compressed_format(&path);
            assert!(result.is_ok(), "Failed case: {filename}");
        }

        Ok(())
    }

    /// Test invalid file extensions
    #[tokio::test]
    async fn invalid_extensions() {
        let cases = vec![
            ("/tmp/text.zip", "zip"),
            ("/tmp/data.rar", "rar"),
            ("/tmp/no_extension", ""),
        ];

        for (filename, _expected_ext) in cases {
            let dir = tempdir().unwrap();
            let path = dir.path().join(filename);

            // Create a simple compressed file instead of using create_valid_snapshot
            let mut file = File::create(&path).unwrap();
            // Write some dummy content
            file.write_all(b"dummy content").unwrap();

            let result = validate_compressed_format(&path);
            trace!("{result:?}",);
            assert!(
                matches!(
                    result,
                    Err(Error::System(SystemError::Storage(StorageError::File(FileError::InvalidExt(msg)))))
                    if msg.contains("Invalid compression extension") || msg.contains("Invalid file extension")
                ),
                "Failed case: {filename}",
            );
        }
    }

    /// Test files with valid extension but invalid header
    #[tokio::test]
    async fn invalid_magic_numbers() -> Result<()> {
        let dir = tempdir().unwrap();
        let path = dir.path().join("invalid.gz");

        // Create file with wrong header but in a temp directory
        let temp_dir = tempdir().unwrap();
        let temp_path = temp_dir.path().join("temp_invalid.gz");

        {
            let mut file = File::create(&temp_path).unwrap();
            for _ in 1..=10 {
                file.write_all(&[0x89, 0x50]).unwrap(); // PNG magic number
            }
        }

        // Copy to final location
        tokio::fs::copy(&temp_path, &path).await.unwrap();

        let result = validate_compressed_format(&path);
        trace!("{result:?}",);
        assert!(matches!(
            result,
            Err(Error::System(SystemError::Storage(StorageError::File(FileError::InvalidGzipHeader(msg)))))
            if msg.contains("Invalid GZIP header")
        ));

        Ok(())
    }

    /// Test empty file handling
    #[test]
    fn empty_file() {
        let dir = tempdir().unwrap();
        let path = dir.path().join("empty.gz");

        File::create(&path).unwrap(); // Empty file
        let result = validate_compressed_format(&path);
        trace!("{result:?}",);

        assert!(matches!(
            result,
            Err(Error::System(SystemError::Storage(StorageError::File(
                FileError::TooSmall(_)
            ))))
        ));
    }
}

#[cfg(test)]
mod compute_checksum_from_file_path_tests {
    use super::*;
    use crate::file_io::compute_checksum_from_file_path;

    /// Test computing checksum for an empty file
    #[tokio::test]
    async fn test_compute_checksum_from_path_empty_file() {
        let temp_dir = tempdir().unwrap();
        let file_path = temp_dir.path().join("empty.txt");
        tokio::fs::write(&file_path, b"").await.unwrap();

        let checksum = compute_checksum_from_file_path(&file_path)
            .await
            .expect("Should compute checksum for empty file");

        // SHA-256 hash of empty data
        let hasher = Sha256::new();
        let expected: [u8; 32] = hasher.finalize().into();

        assert_eq!(
            checksum, expected,
            "Checksum for empty file should be SHA-256 of empty data"
        );
    }

    /// Test computing checksum for a small file
    #[tokio::test]
    async fn test_compute_checksum_from_path_small_file() {
        let temp_dir = tempdir().unwrap();
        let file_path = temp_dir.path().join("test.txt");
        tokio::fs::write(&file_path, b"Hello, world!").await.unwrap();

        let checksum = compute_checksum_from_file_path(&file_path)
            .await
            .expect("Should compute checksum for file");

        // Calculate expected SHA-256
        let mut hasher = Sha256::new();
        hasher.update(b"Hello, world!");
        let expected: [u8; 32] = hasher.finalize().into();

        assert_eq!(
            checksum, expected,
            "Checksum should match SHA-256 of file content"
        );
    }

    /// Test computing checksum for a large file
    #[tokio::test]
    async fn test_compute_checksum_from_path_large_file() {
        let temp_dir = tempdir().unwrap();
        let file_path = temp_dir.path().join("large.bin");

        // Generate 5MB of random data
        let data: Vec<u8> = (0..5 * 1024 * 1024).map(|_| rand::random::<u8>()).collect();
        tokio::fs::write(&file_path, &data).await.unwrap();

        let checksum = compute_checksum_from_file_path(&file_path)
            .await
            .expect("Should compute checksum for large file");

        // Calculate expected SHA-256
        let mut hasher = Sha256::new();
        hasher.update(&data);
        let expected: [u8; 32] = hasher.finalize().into();

        assert_eq!(
            checksum, expected,
            "Checksum should match SHA-256 of large file content"
        );
    }

    /// Test error handling for non-existent file
    #[tokio::test]
    async fn test_compute_checksum_nonexistent_file() {
        let temp_dir = tempdir().unwrap();
        let non_existent_path = temp_dir.path().join("does_not_exist.txt");

        let result = compute_checksum_from_file_path(&non_existent_path).await;

        assert!(result.is_err(), "Should return error for non-existent file");
        match result.unwrap_err() {
            Error::System(SystemError::Storage(StorageError::IoError(_))) => {} // Expected
            other => panic!("Expected IoError, got {other:?}"),
        }
    }

    /// Test checksum consistency across multiple runs
    #[tokio::test]
    async fn test_compute_checksum_consistency() {
        let temp_dir = tempdir().unwrap();
        let file_path = temp_dir.path().join("data.bin");
        tokio::fs::write(&file_path, b"Consistent data").await.unwrap();

        // Compute checksum twice
        let checksum1 = compute_checksum_from_file_path(&file_path)
            .await
            .expect("First computation should succeed");

        let checksum2 = compute_checksum_from_file_path(&file_path)
            .await
            .expect("Second computation should succeed");

        assert_eq!(
            checksum1, checksum2,
            "Checksum should be consistent across multiple computations"
        );
    }

    /// Test that checksum changes when file content changes
    #[tokio::test]
    async fn test_compute_checksum_content_change() {
        let temp_dir = tempdir().unwrap();
        let file_path = temp_dir.path().join("data.txt");

        // First version
        tokio::fs::write(&file_path, b"Version 1").await.unwrap();
        let checksum1 = compute_checksum_from_file_path(&file_path)
            .await
            .expect("First computation should succeed");

        // Second version
        tokio::fs::write(&file_path, b"Version 2").await.unwrap();
        let checksum2 = compute_checksum_from_file_path(&file_path)
            .await
            .expect("Second computation should succeed");

        assert_ne!(
            checksum1, checksum2,
            "Checksum should change when file content changes"
        );
    }
}