|
|
@@ -10,9 +10,10 @@ import (
|
|
|
"sort"
|
|
|
"testing"
|
|
|
|
|
|
- "github.com/zricethezav/gitleaks/v6/config"
|
|
|
- "github.com/zricethezav/gitleaks/v6/manager"
|
|
|
- "github.com/zricethezav/gitleaks/v6/options"
|
|
|
+ "github.com/zricethezav/gitleaks/v7/report"
|
|
|
+
|
|
|
+ "github.com/zricethezav/gitleaks/v7/config"
|
|
|
+ "github.com/zricethezav/gitleaks/v7/options"
|
|
|
|
|
|
"github.com/sergi/go-diff/diffmatchpatch"
|
|
|
)
|
|
|
@@ -27,13 +28,14 @@ func TestScan(t *testing.T) {
|
|
|
opts options.Options
|
|
|
wantPath string
|
|
|
wantErr error
|
|
|
+ wantScanErr error
|
|
|
emptyRepo bool
|
|
|
wantEmpty bool
|
|
|
}{
|
|
|
{
|
|
|
description: "test local repo one aws leak",
|
|
|
opts: options.Options{
|
|
|
- RepoPath: "../test_data/test_repos/test_repo_1",
|
|
|
+ Path: "../test_data/test_repos/test_repo_1",
|
|
|
Report: "../test_data/test_local_repo_one_aws_leak.json.got",
|
|
|
ReportFormat: "json",
|
|
|
},
|
|
|
@@ -43,7 +45,7 @@ func TestScan(t *testing.T) {
|
|
|
description: "test local repo one aws leak threaded",
|
|
|
opts: options.Options{
|
|
|
Threads: runtime.GOMAXPROCS(0),
|
|
|
- RepoPath: "../test_data/test_repos/test_repo_1",
|
|
|
+ Path: "../test_data/test_repos/test_repo_1",
|
|
|
Report: "../test_data/test_local_repo_one_aws_leak.json.got",
|
|
|
ReportFormat: "json",
|
|
|
},
|
|
|
@@ -52,24 +54,25 @@ func TestScan(t *testing.T) {
|
|
|
{
|
|
|
description: "test non existent repo",
|
|
|
opts: options.Options{
|
|
|
- RepoPath: "../test_data/test_repos/no_repo_here",
|
|
|
+ Path: "../test_data/test_repos/no_repo_here",
|
|
|
ReportFormat: "json",
|
|
|
},
|
|
|
+ wantErr: fmt.Errorf("stat ../test_data/test_repos/no_repo_here: no such file or directory"),
|
|
|
emptyRepo: true,
|
|
|
},
|
|
|
{
|
|
|
description: "test local repo one aws leak allowlisted",
|
|
|
opts: options.Options{
|
|
|
- RepoPath: "../test_data/test_repos/test_repo_1",
|
|
|
+ Path: "../test_data/test_repos/test_repo_1",
|
|
|
ReportFormat: "json",
|
|
|
- Config: "../test_data/test_configs/aws_key_allowlist_python_files.toml",
|
|
|
+ ConfigPath: "../test_data/test_configs/aws_key_allowlist_python_files.toml",
|
|
|
},
|
|
|
wantEmpty: true,
|
|
|
},
|
|
|
{
|
|
|
description: "test local repo two leaks",
|
|
|
opts: options.Options{
|
|
|
- RepoPath: "../test_data/test_repos/test_repo_2",
|
|
|
+ Path: "../test_data/test_repos/test_repo_2",
|
|
|
Report: "../test_data/test_local_repo_two_leaks.json.got",
|
|
|
ReportFormat: "json",
|
|
|
},
|
|
|
@@ -78,7 +81,7 @@ func TestScan(t *testing.T) {
|
|
|
{
|
|
|
description: "test local repo two leaks from Commit",
|
|
|
opts: options.Options{
|
|
|
- RepoPath: "../test_data/test_repos/test_repo_2",
|
|
|
+ Path: "../test_data/test_repos/test_repo_2",
|
|
|
Report: "../test_data/test_local_repo_two_leaks_commit_from.json.got",
|
|
|
ReportFormat: "json",
|
|
|
CommitFrom: "996865bb912f3bc45898a370a13aadb315014b55",
|
|
|
@@ -88,7 +91,7 @@ func TestScan(t *testing.T) {
|
|
|
{
|
|
|
description: "test local repo two leaks to Commit",
|
|
|
opts: options.Options{
|
|
|
- RepoPath: "../test_data/test_repos/test_repo_2",
|
|
|
+ Path: "../test_data/test_repos/test_repo_2",
|
|
|
Report: "../test_data/test_local_repo_two_leaks_commit_to.json.got",
|
|
|
ReportFormat: "json",
|
|
|
CommitTo: "996865bb912f3bc45898a370a13aadb315014b55",
|
|
|
@@ -98,7 +101,7 @@ func TestScan(t *testing.T) {
|
|
|
{
|
|
|
description: "test local repo two leaks to from Commit",
|
|
|
opts: options.Options{
|
|
|
- RepoPath: "../test_data/test_repos/test_repo_2",
|
|
|
+ Path: "../test_data/test_repos/test_repo_2",
|
|
|
Report: "../test_data/test_local_repo_two_leaks_commit_to_from.json.got",
|
|
|
ReportFormat: "json",
|
|
|
CommitFrom: "d8ac0b73aeeb45843319cdc5ce506516eb49bf7a",
|
|
|
@@ -109,7 +112,7 @@ func TestScan(t *testing.T) {
|
|
|
{
|
|
|
description: "test local repo two leaks list Commits",
|
|
|
opts: options.Options{
|
|
|
- RepoPath: "../test_data/test_repos/test_repo_2",
|
|
|
+ Path: "../test_data/test_repos/test_repo_2",
|
|
|
Report: "../test_data/test_local_repo_two_leaks_commit_range.json.got",
|
|
|
ReportFormat: "json",
|
|
|
Commits: "d8ac0b73aeeb45843319cdc5ce506516eb49bf7a,996865bb912f3bc45898a370a13aadb315014b55,17471a5fda722a9e423f1a0d3f0d267ea009d41c,51f6dcf6b89b93f4075ba92c400b075631a6cc93,b10b3e2cb320a8c211fda94c4567299d37de7776",
|
|
|
@@ -119,7 +122,7 @@ func TestScan(t *testing.T) {
|
|
|
{
|
|
|
description: "test local repo two leaks file list commits",
|
|
|
opts: options.Options{
|
|
|
- RepoPath: "../test_data/test_repos/test_repo_2",
|
|
|
+ Path: "../test_data/test_repos/test_repo_2",
|
|
|
Report: "../test_data/test_local_repo_two_leaks_file_commit_range.json.got",
|
|
|
ReportFormat: "json",
|
|
|
CommitsFile: "../test_data/test_options/test_local_repo_commits.txt",
|
|
|
@@ -129,8 +132,8 @@ func TestScan(t *testing.T) {
|
|
|
{
|
|
|
description: "test local repo two leaks globally allowlisted",
|
|
|
opts: options.Options{
|
|
|
- RepoPath: "../test_data/test_repos/test_repo_2",
|
|
|
- Config: "../test_data/test_configs/aws_key_global_allowlist_file.toml",
|
|
|
+ Path: "../test_data/test_repos/test_repo_2",
|
|
|
+ ConfigPath: "../test_data/test_configs/aws_key_global_allowlist_file.toml",
|
|
|
ReportFormat: "json",
|
|
|
},
|
|
|
wantEmpty: true,
|
|
|
@@ -138,8 +141,8 @@ func TestScan(t *testing.T) {
|
|
|
{
|
|
|
description: "test local repo two leaks allowlisted",
|
|
|
opts: options.Options{
|
|
|
- RepoPath: "../test_data/test_repos/test_repo_2",
|
|
|
- Config: "../test_data/test_configs/aws_key_allowlist_files.toml",
|
|
|
+ Path: "../test_data/test_repos/test_repo_2",
|
|
|
+ ConfigPath: "../test_data/test_configs/aws_key_allowlist_files.toml",
|
|
|
ReportFormat: "json",
|
|
|
},
|
|
|
wantEmpty: true,
|
|
|
@@ -147,9 +150,9 @@ func TestScan(t *testing.T) {
|
|
|
{
|
|
|
description: "test local repo three leaks dev branch with reportGroup set",
|
|
|
opts: options.Options{
|
|
|
- RepoPath: "../test_data/test_repos/test_repo_3",
|
|
|
+ Path: "../test_data/test_repos/test_repo_3",
|
|
|
Report: "../test_data/test_local_repo_three_leaks_with_report_groups.json.got",
|
|
|
- Config: "../test_data/test_configs/aws_key_with_report_groups.toml",
|
|
|
+ ConfigPath: "../test_data/test_configs/aws_key_with_report_groups.toml",
|
|
|
Branch: "dev",
|
|
|
ReportFormat: "json",
|
|
|
},
|
|
|
@@ -158,9 +161,9 @@ func TestScan(t *testing.T) {
|
|
|
{
|
|
|
description: "test local repo three leaks dev branch",
|
|
|
opts: options.Options{
|
|
|
- RepoPath: "../test_data/test_repos/test_repo_3",
|
|
|
+ Path: "../test_data/test_repos/test_repo_3",
|
|
|
Report: "../test_data/test_local_repo_three_leaks.json.got",
|
|
|
- Config: "../test_data/test_configs/aws_key.toml",
|
|
|
+ ConfigPath: "../test_data/test_configs/aws_key.toml",
|
|
|
Branch: "dev",
|
|
|
ReportFormat: "json",
|
|
|
},
|
|
|
@@ -169,16 +172,16 @@ func TestScan(t *testing.T) {
|
|
|
{
|
|
|
description: "test local repo branch does not exist",
|
|
|
opts: options.Options{
|
|
|
- RepoPath: "../test_data/test_repos/test_repo_3",
|
|
|
+ Path: "../test_data/test_repos/test_repo_3",
|
|
|
Branch: "nobranch",
|
|
|
ReportFormat: "json",
|
|
|
},
|
|
|
- wantEmpty: true,
|
|
|
+ wantScanErr: fmt.Errorf("could not find branch nobranch"),
|
|
|
},
|
|
|
{
|
|
|
description: "test local repo one aws leak single Commit",
|
|
|
opts: options.Options{
|
|
|
- RepoPath: "../test_data/test_repos/test_repo_1",
|
|
|
+ Path: "../test_data/test_repos/test_repo_1",
|
|
|
Report: "../test_data/test_local_repo_one_aws_leak_commit.json.got",
|
|
|
Commit: "6557c92612d3b35979bd426d429255b3bf9fab74",
|
|
|
ReportFormat: "json",
|
|
|
@@ -188,9 +191,9 @@ func TestScan(t *testing.T) {
|
|
|
{
|
|
|
description: "test local repo one aws leak AND leak on python files",
|
|
|
opts: options.Options{
|
|
|
- RepoPath: "../test_data/test_repos/test_repo_1",
|
|
|
+ Path: "../test_data/test_repos/test_repo_1",
|
|
|
Report: "../test_data/test_local_repo_one_aws_leak_and_file_leak.json.got",
|
|
|
- Config: "../test_data/test_configs/aws_key_file_regex.toml",
|
|
|
+ ConfigPath: "../test_data/test_configs/aws_key_file_regex.toml",
|
|
|
ReportFormat: "json",
|
|
|
},
|
|
|
wantPath: "../test_data/test_local_repo_one_aws_leak_and_file_leak.json",
|
|
|
@@ -198,7 +201,7 @@ func TestScan(t *testing.T) {
|
|
|
{
|
|
|
description: "test owner path",
|
|
|
opts: options.Options{
|
|
|
- OwnerPath: "../test_data/test_repos/",
|
|
|
+ Path: "../test_data/test_repos/",
|
|
|
Report: "../test_data/test_local_owner_aws_leak.json.got",
|
|
|
ReportFormat: "json",
|
|
|
},
|
|
|
@@ -207,19 +210,19 @@ func TestScan(t *testing.T) {
|
|
|
{
|
|
|
description: "test owner path allowlist repo",
|
|
|
opts: options.Options{
|
|
|
- OwnerPath: "../test_data/test_repos/",
|
|
|
+ Path: "../test_data/test_repos/",
|
|
|
Report: "../test_data/test_local_owner_aws_leak_allowlist_repo.json.got",
|
|
|
ReportFormat: "json",
|
|
|
- Config: "../test_data/test_configs/aws_key_local_owner_allowlist_repo.toml",
|
|
|
+ ConfigPath: "../test_data/test_configs/aws_key_local_owner_allowlist_repo.toml",
|
|
|
},
|
|
|
wantPath: "../test_data/test_local_owner_aws_leak_allowlist_repo.json",
|
|
|
},
|
|
|
{
|
|
|
description: "test entropy and regex",
|
|
|
opts: options.Options{
|
|
|
- RepoPath: "../test_data/test_repos/test_repo_1",
|
|
|
+ Path: "../test_data/test_repos/test_repo_1",
|
|
|
Report: "../test_data/test_regex_entropy.json.got",
|
|
|
- Config: "../test_data/test_configs/regex_entropy.toml",
|
|
|
+ ConfigPath: "../test_data/test_configs/regex_entropy.toml",
|
|
|
ReportFormat: "json",
|
|
|
},
|
|
|
wantPath: "../test_data/test_regex_entropy.json",
|
|
|
@@ -227,47 +230,27 @@ func TestScan(t *testing.T) {
|
|
|
{
|
|
|
description: "test local repo four entropy alternative config",
|
|
|
opts: options.Options{
|
|
|
- RepoPath: "../test_data/test_repos/test_repo_4",
|
|
|
- Report: "../test_data/test_local_repo_four_alt_config_entropy.json.got",
|
|
|
- RepoConfig: true,
|
|
|
- ReportFormat: "json",
|
|
|
+ Path: "../test_data/test_repos/test_repo_4",
|
|
|
+ Report: "../test_data/test_local_repo_four_alt_config_entropy.json.got",
|
|
|
+ RepoConfigPath: "gitleaks.toml",
|
|
|
+ ReportFormat: "json",
|
|
|
},
|
|
|
wantPath: "../test_data/test_local_repo_four_alt_config_entropy.json",
|
|
|
},
|
|
|
{
|
|
|
description: "test local repo four entropy alternative config",
|
|
|
opts: options.Options{
|
|
|
- RepoPath: "../test_data/test_repos/test_repo_1",
|
|
|
+ Path: "../test_data/test_repos/test_repo_1",
|
|
|
Report: "../test_data/test_regex_allowlist.json.got",
|
|
|
- Config: "../test_data/test_configs/aws_key_aws_allowlisted.toml",
|
|
|
+ ConfigPath: "../test_data/test_configs/aws_key_aws_allowlisted.toml",
|
|
|
ReportFormat: "json",
|
|
|
},
|
|
|
wantEmpty: true,
|
|
|
},
|
|
|
- {
|
|
|
- description: "test local repo one aws leak timeout",
|
|
|
- opts: options.Options{
|
|
|
- RepoPath: "../test_data/test_repos/test_repo_1",
|
|
|
- Report: "../test_data/test_local_repo_one_aws_leak.json.got",
|
|
|
- ReportFormat: "json",
|
|
|
- Timeout: "10ns",
|
|
|
- },
|
|
|
- wantEmpty: true,
|
|
|
- },
|
|
|
- {
|
|
|
- description: "test local repo one aws leak long timeout",
|
|
|
- opts: options.Options{
|
|
|
- RepoPath: "../test_data/test_repos/test_repo_1",
|
|
|
- Report: "../test_data/test_local_repo_one_aws_leak.json.got",
|
|
|
- ReportFormat: "json",
|
|
|
- Timeout: "2m",
|
|
|
- },
|
|
|
- wantPath: "../test_data/test_local_repo_one_aws_leak.json",
|
|
|
- },
|
|
|
{
|
|
|
description: "test owner path depth=2",
|
|
|
opts: options.Options{
|
|
|
- OwnerPath: "../test_data/test_repos/",
|
|
|
+ Path: "../test_data/test_repos/",
|
|
|
Report: "../test_data/test_local_owner_aws_leak_depth_2.json.got",
|
|
|
ReportFormat: "json",
|
|
|
Depth: 2,
|
|
|
@@ -277,7 +260,7 @@ func TestScan(t *testing.T) {
|
|
|
{
|
|
|
description: "test local repo five files at Commit",
|
|
|
opts: options.Options{
|
|
|
- RepoPath: "../test_data/test_repos/test_repo_5",
|
|
|
+ Path: "../test_data/test_repos/test_repo_5",
|
|
|
Report: "../test_data/test_local_repo_five_files_at_commit.json.got",
|
|
|
FilesAtCommit: "a4c9fb737d5552fd96fce5cc7eedb23353ba9ed0",
|
|
|
ReportFormat: "json",
|
|
|
@@ -287,7 +270,7 @@ func TestScan(t *testing.T) {
|
|
|
{
|
|
|
description: "test local repo five files at latest Commit",
|
|
|
opts: options.Options{
|
|
|
- RepoPath: "../test_data/test_repos/test_repo_5",
|
|
|
+ Path: "../test_data/test_repos/test_repo_5",
|
|
|
Report: "../test_data/test_local_repo_five_files_at_latest_commit.json.got",
|
|
|
FilesAtCommit: "latest",
|
|
|
ReportFormat: "json",
|
|
|
@@ -297,31 +280,31 @@ func TestScan(t *testing.T) {
|
|
|
{
|
|
|
description: "test local repo five at Commit",
|
|
|
opts: options.Options{
|
|
|
- RepoPath: "../test_data/test_repos/test_repo_5",
|
|
|
+ Path: "../test_data/test_repos/test_repo_5",
|
|
|
Report: "../test_data/test_local_repo_five_commit.json.got",
|
|
|
Commit: "a4c9fb737d5552fd96fce5cc7eedb23353ba9ed0",
|
|
|
ReportFormat: "json",
|
|
|
- Config: "../test_data/test_configs/generic.toml",
|
|
|
+ ConfigPath: "../test_data/test_configs/generic.toml",
|
|
|
},
|
|
|
wantPath: "../test_data/test_local_repo_five_commit.json",
|
|
|
},
|
|
|
{
|
|
|
description: "test local repo five at latest Commit",
|
|
|
opts: options.Options{
|
|
|
- RepoPath: "../test_data/test_repos/test_repo_5",
|
|
|
+ Path: "../test_data/test_repos/test_repo_5",
|
|
|
Report: "../test_data/test_local_repo_five_at_latest_commit.json.got",
|
|
|
Commit: "latest",
|
|
|
ReportFormat: "json",
|
|
|
- Config: "../test_data/test_configs/generic.toml",
|
|
|
+ ConfigPath: "../test_data/test_configs/generic.toml",
|
|
|
},
|
|
|
wantPath: "../test_data/test_local_repo_five_at_latest_commit.json",
|
|
|
},
|
|
|
{
|
|
|
description: "test local repo six filename",
|
|
|
opts: options.Options{
|
|
|
- RepoPath: "../test_data/test_repos/test_repo_6",
|
|
|
+ Path: "../test_data/test_repos/test_repo_6",
|
|
|
Report: "../test_data/test_local_repo_six_filename.json.got",
|
|
|
- Config: "../test_data/test_configs/regex_filename.toml",
|
|
|
+ ConfigPath: "../test_data/test_configs/regex_filename.toml",
|
|
|
ReportFormat: "json",
|
|
|
},
|
|
|
wantPath: "../test_data/test_local_repo_six_filename.json",
|
|
|
@@ -329,9 +312,9 @@ func TestScan(t *testing.T) {
|
|
|
{
|
|
|
description: "test local repo six filepath",
|
|
|
opts: options.Options{
|
|
|
- RepoPath: "../test_data/test_repos/test_repo_6",
|
|
|
+ Path: "../test_data/test_repos/test_repo_6",
|
|
|
Report: "../test_data/test_local_repo_six_filepath.json.got",
|
|
|
- Config: "../test_data/test_configs/regex_filepath.toml",
|
|
|
+ ConfigPath: "../test_data/test_configs/regex_filepath.toml",
|
|
|
ReportFormat: "json",
|
|
|
},
|
|
|
wantPath: "../test_data/test_local_repo_six_filepath.json",
|
|
|
@@ -339,9 +322,9 @@ func TestScan(t *testing.T) {
|
|
|
{
|
|
|
description: "test local repo six filename and filepath",
|
|
|
opts: options.Options{
|
|
|
- RepoPath: "../test_data/test_repos/test_repo_6",
|
|
|
+ Path: "../test_data/test_repos/test_repo_6",
|
|
|
Report: "../test_data/test_local_repo_six_filepath_filename.json.got",
|
|
|
- Config: "../test_data/test_configs/regex_filepath_filename.toml",
|
|
|
+ ConfigPath: "../test_data/test_configs/regex_filepath_filename.toml",
|
|
|
ReportFormat: "json",
|
|
|
},
|
|
|
wantPath: "../test_data/test_local_repo_six_filepath_filename.json",
|
|
|
@@ -349,9 +332,9 @@ func TestScan(t *testing.T) {
|
|
|
{
|
|
|
description: "test local repo six path globally allowlisted",
|
|
|
opts: options.Options{
|
|
|
- RepoPath: "../test_data/test_repos/test_repo_6",
|
|
|
+ Path: "../test_data/test_repos/test_repo_6",
|
|
|
Report: "../test_data/test_local_repo_six_path_globally_allowlisted.json.got",
|
|
|
- Config: "../test_data/test_configs/aws_key_global_allowlist_path.toml",
|
|
|
+ ConfigPath: "../test_data/test_configs/aws_key_global_allowlist_path.toml",
|
|
|
ReportFormat: "json",
|
|
|
},
|
|
|
wantPath: "../test_data/test_local_repo_six_path_globally_allowlisted.json",
|
|
|
@@ -359,7 +342,7 @@ func TestScan(t *testing.T) {
|
|
|
{
|
|
|
description: "test local repo six leaks since date",
|
|
|
opts: options.Options{
|
|
|
- RepoPath: "../test_data/test_repos/test_repo_6",
|
|
|
+ Path: "../test_data/test_repos/test_repo_6",
|
|
|
Report: "../test_data/test_local_repo_six_leaks_since_date.json.got",
|
|
|
ReportFormat: "json",
|
|
|
CommitSince: "2019-10-25",
|
|
|
@@ -369,7 +352,7 @@ func TestScan(t *testing.T) {
|
|
|
{
|
|
|
description: "test local repo two leaks until date",
|
|
|
opts: options.Options{
|
|
|
- RepoPath: "../test_data/test_repos/test_repo_6",
|
|
|
+ Path: "../test_data/test_repos/test_repo_6",
|
|
|
Report: "../test_data/test_local_repo_six_leaks_until_date.json.got",
|
|
|
ReportFormat: "json",
|
|
|
CommitUntil: "2019-10-25",
|
|
|
@@ -379,7 +362,7 @@ func TestScan(t *testing.T) {
|
|
|
{
|
|
|
description: "test local repo four leaks timerange Commit",
|
|
|
opts: options.Options{
|
|
|
- RepoPath: "../test_data/test_repos/test_repo_4",
|
|
|
+ Path: "../test_data/test_repos/test_repo_4",
|
|
|
Report: "../test_data/test_local_repo_four_leaks_commit_timerange.json.got",
|
|
|
ReportFormat: "json",
|
|
|
CommitSince: "2019-10-25T13:01:27-0400",
|
|
|
@@ -390,27 +373,17 @@ func TestScan(t *testing.T) {
|
|
|
{
|
|
|
description: "test local repo two allowlist Commit config",
|
|
|
opts: options.Options{
|
|
|
- RepoPath: "../test_data/test_repos/test_repo_2",
|
|
|
+ Path: "../test_data/test_repos/test_repo_2",
|
|
|
Report: "../test_data/test_local_repo_two_allowlist_commits.json.got",
|
|
|
- Config: "../test_data/test_configs/allowlist_commit.toml",
|
|
|
+ ConfigPath: "../test_data/test_configs/allowlist_commit.toml",
|
|
|
ReportFormat: "json",
|
|
|
},
|
|
|
wantPath: "../test_data/test_local_repo_two_allowlist_commits.json",
|
|
|
},
|
|
|
- {
|
|
|
- description: "test local repo two deletion",
|
|
|
- opts: options.Options{
|
|
|
- RepoPath: "../test_data/test_repos/test_repo_2",
|
|
|
- Report: "../test_data/test_local_repo_two_leaks_deletion.json.got",
|
|
|
- ReportFormat: "json",
|
|
|
- Deletion: true,
|
|
|
- },
|
|
|
- wantPath: "../test_data/test_local_repo_two_leaks_deletion.json",
|
|
|
- },
|
|
|
{
|
|
|
description: "test local repo eight (merges)",
|
|
|
opts: options.Options{
|
|
|
- RepoPath: "../test_data/test_repos/test_repo_8",
|
|
|
+ Path: "../test_data/test_repos/test_repo_8",
|
|
|
Report: "../test_data/test_local_repo_eight.json.got",
|
|
|
ReportFormat: "json",
|
|
|
},
|
|
|
@@ -419,13 +392,33 @@ func TestScan(t *testing.T) {
|
|
|
{
|
|
|
description: "test local repo nine",
|
|
|
opts: options.Options{
|
|
|
- RepoPath: "../test_data/test_repos/test_repo_9",
|
|
|
+ Path: "../test_data/test_repos/test_repo_9",
|
|
|
Report: "../test_data/test_local_repo_nine_aws_leak.json.got",
|
|
|
- Config: "../test_data/test_configs/large_with_global_allowlist_regex.toml",
|
|
|
+ ConfigPath: "../test_data/test_configs/large_with_global_allowlist_regex.toml",
|
|
|
ReportFormat: "json",
|
|
|
},
|
|
|
wantPath: "../test_data/test_local_repo_nine_aws_leak.json",
|
|
|
},
|
|
|
+ {
|
|
|
+ description: "test dir one no git",
|
|
|
+ opts: options.Options{
|
|
|
+ Path: "../test_data/test_repos/test_dir_1",
|
|
|
+ Report: "../test_data/test_dir1_aws_leak.json.got",
|
|
|
+ ReportFormat: "json",
|
|
|
+ NoGit: true,
|
|
|
+ },
|
|
|
+ wantPath: "../test_data/test_dir1_aws_leak.json",
|
|
|
+ },
|
|
|
+ {
|
|
|
+ description: "test file with leak no git",
|
|
|
+ opts: options.Options{
|
|
|
+ Path: "../test_data/test_repos/test_dir_1/server.test.py",
|
|
|
+ Report: "../test_data/test_file1_aws_leak.json.got",
|
|
|
+ ReportFormat: "json",
|
|
|
+ NoGit: true,
|
|
|
+ },
|
|
|
+ wantPath: "../test_data/test_file1_aws_leak.json",
|
|
|
+ },
|
|
|
}
|
|
|
|
|
|
for _, test := range tests {
|
|
|
@@ -435,31 +428,43 @@ func TestScan(t *testing.T) {
|
|
|
t.Error(err)
|
|
|
}
|
|
|
|
|
|
- m, err := manager.NewManager(test.opts, cfg)
|
|
|
+ scanner, err := NewScanner(test.opts, cfg)
|
|
|
+ if test.wantErr != nil {
|
|
|
+ if err == nil {
|
|
|
+ t.Fatalf("did not receive wantErr: %v", test.wantErr)
|
|
|
+ }
|
|
|
+ if err.Error() != test.wantErr.Error() {
|
|
|
+ t.Fatalf("wantErr does not equal err received: %v", err.Error())
|
|
|
+ }
|
|
|
+ continue
|
|
|
+ }
|
|
|
if err != nil {
|
|
|
- t.Error(err)
|
|
|
+ t.Fatal(err)
|
|
|
}
|
|
|
|
|
|
- err = Run(m)
|
|
|
+ scannerReport, err := scanner.Scan()
|
|
|
|
|
|
- if test.wantErr != nil {
|
|
|
+ if test.wantScanErr != nil {
|
|
|
if err == nil {
|
|
|
- t.Errorf("did not receive wantErr: %v", test.wantErr)
|
|
|
+ t.Fatalf("did not receive wantErr: %v", test.wantScanErr)
|
|
|
}
|
|
|
- if err.Error() != test.wantErr.Error() {
|
|
|
- t.Errorf("wantErr does not equal err received: %v", err.Error())
|
|
|
+ if err.Error() != test.wantScanErr.Error() {
|
|
|
+ t.Fatalf("wantErr does not equal err received: %v", err.Error())
|
|
|
}
|
|
|
continue
|
|
|
}
|
|
|
+ if err != nil {
|
|
|
+ t.Fatal(err)
|
|
|
+ }
|
|
|
|
|
|
- err = m.Report()
|
|
|
+ err = report.WriteReport(scannerReport, test.opts, cfg)
|
|
|
if err != nil {
|
|
|
t.Error(err)
|
|
|
}
|
|
|
|
|
|
if test.wantEmpty {
|
|
|
- if len(m.GetLeaks()) != 0 {
|
|
|
- t.Errorf("wanted no leaks but got some instead: %+v", m.GetLeaks())
|
|
|
+ if len(scannerReport.Leaks) != 0 {
|
|
|
+ t.Errorf("wanted no leaks but got some instead: %+v", scannerReport.Leaks)
|
|
|
}
|
|
|
continue
|
|
|
}
|
|
|
@@ -473,6 +478,7 @@ func TestScan(t *testing.T) {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+//
|
|
|
func TestScanUncommited(t *testing.T) {
|
|
|
moveDotGit("dotGit", ".git")
|
|
|
defer moveDotGit(".git", "dotGit")
|
|
|
@@ -481,6 +487,7 @@ func TestScanUncommited(t *testing.T) {
|
|
|
opts options.Options
|
|
|
wantPath string
|
|
|
wantErr error
|
|
|
+ wantScanErr error
|
|
|
emptyRepo bool
|
|
|
wantEmpty bool
|
|
|
fileToChange string
|
|
|
@@ -489,9 +496,9 @@ func TestScanUncommited(t *testing.T) {
|
|
|
{
|
|
|
description: "test scan local one leak",
|
|
|
opts: options.Options{
|
|
|
- RepoPath: "../test_data/test_repos/test_repo_1",
|
|
|
+ Path: "../test_data/test_repos/test_repo_1",
|
|
|
Report: "../test_data/test_local_repo_one_aws_leak_uncommitted.json.got",
|
|
|
- Uncommited: true,
|
|
|
+ Unstaged: true,
|
|
|
ReportFormat: "json",
|
|
|
},
|
|
|
wantPath: "../test_data/test_local_repo_one_aws_leak_uncommitted.json",
|
|
|
@@ -501,8 +508,8 @@ func TestScanUncommited(t *testing.T) {
|
|
|
{
|
|
|
description: "test scan local no leak",
|
|
|
opts: options.Options{
|
|
|
- RepoPath: "../test_data/test_repos/test_repo_1",
|
|
|
- Uncommited: true,
|
|
|
+ Path: "../test_data/test_repos/test_repo_1",
|
|
|
+ Unstaged: true,
|
|
|
ReportFormat: "json",
|
|
|
},
|
|
|
wantEmpty: true,
|
|
|
@@ -512,9 +519,9 @@ func TestScanUncommited(t *testing.T) {
|
|
|
{
|
|
|
description: "test scan repo with no commits",
|
|
|
opts: options.Options{
|
|
|
- RepoPath: "../test_data/test_repos/test_repo_7",
|
|
|
+ Path: "../test_data/test_repos/test_repo_7",
|
|
|
Report: "../test_data/test_local_repo_seven_aws_leak_uncommitted.json.got",
|
|
|
- Uncommited: true,
|
|
|
+ Unstaged: true,
|
|
|
ReportFormat: "json",
|
|
|
},
|
|
|
wantPath: "../test_data/test_local_repo_seven_aws_leak_uncommitted.json",
|
|
|
@@ -527,11 +534,11 @@ func TestScanUncommited(t *testing.T) {
|
|
|
)
|
|
|
fmt.Println(test.description)
|
|
|
if test.fileToChange != "" {
|
|
|
- old, err = ioutil.ReadFile(fmt.Sprintf("%s/%s", test.opts.RepoPath, test.fileToChange))
|
|
|
+ old, err = ioutil.ReadFile(fmt.Sprintf("%s/%s", test.opts.Path, test.fileToChange))
|
|
|
if err != nil {
|
|
|
t.Error(err)
|
|
|
}
|
|
|
- altered, err := os.OpenFile(fmt.Sprintf("%s/%s", test.opts.RepoPath, test.fileToChange),
|
|
|
+ altered, err := os.OpenFile(fmt.Sprintf("%s/%s", test.opts.Path, test.fileToChange),
|
|
|
os.O_WRONLY|os.O_APPEND, 0644)
|
|
|
if err != nil {
|
|
|
t.Error(err)
|
|
|
@@ -548,21 +555,42 @@ func TestScanUncommited(t *testing.T) {
|
|
|
if err != nil {
|
|
|
t.Error(err)
|
|
|
}
|
|
|
- m, err := manager.NewManager(test.opts, cfg)
|
|
|
+ scanner, err := NewScanner(test.opts, cfg)
|
|
|
+ if test.wantErr != nil {
|
|
|
+ if err == nil {
|
|
|
+ t.Fatalf("did not receive wantErr: %v", test.wantErr)
|
|
|
+ }
|
|
|
+ if err.Error() != test.wantErr.Error() {
|
|
|
+ t.Fatalf("wantErr does not equal err received: %v", err.Error())
|
|
|
+ }
|
|
|
+ continue
|
|
|
+ }
|
|
|
if err != nil {
|
|
|
- t.Error(err)
|
|
|
+ t.Fatal(err)
|
|
|
}
|
|
|
|
|
|
- if err := Run(m); err != nil {
|
|
|
- t.Error(err)
|
|
|
+ scannerReport, err := scanner.Scan()
|
|
|
+
|
|
|
+ if test.wantScanErr != nil {
|
|
|
+ if err == nil {
|
|
|
+ t.Fatalf("did not receive wantErr: %v", test.wantScanErr)
|
|
|
+ }
|
|
|
+ if err.Error() != test.wantScanErr.Error() {
|
|
|
+ t.Fatalf("wantErr does not equal err received: %v", err.Error())
|
|
|
+ }
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ if err != nil {
|
|
|
+ t.Fatal(err)
|
|
|
}
|
|
|
|
|
|
- if err := m.Report(); err != nil {
|
|
|
+ err = report.WriteReport(scannerReport, test.opts, cfg)
|
|
|
+ if err != nil {
|
|
|
t.Error(err)
|
|
|
}
|
|
|
|
|
|
if test.fileToChange != "" {
|
|
|
- err = ioutil.WriteFile(fmt.Sprintf("%s/%s", test.opts.RepoPath, test.fileToChange), old, 0)
|
|
|
+ err = ioutil.WriteFile(fmt.Sprintf("%s/%s", test.opts.Path, test.fileToChange), old, 0)
|
|
|
if err != nil {
|
|
|
t.Error(err)
|
|
|
}
|
|
|
@@ -583,8 +611,8 @@ func TestScanUncommited(t *testing.T) {
|
|
|
|
|
|
func fileCheck(wantPath, gotPath string) error {
|
|
|
var (
|
|
|
- gotLeaks []manager.Leak
|
|
|
- wantLeaks []manager.Leak
|
|
|
+ gotLeaks []report.Leak
|
|
|
+ wantLeaks []report.Leak
|
|
|
)
|
|
|
want, err := ioutil.ReadFile(wantPath)
|
|
|
if err != nil {
|
|
|
@@ -603,7 +631,7 @@ func fileCheck(wantPath, gotPath string) error {
|
|
|
|
|
|
err = json.Unmarshal(want, &wantLeaks)
|
|
|
if err != nil {
|
|
|
- return nil
|
|
|
+ return err
|
|
|
}
|
|
|
|
|
|
sort.Slice(gotLeaks, func(i, j int) bool { return (gotLeaks)[i].Commit < (gotLeaks)[j].Commit })
|
|
|
@@ -629,6 +657,11 @@ func moveDotGit(from, to string) error {
|
|
|
if !dir.IsDir() {
|
|
|
continue
|
|
|
}
|
|
|
+ _, err := os.Stat(fmt.Sprintf("%s/%s/%s", testRepoBase, dir.Name(), from))
|
|
|
+ if os.IsNotExist(err) {
|
|
|
+ continue
|
|
|
+ }
|
|
|
+
|
|
|
err = os.Rename(fmt.Sprintf("%s/%s/%s", testRepoBase, dir.Name(), from),
|
|
|
fmt.Sprintf("%s/%s/%s", testRepoBase, dir.Name(), to))
|
|
|
if err != nil {
|