-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathfront_test.go
More file actions
151 lines (144 loc) · 4.53 KB
/
front_test.go
File metadata and controls
151 lines (144 loc) · 4.53 KB
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
package fronted
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestNewProvider(t *testing.T) {
verifyHostname := "verifyHostname.com"
var tests = []struct {
name string
givenHosts map[string]string
givenTestURL string
givenMasquerades []*Masquerade
givenPassthrough []string
//givenSNIConfig *SNIConfig
givenFrontingSNIs map[string]*SNIConfig
givenVerifyHostname *string
assert func(t *testing.T, actual *Provider)
}{
{
name: "should return a new provider without host aliases, masquerades and passthrough",
givenHosts: map[string]string{},
givenTestURL: "http://test.com",
assert: func(t *testing.T, actual *Provider) {
assert.Empty(t, actual.HostAliases)
assert.Empty(t, actual.Masquerades)
assert.Empty(t, actual.PassthroughPatterns)
assert.Equal(t, "http://test.com", actual.TestURL)
//assert.Nil(t, actual.Validator)
assert.Nil(t, actual.FrontingSNIs)
},
},
{
name: "should return a new provider with host aliases, masquerades and passthrough",
givenHosts: map[string]string{"host1": "alias1", "host2": "alias2"},
givenTestURL: "http://test.com",
givenMasquerades: []*Masquerade{{Domain: "domain1", IpAddress: "127.0.0.1"}},
givenPassthrough: []string{"passthrough1", "passthrough2"},
givenFrontingSNIs: map[string]*SNIConfig{
"test": &SNIConfig{
UseArbitrarySNIs: true,
ArbitrarySNIs: []string{"sni1.com", "sni2.com"},
},
},
givenVerifyHostname: &verifyHostname,
assert: func(t *testing.T, actual *Provider) {
assert.Equal(t, "http://test.com", actual.TestURL)
assert.Equal(t, "alias1", actual.HostAliases["host1"])
assert.Equal(t, "alias2", actual.HostAliases["host2"])
assert.Equal(t, 1, len(actual.Masquerades))
assert.Equal(t, "domain1", actual.Masquerades[0].Domain)
assert.Equal(t, "127.0.0.1", actual.Masquerades[0].IpAddress)
assert.Equal(t, "sni1.com", actual.Masquerades[0].SNI)
assert.Equal(t, verifyHostname, *actual.Masquerades[0].VerifyHostname)
assert.Equal(t, 2, len(actual.PassthroughPatterns))
},
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
actual := NewProvider(tt.givenHosts, tt.givenTestURL, tt.givenMasquerades, tt.givenPassthrough, tt.givenFrontingSNIs, tt.givenVerifyHostname, "test")
tt.assert(t, actual)
})
}
}
func TestGenerateSNI(t *testing.T) {
emptyMasquerade := new(Masquerade)
var tests = []struct {
name string
assert func(t *testing.T, actual string)
givenConfig *SNIConfig
givenMasquerade *Masquerade
}{
{
name: "should return a empty string when given SNI config is nil",
givenConfig: nil,
givenMasquerade: emptyMasquerade,
assert: func(t *testing.T, actual string) {
assert.Empty(t, actual)
},
},
{
name: "should return a empty string when given SNI config is not nil and UseArbitrarySNIs is false",
givenConfig: &SNIConfig{
UseArbitrarySNIs: false,
},
givenMasquerade: emptyMasquerade,
assert: func(t *testing.T, actual string) {
assert.Empty(t, actual)
},
},
{
name: "should return a empty SNI when the list of arbitrary SNIs is empty",
givenConfig: &SNIConfig{
UseArbitrarySNIs: true,
ArbitrarySNIs: []string{},
},
givenMasquerade: &Masquerade{
IpAddress: "1.1.1.1",
Domain: "randomdomain.net",
},
assert: func(t *testing.T, actual string) {
assert.Empty(t, actual)
},
},
{
name: "should return a SNI when given SNI config is not nil and UseArbitrarySNIs is true",
givenConfig: &SNIConfig{
UseArbitrarySNIs: true,
ArbitrarySNIs: []string{"sni1.com", "sni2.com"},
},
givenMasquerade: &Masquerade{
IpAddress: "1.1.1.1",
Domain: "randomdomain.net",
},
assert: func(t *testing.T, actual string) {
assert.NotEmpty(t, actual)
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
actual := generateSNI(tt.givenConfig, tt.givenMasquerade)
tt.assert(t, actual)
})
}
}
func TestNewThreadSafeFronts(t *testing.T) {
// Test with size 0
tsf := newThreadSafeFronts(0)
assert.NotNil(t, tsf)
assert.Equal(t, 0, tsf.frontSize())
assert.NotNil(t, tsf.fronts)
assert.Equal(t, 0, cap(tsf.fronts))
// Test with positive size
size := 5
tsf2 := newThreadSafeFronts(size)
assert.NotNil(t, tsf2)
assert.Equal(t, 0, tsf2.frontSize())
assert.NotNil(t, tsf2.fronts)
assert.Equal(t, size, cap(tsf2.fronts))
// Test that mutex is initialized
assert.NotNil(t, &tsf2.mx)
}