Skip to content

Commit dc6117b

Browse files
author
shadowy-pycoder
committed
Added basic tests for ftp, http, tls parsers
1 parent e6b715e commit dc6117b

File tree

7 files changed

+218
-1
lines changed

7 files changed

+218
-1
lines changed

layers/ftp.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ func (f *FTPMessage) Summary() string {
2121
}
2222

2323
func (f *FTPMessage) Parse(data []byte) error {
24-
sp := bytes.Split(data, lf)
24+
f.summary = nil
25+
f.data = nil
26+
sp := bytes.Split(data, crlf)
2527
lsp := len(sp)
2628
switch {
2729
case lsp > 2:

layers/ftp_test.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package layers
2+
3+
import (
4+
"fmt"
5+
"io"
6+
"testing"
7+
8+
"github.com/stretchr/testify/require"
9+
)
10+
11+
func BenchmarkParseFTP(b *testing.B) {
12+
packet, close := testPacketBench(b, "ftp")
13+
defer close()
14+
b.ResetTimer()
15+
ftp := &FTPMessage{}
16+
for i := 0; i < b.N; i++ {
17+
_ = ftp.Parse(packet)
18+
fmt.Fprint(io.Discard, ftp.String())
19+
}
20+
}
21+
22+
func TestParseFTP(t *testing.T) {
23+
expected := &FTPMessage{
24+
summary: []byte{
25+
0x32, 0x31, 0x31, 0x2d, 0x53, 0x75, 0x70, 0x70, 0x6f, 0x72,
26+
0x74, 0x65, 0x64, 0x20, 0x65, 0x78, 0x74, 0x65, 0x6e, 0x73,
27+
0x69, 0x6f, 0x6e, 0x73, 0x3a, 0x20, 0x20, 0x41, 0x55, 0x54,
28+
0x48, 0x20, 0x54, 0x4c, 0x53, 0x3b, 0x53, 0x53, 0x4c, 0x3b},
29+
data: []byte{
30+
0x2d, 0x20, 0x32, 0x31, 0x31, 0x2d, 0x53, 0x75, 0x70, 0x70, 0x6f, 0x72, 0x74, 0x65, 0x64, 0x20,
31+
0x65, 0x78, 0x74, 0x65, 0x6e, 0x73, 0x69, 0x6f, 0x6e, 0x73, 0x3a, 0x0a, 0x2d, 0x20, 0x20, 0x41,
32+
0x55, 0x54, 0x48, 0x20, 0x54, 0x4c, 0x53, 0x3b, 0x53, 0x53, 0x4c, 0x3b, 0x0a, 0x2d, 0x20, 0x20,
33+
0x43, 0x44, 0x55, 0x50, 0x0a, 0x2d, 0x20, 0x20, 0x43, 0x4c, 0x4e, 0x54, 0x0a, 0x2d, 0x20, 0x20,
34+
0x43, 0x53, 0x49, 0x44, 0x0a, 0x2d, 0x20, 0x20, 0x45, 0x50, 0x53, 0x56, 0x0a, 0x2d, 0x20, 0x20,
35+
0x45, 0x50, 0x52, 0x54, 0x0a, 0x2d, 0x20, 0x20, 0x4d, 0x44, 0x54, 0x4d, 0x0a, 0x2d, 0x20, 0x20,
36+
0x4d, 0x46, 0x4d, 0x54, 0x0a, 0x2d, 0x20, 0x20, 0x4d, 0x4c, 0x53, 0x54, 0x20, 0x54, 0x79, 0x70,
37+
0x65, 0x2a, 0x3b, 0x4d, 0x6f, 0x64, 0x69, 0x66, 0x79, 0x2a, 0x3b, 0x43, 0x72, 0x65, 0x61, 0x74,
38+
0x65, 0x2a, 0x3b, 0x57, 0x69, 0x6e, 0x33, 0x32, 0x2e, 0x65, 0x61, 0x3b, 0x53, 0x69, 0x7a, 0x65,
39+
0x2a, 0x3b, 0x0a, 0x2d, 0x20, 0x20, 0x50, 0x41, 0x53, 0x56, 0x0a, 0x2d, 0x20, 0x20, 0x50, 0x42,
40+
0x53, 0x5a, 0x0a, 0x2d, 0x20, 0x20, 0x50, 0x4f, 0x52, 0x54, 0x0a, 0x2d, 0x20, 0x20, 0x50, 0x52,
41+
0x4f, 0x54, 0x20, 0x43, 0x3b, 0x50, 0x3b, 0x0a, 0x2d, 0x20, 0x20, 0x52, 0x45, 0x53, 0x54, 0x20,
42+
0x53, 0x54, 0x52, 0x45, 0x41, 0x4d, 0x0a, 0x2d, 0x20, 0x20, 0x53, 0x49, 0x5a, 0x45, 0x0a, 0x2d,
43+
0x20, 0x20, 0x54, 0x56, 0x46, 0x53, 0x0a, 0x2d, 0x20, 0x20, 0x55, 0x54, 0x46, 0x38, 0x0a, 0x2d,
44+
0x20, 0x32, 0x31, 0x31, 0x20, 0x45, 0x6e, 0x64, 0x2e},
45+
}
46+
ftp := &FTPMessage{}
47+
packet, close := testPacket(t, "ftp")
48+
defer close()
49+
if err := ftp.Parse(packet); err != nil {
50+
t.Fatal(err)
51+
}
52+
require.Equal(t, expected, ftp)
53+
}

layers/http_test.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package layers
2+
3+
import (
4+
"fmt"
5+
"io"
6+
"testing"
7+
8+
"github.com/stretchr/testify/require"
9+
)
10+
11+
func BenchmarkParseHTTP(b *testing.B) {
12+
packet, close := testPacketBench(b, "http")
13+
defer close()
14+
b.ResetTimer()
15+
http := &HTTPMessage{}
16+
for i := 0; i < b.N; i++ {
17+
_ = http.Parse(packet)
18+
fmt.Fprint(io.Discard, http.String())
19+
}
20+
}
21+
22+
func TestParseHTTP(t *testing.T) {
23+
expected := &HTTPMessage{
24+
summary: []byte{
25+
0x50, 0x4f, 0x53, 0x54, 0x20, 0x2f, 0x61, 0x70, 0x69, 0x20,
26+
0x48, 0x54, 0x54, 0x50, 0x2f, 0x31, 0x2e, 0x31, 0x20, 0x68,
27+
0x6f, 0x73, 0x74, 0x3a, 0x20, 0x31, 0x34, 0x39, 0x2e, 0x31,
28+
0x35, 0x34, 0x2e, 0x31, 0x36, 0x37, 0x2e, 0x32, 0x32, 0x32,
29+
0x3a, 0x38, 0x30,
30+
},
31+
data: []byte{
32+
33+
0x2d, 0x20, 0x50, 0x4f, 0x53, 0x54, 0x20, 0x2f, 0x61, 0x70, 0x69, 0x20, 0x48, 0x54, 0x54, 0x50,
34+
0x2f, 0x31, 0x2e, 0x31, 0x0a, 0x2d, 0x20, 0x68, 0x6f, 0x73, 0x74, 0x3a, 0x20, 0x31, 0x34, 0x39,
35+
0x2e, 0x31, 0x35, 0x34, 0x2e, 0x31, 0x36, 0x37, 0x2e, 0x32, 0x32, 0x32, 0x3a, 0x38, 0x30, 0x0a,
36+
0x2d, 0x20, 0x63, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x6c, 0x65, 0x6e, 0x67, 0x74, 0x68,
37+
0x3a, 0x20, 0x32, 0x31, 0x32, 0x0a, 0x2d, 0x20, 0x63, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d,
38+
0x74, 0x79, 0x70, 0x65, 0x3a, 0x20, 0x61, 0x70, 0x70, 0x6c, 0x69, 0x63, 0x61, 0x74, 0x69, 0x6f,
39+
0x6e, 0x2f, 0x78, 0x2d, 0x77, 0x77, 0x77, 0x2d, 0x66, 0x6f, 0x72, 0x6d, 0x2d, 0x75, 0x72, 0x6c,
40+
0x65, 0x6e, 0x63, 0x6f, 0x64, 0x65, 0x64, 0x0a, 0x2d, 0x20, 0x63, 0x6f, 0x6e, 0x6e, 0x65, 0x63,
41+
0x74, 0x69, 0x6f, 0x6e, 0x3a, 0x20, 0x4b, 0x65, 0x65, 0x70, 0x2d, 0x41, 0x6c, 0x69, 0x76, 0x65,
42+
0x0a, 0x2d, 0x20, 0x61, 0x63, 0x63, 0x65, 0x70, 0x74, 0x2d, 0x65, 0x6e, 0x63, 0x6f, 0x64, 0x69,
43+
0x6e, 0x67, 0x3a, 0x20, 0x67, 0x7a, 0x69, 0x70, 0x2c, 0x20, 0x64, 0x65, 0x66, 0x6c, 0x61, 0x74,
44+
0x65, 0x0a, 0x2d, 0x20, 0x61, 0x63, 0x63, 0x65, 0x70, 0x74, 0x2d, 0x6c, 0x61, 0x6e, 0x67, 0x75,
45+
0x61, 0x67, 0x65, 0x3a, 0x20, 0x65, 0x6e, 0x2d, 0x55, 0x53, 0x2c, 0x2a, 0x0a, 0x2d, 0x20, 0x75,
46+
0x73, 0x65, 0x72, 0x2d, 0x61, 0x67, 0x65, 0x6e, 0x74, 0x3a, 0x20, 0x4d, 0x6f, 0x7a, 0x69, 0x6c,
47+
0x6c, 0x61, 0x2f, 0x35, 0x2e, 0x30,
48+
},
49+
}
50+
http := &HTTPMessage{}
51+
packet, close := testPacket(t, "http")
52+
defer close()
53+
if err := http.Parse(packet); err != nil {
54+
t.Fatal(err)
55+
}
56+
require.Equal(t, expected, http)
57+
}

layers/testdata/ftp.bin

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
211-Supported extensions:
2+
AUTH TLS;SSL;
3+
CDUP
4+
CLNT
5+
CSID
6+
EPSV
7+
EPRT
8+
MDTM
9+
MFMT
10+
MLST Type*;Modify*;Create*;Win32.ea;Size*;
11+
PASV
12+
PBSZ
13+
PORT
14+
PROT C;P;
15+
REST STREAM
16+
SIZE
17+
TVFS
18+
UTF8
19+
211 End.

layers/testdata/http.bin

437 Bytes
Binary file not shown.

layers/testdata/tls.bin

340 Bytes
Binary file not shown.

layers/tls_test.go

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package layers
2+
3+
import (
4+
"fmt"
5+
"io"
6+
"testing"
7+
8+
"github.com/stretchr/testify/require"
9+
)
10+
11+
func BenchmarkParseTLS(b *testing.B) {
12+
packet, close := testPacketBench(b, "tls")
13+
defer close()
14+
b.ResetTimer()
15+
tls := &TLSMessage{}
16+
for i := 0; i < b.N; i++ {
17+
_ = tls.Parse(packet)
18+
fmt.Fprint(io.Discard, tls.String())
19+
}
20+
}
21+
22+
func TestParseTLS(t *testing.T) {
23+
expected := &TLSMessage{
24+
Records: []*Record{{
25+
ContentType: 23,
26+
ContentTypeDesc: "Application Data",
27+
Version: 0x0303,
28+
VersionDesc: "TLS 1.2",
29+
Length: 23,
30+
data: []byte{
31+
0x42, 0xe3, 0xf1, 0x65, 0x80, 0x55, 0x3f, 0x84, 0x2e, 0x54,
32+
0x80, 0xa6, 0xe9, 0x2b, 0x6e, 0xce, 0xdb, 0x0f, 0xb2, 0x53,
33+
0x2e, 0x5f, 0x3e},
34+
},
35+
{
36+
ContentType: 23,
37+
ContentTypeDesc: "Application Data",
38+
Version: 0x0303,
39+
VersionDesc: "TLS 1.2",
40+
Length: 34,
41+
data: []byte{
42+
0x44, 0xed, 0x12, 0xf5, 0x5d, 0x28, 0x1e, 0x64, 0xba, 0x28,
43+
0x39, 0x1a, 0x20, 0x5b, 0xb7, 0x14, 0xff, 0x5c, 0x48, 0x43,
44+
0x04, 0x0e, 0x50, 0x13, 0xdf, 0x19, 0xff, 0x26, 0x53, 0xbb,
45+
0x22, 0xbc, 0xd0, 0x7c,
46+
},
47+
}},
48+
Data: []byte{
49+
0xe5, 0x23, 0x87, 0x66, 0x6d, 0xe7, 0xe7, 0x2e, 0xf5, 0x96,
50+
0x64, 0x59, 0x5d, 0x90, 0xd0, 0xa3, 0x47, 0x0c, 0x05, 0x0d,
51+
0x63, 0x48, 0x06, 0x1c, 0x32, 0x05, 0x33, 0x07, 0x99, 0x16,
52+
0xb8, 0x82, 0x62, 0xa9, 0xab, 0x8a, 0x70, 0x55, 0x35, 0xc6,
53+
0xb0, 0x1e, 0x08, 0xfc, 0xec, 0xf8, 0xc0, 0x55, 0x62, 0xf7,
54+
0x6e, 0xb6, 0x98, 0x89, 0xc9, 0x0c, 0xc0, 0xa2, 0xbe, 0x57,
55+
0xe2, 0x02, 0x38, 0x32, 0xb0, 0xf9, 0x70, 0x08, 0x0d, 0x77,
56+
0x40, 0xb6, 0x29, 0x5e, 0x98, 0xc4, 0x80, 0x9b, 0xd6, 0x3c,
57+
0xab, 0xcf, 0xc2, 0xf8, 0xba, 0x7e, 0xc4, 0x76, 0x3b, 0x31,
58+
0x7f, 0xc4, 0x4d, 0x26, 0xc7, 0x43, 0xf5, 0x0c, 0x6f, 0xc4,
59+
0xf9, 0x2f, 0xdd, 0x71, 0xe3, 0xee, 0x3c, 0xd7, 0xab, 0x94,
60+
0xfd, 0xbf, 0xe9, 0x36, 0xf2, 0x16, 0x09, 0xbd, 0xe5, 0xd3,
61+
0x37, 0x80, 0x51, 0x78, 0x95, 0x47, 0xfd, 0x41, 0x41, 0xa1,
62+
0x8d, 0x23, 0x27, 0xa7, 0xd2, 0xb8, 0xcf, 0xfa, 0xd8, 0x46,
63+
0x1a, 0xeb, 0x5b, 0xdf, 0x28, 0x07, 0x36, 0xe5, 0x07, 0xb7,
64+
0x70, 0xda, 0x06, 0xec, 0x13, 0x3a, 0xd4, 0x4b, 0xdb, 0x13,
65+
0x2a, 0x39, 0x22, 0x3f, 0x04, 0xdd, 0x87, 0xf4, 0xba, 0x9c,
66+
0xc5, 0xcd, 0xb9, 0x7d, 0x38, 0x0d, 0x86, 0xa5, 0xab, 0xdf,
67+
0x2e, 0xa4, 0x51, 0x34, 0xa1, 0xe6, 0x30, 0x45, 0x99, 0x38,
68+
0xcb, 0xa8, 0x84, 0xf6, 0x15, 0x03, 0x1b, 0x5a, 0xf6, 0xbd,
69+
0x56, 0xef, 0xbf, 0xc7, 0xbb, 0x18, 0xe4, 0x6a, 0xac, 0xac,
70+
0xa5, 0xe5, 0xee, 0xed, 0x58, 0x99, 0x83, 0x17, 0x03, 0x03,
71+
0x00, 0x17, 0x2b, 0x8f, 0xfc, 0xba, 0x61, 0xb4, 0x63, 0x0a,
72+
0xa6, 0x9a, 0xf4, 0xce, 0x17, 0x1b, 0xa3, 0x20, 0x6e, 0x80,
73+
0x9f, 0x3d, 0xf7, 0x32, 0x46, 0x17, 0x03, 0x03, 0x00, 0x17,
74+
0xa5, 0x10, 0x47, 0x96, 0x0e, 0x03, 0x36, 0xb7, 0x84, 0x2c,
75+
0xa7, 0xc5, 0x4f, 0xbc, 0x52, 0x2e, 0x5c, 0x56, 0x60, 0x72,
76+
0x05, 0x18, 0x98},
77+
}
78+
79+
tls := &TLSMessage{}
80+
packet, close := testPacket(t, "tls")
81+
defer close()
82+
if err := tls.Parse(packet); err != nil {
83+
t.Fatal(err)
84+
}
85+
require.Equal(t, expected, tls)
86+
}

0 commit comments

Comments
 (0)