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