-
-
Notifications
You must be signed in to change notification settings - Fork 436
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add unit tests for scanning package functions
Signed-off-by: HAHWUL <[email protected]>
- Loading branch information
Showing
7 changed files
with
764 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
package scanning | ||
|
||
import ( | ||
"io" | ||
"io/ioutil" | ||
"net/http" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/hahwul/dalfox/v2/pkg/model" | ||
) | ||
|
||
func TestMakePoC(t *testing.T) { | ||
type args struct { | ||
poc string | ||
req *http.Request | ||
options model.Options | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want string | ||
}{ | ||
{ | ||
name: "HTTP RAW REQUEST", | ||
args: args{ | ||
poc: "http://example.com", | ||
req: func() *http.Request { | ||
req, _ := http.NewRequest(http.MethodGet, "http://example.com", nil) | ||
return req | ||
}(), | ||
options: model.Options{ | ||
PoCType: "http-request", | ||
}, | ||
}, | ||
want: "HTTP RAW REQUEST\nGET / HTTP/1.1\r\nHost: example.com\r\nUser-Agent: Go-http-client/1.1\r\nAccept-Encoding: gzip\r\n\r\n", | ||
}, | ||
{ | ||
name: "curl with body", | ||
args: args{ | ||
poc: "http://example.com", | ||
req: func() *http.Request { | ||
body := ioutil.NopCloser(strings.NewReader("test body")) | ||
req, _ := http.NewRequest(http.MethodPost, "http://example.com", body) | ||
req.GetBody = func() (io.ReadCloser, error) { | ||
return ioutil.NopCloser(strings.NewReader("test body")), nil | ||
} | ||
return req | ||
}(), | ||
options: model.Options{ | ||
PoCType: "curl", | ||
}, | ||
}, | ||
want: "curl -i -k -X POST http://example.com -d \"test body\"", | ||
}, | ||
{ | ||
name: "httpie with body", | ||
args: args{ | ||
poc: "http://example.com", | ||
req: func() *http.Request { | ||
body := ioutil.NopCloser(strings.NewReader("test body")) | ||
req, _ := http.NewRequest(http.MethodPost, "http://example.com", body) | ||
req.GetBody = func() (io.ReadCloser, error) { | ||
return ioutil.NopCloser(strings.NewReader("test body")), nil | ||
} | ||
return req | ||
}(), | ||
options: model.Options{ | ||
PoCType: "httpie", | ||
}, | ||
}, | ||
want: "http POST http://example.com \"test body\" --verify=false -f", | ||
}, | ||
{ | ||
name: "curl without body", | ||
args: args{ | ||
poc: "http://example.com", | ||
req: func() *http.Request { | ||
req, _ := http.NewRequest(http.MethodGet, "http://example.com", nil) | ||
return req | ||
}(), | ||
options: model.Options{ | ||
PoCType: "curl", | ||
}, | ||
}, | ||
want: "curl -i -k http://example.com", | ||
}, | ||
{ | ||
name: "httpie without body", | ||
args: args{ | ||
poc: "http://example.com", | ||
req: func() *http.Request { | ||
req, _ := http.NewRequest(http.MethodGet, "http://example.com", nil) | ||
return req | ||
}(), | ||
options: model.Options{ | ||
PoCType: "httpie", | ||
}, | ||
}, | ||
want: "http http://example.com --verify=false", | ||
}, | ||
{ | ||
name: "default without body", | ||
args: args{ | ||
poc: "http://example.com", | ||
req: func() *http.Request { | ||
req, _ := http.NewRequest(http.MethodGet, "http://example.com", nil) | ||
return req | ||
}(), | ||
options: model.Options{ | ||
PoCType: "default", | ||
}, | ||
}, | ||
want: "http://example.com", | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if got := MakePoC(tt.args.poc, tt.args.req, tt.args.options); got != tt.want { | ||
t.Errorf("MakePoC() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package scanning | ||
|
||
import "testing" | ||
|
||
func Test_checkVStatus(t *testing.T) { | ||
type args struct { | ||
vStatus map[string]bool | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want bool | ||
}{ | ||
{ | ||
name: "All true values", | ||
args: args{ | ||
vStatus: map[string]bool{ | ||
"status1": true, | ||
"status2": true, | ||
}, | ||
}, | ||
want: true, | ||
}, | ||
{ | ||
name: "Contains false value", | ||
args: args{ | ||
vStatus: map[string]bool{ | ||
"status1": true, | ||
"status2": false, | ||
}, | ||
}, | ||
want: false, | ||
}, | ||
{ | ||
name: "Special key with false value", | ||
args: args{ | ||
vStatus: map[string]bool{ | ||
"pleasedonthaveanamelikethis_plz_plz": false, | ||
}, | ||
}, | ||
want: false, | ||
}, | ||
{ | ||
name: "Special key with true value", | ||
args: args{ | ||
vStatus: map[string]bool{ | ||
"pleasedonthaveanamelikethis_plz_plz": true, | ||
}, | ||
}, | ||
want: false, | ||
}, | ||
{ | ||
name: "Empty map", | ||
args: args{ | ||
vStatus: map[string]bool{}, | ||
}, | ||
want: true, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if got := checkVStatus(tt.args.vStatus); got != tt.want { | ||
t.Errorf("checkVStatus() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package scanning | ||
|
||
import ( | ||
"net/http" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/hahwul/dalfox/v2/pkg/model" | ||
) | ||
|
||
func TestSendReq(t *testing.T) { | ||
type args struct { | ||
req *http.Request | ||
payload string | ||
options model.Options | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want string | ||
want1 *http.Response | ||
want2 bool | ||
want3 bool | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "Successful request", | ||
args: args{ | ||
req: func() *http.Request { | ||
req, _ := http.NewRequest(http.MethodGet, "https://dalfox.hahwul.com", nil) | ||
return req | ||
}(), | ||
payload: "test-payload", | ||
options: model.Options{ | ||
Timeout: 10, | ||
}, | ||
}, | ||
want: "dalfox", | ||
want1: &http.Response{StatusCode: http.StatusOK}, | ||
want2: false, | ||
want3: false, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "Request with error", | ||
args: args{ | ||
req: func() *http.Request { | ||
req, _ := http.NewRequest(http.MethodGet, "http://invalid-url", nil) | ||
return req | ||
}(), | ||
payload: "test-payload", | ||
options: model.Options{ | ||
Timeout: 10, | ||
}, | ||
}, | ||
want: "", | ||
want1: nil, | ||
want2: false, | ||
want3: false, | ||
wantErr: true, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got, got1, got2, got3, err := SendReq(tt.args.req, tt.args.payload, tt.args.options) | ||
if (err != nil) != tt.wantErr { | ||
t.Errorf("SendReq() error = %v, wantErr %v", err, tt.wantErr) | ||
return | ||
} | ||
if !strings.Contains(got, tt.want) { | ||
t.Errorf("SendReq() got = %v, want %v", got, tt.want) | ||
} | ||
if got1 != nil && tt.want1 != nil && got1.StatusCode != tt.want1.StatusCode { | ||
t.Errorf("SendReq() got1 = %v, want %v", got1.StatusCode, tt.want1.StatusCode) | ||
} | ||
if got2 != tt.want2 { | ||
t.Errorf("SendReq() got2 = %v, want %v", got2, tt.want2) | ||
} | ||
if got3 != tt.want3 { | ||
t.Errorf("SendReq() got3 = %v, want %v", got3, tt.want3) | ||
} | ||
}) | ||
} | ||
} |
Oops, something went wrong.