github.com/opencontainers/umoci@v0.4.8-0.20240508124516-656e4836fb0d/pkg/hardening/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 hardening
    22  
    23  import (
    24  	"bytes"
    25  	_ "crypto/sha256" // Import is necessary for go-digest
    26  	"github.com/opencontainers/go-digest"
    27  	"io/ioutil"
    28  )
    29  
    30  // Fuzz fuzzes the VerifiedReader.Read() implementation.
    31  func Fuzz(data []byte) int {
    32  	buffer := bytes.NewBuffer(data)
    33  	size := len(data)
    34  	if !digest.SHA256.Available() {
    35  		return -1
    36  	}
    37  	expectedDigest := digest.SHA256.FromBytes(buffer.Bytes())
    38  	verifiedReader := &VerifiedReadCloser{
    39  		Reader:         ioutil.NopCloser(buffer),
    40  		ExpectedDigest: expectedDigest,
    41  		ExpectedSize:   int64(size),
    42  	}
    43  	_, err := verifiedReader.Read(data)
    44  	if err != nil {
    45  		return 0
    46  	}
    47  	return 1
    48  }