|
1 | 1 | package tests |
2 | 2 |
|
3 | | -import "testing" |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "os" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/checkmarx/2ms/lib/reporting" |
| 9 | + "github.com/google/go-cmp/cmp" |
| 10 | +) |
4 | 11 |
|
5 | 12 | func TestIntegration(t *testing.T) { |
6 | 13 | if testing.Short() { |
@@ -79,3 +86,74 @@ func TestIntegration(t *testing.T) { |
79 | 86 | } |
80 | 87 | }) |
81 | 88 | } |
| 89 | + |
| 90 | +func TestSecretsEdgeCases(t *testing.T) { |
| 91 | + if testing.Short() { |
| 92 | + t.Skip("skipping edge cases test") |
| 93 | + } |
| 94 | + |
| 95 | + tests := []struct { |
| 96 | + Name string |
| 97 | + ScanTarget string |
| 98 | + TargetPath string |
| 99 | + ExpectedReportPath string |
| 100 | + }{ |
| 101 | + { |
| 102 | + Name: "secret at end without newline (filesystem)", |
| 103 | + ScanTarget: "filesystem", |
| 104 | + TargetPath: "testData/input/secret_at_end.txt", |
| 105 | + ExpectedReportPath: "testData/expectedReport/report1.json", |
| 106 | + }, |
| 107 | + { |
| 108 | + Name: "secret at end with multiLine (filesystem)", |
| 109 | + ScanTarget: "filesystem", |
| 110 | + TargetPath: "testData/input/multi_line_secret.txt", |
| 111 | + ExpectedReportPath: "testData/expectedReport/report2.json", |
| 112 | + }, |
| 113 | + { |
| 114 | + Name: "secret at end with backspace in newline (filesystem)", |
| 115 | + ScanTarget: "filesystem", |
| 116 | + TargetPath: "testData/input/secret_at_end_with_newline.txt", |
| 117 | + ExpectedReportPath: "testData/expectedReport/report3.json", |
| 118 | + }, |
| 119 | + } |
| 120 | + |
| 121 | + for _, tc := range tests { |
| 122 | + t.Run(tc.Name, func(t *testing.T) { |
| 123 | + executable, err := createCLI(t.TempDir()) |
| 124 | + if err != nil { |
| 125 | + t.Fatalf("failed to build CLI: %s", err) |
| 126 | + } |
| 127 | + |
| 128 | + args := []string{tc.ScanTarget} |
| 129 | + if tc.ScanTarget == "filesystem" { |
| 130 | + args = append(args, "--path", tc.TargetPath) |
| 131 | + } else { |
| 132 | + args = append(args, tc.TargetPath) |
| 133 | + } |
| 134 | + args = append(args, "--ignore-on-exit", "results") |
| 135 | + |
| 136 | + if err := executable.run(args[0], args[1:]...); err != nil { |
| 137 | + t.Fatalf("error running scan with args: %v, got: %v", args, err) |
| 138 | + } |
| 139 | + |
| 140 | + actualReport, err := executable.getReport() |
| 141 | + if err != nil { |
| 142 | + t.Fatalf("failed to get report: %s", err) |
| 143 | + } |
| 144 | + |
| 145 | + expectedBytes, err := os.ReadFile(tc.ExpectedReportPath) |
| 146 | + if err != nil { |
| 147 | + t.Fatalf("failed to read expected report: %s", err) |
| 148 | + } |
| 149 | + var expectedReport reporting.Report |
| 150 | + if err := json.Unmarshal(expectedBytes, &expectedReport); err != nil { |
| 151 | + t.Fatalf("failed to unmarshal expected report: %s", err) |
| 152 | + } |
| 153 | + |
| 154 | + if !cmp.Equal(expectedReport, actualReport) { |
| 155 | + t.Errorf("Scan report does not match expected report:\n%s", cmp.Diff(expectedReport, actualReport)) |
| 156 | + } |
| 157 | + }) |
| 158 | + } |
| 159 | +} |
0 commit comments