| 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 casscip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/internal/cas`/ |
| 17 | |
| 18 | import ( |
| 19 | "bytesscip-go gomod github.com/golang/go/src go1.23.0 bytes/" |
| 20 | "contextscip-go gomod github.com/golang/go/src go1.23.0 context/" |
| 21 | _ "crypto/sha256scip-go gomod github.com/golang/go/src go1.23.0 `crypto/sha256`/" |
| 22 | "errorsscip-go gomod github.com/golang/go/src go1.23.0 errors/" |
| 23 | "ioscip-go gomod github.com/golang/go/src go1.23.0 io/" |
| 24 | "stringsscip-go gomod github.com/golang/go/src go1.23.0 strings/" |
| 25 | "testingscip-go gomod github.com/golang/go/src go1.23.0 testing/" |
| 26 | |
| 27 | "github.com/opencontainers/go-digestscip-go gomod github.com/opencontainers/go-digest v1.0.0 `github.com/opencontainers/go-digest`/" |
| 28 | ocispecscip-go gomod github.com/opencontainers/image-spec v1.1.1 `github.com/opencontainers/image-spec/specs-go/v1`/ "github.com/opencontainers/image-spec/specs-go/v1scip-go gomod github.com/opencontainers/image-spec v1.1.1 `github.com/opencontainers/image-spec/specs-go/v1`/" |
| 29 | "oras.land/oras-go/v2/errdefscip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/errdef`/" |
| 30 | ) |
| 31 | |
| 32 | func TestMemorySuccessscip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/internal/cas`/TestMemorySuccess().(t *testingscip-go gomod github.com/golang/go/src go1.23.0 testing/.Tscip-go gomod github.com/golang/go/src go1.23.0 testing/T#) { |
| 33 | content := []byte("hello world") |
| 34 | desc := ocispecscip-go gomod github.com/opencontainers/image-spec v1.1.1 `github.com/opencontainers/image-spec/specs-go/v1`/.Descriptorscip-go gomod github.com/opencontainers/image-spec v1.1.1 `github.com/opencontainers/image-spec/specs-go/v1`/Descriptor#{ |
| 35 | MediaTypescip-go gomod github.com/opencontainers/image-spec v1.1.1 `github.com/opencontainers/image-spec/specs-go/v1`/Descriptor#MediaType.: "test", |
| 36 | Digestscip-go gomod github.com/opencontainers/image-spec v1.1.1 `github.com/opencontainers/image-spec/specs-go/v1`/Descriptor#Digest.: digestscip-go gomod github.com/opencontainers/go-digest v1.0.0 `github.com/opencontainers/go-digest`/.FromBytesscip-go gomod github.com/opencontainers/go-digest v1.0.0 `github.com/opencontainers/go-digest`/FromBytes().(content), |
| 37 | Sizescip-go gomod github.com/opencontainers/image-spec v1.1.1 `github.com/opencontainers/image-spec/specs-go/v1`/Descriptor#Size.: int64(len(content)), |
| 38 | } |
| 39 | |
| 40 | s := NewMemoryscip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/internal/cas`/NewMemory().() |
| 41 | ctx := contextscip-go gomod github.com/golang/go/src go1.23.0 context/.Backgroundscip-go gomod github.com/golang/go/src go1.23.0 context/Background().() |
| 42 | |
| 43 | err := s.Pushscip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/internal/cas`/Memory#Push().(ctx, desc, bytesscip-go gomod github.com/golang/go/src go1.23.0 bytes/.NewReaderscip-go gomod github.com/golang/go/src go1.23.0 bytes/NewReader().(content)) |
| 44 | if err != nil { |
| 45 | t.Fatalscip-go gomod github.com/golang/go/src go1.23.0 testing/common#Fatal().("Memory.Push() error =", err) |
| 46 | } |
| 47 | |
| 48 | exists, err := s.Existsscip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/internal/cas`/Memory#Exists().(ctx, desc) |
| 49 | if err != nil { |
| 50 | t.Fatalscip-go gomod github.com/golang/go/src go1.23.0 testing/common#Fatal().("Memory.Exists() error =", err) |
| 51 | } |
| 52 | if !exists { |
| 53 | t.Errorfscip-go gomod github.com/golang/go/src go1.23.0 testing/common#Errorf().("Memory.Exists() = %v, want %v", exists, true) |
| 54 | } |
| 55 | |
| 56 | rc, err := s.Fetchscip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/internal/cas`/Memory#Fetch().(ctx, desc) |
| 57 | if err != nil { |
| 58 | t.Fatalscip-go gomod github.com/golang/go/src go1.23.0 testing/common#Fatal().("Memory.Fetch() error =", err) |
| 59 | } |
| 60 | got, err := ioscip-go gomod github.com/golang/go/src go1.23.0 io/.ReadAllscip-go gomod github.com/golang/go/src go1.23.0 io/ReadAll().(rc) |
| 61 | if err != nil { |
| 62 | t.Fatalscip-go gomod github.com/golang/go/src go1.23.0 testing/common#Fatal().("Memory.Fetch().Read() error =", err) |
| 63 | } |
| 64 | err = rc.Closescip-go gomod github.com/golang/go/src go1.23.0 io/Closer#Close().() |
| 65 | if err != nil { |
| 66 | t.Errorscip-go gomod github.com/golang/go/src go1.23.0 testing/common#Error().("Memory.Fetch().Close() error =", err) |
| 67 | } |
| 68 | if !bytesscip-go gomod github.com/golang/go/src go1.23.0 bytes/.Equalscip-go gomod github.com/golang/go/src go1.23.0 bytes/Equal().(got, content) { |
| 69 | t.Errorfscip-go gomod github.com/golang/go/src go1.23.0 testing/common#Errorf().("Memory.Fetch() = %v, want %v", got, content) |
| 70 | } |
| 71 | if got := len(s.Mapscip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/internal/cas`/Memory#Map().()); got != 1 { |
| 72 | t.Errorfscip-go gomod github.com/golang/go/src go1.23.0 testing/common#Errorf().("Memory.Map() = %v, want %v", got, 1) |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | func TestMemoryNotFoundscip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/internal/cas`/TestMemoryNotFound().(t *testingscip-go gomod github.com/golang/go/src go1.23.0 testing/.Tscip-go gomod github.com/golang/go/src go1.23.0 testing/T#) { |
| 77 | content := []byte("hello world") |
| 78 | desc := ocispecscip-go gomod github.com/opencontainers/image-spec v1.1.1 `github.com/opencontainers/image-spec/specs-go/v1`/.Descriptorscip-go gomod github.com/opencontainers/image-spec v1.1.1 `github.com/opencontainers/image-spec/specs-go/v1`/Descriptor#{ |
| 79 | MediaTypescip-go gomod github.com/opencontainers/image-spec v1.1.1 `github.com/opencontainers/image-spec/specs-go/v1`/Descriptor#MediaType.: "test", |
| 80 | Digestscip-go gomod github.com/opencontainers/image-spec v1.1.1 `github.com/opencontainers/image-spec/specs-go/v1`/Descriptor#Digest.: digestscip-go gomod github.com/opencontainers/go-digest v1.0.0 `github.com/opencontainers/go-digest`/.FromBytesscip-go gomod github.com/opencontainers/go-digest v1.0.0 `github.com/opencontainers/go-digest`/FromBytes().(content), |
| 81 | Sizescip-go gomod github.com/opencontainers/image-spec v1.1.1 `github.com/opencontainers/image-spec/specs-go/v1`/Descriptor#Size.: int64(len(content)), |
| 82 | } |
| 83 | |
| 84 | s := NewMemoryscip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/internal/cas`/NewMemory().() |
| 85 | ctx := contextscip-go gomod github.com/golang/go/src go1.23.0 context/.Backgroundscip-go gomod github.com/golang/go/src go1.23.0 context/Background().() |
| 86 | |
| 87 | exists, err := s.Existsscip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/internal/cas`/Memory#Exists().(ctx, desc) |
| 88 | if err != nil { |
| 89 | t.Errorscip-go gomod github.com/golang/go/src go1.23.0 testing/common#Error().("Memory.Exists() error =", err) |
| 90 | } |
| 91 | if exists { |
| 92 | t.Errorfscip-go gomod github.com/golang/go/src go1.23.0 testing/common#Errorf().("Memory.Exists() = %v, want %v", exists, false) |
| 93 | } |
| 94 | |
| 95 | _, err = s.Fetchscip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/internal/cas`/Memory#Fetch().(ctx, desc) |
| 96 | if !errorsscip-go gomod github.com/golang/go/src go1.23.0 errors/.Isscip-go gomod github.com/golang/go/src go1.23.0 errors/Is().(err, errdefscip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/errdef`/.ErrNotFoundscip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/errdef`/ErrNotFound.) { |
| 97 | t.Errorfscip-go gomod github.com/golang/go/src go1.23.0 testing/common#Errorf().("Memory.Fetch() error = %v, want %v", err, errdefscip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/errdef`/.ErrNotFoundscip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/errdef`/ErrNotFound.) |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | func TestMemoryAlreadyExistsscip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/internal/cas`/TestMemoryAlreadyExists().(t *testingscip-go gomod github.com/golang/go/src go1.23.0 testing/.Tscip-go gomod github.com/golang/go/src go1.23.0 testing/T#) { |
| 102 | content := []byte("hello world") |
| 103 | desc := ocispecscip-go gomod github.com/opencontainers/image-spec v1.1.1 `github.com/opencontainers/image-spec/specs-go/v1`/.Descriptorscip-go gomod github.com/opencontainers/image-spec v1.1.1 `github.com/opencontainers/image-spec/specs-go/v1`/Descriptor#{ |
| 104 | MediaTypescip-go gomod github.com/opencontainers/image-spec v1.1.1 `github.com/opencontainers/image-spec/specs-go/v1`/Descriptor#MediaType.: "test", |
| 105 | Digestscip-go gomod github.com/opencontainers/image-spec v1.1.1 `github.com/opencontainers/image-spec/specs-go/v1`/Descriptor#Digest.: digestscip-go gomod github.com/opencontainers/go-digest v1.0.0 `github.com/opencontainers/go-digest`/.FromBytesscip-go gomod github.com/opencontainers/go-digest v1.0.0 `github.com/opencontainers/go-digest`/FromBytes().(content), |
| 106 | Sizescip-go gomod github.com/opencontainers/image-spec v1.1.1 `github.com/opencontainers/image-spec/specs-go/v1`/Descriptor#Size.: int64(len(content)), |
| 107 | } |
| 108 | |
| 109 | s := NewMemoryscip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/internal/cas`/NewMemory().() |
| 110 | ctx := contextscip-go gomod github.com/golang/go/src go1.23.0 context/.Backgroundscip-go gomod github.com/golang/go/src go1.23.0 context/Background().() |
| 111 | |
| 112 | err := s.Pushscip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/internal/cas`/Memory#Push().(ctx, desc, bytesscip-go gomod github.com/golang/go/src go1.23.0 bytes/.NewReaderscip-go gomod github.com/golang/go/src go1.23.0 bytes/NewReader().(content)) |
| 113 | if err != nil { |
| 114 | t.Fatalscip-go gomod github.com/golang/go/src go1.23.0 testing/common#Fatal().("Memory.Push() error =", err) |
| 115 | } |
| 116 | |
| 117 | err = s.Pushscip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/internal/cas`/Memory#Push().(ctx, desc, bytesscip-go gomod github.com/golang/go/src go1.23.0 bytes/.NewReaderscip-go gomod github.com/golang/go/src go1.23.0 bytes/NewReader().(content)) |
| 118 | if !errorsscip-go gomod github.com/golang/go/src go1.23.0 errors/.Isscip-go gomod github.com/golang/go/src go1.23.0 errors/Is().(err, errdefscip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/errdef`/.ErrAlreadyExistsscip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/errdef`/ErrAlreadyExists.) { |
| 119 | t.Errorfscip-go gomod github.com/golang/go/src go1.23.0 testing/common#Errorf().("Memory.Push() error = %v, want %v", err, errdefscip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/errdef`/.ErrAlreadyExistsscip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/errdef`/ErrAlreadyExists.) |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | func TestMemoryBadPushscip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/internal/cas`/TestMemoryBadPush().(t *testingscip-go gomod github.com/golang/go/src go1.23.0 testing/.Tscip-go gomod github.com/golang/go/src go1.23.0 testing/T#) { |
| 124 | content := []byte("hello world") |
| 125 | desc := ocispecscip-go gomod github.com/opencontainers/image-spec v1.1.1 `github.com/opencontainers/image-spec/specs-go/v1`/.Descriptorscip-go gomod github.com/opencontainers/image-spec v1.1.1 `github.com/opencontainers/image-spec/specs-go/v1`/Descriptor#{ |
| 126 | MediaTypescip-go gomod github.com/opencontainers/image-spec v1.1.1 `github.com/opencontainers/image-spec/specs-go/v1`/Descriptor#MediaType.: "test", |
| 127 | Digestscip-go gomod github.com/opencontainers/image-spec v1.1.1 `github.com/opencontainers/image-spec/specs-go/v1`/Descriptor#Digest.: digestscip-go gomod github.com/opencontainers/go-digest v1.0.0 `github.com/opencontainers/go-digest`/.FromBytesscip-go gomod github.com/opencontainers/go-digest v1.0.0 `github.com/opencontainers/go-digest`/FromBytes().(content), |
| 128 | Sizescip-go gomod github.com/opencontainers/image-spec v1.1.1 `github.com/opencontainers/image-spec/specs-go/v1`/Descriptor#Size.: int64(len(content)), |
| 129 | } |
| 130 | |
| 131 | s := NewMemoryscip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/internal/cas`/NewMemory().() |
| 132 | ctx := contextscip-go gomod github.com/golang/go/src go1.23.0 context/.Backgroundscip-go gomod github.com/golang/go/src go1.23.0 context/Background().() |
| 133 | |
| 134 | err := s.Pushscip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/internal/cas`/Memory#Push().(ctx, desc, stringsscip-go gomod github.com/golang/go/src go1.23.0 strings/.NewReaderscip-go gomod github.com/golang/go/src go1.23.0 strings/NewReader().("foobar")) |
| 135 | if err == nil { |
| 136 | t.Errorfscip-go gomod github.com/golang/go/src go1.23.0 testing/common#Errorf().("Memory.Push() error = %v, wantErr %v", err, true) |
| 137 | } |
| 138 | } |