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