forked from mtusi2026/mtusi2026lab4
131 lines
4.0 KiB
Go
131 lines
4.0 KiB
Go
package objectstore
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"errors"
|
|
"image"
|
|
"image/png"
|
|
"io"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"gitflic.ru/project/d-sotnikova5/mtusi2026lab1-fork/internal/upload"
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
func TestObjectKey(t *testing.T) {
|
|
for _, bad := range []string{"", "../x", "photo.png", "550E8400-E29B-41D4-A716-446655440000"} {
|
|
if _, err := ObjectKey(bad); !errors.Is(err, ErrInput) {
|
|
t.Fatalf("accepted key %q", bad)
|
|
}
|
|
}
|
|
id := uuid.NewString()
|
|
if key, err := ObjectKey(id); err != nil || key != "attachments/"+id {
|
|
t.Fatal("invalid generated key")
|
|
}
|
|
}
|
|
|
|
func TestInvalidConfig(t *testing.T) {
|
|
for _, endpoint := range []string{"", "ftp://s3:9000", "http://user:password@s3:9000", "http://s3:9000/path", "http://s3:9000?secret=hidden"} {
|
|
_, err := New(Config{Endpoint: endpoint, Region: "us-east-1", Bucket: "messenger-attachments"})
|
|
if !errors.Is(err, ErrConfig) {
|
|
t.Fatal("invalid config accepted")
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestS3Integration(t *testing.T) {
|
|
if os.Getenv("S3_INTEGRATION") != "1" {
|
|
t.Skip("explicit S3 integration mode required")
|
|
}
|
|
config := Config{
|
|
Endpoint: os.Getenv("S3_ENDPOINT"), Region: os.Getenv("S3_REGION"), Bucket: os.Getenv("S3_BUCKET"),
|
|
AccessKeyFile: os.Getenv("S3_ACCESS_KEY_ID_FILE"), SecretKeyFile: os.Getenv("S3_SECRET_ACCESS_KEY_FILE"),
|
|
}
|
|
client, err := New(config)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer client.Close()
|
|
ctx := context.Background()
|
|
id := uuid.NewString()
|
|
defer func() {
|
|
if err := client.Delete(context.Background(), id); err != nil {
|
|
t.Errorf("fixture cleanup: %v", err)
|
|
}
|
|
}()
|
|
var encoded bytes.Buffer
|
|
if err := png.Encode(&encoded, image.NewNRGBA(image.Rect(0, 0, 3, 2))); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
p, err := upload.NewValidator(t.TempDir()).Prepare(ctx, bytes.NewReader(encoded.Bytes()), "image/png", "original.png")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer p.Close()
|
|
if err := client.Put(ctx, id, p.File, p.SizeBytes, p.ContentType, p.SHA256); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Log("PASS: validated original uploaded with API credentials")
|
|
original, err := client.Open(ctx, id)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
data, readErr := io.ReadAll(original.Body)
|
|
closeErr := original.Body.Close()
|
|
if readErr != nil || closeErr != nil {
|
|
t.Fatalf("download: %v %v", readErr, closeErr)
|
|
}
|
|
sum := sha256.Sum256(data)
|
|
if !bytes.Equal(data, encoded.Bytes()) || original.SizeBytes != p.SizeBytes || original.ContentType != "image/png" || hex.EncodeToString(sum[:]) != p.SHA256 {
|
|
t.Fatal("original or metadata mismatch")
|
|
}
|
|
t.Log("PASS: downloaded original bytes, MIME, size and SHA-256 match")
|
|
if err := client.Delete(ctx, id); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := client.Open(ctx, id); !errors.Is(err, ErrNotFound) {
|
|
t.Fatalf("missing object: %v", err)
|
|
}
|
|
if err := client.Delete(ctx, id); err != nil {
|
|
t.Fatal("repeat delete failed")
|
|
}
|
|
t.Log("PASS: deleted object is not found; repeat delete succeeds")
|
|
cancelled, cancel := context.WithCancel(ctx)
|
|
cancel()
|
|
if _, err := client.Open(cancelled, id); !errors.Is(err, context.Canceled) {
|
|
t.Fatalf("cancellation: %v", err)
|
|
}
|
|
t.Log("PASS: caller cancellation propagated")
|
|
badSecret := filepath.Join(t.TempDir(), "secret")
|
|
if err := os.WriteFile(badSecret, []byte("deliberately-invalid-secret"), 0600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
wrongConfig := config
|
|
wrongConfig.SecretKeyFile = badSecret
|
|
wrongClient, err := New(wrongConfig)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer wrongClient.Close()
|
|
if _, err := wrongClient.Open(ctx, id); !errors.Is(err, ErrUnavailable) {
|
|
t.Fatalf("credential failure: %v", err)
|
|
}
|
|
t.Log("PASS: incorrect credentials yield sanitized unavailable error")
|
|
unavailableConfig := config
|
|
unavailableConfig.Endpoint = "http://127.0.0.1:1"
|
|
unavailableClient, err := New(unavailableConfig)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer unavailableClient.Close()
|
|
if _, err := unavailableClient.Open(ctx, id); !errors.Is(err, ErrUnavailable) {
|
|
t.Fatalf("unreachable endpoint: %v", err)
|
|
}
|
|
t.Log("PASS: unreachable endpoint yields sanitized unavailable error")
|
|
}
|