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
use tempfile::NamedTempFile;

use crate::Error;
use crate::config::tls::TlsConfig;

#[test]
fn test_tls_config_default_values() {
    let config = TlsConfig::default();

    assert!(!config.enable_tls);
    assert!(!config.generate_self_signed_certificates);
    assert!(!config.enable_mtls);

    let result = config.validate();
    assert!(
        result.is_ok(),
        "Default config should validate when TLS is disabled"
    );

    assert_eq!(
        config.certificate_authority_root_path,
        "/etc/ssl/certs/ca.pem"
    );
    assert_eq!(config.server_certificate_path, "./certs/server.pem");
    assert_eq!(config.server_private_key_path, "./certs/server.key");
    assert_eq!(
        config.client_certificate_authority_root_path,
        "/etc/ssl/certs/ca.pem"
    );
}

#[test]
fn test_validate_tls_disabled_should_succeed() {
    let config = TlsConfig {
        enable_tls: false,
        ..Default::default()
    };

    let result = config.validate();
    assert!(
        result.is_ok(),
        "Validation should succeed when TLS is disabled"
    );
}
#[test]
fn test_validate_mtls_without_tls_should_fail() {
    // Now create a config that will hit the mTLS check
    let invalid_config = TlsConfig {
        enable_tls: false, // TLS disabled
        enable_mtls: true, // But mTLS enabled - this should fail
        ..Default::default()
    };

    // Directly check the mTLS condition that would cause the error
    assert!(
        invalid_config.enable_mtls && !invalid_config.enable_tls,
        "Config should have mTLS enabled but TLS disabled"
    );

    // The real test is to verify the error condition is caught
    let result = invalid_config.validate();
    assert!(
        result.is_err(),
        "Validation should fail when mTLS is enabled without TLS"
    );

    // Check the error message
    let error = result.unwrap_err();
    assert!(matches!(error, Error::Config(_)));
    assert!(error.to_string().contains("mTLS requires enable_tls to be true"));
}

#[test]
fn test_validate_self_signed_with_cert_paths_should_fail() {
    let config = TlsConfig {
        enable_tls: true,
        generate_self_signed_certificates: true,
        server_certificate_path: "./certs/existing.pem".to_string(),
        server_private_key_path: "./certs/existing.key".to_string(),
        ..Default::default()
    };

    let result = config.validate();
    assert!(
        result.is_err(),
        "Validation should fail when self-signed is enabled with certificate paths"
    );

    let error = result.unwrap_err();
    assert!(matches!(error, Error::Config(_)));
    assert!(
        error.to_string().contains(
            "Cannot specify certificate paths with generate_self_signed_certificates=true"
        )
    );
}

#[test]
fn test_validate_self_signed_without_paths_should_succeed() {
    let config = TlsConfig {
        enable_tls: true,
        generate_self_signed_certificates: true,
        server_certificate_path: String::new(),
        server_private_key_path: String::new(),
        ..Default::default()
    };

    let result = config.validate();
    assert!(
        result.is_ok(),
        "Validation should succeed when self-signed is enabled without certificate paths"
    );
}

#[test]
fn test_validate_missing_server_certificate_should_fail() {
    let config = TlsConfig {
        enable_tls: true,
        generate_self_signed_certificates: false,
        server_certificate_path: "/nonexistent/server.pem".to_string(),
        ..Default::default()
    };

    let result = config.validate();
    assert!(
        result.is_err(),
        "Validation should fail when server certificate is missing"
    );

    let error = result.unwrap_err();
    assert!(matches!(error, Error::Config(_)));
    assert!(error.to_string().contains("server certificate file"));
    assert!(error.to_string().contains("not found"));
}

