Skip to content

Commit

Permalink
Add unit tests for scanning package functions
Browse files Browse the repository at this point in the history
Signed-off-by: HAHWUL <[email protected]>
  • Loading branch information
hahwul committed Dec 7, 2024
1 parent 17ffdf7 commit 175a015
Show file tree
Hide file tree
Showing 3 changed files with 203 additions and 0 deletions.
37 changes: 37 additions & 0 deletions pkg/scanning/entity_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package scanning

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestInterfaceGetGfXSS(t *testing.T) {
list, length := InterfaceGetGfXSS()
assert.NotNil(t, list)
assert.Greater(t, length, 0)
}

func TestInterfaceGetEventHandlers(t *testing.T) {
list, length := InterfaceGetEventHandlers()
assert.NotNil(t, list)
assert.Greater(t, length, 0)
}

func TestInterfaceGetTags(t *testing.T) {
list, length := InterfaceGetTags()
assert.NotNil(t, list)
assert.Greater(t, length, 0)
}

func TestInterfaceGetSpecialChar(t *testing.T) {
list, length := InterfaceGetSpecialChar()
assert.NotNil(t, list)
assert.Greater(t, length, 0)
}

func TestInterfaceGetUsefulCode(t *testing.T) {
list, length := InterfaceGetUsefulCode()
assert.NotNil(t, list)
assert.Greater(t, length, 0)
}
106 changes: 106 additions & 0 deletions pkg/scanning/ignore_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package scanning

import "testing"

func Test_isAllowType(t *testing.T) {
type args struct {
contentType string
}
tests := []struct {
name string
args args
want bool
}{
{
name: "Allowed type - text/html",
args: args{
contentType: "text/html",
},
want: true,
},
{
name: "Not allowed type - application/json",
args: args{
contentType: "application/json",
},
want: false,
},
{
name: "Not allowed type - text/javascript",
args: args{
contentType: "text/javascript",
},
want: false,
},
{
name: "Allowed type with charset - text/html; charset=UTF-8",
args: args{
contentType: "text/html; charset=UTF-8",
},
want: true,
},
{
name: "Not allowed type with charset - application/json; charset=UTF-8",
args: args{
contentType: "application/json; charset=UTF-8",
},
want: false,
},
{
name: "Allowed type - application/xml",
args: args{
contentType: "application/xml",
},
want: true,
},
{
name: "Not allowed type - image/jpeg",
args: args{
contentType: "image/jpeg",
},
want: false,
},
{
name: "Not allowed type - image/png",
args: args{
contentType: "image/png",
},
want: false,
},
{
name: "Not allowed type - text/plain",
args: args{
contentType: "text/plain",
},
want: false,
},
{
name: "Not allowed type - text/css",
args: args{
contentType: "text/css",
},
want: false,
},
{
name: "Not allowed type - application/rss+xml",
args: args{
contentType: "application/rss+xml",
},
want: false,
},
{
name: "Allowed type - text/xml",
args: args{
contentType: "text/xml",
},
want: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := isAllowType(tt.args.contentType); got != tt.want {
t.Errorf("isAllowType() = %v, want %v", got, tt.want)
}
})
}
}
60 changes: 60 additions & 0 deletions pkg/scanning/multicast_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package scanning

import (
"reflect"
"testing"
)

func TestMakeTargetSlice(t *testing.T) {
type args struct {
targets []string
}
tests := []struct {
name string
args args
want map[string][]string
}{
{
name: "Single target",
args: args{
targets: []string{"http://example.com"},
},
want: map[string][]string{
"example.com": {"http://example.com"},
},
},
{
name: "Multiple targets with same hostname",
args: args{
targets: []string{"http://example.com", "https://example.com/path"},
},
want: map[string][]string{
"example.com": {"http://example.com", "https://example.com/path"},
},
},
{
name: "Multiple targets with different hostnames",
args: args{
targets: []string{"http://example.com", "https://another.com"},
},
want: map[string][]string{
"example.com": {"http://example.com"},
"another.com": {"https://another.com"},
},
},
{
name: "Empty targets",
args: args{
targets: []string{},
},
want: map[string][]string{},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := MakeTargetSlice(tt.args.targets); !reflect.DeepEqual(got, tt.want) {
t.Errorf("MakeTargetSlice() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit 175a015

Please sign in to comment.