oras.land/oras-go/v2@v2.5.1-0.20240520045656-aef90e4d04c4/content/example_test.go (about)

     1  /*
     2  Copyright The ORAS Authors.
     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  
    16  package content_test
    17  
    18  import (
    19  	"bytes"
    20  	"fmt"
    21  	"io"
    22  
    23  	ocispec "github.com/opencontainers/image-spec/specs-go/v1"
    24  	"oras.land/oras-go/v2/content"
    25  )
    26  
    27  // ExampleVerifyReader gives an example of creating and using VerifyReader.
    28  func ExampleVerifyReader() {
    29  	blob := []byte("hello world")
    30  	desc := content.NewDescriptorFromBytes(ocispec.MediaTypeImageLayer, blob)
    31  	r := bytes.NewReader(blob)
    32  	vr := content.NewVerifyReader(r, desc)
    33  	buf := bytes.NewBuffer(nil)
    34  	if _, err := io.Copy(buf, vr); err != nil {
    35  		panic(err)
    36  	}
    37  
    38  	// note: users should not trust the the read content until
    39  	// Verify() returns nil.
    40  	if err := vr.Verify(); err != nil {
    41  		panic(err)
    42  	}
    43  
    44  	fmt.Println(buf)
    45  
    46  	// Output:
    47  	// hello world
    48  }