github.com/xraypb/Xray-core@v1.8.1/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/xraypb/Xray-core/common"
     9  	"github.com/xraypb/Xray-core/common/buf"
    10  	"github.com/xraypb/Xray-core/common/protocol"
    11  	"github.com/xraypb/Xray-core/common/uuid"
    12  	. "github.com/xraypb/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  		AlterIds: 1024,
    20  		Level:    128,
    21  		ValidMin: 16,
    22  	}
    23  
    24  	buffer := buf.New()
    25  	common.Must(MarshalCommand(sa, buffer))
    26  
    27  	cmd, err := UnmarshalCommand(1, buffer.BytesFrom(2))
    28  	common.Must(err)
    29  
    30  	sa2, ok := cmd.(*protocol.CommandSwitchAccount)
    31  	if !ok {
    32  		t.Fatal("failed to convert command to CommandSwitchAccount")
    33  	}
    34  	if r := cmp.Diff(sa2, sa); r != "" {
    35  		t.Error(r)
    36  	}
    37  }
    38  
    39  func TestSwitchAccountBugOffByOne(t *testing.T) {
    40  	sa := &protocol.CommandSwitchAccount{
    41  		Port:     1234,
    42  		ID:       uuid.New(),
    43  		AlterIds: 1024,
    44  		Level:    128,
    45  		ValidMin: 16,
    46  	}
    47  
    48  	buffer := buf.New()
    49  	csaf := CommandSwitchAccountFactory{}
    50  	common.Must(csaf.Marshal(sa, buffer))
    51  
    52  	Payload := buffer.Bytes()
    53  
    54  	cmd, err := csaf.Unmarshal(Payload[:len(Payload)-1])
    55  	assert.Error(t, err)
    56  	assert.Nil(t, cmd)
    57  }