forked from mholt/caddy-l4
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
dbc2da5
commit a3b42fc
Showing
2 changed files
with
169 additions
and
42 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
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,100 @@ | ||
package l4postgres | ||
|
||
import ( | ||
"bytes" | ||
"encoding/binary" | ||
"io" | ||
"net" | ||
"sync" | ||
"testing" | ||
|
||
"github.com/mholt/caddy-l4/layer4" | ||
"go.uber.org/zap" | ||
) | ||
|
||
// Example extends Message with utils to create example messages. | ||
// ref: https://github.com/rueian/pgbroker/blob/master/message/util.go | ||
type example struct { | ||
message | ||
} | ||
|
||
func (b *example) WriteByte(i byte) { | ||
b.data[b.offset] = i | ||
b.offset++ | ||
} | ||
|
||
func (b *example) WriteByteN(i []byte) { | ||
for _, s := range i { | ||
b.WriteByte(s) | ||
} | ||
} | ||
|
||
func (b *example) WriteUint32(i uint32) { | ||
binary.BigEndian.PutUint32(b.data[b.offset:b.offset+4], i) | ||
b.offset += 4 | ||
} | ||
|
||
func (b *example) WriteString(i string) { | ||
b.WriteByteN([]byte(i)) | ||
b.WriteByte(0) | ||
} | ||
|
||
var ExampleSSLRequest = func() []byte { | ||
x := &example{message: message{data: make([]byte, 8)}} | ||
x.WriteUint32(8) | ||
x.WriteUint32(sslRequestCode) | ||
return x.data | ||
} | ||
|
||
var ExampleStartupMessage = func() []byte { | ||
x := &example{message: message{data: make([]byte, 8)}} | ||
x.WriteUint32(8) | ||
x.WriteUint32(sslRequestCode) | ||
x.WriteString("user=postgres") | ||
x.offset = 0 | ||
return x.data | ||
} | ||
|
||
func assertNoError(t *testing.T, err error) { | ||
t.Helper() | ||
if err != nil { | ||
t.Fatalf("Unexpected error: %s\n", err) | ||
} | ||
} | ||
|
||
func closePipe(wg *sync.WaitGroup, c1 net.Conn, c2 net.Conn) { | ||
wg.Wait() | ||
_ = c1.Close() | ||
_ = c2.Close() | ||
} | ||
|
||
func TestPostgresSSLMatch(t *testing.T) { | ||
wg := &sync.WaitGroup{} | ||
in, out := net.Pipe() | ||
defer closePipe(wg, in, out) | ||
|
||
cx := layer4.WrapConnection(in, &bytes.Buffer{}, zap.NewNop()) | ||
|
||
x := ExampleSSLRequest() | ||
|
||
wg.Add(1) | ||
go func() { | ||
defer wg.Done() | ||
defer out.Close() | ||
_, err := out.Write(x) | ||
assertNoError(t, err) | ||
}() | ||
|
||
matcher := MatchPostgresSSL{ | ||
Required: true, | ||
} | ||
|
||
matched, err := matcher.Match(cx) | ||
assertNoError(t, err) | ||
|
||
if !matched { | ||
t.Fatalf("matcher did not match SSL") | ||
} | ||
|
||
_, _ = io.Copy(io.Discard, in) | ||
} |