Ejemplo n.º 1
0
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)
}
Ejemplo n.º 2
0
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")
}
Ejemplo n.º 3
0
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")
		}
	}
}
Ejemplo n.º 4
0
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)
}
Ejemplo n.º 5
0
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")
}
Ejemplo n.º 6
0
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))
}
Ejemplo n.º 7
0
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)
}
Ejemplo n.º 8
0
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})
}
Ejemplo n.º 9
0
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")
}
Ejemplo n.º 10
0
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")
}
Ejemplo n.º 11
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)
}
Ejemplo n.º 12
0
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})
}
Ejemplo n.º 13
0
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())
}
Ejemplo n.º 14
0
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")
		}
	}
}
Ejemplo n.º 15
0
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,
		)
	}
}
Ejemplo n.º 16
0
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)
	}
}
Ejemplo n.º 17
0
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]])
	}
}
Ejemplo n.º 18
0
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, "")
}
Ejemplo n.º 19
0
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)
}
Ejemplo n.º 20
0
func TestGetEndpointByID(t *testing.T) {
	assert := assert.Assert(t)

	var yml string
	var setting *kasi_conf.CoreSetting
	var endpointSetting *kasi_conf.EndpointSetting
	var err error

	yml = loadFile("config_check_id.yml")
	setting, err = kasi.ParseConfig(yml)
	assert.Equal(err, nil)

	endpointSetting, err = setting.GetEndpointByID("findme")
	assert.Equal(endpointSetting, setting.Services[0].Endpoints[0])

	endpointSettingToFind := setting.Services[0].Endpoints[1]
	endpointSetting, err = setting.GetEndpointByID(endpointSettingToFind.GetID())
	assert.Equal(endpointSettingToFind, endpointSetting)
}
Ejemplo n.º 21
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)
	}
}
Ejemplo n.º 22
0
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)
}
Ejemplo n.º 23
0
// Make sure I wrote heap game correctly before I try to win it.
func TestHeapGame(t *testing.T) {
	assert := assert.Assert(t)
	assert.Nil(nil, "good")

	f := HeapUtilityFunction

	game := NewHeapGame(1, 2, 3, 4, 5, 6, 7, 8)
	assert.Equal(f(game), 1)

	legalMoves := game.LegalMoves()
	move := legalMoves[0]
	game1 := game.Copy()
	over, _ := game1.Play(move)
	assert.False(over, "game not yet over")
	assert.NotEqual(game, game1)
	assert.Equal(f(game1), 2)

	game2 := game1.Copy()
	over, _ = game2.Play(1)
	assert.Equal(f(game2), 5)
	assert.True(over, "game should be over")
}
Ejemplo n.º 24
0
func TestGetServicesByBind(t *testing.T) {
	assert := assert.Assert(t)

	var coreSetting *kasi_conf.CoreSetting

	// blank services
	coreSetting = &kasi_conf.CoreSetting{}
	services := coreSetting.GetServicesByBind()
	assert.Equal(len(services), 0)

	makeCoreSettingsWithPorts := func(ports ...int) *kasi_conf.CoreSetting {
		services := []*kasi_conf.ServiceSetting{}
		for _, port := range ports {
			service := kasi_conf.ServiceSetting{
				Bind: &net.TCPAddr{Port: port},
			}
			services = append(services, &service)
		}

		return &kasi_conf.CoreSetting{
			Services: services,
		}
	}

	// 2 services, which have different bind
	coreSetting = makeCoreSettingsWithPorts(80, 90)

	services = coreSetting.GetServicesByBind()
	assert.Equal(len(services), 2)

	// if they have same bind
	coreSetting = makeCoreSettingsWithPorts(80, 80)

	services = coreSetting.GetServicesByBind()
	assert.Equal(len(services), 1)
}
Ejemplo n.º 25
0
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)
}
Ejemplo n.º 26
0
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,
		)
	}
}
Ejemplo n.º 27
0
func TestToJson(t *testing.T) {
	assert := assert.Assert(t)

	simpleStruct := struct {
		fieldHdden  string
		FieldString string
		FieldInt    int
		FieldArray  []string
	}{
		fieldHdden:  "fieldHdden",
		FieldString: "FieldString",
		FieldInt:    9,
		FieldArray: []string{
			"item0",
			"item1",
		},
	}

	cases := []struct {
		in   interface{}
		want string
	}{
		{
			simpleStruct, `{
  "FieldString": "FieldString",
  "FieldInt": 9,
  "FieldArray": [
    "item0",
    "item1"
  ]
}`,
		},
	}

	for _, c := range cases {
		assert.Equal(ToJson(c.in), c.want)
	}
}
Ejemplo n.º 28
0
func TestSplitHostPort(t *testing.T) {
	assert := assert.Assert(t)

	cases := []struct {
		in   string
		want *net.TCPAddr
	}{
		{":80", &net.TCPAddr{Port: 80}},
		{"0.0.0.0:80", &net.TCPAddr{Port: 80}},
		{"*:80", &net.TCPAddr{Port: 80}},
		{"192.168.0.1:80", &net.TCPAddr{IP: net.ParseIP("192.168.0.1"), Port: 80}},
	}
	for _, c := range cases {
		assert.Equal(
			func() *net.TCPAddr {
				p, err := splitHostPort(c.in)
				assert.Nil(err)
				return p
			}(),
			c.want,
		)
	}
}
Ejemplo n.º 29
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")
}