aboutsummaryrefslogtreecommitdiff
path: root/client/keys_test.go
blob: 6a6949238ddafad3d4aec00fd0cef63b2bcc8452 (plain)
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
57
58
59
60
61
62
63
64
65
66
67
68
package client

import (
	"crypto/rsa"
	"reflect"
	"testing"

	"golang.org/x/crypto/ed25519"
)

func TestGenerateKeys(t *testing.T) {
	var tests = []struct {
		keytype string
		keysize int
		want    string
	}{
		{"rsa", 1024, "*rsa.PrivateKey"},
		{"rsa", 0, "*rsa.PrivateKey"},
		{"ecdsa", 0, "*ecdsa.PrivateKey"},
		{"ecdsa", 384, "*ecdsa.PrivateKey"},
		{"ed25519", 0, "*ed25519.PrivateKey"},
	}

	for _, tst := range tests {
		var k Key
		var err error
		k, _, err = GenerateKey(KeyType(tst.keytype), KeySize(tst.keysize))
		if err != nil {
			t.Error(err)
		}
		if reflect.TypeOf(k).String() != tst.want {
			t.Errorf("Wrong key type returned. Got %T, wanted %s", k, tst.want)
		}
	}
}

func TestDefaultOptions(t *testing.T) {
	k, _, err := GenerateKey()
	if err != nil {
		t.Error(err)
	}
	_, ok := k.(*rsa.PrivateKey)
	if !ok {
		t.Errorf("Unexpected key type %T, wanted *rsa.PrivateKey", k)
	}
}

func TestGenerateKeyType(t *testing.T) {
	k, _, err := GenerateKey(KeyType("ed25519"))
	if err != nil {
		t.Error(err)
	}
	_, ok := k.(*ed25519.PrivateKey)
	if !ok {
		t.Errorf("Unexpected key type %T, wanted *ed25519.PrivateKey", k)
	}
}

func TestGenerateKeySize(t *testing.T) {
	k, _, err := GenerateKey(KeySize(1024))
	if err != nil {
		t.Error(err)
	}
	_, ok := k.(*rsa.PrivateKey)
	if !ok {
		t.Errorf("Unexpected key type %T, wanted *rsa.PrivateKey", k)
	}
}