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
//! # Integration Tests
//!
//! This module contains integration tests for git-crypt, testing complete workflows
//! and end-to-end scenarios.
//!
//! ## Test Coverage
//!
//! - Repository initialization and setup
//! - Key export and import workflows
//! - Lock and unlock cycles
//! - CLI commands and error handling
//! - Multi-repository key isolation
//! - Full encryption/decryption workflows
//!
//! ## Running Tests
//!
//! ```bash
//! # Run all integration tests
//! cargo test --test integration_test
//!
//! # Run specific test
//! cargo test --test integration_test test_init_command
//!
//! # Run with output
//! cargo test --test integration_test -- --nocapture
//! ```

mod common;

use common::{create_git_repo, git_crypt_cmd};
use predicates::prelude::*;
use std::fs;
use std::process::Command as StdCommand;
use tempfile::TempDir;

#[test]
fn test_init_command() {
    let temp = create_git_repo();

    let mut cmd = git_crypt_cmd();
    cmd.arg("init")
        .current_dir(temp.path())
        .assert()
        .success()
        .stdout(predicate::str::contains("Initializing git-crypt"))
        .stdout(predicate::str::contains("Generated new encryption key"));

    // Verify directory structure was created
    assert!(temp.path().join(".git/git-crypt").exists());
    assert!(temp.path().join(".git/git-crypt/keys").exists());
    assert!(temp.path().join(".git/git-crypt/keys/default").exists());
}

#[test]
fn test_init_twice_succeeds_but_warns() {
    let temp = create_git_repo();

    // First init
    git_crypt_cmd()
        .arg("init")
        .current_dir(temp.path())
        .assert()
        .success();

    // Second init
    git_crypt_cmd()
        .arg("init")
        .current_dir(temp.path())
        .assert()
        .success()
        .stdout(predicate::str::contains("already initialized"));
}

#[test]
fn test_init_outside_git_repo_fails() {
    let temp = TempDir::new().unwrap();

    git_crypt_cmd()
        .arg("init")
        .current_dir(temp.path())
        .assert()
        .failure()
        .stderr(
            predicate::str::contains("not in a git repository")
                .or(predicate::str::contains("Not in a git repository")),
        );
}

#[test]
fn test_export_and_import_key() {
    let temp = create_git_repo();

    // Initialize
    git_crypt_cmd()
        .arg("init")
        .current_dir(temp.path())
        .assert()
        .success();

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

    // Export key
    git_crypt_cmd()
        .args(["export-key", key_file.to_str().unwrap()])
        .current_dir(temp.path())
        .assert()
        .success()
        .stdout(predicate::str::contains("Key exported successfully"));

    assert!(key_file.exists());
    assert_eq!(fs::metadata(&key_file).unwrap().len(), 32); // 256 bits

    // Create second repo and import
    let temp2 = create_git_repo();

    git_crypt_cmd()
        .arg("init")
        .current_dir(temp2.path())
        .assert()
        .success();

    git_crypt_cmd()
        .args(["import-key", key_file.to_str().unwrap()])
        .current_dir(temp2.path())
        .assert()
        .success()
        .stdout(predicate::str::contains("Key imported successfully"));
}

#[test]
fn test_export_key_before_init_fails() {
    let temp = create_git_repo();
    let key_file = temp.path().join("exported.key");

    git_crypt_cmd()
        .args(["export-key", key_file.to_str().unwrap()])
        .current_dir(temp.path())
        .assert()
        .failure()
        .stderr(predicate::str::contains("not initialized"));
}

#[test]
fn test_unlock_with_key_file() {
    let temp = create_git_repo();

    // Initialize and export key
    git_crypt_cmd()
        .arg("init")
        .current_dir(temp.path())
        .assert()
        .success();

    let key_file = temp.path().join("key.bin");
    git_crypt_cmd()
        .args(["export-key", key_file.to_str().unwrap()])
        .current_dir(temp.path())
        .assert()
        .success();

    // Lock the repo
    git_crypt_cmd()
        .arg("lock")
        .current_dir(temp.path())
        .assert()
        .success();

    // Unlock with key file
    git_crypt_cmd()
        .args(["unlock", "--key-file", key_file.to_str().unwrap()])
        .current_dir(temp.path())
        .assert()
        .success()
        .stdout(predicate::str::contains("Repository unlocked successfully"));
}

#[test]
fn test_lock_and_unlock() {
    let temp = create_git_repo();

    // Initialize
    git_crypt_cmd()
        .arg("init")
        .current_dir(temp.path())
        .assert()
        .success();

    // Lock
    git_crypt_cmd()
        .arg("lock")
        .current_dir(temp.path())
        .assert()
        .success()
        .stdout(predicate::str::contains("Repository locked"));

    // Unlock
    git_crypt_cmd()
        .arg("unlock")
        .current_dir(temp.path())
        .assert()
        .success()
        .stdout(predicate::str::contains("Repository unlocked"));
}

