user_json_test.go 985 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. // +build json
  2. package protocol_test
  3. import (
  4. "encoding/json"
  5. "testing"
  6. . "github.com/v2ray/v2ray-core/common/protocol"
  7. "github.com/v2ray/v2ray-core/testing/assert"
  8. )
  9. func TestUserParsing(t *testing.T) {
  10. assert := assert.On(t)
  11. user := new(User)
  12. err := json.Unmarshal([]byte(`{
  13. "id": "96edb838-6d68-42ef-a933-25f7ac3a9d09",
  14. "email": "love@v2ray.com",
  15. "level": 1,
  16. "alterId": 100
  17. }`), user)
  18. assert.Error(err).IsNil()
  19. assert.Byte(byte(user.Level)).Equals(1)
  20. account, ok := user.Account.(*VMessAccount)
  21. assert.Bool(ok).IsTrue()
  22. assert.String(account.ID.String()).Equals("96edb838-6d68-42ef-a933-25f7ac3a9d09")
  23. }
  24. func TestInvalidUserJson(t *testing.T) {
  25. assert := assert.On(t)
  26. user := new(User)
  27. err := json.Unmarshal([]byte(`{"id": 1234}`), user)
  28. assert.Error(err).IsNotNil()
  29. }
  30. func TestInvalidIdJson(t *testing.T) {
  31. assert := assert.On(t)
  32. user := new(User)
  33. err := json.Unmarshal([]byte(`{"id": "1234"}`), user)
  34. assert.Error(err).IsNotNil()
  35. }