-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
Copy pathinit_handshake_test.go
220 lines (194 loc) · 5.87 KB
/
init_handshake_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
218
219
220
// Copyright 2021 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
package server
import (
"context"
"net"
"os"
"path"
"strings"
"testing"
"time"
"github.com/cockroachdb/cockroach/pkg/base"
"github.com/cockroachdb/cockroach/pkg/testutils"
"github.com/cockroachdb/cockroach/pkg/util/leaktest"
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/cockroachdb/cockroach/pkg/util/timeutil"
"github.com/cockroachdb/errors"
"github.com/stretchr/testify/require"
)
func TestInitHandshake(t *testing.T) {
defer leaktest.AfterTest(t)()
defer log.Scope(t).Close(t)
timeout := 140 * time.Second
if deadline, ok := t.Deadline(); ok {
timeout = deadline.Sub(timeutil.Now())
}
ctx, cancel := context.WithTimeout(context.TODO(), timeout)
defer cancel()
tempDir, del := testutils.TempDir(t)
defer del()
cfg1 := &base.Config{}
cfg1.InitDefaults()
cfg1.SSLCertsDir = path.Join(tempDir, "temp1")
require.NoError(t, os.Mkdir(cfg1.SSLCertsDir, 0755))
cfg2 := &base.Config{}
cfg2.InitDefaults()
cfg2.SSLCertsDir = path.Join(tempDir, "temp2")
require.NoError(t, os.Mkdir(cfg2.SSLCertsDir, 0755))
cfg3 := &base.Config{}
cfg3.InitDefaults()
cfg3.SSLCertsDir = path.Join(tempDir, "temp3")
require.NoError(t, os.Mkdir(cfg3.SSLCertsDir, 0755))
errReturned := make(chan error, 1)
// Do a three-node handshake, and ensure no error is returned. The errors
// returned should be nil, and one of the temp SSL certs directories should
// not be empty.
var addr1, addr2, addr3 string
listener1, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
t.Fatal(err)
}
defer listener1.Close()
addr1 = listener1.Addr().String()
listener2, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
t.Fatal(err)
}
defer listener2.Close()
addr2 = listener2.Addr().String()
listener3, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
t.Fatal(err)
}
defer listener3.Close()
addr3 = listener3.Addr().String()
go func() {
errReturned <- InitHandshake(ctx, cfg1, "foobar", 2, []string{addr2, addr3}, cfg1.SSLCertsDir, listener1)
}()
go func() {
errReturned <- InitHandshake(ctx, cfg1, "foobar", 2, []string{addr1, addr3}, cfg1.SSLCertsDir, listener2)
}()
go func() {
errReturned <- InitHandshake(ctx, cfg1, "foobar", 2, []string{addr1, addr2}, cfg1.SSLCertsDir, listener3)
}()
count := 0
// All of the errors returned to errReturned should be nil.
var combined error
for count < 3 {
select {
case err := <-errReturned:
combined = errors.CombineErrors(combined, err)
count++
case <-ctx.Done():
t.Fatal("test timeout exceeded")
}
}
require.NoError(t, combined)
dirs := []string{cfg1.SSLCertsDir, cfg2.SSLCertsDir, cfg3.SSLCertsDir}
found := false
// One of the directories should be non-empty, indicating a successful
// provisioning of the init bundle.
for _, dir := range dirs {
f, err := os.Open(dir)
require.NoError(t, err)
_, err = f.Readdirnames(1)
if err == nil {
found = true
break
}
}
require.True(t, found)
}
func TestInitHandshakeWrongToken(t *testing.T) {
defer leaktest.AfterTest(t)()
defer log.Scope(t).Close(t)
// The test deadline needs to be greater than this for this test to pass,
// as one of the nodes will have to wait for this context to time out.
timeout := 70 * time.Second
ctx, cancel := context.WithTimeout(context.TODO(), timeout)
defer cancel()
tempDir, del := testutils.TempDir(t)
defer del()
cfg1 := &base.Config{}
cfg1.InitDefaults()
cfg1.SSLCertsDir = path.Join(tempDir, "temp1")
require.NoError(t, os.Mkdir(cfg1.SSLCertsDir, 0755))
cfg2 := &base.Config{}
cfg2.InitDefaults()
cfg2.SSLCertsDir = path.Join(tempDir, "temp2")
require.NoError(t, os.Mkdir(cfg2.SSLCertsDir, 0755))
cfg3 := &base.Config{}
cfg3.InitDefaults()
cfg3.SSLCertsDir = path.Join(tempDir, "temp3")
require.NoError(t, os.Mkdir(cfg3.SSLCertsDir, 0755))
errReturned := make(chan error, 1)
// Do a three-node handshake, with one node having the wrong token. At least
// one of the three errors returned should be non-nil.
var addr1, addr2, addr3 string
listener1, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
t.Fatal(err)
}
defer listener1.Close()
addr1 = listener1.Addr().String()
listener2, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
t.Fatal(err)
}
defer listener2.Close()
addr2 = listener2.Addr().String()
listener3, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
t.Fatal(err)
}
defer listener3.Close()
addr3 = listener3.Addr().String()
go func() {
errReturned <- InitHandshake(ctx, cfg1, "foobar", 2, []string{addr2, addr3}, cfg1.SSLCertsDir, listener1)
}()
go func() {
errReturned <- InitHandshake(ctx, cfg1, "foobarbaz", 2, []string{addr1, addr3}, cfg1.SSLCertsDir, listener2)
}()
go func() {
errReturned <- InitHandshake(ctx, cfg1, "foobar", 2, []string{addr1, addr2}, cfg1.SSLCertsDir, listener3)
}()
count := 0
sigErrors := 0
// All of the errors returned to errReturned should not be nil, and one of
// them should contain "invalid signature".
for count < 3 {
select {
case err := <-errReturned:
if err == nil {
t.Fatal("expected non-nil error from nodes")
}
if strings.Contains(err.Error(), "invalid signature") {
sigErrors++
}
count++
}
}
require.Greater(t, sigErrors, 0)
dirs := []string{cfg1.SSLCertsDir, cfg2.SSLCertsDir, cfg3.SSLCertsDir}
found := false
// All of the cert dirs should be empty, indicating an unsuccessful
// provisioning of the init bundle.
for _, dir := range dirs {
f, err := os.Open(dir)
require.NoError(t, err)
_, err = f.Readdirnames(1)
if err == nil {
found = true
break
}
}
require.False(t, found)
}