-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathexpect-ct_test.go
190 lines (157 loc) · 5.19 KB
/
expect-ct_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
package helmet
import (
"testing"
)
func TestExpectCT_DirectiveMaxAge(t *testing.T) {
t.Parallel()
testCases := []struct {
name string
maxAge int
expectedDirective ExpectCTDirective
}{
{name: "0", maxAge: 0, expectedDirective: ""},
{name: "-1", maxAge: -1, expectedDirective: ""},
{name: "1", maxAge: 1, expectedDirective: "max-age=1"},
{name: "1 day", maxAge: 86400, expectedDirective: "max-age=86400"},
}
for _, tc := range testCases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
directive := ExpectCTDirectiveMaxAge(tc.maxAge)
if directive != tc.expectedDirective {
t.Errorf("Incorrect MaxAge\tExpected: %s\tActual: %s\n", tc.expectedDirective, directive)
}
})
}
}
func TestExpectCT_DirectiveReportURI(t *testing.T) {
t.Parallel()
testCases := []struct {
name string
reportURI string
expectedDirective ExpectCTDirective
}{
{name: "Empty", reportURI: "", expectedDirective: ""},
{name: "Not empty", reportURI: "/report-uri", expectedDirective: `report-uri="/report-uri"`},
}
for _, tc := range testCases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
directive := ExpectCTDirectiveReportURI(tc.reportURI)
if directive != tc.expectedDirective {
t.Errorf("Incorrect ReportURI\tExpected: %s\tActual: %s\n", tc.expectedDirective, directive)
}
})
}
}
func TestExpectCT_NewExpectCT(t *testing.T) {
t.Parallel()
testCases := []struct {
name string
maxAge int
enforce bool
reportURI string
}{
{name: "Empty", maxAge: 0, enforce: false, reportURI: ""},
{name: "Max Age", maxAge: 30, enforce: false, reportURI: ""},
{name: "Enforce", maxAge: 0, enforce: true, reportURI: ""},
{name: "ReportURI", maxAge: 0, enforce: false, reportURI: "/report-uri"},
{name: "Max Age, Enforce", maxAge: 30, enforce: true, reportURI: ""},
{name: "Max Age, ReportURI", maxAge: 30, enforce: false, reportURI: "/report-uri"},
{name: "Enforce, ReportURI", maxAge: 0, enforce: true, reportURI: "/report-uri"},
{name: "Max Age, Enforce, ReportURI", maxAge: 30, enforce: true, reportURI: "/report-uri"},
}
for _, tc := range testCases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
ect := NewExpectCT(tc.maxAge, tc.enforce, tc.reportURI)
if ect.MaxAge != tc.maxAge {
t.Errorf("Incorrect MaxAge\tExpected: %d\tActual: %d\n", tc.maxAge, ect.MaxAge)
}
if ect.Enforce != tc.enforce {
t.Errorf("Incorrect Enforce\tExpected: %t\tActual: %t\n", tc.enforce, ect.Enforce)
}
if ect.ReportURI != tc.reportURI {
t.Errorf("Incorrect ReportURI\tExpected: %s\tActual: %s\n", tc.reportURI, ect.ReportURI)
}
if ect.cache != "" {
t.Errorf("Cache should not be set\tActual: %s\n", ect.cache)
}
})
}
}
func TestExpectCT_EmptyExpectCT(t *testing.T) {
t.Parallel()
ect := EmptyExpectCT()
if ect.MaxAge != 0 {
t.Errorf("MaxAge should be zero\tActual: %d\n", ect.MaxAge)
}
if ect.Enforce != false {
t.Errorf("Enforce should be false\tActual: %t\n", ect.Enforce)
}
if ect.ReportURI != "" {
t.Errorf("ReportURI should be empty\tActual: %s\n", ect.ReportURI)
}
if ect.cache != "" {
t.Errorf("Cache should not be set\tActual: %s\n", ect.cache)
}
}
func TestExpectCT_String(t *testing.T) {
t.Parallel()
testCases := []struct {
name string
expectCT *ExpectCT
expectedHeader string
}{
{name: "Empty", expectCT: EmptyExpectCT(), expectedHeader: ""},
{name: "Max Age Zero", expectCT: NewExpectCT(0, false, ""), expectedHeader: ""},
{name: "Max Age", expectCT: NewExpectCT(30, false, ""), expectedHeader: "max-age=30"},
{name: "Max Age, Enforce", expectCT: NewExpectCT(30, true, ""), expectedHeader: "max-age=30, enforce"},
{name: "Max Age, Report URI", expectCT: NewExpectCT(30, false, "/report-uri"), expectedHeader: `max-age=30, report-uri="/report-uri"`},
{name: "Max Age, Enforce, Report URI", expectCT: NewExpectCT(30, true, "/report-uri"), expectedHeader: `max-age=30, enforce, report-uri="/report-uri"`},
}
for _, tc := range testCases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
header := tc.expectCT.String()
if header != tc.expectedHeader {
t.Errorf("Expected: %s\tActual: %s\n", tc.expectedHeader, header)
}
// check that the cache is set
if tc.expectCT.cache != header {
t.Errorf("ExpectCT String() cache is not set!\tActual: %s\n", tc.expectCT.cache)
}
// utilize said cache
header = tc.expectCT.String()
if header != tc.expectedHeader {
t.Errorf("Expected: %s\tActual: %s\n", tc.expectedHeader, header)
}
})
}
}
func TestExpectCT_Empty(t *testing.T) {
t.Parallel()
testCases := []struct {
name string
expectCT *ExpectCT
expectedEmpty bool
}{
{name: "Empty", expectCT: EmptyExpectCT(), expectedEmpty: true},
{name: "Max Age Zero", expectCT: NewExpectCT(0, false, ""), expectedEmpty: true},
{name: "Max Age Set", expectCT: NewExpectCT(30, false, ""), expectedEmpty: false},
}
for _, tc := range testCases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
exists := tc.expectCT.Empty()
if exists != tc.expectedEmpty {
t.Errorf("Expected: %t\tActual: %t\n", tc.expectedEmpty, exists)
}
})
}
}