github.com/google/osv-scalibr@v0.4.1/binary/proto/package_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 proto_test 16 17 import ( 18 "errors" 19 "testing" 20 21 "github.com/google/go-cmp/cmp" 22 "github.com/google/osv-scalibr/binary/proto" 23 "github.com/google/osv-scalibr/extractor" 24 "google.golang.org/protobuf/testing/protocmp" 25 26 spb "github.com/google/osv-scalibr/binary/proto/scan_result_go_proto" 27 ) 28 29 var ( 30 pkgOpts = []cmp.Option{ 31 protocmp.IgnoreFields(&spb.Package{}, "id"), 32 } 33 ) 34 35 func TestPackageToProto(t *testing.T) { 36 testCases := []struct { 37 desc string 38 pkg *extractor.Package 39 want *spb.Package 40 wantErr error 41 }{ 42 { 43 desc: "nil", 44 pkg: nil, 45 want: nil, 46 }, 47 { 48 desc: "success", 49 pkg: purlDPKGAnnotationPackage, 50 want: purlDPKGAnnotationPackageProto, 51 }, 52 } 53 54 for _, tc := range testCases { 55 t.Run(tc.desc, func(t *testing.T) { 56 got, err := proto.PackageToProto(tc.pkg) 57 if !errors.Is(err, tc.wantErr) { 58 t.Errorf("PackageToProto(%v) returned error %v, want error %v", tc.pkg, err, tc.wantErr) 59 } 60 61 if got != nil { 62 if got.GetId() == "" { 63 t.Errorf("PackageToProto(%v) returned empty ID, want non-empty ID", tc.pkg) 64 } 65 // Ignore the ID field because it is randomly generated. 66 got.Id = "" 67 } 68 69 if diff := cmp.Diff(tc.want, got, protocmp.Transform()); diff != "" { 70 t.Fatalf("PackageToProto(%v) returned diff (-want +got):\n%s", tc.pkg, diff) 71 } 72 73 // No need to test the reverse conversion if the result is nil. 74 if got == nil { 75 return 76 } 77 78 // Test the reverse conversion for completeness. 79 gotPB, err := proto.PackageToStruct(got) 80 if err != nil { 81 t.Fatalf("PackageToStruct(%v) returned error %v, want nil", got, err) 82 } 83 if diff := cmp.Diff(tc.pkg, gotPB, protocmp.Transform()); diff != "" { 84 t.Fatalf("PackageToStruct(%v) returned diff (-want +got):\n%s", got, diff) 85 } 86 }) 87 } 88 } 89 90 func TestPackageToStruct(t *testing.T) { 91 testCases := []struct { 92 desc string 93 pkg *spb.Package 94 want *extractor.Package 95 wantErr error 96 }{ 97 { 98 desc: "nil", 99 pkg: nil, 100 want: nil, 101 }, 102 { 103 desc: "success", 104 pkg: purlDPKGAnnotationPackageProto, 105 want: purlDPKGAnnotationPackage, 106 }, 107 } 108 109 for _, tc := range testCases { 110 t.Run(tc.desc, func(t *testing.T) { 111 got, err := proto.PackageToStruct(tc.pkg) 112 if !errors.Is(err, tc.wantErr) { 113 t.Fatalf("PackageToStruct(%v) returned error %v, want error %v", tc.pkg, err, tc.wantErr) 114 } 115 if diff := cmp.Diff(tc.want, got, protocmp.Transform()); diff != "" { 116 t.Fatalf("PackageToStruct(%v) returned diff (-want +got):\n%s", tc.pkg, diff) 117 } 118 119 // No need to test the reverse conversion if the result is nil. 120 if got == nil { 121 return 122 } 123 124 // Test the reverse conversion for completeness. 125 gotPB, err := proto.PackageToProto(got) 126 if err != nil { 127 t.Fatalf("PackageToProto(%v) returned error %v, want nil", got, err) 128 } 129 // Ignore the ID field because it is randomly generated. 130 gotPB.Id = "" 131 if diff := cmp.Diff(tc.pkg, gotPB, protocmp.Transform()); diff != "" { 132 t.Fatalf("PackageToProto(%v) returned diff (-want +got):\n%s", got, diff) 133 } 134 }) 135 } 136 }