#[test]
fn test_validate_missing_server_private_key_should_fail() {
    // Create temporary files for the other required certificates
    let cert_file = NamedTempFile::new().unwrap();

    let config = TlsConfig {
        enable_tls: true,
        generate_self_signed_certificates: false,
        server_private_key_path: "/nonexistent/server.key".to_string(),
        server_certificate_path: cert_file.path().to_string_lossy().to_string(),
        ..Default::default()
    };

    let result = config.validate();
    assert!(
        result.is_err(),
        "Validation should fail when server private key is missing"
    );

    let error = result.unwrap_err();
    assert!(matches!(error, Error::Config(_)));

    // The error message will contain "server private key" and "not found"
    // but let's be more flexible and just check for key parts
    let error_str = error.to_string();
    assert!(error_str.contains("not found"));
    // The path in the error might be formatted differently, but should include our path
    assert!(
        error_str.contains("server.key")
            || error_str.contains("/nonexistent")
            || error_str.contains("server private key")
    );
}
#[test]
fn test_validate_missing_ca_certificate_should_fail() {
    // Create temporary files for the other required certificates
    let cert_file = NamedTempFile::new().unwrap();
    let key_file = NamedTempFile::new().unwrap();

    let config = TlsConfig {
        enable_tls: true,
        generate_self_signed_certificates: false,
        certificate_authority_root_path: "/nonexistent/ca.pem".to_string(),
        server_certificate_path: cert_file.path().to_string_lossy().to_string(),
        server_private_key_path: key_file.path().to_string_lossy().to_string(),
        ..Default::default()
    };

    let result = config.validate();
    assert!(
        result.is_err(),
        "Validation should fail when CA certificate is missing"
    );

    let error = result.unwrap_err();
    assert!(matches!(error, Error::Config(_)));

    // The error message will contain "CA certificate" and "not found"
    // but let's be more flexible and just check for key parts
    let error_str = error.to_string();
    assert!(error_str.contains("not found"));
    // The path in the error might be formatted differently, but should include our path
    assert!(
        error_str.contains("ca.pem")
            || error_str.contains("/nonexistent")
            || error_str.contains("CA certificate")
    );
}
#[test]
fn test_validate_mtls_missing_client_ca_should_fail() {
    // Create temporary files for the other required certificates
    let cert_file = NamedTempFile::new().unwrap();
    let key_file = NamedTempFile::new().unwrap();
    let ca_file = NamedTempFile::new().unwrap();

    let config = TlsConfig {
        enable_tls: true,
        enable_mtls: true,
        generate_self_signed_certificates: false,
        client_certificate_authority_root_path: "/nonexistent/client-ca.pem".to_string(),
        // Add the other paths to ensure validation reaches the client CA check
        server_certificate_path: cert_file.path().to_string_lossy().to_string(),
        server_private_key_path: key_file.path().to_string_lossy().to_string(),
        certificate_authority_root_path: ca_file.path().to_string_lossy().to_string(),
    };

    let result = config.validate();
    assert!(
        result.is_err(),
        "Validation should fail when client CA certificate is missing for mTLS"
    );

    let error = result.unwrap_err();
    assert!(matches!(error, Error::Config(_)));

    // The error message will contain "client CA certificate" and "not found"
    // but let's be more flexible and just check for key parts
    let error_str = error.to_string();
    assert!(error_str.contains("not found"));
    // The path in the error might be formatted differently, but should include our path
    assert!(
        error_str.contains("client-ca.pem")
            || error_str.contains("/nonexistent")
            || error_str.contains("client CA certificate")
    );
}

#[test]
fn test_validate_mtls_with_valid_client_ca_should_succeed() {
    // Create temporary certificate files for testing
    let cert_file = NamedTempFile::new().unwrap();
    let key_file = NamedTempFile::new().unwrap();
    let ca_file = NamedTempFile::new().unwrap();
    let client_ca_file = NamedTempFile::new().unwrap();

    let config = TlsConfig {
        enable_tls: true,
        enable_mtls: true,
        generate_self_signed_certificates: false,
        server_certificate_path: cert_file.path().to_string_lossy().to_string(),
        server_private_key_path: key_file.path().to_string_lossy().to_string(),
        certificate_authority_root_path: ca_file.path().to_string_lossy().to_string(),
        client_certificate_authority_root_path: client_ca_file.path().to_string_lossy().to_string(),
    };

    let result = config.validate();
    assert!(
        result.is_ok(),
        "Validation should succeed with valid certificate files for mTLS"
    );
}

