1// Copyright 2018 The etcd Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package integration
16
17import (
18	"context"
19	"crypto/tls"
20	"testing"
21	"time"
22
23	"github.com/coreos/etcd/clientv3"
24	"github.com/coreos/etcd/pkg/testutil"
25)
26
27func TestTLSClientCipherSuitesValid(t *testing.T)    { testTLSCipherSuites(t, true) }
28func TestTLSClientCipherSuitesMismatch(t *testing.T) { testTLSCipherSuites(t, false) }
29
30// testTLSCipherSuites ensures mismatching client-side cipher suite
31// fail TLS handshake with the server.
32func testTLSCipherSuites(t *testing.T, valid bool) {
33	defer testutil.AfterTest(t)
34
35	cipherSuites := []uint16{
36		tls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
37		tls.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
38		tls.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
39		tls.TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
40		tls.TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305,
41		tls.TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305,
42	}
43	srvTLS, cliTLS := testTLSInfo, testTLSInfo
44	if valid {
45		srvTLS.CipherSuites, cliTLS.CipherSuites = cipherSuites, cipherSuites
46	} else {
47		srvTLS.CipherSuites, cliTLS.CipherSuites = cipherSuites[:2], cipherSuites[2:]
48	}
49
50	clus := NewClusterV3(t, &ClusterConfig{Size: 1, ClientTLS: &srvTLS})
51	defer clus.Terminate(t)
52
53	cc, err := cliTLS.ClientConfig()
54	if err != nil {
55		t.Fatal(err)
56	}
57	cli, cerr := clientv3.New(clientv3.Config{
58		Endpoints:   []string{clus.Members[0].GRPCAddr()},
59		DialTimeout: time.Second,
60		TLS:         cc,
61	})
62	if cli != nil {
63		cli.Close()
64	}
65	if !valid && cerr != context.DeadlineExceeded {
66		t.Fatalf("expected %v with TLS handshake failure, got %v", context.DeadlineExceeded, cerr)
67	}
68	if valid && cerr != nil {
69		t.Fatalf("expected TLS handshake success, got %v", cerr)
70	}
71}
72