commands_test.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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. Level: 128,
  17. ValidMin: 16,
  18. }
  19. buffer := buf.New()
  20. common.Must(MarshalCommand(sa, buffer))
  21. cmd, err := UnmarshalCommand(1, buffer.BytesFrom(2))
  22. common.Must(err)
  23. sa2, ok := cmd.(*protocol.CommandSwitchAccount)
  24. if !ok {
  25. t.Fatal("failed to convert command to CommandSwitchAccount")
  26. }
  27. if r := cmp.Diff(sa2, sa); r != "" {
  28. t.Error(r)
  29. }
  30. }
  31. func TestSwitchAccountBugOffByOne(t *testing.T) {
  32. sa := &protocol.CommandSwitchAccount{
  33. Port: 1234,
  34. ID: uuid.New(),
  35. Level: 128,
  36. ValidMin: 16,
  37. }
  38. buffer := buf.New()
  39. csaf := CommandSwitchAccountFactory{}
  40. common.Must(csaf.Marshal(sa, buffer))
  41. Payload := buffer.Bytes()
  42. cmd, err := csaf.Unmarshal(Payload[:len(Payload)-1])
  43. assert.Error(t, err)
  44. assert.Nil(t, cmd)
  45. }