|
2 | 2 |
|
3 | 3 | import com.checkmarx.ast.realtime.RealtimeLocation; |
4 | 4 | import com.checkmarx.ast.secretsrealtime.SecretsRealtimeResults; |
| 5 | +import com.checkmarx.ast.secretsrealtime.MaskResult; |
| 6 | +import com.checkmarx.ast.secretsrealtime.MaskedSecret; |
5 | 7 | import com.checkmarx.ast.wrapper.CxException; |
6 | 8 | import org.junit.jupiter.api.*; |
7 | 9 |
|
@@ -204,6 +206,188 @@ void secretsScanMultipleFileTypes() { |
204 | 206 | } |
205 | 207 | } |
206 | 208 |
|
| 209 | + /* ------------------------------------------------------ */ |
| 210 | + /* Integration tests for Secrets Masking functionality */ |
| 211 | + /* ------------------------------------------------------ */ |
| 212 | + |
| 213 | + /** |
| 214 | + * Tests basic mask secrets functionality - successful case. |
| 215 | + * Similar to the JavaScript test, verifies that the mask command returns proper MaskResult |
| 216 | + * with masked secrets detected in a JSON file containing API keys and passwords. |
| 217 | + */ |
| 218 | + @Test |
| 219 | + @DisplayName("Mask secrets successful case - returns masked content") |
| 220 | + void maskSecretsSuccessfulCase() throws Exception { |
| 221 | + Assumptions.assumeTrue(isCliConfigured(), "PATH_TO_EXECUTABLE not configured - skipping integration test"); |
| 222 | + String secretsFile = "src/test/resources/secrets-test.json"; |
| 223 | + Assumptions.assumeTrue(Files.exists(Paths.get(secretsFile)), "Secrets test file not found - cannot test masking"); |
| 224 | + |
| 225 | + MaskResult result = wrapper.maskSecrets(secretsFile); |
| 226 | + |
| 227 | + assertNotNull(result, "Mask result should not be null"); |
| 228 | + assertNotNull(result.getMaskedSecrets(), "Masked secrets list should be initialized"); |
| 229 | + assertNotNull(result.getMaskedFile(), "Masked file content should be provided"); |
| 230 | + |
| 231 | + // Expect at least one secret to be found in our test file |
| 232 | + assertFalse(result.getMaskedSecrets().isEmpty(), "Should find masked secrets in test file"); |
| 233 | + |
| 234 | + // Verify structure of masked secrets |
| 235 | + MaskedSecret firstSecret = result.getMaskedSecrets().get(0); |
| 236 | + assertNotNull(firstSecret.getMasked(), "Masked value should be provided"); |
| 237 | + assertTrue(firstSecret.getLine() > 0, "Line number should be positive"); |
| 238 | + |
| 239 | + // Masked file should contain the original structure but with secrets redacted |
| 240 | + assertFalse(result.getMaskedFile().trim().isEmpty(), "Masked file content should not be empty"); |
| 241 | + assertTrue(result.getMaskedFile().contains("{"), "Masked file should preserve JSON structure"); |
| 242 | + } |
| 243 | + |
| 244 | + /** |
| 245 | + * Tests mask functionality across different file types. |
| 246 | + * Verifies that the mask command can handle various file extensions and formats |
| 247 | + * without crashing and produces appropriate masked results. |
| 248 | + */ |
| 249 | + @Test |
| 250 | + @DisplayName("Mask secrets handles multiple file types correctly") |
| 251 | + void maskSecretsMultipleFileTypes() { |
| 252 | + Assumptions.assumeTrue(isCliConfigured(), "PATH_TO_EXECUTABLE not configured - skipping integration test"); |
| 253 | + |
| 254 | + String[] testFiles = { |
| 255 | + "src/test/resources/python-vul-file.py", |
| 256 | + "src/test/resources/csharp-file.cs" |
| 257 | + }; |
| 258 | + |
| 259 | + for (String filePath : testFiles) { |
| 260 | + if (Files.exists(Paths.get(filePath))) { |
| 261 | + assertDoesNotThrow(() -> { |
| 262 | + MaskResult result = wrapper.maskSecrets(filePath); |
| 263 | + assertNotNull(result, "Mask result should not be null for file: " + filePath); |
| 264 | + assertNotNull(result.getMaskedSecrets(), "Masked secrets should be initialized for: " + filePath); |
| 265 | + assertNotNull(result.getMaskedFile(), "Masked file should not be null for: " + filePath); |
| 266 | + }, "Mask command should handle file type gracefully: " + filePath); |
| 267 | + } |
| 268 | + } |
| 269 | + } |
| 270 | + |
| 271 | + /** |
| 272 | + * Tests error handling when masking a non-existent file. |
| 273 | + * Verifies that the mask command properly throws a CxException with meaningful error message |
| 274 | + * when provided with invalid file paths. |
| 275 | + */ |
| 276 | + @Test |
| 277 | + @DisplayName("Mask secrets throws appropriate exception for non-existent file") |
| 278 | + void maskSecretsHandlesInvalidPath() { |
| 279 | + Assumptions.assumeTrue(isCliConfigured(), "PATH_TO_EXECUTABLE not configured - skipping integration test"); |
| 280 | + |
| 281 | + // Test with a non-existent file path |
| 282 | + String invalidPath = "src/test/resources/NonExistentFile.py"; |
| 283 | + |
| 284 | + // The CLI should throw a CxException with a meaningful error message for invalid paths |
| 285 | + CxException exception = assertThrows(CxException.class, () -> |
| 286 | + wrapper.maskSecrets(invalidPath) |
| 287 | + ); |
| 288 | + |
| 289 | + // Verify the exception contains information about the invalid file path |
| 290 | + String errorMessage = exception.getMessage(); |
| 291 | + assertNotNull(errorMessage, "Exception should contain an error message"); |
| 292 | + assertTrue(errorMessage.contains("invalid file path") || errorMessage.contains("file") || errorMessage.contains("path"), |
| 293 | + "Exception message should indicate the issue is related to file path: " + errorMessage); |
| 294 | + } |
| 295 | + |
| 296 | + /** |
| 297 | + * Tests that masked file content differs from original when secrets are present. |
| 298 | + * Verifies that the masking process actually modifies the file content to redact secrets. |
| 299 | + */ |
| 300 | + @Test |
| 301 | + @DisplayName("Masked file content differs from original when secrets exist") |
| 302 | + void maskedContentDiffersFromOriginal() throws Exception { |
| 303 | + Assumptions.assumeTrue(isCliConfigured(), "PATH_TO_EXECUTABLE not configured - skipping integration test"); |
| 304 | + String secretsFile = "src/test/resources/secrets-test.json"; |
| 305 | + Assumptions.assumeTrue(Files.exists(Paths.get(secretsFile)), "Secrets test file not found - cannot test content masking"); |
| 306 | + |
| 307 | + // Read original file content |
| 308 | + String originalContent = Files.readString(Paths.get(secretsFile)); |
| 309 | + |
| 310 | + // Get masked content |
| 311 | + MaskResult result = wrapper.maskSecrets(secretsFile); |
| 312 | + assertNotNull(result, "Mask result should not be null"); |
| 313 | + |
| 314 | + String maskedContent = result.getMaskedFile(); |
| 315 | + assertNotNull(maskedContent, "Masked content should not be null"); |
| 316 | + |
| 317 | + // Since our test file contains secrets, the content should be different after masking |
| 318 | + if (!result.getMaskedSecrets().isEmpty()) { |
| 319 | + assertNotEquals(originalContent, maskedContent, |
| 320 | + "Masked content should differ from original when secrets are present"); |
| 321 | + |
| 322 | + // Verify that original secrets are not present in masked content |
| 323 | + assertFalse(maskedContent.contains("sk-1234567890abcdef1234567890abcdef"), |
| 324 | + "Original API key should be masked in output"); |
| 325 | + assertFalse(maskedContent.contains("SuperSecret123!"), |
| 326 | + "Original password should be masked in output"); |
| 327 | + } |
| 328 | + } |
| 329 | + |
| 330 | + /* ------------------------------------------------------ */ |
| 331 | + /* Unit tests for Mask JSON parsing functionality */ |
| 332 | + /* ------------------------------------------------------ */ |
| 333 | + |
| 334 | + /** |
| 335 | + * Tests MaskResult JSON parsing with valid mask command response. |
| 336 | + * Verifies that well-formed mask JSON is correctly parsed into MaskResult objects. |
| 337 | + */ |
| 338 | + @Test |
| 339 | + @DisplayName("Valid mask JSON response parsing creates correct MaskResult") |
| 340 | + void testMaskResultJsonParsing() { |
| 341 | + String json = "{" + |
| 342 | + "\"maskedSecrets\":[" + |
| 343 | + "{\"masked\":\"****\",\"secret\":\"password123\",\"line\":5}," + |
| 344 | + "{\"masked\":\"***\",\"secret\":\"key\",\"line\":10}" + |
| 345 | + "]," + |
| 346 | + "\"maskedFile\":\"const password = '****';\\nconst apiKey = '***';\"" + |
| 347 | + "}"; |
| 348 | + |
| 349 | + MaskResult result = MaskResult.fromJsonString(json); |
| 350 | + |
| 351 | + assertNotNull(result, "MaskResult should not be null"); |
| 352 | + assertEquals(2, result.getMaskedSecrets().size(), "Should parse 2 masked secrets"); |
| 353 | + |
| 354 | + MaskedSecret firstSecret = result.getMaskedSecrets().get(0); |
| 355 | + assertEquals("****", firstSecret.getMasked()); |
| 356 | + assertEquals("password123", firstSecret.getSecret()); |
| 357 | + assertEquals(5, firstSecret.getLine()); |
| 358 | + |
| 359 | + MaskedSecret secondSecret = result.getMaskedSecrets().get(1); |
| 360 | + assertEquals("***", secondSecret.getMasked()); |
| 361 | + assertEquals("key", secondSecret.getSecret()); |
| 362 | + assertEquals(10, secondSecret.getLine()); |
| 363 | + |
| 364 | + assertTrue(result.getMaskedFile().contains("const password = '****'")); |
| 365 | + assertTrue(result.getMaskedFile().contains("const apiKey = '***'")); |
| 366 | + } |
| 367 | + |
| 368 | + /** |
| 369 | + * Tests MaskResult parsing robustness with edge cases. |
| 370 | + * Verifies that the parser gracefully handles various invalid input scenarios. |
| 371 | + */ |
| 372 | + @Test |
| 373 | + @DisplayName("MaskResult handles malformed JSON and edge cases gracefully") |
| 374 | + void testMaskResultEdgeCases() { |
| 375 | + // Blank/null inputs |
| 376 | + assertNull(MaskResult.fromJsonString("")); |
| 377 | + assertNull(MaskResult.fromJsonString(" ")); |
| 378 | + assertNull(MaskResult.fromJsonString(null)); |
| 379 | + |
| 380 | + // Invalid JSON structures |
| 381 | + assertNull(MaskResult.fromJsonString("{")); |
| 382 | + assertNull(MaskResult.fromJsonString("not a json")); |
| 383 | + |
| 384 | + // Empty but valid JSON |
| 385 | + MaskResult emptyResult = MaskResult.fromJsonString("{}"); |
| 386 | + assertNotNull(emptyResult); |
| 387 | + assertTrue(emptyResult.getMaskedSecrets().isEmpty()); |
| 388 | + assertNotNull(emptyResult.getMaskedFile()); |
| 389 | + } |
| 390 | + |
207 | 391 | /* ------------------------------------------------------ */ |
208 | 392 | /* Unit tests for JSON parsing robustness */ |
209 | 393 | /* ------------------------------------------------------ */ |
|
0 commit comments