128 lines
3.0 KiB
Go
Raw Normal View History

package main
import (
"bufio"
"flag"
"fmt"
"log"
"os"
"strings"
"github.com/kitabisa/go-ci"
"github.com/logrusorgru/aurora"
"github.com/pkg/errors"
2021-09-07 17:31:46 +03:00
"github.com/projectdiscovery/nuclei/v3/pkg/testutils"
)
var (
success = aurora.Green("[✓]").String()
failed = aurora.Red("[✘]").String()
mainNucleiBinary = flag.String("main", "", "Main Branch Nuclei Binary")
devNucleiBinary = flag.String("dev", "", "Dev Branch Nuclei Binary")
testcases = flag.String("testcases", "", "Test cases file for nuclei functional tests")
)
func main() {
flag.Parse()
debug := os.Getenv("DEBUG") == "true" || os.Getenv("RUNNER_DEBUG") == "1"
if err, errored := runFunctionalTests(debug); err != nil {
log.Fatalf("Could not run functional tests: %s\n", err)
} else if errored {
os.Exit(1)
}
}
func runFunctionalTests(debug bool) (error, bool) {
file, err := os.Open(*testcases)
if err != nil {
return errors.Wrap(err, "could not open test cases"), true
}
defer func() {
Remove singletons from Nuclei engine (continuation of #6210) (#6296) * introducing execution id * wip * . * adding separate execution context id * lint * vet * fixing pg dialers * test ignore * fixing loader FD limit * test * fd fix * wip: remove CloseProcesses() from dev merge * wip: fix merge issue * protocolstate: stop memguarding on last dialer delete * avoid data race in dialers.RawHTTPClient * use shared logger and avoid race conditions * use shared logger and avoid race conditions * go mod * patch executionId into compiled template cache * clean up comment in Parse * go mod update * bump echarts * address merge issues * fix use of gologger * switch cmd/nuclei to options.Logger * address merge issues with go.mod * go vet: address copy of lock with new Copy function * fixing tests * disable speed control * fix nil ExecuterOptions * removing deprecated code * fixing result print * default logger * cli default logger * filter warning from results * fix performance test * hardcoding path * disable upload * refactor(runner): uses `Warning` instead of `Print` for `pdcpUploadErrMsg` Signed-off-by: Dwi Siswanto <git@dw1.io> * Revert "disable upload" This reverts commit 114fbe6663361bf41cf8b2645fd2d57083d53682. * Revert "hardcoding path" This reverts commit cf12ca800e0a0e974bd9fd4826a24e51547f7c00. --------- Signed-off-by: Dwi Siswanto <git@dw1.io> Co-authored-by: Mzack9999 <mzack9999@protonmail.com> Co-authored-by: Dwi Siswanto <git@dw1.io> Co-authored-by: Dwi Siswanto <25837540+dwisiswant0@users.noreply.github.com>
2025-07-09 14:47:26 -05:00
_ = file.Close()
}()
errored, failedTestCases := runTestCases(file, debug)
if ci.IsCI() {
fmt.Println("::group::Failed tests with debug")
for _, failedTestCase := range failedTestCases {
_ = runTestCase(failedTestCase, true)
}
fmt.Println("::endgroup::")
}
return nil, errored
}
func runTestCases(file *os.File, debug bool) (bool, []string) {
errored := false
var failedTestCases []string
scanner := bufio.NewScanner(file)
for scanner.Scan() {
testCase := strings.TrimSpace(scanner.Text())
if testCase == "" {
continue
}
// skip comments
if strings.HasPrefix(testCase, "#") {
continue
}
if runTestCase(testCase, debug) {
errored = true
failedTestCases = append(failedTestCases, testCase)
}
}
return errored, failedTestCases
}
func runTestCase(testCase string, debug bool) bool {
if err := runIndividualTestCase(testCase, debug); err != nil {
fmt.Fprintf(os.Stderr, "%s Test \"%s\" failed: %s\n", failed, testCase, err)
return true
} else {
fmt.Printf("%s Test \"%s\" passed!\n", success, testCase)
}
return false
}
func runIndividualTestCase(testcase string, debug bool) error {
quoted := false
// split upon unquoted spaces
parts := strings.FieldsFunc(testcase, func(r rune) bool {
if r == '"' {
quoted = !quoted
}
return !quoted && r == ' '
})
// Quoted strings containing spaces are expressions and must have trailing \" removed
for index, part := range parts {
if strings.Contains(part, " ") {
parts[index] = strings.Trim(part, "\"")
}
}
var finalArgs []string
if len(parts) > 1 {
finalArgs = parts[1:]
}
mainOutput, err := testutils.RunNucleiBinaryAndGetLoadedTemplates(*mainNucleiBinary, debug, finalArgs)
if err != nil {
return errors.Wrap(err, "could not run nuclei main test")
}
devOutput, err := testutils.RunNucleiBinaryAndGetLoadedTemplates(*devNucleiBinary, debug, finalArgs)
if err != nil {
return errors.Wrap(err, "could not run nuclei dev test")
}
if mainOutput == devOutput {
return nil
}
2021-09-07 17:31:46 +03:00
return fmt.Errorf("%s main is not equal to %s dev", mainOutput, devOutput)
}