-
Notifications
You must be signed in to change notification settings - Fork 13
/
tls_test.go
97 lines (88 loc) · 2.06 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package kafka
import (
"os"
"testing"
)
func TestTLSConfig_TLSConfig(t *testing.T) {
// Given
rootca, err := os.CreateTemp("", "rootca*.pem")
if err != nil {
t.Fatalf("Error creating rootca pem temp file %s", err.Error())
}
defer os.Remove(rootca.Name())
intermediate, err := os.CreateTemp("", "intermediate*.pem")
if err != nil {
t.Fatalf("Error creating rootca pem temp file %s", err.Error())
}
defer os.Remove(intermediate.Name())
tlsCfg := TLSConfig{
RootCAPath: rootca.Name(),
IntermediateCAPath: intermediate.Name(),
}
// When
_, err = tlsCfg.TLSConfig()
// Then
if err != nil {
t.Fatalf("Error when settings tls certificates %s", err.Error())
}
}
func TestTLSConfig_IsEmpty(t *testing.T) {
type fields struct {
RootCAPath string
IntermediateCAPath string
}
tests := []struct {
name string
fields fields
want bool
}{
{
name: "Empty_When_Paths_Does_Not_Exist",
fields: fields{},
want: true,
},
{
name: "Filled_When_Paths_Exist",
fields: fields{RootCAPath: "somepath", IntermediateCAPath: "somepath"},
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := &TLSConfig{
RootCAPath: tt.fields.RootCAPath,
IntermediateCAPath: tt.fields.IntermediateCAPath,
}
if got := c.IsEmpty(); got != tt.want {
t.Errorf("IsEmpty() = %v, want %v", got, tt.want)
}
})
}
}
func TestTLSConfig_Json(t *testing.T) {
t.Run("Should_Convert_Nil_Config_To_Json", func(t *testing.T) {
// Given
var cfg *TLSConfig
expected := "{}"
// When
result := cfg.JSON()
// Then
if result != expected {
t.Fatal("result must be equal to expected")
}
})
t.Run("Should_Convert_To_Json", func(t *testing.T) {
// Given
cfg := &TLSConfig{
RootCAPath: "resources/ca",
IntermediateCAPath: "resources/intCa",
}
expected := "{\"RootCAPath\": \"resources/ca\", \"IntermediateCAPath\": \"resources/intCa\"}"
// When
result := cfg.JSON()
// Then
if result != expected {
t.Fatal("result must be equal to expected")
}
})
}