Browse Source

test case for socks udp

V2Ray 10 năm trước cách đây
mục cha
commit
59a6e1cef8
1 tập tin đã thay đổi với 55 bổ sung0 xóa
  1. 55 0
      proxy/socks/socks_test.go

+ 55 - 0
proxy/socks/socks_test.go

@@ -114,3 +114,58 @@ func TestSocksTcpConnectWithUserPass(t *testing.T) {
 	assert.Bytes(dataReturned).Equals(och.Data2Return)
 	assert.String(targetServer).Equals(och.Destination.Address().String())
 }
+
+func TestSocksUdpSend(t *testing.T) {
+	assert := unit.Assert(t)
+	port := uint16(12372)
+
+	och := &mocks.OutboundConnectionHandler{
+		Data2Send:   bytes.NewBuffer(make([]byte, 0, 1024)),
+		Data2Return: []byte("The data to be returned to socks server."),
+	}
+
+	core.RegisterOutboundConnectionHandlerFactory("mock_och", och)
+
+	config := mocks.Config{
+		PortValue: port,
+		InboundConfigValue: &mocks.ConnectionConfig{
+			ProtocolValue: "socks",
+			ContentValue:  []byte("{\"auth\": \"noauth\", \"udp\": true}"),
+		},
+		OutboundConfigValue: &mocks.ConnectionConfig{
+			ProtocolValue: "mock_och",
+			ContentValue:  nil,
+		},
+	}
+
+	point, err := core.NewPoint(&config)
+	assert.Error(err).IsNil()
+
+	err = point.Start()
+	assert.Error(err).IsNil()
+
+	conn, err := net.DialUDP("udp", nil, &net.UDPAddr{
+		IP:   []byte{127, 0, 0, 1},
+		Port: int(port),
+		Zone: "",
+	})
+
+	assert.Error(err).IsNil()
+
+	data2Send := []byte("Fake DNS request")
+
+	buffer := make([]byte, 0, 1024)
+	buffer = append(buffer, 0, 0, 0)
+	buffer = append(buffer, 1, 8, 8, 4, 4, 0, 53)
+	buffer = append(buffer, data2Send...)
+
+	conn.Write(buffer)
+
+	response := make([]byte, 1024)
+	nBytes, err := conn.Read(response)
+
+	assert.Error(err).IsNil()
+	assert.Bytes(response[:nBytes]).Equals(och.Data2Return)
+	assert.Bytes(data2Send).Equals(och.Data2Send.Bytes())
+	assert.String(och.Destination.String()).Equals("udp:8.8.4.4:53")
+}