marshal_test.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. package reflect_test
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "strings"
  6. "testing"
  7. "github.com/xtls/xray-core/common/protocol"
  8. . "github.com/xtls/xray-core/common/reflect"
  9. cserial "github.com/xtls/xray-core/common/serial"
  10. iserial "github.com/xtls/xray-core/infra/conf/serial"
  11. "github.com/xtls/xray-core/proxy/shadowsocks"
  12. )
  13. func TestMashalAccount(t *testing.T) {
  14. account := &shadowsocks.Account{
  15. Password: "shadowsocks-password",
  16. CipherType: shadowsocks.CipherType_CHACHA20_POLY1305,
  17. }
  18. user := &protocol.User{
  19. Level: 0,
  20. Email: "[email protected]",
  21. Account: cserial.ToTypedMessage(account),
  22. }
  23. j, ok := MarshalToJson(user, false)
  24. if !ok || strings.Contains(j, "_TypedMessage_") {
  25. t.Error("marshal account failed")
  26. }
  27. kws := []string{"CHACHA20_POLY1305", "cipherType", "shadowsocks-password"}
  28. for _, kw := range kws {
  29. if !strings.Contains(j, kw) {
  30. t.Error("marshal account failed")
  31. }
  32. }
  33. // t.Log(j)
  34. }
  35. func TestMashalStruct(t *testing.T) {
  36. type Foo = struct {
  37. N int `json:"n"`
  38. Np *int `json:"np"`
  39. S string `json:"s"`
  40. Arr *[]map[string]map[string]string `json:"arr"`
  41. }
  42. n := 1
  43. np := &n
  44. arr := make([]map[string]map[string]string, 0)
  45. m1 := make(map[string]map[string]string, 0)
  46. m2 := make(map[string]string, 0)
  47. m2["hello"] = "world"
  48. m1["foo"] = m2
  49. arr = append(arr, m1)
  50. f1 := Foo{
  51. N: n,
  52. Np: np,
  53. S: "hello",
  54. Arr: &arr,
  55. }
  56. s, ok1 := MarshalToJson(f1, true)
  57. sp, ok2 := MarshalToJson(&f1, true)
  58. if !ok1 || !ok2 || s != sp {
  59. t.Error("marshal failed")
  60. }
  61. f2 := Foo{}
  62. if json.Unmarshal([]byte(s), &f2) != nil {
  63. t.Error("json unmarshal failed")
  64. }
  65. v := (*f2.Arr)[0]["foo"]["hello"]
  66. if f1.N != f2.N || *(f1.Np) != *(f2.Np) || f1.S != f2.S || v != "world" {
  67. t.Error("f1 not equal to f2")
  68. }
  69. }
  70. func TestMarshalConfigJson(t *testing.T) {
  71. buf := bytes.NewBufferString(getConfig())
  72. config, err := iserial.DecodeJSONConfig(buf)
  73. if err != nil {
  74. t.Error("decode JSON config failed")
  75. }
  76. bc, err := config.Build()
  77. if err != nil {
  78. t.Error("build core config failed")
  79. }
  80. tmsg := cserial.ToTypedMessage(bc)
  81. tc, ok := MarshalToJson(tmsg, true)
  82. if !ok {
  83. t.Error("marshal config failed")
  84. }
  85. // t.Log(tc)
  86. keywords := []string{
  87. "4784f9b8-a879-4fec-9718-ebddefa47750",
  88. "bing.com",
  89. "inboundTag",
  90. "level",
  91. "stats",
  92. "userDownlink",
  93. "userUplink",
  94. "system",
  95. "inboundDownlink",
  96. "outboundUplink",
  97. }
  98. for _, kw := range keywords {
  99. if !strings.Contains(tc, kw) {
  100. t.Error("marshaled config error")
  101. }
  102. }
  103. }
  104. func getConfig() string {
  105. return `{
  106. "log": {
  107. "loglevel": "debug"
  108. },
  109. "stats": {},
  110. "policy": {
  111. "levels": {
  112. "0": {
  113. "statsUserUplink": true,
  114. "statsUserDownlink": true
  115. }
  116. },
  117. "system": {
  118. "statsInboundUplink": true,
  119. "statsInboundDownlink": true,
  120. "statsOutboundUplink": true,
  121. "statsOutboundDownlink": true
  122. }
  123. },
  124. "inbounds": [
  125. {
  126. "tag": "agentin",
  127. "protocol": "http",
  128. "port": 8080,
  129. "listen": "127.0.0.1",
  130. "settings": {}
  131. },
  132. {
  133. "listen": "127.0.0.1",
  134. "port": 10085,
  135. "protocol": "dokodemo-door",
  136. "settings": {
  137. "address": "127.0.0.1"
  138. },
  139. "tag": "api-in"
  140. }
  141. ],
  142. "api": {
  143. "tag": "api",
  144. "services": [
  145. "HandlerService",
  146. "StatsService"
  147. ]
  148. },
  149. "routing": {
  150. "rules": [
  151. {
  152. "inboundTag": [
  153. "api-in"
  154. ],
  155. "outboundTag": "api",
  156. "type": "field"
  157. }
  158. ],
  159. "domainStrategy": "AsIs"
  160. },
  161. "outbounds": [
  162. {
  163. "protocol": "vless",
  164. "settings": {
  165. "vnext": [
  166. {
  167. "address": "1.2.3.4",
  168. "port": 1234,
  169. "users": [
  170. {
  171. "id": "4784f9b8-a879-4fec-9718-ebddefa47750",
  172. "encryption": "none"
  173. }
  174. ]
  175. }
  176. ]
  177. },
  178. "tag": "agentout",
  179. "streamSettings": {
  180. "network": "ws",
  181. "security": "none",
  182. "wsSettings": {
  183. "path": "/?ed=2048",
  184. "headers": {
  185. "Host": "bing.com"
  186. }
  187. }
  188. }
  189. }
  190. ]
  191. }`
  192. }