-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Postgres connector validates packet lengths
- Loading branch information
1 parent
06bb65a
commit 2a37666
Showing
3 changed files
with
73 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
internal/plugin/connectors/tcp/pg/protocol/protocol_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package protocol | ||
|
||
import ( | ||
"encoding/binary" | ||
"net" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestReadMessage(t *testing.T) { | ||
t.Run("parses contents", func(t *testing.T) { | ||
r, w := net.Pipe() | ||
expectedMessageType := byte(12) | ||
expectedMessage := []byte{0,1,2,3,4} | ||
|
||
go func() { | ||
err := binary.Write(w, binary.BigEndian, expectedMessageType) | ||
if err != nil { | ||
panic(err) | ||
} | ||
err = binary.Write(w, binary.BigEndian, int32(len(expectedMessage) + 4)) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
_, err = w.Write(expectedMessage) | ||
if err != nil { | ||
panic(err) | ||
} | ||
}() | ||
messageType, message, err := ReadMessage(r) | ||
|
||
if !assert.NoError(t, err) { | ||
return | ||
} | ||
|
||
assert.Equal(t, expectedMessage, message) | ||
assert.Equal(t, expectedMessageType, messageType) | ||
}) | ||
|
||
t.Run("validates message length", func(t *testing.T) { | ||
r, w := net.Pipe() | ||
expectedMessageType := byte(12) | ||
// a message length less than 4 is invalid | ||
expectedMessageLength := int32(3) | ||
|
||
go func() { | ||
err := binary.Write(w, binary.BigEndian, expectedMessageType) | ||
if err != nil { | ||
panic(err) | ||
} | ||
err = binary.Write(w, binary.BigEndian, expectedMessageLength) | ||
if err != nil { | ||
panic(err) | ||
} | ||
}() | ||
_, _, err := ReadMessage(r) | ||
if assert.Error(t, err) { | ||
return | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters