diff --git a/plugin/client_test.go b/plugin/client_test.go index 3fc8482..5914b5c 100644 --- a/plugin/client_test.go +++ b/plugin/client_test.go @@ -7,6 +7,7 @@ package plugin import ( + "bufio" "bytes" "errors" "io" @@ -19,6 +20,7 @@ import ( "filippo.io/age" "filippo.io/age/internal/bech32" + "filippo.io/age/internal/format" ) func TestMain(m *testing.M) { @@ -104,6 +106,22 @@ func TestPluginNameCase(t *testing.T) { } } +func TestRequestValueEmpty(t *testing.T) { + p, err := New("test") + if err != nil { + t.Fatal(err) + } + p.SetIO(nil, io.Discard, io.Discard) + p.sr = format.NewStanzaReader(bufio.NewReader(strings.NewReader("-> ok\n\n"))) + value, err := p.RequestValue("prompt", true) + if err != nil { + t.Fatal(err) + } + if value != "" { + t.Errorf("value = %q", value) + } +} + func TestLabels(t *testing.T) { if runtime.GOOS == "windows" { t.Skip("Windows support is TODO") diff --git a/plugin/plugin.go b/plugin/plugin.go index c9d28fe..a5ce595 100644 --- a/plugin/plugin.go +++ b/plugin/plugin.go @@ -537,7 +537,7 @@ func (p *Plugin) RequestValue(prompt string, secret bool) (string, error) { if s.Type == "fail" { return "", fmt.Errorf("client failed to request value") } - if err := expectStanzaWithBody(s, 0); err != nil { + if err := expectStanzaWithAnyBody(s, 0); err != nil { return "", p.fatalInteractf("%v", err) } return string(s.Body), nil @@ -596,8 +596,8 @@ func expectStanzaWithNoBody(s *format.Stanza, wantArgs int) error { } func expectStanzaWithBody(s *format.Stanza, wantArgs int) error { - if len(s.Args) != wantArgs { - return fmt.Errorf("%s stanza has %d arguments, want %d", s.Type, len(s.Args), wantArgs) + if err := expectStanzaWithAnyBody(s, wantArgs); err != nil { + return err } if len(s.Body) == 0 { return fmt.Errorf("%s stanza has 0 bytes of body, want >0", s.Type) @@ -605,6 +605,13 @@ func expectStanzaWithBody(s *format.Stanza, wantArgs int) error { return nil } +func expectStanzaWithAnyBody(s *format.Stanza, wantArgs int) error { + if len(s.Args) != wantArgs { + return fmt.Errorf("%s stanza has %d arguments, want %d", s.Type, len(s.Args), wantArgs) + } + return nil +} + func (p *Plugin) recipientError(idx int, err error) int { if err := p.writeError([]string{"recipient", fmt.Sprint(idx)}, err); err != nil { return p.fatalf("%v", err)