github.com/Asutorufa/yuhaiin@v0.3.6-0.20240502055049-7984da7023a0/pkg/net/proxy/yuubinsya/packet_test.go (about)

     1  package yuubinsya
     2  
     3  import (
     4  	"math/rand/v2"
     5  	"net"
     6  	"testing"
     7  
     8  	"github.com/Asutorufa/yuhaiin/pkg/net/proxy/yuubinsya/crypto"
     9  	"github.com/Asutorufa/yuhaiin/pkg/net/proxy/yuubinsya/plain"
    10  	"github.com/Asutorufa/yuhaiin/pkg/net/proxy/yuubinsya/types"
    11  	"github.com/Asutorufa/yuhaiin/pkg/utils/assert"
    12  	"github.com/Asutorufa/yuhaiin/pkg/utils/pool"
    13  )
    14  
    15  var letters = []byte("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
    16  
    17  func randSeq(n int) []byte {
    18  	b := make([]byte, n)
    19  	for i := range b {
    20  		b[i] = letters[rand.IntN(len(letters))]
    21  	}
    22  	return b
    23  }
    24  
    25  func TestEncode(t *testing.T) {
    26  	auth, err := crypto.GetAuth([]byte("test"))
    27  	assert.NoError(t, err)
    28  
    29  	buf := pool.GetBytesWriter(pool.MaxSegmentSize)
    30  	assert.NoError(t, types.EncodePacket(buf, &net.UDPAddr{IP: net.IPv4(127, 0, 0, 1), Port: 1234},
    31  		randSeq(rand.IntN(1000)), auth, true))
    32  
    33  	t.Log(buf.Bytes())
    34  
    35  	data, addr, err := types.DecodePacket(buf.Bytes(), auth, true)
    36  	assert.NoError(t, err)
    37  
    38  	t.Log(data, addr)
    39  
    40  	plainauth := plain.NewAuth([]byte{1, 2, 3, 4, 5})
    41  
    42  	buf = pool.GetBytesWriter(pool.MaxSegmentSize)
    43  	assert.NoError(t, types.EncodePacket(buf,
    44  		&net.UDPAddr{IP: net.IPv4(127, 0, 0, 1), Port: 1234},
    45  		randSeq(rand.IntN(1000)), plainauth, true))
    46  
    47  	t.Log(buf.Bytes())
    48  
    49  	data, addr, err = types.DecodePacket(buf.Bytes(), plainauth, true)
    50  	assert.NoError(t, err)
    51  
    52  	t.Log(data, addr)
    53  
    54  	buf = pool.GetBytesWriter(pool.MaxSegmentSize)
    55  	assert.NoError(t, types.EncodePacket(buf,
    56  		&net.UDPAddr{IP: net.IPv4(127, 0, 0, 1), Port: 1234},
    57  		randSeq(rand.IntN(1000)), nil, false))
    58  
    59  	t.Log(buf.Bytes())
    60  
    61  	data, addr, err = types.DecodePacket(buf.Bytes(), nil, false)
    62  	assert.NoError(t, err)
    63  
    64  	t.Log(data, addr)
    65  }