func TestHostnamesInheritFromDefault(t *testing.T) { assert := assert.Assert(t) yml := loadFile("config_hostnames_inherit_from_default.yml") setting, err := kasi.ParseConfig(yml) assert.Equal(err, nil) assert.Equal(len(setting.Services), 1) assert.Equal(len(setting.Services[0].Hostnames), 2) for _, h := range []string{"my0.github.com", "my1.github.com"} { if sort.SearchStrings(setting.Services[0].Hostnames, h) == 2 { t.Error("failed to parse hostnames") } } ymlOverride := loadFile("config_hostnames_override_default.yml") setting, err = kasi.ParseConfig(ymlOverride) assert.Nil(err) assert.Equal(len(setting.Services), 1) assert.Equal(len(setting.Services[0].Hostnames), 1) for _, h := range []string{"my2.github.com"} { if sort.SearchStrings(setting.Services[0].Hostnames, h) == 2 { t.Error("failed to parse hostnames") } } }
func TestEndpointsOpen(t *testing.T) { assert := assert.Assert(t) var yml string var setting *kasi_conf.CoreSetting var err error yml = loadFile("config_simple_endpoint0.yml") setting, err = kasi.ParseConfig(yml) assert.Nil(err) assert.Equal(len(setting.Services[0].Endpoints), 1) yml = loadFile("config_opened_endpoint.yml") setting, err = kasi.ParseConfig(yml) assert.Nil(err) assert.Equal(len(setting.Services[0].Endpoints), 1) yml = loadFile("config_closed_endpoint.yml") setting, err = kasi.ParseConfig(yml) assert.Nil(err) assert.False(setting.Services[0].Opened(), "") assert.Equal(len(setting.Services[0].Endpoints), 0) }
func TestMiddleware(t *testing.T) { assert := assert.Assert(t) yml := loadFile("conf_middlewares.yml") setting, err := kasi.ParseConfig(yml) assert.Equal(err, nil) assert.Equal(len(setting.Middlewares), 2) service0, _ := setting.GetServiceByID("service0") assert.Equal(service0.Middleware, "base") service1, _ := setting.GetServiceByID("service1") assert.Equal(service1.Middleware, "extend") assert.Equal(len(service0.Endpoints[0].Middleware), 2) assert.Equal(service0.Endpoints[0].Middleware[0], "a.js") assert.Equal(service0.Endpoints[0].Middleware[1], "b.js") assert.Equal(len(service0.Endpoints[0].Middleware), 2) assert.Nil(service0.Endpoints[1].Middleware) assert.Equal(len(service1.Endpoints[0].Middleware), 2) assert.Equal(service1.Endpoints[0].Middleware[0], "a.js") assert.Equal(service1.Endpoints[0].Middleware[1], "b.js") assert.Equal(len(service1.Endpoints[1].Middleware), 2) assert.Equal(service1.Endpoints[1].Middleware[0], "c.js") assert.Equal(service1.Endpoints[1].Middleware[1], "d.js") }
func TestServices(t *testing.T) { assert := assert.Assert(t) services := new(Services) assert.Equal(len(services.Services), 0) // Addition services.Add(Service{Name: "google.com", Password: "******", Meta: "Personal"}) assert.Equal(len(services.Services), 1) assert.Equal(services.Services[0].Name, "google.com") // replacement services.Add(Service{Name: "google.com", Password: "******"}) assert.Equal(len(services.Services), 1) assert.Equal(services.Services[0].Password, "lkjpoiu") // Addition services.Add(Service{Name: "facebook.com", Password: "******"}) assert.Equal(len(services.Services), 2) // another replacement services.Add(Service{Name: "google.com", Password: "******"}) assert.Equal(services.Get("google.com").Password, "asghasd") assert.Equal(len(services.Services), 2) services.Add(Service{Name: "golang.org"}) goServices := services.Search("go") assert.Equal(len(goServices), 2) assert.True(goServices[0].Name[:2] == "go", "Service 0 begins with go") assert.True(goServices[1].Name[:2] == "go", "Service 1 begins with go") assert.Equal(len(services.Search("*")), 3) }
func TestJoinURL(t *testing.T) { assert := assert.Assert(t) var base, target string // 2 FQDN url base = "http://a0.com/b0/c0.html" target = "http://a1.com/b1/c1.html" assert.Equal(joinURL(base, target), target) // 1 FQDN and 1 path base = "http://a0.com/b0/c0.html" target = "/b1/c1.html" assert.Equal(joinURL(base, target), "http://a0.com"+target) // 1 invalid url and 1 valid url base = "****************\\*" target = "/b1/c1.html" assert.Equal(joinURL(base, target), target) // 2 path, but one is relative base = "/b0/c0.html" target = "b1/c1.html" assert.Equal(joinURL(base, target), base+"/"+target) // merge querystring base = "/b0/c0.html?a=1&b=2" target = "b1/c1.html?a=3" assert.Equal(joinURL(base, target), "/b0/c0.html/b1/c1.html?a=3&b=2") }
func TestParseTimeUnit(t *testing.T) { assert := assert.Assert(t) cases := []struct { in string want time.Duration }{ {"10", time.Nanosecond * 10}, {"100000", time.Nanosecond * 100 * 1000}, {"1s", time.Second * 1}, {"2s", time.Second * 2}, {"3m", time.Minute * 3}, {"4h", time.Hour * 4}, {"5d", time.Hour * 24 * 5}, {"6M", time.Hour * 24 * 30 * 6}, {"7y", time.Hour * 24 * 365 * 7}, } for _, c := range cases { assert.Equal( func() time.Duration { p, err := parseTimeUnit(c.in) assert.Nil(err) return p }(), c.want, ) } }
func Test_ConnectCounter(t *testing.T) { assert := assert.Assert(t) cc := new(ConnectCounter) assert.False(cc.incr(true), "f1") assert.False(cc.incr(true), "f2") assert.False(cc.incr(true), "f3") assert.True(cc.incr(true), "t") }
func TestPasswordGenerationWithWords(t *testing.T) { assert := assert.Assert(t) generator := NewPasswordGenerator("w24") assert.Equal(generator.length, 24) assert.Equal(generator.words, true) password := generator.generate() assert.True(len(password) >= 24, "length") }
func TestBoard_turn_switches_after_each_play(t *testing.T) { assert := assert.Assert(t) g := NewGame() assert.Equal(g.Turn(), byte(1)) g.Play(1) assert.Equal(g.Turn(), byte(2)) g.Play(1) assert.Equal(g.Turn(), byte(1)) }
func TestDuplicatedID(t *testing.T) { assert := assert.Assert(t) var yml string var err error yml = loadFile("config_duplicated_id.yml") _, err = kasi.ParseConfig(yml) assert.NotNil(err) }
func Test_C4Game(t *testing.T) { assert := assert.Assert(t) game := GameWrapper{NewGame()} g := minimax.Game(game) assert.NotNil(g, "not nil") game1 := game.Copy() game1.Play(1) assert.NotEqual(game, game1) }
func Test_HasWonUtilityFunction_returns_1_when_won(t *testing.T) { assert := assert.Assert(t) game := NewGame() wrapper := GameWrapper{game} game.SetRows("1110000") assert.Equal(HasWon(wrapper), 0) hasWon, _ := game.Play(4) // now it's player 2's turn, but 1 won assert.True(hasWon, "should have won") assert.Equal(HasWon(wrapper), 1) }
func TestEnv(t *testing.T) { assert := assert.Assert(t) yml := loadFile("config_env.yml") setting, err := kasi.ParseConfig(yml) assert.Equal(err, nil) assert.NotEqual(setting.Env.Hostname, "") assert.NotEqual(setting.Env.GOOS, "") }
func TestPasswordGeneration(t *testing.T) { assert := assert.Assert(t) generator := NewPasswordGenerator("lunc20") assert.Equal(generator.length, 20) password := generator.generate() assert.Equal(len(password), 20) assert.True(strings.ContainsAny(password, LOWERCASE), "missing characters") assert.True(strings.ContainsAny(password, UPPERCASE), "missing characters") assert.True(strings.ContainsAny(password, NUMBERS), "missing characters") assert.True(strings.ContainsAny(password, CHARACTERS), "missing characters") }
func Test_Solve_avert_disaster(t *testing.T) { assert := assert.Assert(t) game := NewGame() game.SetRows( "2000000", "2000000", "1110000") game.turn = p2 move := Solve(game, 2) assert.Equal(move, 4) }
func TestHostnamesWithString(t *testing.T) { assert := assert.Assert(t) yml := loadFile("config_hostnames_string.yml") setting, err := kasi.ParseConfig(yml) assert.Nil(err) assert.Equal(len(setting.Services), 1) assert.Equal(len(setting.Services[0].Hostnames), 1) assert.Equal(setting.Services[0].Hostnames[0], "my0.github.com") }
func Test_Copy(t *testing.T) { assert := assert.Assert(t) g := NewGame() g.SetRows("1212000") g1 := g.Copy() g1.Play(5) assert.Equal(g.turn, p1) assert.Equal(g1.turn, p2) assert.Equal(g.board[0], []byte{1, 2, 1, 2, 0, 0, 0}) assert.Equal(g1.board[0], []byte{1, 2, 1, 2, 1, 0, 0}) }
func TestMinimax(t *testing.T) { assert := assert.Assert(t) // p1 [--p2--] [------p1------] game := NewHeapGame(0, 0, 1, 0, 1, -1, 0, 0, 1, -1, 0, 1, 2, 0, 1) fmt.Println(game.utility) // best move: 1 solver := &Solver{HeapUtilityFunction, 2} move, utility := solver.Solve(game) moveInt := move.(int) assert.Equal(moveInt, 1) assert.Equal(utility, 1) }
func Test_Play_returns_true_when_wins_left_diagonal(t *testing.T) { assert := assert.Assert(t) g := NewGame() g.SetRows( "0000000", "0001200", "0012200", "0121200") win, err := g.Play(5) assert.Nil(err, "error after player 1 played final piece") assert.True(win, "player 1 should have won") }
func Test_LegalMoves(t *testing.T) { assert := assert.Assert(t) g := NewGame() assert.Equal(g.LegalMoves(), []int{1, 2, 3, 4, 5, 6, 7}) for row := 0; row < ROWS/2; row++ { g.Play(1) g.Play(1) g.Play(4) g.Play(4) } assert.Equal(g.LegalMoves(), []int{2, 3, 5, 6, 7}) }
func Test_Play_returns_true_when_wins_in_row(t *testing.T) { assert := assert.Assert(t) g := NewGame() players := []int{1, 2} for i := 1; i < 4; i++ { for _, p := range players { won, err := g.Play(i) assert.False(won, "player %v should not have won", p) assert.Nil(err, "error after player %d played", p) } } won, err := g.Play(4) assert.Nil(err, "error when player 1 played final piece") assert.True(won, "player 1 should have won") }
func TestHostnamesWithStrings(t *testing.T) { assert := assert.Assert(t) yml := loadFile("config_hostnames_strings.yml") setting, err := kasi.ParseConfig(yml) assert.Nil(err) assert.Equal(len(setting.Services), 1) assert.Equal(len(setting.Services[0].Hostnames), 2) for _, h := range []string{"my0.github.com", "my1.github.com"} { if sort.SearchStrings(setting.Services[0].Hostnames, h) == 2 { t.Error("failed to parse hostnames") } } }
func TestID(t *testing.T) { assert := assert.Assert(t) var yml string var setting *kasi_conf.CoreSetting var err error yml = loadFile("config_check_id.yml") setting, err = kasi.ParseConfig(yml) assert.Equal(err, nil) assert.Equal(setting.Services[0].GetID(), "this-is-service-id") assert.Equal(setting.Services[0].Endpoints[0].GetID(), "findme") assert.Equal(len(setting.Services[0].Endpoints[1].GetID()), 36) assert.Equal(setting.Services[0].Endpoints[1].GetID(), setting.Services[0].Endpoints[1].GetID()) }
func TestBindInheritFromDefault(t *testing.T) { assert := assert.Assert(t) yml := loadFile("conf_default_bind.yml") setting, err := kasi.ParseConfig(yml) assert.Equal(err, nil) cases := map[string]*net.TCPAddr{ "a.com": setting.GetDefaultSetting().Bind, "b.com": &net.TCPAddr{Port: 90}, } for _, service := range setting.Services { assert.Equal(cases[service.Hostnames[0]], service.Bind) } }
func Test_Solve_aver_vertical_disaster(t *testing.T) { assert := assert.Assert(t) game := NewGame() game.SetRows( "1000000", "2010000", "2211000", "2211120") over, _ := game.Play(2) assert.False(over, "wtf") // game.turn = p2 // move := SolveVerbose(game, 2) // assert.Equal(move, 3) }
func TestSetRows_sets_rows(t *testing.T) { assert := assert.Assert(t) g := NewGame() g.SetRows( " 1112", " 11212") expectedBoard := [][]byte{ {0, 0, 1, 1, 2, 1, 2}, {0, 0, 0, 1, 1, 1, 2}, {0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0}} assert.Equal(g.board, expectedBoard) }
func TestTimeout(t *testing.T) { assert := assert.Assert(t) yml := loadFile("config_timeout.yml") setting, err := kasi.ParseConfig(yml) assert.Equal(err, nil) cases := map[string]time.Duration{ "a.com": time.Second * 9, "b.com": time.Second * 0, "c.com": setting.GetDefaultSetting().Timeout, "d.com": setting.GetDefaultSetting().Timeout, } for _, service := range setting.Services { assert.Equal(service.Timeout, cases[service.Hostnames[0]]) } }
func TestBind(t *testing.T) { assert := assert.Assert(t) yml := loadFile("config_multiple_bind.yml") setting, err := kasi.ParseConfig(yml) assert.Equal(err, nil) cases := map[string]*net.TCPAddr{ "a.com": &net.TCPAddr{Port: 8000}, "b.com": &net.TCPAddr{Port: 8000}, "c.com": &net.TCPAddr{Port: 8000}, "d.com": &net.TCPAddr{IP: net.ParseIP("127.0.0.1"), Port: 8000}, } for _, service := range setting.Services { assert.Equal(cases[service.Hostnames[0]], service.Bind) } }
func TestEndpoints(t *testing.T) { assert := assert.Assert(t) var yml string var setting *kasi_conf.CoreSetting var err error yml = loadFile("config_simple_endpoint0.yml") setting, err = kasi.ParseConfig(yml) assert.Nil(err) assert.Equal(len(setting.Services[0].Endpoints), 1) yml = loadFile("config_simple_endpoint1.yml") setting, err = kasi.ParseConfig(yml) assert.Nil(err) assert.Equal(len(setting.Services[0].Endpoints), 2) }
func TestIsPath(t *testing.T) { assert := assert.Assert(t) cases := []struct { in string want bool }{ {"/a/", true}, {"://a/", true}, {"http://a.com/", false}, {"p://a.com/", false}, } for _, c := range cases { assert.Equal( isPath(c.in), c.want, ) } }