-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
/
Copy pathssh_test.go
56 lines (48 loc) · 1.12 KB
/
ssh_test.go
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
// Copyright 2025 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package ssh_test
import (
"crypto/ecdsa"
"crypto/ed25519"
"crypto/rsa"
"io"
"os"
"path/filepath"
"testing"
"code.gitea.io/gitea/modules/ssh"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
gossh "golang.org/x/crypto/ssh"
)
func TestGenKeyPair(t *testing.T) {
testCases := []struct {
keyPath string
expectedType any
}{
{
keyPath: "/gitea.rsa",
expectedType: &rsa.PrivateKey{},
},
{
keyPath: "/gitea.ed25519",
expectedType: &ed25519.PrivateKey{},
},
{
keyPath: "/gitea.ecdsa",
expectedType: &ecdsa.PrivateKey{},
},
}
for _, tC := range testCases {
t.Run("Generate "+filepath.Ext(tC.keyPath), func(t *testing.T) {
path := t.TempDir() + tC.keyPath
require.NoError(t, ssh.GenKeyPair(path))
file, err := os.Open(path)
require.NoError(t, err)
bytes, err := io.ReadAll(file)
require.NoError(t, err)
privateKey, err := gossh.ParseRawPrivateKey(bytes)
require.NoError(t, err)
assert.IsType(t, tC.expectedType, privateKey)
})
}
}