Migrating hosting from Codeberg to git.kmalloc.org. Renames the module path in go.mod, every internal import, and the OCI image source label in the Dockerfile. Build and full test suite verified green after the rename.
251 lines
9.5 KiB
Go
251 lines
9.5 KiB
Go
package storage
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"crypto/ed25519"
|
|
"errors"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/uptrace/bun"
|
|
|
|
"git.kmalloc.org/squid/sonda/internal/federation"
|
|
)
|
|
|
|
func testKey(t *testing.T, seed byte) ed25519.PublicKey {
|
|
t.Helper()
|
|
pub, _, err := ed25519.GenerateKey(bytes.NewReader(bytes.Repeat([]byte{seed}, 64)))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return pub
|
|
}
|
|
|
|
func TestFederationSettings(t *testing.T) {
|
|
forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) {
|
|
ctx := context.Background()
|
|
repo := NewFederationRepo(db)
|
|
|
|
if _, found, err := repo.Settings(ctx); err != nil || found {
|
|
t.Fatalf("Settings before any save = found %v, %v", found, err)
|
|
}
|
|
|
|
pub, priv, err := ed25519.GenerateKey(bytes.NewReader(bytes.Repeat([]byte{7}, 64)))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
want := federation.Settings{
|
|
Enabled: true, Name: "Home", Address: "https://music.example.org",
|
|
PublicKey: pub, PrivateKey: priv, UpdatedAt: time.Unix(1_700_000_000, 0),
|
|
}
|
|
if err := repo.SaveSettings(ctx, want); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
// A second save replaces the row, it does not add one.
|
|
want.Name = "Away"
|
|
want.Enabled = false
|
|
if err := repo.SaveSettings(ctx, want); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
got, found, err := repo.Settings(ctx)
|
|
if err != nil || !found {
|
|
t.Fatalf("Settings = found %v, %v", found, err)
|
|
}
|
|
if got.Enabled || got.Name != "Away" || got.Address != want.Address ||
|
|
!bytes.Equal(got.PublicKey, pub) || !bytes.Equal(got.PrivateKey, priv) || !got.UpdatedAt.Equal(want.UpdatedAt) {
|
|
t.Errorf("Settings = %+v, want %+v", got, want)
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestFederationSettingsWithoutKeys(t *testing.T) {
|
|
forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) {
|
|
ctx := context.Background()
|
|
repo := NewFederationRepo(db)
|
|
if err := repo.SaveSettings(ctx, federation.Settings{Name: "x", UpdatedAt: time.Unix(5, 0)}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
got, found, err := repo.Settings(ctx)
|
|
if err != nil || !found || got.PublicKey != nil || got.PrivateKey != nil {
|
|
t.Fatalf("Settings = %+v, found %v, %v: a server with no keys yet must read back with none", got, found, err)
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestFederationInvitesAreSingleUseAndExpire(t *testing.T) {
|
|
forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) {
|
|
ctx := context.Background()
|
|
repo := NewFederationRepo(db)
|
|
now := time.Unix(1_700_000_000, 0)
|
|
|
|
if err := repo.AddInvite(ctx, "hash-a", now, now.Add(time.Hour)); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if ok, err := repo.UseInvite(ctx, "no-such", now); err != nil || ok {
|
|
t.Errorf("unknown invite: ok = %v, %v", ok, err)
|
|
}
|
|
if ok, err := repo.UseInvite(ctx, "hash-a", now.Add(2*time.Hour)); err != nil || ok {
|
|
t.Errorf("expired invite: ok = %v, %v", ok, err)
|
|
}
|
|
if ok, err := repo.UseInvite(ctx, "hash-a", now.Add(time.Minute)); err != nil || !ok {
|
|
t.Errorf("first use: ok = %v, %v", ok, err)
|
|
}
|
|
if ok, err := repo.UseInvite(ctx, "hash-a", now.Add(time.Minute)); err != nil || ok {
|
|
t.Errorf("second use: ok = %v, %v, an invite is good once", ok, err)
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestFederationFriends(t *testing.T) {
|
|
forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) {
|
|
ctx := context.Background()
|
|
repo := NewFederationRepo(db)
|
|
now := time.Unix(1_700_000_000, 0)
|
|
|
|
if list, err := repo.Friends(ctx); err != nil || len(list) != 0 {
|
|
t.Fatalf("Friends = %v, %v", list, err)
|
|
}
|
|
a, err := repo.AddFriend(ctx, federation.Friend{
|
|
Name: "Anna", Address: "https://anna.example.org", PublicKey: testKey(t, 1), State: federation.Incoming, CreatedAt: now,
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if a.ID <= 0 {
|
|
t.Fatalf("AddFriend returned %+v", a)
|
|
}
|
|
if _, err := repo.AddFriend(ctx, federation.Friend{
|
|
Name: "Again", Address: "https://x.example.org", PublicKey: testKey(t, 1), State: federation.Outgoing, CreatedAt: now,
|
|
}); !errors.Is(err, federation.ErrAlreadyFriend) {
|
|
t.Errorf("same key twice: err = %v, want ErrAlreadyFriend", err)
|
|
}
|
|
b, err := repo.AddFriend(ctx, federation.Friend{
|
|
Name: "Bruno", Address: "https://bruno.example.org", PublicKey: testKey(t, 2), State: federation.Outgoing, CreatedAt: now.Add(time.Second),
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
list, err := repo.Friends(ctx)
|
|
if err != nil || len(list) != 2 || list[0].ID != a.ID || list[1].ID != b.ID {
|
|
t.Fatalf("Friends = %+v, %v, want Anna then Bruno", list, err)
|
|
}
|
|
got, err := repo.Friend(ctx, a.ID)
|
|
if err != nil || got.Name != "Anna" || got.State != federation.Incoming || !got.ApprovedAt.IsZero() ||
|
|
!bytes.Equal(got.PublicKey, testKey(t, 1)) || !got.CreatedAt.Equal(now) {
|
|
t.Errorf("Friend = %+v, %v", got, err)
|
|
}
|
|
byKey, found, err := repo.FriendByKey(ctx, testKey(t, 2))
|
|
if err != nil || !found || byKey.ID != b.ID {
|
|
t.Errorf("FriendByKey = %+v, %v, %v", byKey, found, err)
|
|
}
|
|
if _, found, err := repo.FriendByKey(ctx, testKey(t, 9)); err != nil || found {
|
|
t.Errorf("FriendByKey unknown = %v, %v", found, err)
|
|
}
|
|
|
|
// Approving moves an incoming friend to active; the other one is not waiting for me.
|
|
if err := repo.Approve(ctx, a.ID, now.Add(time.Hour)); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got, _ := repo.Friend(ctx, a.ID); got.State != federation.Active || !got.ApprovedAt.Equal(now.Add(time.Hour)) {
|
|
t.Errorf("after Approve: %+v", got)
|
|
}
|
|
if err := repo.Approve(ctx, a.ID, now); !errors.Is(err, federation.ErrWrongState) {
|
|
t.Errorf("approve an active friend: %v, want ErrWrongState", err)
|
|
}
|
|
if err := repo.Approve(ctx, b.ID, now); !errors.Is(err, federation.ErrWrongState) {
|
|
t.Errorf("approve an outgoing friend: %v, want ErrWrongState", err)
|
|
}
|
|
if err := repo.Approve(ctx, 999, now); !errors.Is(err, federation.ErrFriendMissing) {
|
|
t.Errorf("approve missing: %v, want ErrFriendMissing", err)
|
|
}
|
|
|
|
if err := repo.DeleteFriend(ctx, a.ID); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := repo.Friend(ctx, a.ID); !errors.Is(err, federation.ErrFriendMissing) {
|
|
t.Errorf("Friend after delete: %v", err)
|
|
}
|
|
if err := repo.DeleteFriend(ctx, a.ID); !errors.Is(err, federation.ErrFriendMissing) {
|
|
t.Errorf("delete twice: %v, want ErrFriendMissing", err)
|
|
}
|
|
// The key is free again: the same server can be invited anew.
|
|
if _, err := repo.AddFriend(ctx, federation.Friend{
|
|
Name: "Anna", Address: "https://anna.example.org", PublicKey: testKey(t, 1), State: federation.Incoming, CreatedAt: now,
|
|
}); err != nil {
|
|
t.Errorf("add after delete: %v", err)
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestFederationLimitsAreStored(t *testing.T) {
|
|
forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) {
|
|
ctx := context.Background()
|
|
repo := NewFederationRepo(db)
|
|
if err := repo.SaveSettings(ctx, federation.Settings{Enabled: true, MaxStreams: 5, MaxKiBPerSec: 0, UpdatedAt: time.Unix(5, 0)}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
got, _, err := repo.Settings(ctx)
|
|
if err != nil || got.MaxStreams != 5 || got.MaxKiBPerSec != 0 {
|
|
t.Fatalf("Settings = %+v, %v: 0 KiB a second means no limit and must stay 0", got, err)
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestAnOutgoingFriendBecomesActive(t *testing.T) {
|
|
forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) {
|
|
ctx := context.Background()
|
|
repo := NewFederationRepo(db)
|
|
now := time.Unix(1_700_000_000, 0)
|
|
out, _ := repo.AddFriend(ctx, federation.Friend{Name: "A", Address: "https://a.example.org", PublicKey: testKey(t, 1), State: federation.Outgoing, CreatedAt: now})
|
|
in, _ := repo.AddFriend(ctx, federation.Friend{Name: "B", Address: "https://b.example.org", PublicKey: testKey(t, 2), State: federation.Incoming, CreatedAt: now})
|
|
|
|
if err := repo.Activate(ctx, in.ID, now); !errors.Is(err, federation.ErrWrongState) {
|
|
t.Errorf("activate an incoming friend: %v, want ErrWrongState", err)
|
|
}
|
|
if err := repo.Activate(ctx, 999, now); !errors.Is(err, federation.ErrFriendMissing) {
|
|
t.Errorf("activate a missing friend: %v, want ErrFriendMissing", err)
|
|
}
|
|
if err := repo.Activate(ctx, out.ID, now.Add(time.Hour)); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got, _ := repo.Friend(ctx, out.ID); got.State != federation.Active || !got.ApprovedAt.Equal(now.Add(time.Hour)) {
|
|
t.Errorf("after Activate: %+v", got)
|
|
}
|
|
if err := repo.Activate(ctx, out.ID, now); !errors.Is(err, federation.ErrWrongState) {
|
|
t.Errorf("activate twice: %v, want ErrWrongState", err)
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestTheRequestsOfFriendsAreLoggedListedAndPruned(t *testing.T) {
|
|
forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) {
|
|
ctx := context.Background()
|
|
repo := NewFederationRepo(db)
|
|
now := time.Unix(1_700_000_000, 0)
|
|
f, _ := repo.AddFriend(ctx, federation.Friend{Name: "Anna", Address: "https://a.example.org", PublicKey: testKey(t, 1), State: federation.Active, CreatedAt: now})
|
|
for i, at := range []time.Time{now, now.Add(time.Hour), now.Add(2 * time.Hour)} {
|
|
if err := repo.LogRequest(ctx, federation.FriendRequest{At: at, FriendID: f.ID, Route: "/api/v1/peer/tracks/:id/stream", Status: 200 + i, Bytes: int64(i)}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
got, err := repo.Requests(ctx, 2)
|
|
if err != nil || len(got) != 2 || got[0].Status != 202 || got[1].Status != 201 || got[0].FriendName != "Anna" || got[0].Route == "" {
|
|
t.Fatalf("Requests(2) = %+v, %v: want the newest two, with the name", got, err)
|
|
}
|
|
if err := repo.PruneRequests(ctx, now.Add(30*time.Minute)); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got, _ := repo.Requests(ctx, 10); len(got) != 2 {
|
|
t.Errorf("after pruning: %d requests, want 2", len(got))
|
|
}
|
|
// A friend that goes takes its log with it.
|
|
if err := repo.DeleteFriend(ctx, f.ID); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got, _ := repo.Requests(ctx, 10); len(got) != 0 {
|
|
t.Errorf("after the friend was removed: %d requests, want 0", len(got))
|
|
}
|
|
})
|
|
}
|