|
16 | 16 | */ |
17 | 17 | package io.github.guacsec.trustifyda.providers; |
18 | 18 |
|
| 19 | +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; |
19 | 20 | import static org.junit.jupiter.api.Assertions.assertEquals; |
20 | 21 | import static org.junit.jupiter.api.Assertions.assertFalse; |
21 | 22 | import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 23 | +import static org.junit.jupiter.api.Assertions.assertNull; |
22 | 24 | import static org.junit.jupiter.api.Assertions.assertThrows; |
23 | 25 | import static org.junit.jupiter.api.Assertions.assertTrue; |
24 | 26 |
|
| 27 | +import com.github.packageurl.PackageURL; |
| 28 | +import io.github.guacsec.trustifyda.sbom.Sbom; |
| 29 | +import io.github.guacsec.trustifyda.sbom.SbomFactory; |
| 30 | +import io.github.guacsec.trustifyda.tools.Ecosystem.Type; |
25 | 31 | import java.io.IOException; |
26 | 32 | import java.nio.charset.StandardCharsets; |
27 | 33 | import java.nio.file.Files; |
28 | 34 | import java.nio.file.Path; |
| 35 | +import java.util.HashMap; |
| 36 | +import java.util.HashSet; |
29 | 37 | import java.util.Set; |
30 | 38 | import org.junit.jupiter.api.Test; |
31 | 39 | import org.junit.jupiter.api.io.TempDir; |
| 40 | +import org.tomlj.Toml; |
| 41 | +import org.tomlj.TomlParseResult; |
32 | 42 |
|
33 | 43 | public class CargoProviderCargoParsingTest { |
34 | 44 |
|
@@ -720,4 +730,40 @@ public void testVirtualRootWithoutVersionUsesDefault(@TempDir Path tempDir) thro |
720 | 730 |
|
721 | 731 | assertTrue(stackSbom.contains("1.0.0"), "SBOM should contain default version: 1.0.0"); |
722 | 732 | } |
| 733 | + |
| 734 | + @Test |
| 735 | + public void testVirtualWorkspaceWithoutWorkspaceDepsDoesNotThrowNPE(@TempDir Path tempDir) |
| 736 | + throws Exception { |
| 737 | + // Create a Cargo.toml with [workspace] members but NO [workspace.dependencies] |
| 738 | + Path cargoToml = tempDir.resolve("Cargo.toml"); |
| 739 | + String content = |
| 740 | + """ |
| 741 | + [workspace] |
| 742 | + members = ["crate-a", "crate-b"] |
| 743 | +
|
| 744 | + [workspace.package] |
| 745 | + version = "1.0.0" |
| 746 | + edition = "2021" |
| 747 | + """; |
| 748 | + Files.writeString(cargoToml, content); |
| 749 | + |
| 750 | + TomlParseResult tomlResult = Toml.parse(cargoToml); |
| 751 | + // Verify precondition: workspace.dependencies table is null |
| 752 | + assertNull( |
| 753 | + tomlResult.getTable("workspace.dependencies"), |
| 754 | + "Precondition: workspace.dependencies table should be null"); |
| 755 | + |
| 756 | + CargoProvider provider = new CargoProvider(cargoToml); |
| 757 | + Sbom sbom = SbomFactory.newInstance(); |
| 758 | + PackageURL root = |
| 759 | + new PackageURL(Type.CARGO.getType(), null, "test-workspace", "1.0.0", null, null); |
| 760 | + sbom.addRoot(root); |
| 761 | + |
| 762 | + // This should NOT throw NPE when [workspace.dependencies] is absent |
| 763 | + assertDoesNotThrow( |
| 764 | + () -> |
| 765 | + provider.processWorkspaceDependencies( |
| 766 | + sbom, root, new HashMap<>(), new HashSet<>(), tomlResult), |
| 767 | + "processWorkspaceDependencies should handle missing [workspace.dependencies] gracefully"); |
| 768 | + } |
723 | 769 | } |
0 commit comments