This repository was archived by the owner on Aug 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 605
Expand file tree
/
Copy pathmethods_test.go
More file actions
144 lines (117 loc) · 2.94 KB
/
Copy pathmethods_test.go
File metadata and controls
144 lines (117 loc) · 2.94 KB
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package sshkeys
import (
"flag"
"koding/klient/testutil"
"log"
"os"
"os/user"
"strings"
"testing"
"github.com/koding/kite"
)
var exampleKey = struct {
Key string
Fingerprint string
}{
Key: "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCwhQ3QB/4xtgpVYQVOoogNxz+3L3bjUuoOP/1duR9JxzLL0maxV64lVyLxLw14G+tG0JS42vtkySX9VzQRWofQlRNK5YdAtBQY7Xl/bWvjtXU/oI9F8o59cTkEduG4UC8nqGlMcabvD3+YnkF1HOxLnkDa+MfnTjA/+SlpJYFFzXfgKbiOSBZFie/nb+KY/40YJ74V9QGpY8qPxWct4pjopcgE2mEigZLc5UgCUNmRfYWvqqevLeph0fqXWliYtLQcTLpoZIVscK8fPXkHPKcopMVnqc9Z+vTW02qniLgPV1HCzPBTWynEZ6a51fVrqraRlDVeEruMJZZEjp33YD71 klient@example.com",
Fingerprint: "5d:30:cd:3f:f3:60:64:ea:1d:f1:cc:7f:06:23:11:f0",
}
var (
sshkeys *kite.Kite
// remote defines a remote user calling the sshkeys kite
remote *kite.Client
)
func TestMain(m *testing.M) {
flag.Parse()
kiteURL := testutil.GenKiteURL()
sshkeys = kite.New("sshkeys", "0.0.1")
sshkeys.Config.DisableAuthentication = true
sshkeys.Config.Port = kiteURL.Port()
sshkeys.HandleFunc("list", List)
sshkeys.HandleFunc("add", Add)
sshkeys.HandleFunc("delete", Delete)
go sshkeys.Run()
<-sshkeys.ServerReadyNotify()
defer sshkeys.Close()
u, err := user.Current()
if err != nil {
panic(err)
}
remoteKite := kite.New("remote", "0.0.1")
remoteKite.Config.Username = u.Username
remote = remoteKite.NewClient(kiteURL.String())
err = remote.Dial()
if err != nil {
log.Fatalf("err")
}
defer remoteKite.Close()
os.Exit(m.Run())
}
func TestAdd(t *testing.T) {
resp, err := remote.Tell("add", struct {
Keys []string
}{
Keys: []string{exampleKey.Key},
})
if err != nil {
t.Fatal(err)
}
b, err := resp.Bool()
if err != nil {
t.Fatal(err)
}
if !b {
t.Error("add method should return true, got false")
}
}
func TestList(t *testing.T) {
resp, err := remote.Tell("list")
if err != nil {
t.Fatal(err)
}
var r map[string]string
if err := resp.Unmarshal(&r); err != nil {
t.Fatal(err)
}
key, ok := r[exampleKey.Fingerprint]
if !ok {
t.Errorf("list: couldn't find key with fingerprint %s", exampleKey.Fingerprint)
}
if key != exampleKey.Key {
t.Errorf("list: added key is not equal. \nGot: %v\nWant: %s\n", key, exampleKey.Key)
}
}
func TestDelete(t *testing.T) {
resp, err := remote.Tell("delete", struct {
Fingerprints []string
}{
Fingerprints: []string{exampleKey.Fingerprint},
})
if err != nil {
t.Fatal(err)
}
b, err := resp.Bool()
if err != nil {
t.Fatal(err)
}
if !b {
t.Error("delete method should return true, got false")
}
resp, err = remote.Tell("list")
if strings.Contains(err.Error(), "no ssh keys found") {
// means the authorized_keys is totally empty right now, we can skip the test
// immediately
t.SkipNow()
}
if err != nil {
t.Fatal(err)
}
var r map[string]string
if err := resp.Unmarshal(&r); err != nil {
t.Fatal(err)
}
_, ok := r[exampleKey.Fingerprint]
if ok {
t.Errorf("delete: key for '%s still exists after deletion'", exampleKey.Fingerprint)
}
}