-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathfiles_test.go
More file actions
83 lines (69 loc) · 2.19 KB
/
files_test.go
File metadata and controls
83 lines (69 loc) · 2.19 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
package httpfuzz
import (
"bytes"
"fmt"
"testing"
)
func TestGenerateFileDifferentFileTypes(t *testing.T) {
const expectedFileLength = int64(1024)
for fileType, header := range headerRegistry {
file, err := GenerateFile(fileType, expectedFileLength, "")
if err != nil {
t.Fatal(err)
}
fileBytes := file.Payload
actualFileLength := int64(len(fileBytes))
if actualFileLength != file.Size {
t.Fatalf("File size does not match metadata, expected %d, got %d", actualFileLength, file.Size)
}
expectedFileName := fmt.Sprintf("name.%s", fileType)
if file.Name != expectedFileName {
t.Fatalf("Expected filename %s, got %s", expectedFileName, file.Name)
}
if actualFileLength != expectedFileLength {
t.Fatalf("Expected file of size %d bytes, got %d bytes", expectedFileLength, actualFileLength)
}
if !bytes.Equal(file.Header, header) {
t.Fatalf("File metadata header must match one provided")
}
if !bytes.HasPrefix(fileBytes, header) {
t.Fatal("File first bytes must be the file header")
}
}
}
func TestFileFromReturnsProperContents(t *testing.T) {
file, err := FileFrom("./testpayloads/payload.php", "jpg")
if err != nil {
t.Fatal(err)
}
const expectedPayload = "<?php echo phpinfo(); ?>"
actualPayload := string(file.Payload)
if actualPayload != expectedPayload {
t.Fatalf("Expected %s, got %s", expectedPayload, actualPayload)
}
if file.Size != int64(len(actualPayload)) {
t.Fatalf("Mismatched file metadata size.")
}
const expectedFileName = "payload.php.jpg"
if file.Name != expectedFileName {
t.Fatalf("Expected %s, got %s", expectedFileName, file.Name)
}
}
func TestFileFromReturnsProperFilenameWithEmptyExtension(t *testing.T) {
file, err := FileFrom("./testpayloads/payload.php", "")
if err != nil {
t.Fatal(err)
}
const expectedPayload = "<?php echo phpinfo(); ?>"
actualPayload := string(file.Payload)
if actualPayload != expectedPayload {
t.Fatalf("Expected %s, got %s", expectedPayload, actualPayload)
}
if file.Size != int64(len(actualPayload)) {
t.Fatalf("Mismatched file metadata size.")
}
const expectedFileName = "payload.php"
if file.Name != expectedFileName {
t.Fatalf("Expected %s, got %s", expectedFileName, file.Name)
}
}