Ejemplo n.º 1
0
func TestGetServices(t *testing.T) {

	db, err := openDB()

	defer db.Close()

	svc := steward.Service{ID: -1, Name: "lol", Port: 10,
		CheckerPath: "/test", UDP: false}

	const services_amount int = 5

	for i := 0; i < services_amount; i++ {
		svc.Port = i
		err = steward.AddService(db.db, svc)
	}

	services, err := steward.GetServices(db.db)
	if err != nil {
		log.Fatalln("Get services fail:", err)
	}

	if len(services) != services_amount {
		log.Fatalln("Get services moar than add")
	}

	for i := 0; i < len(services); i++ {
		svc.ID = i + 1
		svc.Port = i
		if services[i] != svc {
			log.Fatalln("Get service", services[i], "instead", svc)
		}
	}
}
Ejemplo n.º 2
0
func fillTestServices(db *sql.DB) {
	for _, service := range []string{"Foo", "Bar", "Baz", "Boo"} {

		err := steward.AddService(db,
			steward.Service{ID: -1, Name: service, Port: 8080,
				CheckerPath: "", UDP: false})
		if err != nil {
			log.Fatalln("Add service failed:", err)
		}
	}
}
Ejemplo n.º 3
0
func TestAddService(t *testing.T) {

	db, err := openDB()

	defer db.Close()

	svc := steward.Service{ID: -1, Name: "lol", Port: 10,
		CheckerPath: "/test", UDP: false}

	err = steward.AddService(db.db, svc)
	if err != nil {
		log.Fatalln("Add service fail:", err)
	}
}
Ejemplo n.º 4
0
func reinitDatabase(db *sql.DB, config config.Config) {

	var err error

	if config.Database.SafeReinit {
		if time.Now().After(config.Pulse.Start.Time) {
			log.Fatalln("Reinit after start not allowed")
		}
	}

	log.Println("Reinit database")

	log.Println("Clean database")

	steward.CleanDatabase(db)

	for _, team := range config.Teams {

		log.Println("Add team", team.Name)

		_, err = steward.AddTeam(db, team)
		if err != nil {
			log.Fatalln("Add team failed:", err)
		}
	}

	for _, svc := range config.Services {

		var network string
		if svc.UDP {
			network = "udp"
		} else {
			network = "tcp"
		}

		log.Printf("Add service %s (%s)\n", svc.Name, network)

		err = steward.AddService(db, svc)
		if err != nil {
			log.Fatalln("Add service failed:", err)
		}
	}
}
Ejemplo n.º 5
0
func TestGame(*testing.T) {

	db, err := openDB()
	if err != nil {
		log.Fatalln("Open database fail:", err)
	}

	min_port_num := 1024
	max_port_num := 65535

	rand.Seed(time.Now().UnixNano())

	port := min_port_num + rand.Intn(max_port_num-min_port_num)

	log.Println("Use port", port)

	service_path := "../checker/python-api/dummy_service.py"

	svc := newDummyService(service_path, port)
	svc.Stop() // kill
	svc.Start()

	time.Sleep(time.Second) // wait for service init

	defer svc.Stop()

	priv, err := vexillary.GenerateKey()
	if err != nil {
		log.Fatalln("Generate key fail:", err)
	}

	for index, team := range []string{"FooTeam", "BarTeam", "BazTeam"} {

		// just trick for bypass UNIQUE team subnet
		subnet := fmt.Sprintf("127.%d.0.1/24", index)

		vulnbox := fmt.Sprintf("127.0.%d.3", index)

		t := steward.Team{ID: -1, Name: team, Subnet: subnet,
			Vulnbox: vulnbox}

		_, err = steward.AddTeam(db.db, t)
		if err != nil {
			log.Fatalln("Add team failed:", err)
		}
	}

	checker_path := "../checker/python-api/dummy_checker.py"

	for _, service := range []string{"Foo", "Bar", "Baz"} {

		err = steward.AddService(db.db,
			steward.Service{ID: -1, Name: service, Port: port,
				CheckerPath: checker_path, UDP: false})
		if err != nil {
			log.Fatalln("Add service failed:", err)
		}
	}

	round_len := 30 * time.Second
	timeout_between_check := 10 * time.Second

	game, err := pulse.NewGame(db.db, priv, round_len, timeout_between_check)

	defer game.Over()

	end_time := time.Now().Add(time.Minute + 10*time.Second)

	err = game.Run(end_time)

	if err != nil {
		log.Fatalln("Game error:", err)
	}

	for round := 1; round <= 2; round++ {

		for team_id := 1; team_id <= 3; team_id++ {

			res, err := steward.GetRoundResult(db.db, team_id, round)
			if err != nil {
				log.Fatalf("Get round %d result fail: %s",
					round, err)
			}

			if res.DefenceScore != float64(round*2) {
				log.Fatalln("Invalid defence score")
			}

			if res.AttackScore != 0 {
				log.Fatalln("Invalid attack score")
			}
		}
	}
}
Ejemplo n.º 6
0
func TestFlagsWork(t *testing.T) {

	db, err := openDB()
	if err != nil {
		log.Fatalln("Open database failed:", err)
	}

	defer db.Close()

	min_port_num := 1024
	max_port_num := 65535

	rand.Seed(time.Now().UnixNano())

	port := min_port_num + rand.Intn(max_port_num-min_port_num)

	log.Println("Use port", port)

	service := newDummyService("python-api/dummy_service.py", port)
	service.Stop() // if already run

	priv, err := vexillary.GenerateKey()
	if err != nil {
		log.Fatalln("Generate key failed:", err)
	}

	fillTestTeams(db.db)

	checker_path := "python-api/dummy_checker.py"

	for _, service := range []string{"Foo", "Bar", "Baz"} {

		err = steward.AddService(db.db,
			steward.Service{ID: -1, Name: service, Port: port,
				CheckerPath: checker_path, UDP: false})
		if err != nil {
			log.Fatalln("Add service failed:", err)
		}
	}

	round, err := steward.NewRound(db.db, time.Minute)
	if err != nil {
		log.Fatalln("Create new round failed:", err)
	}

	teams, err := steward.GetTeams(db.db)
	if err != nil {
		log.Fatalln("Get teams failed:", err)
	}

	services, err := steward.GetServices(db.db)
	if err != nil {
		log.Fatalln("Get services failed:", err)
	}

	err = checker.PutFlags(db.db, priv, round, teams, services)
	if err != nil {
		log.Fatalln("Put flags failed:", err)
	}

	// No services -> all down
	checkServicesStatus(db.db, round, teams, services, steward.StatusDown)

	// Start service
	service.Start()

	time.Sleep(time.Second)

	service.BrokeLogic()

	err = checker.PutFlags(db.db, priv, round, teams, services)
	if err != nil {
		log.Fatalln("Put flags failed:", err)
	}

	checkServicesStatus(db.db, round, teams, services, steward.StatusMumble)

	service.RestoreLogic()

	time.Sleep(time.Second)

	round, err = steward.NewRound(db.db, time.Minute)
	if err != nil {
		log.Fatalln("Create new round failed:", err)
	}

	log.Println("Put flags to correct service...")

	err = checker.PutFlags(db.db, priv, round, teams, services)
	if err != nil {
		log.Fatalln("Put flags failed:", err)
	}

	checkServicesStatus(db.db, round, teams, services, steward.StatusUP)

	log.Println("Check flags of correct service...")

	err = checker.CheckFlags(db.db, round, teams, services)
	if err != nil {
		log.Fatalln("Check flags failed:", err)
	}

	checkServicesStatus(db.db, round, teams, services, steward.StatusUP)

	service.ClearFlags()

	log.Println("Check flags of service with removed flags...")

	err = checker.CheckFlags(db.db, round, teams, services)
	if err != nil {
		log.Fatalln("Check flags failed:", err)
	}

	checkServicesStatus(db.db, round, teams, services, steward.StatusCorrupt)

	log.Println("Stop service...")
	service.Stop()

	log.Println("Check flags of stopped service...")

	err = checker.CheckFlags(db.db, round, teams, services)
	if err != nil {
		log.Fatalln("Check flags failed:", err)
	}

	checkServicesStatus(db.db, round, teams, services, steward.StatusDown)
}