|
| 1 | +package com.checkmarx.ast; |
| 2 | + |
| 3 | +import com.checkmarx.ast.mask.MaskResult; |
| 4 | +import com.checkmarx.ast.mask.MaskedSecret; |
| 5 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 6 | +import org.junit.jupiter.api.Assertions; |
| 7 | +import org.junit.jupiter.api.Test; |
| 8 | + |
| 9 | +public class MaskTest extends BaseTest { |
| 10 | + |
| 11 | + private static final String RESULTS_FILE = "target/test-classes/results.json"; |
| 12 | + private static final String SECRETS_REALTIME_FILE = "target/test-classes/Secrets-realtime.json"; |
| 13 | + |
| 14 | + @Test |
| 15 | + void testMaskSecretsWithFileContainingSecrets() throws Exception { |
| 16 | + // Tests CLI execution with file containing actual secrets and validates masking behavior |
| 17 | + MaskResult result = wrapper.maskSecrets(SECRETS_REALTIME_FILE); |
| 18 | + |
| 19 | + Assertions.assertNotNull(result); |
| 20 | + Assertions.assertNotNull(result.getMaskedFile()); |
| 21 | + Assertions.assertNotNull(result.getMaskedSecrets()); |
| 22 | + Assertions.assertFalse(result.getMaskedSecrets().isEmpty()); |
| 23 | + |
| 24 | + MaskedSecret secret = result.getMaskedSecrets().get(0); |
| 25 | + Assertions.assertNotNull(secret.getMasked()); |
| 26 | + Assertions.assertNotNull(secret.getSecret()); |
| 27 | + Assertions.assertEquals(5, secret.getLine()); |
| 28 | + Assertions.assertTrue(secret.getMasked().contains("<masked>") || secret.getMasked().contains("\\u003cmasked\\u003e")); |
| 29 | + Assertions.assertTrue(secret.getSecret().contains("-----BEGIN RSA PRIVATE KEY-----")); |
| 30 | + Assertions.assertTrue(secret.getSecret().length() > secret.getMasked().length()); |
| 31 | + } |
| 32 | + |
| 33 | + @Test |
| 34 | + void testMaskSecretsWithFileContainingNoSecrets() throws Exception { |
| 35 | + // Tests CLI execution with file containing no secrets |
| 36 | + MaskResult result = wrapper.maskSecrets(RESULTS_FILE); |
| 37 | + |
| 38 | + Assertions.assertNotNull(result); |
| 39 | + Assertions.assertNotNull(result.getMaskedFile()); |
| 40 | + Assertions.assertFalse(result.getMaskedFile().isEmpty()); |
| 41 | + } |
| 42 | + |
| 43 | + @Test |
| 44 | + void testMaskSecretsErrorHandling() { |
| 45 | + // Tests CLI error handling for invalid inputs |
| 46 | + Assertions.assertThrows(Exception.class, () -> wrapper.maskSecrets(null)); |
| 47 | + Assertions.assertThrows(Exception.class, () -> wrapper.maskSecrets("non-existent-file.json")); |
| 48 | + Assertions.assertDoesNotThrow(() -> wrapper.maskSecrets(RESULTS_FILE)); |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + void testMaskSecretsResponseParsing() throws Exception { |
| 53 | + // Tests CLI response structure and JSON parsing functionality |
| 54 | + MaskResult result = wrapper.maskSecrets(SECRETS_REALTIME_FILE); |
| 55 | + |
| 56 | + Assertions.assertNotNull(result); |
| 57 | + Assertions.assertNotNull(result.getMaskedSecrets()); |
| 58 | + Assertions.assertFalse(result.getMaskedSecrets().isEmpty()); |
| 59 | + |
| 60 | + MaskedSecret secret = result.getMaskedSecrets().get(0); |
| 61 | + Assertions.assertNotNull(secret.getMasked()); |
| 62 | + Assertions.assertNotNull(secret.getSecret()); |
| 63 | + Assertions.assertTrue(secret.getLine() >= 0); |
| 64 | + |
| 65 | + Assertions.assertNull(MaskResult.fromLine("")); |
| 66 | + Assertions.assertNull(MaskResult.fromLine("{invalid json}")); |
| 67 | + Assertions.assertNull(MaskResult.fromLine(null)); |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + void testMaskSecretsObjectBehavior() throws Exception { |
| 72 | + // Tests object equality, serialization and consistency with CLI responses |
| 73 | + MaskResult result1 = wrapper.maskSecrets(SECRETS_REALTIME_FILE); |
| 74 | + MaskResult result2 = wrapper.maskSecrets(SECRETS_REALTIME_FILE); |
| 75 | + |
| 76 | + Assertions.assertEquals(result1.getMaskedFile(), result2.getMaskedFile()); |
| 77 | + Assertions.assertNotNull(result1.toString()); |
| 78 | + Assertions.assertTrue(result1.toString().contains("MaskResult")); |
| 79 | + |
| 80 | + if (result1.getMaskedSecrets() != null && !result1.getMaskedSecrets().isEmpty()) { |
| 81 | + MaskedSecret secret1 = result1.getMaskedSecrets().get(0); |
| 82 | + MaskedSecret secret2 = result2.getMaskedSecrets().get(0); |
| 83 | + |
| 84 | + Assertions.assertEquals(secret1.getMasked(), secret2.getMasked()); |
| 85 | + Assertions.assertEquals(secret1.getSecret(), secret2.getSecret()); |
| 86 | + Assertions.assertEquals(secret1.getLine(), secret2.getLine()); |
| 87 | + Assertions.assertEquals(secret1.hashCode(), secret2.hashCode()); |
| 88 | + Assertions.assertEquals(secret1, secret1); |
| 89 | + Assertions.assertNotEquals(secret1, null); |
| 90 | + |
| 91 | + String toString = secret1.toString(); |
| 92 | + Assertions.assertNotNull(toString); |
| 93 | + Assertions.assertTrue(toString.contains("MaskedSecret")); |
| 94 | + } |
| 95 | + |
| 96 | + ObjectMapper mapper = new ObjectMapper(); |
| 97 | + String json = mapper.writeValueAsString(result1); |
| 98 | + MaskResult deserialized = mapper.readValue(json, MaskResult.class); |
| 99 | + |
| 100 | + Assertions.assertEquals(result1.getMaskedFile(), deserialized.getMaskedFile()); |
| 101 | + if (result1.getMaskedSecrets() != null) { |
| 102 | + Assertions.assertEquals(result1.getMaskedSecrets().size(), deserialized.getMaskedSecrets().size()); |
| 103 | + } |
| 104 | + } |
| 105 | +} |
0 commit comments