-
Notifications
You must be signed in to change notification settings - Fork 1
/
tls_test.go
75 lines (67 loc) · 1.74 KB
/
tls_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
// Copyright (c) 2022 Meng Huang ([email protected])
// This package is licensed under a MIT license that can be found in the LICENSE file.
package rpc
import (
"os"
"testing"
)
func TestLoadTLSConfig(t *testing.T) {
var caCertFileName = "tmpTestCaCertFile"
var serverCertFileName = "tmpTestServerCertFile"
var serverKeyFileName = "tmpTestServerKeyFile"
var err error
_, err = LoadServerTLSConfig("", "")
if err == nil {
t.Error("should be no such file or directory")
}
certFile, _ := os.Create(serverCertFileName)
certFile.Write(DefaultServerCertPEM)
certFile.Close()
defer os.Remove(serverCertFileName)
_, err = LoadServerTLSConfig(serverCertFileName, "")
if err == nil {
t.Error("should be no such file or directory")
}
keyFile, _ := os.Create(serverKeyFileName)
keyFile.Write(DefaultServerKeyPEM)
keyFile.Close()
defer os.Remove(serverKeyFileName)
_, err = LoadServerTLSConfig(serverCertFileName, serverKeyFileName)
if err != nil {
t.Error(err)
}
_, err = LoadClientTLSConfig("", "")
if err == nil {
t.Error("should be no such file or directory")
}
caCertFile, _ := os.Create(caCertFileName)
caCertFile.Write(DefaultRootCertPEM)
caCertFile.Close()
defer os.Remove(caCertFileName)
_, err = LoadClientTLSConfig(caCertFileName, "")
if err != nil {
t.Error(err)
}
}
func TestServerTLSConfig(t *testing.T) {
defer func() {
if err := recover(); err == nil {
t.Error("should panic")
}
}()
ServerTLSConfig(DefaultServerCertPEM, []byte{})
}
func TestClientTLSConfig(t *testing.T) {
defer func() {
if err := recover(); err == nil {
t.Error("should panic")
}
}()
ClientTLSConfig([]byte{}, "")
}
func TestDefalutServerName(t *testing.T) {
sub := "Hello"
if DefalutServerName("Hello")[:len(sub)] != sub {
t.Error()
}
}