|
|
|
@ -23,8 +23,9 @@ func equalRequestHeader(x, y *protocol.RequestHeader) bool {
|
|
|
|
|
}))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestUDPEncoding(t *testing.T) {
|
|
|
|
|
request := &protocol.RequestHeader{
|
|
|
|
|
func TestUDPEncodingDecoding(t *testing.T) {
|
|
|
|
|
testRequests := []protocol.RequestHeader{
|
|
|
|
|
{
|
|
|
|
|
Version: Version,
|
|
|
|
|
Command: protocol.RequestCommandUDP,
|
|
|
|
|
Address: net.LocalHostIP,
|
|
|
|
@ -36,11 +37,26 @@ func TestUDPEncoding(t *testing.T) {
|
|
|
|
|
CipherType: CipherType_AES_128_GCM,
|
|
|
|
|
}),
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
Version: Version,
|
|
|
|
|
Command: protocol.RequestCommandUDP,
|
|
|
|
|
Address: net.LocalHostIP,
|
|
|
|
|
Port: 1234,
|
|
|
|
|
User: &protocol.MemoryUser{
|
|
|
|
|
Email: "love@example.com",
|
|
|
|
|
Account: toAccount(&Account{
|
|
|
|
|
Password: "123",
|
|
|
|
|
CipherType: CipherType_NONE,
|
|
|
|
|
}),
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, request := range testRequests {
|
|
|
|
|
data := buf.New()
|
|
|
|
|
common.Must2(data.WriteString("test string"))
|
|
|
|
|
encodedData, err := EncodeUDPPacket(request, data.Bytes())
|
|
|
|
|
encodedData, err := EncodeUDPPacket(&request, data.Bytes())
|
|
|
|
|
common.Must(err)
|
|
|
|
|
|
|
|
|
|
validator := new(Validator)
|
|
|
|
@ -52,9 +68,36 @@ func TestUDPEncoding(t *testing.T) {
|
|
|
|
|
t.Error("data: ", r)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if equalRequestHeader(decodedRequest, request) == false {
|
|
|
|
|
if equalRequestHeader(decodedRequest, &request) == false {
|
|
|
|
|
t.Error("different request")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestUDPDecodingWithPayloadTooShort(t *testing.T) {
|
|
|
|
|
testAccounts := []protocol.Account{
|
|
|
|
|
toAccount(&Account{
|
|
|
|
|
Password: "password",
|
|
|
|
|
CipherType: CipherType_AES_128_GCM,
|
|
|
|
|
}),
|
|
|
|
|
toAccount(&Account{
|
|
|
|
|
Password: "password",
|
|
|
|
|
CipherType: CipherType_NONE,
|
|
|
|
|
}),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, account := range testAccounts {
|
|
|
|
|
data := buf.New()
|
|
|
|
|
data.WriteString("short payload")
|
|
|
|
|
validator := new(Validator)
|
|
|
|
|
validator.Add(&protocol.MemoryUser{
|
|
|
|
|
Account: account,
|
|
|
|
|
})
|
|
|
|
|
_, _, err := DecodeUDPPacket(validator, data)
|
|
|
|
|
if err == nil {
|
|
|
|
|
t.Fatal("expected error")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestTCPRequest(t *testing.T) {
|
|
|
|
|