|
1 | 1 | package sshkeys |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "context" |
4 | 5 | "encoding/pem" |
5 | 6 | "testing" |
6 | 7 |
|
@@ -88,3 +89,73 @@ func TestPrivateKey_IsEncrypted(t *testing.T) { |
88 | 89 | }) |
89 | 90 | } |
90 | 91 | } |
| 92 | + |
| 93 | +func TestPrivateKey_signer(t *testing.T) { |
| 94 | + const encryptedFixturePassphrase = "pipomolo" |
| 95 | + const macOSYosemitePassphrase = "scalingo" |
| 96 | + const longPassphrase = "TrushorbifbixMytyubDaphnacFoldacdeirnAykEtHyacobuc" |
| 97 | + |
| 98 | + tests := map[string]struct { |
| 99 | + block *pem.Block |
| 100 | + passphrase string |
| 101 | + signerType string |
| 102 | + }{ |
| 103 | + "Encrypted AES RSA Key": { |
| 104 | + block: pemDecode(aesRSA), |
| 105 | + passphrase: encryptedFixturePassphrase, |
| 106 | + signerType: "ssh-rsa", |
| 107 | + }, |
| 108 | + "Encrypted AES RSA Key (Mac OS Maverick)": { |
| 109 | + block: pemDecode(aesRSAMacOSMaverick), |
| 110 | + passphrase: encryptedFixturePassphrase, |
| 111 | + signerType: "ssh-rsa", |
| 112 | + }, |
| 113 | + "Encrypted AES RSA Key (Mac OS Yosemite)": { |
| 114 | + block: pemDecode(aesRSAMacOSYosemite), |
| 115 | + passphrase: macOSYosemitePassphrase, |
| 116 | + signerType: "ssh-rsa", |
| 117 | + }, |
| 118 | + "Encrypted DES3 RSA Key": { |
| 119 | + block: pemDecode(des3RSA), |
| 120 | + passphrase: encryptedFixturePassphrase, |
| 121 | + signerType: "ssh-rsa", |
| 122 | + }, |
| 123 | + "Encrypted DES3 RSA Key with Long Passphrase": { |
| 124 | + block: pemDecode(des3RSALongPassphrase), |
| 125 | + passphrase: longPassphrase, |
| 126 | + signerType: "ssh-rsa", |
| 127 | + }, |
| 128 | + "Encrypted OpenSSH RSA Key": { |
| 129 | + block: pemDecode(openSSHRSA), |
| 130 | + passphrase: encryptedFixturePassphrase, |
| 131 | + signerType: "ssh-rsa", |
| 132 | + }, |
| 133 | + "Encrypted OpenSSH ecdsa Key": { |
| 134 | + block: pemDecode(openSSHecdsa), |
| 135 | + passphrase: encryptedFixturePassphrase, |
| 136 | + signerType: "ecdsa-sha2-nistp256", |
| 137 | + }, |
| 138 | + "Encrypted OpenSSH ed25519 Key": { |
| 139 | + block: pemDecode(openSSHed25519), |
| 140 | + passphrase: encryptedFixturePassphrase, |
| 141 | + signerType: "ssh-ed25519", |
| 142 | + }, |
| 143 | + } |
| 144 | + |
| 145 | + for name, test := range tests { |
| 146 | + t.Run(name, func(t *testing.T) { |
| 147 | + privateKey := &PrivateKey{ |
| 148 | + Path: "n/a", |
| 149 | + Block: test.block, |
| 150 | + PasswordMethod: func(ctx context.Context, prompt string) (string, error) { |
| 151 | + return test.passphrase, nil |
| 152 | + }, |
| 153 | + } |
| 154 | + |
| 155 | + signer, err := privateKey.signer(t.Context()) |
| 156 | + require.NoError(t, err) |
| 157 | + require.NotNil(t, signer) |
| 158 | + require.Equal(t, test.signerType, signer.PublicKey().Type()) |
| 159 | + }) |
| 160 | + } |
| 161 | +} |
0 commit comments