|
| 1 | +// SPDX-License-Identifier: AGPL-3.0-or-later |
| 2 | + |
| 3 | +package main |
| 4 | + |
| 5 | +import ( |
| 6 | + "os" |
| 7 | + "path/filepath" |
| 8 | + "strings" |
| 9 | + "testing" |
| 10 | +) |
| 11 | + |
| 12 | +// TestBackupIdentityNoFile: nothing to back up when the path is absent. |
| 13 | +// Returns ("", nil) so the caller proceeds to write a fresh identity. |
| 14 | +func TestBackupIdentityNoFile(t *testing.T) { |
| 15 | + dir := t.TempDir() |
| 16 | + path := filepath.Join(dir, "identity.json") |
| 17 | + |
| 18 | + bak, err := backupIdentity(path) |
| 19 | + if err != nil { |
| 20 | + t.Fatalf("backupIdentity on missing file: unexpected error %v", err) |
| 21 | + } |
| 22 | + if bak != "" { |
| 23 | + t.Fatalf("expected empty backup path for missing file, got %q", bak) |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +// TestBackupIdentityCopiesExisting: an existing identity is copied to a |
| 28 | +// timestamped .bak file with its contents preserved, and the original is |
| 29 | +// left in place for SaveIdentity to overwrite. |
| 30 | +func TestBackupIdentityCopiesExisting(t *testing.T) { |
| 31 | + dir := t.TempDir() |
| 32 | + path := filepath.Join(dir, "identity.json") |
| 33 | + const content = `{"node_id":42,"private_key":"old"}` |
| 34 | + if err := os.WriteFile(path, []byte(content), 0o600); err != nil { |
| 35 | + t.Fatalf("seed identity: %v", err) |
| 36 | + } |
| 37 | + |
| 38 | + bak, err := backupIdentity(path) |
| 39 | + if err != nil { |
| 40 | + t.Fatalf("backupIdentity: unexpected error %v", err) |
| 41 | + } |
| 42 | + if bak == "" { |
| 43 | + t.Fatal("expected a backup path, got empty") |
| 44 | + } |
| 45 | + if !strings.HasPrefix(bak, path+".bak-") { |
| 46 | + t.Fatalf("backup path %q lacks expected prefix %q.bak-", bak, path) |
| 47 | + } |
| 48 | + |
| 49 | + got, err := os.ReadFile(bak) |
| 50 | + if err != nil { |
| 51 | + t.Fatalf("read backup: %v", err) |
| 52 | + } |
| 53 | + if string(got) != content { |
| 54 | + t.Fatalf("backup content mismatch: got %q want %q", got, content) |
| 55 | + } |
| 56 | + |
| 57 | + // Original must still exist (we copy, not move) so the overwrite path |
| 58 | + // has something to replace. |
| 59 | + if _, err := os.Stat(path); err != nil { |
| 60 | + t.Fatalf("original identity should remain: %v", err) |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +// TestBackupIdentityRefusesUnwritable: when a file exists but the backup |
| 65 | +// cannot be written, an error is returned so the caller refuses to |
| 66 | +// overwrite the live identity blind. |
| 67 | +func TestBackupIdentityRefusesUnwritable(t *testing.T) { |
| 68 | + dir := t.TempDir() |
| 69 | + path := filepath.Join(dir, "identity.json") |
| 70 | + if err := os.WriteFile(path, []byte("x"), 0o600); err != nil { |
| 71 | + t.Fatalf("seed identity: %v", err) |
| 72 | + } |
| 73 | + // Make the directory read-only so the .bak sibling can't be created. |
| 74 | + if err := os.Chmod(dir, 0o500); err != nil { |
| 75 | + t.Fatalf("chmod dir: %v", err) |
| 76 | + } |
| 77 | + t.Cleanup(func() { _ = os.Chmod(dir, 0o700) }) |
| 78 | + |
| 79 | + if os.Geteuid() == 0 { |
| 80 | + t.Skip("running as root bypasses directory permissions") |
| 81 | + } |
| 82 | + |
| 83 | + if _, err := backupIdentity(path); err == nil { |
| 84 | + t.Fatal("expected error when backup cannot be written, got nil") |
| 85 | + } |
| 86 | +} |
0 commit comments