forked from ipfs/go-ipfs-api
-
Notifications
You must be signed in to change notification settings - Fork 4
/
key_test.go
116 lines (93 loc) · 2.67 KB
/
key_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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package shell
import (
"context"
"testing"
"github.com/cheekybits/is"
)
func TestKeyGen(t *testing.T) {
is := is.New(t)
s := NewShell(shellUrl)
defer func() {
_, err := s.KeyRm(context.Background(), "testKey1")
is.Nil(err)
}()
key1, err := s.KeyGen(context.Background(), "testKey1", KeyGen.Type("ed25519"))
is.Nil(err)
is.Equal(key1.Name, "testKey1")
is.NotNil(key1.Id)
defer func() {
_, err = s.KeyRm(context.Background(), "testKey2")
is.Nil(err)
}()
key2, err := s.KeyGen(context.Background(), "testKey2", KeyGen.Type("ed25519"))
is.Nil(err)
is.Equal(key2.Name, "testKey2")
is.NotNil(key2.Id)
defer func() {
_, err = s.KeyRm(context.Background(), "testKey3")
is.Nil(err)
}()
key3, err := s.KeyGen(context.Background(), "testKey3", KeyGen.Type("rsa"))
is.Nil(err)
is.Equal(key3.Name, "testKey3")
is.NotNil(key3.Id)
defer func() {
_, err = s.KeyRm(context.Background(), "testKey4")
is.Nil(err)
}()
key4, err := s.KeyGen(context.Background(), "testKey4", KeyGen.Type("rsa"), KeyGen.Size(4096))
is.Nil(err)
is.Equal(key4.Name, "testKey4")
is.NotNil(key4.Id)
_, err = s.KeyGen(context.Background(), "testKey5", KeyGen.Type("rsa"), KeyGen.Size(1024))
is.NotNil(err)
is.Equal(err.Error(), "key/gen: rsa keys must be >= 2048 bits to be useful")
}
func TestKeyList(t *testing.T) {
is := is.New(t)
s := NewShell(shellUrl)
defer func() {
_, err := s.KeyRm(context.Background(), "testKey")
is.Nil(err)
}()
key, err := s.KeyGen(context.Background(), "testKey")
is.Nil(err)
keys, err := s.KeyList(context.Background())
is.Nil(err)
is.Equal(len(keys), 2)
is.Equal(keys[0].Name, "self")
is.NotNil(keys[0].Id)
is.NotNil(keys[1].Id, key.Id)
is.NotNil(keys[1].Name, key.Name)
}
func TestKeyRename(t *testing.T) {
is := is.New(t)
s := NewShell(shellUrl)
key, err := s.KeyGen(context.Background(), "test1")
is.Nil(err)
out, err := s.KeyRename(context.Background(), "test1", "test2", false)
is.Nil(err)
is.Equal(out.Now, "test2")
is.Equal(out.Was, "test1")
is.Equal(out.Id, key.Id)
is.False(out.Overwrite)
_, err = s.KeyRm(context.Background(), "test2")
is.Nil(err)
_, err = s.KeyRename(context.Background(), "test2", "test1", false)
is.NotNil(err)
is.Equal(err.Error(), "key/rename: no key named test2 was found")
}
func TestKeyRm(t *testing.T) {
is := is.New(t)
s := NewShell(shellUrl)
key, err := s.KeyGen(context.Background(), "testKey")
is.Nil(err)
keys, err := s.KeyRm(context.Background(), "testKey")
is.Nil(err)
is.Equal(len(keys), 1)
is.Equal(keys[0].Name, key.Name)
is.Equal(keys[0].Id, key.Id)
_, err = s.KeyRm(context.Background(), "testKey")
is.NotNil(err)
is.Equal(err.Error(), "key/rm: no key named testKey was found")
}