-
Notifications
You must be signed in to change notification settings - Fork 126
Expand file tree
/
Copy pathPurlGenerationTests.cs
More file actions
138 lines (116 loc) · 6.09 KB
/
PurlGenerationTests.cs
File metadata and controls
138 lines (116 loc) · 6.09 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
#nullable disable
namespace Microsoft.ComponentDetection.Contracts.Tests;
using AwesomeAssertions;
using Microsoft.ComponentDetection.Contracts.TypedComponent;
using Microsoft.VisualStudio.TestTools.UnitTesting;
[TestClass]
[TestCategory("Governance/All")]
[TestCategory("Governance/ComponentDetection")]
public class PurlGenerationTests
{
[TestMethod]
public void NpmPackageNameShouldBeLowerCase()
{
// According to the spec package name should not have uppercase letters
// https://github.com/package-url/purl-spec/blame/180c46d266c45aa2bd81a2038af3f78e87bb4a25/README.rst#L656
var npmComponent = new NpmComponent("TEST", "1.2.3");
npmComponent.PackageUrl.Name.Should().Be("test");
}
[TestMethod]
public void GoPackageShouldPreferHashOverVersion()
{
// Commit should be used in place of version when available
// https://github.com/package-url/purl-spec/blame/180c46d266c45aa2bd81a2038af3f78e87bb4a25/README.rst#L610
var goComponent = new GoComponent("github.com/example/test", "1.2.3", "deadbeef");
goComponent.PackageUrl.Version.Should().Be("deadbeef");
}
[TestMethod]
public void PipPackageShouldBeModified()
{
// Package name should be lowercased and replace '_' with '-'
// https://github.com/package-url/purl-spec/blame/180c46d266c45aa2bd81a2038af3f78e87bb4a25/README.rst#L680
var pipComponent = new PipComponent("CHANGE_ME", "1.2.3");
pipComponent.PackageUrl.Name.Should().Be("change-me");
}
[TestMethod]
public void DebianAndUbuntuAreDebType()
{
// Ubuntu and debian are "deb" component types
// https://github.com/package-url/purl-spec/blame/180c46d266c45aa2bd81a2038af3f78e87bb4a25/README.rst#L537
var ubuntuComponent = new LinuxComponent("Ubuntu", "18.04", "bash", "1");
var debianComponent = new LinuxComponent("Debian", "buster", "bash", "1");
ubuntuComponent.PackageUrl.Type.Should().Be("deb");
debianComponent.PackageUrl.Type.Should().Be("deb");
ubuntuComponent.PackageUrl.Qualifiers["distro"].Should().Be("ubuntu-18.04");
debianComponent.PackageUrl.Qualifiers["distro"].Should().Be("debian-buster");
}
[TestMethod]
public void CentOsFedoraAndRHELAreRpmType()
{
// CentOS, Fedora and RHEL use "rpm" component types
// https://github.com/package-url/purl-spec/blame/180c46d266c45aa2bd81a2038af3f78e87bb4a25/README.rst#L693
var centosComponent = new LinuxComponent("CentOS", "18.04", "bash", "1");
var fedoraComponent = new LinuxComponent("Fedora", "18.04", "bash", "1");
var rhelComponent = new LinuxComponent("Red Hat Enterprise Linux", "18.04", "bash", "1");
centosComponent.PackageUrl.Type.Should().Be("rpm");
fedoraComponent.PackageUrl.Type.Should().Be("rpm");
rhelComponent.PackageUrl.Type.Should().Be("rpm");
centosComponent.PackageUrl.Qualifiers["distro"].Should().Be("centos-18.04");
fedoraComponent.PackageUrl.Qualifiers["distro"].Should().Be("fedora-18.04");
rhelComponent.PackageUrl.Qualifiers["distro"].Should().Be("redhat-18.04");
}
[TestMethod]
public void AlpineIsApkType()
{
// Alpine uses "apk" purl type
// https://github.com/package-url/purl-spec/blob/master/PURL-TYPES.rst#apk
var alpineComponent = new LinuxComponent("Alpine", "3.13", "bash", "1");
alpineComponent.PackageUrl.Type.Should().Be("apk");
alpineComponent.PackageUrl.Namespace.Should().Be("alpine");
alpineComponent.PackageUrl.Qualifiers["distro"].Should().Be("alpine-3.13");
}
[TestMethod]
public void UnknownDistroDoesNotHavePurl()
{
var unknownLinuxComponent = new LinuxComponent("Linux", "0", "bash", "1'");
unknownLinuxComponent.PackageUrl.Should().BeNull();
}
[TestMethod]
public void DistroNamesAreLowerCased()
{
// Distros must be lower cased for both deb and rpm
// https://github.com/package-url/purl-spec/blame/180c46d266c45aa2bd81a2038af3f78e87bb4a25/README.rst#L537
// https://github.com/package-url/purl-spec/blame/180c46d266c45aa2bd81a2038af3f78e87bb4a25/README.rst#L694
var ubuntuComponent = new LinuxComponent("UbUnTu", "18.04", "bash", "1");
var fedoraComponent = new LinuxComponent("FeDoRa", "22", "bash", "1");
ubuntuComponent.PackageUrl.Namespace.Should().Be("ubuntu");
fedoraComponent.PackageUrl.Namespace.Should().Be("fedora");
}
[TestMethod]
public void RhelNamespaceIsRedhat()
{
// RHEL should use "redhat" as the namespace and distro id, matching Syft conventions
var rhelComponent = new LinuxComponent("Red Hat Enterprise Linux", "9.0", "bash", "1");
rhelComponent.PackageUrl.Namespace.Should().Be("redhat");
rhelComponent.PackageUrl.Qualifiers["distro"].Should().Be("redhat-9.0");
}
[TestMethod]
public void CocoaPodNameShouldSupportPurl()
{
// https://github.com/package-url/purl-spec/blob/b8ddd39a6d533b8895f3b741f2e62e2695d82aa4/PURL-TYPES.rst#cocoapods
var packageOne = new PodComponent("AFNetworking", "4.0.1");
var packageTwo = new PodComponent("MapsIndoors", "3.24.0");
var packageThree = new PodComponent("googleUtilities", "7.5.2");
packageOne.PackageUrl.Type.Should().Be("cocoapods");
packageOne.PackageUrl.ToString().Should().Be("pkg:cocoapods/AFNetworking@4.0.1");
packageTwo.PackageUrl.ToString().Should().Be("pkg:cocoapods/MapsIndoors@3.24.0");
packageThree.PackageUrl.ToString().Should().Be("pkg:cocoapods/googleUtilities@7.5.2");
}
[TestMethod]
public void CocoaPodNameShouldPurlWithCustomQualifier()
{
// https://github.com/package-url/purl-spec/blob/b8ddd39a6d533b8895f3b741f2e62e2695d82aa4/PURL-TYPES.rst#cocoapods
var packageOne = new PodComponent("AFNetworking", "4.0.1", "https://custom_repo.example.com/path/to/repo/specs.git");
packageOne.PackageUrl.ToString().Should().Be("pkg:cocoapods/AFNetworking@4.0.1?repository_url=https:%2F%2Fcustom_repo.example.com%2Fpath%2Fto%2Frepo%2Fspecs.git");
}
}