git-crypt 0.1.4

A Rust implementation of git-crypt for transparent encryption of files in a git repository
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
//! # Edge Case and Error Handling Tests
//!
//! Tests corner cases and error conditions to ensure robustness.
//!
//! ## Test Coverage
//!
//! - **Large files**: 10MB file encryption/decryption
//! - **Empty files**: Zero-byte file handling
//! - **Binary data**: Files with null bytes and all byte values
//! - **Unicode**: International characters in filenames and content
//! - **Data corruption**: Tamper detection and authentication
//! - **File operations**: Overwriting existing files
//! - **Concurrency**: Thread-safe operations
//! - **Permissions**: Directory and file security (Unix)
//! - **Idempotency**: Lock/unlock repeated operations
//! - **Special characters**: Control characters and edge cases
//!
//! ## Security Tests
//!
//! These tests verify cryptographic properties:
//! - Corrupted data is detected and rejected
//! - Key isolation between repositories
//! - File permissions are properly restricted (0600 on Unix)
//!
//! ## Running Tests
//!
//! ```bash
//! # Run all edge case tests
//! cargo test --test edge_cases_test
//!
//! # Run with backtrace for debugging
//! RUST_BACKTRACE=1 cargo test --test edge_cases_test
//! ```

mod common;

use common::{create_git_repo, git_crypt_bin, git_crypt_cmd};
use std::fs;
use std::io::Write;
use std::process::{Command as StdCommand, Stdio};

#[test]
fn test_very_large_file_encryption() {
    let temp = create_git_repo();
    git_crypt_cmd()
        .arg("init")
        .current_dir(temp.path())
        .assert()
        .success();

    // Create 10MB file
    let large_data = vec![0x42u8; 10 * 1024 * 1024];

    let mut clean = StdCommand::new(git_crypt_bin())
        .arg("clean")
        .current_dir(temp.path())
        .stdin(Stdio::piped())
        .stdout(Stdio::piped())
        .spawn()
        .unwrap();

    clean
        .stdin
        .as_mut()
        .unwrap()
        .write_all(&large_data)
        .unwrap();
    let encrypted = clean.wait_with_output().unwrap();
    assert!(encrypted.status.success());

    // Decrypt back
    let mut smudge = StdCommand::new(git_crypt_bin())
        .arg("smudge")
        .current_dir(temp.path())
        .stdin(Stdio::piped())
        .stdout(Stdio::piped())
        .spawn()
        .unwrap();

    smudge
        .stdin
        .as_mut()
        .unwrap()
        .write_all(&encrypted.stdout)
        .unwrap();
    let decrypted = smudge.wait_with_output().unwrap();
    assert!(decrypted.status.success());
    assert_eq!(decrypted.stdout.len(), large_data.len());
}

#[test]
fn test_empty_file_encryption() {
    let temp = create_git_repo();
    git_crypt_cmd()
        .arg("init")
        .current_dir(temp.path())
        .assert()
        .success();

    let empty_data = b"";

    let mut clean = StdCommand::new(git_crypt_bin())
        .arg("clean")
        .current_dir(temp.path())
        .stdin(Stdio::piped())
        .stdout(Stdio::piped())
        .spawn()
        .unwrap();

    clean.stdin.as_mut().unwrap().write_all(empty_data).unwrap();
    let encrypted = clean.wait_with_output().unwrap();
    assert!(encrypted.status.success());

    let mut smudge = StdCommand::new(git_crypt_bin())
        .arg("smudge")
        .current_dir(temp.path())
        .stdin(Stdio::piped())
        .stdout(Stdio::piped())
        .spawn()
        .unwrap();

    smudge
        .stdin
        .as_mut()
        .unwrap()
        .write_all(&encrypted.stdout)
        .unwrap();
    let decrypted = smudge.wait_with_output().unwrap();
    assert!(decrypted.status.success());
    assert_eq!(&decrypted.stdout[..], empty_data);
}

