1
1
Fork 0
mirror of https://github.com/OJ/gobuster.git synced 2024-05-06 23:26:02 +02:00
gobuster/helper/helper_test.go
Christian Mehlmauer 0a0cab949f
Dev Updates (#305)
* retry on timeout
* Google Cloud Bucket enumeration
* colors in output
* goreleaser
* fix nil reference errors
2022-10-08 18:41:25 +02:00

121 lines
4.9 KiB
Go

package helper
import (
"reflect"
"testing"
"github.com/OJ/gobuster/v3/libgobuster"
)
func TestParseExtensions(t *testing.T) {
t.Parallel()
var tt = []struct {
testName string
extensions string
expectedExtensions libgobuster.Set[string]
expectedError string
}{
{"Valid extensions", "php,asp,txt", libgobuster.Set[string]{Set: map[string]bool{"php": true, "asp": true, "txt": true}}, ""},
{"Spaces", "php, asp , txt", libgobuster.Set[string]{Set: map[string]bool{"php": true, "asp": true, "txt": true}}, ""},
{"Double extensions", "php,asp,txt,php,asp,txt", libgobuster.Set[string]{Set: map[string]bool{"php": true, "asp": true, "txt": true}}, ""},
{"Leading dot", ".php,asp,.txt", libgobuster.Set[string]{Set: map[string]bool{"php": true, "asp": true, "txt": true}}, ""},
{"Empty string", "", libgobuster.NewSet[string](), "invalid extension string provided"},
}
for _, x := range tt {
x := x // NOTE: https://github.com/golang/go/wiki/CommonMistakes#using-goroutines-on-loop-iterator-variables
t.Run(x.testName, func(t *testing.T) {
t.Parallel()
ret, err := ParseExtensions(x.extensions)
if x.expectedError != "" {
if err != nil && err.Error() != x.expectedError {
t.Fatalf("Expected error %q but got %q", x.expectedError, err.Error())
}
} else if !reflect.DeepEqual(x.expectedExtensions, ret) {
t.Fatalf("Expected %v but got %v", x.expectedExtensions, ret)
}
})
}
}
func TestParseCommaSeparatedInt(t *testing.T) {
t.Parallel()
var tt = []struct {
testName string
stringCodes string
expectedCodes libgobuster.Set[int]
expectedError string
}{
{"Valid codes", "200,100,202", libgobuster.Set[int]{Set: map[int]bool{100: true, 200: true, 202: true}}, ""},
{"Spaces", "200, 100 , 202", libgobuster.Set[int]{Set: map[int]bool{100: true, 200: true, 202: true}}, ""},
{"Double codes", "200, 100, 202, 100", libgobuster.Set[int]{Set: map[int]bool{100: true, 200: true, 202: true}}, ""},
{"Invalid code", "200,AAA", libgobuster.NewSet[int](), "invalid string given: AAA"},
{"Invalid integer", "2000000000000000000000000000000", libgobuster.NewSet[int](), "invalid string given: 2000000000000000000000000000000"},
{"Empty string", "", libgobuster.NewSet[int](), "invalid string provided"},
}
for _, x := range tt {
x := x // NOTE: https://github.com/golang/go/wiki/CommonMistakes#using-goroutines-on-loop-iterator-variables
t.Run(x.testName, func(t *testing.T) {
t.Parallel()
ret, err := ParseCommaSeparatedInt(x.stringCodes)
if x.expectedError != "" {
if err != nil && err.Error() != x.expectedError {
t.Fatalf("Expected error %q but got %q", x.expectedError, err.Error())
}
} else if !reflect.DeepEqual(x.expectedCodes, ret) {
t.Fatalf("Expected %v but got %v", x.expectedCodes, ret)
}
})
}
}
func BenchmarkParseExtensions(b *testing.B) {
var tt = []struct {
testName string
extensions string
expectedExtensions libgobuster.Set[string]
expectedError string
}{
{"Valid extensions", "php,asp,txt", libgobuster.Set[string]{Set: map[string]bool{"php": true, "asp": true, "txt": true}}, ""},
{"Spaces", "php, asp , txt", libgobuster.Set[string]{Set: map[string]bool{"php": true, "asp": true, "txt": true}}, ""},
{"Double extensions", "php,asp,txt,php,asp,txt", libgobuster.Set[string]{Set: map[string]bool{"php": true, "asp": true, "txt": true}}, ""},
{"Leading dot", ".php,asp,.txt", libgobuster.Set[string]{Set: map[string]bool{"php": true, "asp": true, "txt": true}}, ""},
{"Empty string", "", libgobuster.NewSet[string](), "invalid extension string provided"},
}
for _, x := range tt {
x := x // NOTE: https://github.com/golang/go/wiki/CommonMistakes#using-goroutines-on-loop-iterator-variables
b.Run(x.testName, func(b2 *testing.B) {
for y := 0; y < b2.N; y++ {
_, _ = ParseExtensions(x.extensions)
}
})
}
}
func BenchmarkParseCommaSeparatedInt(b *testing.B) {
var tt = []struct {
testName string
stringCodes string
expectedCodes libgobuster.Set[int]
expectedError string
}{
{"Valid codes", "200,100,202", libgobuster.Set[int]{Set: map[int]bool{100: true, 200: true, 202: true}}, ""},
{"Spaces", "200, 100 , 202", libgobuster.Set[int]{Set: map[int]bool{100: true, 200: true, 202: true}}, ""},
{"Double codes", "200, 100, 202, 100", libgobuster.Set[int]{Set: map[int]bool{100: true, 200: true, 202: true}}, ""},
{"Invalid code", "200,AAA", libgobuster.NewSet[int](), "invalid string given: AAA"},
{"Invalid integer", "2000000000000000000000000000000", libgobuster.NewSet[int](), "invalid string given: 2000000000000000000000000000000"},
{"Empty string", "", libgobuster.NewSet[int](), "invalid string string provided"},
}
for _, x := range tt {
x := x // NOTE: https://github.com/golang/go/wiki/CommonMistakes#using-goroutines-on-loop-iterator-variables
b.Run(x.testName, func(b2 *testing.B) {
for y := 0; y < b2.N; y++ {
_, _ = ParseCommaSeparatedInt(x.stringCodes)
}
})
}
}