package link import ( "context" "strings" "testing" "time" ) type roomBus struct { Bus limit int64 sent [][]byte } func (b *roomBus) MaxPayload() int64 { return b.limit } func (b *roomBus) PublishDeclaration(_ context.Context, _ string, body []byte) error { b.sent = append(b.sent, body) return nil } type fixedSigner struct{} func (fixedSigner) Sign(context.Context, []byte) ([]byte, error) { return []byte("sig"), nil } // A declaration is measured as it goes on the wire against the bus's own limit; one over it is refused saying size and // limit, and what the measure sees is told to whoever raises a condition (issue 1689). func TestADeclarationOverTheBusLimitIsRefusedSayingSizeAndLimit(t *testing.T) { var seen []int64 was := DeclarationMeasured DeclarationMeasured = func(_ context.Context, node string, size, limit int64) { if node != "novox" || limit != 400 { t.Errorf("measured %s against %d", node, limit) } seen = append(seen, size) } t.Cleanup(func() { DeclarationMeasured = was }) bus := &roomBus{limit: 400} small := []byte(`{"a":"` + strings.Repeat("x", 100) + `"}`) if err := Declare(t.Context(), bus, fixedSigner{}, "novox", small, time.Second); err != nil || len(bus.sent) != 1 { t.Fatalf("a small declaration: %v, %d sent", err, len(bus.sent)) } if int64(len(bus.sent[0])) != seen[0] { t.Fatalf("measured %d bytes, sent %d", seen[0], len(bus.sent[0])) } big := []byte(`{"a":"` + strings.Repeat("x", 300) + `"}`) err := Declare(t.Context(), bus, fixedSigner{}, "novox", big, time.Second) if err == nil || !strings.Contains(err.Error(), "400 bytes") || !strings.Contains(err.Error(), "novox") || !strings.Contains(err.Error(), "over") || strings.Contains(err.Error(), "maximum payload exceeded") { t.Fatalf("an oversize declaration: %v", err) } if len(bus.sent) != 1 || len(seen) != 2 || seen[1] <= 400 { t.Fatalf("it was sent (%d) or not measured (%v)", len(bus.sent), seen) } }