#[test]
fn test_binary_file_with_null_bytes() {
    let temp = create_git_repo();
    git_crypt_cmd()
        .arg("init")
        .current_dir(temp.path())
        .assert()
        .success();

    let binary_data: Vec<u8> = vec![0x00, 0xFF, 0x00, 0x42, 0x00, 0x00, 0xAA, 0xBB];

    let mut clean = StdCommand::new(git_crypt_bin())
        .arg("clean")
        .current_dir(temp.path())
        .stdin(Stdio::piped())
        .stdout(Stdio::piped())
        .spawn()
        .unwrap();

    clean
        .stdin
        .as_mut()
        .unwrap()
        .write_all(&binary_data)
        .unwrap();
    let encrypted = clean.wait_with_output().unwrap();
    assert!(encrypted.status.success());

    let mut smudge = StdCommand::new(git_crypt_bin())
        .arg("smudge")
        .current_dir(temp.path())
        .stdin(Stdio::piped())
        .stdout(Stdio::piped())
        .spawn()
        .unwrap();

    smudge
        .stdin
        .as_mut()
        .unwrap()
        .write_all(&encrypted.stdout)
        .unwrap();
    let decrypted = smudge.wait_with_output().unwrap();
    assert!(decrypted.status.success());
    assert_eq!(decrypted.stdout, binary_data);
}

#[test]
fn test_unicode_filenames_and_content() {
    let temp = create_git_repo();
    git_crypt_cmd()
        .arg("init")
        .current_dir(temp.path())
        .assert()
        .success();

    // Use content that is less likely to have ASCII-heavy encrypted output
    let unicode_content = "Hello 世界! Emoji: 🔐🦀 Math: ∑∫∂ ".repeat(5); // Repeat to ensure sufficient length

    let mut clean = StdCommand::new(git_crypt_bin())
        .arg("clean")
        .current_dir(temp.path())
        .stdin(Stdio::piped())
        .stdout(Stdio::piped())
        .spawn()
        .unwrap();

    clean
        .stdin
        .as_mut()
        .unwrap()
        .write_all(unicode_content.as_bytes())
        .unwrap();
    let encrypted = clean.wait_with_output().unwrap();
    assert!(encrypted.status.success());

    let mut smudge = StdCommand::new(git_crypt_bin())
        .arg("smudge")
        .current_dir(temp.path())
        .stdin(Stdio::piped())
        .stdout(Stdio::piped())
        .spawn()
        .unwrap();

    smudge
        .stdin
        .as_mut()
        .unwrap()
        .write_all(&encrypted.stdout)
        .unwrap();
    let decrypted = smudge.wait_with_output().unwrap();
    assert!(decrypted.status.success());
    assert_eq!(
        String::from_utf8(decrypted.stdout).unwrap(),
        unicode_content
    );
}

#[test]
fn test_corrupted_encrypted_data() {
    let temp = create_git_repo();
    git_crypt_cmd()
        .arg("init")
        .current_dir(temp.path())
        .assert()
        .success();

    let plaintext = b"Secret message";

    // Encrypt
    let mut clean = StdCommand::new(git_crypt_bin())
        .arg("clean")
        .current_dir(temp.path())
        .stdin(Stdio::piped())
        .stdout(Stdio::piped())
        .spawn()
        .unwrap();

    clean.stdin.as_mut().unwrap().write_all(plaintext).unwrap();
    let encrypted = clean.wait_with_output().unwrap();
    assert!(encrypted.status.success());

    // Corrupt the encrypted data
    let mut corrupted = encrypted.stdout.clone();
    if corrupted.len() > 15 {
        corrupted[15] ^= 0xFF;
    }

    // Try to decrypt corrupted data
    let mut smudge = StdCommand::new(git_crypt_bin())
        .arg("smudge")
        .current_dir(temp.path())
        .stdin(Stdio::piped())
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .spawn()
        .unwrap();

    smudge
        .stdin
        .as_mut()
        .unwrap()
        .write_all(&corrupted)
        .unwrap();
    let output = smudge.wait_with_output().unwrap();

    // Should fail
    assert!(!output.status.success());
}

#[test]
fn test_export_key_to_existing_file() {
    let temp = create_git_repo();
    git_crypt_cmd()
        .arg("init")
        .current_dir(temp.path())
        .assert()
        .success();

    let key_file = temp.path().join("key.bin");

    // First export
    git_crypt_cmd()
        .args(["export-key", key_file.to_str().unwrap()])
        .current_dir(temp.path())
        .assert()
        .success();

    let first_key = fs::read(&key_file).unwrap();

    // Export again (should overwrite)
    git_crypt_cmd()
        .args(["export-key", key_file.to_str().unwrap()])
        .current_dir(temp.path())
        .assert()
        .success();

    let second_key = fs::read(&key_file).unwrap();

    // Should be the same key
    assert_eq!(first_key, second_key);
}

#[test]
fn test_invalid_command() {
    git_crypt_cmd().arg("invalid-command").assert().failure();
}

