mirror of
https://github.com/shtorm-7/sing-box-extended.git
synced 2026-06-23 02:52:24 +03:00
Add udpnat test
This commit is contained in:
59
test/udpnat_test.go
Normal file
59
test/udpnat_test.go
Normal file
@@ -0,0 +1,59 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/sagernet/sing/common"
|
||||
"github.com/sagernet/sing/common/buf"
|
||||
E "github.com/sagernet/sing/common/exceptions"
|
||||
M "github.com/sagernet/sing/common/metadata"
|
||||
N "github.com/sagernet/sing/common/network"
|
||||
"github.com/sagernet/sing/common/udpnat"
|
||||
)
|
||||
|
||||
func TestUDPNatClose(t *testing.T) {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
connCtx, connCancel := common.ContextWithCancelCause(context.Background())
|
||||
defer connCancel(net.ErrClosed)
|
||||
service := udpnat.New[int](ctx, 1, &testUDPNatCloseHandler{connCancel})
|
||||
service.NewPacket(ctx, 0, buf.As([]byte("Hello")), M.Metadata{}, func(natConn N.PacketConn) N.PacketWriter {
|
||||
return &testPacketWriter{}
|
||||
})
|
||||
select {
|
||||
case <-connCtx.Done():
|
||||
if E.IsClosed(connCtx.Err()) {
|
||||
t.Fatal(E.New("conn closed unexpectedly: ", connCtx.Err()))
|
||||
}
|
||||
case <-time.After(2 * time.Second):
|
||||
t.Fatal("conn not closed")
|
||||
}
|
||||
}
|
||||
|
||||
type testUDPNatCloseHandler struct {
|
||||
done common.ContextCancelCauseFunc
|
||||
}
|
||||
|
||||
func (h *testUDPNatCloseHandler) NewPacketConnection(ctx context.Context, conn N.PacketConn, metadata M.Metadata) error {
|
||||
for {
|
||||
buffer := buf.NewPacket()
|
||||
_, err := conn.ReadPacket(buffer)
|
||||
buffer.Release()
|
||||
if err != nil {
|
||||
h.done(err)
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (h *testUDPNatCloseHandler) NewError(ctx context.Context, err error) {
|
||||
}
|
||||
|
||||
type testPacketWriter struct{}
|
||||
|
||||
func (t *testPacketWriter) WritePacket(buffer *buf.Buffer, destination M.Socksaddr) error {
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user