-
Notifications
You must be signed in to change notification settings - Fork 0
/
keys_test.go
57 lines (48 loc) · 1.09 KB
/
keys_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
57
package sshutil
import (
"errors"
"testing"
)
type badReader struct{}
var errBadRand = errors.New("no entropy")
func (reader badReader) Read(b []byte) (n int, err error) {
return 0, errBadRand
}
type goodReader struct{}
func (reader goodReader) Read(b []byte) (n int, err error) {
emptyKey := make([]byte, len(b))
copy(b, emptyKey)
b[0] = byte('A')
return len(b), nil
}
func TestGenerateKeyRand_Bad(t *testing.T) {
s, err := GenerateKeyRand(badReader{})
if s != nil {
t.Error("expected nil signer")
}
if !errors.Is(err, errBadRand) {
t.Error("expected error")
}
}
func TestGenerateKeyRand_Okay(t *testing.T) {
s, err := GenerateKeyRand(goodReader{})
if err != nil {
t.Errorf("error generating key %v", err)
}
if s == nil {
t.Fatal("expected non-nil key")
}
if s.PublicKey().Type() != "ecdsa-sha2-nistp256" {
t.Error("expected 256 bit ecdsa key")
t.Errorf("got %s", s.PublicKey().Type())
}
}
func TestGenerateKey_Okay(t *testing.T) {
ssh, err := GenerateKey()
if err != nil {
t.Errorf("error generating key %v", err)
}
if ssh == nil {
t.Error("expected non-nil key")
}
}