github.com/google/osv-scalibr@v0.4.1/extractor/filesystem/language/java/archive/filename_test.go (about) 1 // Copyright 2025 Google LLC 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package archive_test 16 17 import ( 18 "testing" 19 20 "github.com/google/osv-scalibr/extractor/filesystem/language/java/archive" 21 ) 22 23 func TestParseFilename(t *testing.T) { 24 tests := []struct { 25 desc string 26 path string 27 want *archive.JarProps 28 }{ 29 { 30 desc: "[name]-[version]", 31 path: "some/path/yolo-1.2.3.jar", 32 want: &archive.JarProps{ 33 ArtifactID: "yolo", 34 Version: "1.2.3", 35 }, 36 }, 37 { 38 desc: "Group_ID_in_filename", 39 path: "some/path/com.google.src.yolo-1.2.3.jar", 40 want: &archive.JarProps{ 41 ArtifactID: "com.google.src.yolo", 42 Version: "1.2.3", 43 GroupID: "com.google.src", 44 }, 45 }, 46 { 47 desc: "Multiple_dashes_in_name", 48 path: "some/path/the-yolo-package-1.2.3.jar", 49 want: &archive.JarProps{ 50 ArtifactID: "the-yolo-package", 51 Version: "1.2.3", 52 }, 53 }, 54 { 55 desc: "Multiple_dashes_in_version", 56 path: "some/path/yolo-1.2.3-jre.jar", 57 want: &archive.JarProps{ 58 ArtifactID: "yolo", 59 Version: "1.2.3-jre", 60 }, 61 }, 62 { 63 desc: "[name]_[version]", 64 path: "some/path/the-yolo-package_1.2.3-jre.jar", 65 want: &archive.JarProps{ 66 ArtifactID: "the-yolo-package", 67 Version: "1.2.3-jre", 68 }, 69 }, 70 { 71 desc: "[name].[version]", 72 path: "some/path/the-yolo-package.1.2.3-jre.jar", 73 want: &archive.JarProps{ 74 ArtifactID: "the-yolo-package", 75 Version: "1.2.3-jre", 76 }, 77 }, 78 { 79 desc: "ambiguous_versioning", 80 path: "mockito-4-2_3-3.2.12.0-RC2.jar", 81 // Incorrect parsing behavior: According to 82 // https://mvnrepository.com/artifact/org.scalatestplus/mockito-4-2_3/3.2.12.0-RC2 83 // The package mockito-4-2_3 and version is 2.12.0-RC2. To get this right we'd need 84 // to somehow know that the initial 4-2_3 is part of the package name. 85 want: &archive.JarProps{ 86 ArtifactID: "mockito", 87 Version: "4-2_3-3.2.12.0-RC2", 88 }, 89 }, 90 { 91 desc: "Version_starts_with_'build'", 92 path: "some/path/yolo-build1.2.3.jar", 93 want: &archive.JarProps{ 94 ArtifactID: "yolo", 95 Version: "build1.2.3", 96 }, 97 }, 98 { 99 desc: "'build'_part_of_package_name", 100 path: "some/path/yolo-buildasd-1.2.3.jar", 101 want: &archive.JarProps{ 102 ArtifactID: "yolo-buildasd", 103 Version: "1.2.3", 104 }, 105 }, 106 { 107 desc: "Version_starts_with_'r'", 108 path: "some/path/yolo-r12.jar", 109 want: &archive.JarProps{ 110 ArtifactID: "yolo", 111 Version: "r12", 112 }, 113 }, 114 { 115 desc: "Version_starts_with_'rc'", 116 path: "usr/share/java/jcsp-core-rc4.jar", 117 want: &archive.JarProps{ 118 ArtifactID: "jcsp-core", 119 Version: "rc4", 120 }, 121 }, 122 { 123 desc: "'rc'_part_of_package_name", 124 path: "some/path/yolo-rc1asd-1.2.3.jar", 125 want: &archive.JarProps{ 126 ArtifactID: "yolo-rc1asd", 127 Version: "1.2.3", 128 }, 129 }, 130 } 131 132 for _, tt := range tests { 133 t.Run(tt.desc, func(t *testing.T) { 134 if got := archive.ParseFilename(tt.path); *got != *tt.want { 135 t.Errorf("ParseFilename(%s): got %v, want %v", tt.path, got, tt.want) 136 } 137 }) 138 } 139 } 140 141 func TestParseFilenameVersionNotFound(t *testing.T) { 142 tests := []struct { 143 desc string 144 path string 145 }{ 146 { 147 desc: "no_version_in_name", 148 path: "some/path/yolo.jar", 149 }, 150 151 { 152 desc: "not_a_Java_archive", 153 path: "some/path/foo", 154 }, 155 { 156 desc: "empty_path", 157 path: "", 158 }, 159 } 160 161 for _, tt := range tests { 162 t.Run(tt.desc, func(t *testing.T) { 163 if got := archive.ParseFilename(tt.path); got != nil { 164 t.Errorf("ParseFilename(%s): got %v, want nil", tt.path, got) 165 } 166 }) 167 } 168 }