57 lines
1.7 KiB
Go
57 lines
1.7 KiB
Go
package oss
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func signingFixture() Config {
|
|
return Config{Endpoint: "https://oss.example.invalid", Region: "cn-test", Bucket: "test-bucket", AccessKeyID: "local-test-key", AccessKeySecret: "local-test-secret", GrantTTL: 15 * time.Minute, MaxAssetBytes: 1024}
|
|
}
|
|
|
|
func TestGrantLifetimeIsFixed(t *testing.T) {
|
|
for _, ttl := range []time.Duration{-time.Second, time.Second, 14 * time.Minute, 16 * time.Minute, time.Hour} {
|
|
cfg := signingFixture()
|
|
cfg.GrantTTL = ttl
|
|
if _, err := NewClient(cfg); err == nil {
|
|
t.Fatalf("unsupported TTL accepted: %v", ttl)
|
|
}
|
|
}
|
|
for _, ttl := range []time.Duration{0, 15 * time.Minute} {
|
|
cfg := signingFixture()
|
|
cfg.GrantTTL = ttl
|
|
client, err := NewClient(cfg)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
now := time.Now().UTC()
|
|
grant, err := client.Grant(context.Background(), "upload-a", "recordings/a", strings.Repeat("a", 64), 100, now)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if grant.ExpiresAtUnixMs != now.Add(15*time.Minute).UnixMilli() {
|
|
t.Fatal("grant is not exactly 15 minutes")
|
|
}
|
|
if grant.MaxBytes != 100 {
|
|
t.Fatal("requested object size was changed")
|
|
}
|
|
if grant.GetBucket() != cfg.Bucket {
|
|
t.Fatalf("Agent was not given the Dispatcher-owned original OSS bucket: %q", grant.GetBucket())
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestGrantRejectsInvalidBoundsInsteadOfClamping(t *testing.T) {
|
|
client, err := NewClient(signingFixture())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, size := range []int64{-1, 0, 1025} {
|
|
if _, err := client.Grant(context.Background(), "upload-a", "recordings/a", strings.Repeat("a", 64), size, time.Now()); err == nil {
|
|
t.Fatalf("invalid size %d silently accepted", size)
|
|
}
|
|
}
|
|
}
|