#[test]
fn test_concurrent_operations() {
    use std::thread;

    let temp = create_git_repo();
    git_crypt_cmd()
        .arg("init")
        .current_dir(temp.path())
        .assert()
        .success();

    let temp_path = temp.path().to_path_buf();

    // Spawn multiple threads doing encryption
    let handles: Vec<_> = (0..5)
        .map(|i| {
            let path = temp_path.clone();
            thread::spawn(move || {
                let data = format!("Thread {} data", i);

                let mut clean = StdCommand::new(git_crypt_bin())
                    .arg("clean")
                    .current_dir(&path)
                    .stdin(Stdio::piped())
                    .stdout(Stdio::piped())
                    .spawn()
                    .unwrap();

                clean
                    .stdin
                    .as_mut()
                    .unwrap()
                    .write_all(data.as_bytes())
                    .unwrap();
                let output = clean.wait_with_output().unwrap();

                assert!(output.status.success());
            })
        })
        .collect();

    // Wait for all threads
    for handle in handles {
        handle.join().unwrap();
    }
}

#[test]
fn test_key_with_directory_permissions() {
    let temp = create_git_repo();
    git_crypt_cmd()
        .arg("init")
        .current_dir(temp.path())
        .assert()
        .success();

    let git_crypt_dir = temp.path().join(".git/git-crypt");
    let keys_dir = git_crypt_dir.join("keys");

    assert!(git_crypt_dir.is_dir());
    assert!(keys_dir.is_dir());
}

#[test]
fn test_lock_unlock_idempotent() {
    let temp = create_git_repo();
    git_crypt_cmd()
        .arg("init")
        .current_dir(temp.path())
        .assert()
        .success();

    // Lock multiple times
    for _ in 0..3 {
        git_crypt_cmd()
            .arg("lock")
            .current_dir(temp.path())
            .assert()
            .success();
    }

    // Unlock multiple times
    for _ in 0..3 {
        git_crypt_cmd()
            .arg("unlock")
            .current_dir(temp.path())
            .assert()
            .success();
    }
}

#[test]
fn test_special_characters_in_data() {
    let temp = create_git_repo();
    git_crypt_cmd()
        .arg("init")
        .current_dir(temp.path())
        .assert()
        .success();

    // Data with special characters
    let special_data = b"Line1\nLine2\r\nTab\there\0null\x01\x02\x03\xFF";

    let mut clean = StdCommand::new(git_crypt_bin())
        .arg("clean")
        .current_dir(temp.path())
        .stdin(Stdio::piped())
        .stdout(Stdio::piped())
        .spawn()
        .unwrap();

    clean
        .stdin
        .as_mut()
        .unwrap()
        .write_all(special_data)
        .unwrap();
    let encrypted = clean.wait_with_output().unwrap();
    assert!(encrypted.status.success());

    let mut smudge = StdCommand::new(git_crypt_bin())
        .arg("smudge")
        .current_dir(temp.path())
        .stdin(Stdio::piped())
        .stdout(Stdio::piped())
        .spawn()
        .unwrap();

    smudge
        .stdin
        .as_mut()
        .unwrap()
        .write_all(&encrypted.stdout)
        .unwrap();
    let decrypted = smudge.wait_with_output().unwrap();
    assert!(decrypted.status.success());
    assert_eq!(&decrypted.stdout[..], special_data);
}

#[test]
fn test_repeated_key_operations() {
    let temp = create_git_repo();
    git_crypt_cmd()
        .arg("init")
        .current_dir(temp.path())
        .assert()
        .success();

    let key_file = temp.path().join("key.bin");

    // Export and import repeatedly
    for _ in 0..5 {
        git_crypt_cmd()
            .args(["export-key", key_file.to_str().unwrap()])
            .current_dir(temp.path())
            .assert()
            .success();

        git_crypt_cmd()
            .args(["import-key", key_file.to_str().unwrap()])
            .current_dir(temp.path())
            .assert()
            .success();
    }

    // Key should still work
    let plaintext = b"Test after repeated operations";

    let mut clean = StdCommand::new(git_crypt_bin())
        .arg("clean")
        .current_dir(temp.path())
        .stdin(Stdio::piped())
        .stdout(Stdio::piped())
        .spawn()
        .unwrap();

    clean.stdin.as_mut().unwrap().write_all(plaintext).unwrap();
    let encrypted = clean.wait_with_output().unwrap();
    assert!(encrypted.status.success());
}