|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.IO; |
| 4 | +using System.IO.Abstractions.TestingHelpers; |
| 5 | +using System.Linq; |
| 6 | +using System.Threading.Tasks; |
| 7 | +using CycloneDX.Models; |
| 8 | +using Xunit; |
| 9 | + |
| 10 | +namespace CycloneDX.Tests.FunctionalTests |
| 11 | +{ |
| 12 | + public class MetaData |
| 13 | + { |
| 14 | + private MockFileSystem getMockFS() |
| 15 | + { |
| 16 | + return new MockFileSystem(new Dictionary<string, MockFileData> |
| 17 | + { |
| 18 | + { MockUnixSupport.Path("c:/ProjectPath/obj/project.assets.json"), |
| 19 | + new MockFileData( |
| 20 | + File.ReadAllText(Path.Combine("FunctionalTests", "TestcaseFiles", "SimpleNET6.0Library.json"))) }, |
| 21 | + { MockUnixSupport.Path("c:/ProjectPath/Project.csproj"), new MockFileData(FunctionalTestHelper.CsprojContents) }, |
| 22 | + { MockUnixSupport.Path("c:/ProjectPath/metadata.xml"), |
| 23 | + new MockFileData( |
| 24 | + File.ReadAllText(Path.Combine("FunctionalTests", "TestcaseFiles", "metadata.xml"))) } |
| 25 | + }); |
| 26 | + } |
| 27 | + |
| 28 | + [Fact] |
| 29 | + public async Task ImportedMetaDataAreInBomOutput() |
| 30 | + { |
| 31 | + var options = new RunOptions |
| 32 | + { |
| 33 | + importMetadataPath = MockUnixSupport.Path("c:/ProjectPath/metadata.xml") |
| 34 | + }; |
| 35 | + |
| 36 | + var bom = await FunctionalTestHelper.Test(options, getMockFS()); |
| 37 | + |
| 38 | + Assert.Equal("CycloneDX", bom.Metadata.Component.Name); |
| 39 | + Assert.Equal("1.3.0", bom.Metadata.Component.Version); |
| 40 | + Assert.Equal(Component.Classification.Application, bom.Metadata.Component.Type); |
| 41 | + Assert.False(string.IsNullOrEmpty(bom.Metadata.Component.Description)); |
| 42 | + Assert.Equal("Apache License 2.0", bom.Metadata.Component.Licenses.First().License.Name); |
| 43 | + Assert.Equal("Apache-2.0", bom.Metadata.Component.Licenses.First().License.Id); |
| 44 | + Assert.Equal("pkg:nuget/CycloneDX@1.3.0", bom.Metadata.Component.Purl); |
| 45 | + } |
| 46 | + |
| 47 | + [Fact] |
| 48 | + public async Task IfNoMetadataIsImportedTimestampIsSetAutomatically() |
| 49 | + { |
| 50 | + var options = new RunOptions |
| 51 | + { |
| 52 | + }; |
| 53 | + |
| 54 | + var bom = await FunctionalTestHelper.Test(options, getMockFS()); |
| 55 | + Assert.True(bom.Metadata.Timestamp.Value > DateTime.UtcNow.AddMinutes(-10)); |
| 56 | + } |
| 57 | + |
| 58 | + [Fact] |
| 59 | + public async Task IfMetadataWithoutTimestampIsImportedTimestampStillGetsSet() |
| 60 | + { |
| 61 | + var options = new RunOptions |
| 62 | + { |
| 63 | + importMetadataPath = MockUnixSupport.Path("c:/ProjectPath/metadata.xml") |
| 64 | + }; |
| 65 | + |
| 66 | + var bom = await FunctionalTestHelper.Test(options, getMockFS()); |
| 67 | + Assert.True(bom.Metadata.Timestamp.Value > DateTime.UtcNow.AddMinutes(-10)); |
| 68 | + } |
| 69 | + |
| 70 | + [Fact] |
| 71 | + public async Task SetVersionOverwritesVersionWhenMetadataAreProvided() |
| 72 | + { |
| 73 | + var options = new RunOptions |
| 74 | + { |
| 75 | + importMetadataPath = MockUnixSupport.Path("c:/ProjectPath/metadata.xml"), |
| 76 | + setVersion = "3.0.4" |
| 77 | + |
| 78 | + }; |
| 79 | + |
| 80 | + var bom = await FunctionalTestHelper.Test(options, getMockFS()); |
| 81 | + |
| 82 | + Assert.Equal("CycloneDX", bom.Metadata.Component.Name); |
| 83 | + Assert.Equal("3.0.4", bom.Metadata.Component.Version); |
| 84 | + Assert.Equal(Component.Classification.Application, bom.Metadata.Component.Type); |
| 85 | + Assert.False(string.IsNullOrEmpty(bom.Metadata.Component.Description)); |
| 86 | + Assert.Equal("Apache License 2.0", bom.Metadata.Component.Licenses.First().License.Name); |
| 87 | + Assert.Equal("Apache-2.0", bom.Metadata.Component.Licenses.First().License.Id); |
| 88 | + Assert.Equal("pkg:nuget/CycloneDX@1.3.0", bom.Metadata.Component.Purl); |
| 89 | + } |
| 90 | + |
| 91 | + [Fact] |
| 92 | + public async Task SetNameOverwritesNameWhenMetadataAreProvided() |
| 93 | + { |
| 94 | + var options = new RunOptions |
| 95 | + { |
| 96 | + importMetadataPath = MockUnixSupport.Path("c:/ProjectPath/metadata.xml"), |
| 97 | + setName = "Foo" |
| 98 | + |
| 99 | + }; |
| 100 | + |
| 101 | + var bom = await FunctionalTestHelper.Test(options, getMockFS()); |
| 102 | + |
| 103 | + Assert.Equal("Foo", bom.Metadata.Component.Name); |
| 104 | + Assert.Equal("1.3.0", bom.Metadata.Component.Version); |
| 105 | + Assert.Equal(Component.Classification.Application, bom.Metadata.Component.Type); |
| 106 | + Assert.False(string.IsNullOrEmpty(bom.Metadata.Component.Description)); |
| 107 | + Assert.Equal("Apache License 2.0", bom.Metadata.Component.Licenses.First().License.Name); |
| 108 | + Assert.Equal("Apache-2.0", bom.Metadata.Component.Licenses.First().License.Id); |
| 109 | + Assert.Equal("pkg:nuget/CycloneDX@1.3.0", bom.Metadata.Component.Purl); |
| 110 | + } |
| 111 | + |
| 112 | + [Fact] |
| 113 | + public async Task SetTypeOverwritesTypeWhenMetadataAreProvided() |
| 114 | + { |
| 115 | + var options = new RunOptions |
| 116 | + { |
| 117 | + importMetadataPath = MockUnixSupport.Path("c:/ProjectPath/metadata.xml"), |
| 118 | + setType = Component.Classification.Container |
| 119 | + |
| 120 | + }; |
| 121 | + |
| 122 | + var bom = await FunctionalTestHelper.Test(options, getMockFS()); |
| 123 | + |
| 124 | + Assert.Equal("CycloneDX", bom.Metadata.Component.Name); |
| 125 | + Assert.Equal("1.3.0", bom.Metadata.Component.Version); |
| 126 | + Assert.Equal(Component.Classification.Container, bom.Metadata.Component.Type); |
| 127 | + Assert.False(string.IsNullOrEmpty(bom.Metadata.Component.Description)); |
| 128 | + Assert.Equal("Apache License 2.0", bom.Metadata.Component.Licenses.First().License.Name); |
| 129 | + Assert.Equal("Apache-2.0", bom.Metadata.Component.Licenses.First().License.Id); |
| 130 | + Assert.Equal("pkg:nuget/CycloneDX@1.3.0", bom.Metadata.Component.Purl); |
| 131 | + } |
| 132 | + } |
| 133 | +} |
0 commit comments