github.com/google/osv-scalibr@v0.4.1/enricher/vulnmatch/osvdev/fakeclient/fakeclient.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 fakeclient contains a mock implementation of the OSV.dev client for testing purposes.
    16  package fakeclient
    17  
    18  import (
    19  	"context"
    20  	"fmt"
    21  
    22  	"github.com/google/osv-scalibr/enricher/vulnmatch/osvdev"
    23  	osvpb "github.com/ossf/osv-schema/bindings/go/osvschema"
    24  	osvapipb "osv.dev/bindings/go/api"
    25  )
    26  
    27  type client struct {
    28  	data map[string][]*osvpb.Vulnerability
    29  }
    30  
    31  // New returns an OSV.dev fakeclient
    32  func New(data map[string][]*osvpb.Vulnerability) osvdev.Client {
    33  	return &client{
    34  		data: data,
    35  	}
    36  }
    37  
    38  // GetVulnByID implements osvdev.Client.
    39  func (c *client) GetVulnByID(_ context.Context, id string) (*osvpb.Vulnerability, error) {
    40  	for _, vulns := range c.data {
    41  		for _, vv := range vulns {
    42  			if vv.Id == id {
    43  				return vv, nil
    44  			}
    45  		}
    46  	}
    47  	return nil, fmt.Errorf("vuln %q not found", id)
    48  }
    49  
    50  // Query implements osvdev.Client.
    51  func (c *client) Query(_ context.Context, query *osvapipb.Query) (*osvapipb.VulnerabilityList, error) {
    52  	key := fmt.Sprintf("%s:%s:%s", query.Package.Name, query.GetVersion(), query.GetCommit())
    53  	return &osvapipb.VulnerabilityList{
    54  		Vulns: c.data[key],
    55  	}, nil
    56  }
    57  
    58  // QueryBatch implements osvdev.Client.
    59  func (c *client) QueryBatch(ctx context.Context, queries []*osvapipb.Query) (*osvapipb.BatchVulnerabilityList, error) {
    60  	res := &osvapipb.BatchVulnerabilityList{}
    61  
    62  	for _, qq := range queries {
    63  		if err := ctx.Err(); err != nil {
    64  			return res, err
    65  		}
    66  
    67  		rsp, err := c.Query(ctx, qq)
    68  		if err != nil {
    69  			return res, err
    70  		}
    71  
    72  		var vulns []*osvpb.Vulnerability
    73  		for _, vv := range rsp.Vulns {
    74  			vulns = append(vulns, &osvpb.Vulnerability{Id: vv.Id})
    75  		}
    76  		res.Results = append(res.Results, &osvapipb.VulnerabilityList{Vulns: vulns})
    77  	}
    78  
    79  	return res, nil
    80  }