@@ -0,0 +1,144 @@ |
| 1 | +// SPDX-License-Identifier: AGPL-3.0-or-later |
| 2 | + |
| 3 | +package sshkey |
| 4 | + |
| 5 | +import ( |
| 6 | + "errors" |
| 7 | + "os" |
| 8 | + "os/exec" |
| 9 | + "path/filepath" |
| 10 | + "strings" |
| 11 | + "testing" |
| 12 | +) |
| 13 | + |
| 14 | +func mustRead(t *testing.T, name string) string { |
| 15 | + t.Helper() |
| 16 | + b, err := os.ReadFile(filepath.Join("testdata", name)) |
| 17 | + if err != nil { |
| 18 | + t.Fatalf("read %s: %v", name, err) |
| 19 | + } |
| 20 | + return string(b) |
| 21 | +} |
| 22 | + |
| 23 | +func TestParse_AcceptsEd25519(t *testing.T) { |
| 24 | + t.Parallel() |
| 25 | + pub := mustRead(t, "ed25519.pub") |
| 26 | + got, err := Parse("my laptop", pub) |
| 27 | + if err != nil { |
| 28 | + t.Fatalf("Parse: %v", err) |
| 29 | + } |
| 30 | + if got.Type != "ssh-ed25519" { |
| 31 | + t.Fatalf("type = %q", got.Type) |
| 32 | + } |
| 33 | + if !strings.HasPrefix(got.Fingerprint, "SHA256:") { |
| 34 | + t.Fatalf("fingerprint shape: %q", got.Fingerprint) |
| 35 | + } |
| 36 | + // Cross-check against ssh-keygen if available — defensive. |
| 37 | + if _, err := exec.LookPath("ssh-keygen"); err == nil { |
| 38 | + out, err := exec.Command("ssh-keygen", "-E", "sha256", "-lf", |
| 39 | + filepath.Join("testdata", "ed25519.pub")).Output() |
| 40 | + if err != nil { |
| 41 | + t.Fatalf("ssh-keygen: %v", err) |
| 42 | + } |
| 43 | + if !strings.Contains(string(out), got.Fingerprint) { |
| 44 | + t.Fatalf("fingerprint mismatch with ssh-keygen: %q vs %s", |
| 45 | + got.Fingerprint, out) |
| 46 | + } |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +func TestParse_AcceptsRSA2048(t *testing.T) { |
| 51 | + t.Parallel() |
| 52 | + pub := mustRead(t, "rsa2048.pub") |
| 53 | + got, err := Parse("ci runner", pub) |
| 54 | + if err != nil { |
| 55 | + t.Fatalf("Parse: %v", err) |
| 56 | + } |
| 57 | + if got.Type != "ssh-rsa" { |
| 58 | + t.Fatalf("type = %q", got.Type) |
| 59 | + } |
| 60 | + if got.Bits < 2048 { |
| 61 | + t.Fatalf("bits = %d", got.Bits) |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +func TestParse_AcceptsECDSA(t *testing.T) { |
| 66 | + t.Parallel() |
| 67 | + pub := mustRead(t, "ecdsa256.pub") |
| 68 | + got, err := Parse("hsm", pub) |
| 69 | + if err != nil { |
| 70 | + t.Fatalf("Parse: %v", err) |
| 71 | + } |
| 72 | + if got.Type != "ecdsa-sha2-nistp256" { |
| 73 | + t.Fatalf("type = %q", got.Type) |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +func TestParse_RejectsRSA1024(t *testing.T) { |
| 78 | + t.Parallel() |
| 79 | + pub := mustRead(t, "rsa1024.pub") |
| 80 | + _, err := Parse("weak", pub) |
| 81 | + if !errors.Is(err, ErrRSATooShort) { |
| 82 | + t.Fatalf("expected ErrRSATooShort, got %v", err) |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +func TestParse_RejectsUnparseable(t *testing.T) { |
| 87 | + t.Parallel() |
| 88 | + cases := []string{ |
| 89 | + "", |
| 90 | + "not-a-key", |
| 91 | + "ssh-rsa AAAA broken", |
| 92 | + "ssh-dss AAAAB3NzaC1kc3MAAACB", |
| 93 | + } |
| 94 | + for _, in := range cases { |
| 95 | + _, err := Parse("title", in) |
| 96 | + if err == nil { |
| 97 | + t.Errorf("expected error for %q", in) |
| 98 | + } |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +func TestParse_RejectsEmptyTitle(t *testing.T) { |
| 103 | + t.Parallel() |
| 104 | + pub := mustRead(t, "ed25519.pub") |
| 105 | + _, err := Parse(" ", pub) |
| 106 | + if !errors.Is(err, ErrTitleEmpty) { |
| 107 | + t.Fatalf("expected ErrTitleEmpty, got %v", err) |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +func TestParse_RejectsLongTitle(t *testing.T) { |
| 112 | + t.Parallel() |
| 113 | + pub := mustRead(t, "ed25519.pub") |
| 114 | + _, err := Parse(strings.Repeat("a", 81), pub) |
| 115 | + if !errors.Is(err, ErrTitleTooLong) { |
| 116 | + t.Fatalf("expected ErrTitleTooLong, got %v", err) |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +func TestParse_RejectsControlCharsInTitle(t *testing.T) { |
| 121 | + t.Parallel() |
| 122 | + pub := mustRead(t, "ed25519.pub") |
| 123 | + _, err := Parse("oops\nnewline", pub) |
| 124 | + if !errors.Is(err, ErrTitleControl) { |
| 125 | + t.Fatalf("expected ErrTitleControl, got %v", err) |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +func TestParse_CanonicalizesPublicKey(t *testing.T) { |
| 130 | + t.Parallel() |
| 131 | + pub := mustRead(t, "ed25519.pub") |
| 132 | + got, _ := Parse("my laptop", pub) |
| 133 | + // No trailing newline, no comment. |
| 134 | + if strings.HasSuffix(got.PublicKey, "\n") { |
| 135 | + t.Fatal("canonical key has trailing newline") |
| 136 | + } |
| 137 | + if strings.Contains(got.PublicKey, "fixture-ed25519") { |
| 138 | + t.Fatal("canonical key includes original comment") |
| 139 | + } |
| 140 | + parts := strings.Fields(got.PublicKey) |
| 141 | + if len(parts) != 2 { |
| 142 | + t.Fatalf("canonical key should be 'algo b64', got %q", got.PublicKey) |
| 143 | + } |
| 144 | +} |