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