1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950 |
- package udp
- import (
- "net"
- "testing"
- "github.com/stretchr/testify/assert"
- )
- var (
- content string = "udp packet test"
- src string = "1.1.1.1:1000"
- dst string = "2.2.2.2:2000"
- udpMsg *UdpPacket
- )
- func init() {
- srcAddr, _ := net.ResolveUDPAddr("udp", src)
- dstAddr, _ := net.ResolveUDPAddr("udp", dst)
- udpMsg = NewUdpPacket([]byte(content), srcAddr, dstAddr)
- }
- func TestPack(t *testing.T) {
- assert := assert.New(t)
- msg := udpMsg.Pack()
- assert.Equal(string(msg), `{"content":"dWRwIHBhY2tldCB0ZXN0","src":"1.1.1.1:1000","dst":"2.2.2.2:2000"}`)
- }
- func TestUnpack(t *testing.T) {
- assert := assert.New(t)
- udpMsg.UnPack([]byte(`{"content":"dWRwIHBhY2tldCB0ZXN0","src":"1.1.1.1:1000","dst":"2.2.2.2:2000"}`))
- assert.Equal(content, string(udpMsg.Content))
- assert.Equal(src, udpMsg.Src.String())
- assert.Equal(dst, udpMsg.Dst.String())
- }
|