Beispiel #1
0
func TestGetAdvisoryScore(t *testing.T) {

	db, err := openDB()

	defer db.Close()

	team_id := 10
	advisory_text := "advisory text"
	score := 40
	amount := 5

	var ids []int

	for i := 0; i < amount; i++ {
		entry_text := fmt.Sprintf("%s%d", advisory_text, i)
		id, err := steward.AddAdvisory(db.db, team_id, entry_text)
		if err != nil {
			log.Fatalln("Add advisory failed:", err)
		}
		ids = append(ids, id)
		steward.ReviewAdvisory(db.db, id, score)
	}

	team_score, err := steward.GetAdvisoryScore(db.db, team_id)
	if err != nil {
		log.Fatalln("Get team advisory score fail:", err)
	}

	if team_score != score*amount {
		log.Fatalf("Team advisory score (%d) not equal of sum of "+
			"entries (%d)", team_score, score*amount)
	}
}
Beispiel #2
0
func TestGetAdvisories(t *testing.T) {

	db, err := openDB()

	defer db.Close()

	team_id := 10

	var adv1, adv2 steward.Advisory

	adv1.Text = "adv1_test"
	adv1.Score = 10
	adv1.Reviewed = true

	adv2.Text = "adv2_test"
	adv2.Score = 20
	adv2.Reviewed = true

	adv1.ID, _ = steward.AddAdvisory(db.db, team_id, adv1.Text)
	adv2.ID, _ = steward.AddAdvisory(db.db, team_id, adv2.Text)

	steward.ReviewAdvisory(db.db, adv1.ID, adv1.Score)
	steward.ReviewAdvisory(db.db, adv2.ID, adv2.Score)

	advisories, err := steward.GetAdvisories(db.db)
	if err != nil {
		log.Fatalln("Get all advisories failed:", err)
	}

	if len(advisories) != 2 {
		log.Fatalln("Get advisories more than added")
	}

	if time.Now().Sub(advisories[0].Timestamp) > 5*time.Second {
		log.Fatalln("Time must be ~ current:", advisories[0].Timestamp)
	}

	// No timestamp check
	adv1.Timestamp = advisories[0].Timestamp
	adv2.Timestamp = advisories[1].Timestamp

	if advisories[0] != adv1 || advisories[1] != adv2 {
		log.Fatalln("Added advisories broken")
	}

}
Beispiel #3
0
func TestHideUnhideAdvisory(*testing.T) {

	db, err := openDB()

	defer db.Close()

	adv := steward.Advisory{Text: "pony"}

	team_id := 10

	adv.ID, err = steward.AddAdvisory(db.db, team_id, adv.Text)
	if err != nil {
		log.Fatalln("Add advisory fail:", err)
	}

	advisories, err := steward.GetAdvisories(db.db)
	if err != nil {
		log.Fatalln("Get advisory fail:", err)
	}

	if len(advisories) != 1 {
		log.Fatalln("No added advisory")
	}

	err = steward.HideAdvisory(db.db, adv.ID, true)
	if err != nil {
		log.Fatalln("Hide advisory fail:", err)
	}

	advisories, err = steward.GetAdvisories(db.db)
	if err != nil {
		log.Fatalln("Get advisory fail:", err)
	}

	if len(advisories) != 0 {
		log.Fatalln("Hide advisory does not work")
	}

	err = steward.HideAdvisory(db.db, adv.ID, false)
	if err != nil {
		log.Fatalln("Hide advisory fail:", err)
	}

	advisories, err = steward.GetAdvisories(db.db)
	if err != nil {
		log.Fatalln("Get advisory fail:", err)
	}

	if len(advisories) != 1 {
		log.Fatalln("Unhide advisory does not work")
	}
}
Beispiel #4
0
func TestAddAdvisory(t *testing.T) {

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

	defer db.Close()

	_, err = steward.AddAdvisory(db.db, 10, "ololo")
	if err != nil {
		log.Fatalln("Add advisory failed:", err)
	}
}
Beispiel #5
0
func TestReviewAdvisory(t *testing.T) {

	db, err := openDB()

	defer db.Close()

	team_id := 10
	advisory_text := "advisory text"

	id, _ := steward.AddAdvisory(db.db, team_id, advisory_text)

	err = steward.ReviewAdvisory(db.db, id, 20)
	if err != nil {
		log.Fatalln("Review advisory fail:", err)
	}
}
Beispiel #6
0
func advisoryHandler(conn net.Conn, db *sql.DB) {

	addr := conn.RemoteAddr().String()

	defer conn.Close()

	round, err := steward.CurrentRound(db)
	if err != nil {
		log.Println("Get current round fail:", err)
		fmt.Fprint(conn, internalErrorMsg)
		return
	}

	roundEndTime := round.StartTime.Add(round.Len)

	if time.Now().After(roundEndTime) {
		fmt.Fprintln(conn, "Current contest not runned")
		return
	}

	fmt.Fprint(conn, "IBST.PSU CTF Advisory Receiver\n"+
		"Insert empty line for close\n"+
		"Input advisory: ")

	scanner := bufio.NewScanner(conn)
	var advisory string
	for scanner.Scan() {
		advisory += scanner.Text() + "\n"
		if len(advisory) > 2 {
			if advisory[len(advisory)-2:len(advisory)-1] == "\n" {
				// remove last newline
				advisory = advisory[:len(advisory)-1]
				break
			}
		}
	}

	httpGetRoot := "GET / HTTP/1.1"
	if len(advisory) > len(httpGetRoot) {
		if advisory[0:len(httpGetRoot)] == httpGetRoot {
			fmt.Fprintf(conn, "\n\nIt's not a HTTP server! "+
				"Use netcat for communication.")
			return
		}
	}

	r := `[ -~]`
	if hasUnacceptableSymbols(advisory, r) {
		fmt.Fprintf(conn, "Accept only %s\n", r)
		return
	}

	team, err := teamByAddr(db, addr)
	if err != nil {
		log.Println("\tGet team by ip failed:", err)
		fmt.Fprint(conn, invalidTeamMsg)
		return
	}

	_, err = steward.AddAdvisory(db, team.ID, advisory)
	if err != nil {
		log.Println("\tAdd advisory failed:", err)
		fmt.Fprint(conn, internalErrorMsg)
		return
	}

	fmt.Fprint(conn, "Accepted\n")
}