35 lines
1.5 KiB
Go
35 lines
1.5 KiB
Go
package rpc
|
|
|
|
import (
|
|
"testing"
|
|
"time"
|
|
|
|
agentv1 "git.ipao.vip/rogee/go-sip/gen/agent/v1"
|
|
"google.golang.org/grpc/codes"
|
|
"google.golang.org/grpc/status"
|
|
)
|
|
|
|
func TestCurrentSessionMetaUsesOnlyActivatedUnexpiredAgentBinding(t *testing.T) {
|
|
registry := NewSessionRegistry("")
|
|
now := time.Unix(100, 0).UTC()
|
|
if _, err := registry.CurrentMeta("agent-a", now); status.Code(err) != codes.Unauthenticated {
|
|
t.Fatalf("unactivated Agent fabricated a session: %v", err)
|
|
}
|
|
binding := &agentv1.AgentBinding{AgentId: "agent-a", CellId: "cell-a", ExpectedBootId: "boot-a", DispatcherEpoch: "epoch-a", SessionGeneration: 1}
|
|
session, _, err := registry.Activate(binding, "activate-a", "digest-a", now)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
meta, err := registry.CurrentMeta("agent-a", now.Add(time.Second))
|
|
if err != nil || meta.ProtocolVersion != "agent.v1" || meta.AgentId != binding.AgentId || meta.CellId != binding.CellId ||
|
|
meta.BootId != binding.ExpectedBootId || meta.DispatcherEpoch != binding.DispatcherEpoch || meta.SessionGeneration != session.SessionGeneration {
|
|
t.Fatalf("active session metadata differs from bound Agent: meta=%+v err=%v", meta, err)
|
|
}
|
|
if err := registry.Authorize(meta, now.Add(time.Second)); err != nil {
|
|
t.Fatalf("derived metadata is not authorized: %v", err)
|
|
}
|
|
if _, err := registry.CurrentMeta("agent-a", time.UnixMilli(session.ExpiresAtUnixMs)); status.Code(err) != codes.Unauthenticated {
|
|
t.Fatalf("expired session metadata was still returned: %v", err)
|
|
}
|
|
}
|