github.com/google/syzkaller@v0.0.0-20240517125934-c0f1611a36d6/pkg/asset/backend_dummy.go (about)

     1  // Copyright 2022 syzkaller project authors. All rights reserved.
     2  // Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.
     3  
     4  package asset
     5  
     6  import (
     7  	"fmt"
     8  	"strings"
     9  	"time"
    10  )
    11  
    12  type objectUploadCallback func(req *uploadRequest) (*uploadResponse, error)
    13  type objectRemoveCallback func(url string) error
    14  
    15  type dummyObject struct {
    16  	createdAt       time.Time
    17  	contentType     string
    18  	contentEncoding string
    19  }
    20  
    21  type dummyStorageBackend struct {
    22  	currentTime  time.Time
    23  	objects      map[string]*dummyObject
    24  	objectUpload objectUploadCallback
    25  	objectRemove objectRemoveCallback
    26  }
    27  
    28  func makeDummyStorageBackend() *dummyStorageBackend {
    29  	return &dummyStorageBackend{
    30  		currentTime: time.Now(),
    31  		objects:     make(map[string]*dummyObject),
    32  	}
    33  }
    34  
    35  type dummyWriteCloser struct {
    36  }
    37  
    38  func (dwc *dummyWriteCloser) Write(p []byte) (int, error) {
    39  	return len(p), nil
    40  }
    41  
    42  func (dwc *dummyWriteCloser) Close() error {
    43  	return nil
    44  }
    45  
    46  func (be *dummyStorageBackend) upload(req *uploadRequest) (*uploadResponse, error) {
    47  	be.objects[req.savePath] = &dummyObject{
    48  		createdAt:       be.currentTime,
    49  		contentType:     req.contentType,
    50  		contentEncoding: req.contentEncoding,
    51  	}
    52  	if be.objectUpload != nil {
    53  		return be.objectUpload(req)
    54  	}
    55  	return &uploadResponse{writer: &dummyWriteCloser{}, path: req.savePath}, nil
    56  }
    57  
    58  func (be *dummyStorageBackend) downloadURL(path string, publicURL bool) (string, error) {
    59  	return "http://download/" + path, nil
    60  }
    61  
    62  func (be *dummyStorageBackend) getPath(url string) (string, error) {
    63  	if strings.HasPrefix(url, "http://unknown-bucket/") {
    64  		return "", ErrUnknownBucket
    65  	}
    66  	if strings.HasPrefix(url, "http://download/") {
    67  		return strings.TrimPrefix(url, "http://download/"), nil
    68  	}
    69  	return "", fmt.Errorf("unknown URL format")
    70  }
    71  
    72  func (be *dummyStorageBackend) list() ([]storedObject, error) {
    73  	ret := []storedObject{}
    74  	for path, obj := range be.objects {
    75  		ret = append(ret, storedObject{
    76  			path:      path,
    77  			createdAt: obj.createdAt,
    78  		})
    79  	}
    80  	return ret, nil
    81  }
    82  
    83  func (be *dummyStorageBackend) remove(path string) error {
    84  	if be.objectRemove != nil {
    85  		if err := be.objectRemove(path); err != nil {
    86  			return err
    87  		}
    88  	}
    89  	if _, ok := be.objects[path]; !ok {
    90  		return ErrAssetDoesNotExist
    91  	}
    92  	delete(be.objects, path)
    93  	return nil
    94  }
    95  
    96  func (be *dummyStorageBackend) hasOnly(paths []string) error {
    97  	makeError := func() error {
    98  		return fmt.Errorf("object sets are not equal; needed: %#v; uploaded: %#v", paths, be.objects)
    99  	}
   100  	if len(paths) != len(be.objects) {
   101  		return makeError()
   102  	}
   103  	for _, path := range paths {
   104  		if be.objects[path] == nil {
   105  			return makeError()
   106  		}
   107  	}
   108  	return nil
   109  }