oras.land/oras-go/v2@v2.5.1-0.20240520045656-aef90e4d04c4/internal/ioutil/io_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 ioutil 17 18 import ( 19 "bytes" 20 "errors" 21 "io" 22 "os" 23 "reflect" 24 "testing" 25 26 ocispec "github.com/opencontainers/image-spec/specs-go/v1" 27 "oras.land/oras-go/v2/content" 28 ) 29 30 func TestUnwrapNopCloser(t *testing.T) { 31 var reader struct { 32 io.Reader 33 } 34 var readerWithWriterTo struct { 35 io.Reader 36 io.WriterTo 37 } 38 39 tests := []struct { 40 name string 41 rc io.Reader 42 want io.Reader 43 }{ 44 { 45 name: "nil", 46 }, 47 { 48 name: "no-op closer with plain io.Reader", 49 rc: io.NopCloser(reader), 50 want: reader, 51 }, 52 { 53 name: "no-op closer with io.WriteTo", 54 rc: io.NopCloser(readerWithWriterTo), 55 want: readerWithWriterTo, 56 }, 57 { 58 name: "any ReadCloser", 59 rc: os.Stdin, 60 want: os.Stdin, 61 }, 62 } 63 for _, tt := range tests { 64 t.Run(tt.name, func(t *testing.T) { 65 if got := UnwrapNopCloser(tt.rc); !reflect.DeepEqual(got, tt.want) { 66 t.Errorf("UnwrapNopCloser() = %v, want %v", got, tt.want) 67 } 68 }) 69 } 70 } 71 72 func TestCopyBuffer(t *testing.T) { 73 blob := []byte("foo") 74 type args struct { 75 src io.Reader 76 buf []byte 77 desc ocispec.Descriptor 78 } 79 tests := []struct { 80 name string 81 args args 82 wantDst string 83 wantErr error 84 }{ 85 { 86 name: "exact buffer size, no errors", 87 args: args{bytes.NewReader(blob), make([]byte, 3), content.NewDescriptorFromBytes("test", blob)}, 88 wantDst: "foo", 89 wantErr: nil, 90 }, 91 { 92 name: "small buffer size, no errors", 93 args: args{bytes.NewReader(blob), make([]byte, 1), content.NewDescriptorFromBytes("test", blob)}, 94 wantDst: "foo", 95 wantErr: nil, 96 }, 97 { 98 name: "big buffer size, no errors", 99 args: args{bytes.NewReader(blob), make([]byte, 5), content.NewDescriptorFromBytes("test", blob)}, 100 wantDst: "foo", 101 wantErr: nil, 102 }, 103 { 104 name: "wrong digest", 105 args: args{bytes.NewReader(blob), make([]byte, 3), content.NewDescriptorFromBytes("test", []byte("bar"))}, 106 wantDst: "foo", 107 wantErr: content.ErrMismatchedDigest, 108 }, 109 { 110 name: "wrong size, descriptor size is smaller", 111 args: args{bytes.NewReader(blob), make([]byte, 3), content.NewDescriptorFromBytes("test", []byte("fo"))}, 112 wantDst: "foo", 113 wantErr: content.ErrTrailingData, 114 }, 115 { 116 name: "wrong size, descriptor size is larger", 117 args: args{bytes.NewReader(blob), make([]byte, 3), content.NewDescriptorFromBytes("test", []byte("fooo"))}, 118 wantDst: "foo", 119 wantErr: io.ErrUnexpectedEOF, 120 }, 121 } 122 for _, tt := range tests { 123 t.Run(tt.name, func(t *testing.T) { 124 dst := &bytes.Buffer{} 125 err := CopyBuffer(dst, tt.args.src, tt.args.buf, tt.args.desc) 126 if !errors.Is(err, tt.wantErr) { 127 t.Errorf("CopyBuffer() error = %v, wantErr %v", err, tt.wantErr) 128 return 129 } 130 gotDst := dst.String() 131 if err == nil && gotDst != tt.wantDst { 132 t.Errorf("CopyBuffer() = %v, want %v", gotDst, tt.wantDst) 133 } 134 }) 135 } 136 }