github.com/xmplusdev/xray-core@v1.8.10/proxy/vmess/encoding/commands_test.go (about)

     1  package encoding_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/google/go-cmp/cmp"
     7  	"github.com/stretchr/testify/assert"
     8  	"github.com/xmplusdev/xray-core/common"
     9  	"github.com/xmplusdev/xray-core/common/buf"
    10  	"github.com/xmplusdev/xray-core/common/protocol"
    11  	"github.com/xmplusdev/xray-core/common/uuid"
    12  	. "github.com/xmplusdev/xray-core/proxy/vmess/encoding"
    13  )
    14  
    15  func TestSwitchAccount(t *testing.T) {
    16  	sa := &protocol.CommandSwitchAccount{
    17  		Port:     1234,
    18  		ID:       uuid.New(),
    19  		Level:    128,
    20  		ValidMin: 16,
    21  	}
    22  
    23  	buffer := buf.New()
    24  	common.Must(MarshalCommand(sa, buffer))
    25  
    26  	cmd, err := UnmarshalCommand(1, buffer.BytesFrom(2))
    27  	common.Must(err)
    28  
    29  	sa2, ok := cmd.(*protocol.CommandSwitchAccount)
    30  	if !ok {
    31  		t.Fatal("failed to convert command to CommandSwitchAccount")
    32  	}
    33  	if r := cmp.Diff(sa2, sa); r != "" {
    34  		t.Error(r)
    35  	}
    36  }
    37  
    38  func TestSwitchAccountBugOffByOne(t *testing.T) {
    39  	sa := &protocol.CommandSwitchAccount{
    40  		Port:     1234,
    41  		ID:       uuid.New(),
    42  		Level:    128,
    43  		ValidMin: 16,
    44  	}
    45  
    46  	buffer := buf.New()
    47  	csaf := CommandSwitchAccountFactory{}
    48  	common.Must(csaf.Marshal(sa, buffer))
    49  
    50  	Payload := buffer.Bytes()
    51  
    52  	cmd, err := csaf.Unmarshal(Payload[:len(Payload)-1])
    53  	assert.Error(t, err)
    54  	assert.Nil(t, cmd)
    55  }