github.com/v2fly/v2ray-core/v5@v5.16.2-0.20240507031116-8191faa6e095/infra/conf/merge/merge_test.go (about) 1 // Copyright 2020 Jebbs. All rights reserved. 2 // Use of this source code is governed by MIT 3 // license that can be found in the LICENSE file. 4 5 package merge_test 6 7 import ( 8 "bytes" 9 "reflect" 10 "strings" 11 "testing" 12 13 "github.com/v2fly/v2ray-core/v5/infra/conf/merge" 14 "github.com/v2fly/v2ray-core/v5/infra/conf/serial" 15 ) 16 17 func TestMergeV2Style(t *testing.T) { 18 json1 := ` 19 { 20 "log": {"access": "some_value", "loglevel": "debug"}, 21 "inbounds": [{"tag": "in-1"}], 22 "outbounds": [{"_priority": 100, "tag": "out-1"}], 23 "routing": {"rules": [ 24 {"_tag":"default_route","inboundTag":["in-1"],"outboundTag":"out-1"} 25 ]} 26 } 27 ` 28 json2 := ` 29 { 30 "log": {"loglevel": "error"}, 31 "inbounds": [{"tag": "in-2"}], 32 "outbounds": [{"_priority": -100, "tag": "out-2"}], 33 "routing": {"rules": [ 34 {"inboundTag":["in-2"],"outboundTag":"out-2"}, 35 {"_tag":"default_route","inboundTag":["in-1.1"],"outboundTag":"out-1.1"} 36 ]} 37 } 38 ` 39 expected := ` 40 { 41 "log": {"access": "some_value", "loglevel": "error"}, 42 "inbounds": [{"tag": "in-1"},{"tag": "in-2"}], 43 "outbounds": [ 44 {"tag": "out-2"}, 45 {"tag": "out-1"} 46 ], 47 "routing": {"rules": [ 48 {"inboundTag":["in-1","in-1.1"],"outboundTag":"out-1.1"}, 49 {"inboundTag":["in-2"],"outboundTag":"out-2"} 50 ]} 51 } 52 ` 53 m, err := merge.JSONs([][]byte{[]byte(json1), []byte(json2)}) 54 if err != nil { 55 t.Error(err) 56 } 57 assertResult(t, m, expected) 58 } 59 60 func TestMergeTag(t *testing.T) { 61 json1 := ` 62 { 63 "routing": { 64 "rules": [{ 65 "tag":"1", 66 "inboundTag": ["in-1"], 67 "outboundTag": "out-1" 68 }] 69 } 70 } 71 ` 72 json2 := ` 73 { 74 "routing": { 75 "rules": [{ 76 "_tag":"1", 77 "inboundTag": ["in-2"], 78 "outboundTag": "out-2" 79 }] 80 } 81 } 82 ` 83 expected := ` 84 { 85 "routing": { 86 "rules": [{ 87 "tag":"1", 88 "inboundTag": ["in-1", "in-2"], 89 "outboundTag": "out-2" 90 }] 91 } 92 } 93 ` 94 m, err := merge.JSONs([][]byte{[]byte(json1), []byte(json2)}) 95 if err != nil { 96 t.Error(err) 97 } 98 assertResult(t, m, expected) 99 } 100 101 func TestMergeTagValueTypes(t *testing.T) { 102 json1 := ` 103 { 104 "array_1": [{ 105 "_tag":"1", 106 "array_2": [{ 107 "_tag":"2", 108 "array_3.1": ["string",true,false], 109 "array_3.2": [1,2,3], 110 "number_1": 1, 111 "number_2": 1, 112 "bool_1": true, 113 "bool_2": true 114 }] 115 }] 116 } 117 ` 118 json2 := ` 119 { 120 "array_1": [{ 121 "_tag":"1", 122 "array_2": [{ 123 "_tag":"2", 124 "array_3.1": [0,1,null], 125 "array_3.2": null, 126 "number_1": 0, 127 "number_2": 1, 128 "bool_1": true, 129 "bool_2": false, 130 "null_1": null 131 }] 132 }] 133 } 134 ` 135 expected := ` 136 { 137 "array_1": [{ 138 "array_2": [{ 139 "array_3.1": ["string",true,false,0,1,null], 140 "array_3.2": [1,2,3], 141 "number_1": 0, 142 "number_2": 1, 143 "bool_1": true, 144 "bool_2": false, 145 "null_1": null 146 }] 147 }] 148 } 149 ` 150 m, err := merge.JSONs([][]byte{[]byte(json1), []byte(json2)}) 151 if err != nil { 152 t.Error(err) 153 } 154 assertResult(t, m, expected) 155 } 156 157 func TestMergeTagDeep(t *testing.T) { 158 json1 := ` 159 { 160 "array_1": [{ 161 "_tag":"1", 162 "array_2": [{ 163 "_tag":"2", 164 "array_3": [true,false,"string"] 165 }] 166 }] 167 } 168 ` 169 json2 := ` 170 { 171 "array_1": [{ 172 "_tag":"1", 173 "array_2": [{ 174 "_tag":"2", 175 "_priority":-100, 176 "array_3": [0,1,null] 177 }] 178 }] 179 } 180 ` 181 expected := ` 182 { 183 "array_1": [{ 184 "array_2": [{ 185 "array_3": [0,1,null,true,false,"string"] 186 }] 187 }] 188 } 189 ` 190 m, err := merge.JSONs([][]byte{[]byte(json1), []byte(json2)}) 191 if err != nil { 192 t.Error(err) 193 } 194 assertResult(t, m, expected) 195 } 196 197 func assertResult(t *testing.T, value []byte, expected string) { 198 v := make(map[string]interface{}) 199 err := serial.DecodeJSON(bytes.NewReader(value), &v) 200 if err != nil { 201 t.Error(err) 202 } 203 e := make(map[string]interface{}) 204 err = serial.DecodeJSON(strings.NewReader(expected), &e) 205 if err != nil { 206 t.Error(err) 207 } 208 if !reflect.DeepEqual(v, e) { 209 t.Fatalf("expected:\n%s\n\nactual:\n%s", expected, string(value)) 210 } 211 }