2021-02-25 23:32:43 +05:30
package main
import (
2023-01-24 22:04:52 +05:30
"flag"
2021-02-25 23:32:43 +05:30
"fmt"
"os"
"strings"
"github.com/logrusorgru/aurora"
2021-11-25 17:09:20 +02:00
2021-11-05 03:01:41 +05:30
"github.com/projectdiscovery/nuclei/v2/pkg/testutils"
2021-02-25 23:32:43 +05:30
)
var (
2021-12-14 18:13:53 +02:00
debug = os . Getenv ( "DEBUG" ) == "true"
githubAction = os . Getenv ( "GH_ACTION" ) == "true"
customTests = os . Getenv ( "TESTS" )
2021-07-30 15:29:12 +05:30
2021-12-14 18:13:53 +02:00
success = aurora . Green ( "[✓]" ) . String ( )
failed = aurora . Red ( "[✘]" ) . String ( )
2021-02-25 23:32:43 +05:30
2021-12-14 18:13:53 +02:00
protocolTests = map [ string ] map [ string ] testutils . TestCase {
2023-03-17 15:39:12 +01:00
"http" : httpTestcases ,
// "network": networkTestcases,
// "dns": dnsTestCases,
// "workflow": workflowTestcases,
// "loader": loaderTestcases,
// "websocket": websocketTestCases,
// "headless": headlessTestcases,
// "whois": whoisTestCases,
// "ssl": sslTestcases,
// "code": codeTestcases,
// "templatesPath": templatesPathTestCases,
// "templatesDir": templatesDirTestCases,
// "file": fileTestcases,
// "offlineHttp": offlineHttpTestcases,
// "customConfigDir": customConfigDirTestCases,
// "fuzzing": fuzzingTestCases,
2021-02-27 02:23:06 +05:30
}
2023-01-24 22:04:52 +05:30
// For debug purposes
runProtocol = ""
runTemplate = ""
2023-02-01 17:23:28 +05:30
extraArgs = [ ] string { }
2021-12-14 18:13:53 +02:00
)
2021-02-27 02:23:06 +05:30
2021-12-14 18:13:53 +02:00
func main ( ) {
2023-01-24 22:04:52 +05:30
flag . StringVar ( & runProtocol , "protocol" , "" , "run integration tests of given protocol" )
flag . StringVar ( & runTemplate , "template" , "" , "run integration test of given template" )
flag . Parse ( )
2023-02-01 17:23:28 +05:30
// allows passing extra args to nuclei
eargs := os . Getenv ( "DebugExtraArgs" )
if eargs != "" {
extraArgs = strings . Split ( eargs , " " )
testutils . ExtraDebugArgs = extraArgs
}
2023-01-24 22:04:52 +05:30
if runProtocol != "" {
debug = true
debugTests ( )
os . Exit ( 1 )
}
2021-12-14 18:13:53 +02:00
failedTestTemplatePaths := runTests ( toMap ( toSlice ( customTests ) ) )
if len ( failedTestTemplatePaths ) > 0 {
if githubAction {
debug = true
fmt . Println ( "::group::Failed integration tests in debug mode" )
_ = runTests ( failedTestTemplatePaths )
fmt . Println ( "::endgroup::" )
}
os . Exit ( 1 )
}
}
2023-01-24 22:04:52 +05:30
func debugTests ( ) {
for tpath , testcase := range protocolTests [ runProtocol ] {
if runTemplate != "" && ! strings . Contains ( tpath , runTemplate ) {
continue
}
if err := testcase . Execute ( tpath ) ; err != nil {
2023-02-01 17:23:28 +05:30
fmt . Printf ( "\n%v" , err . Error ( ) )
2023-01-24 22:04:52 +05:30
}
}
}
2021-12-14 18:13:53 +02:00
func runTests ( customTemplatePaths map [ string ] struct { } ) map [ string ] struct { } {
failedTestTemplatePaths := map [ string ] struct { } { }
for proto , testCases := range protocolTests {
if len ( customTemplatePaths ) == 0 {
fmt . Printf ( "Running test cases for %q protocol\n" , aurora . Blue ( proto ) )
}
for templatePath , testCase := range testCases {
if len ( customTemplatePaths ) == 0 || contains ( customTemplatePaths , templatePath ) {
2022-10-13 11:05:10 -05:00
if failedTemplatePath , err := execute ( testCase , templatePath ) ; err != nil {
2021-12-14 18:13:53 +02:00
failedTestTemplatePaths [ failedTemplatePath ] = struct { } { }
2021-02-27 02:23:06 +05:30
}
}
2021-02-25 23:32:43 +05:30
}
}
2021-12-14 18:13:53 +02:00
return failedTestTemplatePaths
}
2022-10-13 11:05:10 -05:00
func execute ( testCase testutils . TestCase , templatePath string ) ( string , error ) {
2021-12-14 18:13:53 +02:00
if err := testCase . Execute ( templatePath ) ; err != nil {
_ , _ = fmt . Fprintf ( os . Stderr , "%s Test \"%s\" failed: %s\n" , failed , templatePath , err )
2022-10-13 11:05:10 -05:00
return templatePath , err
2021-07-30 15:29:12 +05:30
}
2021-12-14 18:13:53 +02:00
fmt . Printf ( "%s Test \"%s\" passed!\n" , success , templatePath )
2022-10-13 11:05:10 -05:00
return "" , nil
2021-02-25 23:32:43 +05:30
}
2021-12-15 16:03:57 +02:00
func expectResultsCount ( results [ ] string , expectedNumber int ) error {
2023-03-17 15:39:12 +01:00
fmt . Println ( results , len ( results ) )
2021-12-15 16:03:57 +02:00
if len ( results ) != expectedNumber {
return fmt . Errorf ( "incorrect number of results: %d (actual) vs %d (expected) \nResults:\n\t%s\n" , len ( results ) , expectedNumber , strings . Join ( results , "\n\t" ) )
}
return nil
2021-02-25 23:32:43 +05:30
}
2021-12-14 18:13:53 +02:00
func toSlice ( value string ) [ ] string {
if strings . TrimSpace ( value ) == "" {
return [ ] string { }
}
return strings . Split ( value , "," )
}
func toMap ( slice [ ] string ) map [ string ] struct { } {
result := make ( map [ string ] struct { } , len ( slice ) )
for _ , value := range slice {
if _ , ok := result [ value ] ; ! ok {
result [ value ] = struct { } { }
}
}
return result
}
func contains ( input map [ string ] struct { } , value string ) bool {
_ , ok := input [ value ]
return ok
}