-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathidentity_test.go
182 lines (147 loc) · 4.09 KB
/
identity_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
package secureio_test
import (
"context"
"io/ioutil"
"math/rand"
"os"
"path"
"sync"
"testing"
"time"
"github.com/stretchr/testify/assert"
. "github.com/xaionaro-go/secureio"
)
func TestNewIdentity(t *testing.T) {
dirPath0, err := ioutil.TempDir(os.TempDir(), `secureio-test`)
assert.NoError(t, err)
//defer os.RemoveAll(dirPath0)
dirPath1, err := ioutil.TempDir(os.TempDir(), `secureio-test`)
assert.NoError(t, err)
//defer os.RemoveAll(dirPath1)
identity0, err := NewIdentity(dirPath0)
assert.NoError(t, err)
assert.NotNil(t, identity0)
identity1, err := NewIdentity(dirPath0)
assert.NoError(t, err)
assert.NotNil(t, identity1)
assert.Equal(t, identity0.Keys.Private, identity1.Keys.Private)
identity2, err := NewIdentityFromPrivateKey(identity0.Keys.Private)
assert.NoError(t, err)
assert.NotNil(t, identity2)
assert.Equal(t, identity0.Keys.Public, identity2.Keys.Public)
remoteIdentity0, err := NewRemoteIdentity(path.Join(dirPath1, `id_ed25519.pub`))
assert.Error(t, err)
assert.Nil(t, remoteIdentity0)
remoteIdentity0, err = NewRemoteIdentity(path.Join(dirPath0, `/id_ed25519.pub`))
assert.NoError(t, err)
assert.NotNil(t, remoteIdentity0)
assert.Nil(t, remoteIdentity0.Keys.Private)
assert.NotNil(t, remoteIdentity0.Keys.Public)
assert.Equal(t, identity0.Keys.Public, remoteIdentity0.Keys.Public)
remoteIdentity1, err := NewRemoteIdentityFromPublicKey(remoteIdentity0.Keys.Public)
assert.NoError(t, err)
assert.NotNil(t, remoteIdentity1)
assert.Equal(t, remoteIdentity0.Keys.Public, remoteIdentity1.Keys.Public)
_, err = NewRemoteIdentityFromPublicKey(nil)
assert.Error(t, err)
_, err = NewIdentityFromPrivateKey(nil)
assert.Error(t, err)
}
func testIdentityMutualConfirmationOfIdentityWithPSKs(t *testing.T, remoteIsKnown, shouldFail bool, psk0, psk1 []byte) {
identity0, identity1, conn0, conn1 := testPair(t)
defer conn0.Close()
defer conn1.Close()
opts0 := &SessionOptions{}
opts1 := &SessionOptions{}
opts0.KeyExchangerOptions.PSK = psk0
opts1.KeyExchangerOptions.PSK = psk1
opts0.EnableDebug = true
opts1.EnableDebug = true
ctx, cancelFunc := context.WithDeadline(context.Background(), time.Now().Add(time.Second*5))
defer cancelFunc()
go func() {
select {
case <-ctx.Done():
case <-time.After(time.Second):
}
}()
var wg sync.WaitGroup
var err0 error
var keys0 [][]byte
wg.Add(1)
go func() {
defer wg.Done()
identity1 := identity1
if !remoteIsKnown {
identity1 = nil
}
keys0, err0 = identity0.MutualConfirmationOfIdentity(
ctx,
identity1,
conn0,
&testLogger{t},
opts0,
func(sess *Session) error {
printLogsOfSession(t, !shouldFail, sess)
return nil
},
)
}()
var err1 error
var keys1 [][]byte
wg.Add(1)
go func() {
defer wg.Done()
identity0 := identity0
if !remoteIsKnown {
identity0 = nil
}
keys1, err1 = identity1.MutualConfirmationOfIdentity(
ctx,
identity0,
conn1,
&testLogger{t},
opts1,
func(sess *Session) error {
printLogsOfSession(t, !shouldFail, sess)
return nil
},
)
}()
wg.Wait()
if shouldFail {
assert.Error(t, err0)
assert.Error(t, err1)
assert.Nil(t, keys0)
assert.Nil(t, keys1)
} else {
assert.NoError(t, err0)
assert.NoError(t, err1)
assert.NotNil(t, keys0)
assert.NotNil(t, keys1)
assert.Equal(t, keys0, keys1)
}
testConnIsOpen(t, conn0, conn1)
}
func TestIdentityMutualConfirmationOfIdentityWithoutPSK(t *testing.T) {
testIdentityMutualConfirmationOfIdentityWithPSKs(t, true, false, nil, nil)
}
func TestIdentityMutualConfirmationOfIdentityWithPSK(t *testing.T) {
psk := make([]byte, 64)
rand.Read(psk)
testIdentityMutualConfirmationOfIdentityWithPSKs(t, true, false, psk, psk)
}
func TestIdentityMutualConfirmationOfIdentityWithWrongPSK(t *testing.T) {
psk0 := make([]byte, 64)
psk1 := make([]byte, 64)
rand.Read(psk0)
copy(psk1, psk0)
psk0[63] = 0
psk1[63] = 1
testIdentityMutualConfirmationOfIdentityWithPSKs(t, true, true, psk0, psk1)
}
func TestIdentityMutualConfirmationOfIdentityByPSK(t *testing.T) {
psk := make([]byte, 64)
rand.Read(psk)
testIdentityMutualConfirmationOfIdentityWithPSKs(t, false, false, psk, psk)
}