-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse_test.go
278 lines (244 loc) · 10.5 KB
/
parse_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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
package apachelogparser
import (
"errors"
"reflect"
"strings"
"testing"
)
func TestParseLogRecord(t *testing.T) {
tests := []struct {
record string
log interface{}
err error
}{
{"127.0.0.1 - frank [10/Oct/2000:13:55:36 -0700] \"GET /apache_pb.gif HTTP/1.0\" 200 2326 xyz", nil, ErrInvalidLog},
{"127.0.0.1 - - dda [10/Oct/2000:13:55:36 -0700] \"GET /apache_pb.gif HTTP/1.0\" 200 2326", nil, ErrInvalidLog},
{"[10/Oct/2000:13:55:36 -0700] 200 2326", nil, ErrInvalidLog},
{"[10/Oct/2000:13:55:36 -0700] \"GET /apache_pb.gif HTTP/1.0\" 200 2326", nil, ErrInvalidLog},
}
t.Log("===========================================")
t.Log("Check non-standard log records (fewer or more fields than Common Log Format or Combined Log Format)")
t.Log("-------------------------------------------")
for _, v := range tests {
t.Log("checking ...", v.record)
rec, err := ParseLogRecord(v.record)
if !isNil(rec) {
t.Errorf("FAIL: returned log is not nil")
}
if !errors.Is(err, v.err) {
t.Errorf("FAIL: log is not marked as invalid")
}
}
tests = []struct {
record string
log interface{}
err error
}{
{"0.01 - rob [10/Oct/2000:13:55:36 -0700] \"GET /apache_pb.gif HTTP/1.0\" 200 2326", nil, ErrInvalidIP},
{"1270.0.1 - rob [10/Oct/2000:13:55:36 -0700] \"GET /apache_pb.gif HTTP/1.0\" 200 2326", nil, ErrInvalidIP},
{"abcdef - rob [10/Oct/2000:13:55:36 -0700] \"GET /apache_pb.gif HTTP/1.0\" 200 2326", nil, ErrInvalidIP},
}
t.Log("===========================================")
t.Log("Check invalid IP")
t.Log("-------------------------------------------")
for _, v := range tests {
t.Log("checking ...", v.record)
_, err := ParseLogRecord(v.record)
if !errors.Is(err, v.err) {
t.Errorf("FAIL: IP not marked as invalid")
}
}
tests = []struct {
record string
log interface{}
err error
}{
{"127.0.110.1 - rob 10/Oct/2000:13:55:36 -0700] \"GET /apache_pb.gif HTTP/1.0\" 200 2326", nil, ErrInvalidFieldTimestamp},
{"127.0.0.1 - rob [10/Oct/2000:13:55:36 -0700 \"GET /apache_pb.gif HTTP/1.0\" 200 2326", nil, ErrInvalidFieldTimestamp},
{"127.255.0.123 - rob &10/Oct/2000:13:55:36 -0700] \"GET /apache_pb.gif HTTP/1.0\" 200 2326", nil, ErrInvalidFieldTimestamp},
{"127.1.1.1 - rob [10/Oct/2000:13:55:36 -0700) \"GET /apache_pb.gif HTTP/1.0\" 200 2326", nil, ErrInvalidFieldTimestamp},
}
t.Log("===========================================")
t.Log("Check Timestamp field starts with [ and ends with ]")
t.Log("-------------------------------------------")
for _, v := range tests {
t.Log("checking ...", v.record)
_, err := ParseLogRecord(v.record)
if !errors.Is(err, v.err) {
t.Errorf("FAIL: Timestamp field not marked as invalid")
}
}
tests = []struct {
record string
log interface{}
err error
}{
{"127.0.110.1 - rob [x10/Oct/2000:13:55:36 -0700] \"GET /apache_pb.gif HTTP/1.0\" 200 2326", nil, ErrInvalidTimestamp},
{"127.0.0.1 - rob [y10/Dec/2000:13:55:36 -0700] \"GET /apache_pb.gif HTTP/1.0\" 200 2326", nil, ErrInvalidTimestamp},
{"127.255.0.123 - rob [10/Oct/2222:13:55:36 -250] \"GET /apache_pb.gif HTTP/1.0\" 200 2326", nil, ErrInvalidTimestamp},
{"127.1.1.1 - - [10/Dec/2000:13:55:36 &800] \"GET /apache_pb.gif HTTP/1.0\" 200 2326", nil, ErrInvalidTimestamp},
}
t.Log("===========================================")
t.Log("Check invalid Timestamp ")
t.Log("-------------------------------------------")
for _, v := range tests {
t.Log("checking ...", v.record)
_, err := ParseLogRecord(v.record)
if !errors.Is(err, v.err) {
t.Errorf("FAIL: Timestamp not marked as invalid")
}
}
tests = []struct {
record string
log interface{}
err error
}{
{"127.0.110.1 - rob [10/Oct/2000:13:55:36 -0700] \"GET /apache_pb.gif HTTP/1.0\" abc1 2326", nil, ErrInvalidStatus},
{"40.77.189.89 - - [23/Dec/2021:06:55:20 -0600] \"GET /assets/front/assets/vendor/bootstrap/css/bootstrap.min.css HTTP/2\" 20x0 114625", nil, ErrInvalidStatus},
{"127.0.0.1 - rob [10/Dec/2000:13:55:36 -0700] \"GET /apache_pb.gif HTTP/1.0\" abcd 200$", nil, ErrInvalidStatus},
{"127.255.0.123 - rob [10/Oct/2222:13:55:36 -0250] \"GET /apache_pb.gif HTTP/1.0\" #200 2326", nil, ErrInvalidStatus},
{"127.1.1.1 - - [10/Dec/2000:13:55:36 -0800] \"GET /apache_pb.gif HTTP/1.0\" 20222206423688288883 2326", nil, ErrInvalidStatus},
}
t.Log("===========================================")
t.Log("Check invalid Status")
t.Log("-------------------------------------------")
for _, v := range tests {
t.Log("checking ...", v.record)
_, err := ParseLogRecord(v.record)
if !errors.Is(err, v.err) {
t.Errorf("FAIL: Status not marked as invalid")
}
}
tests = []struct {
record string
log interface{}
err error
}{
{"127.0.110.1 - rob [10/Oct/2000:13:55:36 -0700] \"GET /apache_pb.gif HTTP/1.0\" 200 2a326", nil, ErrInvalidSize},
{"40.77.189.89 - - [23/Dec/2021:06:55:20 -0600] \"GET /assets/front/assets/vendor/bootstrap/css/bootstrap.min.css HTTP/2\" 200 x114625", nil, ErrInvalidSize},
{"127.0.0.1 - rob [10/Dec/2000:13:55:36 -0700] \"GET /apache_pb.gif HTTP/1.0\" 200 2326#", nil, ErrInvalidSize},
{"127.255.0.123 - rob [10/Oct/2222:13:55:36 -0250] \"GET /apache_pb.gif HTTP/1.0\" 200 abcde", nil, ErrInvalidSize},
{"127.1.1.1 xyz - [10/Dec/2000:13:55:36 -0800] \"GET /apache_pb.gif HTTP/1.0\" 200 $2326", nil, ErrInvalidSize},
}
t.Log("===========================================")
t.Log("Check invalid Size")
t.Log("-------------------------------------------")
for _, v := range tests {
t.Log("checking ...", v.record)
_, err := ParseLogRecord(v.record)
if !errors.Is(err, v.err) {
t.Errorf("FAIL: Size not marked as invalid")
}
}
testCommonLog(t)
testCombinedLog(t)
}
func testCommonLog(t *testing.T) {
tests := []struct {
record []string
IP string
Identity string
User string
Timestamp string
Request request
Status int
Size int64
err error
}{
{[]string{"127.0.0.1", "-", "rob", "[10/Oct/2000:13:55:36 -0700]", "GET /apache_pb.gif HTTP/1.0", "200", "2326"},
"127.0.0.1", "-", "rob", "2000-10-10 13:55:36 -0700 PDT", request{"GET", "/apache_pb.gif", "HTTP/1.0"}, 200, 2326, nil},
{[]string{"154.53.43.164", "-", "-", "[23/Dec/2021:14:41:47 -0600]", "GET /new/installer.php HTTP/1.1", "301", "707"},
"154.53.43.164", "-", "-", "2021-12-23 14:41:47 -0600 -0600", request{"GET", "/new/installer.php", "HTTP/1.1"}, 301, 707, nil},
}
t.Log("===========================================")
t.Log("Check Common Log")
t.Log("-------------------------------------------")
for _, v := range tests {
t.Log("checking ...", v.record)
log, err := ParseLogRecord(strings.Join(v.record, " "))
if !errors.Is(err, v.err) {
t.Errorf("FAIL: Error is not nil")
return
}
if log == nil {
t.Errorf("FAIL: log not returned")
return
}
l := log.(*CommonLog)
g, w := string(l.IP.String()), v.IP
compareGotWant(t, "IP", g, w)
g, w = l.Identity, v.Identity
compareGotWant(t, "Identity", g, w)
g, w = l.User, v.User
compareGotWant(t, "User", g, w)
g, w = string(l.Timestamp.String()), v.Timestamp
compareGotWant(t, "Timestamp", g, w)
g_struct, w_struct := l.Request, v.Request
compareGotWant(t, "Request", g_struct, w_struct)
g_int, w_int := l.Status, v.Status
compareGotWant(t, "Status", g_int, w_int)
g_int64, w_int64 := l.Size, v.Size
compareGotWant(t, "Size", g_int64, w_int64)
}
}
func testCombinedLog(t *testing.T) {
tests := []struct {
record []string
IP string
Identity string
User string
Timestamp string
Request request
Status int
Size int64
Referer string
UserAgent string
err error
}{
//{[]string{"127.0.0.1", "-", "rob", "[10/Oct/2000:13:55:36 -0700]", "GET /apache_pb.gif HTTP/1.0", "200", "2326"},
{[]string{"154.53.43.164", "-", "-", "[23/Dec/2021:14:41:34 -0600]", "\"GET /apache_pb.gif HTTP/1.0\"", "301", "707", "\"http://localhost/\"", "\"curl/7.68.0\""},
"154.53.43.164", "-", "-", "2021-12-23 14:41:34 -0600 -0600", request{"GET", "/apache_pb.gif", "HTTP/1.0"}, 301, 707, "http://localhost/", "curl/7.68.0", nil},
{[]string{"154.53.43.164", "-", "-", "[23/Dec/2021:14:42:03 -0600]", "\"GET /index.php/Back/installer.php HTTP/2\"", "404", "474", "\"-\"", "\"curl/7.68.0\""},
"154.53.43.164", "-", "-", "2021-12-23 14:42:03 -0600 -0600", request{"GET", "/index.php/Back/installer.php", "HTTP/2"}, 404, 474, "-", "curl/7.68.0", nil},
{[]string{"64.180.189.161", "-", "-", "[12/Dec/2022:18:41:08 -0600]", "\"GET /cpsess6383042128/3rdparty/phpMyAdmin/themes/pmahomme/img/col_pointer.png HTTP/2\"", "200", "102", "\"https://cpanel.findaroommate.com/cpsess6383042128/3rdparty/phpMyAdmin/phpmyadmin.css.php?nocache=6470895733ltr&server=1\"", "\"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36\""},
"64.180.189.161", "-", "-", "2022-12-12 18:41:08 -0600 -0600", request{"GET", "/cpsess6383042128/3rdparty/phpMyAdmin/themes/pmahomme/img/col_pointer.png", "HTTP/2"}, 200, 102, "https://cpanel.findaroommate.com/cpsess6383042128/3rdparty/phpMyAdmin/phpmyadmin.css.php?nocache=6470895733ltr&server=1", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36", nil},
}
t.Log("===========================================")
t.Log("Check Combined Log")
t.Log("-------------------------------------------")
for _, v := range tests {
t.Log("checking ...", v.record)
log, err := ParseLogRecord(strings.Join(v.record, " "))
if !errors.Is(err, v.err) {
t.Errorf("FAIL: Error is not nil, got: %v, want: %v", err, v.err)
return
}
if log == nil {
t.Errorf("FAIL: log not returned")
return
}
l := log.(*CombinedLog)
g, w := string(l.Common.IP.String()), v.IP
compareGotWant(t, "IP", g, w)
g, w = l.Common.Identity, v.Identity
compareGotWant(t, "Identity", g, w)
g, w = l.Common.User, v.User
compareGotWant(t, "User", g, w)
g, w = string(l.Common.Timestamp.String()), v.Timestamp
compareGotWant(t, "Timestamp", g, w)
g_struct, w_struct := l.Common.Request, v.Request
compareGotWant(t, "Request", g_struct, w_struct)
g_int, w_int := l.Common.Status, v.Status
compareGotWant(t, "Status", g_int, w_int)
g_int64, w_int64 := l.Common.Size, v.Size
compareGotWant(t, "Size", g_int64, w_int64)
}
}
func compareGotWant(t *testing.T, f string, g interface{}, w interface{}) {
if g != w {
t.Errorf("FAIL: correct %v not returned, got: %v, want: %v", f, g, w)
}
}
func isNil(v interface{}) bool {
return v == nil || (reflect.ValueOf(v).Kind() == reflect.Ptr && reflect.ValueOf(v).IsNil())
}