package main import ( "bytes" "context" "io" "net" "strings" "testing" ) type openerFunc func(context.Context, string) (io.ReadWriteCloser, error) func (f openerFunc) Open(ctx context.Context, channel string) (io.ReadWriteCloser, error) { return f(ctx, channel) } func TestRunRequiresConfirmationAndBounds(t *testing.T) { if code := run([]string{"-direction=tx", "-payload-base64=YQ=="}, io.Discard, io.Discard, nil); code != 2 { t.Fatalf("unconfirmed code = %d", code) } oversized := strings.Repeat("YQ==", 65) if code := run([]string{"-direction=tx", "-operator-confirmed-ch01-com3", "-payload-base64=" + oversized}, io.Discard, io.Discard, nil); code != 2 { t.Fatalf("oversized code = %d", code) } } func TestRunTXAndRXUseCanonicalStreamWithoutPrintingPayload(t *testing.T) { for _, direction := range []string{"tx", "rx"} { t.Run(direction, func(t *testing.T) { service, peer := net.Pipe() defer peer.Close() opener := openerFunc(func(_ context.Context, channel string) (io.ReadWriteCloser, error) { if channel != "CH01" { t.Fatalf("channel = %s", channel) } return service, nil }) payload := []byte("bounded-check") var stdout bytes.Buffer args := []string{"-direction=" + direction, "-operator-confirmed-ch01-com3"} if direction == "tx" { args = append(args, "-payload-base64=Ym91bmRlZC1jaGVjaw==") go func() { _, _ = io.ReadFull(peer, make([]byte, len(payload))) }() } else { args = append(args, "-bytes=13") go func() { _, _ = peer.Write(payload) }() } if code := run(args, &stdout, io.Discard, opener); code != 0 { t.Fatalf("code = %d", code) } if stdout.String() == "" || strings.Contains(stdout.String(), string(payload)) { t.Fatalf("unsafe report = %s", stdout.String()) } }) } }