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