marshal_test.go 3.2 KB

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