56 lines
2.4 KiB
Go
56 lines
2.4 KiB
Go
package signal
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"strings"
|
|
"testing"
|
|
|
|
core "git.yuntuo.hk/k2safe/orbit/model"
|
|
"git.yuntuo.hk/k2safe/orbit/platform/auth"
|
|
)
|
|
|
|
type captureStore struct {
|
|
calls int
|
|
input Input
|
|
}
|
|
|
|
func (s *captureStore) Send(_ context.Context, _ auth.Token, _ core.ID, _ string, in Input) (Result, error) {
|
|
s.calls++
|
|
s.input = in
|
|
return Result{}, nil
|
|
}
|
|
func (s *captureStore) Get(context.Context, auth.Token, core.ID, core.ID) (View, error) {
|
|
s.calls++
|
|
return View{}, nil
|
|
}
|
|
func TestSignalInputBoundary(t *testing.T) {
|
|
store := &captureStore{}
|
|
api := New(store)
|
|
id := core.ID("11111111111111111111111111111111")
|
|
base := Input{Name: "completed", PayloadVersion: "v1", TargetStepKey: "wait", TargetActivationSequence: 1, CorrelationKey: "关联 ", Payload: json.RawMessage(`{"n":9007199254740993}`), Reason: "verified source", EvidenceRef: "proof:record-1"}
|
|
for _, raw := range []string{`{"n":9007199254740993}`, `null`} {
|
|
in := base
|
|
in.Payload = json.RawMessage(raw)
|
|
if _, err := api.Send(context.Background(), auth.Token{}, id, "original", in); err != nil || string(store.input.Payload) != raw || store.input.CorrelationKey != base.CorrelationKey {
|
|
t.Fatal("signal input changed", err)
|
|
}
|
|
}
|
|
for _, change := range []func(*Input){
|
|
func(in *Input) { in.TargetActivationSequence = 0 }, func(in *Input) { in.CorrelationKey = "" }, func(in *Input) { in.CorrelationKey = strings.Repeat("中", 171) }, func(in *Input) { in.CorrelationKey = "x\x00y" },
|
|
func(in *Input) { in.Name = "a:b" }, func(in *Input) { in.Name = strings.Repeat("n", 129) }, func(in *Input) { in.PayloadVersion = "" }, func(in *Input) { in.TargetStepKey = "$root" },
|
|
func(in *Input) { in.Payload = nil }, func(in *Input) { in.Payload = json.RawMessage(`{"n":1,"n":2}`) }, func(in *Input) { in.Payload = json.RawMessage(`"\ud800"`) },
|
|
func(in *Input) { in.Reason = " " }, func(in *Input) { in.Reason = strings.Repeat("中", 1025) }, func(in *Input) { in.EvidenceRef = "proof:secret\nvalue" }, func(in *Input) { in.EvidenceRef = strings.Repeat("x", 513) },
|
|
} {
|
|
in := base
|
|
change(&in)
|
|
before := store.calls
|
|
if _, err := api.Send(context.Background(), auth.Token{}, id, "original", in); err == nil || before != store.calls {
|
|
t.Fatal("invalid signal reached store")
|
|
}
|
|
}
|
|
if _, err := api.Send(context.Background(), auth.Token{}, id, "", base); err == nil {
|
|
t.Fatal("missing identity accepted")
|
|
}
|
|
}
|