-
Notifications
You must be signed in to change notification settings - Fork 635
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
remove redundant c.browser.Connect() (#428)
* remove redundant c.browser.Connect() * Add functional test * create config folder if doesn't exist * change test compare logic * fix lint error * create incognito browser in New * fix nil pointer err --------- Co-authored-by: Tarun Koyalwar <[email protected]>
- Loading branch information
1 parent
dcddcf6
commit 7275aa4
Showing
8 changed files
with
194 additions
and
14 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,30 @@ | ||
name: 🧪 Functional Test | ||
|
||
on: | ||
pull_request: | ||
paths: | ||
- '**.go' | ||
- '**.mod' | ||
workflow_dispatch: | ||
|
||
jobs: | ||
functional: | ||
name: Functional Test | ||
runs-on: ${{ matrix.os }} | ||
strategy: | ||
matrix: | ||
os: [ubuntu-latest, windows-latest, macOS-latest] | ||
steps: | ||
- name: Set up Go | ||
uses: actions/setup-go@v4 | ||
with: | ||
go-version: 1.19 | ||
|
||
- name: Check out code | ||
uses: actions/checkout@v3 | ||
|
||
- name: Functional Tests | ||
run: | | ||
chmod +x run.sh | ||
bash run.sh ${{ matrix.os }} | ||
working-directory: cmd/functional-test |
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,58 @@ | ||
package main | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
"log" | ||
"os" | ||
"strings" | ||
|
||
"github.com/logrusorgru/aurora" | ||
"github.com/pkg/errors" | ||
"github.com/projectdiscovery/katana/internal/testutils" | ||
) | ||
|
||
var ( | ||
debug = os.Getenv("DEBUG") == "true" | ||
success = aurora.Green("[✓]").String() | ||
failed = aurora.Red("[✘]").String() | ||
errored = false | ||
devKatanaBinary = flag.String("dev", "", "Dev Branch Katana Binary") | ||
) | ||
|
||
func main() { | ||
flag.Parse() | ||
if err := runFunctionalTests(); err != nil { | ||
log.Fatalf("Could not run functional tests: %s\n", err) | ||
} | ||
if errored { | ||
os.Exit(1) | ||
} | ||
} | ||
|
||
func runFunctionalTests() error { | ||
for _, testcase := range testutils.TestCases { | ||
if err := runIndividualTestCase(testcase); err != nil { | ||
errored = true | ||
fmt.Fprintf(os.Stderr, "%s Test \"%s\" failed: %s\n", failed, testcase.Name, err) | ||
} else { | ||
fmt.Printf("%s Test \"%s\" passed!\n", success, testcase.Name) | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func runIndividualTestCase(testcase testutils.TestCase) error { | ||
argsParts := strings.Fields(testcase.Args) | ||
devOutput, err := testutils.RunKatanaBinaryAndGetResults(testcase.Target, *devKatanaBinary, debug, argsParts) | ||
if err != nil { | ||
return errors.Wrap(err, "could not run Katana dev test") | ||
} | ||
if testcase.CompareFunc != nil { | ||
return testcase.CompareFunc(testcase.Target, devOutput) | ||
} | ||
if !testutils.CompareOutput(devOutput, testcase.Expected) { | ||
return errors.Errorf("expected output %s, got %s", testcase.Expected, devOutput) | ||
} | ||
return nil | ||
} |
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,20 @@ | ||
#!/bin/bash | ||
|
||
# reading os type from arguments | ||
CURRENT_OS=$1 | ||
|
||
if [ "${CURRENT_OS}" == "windows-latest" ];then | ||
extension=.exe | ||
fi | ||
|
||
echo "::group::Building functional-test binary" | ||
go build -o functional-test$extension | ||
echo "::endgroup::" | ||
|
||
echo "::group::Building katana binary from current branch" | ||
go build -o katana_dev$extension ../katana | ||
echo "::endgroup::" | ||
|
||
|
||
echo 'Starting katana functional test' | ||
./functional-test$extension -dev ./katana_dev$extension |
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
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,13 @@ | ||
package testutils | ||
|
||
func CompareOutput(input, expected []string) bool { | ||
if len(input) != len(expected) { | ||
return false | ||
} | ||
for i, v := range input { | ||
if v != expected[i] { | ||
return false | ||
} | ||
} | ||
return true | ||
} |
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,27 @@ | ||
package testutils | ||
|
||
import ( | ||
"fmt" | ||
"os/exec" | ||
"strings" | ||
) | ||
|
||
func RunKatanaBinaryAndGetResults(target string, katanaBinary string, debug bool, args []string) ([]string, error) { | ||
cmd := exec.Command("bash", "-c") | ||
cmdLine := fmt.Sprintf(`echo %s | %s `, target, katanaBinary) | ||
cmdLine += strings.Join(args, " ") | ||
|
||
cmd.Args = append(cmd.Args, cmdLine) | ||
data, err := cmd.Output() | ||
if err != nil { | ||
return nil, err | ||
} | ||
parts := []string{} | ||
items := strings.Split(string(data), "\n") | ||
for _, i := range items { | ||
if i != "" { | ||
parts = append(parts, i) | ||
} | ||
} | ||
return parts, nil | ||
} |
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,32 @@ | ||
package testutils | ||
|
||
import ( | ||
"strings" | ||
|
||
errorutils "github.com/projectdiscovery/utils/errors" | ||
) | ||
|
||
type TestCase struct { | ||
Name string | ||
Target string | ||
Args string | ||
Expected []string | ||
CompareFunc func(target string, got []string) error | ||
} | ||
|
||
var TestCases = []TestCase{ | ||
{ | ||
Name: "Headless Browser Without Incognito", | ||
Target: "https://www.hackerone.com/", | ||
Expected: nil, | ||
Args: "-headless -no-incognito -depth 2 -silent", | ||
CompareFunc: func(target string, got []string) error { | ||
for _, res := range got { | ||
if strings.Contains(res, target) { | ||
return nil | ||
} | ||
} | ||
return errorutils.New("expected %v target in output, but got %v ", target, strings.Join(got, "\n")) | ||
}, | ||
}, | ||
} |
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