github.com/google/osv-scalibr@v0.4.1/artifact/image/layerscanning/testing/fakelayer/fake_layer.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 fakelayer provides a fake implementation of the image.Layer interface for testing
    16  // purposes.
    17  package fakelayer
    18  
    19  import (
    20  	"errors"
    21  	"io"
    22  	"io/fs"
    23  	"os"
    24  	"path"
    25  	"path/filepath"
    26  
    27  	scalibrfs "github.com/google/osv-scalibr/fs"
    28  	"github.com/opencontainers/go-digest"
    29  )
    30  
    31  // FakeLayer is a fake implementation of the image.Layer interface for testing purposes.
    32  type FakeLayer struct {
    33  	testDir      string
    34  	diffID       digest.Digest
    35  	buildCommand string
    36  	files        map[string]string
    37  }
    38  
    39  // New creates a new FakeLayer.
    40  func New(testDir string, diffID digest.Digest, buildCommand string, files map[string]string, filesAlreadyExist bool) (*FakeLayer, error) {
    41  	if !filesAlreadyExist {
    42  		for name, contents := range files {
    43  			filename := filepath.Join(testDir, name)
    44  			if err := os.MkdirAll(filepath.Dir(filename), 0700); err != nil {
    45  				return nil, err
    46  			}
    47  
    48  			if err := os.WriteFile(filename, []byte(contents), 0600); err != nil {
    49  				return nil, err
    50  			}
    51  		}
    52  	}
    53  	return &FakeLayer{
    54  		testDir:      testDir,
    55  		diffID:       diffID,
    56  		buildCommand: buildCommand,
    57  		files:        files,
    58  	}, nil
    59  }
    60  
    61  // FS is not currently used for the purposes of layer scanning, thus a nil value is returned.
    62  func (fakeLayer *FakeLayer) FS() scalibrfs.FS {
    63  	return fakeLayer
    64  }
    65  
    66  // DiffID returns the diffID of the layer.
    67  func (fakeLayer *FakeLayer) DiffID() digest.Digest {
    68  	return fakeLayer.diffID
    69  }
    70  
    71  // Command returns the command of the layer.
    72  func (fakeLayer *FakeLayer) Command() string {
    73  	return fakeLayer.buildCommand
    74  }
    75  
    76  // IsEmpty returns false for the purposes of layer scanning.
    77  func (fakeLayer *FakeLayer) IsEmpty() bool {
    78  	return false
    79  }
    80  
    81  // Uncompressed is not used for the purposes of layer scanning, thus a nil value is returned.
    82  func (fakeLayer *FakeLayer) Uncompressed() (io.ReadCloser, error) {
    83  	return nil, errors.New("not implemented")
    84  }
    85  
    86  // -------------------------------------------------------------------------------------------------
    87  // scalibrfs.FS implementation
    88  // -------------------------------------------------------------------------------------------------
    89  
    90  // Open returns a file if it exists in the files map.
    91  func (fakeLayer *FakeLayer) Open(name string) (fs.File, error) {
    92  	filename := filepath.Join(fakeLayer.testDir, name)
    93  
    94  	return os.Open(filename)
    95  }
    96  
    97  // Stat returns the file info of a file if it exists in the files map.
    98  func (fakeLayer *FakeLayer) Stat(name string) (fs.FileInfo, error) {
    99  	return os.Stat(path.Join(fakeLayer.testDir, name))
   100  }
   101  
   102  // ReadDir reads the directory named by name and returns a list of directory entries.
   103  func (fakeLayer *FakeLayer) ReadDir(name string) ([]fs.DirEntry, error) {
   104  	return os.ReadDir(filepath.Join(fakeLayer.testDir, name))
   105  }