github.com/google/osv-scalibr@v0.4.1/testing/mockregistry/mockregistry.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 mockregistry provides a mock implementation of the registry.Registry interface.
    16  package mockregistry
    17  
    18  import (
    19  	"errors"
    20  
    21  	"github.com/google/osv-scalibr/common/windows/registry"
    22  )
    23  
    24  var (
    25  	errFailedToOpenKey = errors.New("failed to open key")
    26  	errValueNotFound   = errors.New("value not found")
    27  )
    28  
    29  // Opener is an opener for the mock registry.
    30  type Opener struct {
    31  	Registry *MockRegistry
    32  }
    33  
    34  // NewOpener creates a new Opener for the mock registry.
    35  func NewOpener(registry *MockRegistry) *Opener {
    36  	return &Opener{Registry: registry}
    37  }
    38  
    39  // Open returns the mock registry.
    40  func (o *Opener) Open() (registry.Registry, error) {
    41  	return o.Registry, nil
    42  }
    43  
    44  // MockRegistry mocks registry access.
    45  type MockRegistry struct {
    46  	Keys map[string]registry.Key
    47  }
    48  
    49  // OpenKey open the requested registry key.
    50  // Note that for mock registry, the hive is not used.
    51  func (o *MockRegistry) OpenKey(_ string, path string) (registry.Key, error) {
    52  	if key, ok := o.Keys[path]; ok {
    53  		return key, nil
    54  	}
    55  
    56  	return nil, errFailedToOpenKey
    57  }
    58  
    59  // Close does nothing when mocking.
    60  func (o *MockRegistry) Close() error {
    61  	return nil
    62  }
    63  
    64  // MockKey mocks a registry.Key.
    65  type MockKey struct {
    66  	KName      string
    67  	KClassName string
    68  	KSubkeys   []registry.Key
    69  	KValues    []registry.Value
    70  }
    71  
    72  // Name returns the name of the key.
    73  func (o *MockKey) Name() string {
    74  	return o.KName
    75  }
    76  
    77  // Close does nothing when mocking.
    78  func (o *MockKey) Close() error {
    79  	return nil
    80  }
    81  
    82  // SubkeyNames returns the names of the subkeys of the key.
    83  func (o *MockKey) SubkeyNames() ([]string, error) {
    84  	var names []string
    85  	for _, subkey := range o.KSubkeys {
    86  		names = append(names, subkey.Name())
    87  	}
    88  
    89  	return names, nil
    90  }
    91  
    92  // Subkeys returns the subkeys of the key.
    93  func (o *MockKey) Subkeys() ([]registry.Key, error) {
    94  	return o.KSubkeys, nil
    95  }
    96  
    97  // ClassName returns the class name of the key.
    98  func (o *MockKey) ClassName() ([]byte, error) {
    99  	return []byte(o.KClassName), nil
   100  }
   101  
   102  // Value returns the value with the given name.
   103  func (o *MockKey) Value(name string) (registry.Value, error) {
   104  	for _, value := range o.KValues {
   105  		if value.Name() == name {
   106  			return value, nil
   107  		}
   108  	}
   109  
   110  	return nil, errValueNotFound
   111  }
   112  
   113  // ValueBytes directly returns the content (as bytes) of the named value.
   114  func (o *MockKey) ValueBytes(name string) ([]byte, error) {
   115  	value, err := o.Value(name)
   116  	if err != nil {
   117  		return nil, err
   118  	}
   119  
   120  	return value.Data()
   121  }
   122  
   123  // ValueString directly returns the content (as string) of the named value.
   124  func (o *MockKey) ValueString(name string) (string, error) {
   125  	value, err := o.Value(name)
   126  	if err != nil {
   127  		return "", err
   128  	}
   129  
   130  	return value.DataString()
   131  }
   132  
   133  // Values returns the different values contained in the key.
   134  func (o *MockKey) Values() ([]registry.Value, error) {
   135  	return o.KValues, nil
   136  }
   137  
   138  // MockValue mocks a registry.Value.
   139  type MockValue struct {
   140  	VName       string
   141  	VData       []byte
   142  	VDataString string
   143  }
   144  
   145  // Name returns the name of the value.
   146  func (o *MockValue) Name() string {
   147  	return o.VName
   148  }
   149  
   150  // Data returns the data contained in the value.
   151  func (o *MockValue) Data() ([]byte, error) {
   152  	return o.VData, nil
   153  }
   154  
   155  // DataString returns the data contained in the value as a string.
   156  func (o *MockValue) DataString() (string, error) {
   157  	return o.VDataString, nil
   158  }