github.com/codingeasygo/util@v0.0.0-20231206062002-1ce2f004b7d9/xprop/xprop_test.go (about) 1 package xprop 2 3 import ( 4 "bytes" 5 "crypto/tls" 6 "fmt" 7 "io/ioutil" 8 "net/http" 9 "net/http/httptest" 10 "os" 11 "os/exec" 12 "testing" 13 "time" 14 15 "github.com/codingeasygo/util/converter" 16 ) 17 18 func assert(v bool) { 19 if !v { 20 panic("error") 21 } 22 } 23 24 func TestEnvReplace(t *testing.T) { 25 f := NewConfig() 26 f.Base = "." 27 f.SetValue("a", "123") 28 f.SetValue("b", "456") 29 fmt.Println(f.EnvReplace("sss${a}${b} ${abc} ${da} ${HOME} ${} ${CONF_DIR}")) 30 if v := f.EnvReplace("${a}"); v != "123" { 31 t.Error(v) 32 } 33 if v := f.EnvReplace("${xxxx,@/123}"); v != "123" { 34 t.Error(v) 35 } 36 if v := f.EnvReplace("${xxxx,@/}"); v != "" { 37 t.Error(v) 38 } 39 f.Clear() 40 if f.Length() != 0 { 41 t.Error("error") 42 } 43 } 44 45 func TestInit(t *testing.T) { 46 f := NewConfig() 47 err := f.LoadWait("not_found.properties", false) 48 if err == nil { 49 panic("init error") 50 } 51 err = f.Load("test_data.properties") 52 if err != nil { 53 t.Error(err.Error()) 54 return 55 } 56 if f.Str("abc_conf") != `1 57 2 58 3` { 59 fmt.Println("->\n", f.Str("abc_conf")) 60 t.Error("error") 61 return 62 } 63 for key, val := range f.config { 64 fmt.Println(key, ":", val) 65 } 66 fmt.Println(f.StrVal("inta")) 67 fmt.Println(f.StrVal("nfound")) 68 fmt.Println(f.IntVal("inta")) 69 fmt.Println(f.IntVal("nfound")) 70 fmt.Println(f.IntVal("a")) 71 fmt.Println(f.Float64Val("floata")) 72 fmt.Println(f.Float64Val("nfound")) 73 fmt.Println(f.Float64Val("a")) 74 fmt.Println(f.StrVal("abc_conf")) 75 fmt.Printf("\n\n\n\n") 76 f.Delete("nfound") 77 f.Delete("a") 78 f.Print() 79 f.PrintSection("loc") 80 } 81 82 func TestOpenError(t *testing.T) { 83 f := NewConfig() 84 fmt.Println(exec.Command("touch", "/tmp/fcg").Run()) 85 fmt.Println(exec.Command("chmod", "000", "/tmp/fcg").Run()) 86 fi, e := os.Open("/tmp/fcg") 87 fmt.Println(fi, e) 88 err := f.Load("/tmp/fcg") 89 if err == nil { 90 panic("init error") 91 } 92 fmt.Println(exec.Command("rm", "-f", "/tmp/fcg").Run()) 93 } 94 95 func TestValue(t *testing.T) { 96 f := NewConfig() 97 err := f.Load("test_data.properties?ukk=123") 98 if err != nil { 99 t.Error(err) 100 return 101 } 102 // 103 assert(f.IntDef(0, "inta") != 0) 104 assert(f.Int64Def(0, "inta") != 0) 105 assert(f.Uint64Def(0, "inta") != 0) 106 assert(f.Float64Def(0, "floata") != 0) 107 assert(f.StrDef("0", "floata") != "0") 108 assert(f.MapDef(nil, "json") != nil) 109 // 110 assert(f.IntDef(0, "notxxx") == 0) 111 assert(f.Int64Def(0, "notxxx") == 0) 112 assert(f.Uint64Def(0, "notxxx") == 0) 113 assert(f.Float64Def(0, "notxxx") == 0) 114 assert(f.StrDef("0", "notxxx") == "0") 115 assert(f.MapDef(nil, "notxxx") == nil) 116 // 117 assert(nil != f.ArrayIntDef(nil, "inta")) 118 assert(nil != f.ArrayInt64Def(nil, "inta")) 119 assert(nil != f.ArrayUint64Def(nil, "inta")) 120 assert(nil != f.ArrayFloat64Def(nil, "floata")) 121 assert(nil != f.ArrayStrDef(nil, "floata")) 122 assert(nil != f.ArrayMapDef(nil, "json2")) 123 // 124 assert(nil == f.ArrayIntDef(nil, "notxxx")) 125 assert(nil == f.ArrayInt64Def(nil, "notxxx")) 126 assert(nil == f.ArrayUint64Def(nil, "notxxx")) 127 assert(nil == f.ArrayFloat64Def(nil, "notxxx")) 128 assert(nil == f.ArrayStrDef(nil, "notxxx")) 129 assert(nil == f.ArrayMapDef(nil, "notxxx")) 130 // 131 assert(os.ModePerm == f.FileModeDef(0, "mode")) 132 assert(os.ModePerm == f.FileModeDef(os.ModePerm, "modex")) 133 assert(os.ModePerm == f.FileModeDef(os.ModePerm, "json2")) 134 } 135 136 func TestLoad(t *testing.T) { 137 http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: true} 138 handlerc := 0 139 proph := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 140 handlerc++ 141 if handlerc > 1 { 142 w.WriteHeader(200) 143 fmt.Fprintf(w, "a=1") 144 } else { 145 w.WriteHeader(500) 146 fmt.Fprintf(w, "testing") 147 } 148 }) 149 confh := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 150 handlerc++ 151 if handlerc > 1 { 152 w.WriteHeader(200) 153 fmt.Fprintf(w, ` 154 [a] 155 1 156 `) 157 } else { 158 w.WriteHeader(500) 159 fmt.Fprintf(w, "testing") 160 } 161 }) 162 var err error 163 var config *Config 164 var ts *httptest.Server 165 // 166 handlerc = 0 167 ts = httptest.NewServer(confh) 168 config, err = LoadConf(ts.URL + "/x.conf") 169 if err != nil { 170 t.Error(err.Error()) 171 return 172 } 173 fmt.Println(converter.JSON(config.config)) 174 assert(config.IntDef(0, "a") == 1) 175 ts.Close() 176 // 177 handlerc = 0 178 ts = httptest.NewServer(proph) 179 config, err = LoadConf(ts.URL) 180 if err != nil { 181 t.Error(err.Error()) 182 return 183 } 184 assert(config.IntDef(0, "a") == 1) 185 ts.Close() 186 // 187 handlerc = 0 188 ts = httptest.NewTLSServer(proph) 189 config, err = LoadConf(ts.URL) 190 if err != nil { 191 t.Error(err.Error()) 192 return 193 } 194 assert(config.IntDef(0, "a") == 1) 195 ts.Close() 196 // 197 config, err = LoadConf("data:text/conf,[a]\n1") 198 if err != nil { 199 t.Error(err.Error()) 200 return 201 } 202 assert(config.IntDef(0, "a") == 1) 203 // 204 config, err = LoadConf("data:text/prop,a=1") 205 if err != nil { 206 t.Error(err.Error()) 207 return 208 } 209 assert(config.IntDef(0, "a") == 1) 210 // 211 os.Remove("/tmp/xprop_test.properties") 212 go func() { 213 time.Sleep(300 * time.Millisecond) 214 ioutil.WriteFile("/tmp/xprop_test.properties", []byte("a=1"), os.ModePerm) 215 }() 216 config, err = LoadConf("/tmp/xprop_test.properties") 217 if err != nil { 218 t.Error(err.Error()) 219 return 220 } 221 assert(config.IntDef(0, "a") == 1) 222 // 223 config = NewConfig() 224 config.LoadPropString("a=1") 225 assert(config.IntDef(0, "a") == 1) 226 // 227 config = NewConfig() 228 config.LoadPropReader("", bytes.NewBufferString("a=1")) 229 assert(config.IntDef(0, "a") == 1) 230 // 231 config = NewConfig() 232 ioutil.WriteFile("/tmp/xprop_test.properties", []byte("a=1"), os.ModePerm) 233 config.LoadFile("/tmp/xprop_test.properties") 234 assert(config.IntDef(0, "a") == 1) 235 // 236 handlerc = 1 237 config = NewConfig() 238 ts = httptest.NewServer(proph) 239 err = config.LoadWeb(ts.URL) 240 if err != nil { 241 t.Error(err.Error()) 242 return 243 } 244 assert(config.IntDef(0, "a") == 1) 245 ts.Close() 246 } 247 248 func TestSection(t *testing.T) { 249 config, err := LoadConf("test_data.properties?ukk=123") 250 if err != nil { 251 t.Error(err) 252 return 253 } 254 if config.StrDef("", "ukk") != "123" { 255 t.Error("not right") 256 return 257 } 258 if config.StrDef("", "/ukk") != "123" { 259 t.Error("not right") 260 return 261 } 262 if config.StrDef("", "abc/txabc") != "1" { 263 t.Error("not right") 264 return 265 } 266 if config.StrDef("", "/abc/txabc") != "1" { 267 t.Error("not right") 268 return 269 } 270 if config.StrDef("", "abd/dxabc") != "1" { 271 t.Error("not right") 272 return 273 } 274 } 275 276 func TestError(t *testing.T) { 277 config := NewConfig() 278 assert(nil == config.exec("", " #xxxx", false)) 279 assert(nil != config.LoadWait("data:text/prop,@l:xxxx", false)) 280 assert(nil != config.LoadWait("data:text/prop,@l:http://127.0.0.1:2332", false)) 281 if _, err := config.webGet("\x01"); err == nil { 282 t.Error("error") 283 return 284 } 285 } 286 287 func TestMerge(t *testing.T) { 288 var cfga, cfgb, cfgc = NewConfig(), NewConfig(), NewConfig() 289 cfga.Masks = map[string]string{ 290 ".*_DB_.*": ".*:[^@]*", 291 } 292 cfga.Load("test_a.properties") 293 cfgb.Load("test_b.properties") 294 if len(cfga.Seces) != 3 { 295 t.Error("error") 296 return 297 } 298 cfga.Merge(cfgb) 299 if len(cfga.Seces) != 4 { 300 t.Error("error") 301 return 302 } 303 cfga.Merge(nil) 304 cfgd := cfga.Clone() 305 if len(cfgd.Seces) != 4 { 306 t.Error("error") 307 return 308 } 309 cfgc.MergeSection("a", cfga) 310 if len(cfgc.Seces) != 1 { 311 t.Error("error") 312 return 313 } 314 } 315 316 func TestRange(t *testing.T) { 317 config, _ := LoadConf("test_a.properties") 318 config.Range("a", func(key string, val interface{}) { 319 fmt.Println(key, val) 320 }) 321 } 322 323 // func TestFileMode(t *testing.T) { 324 // config := NewConfig() 325 // config.SetValue("abc", "077") 326 // fmt.Println(cfg.FileModeV("abc", os.ModePerm)) 327 // fmt.Println(cfg.FileModeV("xx", os.ModePerm)) 328 // fmt.Println(cfg.FileModeV("abc2", os.ModePerm)) 329 // } 330 331 func TestPrintMask(t *testing.T) { 332 config := NewConfig() 333 config.SetValue("loc/DB_DB_URL", "test:123@localhost") 334 config.SetValue("xxx/DB_DB_URL", "test:123@localhost") 335 config.Masks = map[string]string{ 336 ".*_DB_.*": ".*:[^@]*", 337 } 338 config.Print() 339 config.PrintSection("loc") 340 } 341 342 func TestSetValue(t *testing.T) { 343 conf := NewConfig() 344 conf.SetValue("xxx/a", "123") 345 if conf.StrDef("", "xxx/a") != "123" { 346 t.Error("error") 347 return 348 } 349 if conf.StrDef("", "/xxx/a") != "123" { 350 t.Error("error") 351 return 352 } 353 conf.SetValue("/xxx/b", "456") 354 if conf.StrDef("", "xxx/b") != "456" { 355 t.Error("error") 356 return 357 } 358 if conf.StrDef("", "/xxx/b") != "456" { 359 t.Error("error") 360 return 361 } 362 363 }