-
Notifications
You must be signed in to change notification settings - Fork 771
Expand file tree
/
Copy pathsignatures_test.go
More file actions
145 lines (129 loc) · 4.46 KB
/
Copy pathsignatures_test.go
File metadata and controls
145 lines (129 loc) · 4.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
//
// Copyright 2021 The Sigstore Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package remote
import (
"errors"
"io"
"net/http"
"strings"
"testing"
"github.com/google/go-containerregistry/pkg/name"
v1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/google/go-containerregistry/pkg/v1/fake"
"github.com/google/go-containerregistry/pkg/v1/remote"
"github.com/google/go-containerregistry/pkg/v1/remote/transport"
"github.com/google/go-containerregistry/pkg/v1/types"
)
// oversizeLayer reports a huge Size() but its Uncompressed() must never be
// reached once Bundle() enforces the size cap.
type oversizeLayer struct {
size int64
}
func (l *oversizeLayer) Digest() (v1.Hash, error) { return v1.Hash{}, nil }
func (l *oversizeLayer) DiffID() (v1.Hash, error) { return v1.Hash{}, nil }
func (l *oversizeLayer) Size() (int64, error) { return l.size, nil }
func (l *oversizeLayer) MediaType() (types.MediaType, error) {
return "application/vnd.dev.sigstore.bundle+json", nil
}
func (l *oversizeLayer) Compressed() (io.ReadCloser, error) {
return nil, errors.New("Compressed should not be called")
}
func (l *oversizeLayer) Uncompressed() (io.ReadCloser, error) {
return nil, errors.New("Uncompressed should not be called once size is capped")
}
func TestBundleRejectsOversizeLayer(t *testing.T) {
ri := remote.Image
t.Cleanup(func() {
remoteImage = ri
})
remoteImage = func(_ name.Reference, _ ...remote.Option) (v1.Image, error) {
return &fake.FakeImage{
LayersStub: func() ([]v1.Layer, error) {
return []v1.Layer{&oversizeLayer{size: 200 * 1024 * 1024}}, nil
},
}, nil
}
_, err := Bundle(name.MustParseReference("gcr.io/distroless/static:sha256-deadbeef.sig"))
if err == nil {
t.Fatal("Bundle() = nil, wanted size limit error")
}
if !strings.Contains(err.Error(), "exceeded the limit") {
t.Fatalf("Bundle() = %v, wanted size limit error", err)
}
}
func TestSignaturesErrors(t *testing.T) {
ri := remote.Image
t.Cleanup(func() {
remoteImage = ri
})
t.Run("404 returns empty", func(t *testing.T) {
remoteImage = func(_ name.Reference, _ ...remote.Option) (v1.Image, error) {
return nil, &transport.Error{
StatusCode: http.StatusNotFound,
}
}
sigs, err := Signatures(name.MustParseReference("gcr.io/distroless/static:sha256-deadbeef.sig"))
if err != nil {
t.Fatalf("Signatures() = %v", err)
}
if sl, err := sigs.Get(); err != nil {
t.Fatalf("Get() = %v", err)
} else if len(sl) != 0 {
t.Fatalf("len(Get()) = %d, wanted 0", len(sl))
}
})
t.Run("other transport errors propagate", func(t *testing.T) {
want := &transport.Error{
StatusCode: http.StatusInternalServerError,
}
remoteImage = func(_ name.Reference, _ ...remote.Option) (v1.Image, error) {
return nil, want
}
_, err := Signatures(name.MustParseReference("gcr.io/distroless/static:sha256-deadbeef.sig"))
if !errors.Is(err, want) {
t.Fatalf("Signatures() = %v, wanted %v", err, want)
}
})
t.Run("other errors propagate", func(t *testing.T) {
want := errors.New("it's my error, I can cry if I want to")
remoteImage = func(_ name.Reference, _ ...remote.Option) (v1.Image, error) {
return nil, want
}
_, err := Signatures(name.MustParseReference("gcr.io/distroless/static:sha256-deadbeef.sig"))
if !errors.Is(err, want) {
t.Fatalf("Signatures() = %v, wanted %v", err, want)
}
})
t.Run("too many layers", func(t *testing.T) {
remoteImage = func(_ name.Reference, _ ...remote.Option) (v1.Image, error) {
return &fake.FakeImage{
ManifestStub: func() (*v1.Manifest, error) {
return &v1.Manifest{
Layers: make([]v1.Descriptor, 10000),
}, nil
},
}, nil
}
sigs, err := Signatures(name.MustParseReference("gcr.io/distroless/static:sha256-deadbeef.sig"))
if err != nil {
t.Fatalf("Signatures() = %v", err)
}
want := errors.New("number of layers (10000) exceeded the limit (1000)")
_, err = sigs.Get()
if err == nil || want.Error() != err.Error() {
t.Fatalf("Get() = %v", err)
}
})
}