|
| 1 | +package acquisition |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "os" |
| 6 | + "path/filepath" |
| 7 | + "testing" |
| 8 | +) |
| 9 | + |
| 10 | +func TestStoreInfoSetsCompletedTimestamp(t *testing.T) { |
| 11 | + acq := &Acquisition{ |
| 12 | + UUID: "test-acquisition", |
| 13 | + StoragePath: t.TempDir(), |
| 14 | + } |
| 15 | + |
| 16 | + if err := acq.StoreInfo(); err != nil { |
| 17 | + t.Fatalf("StoreInfo() error = %v", err) |
| 18 | + } |
| 19 | + |
| 20 | + if acq.Completed.IsZero() { |
| 21 | + t.Fatal("StoreInfo() left Completed unset") |
| 22 | + } |
| 23 | + |
| 24 | + info, err := os.ReadFile(filepath.Join(acq.StoragePath, "acquisition.json")) |
| 25 | + if err != nil { |
| 26 | + t.Fatalf("ReadFile(acquisition.json) error = %v", err) |
| 27 | + } |
| 28 | + |
| 29 | + var stored Acquisition |
| 30 | + if err := json.Unmarshal(info, &stored); err != nil { |
| 31 | + t.Fatalf("json.Unmarshal(acquisition.json) error = %v", err) |
| 32 | + } |
| 33 | + if stored.Completed.IsZero() { |
| 34 | + t.Fatal("acquisition.json contains a zero completed timestamp") |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +func TestCompleteDoesNotOverwriteExistingCompletedTimestamp(t *testing.T) { |
| 39 | + acq := &Acquisition{ |
| 40 | + UUID: "test-acquisition", |
| 41 | + StoragePath: t.TempDir(), |
| 42 | + } |
| 43 | + |
| 44 | + if err := acq.StoreInfo(); err != nil { |
| 45 | + t.Fatalf("StoreInfo() error = %v", err) |
| 46 | + } |
| 47 | + completed := acq.Completed |
| 48 | + |
| 49 | + acq.Complete() |
| 50 | + |
| 51 | + if !acq.Completed.Equal(completed) { |
| 52 | + t.Fatalf("Complete() changed Completed from %s to %s", completed, acq.Completed) |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +func TestStoreSecurelyUsesCurrentWorkingDirectory(t *testing.T) { |
| 57 | + cwd := t.TempDir() |
| 58 | + t.Chdir(cwd) |
| 59 | + writeTestAgeKey(t, cwd) |
| 60 | + |
| 61 | + storagePath := filepath.Join(cwd, "test-acquisition") |
| 62 | + if err := os.Mkdir(storagePath, 0o755); err != nil { |
| 63 | + t.Fatalf("Mkdir(storagePath) error = %v", err) |
| 64 | + } |
| 65 | + if err := os.WriteFile(filepath.Join(storagePath, "data.txt"), []byte("evidence"), 0o600); err != nil { |
| 66 | + t.Fatalf("WriteFile(data.txt) error = %v", err) |
| 67 | + } |
| 68 | + |
| 69 | + acq := &Acquisition{ |
| 70 | + UUID: "test-acquisition", |
| 71 | + StoragePath: storagePath, |
| 72 | + } |
| 73 | + |
| 74 | + if err := acq.StoreSecurely(); err != nil { |
| 75 | + t.Fatalf("StoreSecurely() error = %v", err) |
| 76 | + } |
| 77 | + |
| 78 | + wantPath := filepath.Join(cwd, "test-acquisition.zip.age") |
| 79 | + if _, err := os.Stat(wantPath); err != nil { |
| 80 | + t.Fatalf("Stat(encrypted output) error = %v", err) |
| 81 | + } |
| 82 | + if _, err := os.Stat(storagePath); !os.IsNotExist(err) { |
| 83 | + t.Fatalf("storage path still exists or returned unexpected error: %v", err) |
| 84 | + } |
| 85 | +} |
0 commit comments