ssh/agent: fix TestMalformedRequests

Use a net.Pipe, rather than setting up a local TCP connection,
for testing malformed SSH requests, since we don't need the
complex intricacies of a real connection to test this protocol
behavior.

Fixes golang/go#50161

Change-Id: I5e46c42041ddd03b06725469216b82b801990e64
Reviewed-on: https://go-review.googlesource.com/c/crypto/+/371874
Reviewed-by: Bryan Mills <bcmills@google.com>
Trust: Roland Shoemaker <roland@golang.org>
Run-TryBot: Roland Shoemaker <roland@golang.org>
TryBot-Result: Gopher Robot <gobot@golang.org>
This commit is contained in:
Roland Shoemaker
2022-01-26 17:37:29 +00:00
parent 5e0467b6c7
commit e04a8579fe
+13 -32
View File
@@ -16,7 +16,6 @@ import (
"runtime" "runtime"
"strconv" "strconv"
"strings" "strings"
"sync"
"testing" "testing"
"time" "time"
@@ -204,44 +203,26 @@ func testAgentInterface(t *testing.T, agent ExtendedAgent, key interface{}, cert
func TestMalformedRequests(t *testing.T) { func TestMalformedRequests(t *testing.T) {
keyringAgent := NewKeyring() keyringAgent := NewKeyring()
listener, err := netListener()
if err != nil {
t.Fatalf("netListener: %v", err)
}
defer listener.Close()
testCase := func(t *testing.T, requestBytes []byte, wantServerErr bool) { testCase := func(t *testing.T, requestBytes []byte, wantServerErr bool) {
var wg sync.WaitGroup c, s := net.Pipe()
wg.Add(1) defer c.Close()
defer s.Close()
go func() { go func() {
defer wg.Done() _, err := c.Write(requestBytes)
c, err := listener.Accept()
if err != nil { if err != nil {
t.Errorf("listener.Accept: %v", err) t.Errorf("Unexpected error writing raw bytes on connection: %v", err)
return
}
defer c.Close()
err = ServeAgent(keyringAgent, c)
if err == nil {
t.Error("ServeAgent should have returned an error to malformed input")
} else {
if (err != io.EOF) != wantServerErr {
t.Errorf("ServeAgent returned expected error: %v", err)
}
} }
c.Close()
}() }()
err := ServeAgent(keyringAgent, s)
c, err := net.Dial("tcp", listener.Addr().String()) if err == nil {
if err != nil { t.Error("ServeAgent should have returned an error to malformed input")
t.Fatalf("net.Dial: %v", err) } else {
if (err != io.EOF) != wantServerErr {
t.Errorf("ServeAgent returned expected error: %v", err)
}
} }
_, err = c.Write(requestBytes)
if err != nil {
t.Errorf("Unexpected error writing raw bytes on connection: %v", err)
}
c.Close()
wg.Wait()
} }
var testCases = []struct { var testCases = []struct {