mirror of
https://github.com/cedar2025/hysteria.git
synced 2025-09-21 23:46:02 +00:00
feat(wip): udp rework server side
This commit is contained in:
@@ -315,179 +315,3 @@ func TestWriteTCPResponse(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestReadUDPRequest(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
data []byte
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "normal no padding",
|
||||
data: []byte("\x00\x00"),
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "normal with padding",
|
||||
data: []byte("\x02gg"),
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "incomplete 1",
|
||||
data: []byte("\x0bhoho"),
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
r := bytes.NewReader(tt.data)
|
||||
if err := ReadUDPRequest(r); (err != nil) != tt.wantErr {
|
||||
t.Errorf("ReadUDPRequest() error = %v, wantErr %v", err, tt.wantErr)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestWriteUDPRequest(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
wantW string // Just a prefix, we don't care about the padding
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "normal",
|
||||
wantW: "\x44\x02",
|
||||
wantErr: false,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
w := &bytes.Buffer{}
|
||||
err := WriteUDPRequest(w)
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Errorf("WriteUDPRequest() error = %v, wantErr %v", err, tt.wantErr)
|
||||
return
|
||||
}
|
||||
if gotW := w.String(); !(strings.HasPrefix(gotW, tt.wantW) && len(gotW) > len(tt.wantW)) {
|
||||
t.Errorf("WriteUDPRequest() gotW = %v, want %v", gotW, tt.wantW)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestReadUDPResponse(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
data []byte
|
||||
want bool
|
||||
want1 uint32
|
||||
want2 string
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "normal ok no padding",
|
||||
data: []byte("\x00\x00\x00\x00\x33\x0bhello world\x00"),
|
||||
want: true,
|
||||
want1: 51,
|
||||
want2: "hello world",
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "normal error with padding",
|
||||
data: []byte("\x01\x00\x00\x33\x33\x06stop!!\x05xxxxx"),
|
||||
want: false,
|
||||
want1: 13107,
|
||||
want2: "stop!!",
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "normal ok no message with padding",
|
||||
data: []byte("\x00\x00\x00\x00\x33\x00\x05xxxxx"),
|
||||
want: true,
|
||||
want1: 51,
|
||||
want2: "",
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "incomplete 1",
|
||||
data: []byte("\x00\x00\x06"),
|
||||
want: false,
|
||||
want1: 0,
|
||||
want2: "",
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "incomplete 2",
|
||||
data: []byte("\x01\x00\x01\x02\x03\x05jesus\x05x"),
|
||||
want: false,
|
||||
want1: 0,
|
||||
want2: "",
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
r := bytes.NewReader(tt.data)
|
||||
got, got1, got2, err := ReadUDPResponse(r)
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Errorf("ReadUDPResponse() error = %v, wantErr %v", err, tt.wantErr)
|
||||
return
|
||||
}
|
||||
if got != tt.want {
|
||||
t.Errorf("ReadUDPResponse() got = %v, want %v", got, tt.want)
|
||||
}
|
||||
if got1 != tt.want1 {
|
||||
t.Errorf("ReadUDPResponse() got1 = %v, want %v", got1, tt.want1)
|
||||
}
|
||||
if got2 != tt.want2 {
|
||||
t.Errorf("ReadUDPResponse() got2 = %v, want %v", got2, tt.want2)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestWriteUDPResponse(t *testing.T) {
|
||||
type args struct {
|
||||
ok bool
|
||||
sessionID uint32
|
||||
msg string
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
wantW string // Just a prefix, we don't care about the padding
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "normal ok",
|
||||
args: args{ok: true, sessionID: 6, msg: "hello world"},
|
||||
wantW: "\x00\x00\x00\x00\x06\x0bhello world",
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "normal error",
|
||||
args: args{ok: false, sessionID: 7, msg: "stop!!"},
|
||||
wantW: "\x01\x00\x00\x00\x07\x06stop!!",
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "empty",
|
||||
args: args{ok: true, sessionID: 0, msg: ""},
|
||||
wantW: "\x00\x00\x00\x00\x00\x00",
|
||||
wantErr: false,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
w := &bytes.Buffer{}
|
||||
err := WriteUDPResponse(w, tt.args.ok, tt.args.sessionID, tt.args.msg)
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Errorf("WriteUDPResponse() error = %v, wantErr %v", err, tt.wantErr)
|
||||
return
|
||||
}
|
||||
if gotW := w.String(); !(strings.HasPrefix(gotW, tt.wantW) && len(gotW) > len(tt.wantW)) {
|
||||
t.Errorf("WriteUDPResponse() gotW = %v, want %v", gotW, tt.wantW)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user