github.com/xmplusdev/xmcore@v1.8.11-0.20240412132628-5518b55526af/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/xmcore/common" 9 "github.com/xmplusdev/xmcore/common/buf" 10 "github.com/xmplusdev/xmcore/common/protocol" 11 "github.com/xmplusdev/xmcore/common/uuid" 12 . "github.com/xmplusdev/xmcore/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 }