mirror of
https://github.com/projectdiscovery/nuclei.git
synced 2026-01-31 15:53:10 +08:00
Added integration tests for http and network
This commit is contained in:
18
integration_tests/http/self-contained.yaml
Normal file
18
integration_tests/http/self-contained.yaml
Normal file
@@ -0,0 +1,18 @@
|
||||
id: example-self-contained-input
|
||||
|
||||
info:
|
||||
name: example-self-contained
|
||||
author: pd-team
|
||||
severity: info
|
||||
|
||||
self-contained: true
|
||||
requests:
|
||||
- raw:
|
||||
- |
|
||||
GET http://localhost:5431/ HTTP/1.1
|
||||
Host: {{Hostname}}
|
||||
|
||||
matchers:
|
||||
- type: word
|
||||
words:
|
||||
- This is self-contained response
|
||||
16
integration_tests/network/self-contained.yaml
Normal file
16
integration_tests/network/self-contained.yaml
Normal file
@@ -0,0 +1,16 @@
|
||||
id: example-self-contained-input
|
||||
|
||||
info:
|
||||
name: example-self-contained
|
||||
author: pd-team
|
||||
severity: info
|
||||
|
||||
self-contained: true
|
||||
network:
|
||||
- host:
|
||||
- "localhost:5431"
|
||||
|
||||
matchers:
|
||||
- type: word
|
||||
words:
|
||||
- "Authentication successful"
|
||||
@@ -31,6 +31,7 @@ var httpTestcases = map[string]testutils.TestCase{
|
||||
"http/raw-unsafe-request.yaml": &httpRawUnsafeRequest{},
|
||||
"http/request-condition.yaml": &httpRequestCondition{},
|
||||
"http/request-condition-new.yaml": &httpRequestCondition{},
|
||||
"http/self-contained.yaml": &httpRequestSelContained{},
|
||||
}
|
||||
|
||||
type httpGetHeaders struct{}
|
||||
@@ -493,3 +494,35 @@ func (h *httpRequestCondition) Execute(filePath string) error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type httpRequestSelContained struct{}
|
||||
|
||||
// Execute executes a test case and returns an error if occurred
|
||||
func (h *httpRequestSelContained) Execute(filePath string) error {
|
||||
router := httprouter.New()
|
||||
var routerErr error
|
||||
|
||||
router.GET("/", func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
|
||||
w.Write([]byte("This is self-contained response"))
|
||||
})
|
||||
server := &http.Server{
|
||||
Addr: "localhost:5431",
|
||||
Handler: router,
|
||||
}
|
||||
go func() {
|
||||
_ = server.ListenAndServe()
|
||||
}()
|
||||
defer server.Close()
|
||||
|
||||
results, err := testutils.RunNucleiTemplateAndGetResults(filePath, "", debug)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if routerErr != nil {
|
||||
return routerErr
|
||||
}
|
||||
if len(results) != 1 {
|
||||
return errIncorrectResultsCount(results)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -7,9 +7,10 @@ import (
|
||||
)
|
||||
|
||||
var networkTestcases = map[string]testutils.TestCase{
|
||||
"network/basic.yaml": &networkBasic{},
|
||||
"network/hex.yaml": &networkBasic{},
|
||||
"network/multi-step.yaml": &networkMultiStep{},
|
||||
"network/basic.yaml": &networkBasic{},
|
||||
"network/hex.yaml": &networkBasic{},
|
||||
"network/multi-step.yaml": &networkMultiStep{},
|
||||
"network/self-contained.yaml": &networkRequestSelContained{},
|
||||
}
|
||||
|
||||
type networkBasic struct{}
|
||||
@@ -94,3 +95,28 @@ func (h *networkMultiStep) Execute(filePath string) error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type networkRequestSelContained struct{}
|
||||
|
||||
// Execute executes a test case and returns an error if occurred
|
||||
func (h *networkRequestSelContained) Execute(filePath string) error {
|
||||
var routerErr error
|
||||
|
||||
ts := testutils.NewTCPServer(func(conn net.Conn) {
|
||||
defer conn.Close()
|
||||
|
||||
_, _ = conn.Write([]byte("Authentication successful"))
|
||||
}, 5431)
|
||||
defer ts.Close()
|
||||
results, err := testutils.RunNucleiTemplateAndGetResults(filePath, "", debug)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if routerErr != nil {
|
||||
return routerErr
|
||||
}
|
||||
if len(results) != 1 {
|
||||
return errIncorrectResultsCount(results)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -81,10 +81,14 @@ type TCPServer struct {
|
||||
}
|
||||
|
||||
// NewTCPServer creates a new TCP server from a handler
|
||||
func NewTCPServer(handler func(conn net.Conn)) *TCPServer {
|
||||
func NewTCPServer(handler func(conn net.Conn), port ...int) *TCPServer {
|
||||
server := &TCPServer{}
|
||||
|
||||
l, err := net.Listen("tcp", "127.0.0.1:0")
|
||||
var gotPort int
|
||||
if len(port) > 0 {
|
||||
gotPort = port[0]
|
||||
}
|
||||
l, err := net.Listen("tcp", fmt.Sprintf("127.0.0.1:%d", gotPort))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user