github.com/google/osv-scalibr@v0.4.1/extractor/filesystem/os/macapps/metadata_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 macapps_test 16 17 import ( 18 "testing" 19 20 "github.com/google/go-cmp/cmp" 21 "github.com/google/osv-scalibr/extractor/filesystem/os/macapps" 22 "google.golang.org/protobuf/proto" 23 "google.golang.org/protobuf/testing/protocmp" 24 25 pb "github.com/google/osv-scalibr/binary/proto/scan_result_go_proto" 26 ) 27 28 func TestSetProto(t *testing.T) { 29 testCases := []struct { 30 desc string 31 m *macapps.Metadata 32 p *pb.Package 33 want *pb.Package 34 }{ 35 { 36 desc: "nil_metadata", 37 m: nil, 38 p: &pb.Package{Name: "some-package"}, 39 want: &pb.Package{Name: "some-package"}, 40 }, 41 { 42 desc: "nil_package", 43 m: &macapps.Metadata{ 44 CFBundleDisplayName: "display-name", 45 }, 46 p: nil, 47 want: nil, 48 }, 49 { 50 desc: "set_metadata", 51 m: &macapps.Metadata{ 52 CFBundleDisplayName: "display-name", 53 }, 54 p: &pb.Package{Name: "some-package"}, 55 want: &pb.Package{ 56 Name: "some-package", 57 Metadata: &pb.Package_MacAppsMetadata{ 58 MacAppsMetadata: &pb.MacAppsMetadata{ 59 BundleDisplayName: "display-name", 60 }, 61 }, 62 }, 63 }, 64 { 65 desc: "override_metadata", 66 m: &macapps.Metadata{ 67 CFBundleDisplayName: "another-display-name", 68 }, 69 p: &pb.Package{ 70 Name: "some-package", 71 Metadata: &pb.Package_MacAppsMetadata{ 72 MacAppsMetadata: &pb.MacAppsMetadata{ 73 BundleDisplayName: "display-name", 74 }, 75 }, 76 }, 77 want: &pb.Package{ 78 Name: "some-package", 79 Metadata: &pb.Package_MacAppsMetadata{ 80 MacAppsMetadata: &pb.MacAppsMetadata{ 81 BundleDisplayName: "another-display-name", 82 }, 83 }, 84 }, 85 }, 86 { 87 desc: "set_all_fields", 88 m: &macapps.Metadata{ 89 CFBundleDisplayName: "display-name", 90 CFBundleIdentifier: "bundle-identifier", 91 CFBundleShortVersionString: "1.2.3", 92 CFBundleExecutable: "executable", 93 CFBundleName: "name", 94 CFBundlePackageType: "package-type", 95 CFBundleSignature: "signature", 96 CFBundleVersion: "1.2.3", 97 KSProductID: "product-id", 98 KSUpdateURL: "update-url", 99 }, 100 p: &pb.Package{Name: "some-package"}, 101 want: &pb.Package{ 102 Name: "some-package", 103 Metadata: &pb.Package_MacAppsMetadata{ 104 MacAppsMetadata: &pb.MacAppsMetadata{ 105 BundleDisplayName: "display-name", 106 BundleIdentifier: "bundle-identifier", 107 BundleShortVersionString: "1.2.3", 108 BundleExecutable: "executable", 109 BundleName: "name", 110 BundlePackageType: "package-type", 111 BundleSignature: "signature", 112 BundleVersion: "1.2.3", 113 ProductId: "product-id", 114 UpdateUrl: "update-url", 115 }, 116 }, 117 }, 118 }, 119 } 120 121 for _, tc := range testCases { 122 t.Run(tc.desc, func(t *testing.T) { 123 p := proto.Clone(tc.p).(*pb.Package) 124 tc.m.SetProto(p) 125 opts := []cmp.Option{ 126 protocmp.Transform(), 127 } 128 if diff := cmp.Diff(tc.want, p, opts...); diff != "" { 129 t.Errorf("Metatadata{%+v}.SetProto(%+v): (-want +got):\n%s", tc.m, tc.p, diff) 130 } 131 132 // Test the reverse conversion for completeness. 133 134 if tc.p == nil && tc.want == nil { 135 return 136 } 137 138 got := macapps.ToStruct(p.GetMacAppsMetadata()) 139 if diff := cmp.Diff(tc.m, got); diff != "" { 140 t.Errorf("ToStruct(%+v): (-want +got):\n%s", p.GetMacAppsMetadata(), diff) 141 } 142 }) 143 } 144 } 145 146 func TestToStruct(t *testing.T) { 147 testCases := []struct { 148 desc string 149 m *pb.MacAppsMetadata 150 want *macapps.Metadata 151 }{ 152 { 153 desc: "nil", 154 m: nil, 155 want: nil, 156 }, 157 { 158 desc: "some_fields", 159 m: &pb.MacAppsMetadata{ 160 BundleDisplayName: "display-name", 161 }, 162 want: &macapps.Metadata{ 163 CFBundleDisplayName: "display-name", 164 }, 165 }, 166 { 167 desc: "all_fields", 168 m: &pb.MacAppsMetadata{ 169 BundleDisplayName: "display-name", 170 BundleIdentifier: "bundle-identifier", 171 BundleShortVersionString: "1.2.3", 172 BundleExecutable: "executable", 173 BundleName: "name", 174 BundlePackageType: "package-type", 175 BundleSignature: "signature", 176 BundleVersion: "1.2.3", 177 ProductId: "product-id", 178 UpdateUrl: "update-url", 179 }, 180 want: &macapps.Metadata{ 181 CFBundleDisplayName: "display-name", 182 CFBundleIdentifier: "bundle-identifier", 183 CFBundleShortVersionString: "1.2.3", 184 CFBundleExecutable: "executable", 185 CFBundleName: "name", 186 CFBundlePackageType: "package-type", 187 CFBundleSignature: "signature", 188 CFBundleVersion: "1.2.3", 189 KSProductID: "product-id", 190 KSUpdateURL: "update-url", 191 }, 192 }, 193 } 194 195 for _, tc := range testCases { 196 t.Run(tc.desc, func(t *testing.T) { 197 got := macapps.ToStruct(tc.m) 198 if diff := cmp.Diff(tc.want, got); diff != "" { 199 t.Errorf("ToStruct(%+v): (-want +got):\n%s", tc.m, diff) 200 } 201 202 if tc.m == nil { 203 return 204 } 205 206 // Test the reverse conversion for completeness. 207 208 gotP := &pb.Package{} 209 wantP := &pb.Package{ 210 Metadata: &pb.Package_MacAppsMetadata{ 211 MacAppsMetadata: tc.m, 212 }, 213 } 214 got.SetProto(gotP) 215 opts := []cmp.Option{ 216 protocmp.Transform(), 217 } 218 if diff := cmp.Diff(wantP, gotP, opts...); diff != "" { 219 t.Errorf("Metatadata{%+v}.SetProto(%+v): (-want +got):\n%s", got, wantP, diff) 220 } 221 }) 222 } 223 }