-
Notifications
You must be signed in to change notification settings - Fork 131
Expand file tree
/
Copy pathVcpkgComponentDetectorTests.cs
More file actions
237 lines (206 loc) · 10.2 KB
/
Copy pathVcpkgComponentDetectorTests.cs
File metadata and controls
237 lines (206 loc) · 10.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
namespace Microsoft.ComponentDetection.Detectors.Tests;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using FluentAssertions;
using Microsoft.ComponentDetection.Common.DependencyGraph;
using Microsoft.ComponentDetection.Contracts;
using Microsoft.ComponentDetection.Contracts.TypedComponent;
using Microsoft.ComponentDetection.Detectors.Vcpkg;
using Microsoft.ComponentDetection.TestsUtilities;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
[TestClass]
[TestCategory("Governance/All")]
[TestCategory("Governance/ComponentDetection")]
public class VcpkgComponentDetectorTests : BaseDetectorTest<VcpkgComponentDetector>
{
private readonly Mock<ICommandLineInvocationService> mockCommandLineInvocationService;
private readonly Mock<IEnvironmentVariableService> mockEnvironmentVariableService;
public VcpkgComponentDetectorTests()
{
this.mockCommandLineInvocationService = new Mock<ICommandLineInvocationService>();
this.DetectorTestUtility.AddServiceMock(this.mockCommandLineInvocationService);
this.mockEnvironmentVariableService = new Mock<IEnvironmentVariableService>();
this.DetectorTestUtility.AddServiceMock(this.mockEnvironmentVariableService);
var componentRecorder = new ComponentRecorder(enableManualTrackingOfExplicitReferences: false);
this.DetectorTestUtility.WithScanRequest(
new ScanRequest(
new DirectoryInfo(Path.GetTempPath()),
null,
null,
new Dictionary<string, string>(),
null,
componentRecorder));
}
[TestMethod]
public async Task TestNlohmannAsync()
{
var spdxFile = @"{
""SPDXID"": ""SPDXRef - DOCUMENT"",
""documentNamespace"":
""https://spdx.org/spdxdocs/nlohmann-json-x64-linux-3.10.4-78c7f190-b402-44d1-a364-b9ac86392b84"",
""name"": ""nlohmann-json:x64-linux@3.10.4 69dcfc6886529ad2d210f71f132d743672a7e65d2c39f53456f17fc5fc08b278"",
""packages"": [
{
""name"": ""nlohmann-json"",
""SPDXID"": ""SPDXRef-port"",
""versionInfo"": ""3.10.4#5"",
""downloadLocation"": ""git+https://github.com/Microsoft/vcpkg#ports/nlohmann-json"",
""homepage"": ""https://github.com/nlohmann/json"",
""licenseConcluded"": ""NOASSERTION"",
""licenseDeclared"": ""NOASSERTION"",
""copyrightText"": ""NOASSERTION"",
""description"": ""JSON for Modern C++"",
""comment"": ""This is the port (recipe) consumed by vcpkg.""
}
]
}";
var (scanResult, componentRecorder) = await this.DetectorTestUtility
.WithFile("vcpkg.spdx.json", spdxFile)
.ExecuteDetectorAsync();
scanResult.ResultCode.Should().Be(ProcessingResultCode.Success);
var detectedComponents = componentRecorder.GetDetectedComponents();
var components = detectedComponents.ToList();
var sbomComponent = (VcpkgComponent)components.FirstOrDefault()?.Component;
sbomComponent.Should().NotBeNull();
components.Should().ContainSingle();
sbomComponent.Id.Should().Be("git+https://github.com/Microsoft/vcpkg#ports/nlohmann-json : nlohmann-json 3.10.4#5 - Vcpkg");
sbomComponent.Name.Should().Be("nlohmann-json");
sbomComponent.Version.Should().Be("3.10.4");
sbomComponent.PortVersion.Should().Be(5);
sbomComponent.SPDXID.Should().Be("SPDXRef-port");
sbomComponent.DownloadLocation.Should().Be("git+https://github.com/Microsoft/vcpkg#ports/nlohmann-json");
sbomComponent.PackageUrl.ToString().Should().Be("pkg:vcpkg/nlohmann-json@3.10.4?port_version=5");
}
[TestMethod]
public async Task TestTinyxmlAndResourceAsync()
{
var spdxFile = @"{
""SPDXID"": ""SPDXRef - DOCUMENT"",
""documentNamespace"":
""https://spdx.org/spdxdocs/tinyxml2-x64-linux-9.0.0-c99e4f03-5275-458b-8a69-b5f8dfa45f18"",
""name"": ""tinyxml2:x64-linux@9.0.0 5c7679507def92c5c71df44aec08a90a5c749f7f805b3f0e8e70f5e8a5b1b8d0"",
""packages"": [
{
""name"": ""tinyxml2:x64-linux"",
""SPDXID"": ""SPDXRef-binary"",
""versionInfo"": ""5c7679507def92c5c71df44aec08a90a5c749f7f805b3f0e8e70f5e8a5b1b8d0"",
""downloadLocation"": ""NONE"",
""licenseConcluded"": ""NOASSERTION"",
""licenseDeclared"": ""NOASSERTION"",
""copyrightText"": ""NOASSERTION"",
""comment"": ""This is a binary package built by vcpkg.""
},
{
""SPDXID"": ""SPDXRef-resource-1"",
""name"": ""leethomason/tinyxml2"",
""downloadLocation"": ""git+https://github.com/leethomason/tinyxml2@9.0.0"",
""licenseConcluded"": ""NOASSERTION"",
""licenseDeclared"": ""NOASSERTION"",
""copyrightText"": ""NOASSERTION"",
""checksums"": [
{
""algorithm"": ""SHA512"",
""checksumValue"": ""9c5ce8131984690df302ca3e32314573b137180ed522c92fd631692979c942372a28f697fdb3d5e56bcf2d3dc596262b724d088153f3e1d721c9536f2a883367""
}
]
}
]
}";
var (scanResult, componentRecorder) = await this.DetectorTestUtility
.WithFile("vcpkg.spdx.json", spdxFile)
.ExecuteDetectorAsync();
scanResult.ResultCode.Should().Be(ProcessingResultCode.Success);
var detectedComponents = componentRecorder.GetDetectedComponents();
var components = detectedComponents.ToList();
components.Should().HaveCount(2);
var sbomComponent = (VcpkgComponent)components.FirstOrDefault(c => ((VcpkgComponent)c?.Component).SPDXID.Equals("SPDXRef-binary")).Component;
sbomComponent.Should().NotBeNull();
sbomComponent.Id.Should().Be("tinyxml2:x64-linux 5c7679507def92c5c71df44aec08a90a5c749f7f805b3f0e8e70f5e8a5b1b8d0 - Vcpkg");
sbomComponent.Name.Should().Be("tinyxml2:x64-linux");
sbomComponent.Version.Should().Be("5c7679507def92c5c71df44aec08a90a5c749f7f805b3f0e8e70f5e8a5b1b8d0");
sbomComponent.SPDXID.Should().Be("SPDXRef-binary");
sbomComponent.DownloadLocation.Should().Be("NONE");
sbomComponent = (VcpkgComponent)components.FirstOrDefault(c => ((VcpkgComponent)c.Component).SPDXID.Equals("SPDXRef-resource-1")).Component;
sbomComponent.Id.Should().Be("git+https://github.com/leethomason/tinyxml2 : leethomason/tinyxml2 9.0.0 - Vcpkg");
sbomComponent.Name.Should().Be("leethomason/tinyxml2");
sbomComponent.Version.Should().Be("9.0.0");
sbomComponent.SPDXID.Should().Be("SPDXRef-resource-1");
sbomComponent.DownloadLocation.Should().Be("git+https://github.com/leethomason/tinyxml2");
}
[TestMethod]
public async Task TestBlankJsonAsync()
{
var spdxFile = "{}";
var (scanResult, componentRecorder) = await this.DetectorTestUtility
.WithFile("vcpkg.spdx.json", spdxFile)
.ExecuteDetectorAsync();
scanResult.ResultCode.Should().Be(ProcessingResultCode.Success);
var detectedComponents = componentRecorder.GetDetectedComponents();
var components = detectedComponents.ToList();
components.Should().BeEmpty();
}
[TestMethod]
public async Task TestInvalidFileAsync()
{
var spdxFile = "invalidspdxfile";
var (scanResult, componentRecorder) = await this.DetectorTestUtility
.WithFile("vcpkg.spdx.json", spdxFile)
.ExecuteDetectorAsync();
scanResult.ResultCode.Should().Be(ProcessingResultCode.Success);
var detectedComponents = componentRecorder.GetDetectedComponents();
var components = detectedComponents.ToList();
components.Should().BeEmpty();
}
[TestMethod]
[DataTestMethod]
[DataRow("vcpkg_installed\\manifest-info.json", "vcpkg.json")]
[DataRow("vcpkg_installed\\vcpkg\\manifest-info.json", "vcpkg.json")]
[DataRow("bad_location\\manifest-info.json", "vcpkg_installed\\packageLocation\\vcpkg.spdx.json")]
public async Task TestVcpkgManifestFileAsync(string manifestPath, string pathToVcpkg)
{
var t_pathToVcpkg = CrossPlatformPath(Path.GetFullPath(pathToVcpkg));
var t_manifestPath = CrossPlatformPath(Path.GetFullPath(manifestPath));
var spdxFile = @"{
""SPDXID"": ""SPDXRef - DOCUMENT"",
""documentNamespace"":
""https://spdx.org/spdxdocs/nlohmann-json-x64-linux-3.10.4-78c7f190-b402-44d1-a364-b9ac86392b84"",
""name"": ""nlohmann-json:x64-linux@3.10.4 69dcfc6886529ad2d210f71f132d743672a7e65d2c39f53456f17fc5fc08b278"",
""packages"": [
{
""name"": ""nlohmann-json"",
""SPDXID"": ""SPDXRef-port"",
""versionInfo"": ""3.10.4#5"",
""downloadLocation"": ""git+https://github.com/Microsoft/vcpkg#ports/nlohmann-json"",
""homepage"": ""https://github.com/nlohmann/json"",
""licenseConcluded"": ""NOASSERTION"",
""licenseDeclared"": ""NOASSERTION"",
""copyrightText"": ""NOASSERTION"",
""description"": ""JSON for Modern C++"",
""comment"": ""This is the port (recipe) consumed by vcpkg.""
}
]
}";
var manifestFile = $@"{{
""manifest-path"": ""{t_pathToVcpkg.Replace("\\", "\\\\")}""
}}";
var (scanResult, componentRecorder) = await this.DetectorTestUtility
.WithFile(CrossPlatformPath(Path.GetFullPath("vcpkg_installed\\packageLocation\\vcpkg.spdx.json")), spdxFile)
.WithFile(t_manifestPath, manifestFile)
.ExecuteDetectorAsync();
scanResult.ResultCode.Should().Be(ProcessingResultCode.Success);
var detectedComponents = componentRecorder.GetDependencyGraphsByLocation();
var singleFileComponent = detectedComponents.FirstOrDefault();
singleFileComponent.Should().NotBeNull();
var expectedResult = singleFileComponent.Key.Replace("/tmp/", string.Empty);
expectedResult.Should().Be(t_pathToVcpkg);
}
private static string CrossPlatformPath(string relPath)
{
var segments = relPath.Split(['\\', '/'], StringSplitOptions.RemoveEmptyEntries);
return Path.Combine(segments);
}
}