nuclei/v2/cmd/integration-test/integration-test.go

133 lines
3.5 KiB
Go
Raw Normal View History

package main
import (
"flag"
"fmt"
"os"
"strings"
"github.com/logrusorgru/aurora"
2021-11-25 15:09:20 +00:00
"github.com/projectdiscovery/nuclei/v2/pkg/testutils"
2023-03-17 16:31:28 +00:00
sliceutil "github.com/projectdiscovery/utils/slice"
)
var (
debug = os.Getenv("DEBUG") == "true"
githubAction = os.Getenv("GH_ACTION") == "true"
customTests = os.Getenv("TESTS")
success = aurora.Green("[✓]").String()
failed = aurora.Red("[✘]").String()
protocolTests = map[string]map[string]testutils.TestCase{
2023-03-17 15:56:14 +00: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-26 20:53:06 +00:00
}
// For debug purposes
runProtocol = ""
runTemplate = ""
extraArgs = []string{}
)
2021-02-26 20:53:06 +00:00
func main() {
flag.StringVar(&runProtocol, "protocol", "", "run integration tests of given protocol")
flag.StringVar(&runTemplate, "template", "", "run integration test of given template")
flag.Parse()
// allows passing extra args to nuclei
eargs := os.Getenv("DebugExtraArgs")
if eargs != "" {
extraArgs = strings.Split(eargs, " ")
testutils.ExtraDebugArgs = extraArgs
}
if runProtocol != "" {
debug = true
debugTests()
os.Exit(1)
}
2023-03-17 16:31:28 +00:00
customTestsList := normalizeSplit(customTests)
failedTestTemplatePaths := runTests(customTestsList)
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)
}
}
func debugTests() {
for tpath, testcase := range protocolTests[runProtocol] {
if runTemplate != "" && !strings.Contains(tpath, runTemplate) {
continue
}
if err := testcase.Execute(tpath); err != nil {
fmt.Printf("\n%v", err.Error())
}
}
}
2023-03-17 16:31:28 +00:00
func runTests(customTemplatePaths []string) []string {
var failedTestTemplatePaths []string
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 {
2023-03-17 16:31:28 +00:00
if len(customTemplatePaths) == 0 || sliceutil.Contains(customTemplatePaths, templatePath) {
if failedTemplatePath, err := execute(testCase, templatePath); err != nil {
2023-03-17 16:31:28 +00:00
failedTestTemplatePaths = append(failedTestTemplatePaths, failedTemplatePath)
2021-02-26 20:53:06 +00:00
}
}
}
}
return failedTestTemplatePaths
}
func execute(testCase testutils.TestCase, templatePath string) (string, error) {
if err := testCase.Execute(templatePath); err != nil {
_, _ = fmt.Fprintf(os.Stderr, "%s Test \"%s\" failed: %s\n", failed, templatePath, err)
return templatePath, err
}
fmt.Printf("%s Test \"%s\" passed!\n", success, templatePath)
return "", nil
}
func expectResultsCount(results []string, expectedNumber int) error {
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
}
2023-03-17 16:31:28 +00:00
func normalizeSplit(str string) []string {
return strings.Split(strings.TrimSpace(str), ",")
}