#[test]
fn test_validate_tls_with_valid_files_should_succeed() {
    // Create temporary certificate files for testing
    let cert_file = NamedTempFile::new().unwrap();
    let key_file = NamedTempFile::new().unwrap();
    let ca_file = NamedTempFile::new().unwrap();

    let config = TlsConfig {
        enable_tls: true,
        generate_self_signed_certificates: false,
        server_certificate_path: cert_file.path().to_string_lossy().to_string(),
        server_private_key_path: key_file.path().to_string_lossy().to_string(),
        certificate_authority_root_path: ca_file.path().to_string_lossy().to_string(),
        ..Default::default()
    };

    let result = config.validate();
    assert!(
        result.is_ok(),
        "Validation should succeed with valid certificate files"
    );
}

#[test]
#[cfg(unix)]
fn test_validate_key_file_secure_permissions_should_succeed() {
    // Create temporary key file
    let key_file = NamedTempFile::new().unwrap();

    let config = TlsConfig {
        enable_tls: true,
        generate_self_signed_certificates: false,
        server_private_key_path: key_file.path().to_string_lossy().to_string(),
        // We also need valid certificate files
        server_certificate_path: key_file.path().to_string_lossy().to_string(),
        certificate_authority_root_path: key_file.path().to_string_lossy().to_string(),
        ..Default::default()
    };

    let result = config.validate();
    assert!(
        result.is_ok(),
        "Validation should succeed with existing files (permissions are not checked in test mode)"
    );
}

#[test]
fn test_validate_cert_file_unreadable_should_fail() {
    // This test simulates the scenario where a file exists but cannot be read
    // In practice, this might happen due to permission issues
    // Note: The actual file reading is disabled in tests, so we'll test the error path
    // by using a path that exists but we can't control the readability in test mode

    // For this test, we'll just verify the error message format when file doesn't exist
    let config = TlsConfig {
        enable_tls: true,
        generate_self_signed_certificates: false,
        server_certificate_path: "/nonexistent/unreadable.pem".to_string(),
        ..Default::default()
    };

    let result = config.validate();
    assert!(
        result.is_err(),
        "Validation should fail for non-existent certificate file"
    );

    let error = result.unwrap_err();
    assert!(matches!(error, Error::Config(_)));
    assert!(error.to_string().contains("not found"));
}

#[test]
fn test_validate_complete_mtls_config_should_succeed() {
    // Create all necessary temporary files for complete mTLS configuration
    let server_cert = NamedTempFile::new().unwrap();
    let server_key = NamedTempFile::new().unwrap();
    let ca_cert = NamedTempFile::new().unwrap();
    let client_ca_cert = NamedTempFile::new().unwrap();

    let config = TlsConfig {
        enable_tls: true,
        enable_mtls: true,
        generate_self_signed_certificates: false,
        server_certificate_path: server_cert.path().to_string_lossy().to_string(),
        server_private_key_path: server_key.path().to_string_lossy().to_string(),
        certificate_authority_root_path: ca_cert.path().to_string_lossy().to_string(),
        client_certificate_authority_root_path: client_ca_cert.path().to_string_lossy().to_string(),
    };

    let result = config.validate();
    assert!(
        result.is_ok(),
        "Complete mTLS configuration should validate successfully"
    );
}

#[test]
fn test_validate_partial_tls_config_should_fail() {
    // Test with some files missing
    let server_cert = NamedTempFile::new().unwrap();

    let config = TlsConfig {
        enable_tls: true,
        generate_self_signed_certificates: false,
        server_certificate_path: server_cert.path().to_string_lossy().to_string(),
        // Missing server_private_key_path and certificate_authority_root_path
        ..Default::default()
    };

    let result = config.validate();
    assert!(
        result.is_err(),
        "Partial TLS configuration should fail validation"
    );

    let error = result.unwrap_err();
    assert!(matches!(error, Error::Config(_)));
    // Should complain about missing server private key or CA certificate
}

#[test]
fn test_validate_empty_paths_with_self_signed_should_succeed() {
    let config = TlsConfig {
        enable_tls: true,
        generate_self_signed_certificates: true,
        server_certificate_path: String::new(),
        server_private_key_path: String::new(),
        certificate_authority_root_path: String::new(),
        client_certificate_authority_root_path: String::new(),
        ..Default::default()
    };

    let result = config.validate();
    assert!(
        result.is_ok(),
        "Empty paths with self-signed generation should succeed"
    );
}