commands_test.go 1.2 KB

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