A peer may hold 4 sync streams and open 1500 a minute; banned peers get none and cannot redial. A node keeps at most 100 unexpired listings per author (--max-per-author); updates, renewals and deletions of held ones still work. At most 16 inbound connections per public IP or IPv6 /64 (--max-conns-per-ip); local and private addresses are exempt. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AeFqV96dVeBX94inzcgZKw
75 lines
2.2 KiB
Go
75 lines
2.2 KiB
Go
package store
|
|
|
|
import (
|
|
"errors"
|
|
"testing"
|
|
"time"
|
|
|
|
"git.stinnesbeck.com/nils/allmende/internal/identity"
|
|
"git.stinnesbeck.com/nils/allmende/internal/listing"
|
|
)
|
|
|
|
func newQuotaEnv(t *testing.T, max int) *env {
|
|
t.Helper()
|
|
e := &env{now: t0}
|
|
e.root, _, _ = identity.NewRoot()
|
|
e.dev = newDevice(t, e.root)
|
|
s, err := Open(":memory:", Options{Difficulty: difficulty, MaxPerAuthor: max, Clock: func() time.Time { return e.now }})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Cleanup(func() { s.Close() })
|
|
e.s = s
|
|
return e
|
|
}
|
|
|
|
func TestAnAuthorCannotHoldMoreNewListingsThanTheQuota(t *testing.T) {
|
|
e := newQuotaEnv(t, 3)
|
|
var held []*listing.Listing
|
|
for i := 0; i < 3; i++ {
|
|
l := e.listing(t, nil)
|
|
e.put(t, l)
|
|
held = append(held, l)
|
|
}
|
|
if _, err := e.s.Put(ctx, e.listing(t, nil)); !errors.Is(err, ErrQuota) {
|
|
t.Fatalf("a fourth listing: err = %v, want ErrQuota", err)
|
|
}
|
|
// Another author is unaffected.
|
|
other := e.sibling(t)
|
|
other.root, _, _ = identity.NewRoot()
|
|
other.dev = newDevice(t, other.root)
|
|
other.s.maxPerAuthor = 3
|
|
other.put(t, other.listing(t, nil))
|
|
|
|
// What the node already holds can still be changed, renewed and deleted.
|
|
upd := e.listing(t, func(l *listing.Listing) { l.ID, l.Seq, l.Title = held[0].ID, 2, "Neu" })
|
|
if o := e.put(t, upd); o != Replaced {
|
|
t.Fatalf("update at the quota: %v, want replaced", o)
|
|
}
|
|
if o := e.put(t, e.tombstone(t, held[1])); o != Replaced {
|
|
t.Fatalf("deleting at the quota: %v, want replaced", o)
|
|
}
|
|
}
|
|
|
|
func TestExpiredListingsDoNotCountAgainstTheQuota(t *testing.T) {
|
|
e := newQuotaEnv(t, 2)
|
|
e.put(t, e.listing(t, nil))
|
|
e.put(t, e.listing(t, nil))
|
|
if _, err := e.s.Put(ctx, e.listing(t, nil)); !errors.Is(err, ErrQuota) {
|
|
t.Fatalf("err = %v, want ErrQuota", err)
|
|
}
|
|
e.now = e.now.Add(8 * 24 * time.Hour) // both have expired
|
|
if _, err := e.s.Put(ctx, e.listing(t, nil)); err != nil {
|
|
t.Fatalf("after the old ones expired: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestTheDefaultQuotaIsHundredAndNegativeMeansNone(t *testing.T) {
|
|
if e := newEnv(t); e.s.maxPerAuthor != DefaultMaxPerAuthor || DefaultMaxPerAuthor != 100 {
|
|
t.Fatalf("default quota %d", e.s.maxPerAuthor)
|
|
}
|
|
if e := newQuotaEnv(t, -1); e.s.maxPerAuthor != 0 {
|
|
t.Fatal("a negative quota must switch the cap off")
|
|
}
|
|
}
|