github.com/anchore/syft@v1.38.2/syft/pkg/cataloger/dotnet/deps_json_test.go (about) 1 package dotnet 2 3 import ( 4 "testing" 5 6 "github.com/google/go-cmp/cmp" 7 "github.com/stretchr/testify/assert" 8 "github.com/stretchr/testify/require" 9 10 "github.com/anchore/syft/syft/file" 11 ) 12 13 func TestTrimLibPrefix(t *testing.T) { 14 tests := []struct { 15 name string 16 input string 17 expected string 18 }{ 19 { 20 name: "Empty path", 21 input: "", 22 expected: "", 23 }, 24 { 25 name: "simple .NET 6.0 path", 26 input: "lib/net6.0/Humanizer.dll", 27 expected: "Humanizer.dll", 28 }, 29 { 30 name: "locale-specific resource path", 31 input: "lib/net6.0/af/Humanizer.resources.dll", 32 expected: "af/Humanizer.resources.dll", 33 }, 34 { 35 name: "netstandard path", 36 input: "lib/netstandard2.0/Serilog.Sinks.Console.dll", 37 expected: "Serilog.Sinks.Console.dll", 38 }, 39 { 40 name: "runtime-specific path", 41 input: "runtimes/linux-arm/lib/netcoreapp2.2/System.Collections.Concurrent.dll", 42 expected: "System.Collections.Concurrent.dll", 43 }, 44 { 45 name: "runtime-specific path with locale", 46 input: "runtimes/win/lib/net6.0/fr-ME/re/Microsoft.Data.SqlClient.resources.dll", 47 expected: "fr-ME/re/Microsoft.Data.SqlClient.resources.dll", 48 }, 49 { 50 name: "subdirectories", 51 input: "lib/net7.0/Microsoft/Extensions/Logging.dll", 52 expected: "Microsoft/Extensions/Logging.dll", 53 }, 54 { 55 name: "doesn't match the pattern", 56 input: "content/styles/main.css", 57 expected: "content/styles/main.css", 58 }, 59 { 60 name: "different framework format", 61 input: "lib/net472/Newtonsoft.Json.dll", 62 expected: "Newtonsoft.Json.dll", 63 }, 64 { 65 name: "frameworkless lib", 66 input: "lib/Newtonsoft.Json.dll", 67 expected: "lib/Newtonsoft.Json.dll", // should not match our pattern 68 }, 69 } 70 71 for _, tc := range tests { 72 t.Run(tc.name, func(t *testing.T) { 73 result := trimLibPrefix(tc.input) 74 if result != tc.expected { 75 t.Errorf("trimLibPrefix(%q) = %q; want %q", tc.input, result, tc.expected) 76 } 77 }) 78 } 79 } 80 81 func TestGetLogicalDepsJSON_MergeTargets(t *testing.T) { 82 deps := depsJSON{ 83 Location: file.NewLocation("/path/to/deps.json"), 84 RuntimeTarget: runtimeTarget{ 85 Name: ".NETCoreApp,Version=v6.0", 86 }, 87 // note: for this test we have two targets with the same name, which will be merged when creating a logical deps 88 Targets: map[string]map[string]depsTarget{ 89 ".NETCoreApp,Version=v6.0": { 90 "Microsoft.CodeAnalysis.CSharp/4.0.0": { 91 Dependencies: map[string]string{ 92 "Microsoft.CodeAnalysis.Common": "4.0.0", 93 }, 94 Runtime: map[string]map[string]string{ 95 "lib/netcoreapp3.1/Microsoft.CodeAnalysis.CSharp.dll": { 96 "assemblyVersion": "4.0.0.0", 97 "fileVersion": "4.0.21.51404", 98 }, 99 }, 100 Resources: map[string]map[string]string{ 101 "lib/netcoreapp3.1/cs/Microsoft.CodeAnalysis.CSharp.resources.dll": { 102 "locale": "cs", 103 }, 104 "lib/netcoreapp3.1/de/Microsoft.CodeAnalysis.CSharp.resources.dll": { 105 "locale": "de", 106 }, 107 "lib/netcoreapp3.1/es/Microsoft.CodeAnalysis.CSharp.resources.dll": { 108 "locale": "es", 109 }, 110 "lib/netcoreapp3.1/fr/Microsoft.CodeAnalysis.CSharp.resources.dll": { 111 "locale": "fr", 112 }, 113 "lib/netcoreapp3.1/it/Microsoft.CodeAnalysis.CSharp.resources.dll": { 114 "locale": "it", 115 }, 116 "lib/netcoreapp3.1/ja/Microsoft.CodeAnalysis.CSharp.resources.dll": { 117 "locale": "ja", 118 }, 119 "lib/netcoreapp3.1/ko/Microsoft.CodeAnalysis.CSharp.resources.dll": { 120 "locale": "ko", 121 }, 122 "lib/netcoreapp3.1/pl/Microsoft.CodeAnalysis.CSharp.resources.dll": { 123 "locale": "pl", 124 }, 125 "lib/netcoreapp3.1/pt-BR/Microsoft.CodeAnalysis.CSharp.resources.dll": { 126 "locale": "pt-BR", 127 }, 128 "lib/netcoreapp3.1/ru/Microsoft.CodeAnalysis.CSharp.resources.dll": { 129 "locale": "ru", 130 }, 131 "lib/netcoreapp3.1/tr/Microsoft.CodeAnalysis.CSharp.resources.dll": { 132 "locale": "tr", 133 }, 134 "lib/netcoreapp3.1/zh-Hans/Microsoft.CodeAnalysis.CSharp.resources.dll": { 135 "locale": "zh-Hans", 136 }, 137 "lib/netcoreapp3.1/zh-Hant/Microsoft.CodeAnalysis.CSharp.resources.dll": { 138 "locale": "zh-Hant", 139 }, 140 }, 141 }, 142 }, 143 "net6.0": { 144 "Microsoft.CodeAnalysis.CSharp/4.0.0": { 145 Dependencies: map[string]string{ 146 "Microsoft.CodeAnalysis.Common": "4.0.0", 147 }, 148 Compile: map[string]map[string]string{ 149 "lib/netcoreapp3.1/Microsoft.CodeAnalysis.CSharp.dll": {}, 150 }, 151 }, 152 "Microsoft.CodeAnalysis.Common/4.0.0": { 153 Dependencies: map[string]string{ 154 "Microsoft.CodeAnalysis.CSharp": "4.0.0", 155 }, 156 }, 157 }, 158 }, 159 Libraries: map[string]depsLibrary{ 160 "Microsoft.CodeAnalysis.CSharp/4.0.0": { 161 Type: "package", 162 Path: "microsoft.codeanalysis.csharp/4.0.0", 163 Sha512: "sha512-example-hash", 164 HashPath: "microsoft.codeanalysis.csharp.4.0.0.nupkg.sha512", 165 }, 166 }, 167 } 168 169 result := getLogicalDepsJSON(deps, &libmanJSON{}) 170 171 assert.Equal(t, "/path/to/deps.json", result.Location.RealPath) 172 assert.Equal(t, ".NETCoreApp,Version=v6.0", result.RuntimeTarget.Name) 173 174 libPackage, exists := result.PackagesByNameVersion["Microsoft.CodeAnalysis.CSharp/4.0.0"] 175 require.True(t, exists, "Expected to find the merged package") 176 177 assert.NotNil(t, libPackage.Library) 178 assert.Equal(t, "package", libPackage.Library.Type) 179 assert.Equal(t, "microsoft.codeanalysis.csharp/4.0.0", libPackage.Library.Path) 180 assert.Equal(t, "sha512-example-hash", libPackage.Library.Sha512) 181 assert.Equal(t, "microsoft.codeanalysis.csharp.4.0.0.nupkg.sha512", libPackage.Library.HashPath) 182 183 require.Equal(t, 2, len(libPackage.Targets), "Expected 2 targets to be merged") 184 185 expectedRuntimePaths := map[string]string{ 186 "Microsoft.CodeAnalysis.CSharp.dll": "lib/netcoreapp3.1/Microsoft.CodeAnalysis.CSharp.dll", 187 } 188 if diff := cmp.Diff(expectedRuntimePaths, libPackage.RuntimePathsByRelativeDLLPath); diff != "" { 189 t.Errorf("RuntimePathsByRelativeDLLPath mismatch (-expected +actual):\n%s", diff) 190 } 191 192 expectedResourcePaths := map[string]string{ 193 "cs/Microsoft.CodeAnalysis.CSharp.resources.dll": "lib/netcoreapp3.1/cs/Microsoft.CodeAnalysis.CSharp.resources.dll", 194 "de/Microsoft.CodeAnalysis.CSharp.resources.dll": "lib/netcoreapp3.1/de/Microsoft.CodeAnalysis.CSharp.resources.dll", 195 "es/Microsoft.CodeAnalysis.CSharp.resources.dll": "lib/netcoreapp3.1/es/Microsoft.CodeAnalysis.CSharp.resources.dll", 196 "fr/Microsoft.CodeAnalysis.CSharp.resources.dll": "lib/netcoreapp3.1/fr/Microsoft.CodeAnalysis.CSharp.resources.dll", 197 "it/Microsoft.CodeAnalysis.CSharp.resources.dll": "lib/netcoreapp3.1/it/Microsoft.CodeAnalysis.CSharp.resources.dll", 198 "ja/Microsoft.CodeAnalysis.CSharp.resources.dll": "lib/netcoreapp3.1/ja/Microsoft.CodeAnalysis.CSharp.resources.dll", 199 "ko/Microsoft.CodeAnalysis.CSharp.resources.dll": "lib/netcoreapp3.1/ko/Microsoft.CodeAnalysis.CSharp.resources.dll", 200 "pl/Microsoft.CodeAnalysis.CSharp.resources.dll": "lib/netcoreapp3.1/pl/Microsoft.CodeAnalysis.CSharp.resources.dll", 201 "pt-BR/Microsoft.CodeAnalysis.CSharp.resources.dll": "lib/netcoreapp3.1/pt-BR/Microsoft.CodeAnalysis.CSharp.resources.dll", 202 "ru/Microsoft.CodeAnalysis.CSharp.resources.dll": "lib/netcoreapp3.1/ru/Microsoft.CodeAnalysis.CSharp.resources.dll", 203 "tr/Microsoft.CodeAnalysis.CSharp.resources.dll": "lib/netcoreapp3.1/tr/Microsoft.CodeAnalysis.CSharp.resources.dll", 204 "zh-Hans/Microsoft.CodeAnalysis.CSharp.resources.dll": "lib/netcoreapp3.1/zh-Hans/Microsoft.CodeAnalysis.CSharp.resources.dll", 205 "zh-Hant/Microsoft.CodeAnalysis.CSharp.resources.dll": "lib/netcoreapp3.1/zh-Hant/Microsoft.CodeAnalysis.CSharp.resources.dll", 206 } 207 if diff := cmp.Diff(expectedResourcePaths, libPackage.ResourcePathsByRelativeDLLPath); diff != "" { 208 t.Errorf("ResourcePathsByRelativeDLLPath mismatch (-expected +actual):\n%s", diff) 209 } 210 211 expectedCompilePaths := map[string]string{ 212 "Microsoft.CodeAnalysis.CSharp.dll": "lib/netcoreapp3.1/Microsoft.CodeAnalysis.CSharp.dll", 213 } 214 if diff := cmp.Diff(expectedCompilePaths, libPackage.CompilePathsByRelativeDLLPath); diff != "" { 215 t.Errorf("CompilePathsByRelativeDLLPath mismatch (-expected +actual):\n%s", diff) 216 } 217 218 assert.Equal(t, 0, libPackage.NativePaths.Size(), "Expected no native paths") 219 220 assert.True(t, result.PackageNameVersions.Has("Microsoft.CodeAnalysis.CSharp/4.0.0")) 221 }