forked from msgboxio/ike
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils_test.go
217 lines (198 loc) · 4.25 KB
/
utils_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
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package ike
import (
"context"
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/x509"
"net"
"os"
"sync"
"testing"
"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
"github.com/msgboxio/ike/platform"
"github.com/pkg/errors"
)
var localAddr, remoteAddr net.Addr
var logger log.Logger
func init() {
logger = level.NewFilter(log.NewLogfmtLogger(os.Stdout), level.AllowDebug())
// logger = log.NewNopLogger()
}
var pskTestID = &PskIdentities{
Primary: "[email protected]",
Ids: map[string][]byte{"[email protected]": []byte("foo")},
}
func certTestIds(t testing.TB) (localID, remoteID Identity) {
roots, err := LoadRoot("test/cert/cacert.pem")
if err != nil {
t.Fatal(err)
}
certs, err := LoadCerts("test/cert/hostcert.der")
if err != nil {
t.Fatalf("Cert: %s", err)
}
key, err := LoadKey("test/cert/hostkey.der")
if err != nil {
t.Fatalf("Key: %s", err)
}
localID = &CertIdentity{
Certificate: certs[0],
PrivateKey: key,
}
remoteID = &CertIdentity{
Roots: roots,
Name: "172.17.0.1",
}
return
}
func eccertTestIds(t testing.TB) (localID, remoteID Identity) {
cacert, cakey, err := NewECCA("TEST CA")
if err != nil {
t.Fatal(err)
}
key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
if err != nil {
t.Fatal(err)
}
cert, err := NewSignedCert(CertID{
CommonName: "172.17.0.1",
}, key.Public(), cacert, cakey)
if err != nil {
t.Fatal(err)
}
localID = &CertIdentity{
Certificate: cert,
PrivateKey: key,
}
roots := x509.NewCertPool()
roots.AddCert(cacert)
remoteID = &CertIdentity{
Roots: roots,
Name: "172.17.0.1",
}
return
}
type testcb struct {
writeTo chan []byte
saTo chan *platform.SaParams
errTo chan error
}
func (t *testcb) ReadPacket() (b []byte, remoteAddr, localAddr net.Addr, err error) {
return
}
func (t *testcb) WritePacket(reply []byte, remoteAddr net.Addr) error {
t.writeTo <- reply
return nil
}
func (t *testcb) Inner() net.Conn { return nil }
func (t *testcb) Close() error { return nil }
func scb(cbk *testcb) *SessionCallback {
return &SessionCallback{
InstallChildSa: func(_ *Session, sa *platform.SaParams) error {
cbk.saTo <- sa
return nil
},
RemoveChildSa: func(_ *Session, sa *platform.SaParams) error {
return nil
},
}
}
func runTestInitiator(cfg *Config, cbk *testcb, readFrom chan []byte, log log.Logger) {
initJitter = 0
initiator, err := NewInitiator(cfg, zeroAddr, zeroAddr, cbk, scb(cbk), log)
if err != nil {
cbk.errTo <- err
}
// run state machine, will send initI on given channel
go func() {
err = RunSession(initiator)
if err != nil {
cbk.errTo <- err
}
}()
for {
msg, err := DecodeMessage(<-readFrom, log)
if err != nil {
cbk.errTo <- err
break
}
initiator.PostMessage(msg)
}
}
func runTestResponder(cfg *Config, cbk *testcb, readFrom chan []byte, log log.Logger) {
waitForInitI := func() *Message {
for {
initI, err := DecodeMessage(<-readFrom, log)
if err != nil {
cbk.errTo <- err
}
if err = checkInitRequest(initI, cbk, cfg, log); errors.Cause(err) == errMissingCookie {
continue
} else if err != nil {
cbk.errTo <- err
return nil
}
return initI
}
}
// wait for initI
initI := waitForInitI()
if initI == nil {
return
}
// create responder
responder, err := NewResponder(cfg, cbk, scb(cbk), initI, log)
if err != nil {
cbk.errTo <- err
}
go func() {
err = RunSession(responder)
if err != nil {
cbk.errTo <- err
}
}()
// initI to responder, will send initR
responder.PostMessage(initI)
for {
authI, err := DecodeMessage(<-readFrom, log)
if err != nil {
cbk.errTo <- err
break
}
// authI to responder, will send authR
responder.PostMessage(authI)
}
}
func waitFor2Sa(t testing.TB, sa chan *platform.SaParams, cerr chan error) (err error) {
wg := &sync.WaitGroup{}
wg.Add(2)
cxt, cancel := context.WithCancel(context.Background())
// receive the 2 sa
go func() {
done:
for {
select {
case <-cxt.Done():
t.Log("XXX DONE XXX")
break done
case sa1 := <-sa:
t.Logf("sa1I: %v", sa1.SpiI)
wg.Done()
case sa2 := <-sa:
t.Logf("sa2R: %+v", sa2.SpiR)
wg.Done()
case err = <-cerr:
cancel()
t.Logf("error: %+v", err)
wg.Done()
wg.Done()
break done
}
}
}()
wg.Wait()
cancel()
return
}