github.com/google/osv-scalibr@v0.4.1/binary/proto/io_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 "os" 19 "path/filepath" 20 "strings" 21 "testing" 22 23 "github.com/google/go-cmp/cmp" 24 "github.com/google/osv-scalibr/binary/proto" 25 26 spb "github.com/google/osv-scalibr/binary/proto/scan_result_go_proto" 27 ) 28 29 func TestWrite(t *testing.T) { 30 testDirPath := t.TempDir() 31 var result = &spb.ScanResult{Version: "1.0.0"} 32 testCases := []struct { 33 desc string 34 path string 35 expectedPrefix string 36 }{ 37 { 38 desc: "textproto", 39 path: "output.textproto", 40 expectedPrefix: "version:", 41 }, 42 { 43 desc: "binproto", 44 path: "output.binproto", 45 expectedPrefix: "\x0a\x051.0.0", 46 }, 47 { 48 desc: "gzipped file", 49 path: "output.textproto.gz", 50 expectedPrefix: "\x1f\x8b", 51 }, 52 } 53 54 for _, tc := range testCases { 55 t.Run(tc.desc, func(t *testing.T) { 56 fullPath := filepath.Join(testDirPath, tc.path) 57 err := proto.Write(fullPath, result) 58 if err != nil { 59 t.Fatalf("proto.Write(%s, %v) returned an error: %v", fullPath, result, err) 60 } 61 62 content, err := os.ReadFile(fullPath) 63 if err != nil { 64 t.Fatalf("error while reading %s: %v", fullPath, err) 65 } 66 prefix := content[:len(tc.expectedPrefix)] 67 if diff := cmp.Diff(tc.expectedPrefix, string(prefix)); diff != "" { 68 t.Errorf("%s contains unexpected prefix, diff (-want +got):\n%s", fullPath, diff) 69 } 70 }) 71 } 72 } 73 74 func TestWrite_InvalidFilename(t *testing.T) { 75 testDirPath := t.TempDir() 76 testPaths := []string{ 77 "config.invalid-extension", 78 "config.invalid-extension.gz", 79 "no-extension", 80 "no-extension.gz", 81 } 82 for _, p := range testPaths { 83 fullPath := filepath.Join(testDirPath, p) 84 if err := proto.Write(fullPath, &spb.ScanResult{}); err == nil || 85 !strings.HasPrefix(err.Error(), "invalid filename") { 86 t.Errorf("proto.Write(%s) didn't return an invalid file error: %v", fullPath, err) 87 } 88 } 89 } 90 91 func TestWriteWithFormat(t *testing.T) { 92 testDirPath := t.TempDir() 93 var result = &spb.ScanResult{Version: "1.0.0"} 94 testCases := []struct { 95 desc string 96 format string 97 expectedPrefix string 98 }{ 99 { 100 desc: "textproto", 101 format: "textproto", 102 expectedPrefix: "version:", 103 }, 104 { 105 desc: "binproto", 106 format: "binproto", 107 expectedPrefix: "\x0a\x051.0.0", 108 }, 109 } 110 111 for _, tc := range testCases { 112 t.Run(tc.desc, func(t *testing.T) { 113 fullPath := filepath.Join(testDirPath, "output") 114 err := proto.WriteWithFormat(fullPath, result, tc.format) 115 if err != nil { 116 t.Fatalf("proto.WriteWithFormat(%s, %v, %s) returned an error: %v", fullPath, result, tc.format, err) 117 } 118 119 content, err := os.ReadFile(fullPath) 120 if err != nil { 121 t.Fatalf("error while reading %s: %v", fullPath, err) 122 } 123 prefix := content[:len(tc.expectedPrefix)] 124 if diff := cmp.Diff(tc.expectedPrefix, string(prefix)); diff != "" { 125 t.Errorf("%s contains unexpected prefix, diff (-want +got):\n%s", fullPath, diff) 126 } 127 }) 128 } 129 }