feat: proxy download image
This commit is contained in:
@ -1,147 +1,69 @@
|
||||
// Copyright 2020 Google LLC All Rights Reserved.
|
||||
//
|
||||
// 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 verify
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"io"
|
||||
"strings"
|
||||
"nf-repo/internal/model"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
v1 "github.com/google/go-containerregistry/pkg/v1"
|
||||
)
|
||||
|
||||
func mustHash(s string, t *testing.T) v1.Hash {
|
||||
h, _, err := v1.SHA256(strings.NewReader(s))
|
||||
func TestVerify(t *testing.T) {
|
||||
bs := []byte(`{"architecture":"amd64","config":{"Hostname":"","Domainname":"","User":"","AttachStdin":false,"AttachStdout":false,"AttachStderr":false,"Tty":false,"OpenStdin":false,"StdinOnce":false,"Env":["PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"],"Cmd":["/bin/sh"],"Image":"sha256:9a5ce069f40cfe0f2270eafbff0a0f2fa08f1add73571af9f78209e96bb8a5e9","Volumes":null,"WorkingDir":"","Entrypoint":null,"OnBuild":null,"Labels":null},"container":"4189cbc534955765760c227f328ec1cdd52e8550681c2bf9f8f990b27b644f9c","container_config":{"Hostname":"4189cbc53495","Domainname":"","User":"","AttachStdin":false,"AttachStdout":false,"AttachStderr":false,"Tty":false,"OpenStdin":false,"StdinOnce":false,"Env":["PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"],"Cmd":["/bin/sh","-c","#(nop) ","CMD [\"/bin/sh\"]"],"Image":"sha256:9a5ce069f40cfe0f2270eafbff0a0f2fa08f1add73571af9f78209e96bb8a5e9","Volumes":null,"WorkingDir":"","Entrypoint":null,"OnBuild":null,"Labels":{}},"created":"2024-01-27T00:30:48.743965523Z","docker_version":"20.10.23","history":[{"created":"2024-01-27T00:30:48.624602109Z","created_by":"/bin/sh -c #(nop) ADD file:37a76ec18f9887751cd8473744917d08b7431fc4085097bb6a09d81b41775473 in / "},{"created":"2024-01-27T00:30:48.743965523Z","created_by":"/bin/sh -c #(nop) CMD [\"/bin/sh\"]","empty_layer":true}],"os":"linux","rootfs":{"type":"layers","diff_ids":["sha256:d4fc045c9e3a848011de66f34b81f052d4f2c15a17bb196d637e526349601820"]}}`)
|
||||
//hash, _ := model.NewHash("sha256:05455a08881ea9cf0e752bc48e61bbd71a34c029bb13df01e40e3e70e0d007bd")
|
||||
badHash, _ := model.NewHash("sha256:12455a08881ea9cf0e7f2bc48e61bbd71a34c029bb13df01e40e3e70e0d007bd")
|
||||
nrc, err := ReadCloser(io.NopCloser(bytes.NewReader(bs)), -1, badHash)
|
||||
if err != nil {
|
||||
t.Fatalf("v1.SHA256(%s) = %v", s, err)
|
||||
t.Error(1, err)
|
||||
return
|
||||
}
|
||||
t.Logf("Hashed: %q -> %q", s, h)
|
||||
return h
|
||||
}
|
||||
|
||||
func TestVerificationFailure(t *testing.T) {
|
||||
want := "This is the input string."
|
||||
buf := bytes.NewBufferString(want)
|
||||
|
||||
verified, err := ReadCloser(io.NopCloser(buf), int64(len(want)), mustHash("not the same", t))
|
||||
nbs, err := io.ReadAll(nrc)
|
||||
if err != nil {
|
||||
t.Fatal("ReadCloser() =", err)
|
||||
t.Error(2, err)
|
||||
}
|
||||
if b, err := io.ReadAll(verified); err == nil {
|
||||
t.Errorf("ReadAll() = %q; want verification error", string(b))
|
||||
|
||||
if string(nbs) != string(bs) {
|
||||
t.Error(3, "not same")
|
||||
}
|
||||
|
||||
t.Log("new bytes:", string(nbs))
|
||||
}
|
||||
|
||||
func TestVerification(t *testing.T) {
|
||||
want := "This is the input string."
|
||||
buf := bytes.NewBufferString(want)
|
||||
|
||||
verified, err := ReadCloser(io.NopCloser(buf), int64(len(want)), mustHash(want, t))
|
||||
func TestVerify2(t *testing.T) {
|
||||
name := "4a666f159bd07f62aa19ce8eceac018931223de67c4bd19ded87b83eb7b103ca"
|
||||
f, err := os.Open(name)
|
||||
if err != nil {
|
||||
t.Fatal("ReadCloser() =", err)
|
||||
t.Error(1, err)
|
||||
return
|
||||
}
|
||||
if _, err := io.ReadAll(verified); err != nil {
|
||||
t.Error("ReadAll() =", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestVerificationSizeUnknown(t *testing.T) {
|
||||
want := "This is the input string."
|
||||
buf := bytes.NewBufferString(want)
|
||||
|
||||
verified, err := ReadCloser(io.NopCloser(buf), SizeUnknown, mustHash(want, t))
|
||||
bs, err := io.ReadAll(f)
|
||||
if err != nil {
|
||||
t.Fatal("ReadCloser() =", err)
|
||||
t.Error(2, err)
|
||||
}
|
||||
if _, err := io.ReadAll(verified); err != nil {
|
||||
t.Error("ReadAll() =", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBadHash(t *testing.T) {
|
||||
h := v1.Hash{
|
||||
Algorithm: "fake256",
|
||||
Hex: "whatever",
|
||||
}
|
||||
_, err := ReadCloser(io.NopCloser(strings.NewReader("hi")), 0, h)
|
||||
if err == nil {
|
||||
t.Errorf("ReadCloser() = %v, wanted err", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBadSize(t *testing.T) {
|
||||
want := "This is the input string."
|
||||
|
||||
// having too much content or expecting too much content returns an error.
|
||||
for _, size := range []int64{3, 100} {
|
||||
t.Run(fmt.Sprintf("expecting size %d", size), func(t *testing.T) {
|
||||
buf := bytes.NewBufferString(want)
|
||||
rc, err := ReadCloser(io.NopCloser(buf), size, mustHash(want, t))
|
||||
if err != nil {
|
||||
t.Fatal("ReadCloser() =", err)
|
||||
}
|
||||
if b, err := io.ReadAll(rc); err == nil {
|
||||
t.Errorf("ReadAll() = %q; want verification error", string(b))
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestDescriptor(t *testing.T) {
|
||||
for _, tc := range []struct {
|
||||
err error
|
||||
desc v1.Descriptor
|
||||
}{{
|
||||
err: errors.New("error verifying descriptor; Data == nil"),
|
||||
}, {
|
||||
err: errors.New(`error verifying Digest; got "sha256:ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad", want ":"`),
|
||||
desc: v1.Descriptor{
|
||||
Data: []byte("abc"),
|
||||
},
|
||||
}, {
|
||||
err: errors.New("error verifying Size; got 3, want 0"),
|
||||
desc: v1.Descriptor{
|
||||
Data: []byte("abc"),
|
||||
Digest: v1.Hash{
|
||||
Algorithm: "sha256",
|
||||
Hex: "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad",
|
||||
},
|
||||
},
|
||||
}, {
|
||||
desc: v1.Descriptor{
|
||||
Data: []byte("abc"),
|
||||
Size: 3,
|
||||
Digest: v1.Hash{
|
||||
Algorithm: "sha256",
|
||||
Hex: "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad",
|
||||
},
|
||||
},
|
||||
}} {
|
||||
got, want := Descriptor(tc.desc), tc.err
|
||||
|
||||
if got == nil {
|
||||
if want != nil {
|
||||
t.Errorf("Descriptor(): got nil, want %v", want)
|
||||
}
|
||||
} else if want == nil {
|
||||
t.Errorf("Descriptor(): got %v, want nil", got)
|
||||
} else if got, want := got.Error(), want.Error(); got != want {
|
||||
t.Errorf("Descriptor(): got %q, want %q", got, want)
|
||||
}
|
||||
|
||||
shaer := sha256.New()
|
||||
shaer.Write(bs)
|
||||
sum := hex.EncodeToString(shaer.Sum(nil))
|
||||
|
||||
r, err := ReadCloser(io.NopCloser(bytes.NewReader(bs)), int64(len(bs)), model.Hash{Algorithm: "sha256", Hex: name})
|
||||
if err != nil {
|
||||
t.Error(4, err)
|
||||
return
|
||||
}
|
||||
|
||||
if _, err = io.ReadAll(r); err != nil {
|
||||
t.Error(5, err)
|
||||
return
|
||||
}
|
||||
|
||||
if sum != name {
|
||||
t.Error(3, fmt.Sprintf("want: %s got: %s", name, sum))
|
||||
}
|
||||
|
||||
_ = r
|
||||
}
|
||||
|
Reference in New Issue
Block a user