#[test]
fn test_help_command() {
    git_crypt_cmd()
        .arg("--help")
        .assert()
        .success()
        .stdout(predicate::str::contains("Transparent file encryption"))
        .stdout(predicate::str::contains("init"))
        .stdout(predicate::str::contains("unlock"))
        .stdout(predicate::str::contains("lock"));
}

#[test]
fn test_version_command() {
    git_crypt_cmd()
        .arg("--version")
        .assert()
        .success()
        .stdout(predicate::str::contains("0.1.0"));
}

#[test]
fn test_add_gpg_user_without_gpg_feature() {
    let temp = create_git_repo();

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

    // Without GPG feature, this should fail with appropriate message
    let result = git_crypt_cmd()
        .args(["add-gpg-user", "test@example.com"])
        .current_dir(temp.path())
        .assert();

    // Should fail (either not implemented or feature not enabled)
    if cfg!(not(feature = "gpg")) {
        result.failure();
    }
}

#[test]
fn test_full_workflow_with_encryption() {
    let temp = create_git_repo();

    // 1. Initialize git-crypt
    git_crypt_cmd()
        .arg("init")
        .current_dir(temp.path())
        .assert()
        .success();

    // 2. Create .gitattributes
    let gitattributes = temp.path().join(".gitattributes");
    fs::write(&gitattributes, "*.secret filter=git-crypt diff=git-crypt\n").unwrap();

    // 3. Add and commit .gitattributes
    StdCommand::new("git")
        .args(["add", ".gitattributes"])
        .current_dir(temp.path())
        .output()
        .unwrap();

    StdCommand::new("git")
        .args(["commit", "-m", "Add gitattributes"])
        .current_dir(temp.path())
        .output()
        .unwrap();

    // 4. Create a secret file
    let secret_file = temp.path().join("test.secret");
    fs::write(&secret_file, b"my secret data").unwrap();

    // Note: Actually testing the git filters would require the binary to be in PATH
    // and proper git filter setup, which is complex in integration tests.
    // The filters are better tested with manual testing or more complex test setup.

    // 5. Export key for sharing
    let key_file = temp.path().join("shared.key");
    git_crypt_cmd()
        .args(["export-key", key_file.to_str().unwrap()])
        .current_dir(temp.path())
        .assert()
        .success();

    assert!(key_file.exists());
}

#[test]
fn test_status_command_placeholder() {
    let temp = create_git_repo();

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

    git_crypt_cmd()
        .arg("status")
        .current_dir(temp.path())
        .assert()
        .success()
        .stdout(predicate::str::contains("not yet implemented"));
}

#[test]
fn test_import_invalid_key_file() {
    let temp = create_git_repo();

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

    // Create invalid key file
    let invalid_key = temp.path().join("invalid.key");
    fs::write(&invalid_key, b"not a valid key").unwrap();

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

#[test]
fn test_key_file_is_32_bytes() {
    let temp = create_git_repo();

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

    let key_path = temp.path().join(".git/git-crypt/keys/default");
    let metadata = fs::metadata(&key_path).unwrap();

    // Key should be exactly 32 bytes (256 bits)
    assert_eq!(metadata.len(), 32);
}

#[cfg(unix)]
#[test]
fn test_key_file_permissions() {
    use std::os::unix::fs::PermissionsExt;

    let temp = create_git_repo();

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

    let key_path = temp.path().join(".git/git-crypt/keys/default");
    let metadata = fs::metadata(&key_path).unwrap();
    let permissions = metadata.permissions();

    // Should be 0600 (owner read/write only)
    assert_eq!(permissions.mode() & 0o777, 0o600);
}

#[test]
fn test_multiple_repos_independent_keys() {
    let temp1 = create_git_repo();
    let temp2 = create_git_repo();

    // Initialize both repos
    git_crypt_cmd()
        .arg("init")
        .current_dir(temp1.path())
        .assert()
        .success();

    git_crypt_cmd()
        .arg("init")
        .current_dir(temp2.path())
        .assert()
        .success();

    // Export keys
    let key1 = temp1.path().join("key1.bin");
    let key2 = temp2.path().join("key2.bin");

    git_crypt_cmd()
        .args(["export-key", key1.to_str().unwrap()])
        .current_dir(temp1.path())
        .assert()
        .success();

    git_crypt_cmd()
        .args(["export-key", key2.to_str().unwrap()])
        .current_dir(temp2.path())
        .assert()
        .success();

    // Keys should be different
    let key1_bytes = fs::read(&key1).unwrap();
    let key2_bytes = fs::read(&key2).unwrap();

    assert_ne!(key1_bytes, key2_bytes);
}