|
1 | 1 | package audit |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "errors" |
| 5 | + "fmt" |
| 6 | + "net/http" |
4 | 7 | "path/filepath" |
5 | 8 | "strings" |
6 | 9 | "testing" |
@@ -197,3 +200,144 @@ func searchForStrWithSubString(t *testing.T, filesList []string, subString strin |
197 | 200 | } |
198 | 201 | assert.Fail(t, "File %s not found in the list", subString) |
199 | 202 | } |
| 203 | + |
| 204 | +func TestAuditWithPartialResults(t *testing.T) { |
| 205 | + testcases := []struct { |
| 206 | + name string |
| 207 | + allowPartialResults bool |
| 208 | + useJas bool |
| 209 | + testDirPath string |
| 210 | + }{ |
| 211 | + { |
| 212 | + name: "Failure in SCA during dependency tree construction", |
| 213 | + allowPartialResults: false, |
| 214 | + useJas: false, |
| 215 | + testDirPath: filepath.Join("..", "..", "tests", "testdata", "projects", "package-managers", "npm", "npm-un-installable"), |
| 216 | + }, |
| 217 | + { |
| 218 | + name: "Failure in SCA during scan itself", |
| 219 | + allowPartialResults: false, |
| 220 | + useJas: false, |
| 221 | + testDirPath: filepath.Join("..", "..", "tests", "testdata", "projects", "package-managers", "npm", "npm-project"), |
| 222 | + }, |
| 223 | + { |
| 224 | + name: "Skip failure in SCA during dependency tree construction", |
| 225 | + allowPartialResults: true, |
| 226 | + useJas: false, |
| 227 | + testDirPath: filepath.Join("..", "..", "tests", "testdata", "projects", "package-managers", "npm", "npm-un-installable"), |
| 228 | + }, |
| 229 | + { |
| 230 | + name: "Skip failure in SCA during scan itself", |
| 231 | + allowPartialResults: true, |
| 232 | + useJas: false, |
| 233 | + testDirPath: filepath.Join("..", "..", "tests", "testdata", "projects", "package-managers", "npm", "npm-project"), |
| 234 | + }, |
| 235 | + // TODO when applying allow-partial-results to JAS make sure to add a test case that checks failures in JAS scans + add some JAS api call to the mock server |
| 236 | + } |
| 237 | + |
| 238 | + serverMock, serverDetails := utils.CreateXrayRestsMockServer(func(w http.ResponseWriter, r *http.Request) { |
| 239 | + if r.RequestURI == "/xray/api/v1/system/version" { |
| 240 | + _, err := w.Write([]byte(fmt.Sprintf(`{"xray_version": "%s", "xray_revision": "xxx"}`, scangraph.GraphScanMinXrayVersion))) |
| 241 | + if !assert.NoError(t, err) { |
| 242 | + return |
| 243 | + } |
| 244 | + } |
| 245 | + if strings.HasPrefix(r.RequestURI, "/xray/api/v1/scan/graph") && r.Method == http.MethodPost { |
| 246 | + // We set SCA scan graph API to fail |
| 247 | + w.WriteHeader(http.StatusBadRequest) |
| 248 | + } |
| 249 | + }) |
| 250 | + defer serverMock.Close() |
| 251 | + |
| 252 | + for _, testcase := range testcases { |
| 253 | + t.Run(testcase.name, func(t *testing.T) { |
| 254 | + tempDirPath, createTempDirCallback := coreTests.CreateTempDirWithCallbackAndAssert(t) |
| 255 | + defer createTempDirCallback() |
| 256 | + |
| 257 | + assert.NoError(t, biutils.CopyDir(testcase.testDirPath, tempDirPath, false, nil)) |
| 258 | + |
| 259 | + auditBasicParams := (&utils.AuditBasicParams{}). |
| 260 | + SetServerDetails(serverDetails). |
| 261 | + SetOutputFormat(format.Table). |
| 262 | + SetUseJas(testcase.useJas). |
| 263 | + SetAllowPartialResults(testcase.allowPartialResults) |
| 264 | + |
| 265 | + auditParams := NewAuditParams(). |
| 266 | + SetWorkingDirs([]string{tempDirPath}). |
| 267 | + SetGraphBasicParams(auditBasicParams). |
| 268 | + SetCommonGraphScanParams(&scangraph.CommonGraphScanParams{ |
| 269 | + ScanType: scanservices.Dependency, |
| 270 | + IncludeVulnerabilities: true, |
| 271 | + MultiScanId: utils.TestScaScanId, |
| 272 | + }) |
| 273 | + auditParams.SetIsRecursiveScan(true) |
| 274 | + |
| 275 | + scanResults, err := RunAudit(auditParams) |
| 276 | + if testcase.allowPartialResults { |
| 277 | + assert.NoError(t, scanResults.ScansErr) |
| 278 | + assert.NoError(t, err) |
| 279 | + } else { |
| 280 | + assert.Error(t, scanResults.ScansErr) |
| 281 | + assert.NoError(t, err) |
| 282 | + } |
| 283 | + }) |
| 284 | + } |
| 285 | +} |
| 286 | + |
| 287 | +func TestCreateErrorIfPartialResultsDisabled(t *testing.T) { |
| 288 | + testcases := []struct { |
| 289 | + name string |
| 290 | + allowPartialResults bool |
| 291 | + auditParallelRunner bool |
| 292 | + }{ |
| 293 | + { |
| 294 | + name: "Allow partial results - no error expected", |
| 295 | + allowPartialResults: true, |
| 296 | + auditParallelRunner: true, |
| 297 | + }, |
| 298 | + { |
| 299 | + name: "Partial results disabled with SecurityParallelRunner", |
| 300 | + allowPartialResults: false, |
| 301 | + auditParallelRunner: true, |
| 302 | + }, |
| 303 | + { |
| 304 | + name: "Partial results disabled without SecurityParallelRunner", |
| 305 | + allowPartialResults: false, |
| 306 | + auditParallelRunner: false, |
| 307 | + }, |
| 308 | + } |
| 309 | + |
| 310 | + for _, testcase := range testcases { |
| 311 | + t.Run(testcase.name, func(t *testing.T) { |
| 312 | + auditBasicParams := (&utils.AuditBasicParams{}).SetAllowPartialResults(testcase.allowPartialResults) |
| 313 | + auditParams := NewAuditParams().SetGraphBasicParams(auditBasicParams) |
| 314 | + |
| 315 | + var auditParallelRunner *utils.SecurityParallelRunner |
| 316 | + if testcase.auditParallelRunner { |
| 317 | + auditParallelRunner = utils.CreateSecurityParallelRunner(1) |
| 318 | + } |
| 319 | + |
| 320 | + err := createErrorIfPartialResultsDisabled(auditParams, auditParallelRunner, "", errors.New("error")) |
| 321 | + if testcase.allowPartialResults { |
| 322 | + assert.NoError(t, err) |
| 323 | + } else { |
| 324 | + if testcase.auditParallelRunner { |
| 325 | + assert.False(t, isErrorsQueueEmpty(auditParallelRunner)) |
| 326 | + } else { |
| 327 | + assert.Error(t, err) |
| 328 | + } |
| 329 | + } |
| 330 | + }) |
| 331 | + } |
| 332 | +} |
| 333 | + |
| 334 | +func isErrorsQueueEmpty(spr *utils.SecurityParallelRunner) bool { |
| 335 | + select { |
| 336 | + case <-spr.ErrorsQueue: |
| 337 | + // Channel is not empty |
| 338 | + return false |
| 339 | + default: |
| 340 | + // Channel is empty |
| 341 | + return true |
| 342 | + } |
| 343 | +} |
0 commit comments