109 lines
3.9 KiB
Go
109 lines
3.9 KiB
Go
package objectstore
|
|
|
|
import (
|
|
"context"
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"errors"
|
|
"net/url"
|
|
"os"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
func TestStoreValidationAndOfflineSigning(t *testing.T) {
|
|
if _, err := New("", "localhost:59000", "key", "secret", "bucket", "us-east-1", false); err == nil {
|
|
t.Fatal("missing internal endpoint was accepted")
|
|
}
|
|
store, err := New("localhost:59000", "localhost:59000", "local_minio", "local_minio_password", "private-exports", "us-east-1", false)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
ctx := context.Background()
|
|
if _, err := store.PresignDownload(ctx, "synthetic/object.json", 0, "export.json"); err == nil {
|
|
t.Fatal("non-positive download TTL was accepted")
|
|
}
|
|
download, err := store.PresignDownload(ctx, "synthetic/object.json", time.Minute, "export.json")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
parsed, err := url.Parse(download)
|
|
if err != nil || parsed.Query().Get("X-Amz-Signature") == "" || parsed.Query().Get("response-content-disposition") != `attachment; filename="export.json"` {
|
|
t.Fatalf("download URL=%q parsed=%+v err=%v", download, parsed, err)
|
|
}
|
|
if _, err := store.PresignUpload(ctx, "synthetic/firmware.bin", 0); err == nil {
|
|
t.Fatal("non-positive upload TTL was accepted")
|
|
}
|
|
upload, err := store.PresignUpload(ctx, "synthetic/firmware.bin", time.Minute)
|
|
if err != nil || !strings.Contains(upload, "X-Amz-Signature=") {
|
|
t.Fatalf("upload URL=%q err=%v", upload, err)
|
|
}
|
|
mediaURL, err := store.PresignMediaDownload(ctx, "synthetic/audio.wav", time.Minute, "audio.wav", "audio/wav", true)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
parsed, _ = url.Parse(mediaURL)
|
|
if parsed.Query().Get("response-content-disposition") != `inline; filename="audio.wav"` || parsed.Query().Get("response-content-type") != "audio/wav" {
|
|
t.Fatalf("media URL=%q", mediaURL)
|
|
}
|
|
if _, err := store.PresignMediaDownload(ctx, "synthetic/audio.wav", -time.Second, "audio.wav", "audio/wav", false); err == nil {
|
|
t.Fatal("negative media TTL was accepted")
|
|
}
|
|
if _, err := store.ReadPrefix(ctx, "synthetic/audio.wav", 0); err == nil {
|
|
t.Fatal("invalid prefix limit was accepted")
|
|
}
|
|
if err := store.Remove(ctx, ""); err != nil {
|
|
t.Fatalf("empty remove: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestStoreMinIOObjectLifecycle(t *testing.T) {
|
|
endpoint := os.Getenv("TEST_OBJECT_STORE_ENDPOINT")
|
|
if endpoint == "" {
|
|
t.Skip("TEST_OBJECT_STORE_ENDPOINT is not set")
|
|
}
|
|
store, err := New(endpoint, endpoint, "local_minio", "local_minio_password", "private-exports", "us-east-1", false)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
ctx := context.Background()
|
|
if err := store.EnsureBucket(ctx); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := store.Ready(ctx); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
objectRef := "integration/objectstore/" + uuid.NewString() + ".json"
|
|
defer store.Remove(ctx, objectRef)
|
|
content := []byte(`{"synthetic":true,"message":"object lifecycle"}`)
|
|
digestBytes := sha256.Sum256(content)
|
|
digest := hex.EncodeToString(digestBytes[:])
|
|
size, err := store.PutJSON(ctx, objectRef, content, digest)
|
|
if err != nil || size != int64(len(content)) {
|
|
t.Fatalf("put size=%d err=%v", size, err)
|
|
}
|
|
info, err := store.Stat(ctx, objectRef)
|
|
if err != nil || info.Size != int64(len(content)) {
|
|
t.Fatalf("stat=%+v err=%v", info, err)
|
|
}
|
|
prefix, err := store.ReadPrefix(ctx, objectRef, 12)
|
|
if err != nil || string(prefix) != string(content[:12]) {
|
|
t.Fatalf("prefix=%q err=%v", prefix, err)
|
|
}
|
|
if err := store.VerifyDigest(ctx, objectRef, digest, int64(len(content)+1)); !errors.Is(err, ErrObjectSizeMismatch) {
|
|
t.Fatalf("size mismatch error=%v", err)
|
|
}
|
|
if err := store.VerifyDigest(ctx, objectRef, strings.Repeat("0", 64), int64(len(content))); !errors.Is(err, ErrObjectDigestMismatch) {
|
|
t.Fatalf("digest mismatch error=%v", err)
|
|
}
|
|
if err := store.VerifyDigest(ctx, objectRef, digest, int64(len(content))); err != nil {
|
|
t.Fatalf("verify object: %v", err)
|
|
}
|
|
if err := store.Remove(ctx, objectRef); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|