| 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 |
"context" |
| 21 |
"errors" |
| 22 |
"io" |
| 23 |
"reflect" |
| 24 |
"testing" |
| 25 |
|
| 26 |
"github.com/opencontainers/go-digest" |
| 27 |
ocispec "github.com/opencontainers/image-spec/specs-go/v1" |
| 28 |
"oras.land/oras-go/v2/content" |
| 29 |
"oras.land/oras-go/v2/errdef" |
| 30 |
"oras.land/oras-go/v2/internal/cas" |
| 31 |
) |
| 32 |
|
| 33 |
func TestLimitedStorage_Push(t *testing.T) { |
| 34 |
data := []byte("test") |
| 35 |
size := int64(len(data)) |
| 36 |
dgst := digest.FromBytes(data) |
| 37 |
mediaType := "application/vnd.test" |
| 38 |
|
| 39 |
tests := []struct { |
| 40 |
name string |
| 41 |
desc ocispec.Descriptor |
| 42 |
limit int64 |
| 43 |
wantErr error |
| 44 |
}{ |
| 45 |
{ |
| 46 |
name: "descriptor size matches actual size and is within limit", |
| 47 |
desc: ocispec.Descriptor{ |
| 48 |
MediaType: mediaType, |
| 49 |
Size: size, |
| 50 |
Digest: dgst, |
| 51 |
}, |
| 52 |
limit: size, |
| 53 |
wantErr: nil, |
| 54 |
}, |
| 55 |
{ |
| 56 |
name: "descriptor size matches actual size but exeeds limit", |
| 57 |
desc: ocispec.Descriptor{ |
| 58 |
MediaType: mediaType, |
| 59 |
Size: size, |
| 60 |
Digest: dgst, |
| 61 |
}, |
| 62 |
limit: size - 1, |
| 63 |
wantErr: errdef.ErrSizeExceedsLimit, |
| 64 |
}, |
| 65 |
{ |
| 66 |
name: "descriptor size mismatches actual size and is within limit", |
| 67 |
desc: ocispec.Descriptor{ |
| 68 |
MediaType: mediaType, |
| 69 |
Size: size - 1, |
| 70 |
Digest: dgst, |
| 71 |
}, |
| 72 |
limit: size, |
| 73 |
wantErr: content.ErrMismatchedDigest, |
| 74 |
}, |
| 75 |
{ |
| 76 |
name: "descriptor size mismatches actual size and exceeds limit", |
| 77 |
desc: ocispec.Descriptor{ |
| 78 |
MediaType: mediaType, |
| 79 |
Size: size + 1, |
| 80 |
Digest: dgst, |
| 81 |
}, |
| 82 |
limit: size, |
| 83 |
wantErr: errdef.ErrSizeExceedsLimit, |
| 84 |
}, |
| 85 |
} |
| 86 |
for _, tt := range tests { |
| 87 |
t.Run(tt.name, func(t *testing.T) { |
| 88 |
ctx := context.Background() |
| 89 |
ls := content.LimitStorage(cas.NewMemory(), tt.limit) |
| 90 |
|
| 91 |
// test Push |
| 92 |
err := ls.Push(ctx, tt.desc, bytes.NewReader(data)) |
| 93 |
if err != nil { |
| 94 |
if errors.Is(err, tt.wantErr) { |
| 95 |
return |
| 96 |
} |
| 97 |
t.Errorf("LimitedStorage.Push() error = %v, wantErr %v", err, tt.wantErr) |
| 98 |
} |
| 99 |
|
| 100 |
// verify |
| 101 |
rc, err := ls.Storage.Fetch(ctx, tt.desc) |
| 102 |
if err != nil { |
| 103 |
t.Fatalf("LimitedStorage.Fetch() error = %v", err) |
| 104 |
} |
| 105 |
defer rc.Close() |
| 106 |
|
| 107 |
got, err := io.ReadAll(rc) |
| 108 |
if err != nil { |
| 109 |
t.Fatalf("io.ReadAll() error = %v", err) |
| 110 |
} |
| 111 |
if !reflect.DeepEqual(got, data) { |
| 112 |
t.Errorf("LimitedStorage.Fetch() = %v, want %v", got, data) |
| 113 |
} |
| 114 |
}) |
| 115 |
} |
| 116 |
} |