github.com/opencontainers/umoci@v0.4.8-0.20240508124516-656e4836fb0d/oci/casext/fuzzer.go (about)

     1  //go:build gofuzz
     2  // +build gofuzz
     3  
     4  /*
     5   * umoci: Umoci Modifies Open Containers' Images
     6   * Copyright (C) 2021 SUSE LLC
     7   *
     8   * Licensed under the Apache License, Version 2.0 (the "License");
     9   * you may not use this file except in compliance with the License.
    10   * You may obtain a copy of the License at
    11   *
    12   *    http://www.apache.org/licenses/LICENSE-2.0
    13   *
    14   * Unless required by applicable law or agreed to in writing, software
    15   * distributed under the License is distributed on an "AS IS" BASIS,
    16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    17   * See the License for the specific language governing permissions and
    18   * limitations under the License.
    19   */
    20  
    21  package casext
    22  
    23  import (
    24  	"context"
    25  	"github.com/opencontainers/umoci/oci/cas/dir"
    26  	"io/ioutil"
    27  	"os"
    28  	"path/filepath"
    29  )
    30  
    31  // Fuzz fuzzes the implementation of dirEngine.{PutBlobJSON,GetBlob}.
    32  func Fuzz(data []byte) int {
    33  	ctx := context.Background()
    34  	root, err := ioutil.TempDir("", "umoci-TestEngineBlobJSON")
    35  	if err != nil {
    36  		return -1
    37  	}
    38  	defer os.RemoveAll(root)
    39  
    40  	image := filepath.Join(root, "image")
    41  	if err := dir.Create(image); err != nil {
    42  		return -1
    43  	}
    44  
    45  	engine, err := dir.Open(image)
    46  	if err != nil {
    47  		return -1
    48  	}
    49  	engineExt := NewEngine(engine)
    50  	defer engine.Close()
    51  
    52  	digest, _, err := engineExt.PutBlobJSON(ctx, string(data))
    53  	if err != nil {
    54  		return 0
    55  	}
    56  	blobReader, err := engine.GetBlob(ctx, digest)
    57  	if err != nil {
    58  		return 0
    59  	}
    60  	defer blobReader.Close()
    61  
    62  	_, err = ioutil.ReadAll(blobReader)
    63  	if err != nil {
    64  		return 0
    65  	}
    66  	return 1
    67  }