udp_test.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. // Copyright 2016 fatedier, fatedier@gmail.com
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package udp
  15. import (
  16. "net"
  17. "testing"
  18. "github.com/stretchr/testify/assert"
  19. )
  20. var (
  21. content string = "udp packet test"
  22. src string = "1.1.1.1:1000"
  23. dst string = "2.2.2.2:2000"
  24. udpMsg *UdpPacket
  25. )
  26. func init() {
  27. srcAddr, _ := net.ResolveUDPAddr("udp", src)
  28. dstAddr, _ := net.ResolveUDPAddr("udp", dst)
  29. udpMsg = NewUdpPacket([]byte(content), srcAddr, dstAddr)
  30. }
  31. func TestPack(t *testing.T) {
  32. assert := assert.New(t)
  33. msg := udpMsg.Pack()
  34. assert.Equal(string(msg), `{"content":"dWRwIHBhY2tldCB0ZXN0","src":"1.1.1.1:1000","dst":"2.2.2.2:2000"}`)
  35. }
  36. func TestUnpack(t *testing.T) {
  37. assert := assert.New(t)
  38. udpMsg.UnPack([]byte(`{"content":"dWRwIHBhY2tldCB0ZXN0","src":"1.1.1.1:1000","dst":"2.2.2.2:2000"}`))
  39. assert.Equal(content, string(udpMsg.Content))
  40. assert.Equal(src, udpMsg.Src.String())
  41. assert.Equal(dst, udpMsg.Dst.String())
  42. }