Beispiel #1
0
func TestInitGitoliteRepo(t *testing.T) {
	GitoliteHostTest(t, func(t *testutil.T, host *GitoliteHost) {
		if privateKey, publicKey, err := testutil.CreateKeyPair(); err != nil {
			t.ErrorNow(err)
		} else if file, err := ioutil.TempFile(os.TempDir(), "battlecode_private_key"); err != nil {
			t.ErrorNow(err)
		} else if _, err := file.WriteString(privateKey); err != nil {
			t.ErrorNow(err)
		} else {
			file.Close()
			defer os.Remove(file.Name())
			if err := host.InitRepository("foo", map[string]int64{"foo": 1}, map[int64]string{1: publicKey}); err != nil {
				t.ErrorNow(err)
			}
			defer host.DeleteRepository("foo")
			repoURL := host.RepositoryURL("foo")
			if repo, err := (TempRemote{}).CheckoutRepositoryWithKeyFile(repoURL, file.Name()); err != nil {
				t.ErrorNow(err)
			} else {
				defer repo.Delete()
				CheckDirectoryContent(t, repo.Dir(), []string{".git"})
			}
		}
	})
}
Beispiel #2
0
func TestUnescaptedParsingFails(t *testing.T) {
	ServerTest(t, func(t *testutil.T, server *ServerState) {
		if r := sendPostExpectStatus(t, server, http.StatusInternalServerError, "/register", strings.NewReader("name=NameFoo&category="+string(tournament.CategoryTest)+"&public_key="+UnescapedSamplePublicKey)); Json(t, r).Key("error").Key("message").String() != "Invalid Public Key" {
			t.ErrorNow("expected 'Invalid Public Key'")
		}
	})
}
Beispiel #3
0
func TestCategories(t *testing.T) {
	ServerTest(t, func(t *testutil.T, server *ServerState) {
		if r := sendGet(t, server, "/categories"); !compareStringsUnordered(Json(t, r).Key("data").Key("categories").Array(), []string{string(tournament.CategoryBattlecode2014), string(tournament.CategoryBattlecode2015), string(tournament.CategoryBattlecode2016)}) {
			t.ErrorNow("expected 3 categories", r)
		}
	})
}
Beispiel #4
0
func TestSubmitPlayerNameError(t *testing.T) {
	ServerTest(t, func(t *testutil.T, server *ServerState) {
		if r := sendJSONPostExpectStatus(t, server, http.StatusInternalServerError, "/submit", map[string]string{"name": "NameFoo", "category": string(tournament.CategoryTest), "commit_hash": SampleCommitHash}); Json(t, r).Key("error").Key("message").String() != "Unknown player" {
			t.ErrorNow(r, "expected 'Unknown player'")
		}
	})
}
Beispiel #5
0
func TestSubmitCommitHashError(t *testing.T) {
	ServerTest(t, func(t *testutil.T, server *ServerState) {
		sendJSONPost(t, server, "/register", map[string]string{"name": "NameFoo", "public_key": SamplePublicKey, "category": string(tournament.CategoryTest)})
		if r := sendJSONPostExpectStatus(t, server, http.StatusInternalServerError, "/submit", map[string]string{"name": "NameFoo", "category": string(tournament.CategoryTest), "commit_hash": "InvalidCommitHash"}); Json(t, r).Key("error").Key("message").String() != "Invalid commit hash" {
			t.ErrorNow(r, "expected 'Unknown player'")
		}
	})
}
Beispiel #6
0
func TestInitExitingLocalRepoFails(t *testing.T) {
	LocalDirHostTest(t, func(t *testutil.T, local *LocalDirHost) {
		t.CheckError(local.InitRepository("foo", nil, nil))
		if err := local.InitRepository("foo", nil, nil); err == nil {
			t.FailNow()
		}
	})
}
func TestCreateExistingMapError(t *testing.T) {
	TournamentTest(t, func(t *testutil.T, tm *Tournament) {
		t.CheckError(tm.CreateMap("NameFoo", "SourceFoo", CategoryTest))
		if err := tm.CreateMap("NameFoo", "SourceFoo", CategoryTest); err == nil {
			t.ErrorNow("expected error")
		}
	})
}
func TestCreateDuplicateUserError(t *testing.T) {
	TournamentTest(t, func(t *testutil.T, tm *Tournament) {
		if _, err := tm.CreateUser("NameFoo", "PublicKeyFoo", CategoryTest); err != nil {
			t.ErrorNow(err)
		}
		if _, err := tm.CreateUser("NameFoo", "PublicKeyFoo", CategoryTest); err == nil {
			t.ErrorNow("expected error")
		}
	})
}
func TestCreateExistingKey(t *testing.T) {
	TournamentTest(t, func(t *testutil.T, tm *Tournament) {
		if _, err := tm.CreateUser("NameFoo", "PublicKeyFoo", CategoryTest); err != nil {
			t.ErrorNow(err)
		}
		if _, err := tm.CreateUser("NameBar", "PublicKeyFoo", CategoryTest); err != nil {
			t.ErrorNow(err)
		}
	})
}
Beispiel #10
0
func TestRunLatestMatches(t *testing.T) {
	TournamentTest(t, func(t *testutil.T, tm *Tournament) {
		runLatestMatches(t, tm)
		if matches, err := tm.ListMatches(CategoryTest); err != nil {
			t.ErrorNow(err)
		} else if len(matches) != 18 {
			t.ErrorNow("Expected 1 got", len(matches))
		}
	})
}
Beispiel #11
0
func TestRegisterJSON(t *testing.T) {
	ServerTest(t, func(t *testutil.T, server *ServerState) {
		r := sendJSONPost(t, server, "/register", map[string]string{"name": "NameFoo", "public_key": SamplePublicKey, "category": string(tournament.CategoryTest)})
		if Json(t, r).Key("data").Key("name").String() != "NameFoo" {
			t.FailNow()
		}
		if Json(t, r).Key("data").Key("public_key").String() != SamplePublicKey {
			t.FailNow()
		}
	})
}
Beispiel #12
0
func TestRegisterQuery(t *testing.T) {
	ServerTest(t, func(t *testutil.T, server *ServerState) {
		r := sendPost(t, server, "/register?name=NameFoo&category="+string(tournament.CategoryTest)+"&public_key="+url.QueryEscape(SamplePublicKey), nil)
		if Json(t, r).Key("data").Key("name").String() != "NameFoo" {
			t.FailNow()
		}
		if Json(t, r).Key("data").Key("public_key").String() != SamplePublicKey {
			t.FailNow()
		}
	})
}
Beispiel #13
0
func TestVersion(t *testing.T) {
	ServerTest(t, func(t *testutil.T, server *ServerState) {
		r := sendGet(t, server, "/version")
		if r["schemaVersion"] == "" {
			t.FailNow()
		}
		if r["sourceVersion"] == "" {
			t.FailNow()
		}
	})
}
Beispiel #14
0
func TestShutdown(t *testing.T) {
	ServerTest(t, func(t *testutil.T, server *ServerState) {
		go server.Serve()
		//Race condition of server not starting
		time.Sleep(time.Millisecond)
		r := sendPost(t, server, "/shutdown", nil)
		if r["shutdown"] == "" {
			t.FailNow()
		}
		sendPostExpectStatus(t, server, http.StatusInternalServerError, "/shutdown", nil)
	})
}
Beispiel #15
0
func TestCheckoutRepository(t *testing.T) {
	LocalDirHostTest(t, func(t *testutil.T, host *LocalDirHost) {
		t.CheckError(host.InitRepository("foo", nil, nil))
		repoURL := host.RepositoryURL("foo")
		if repo, err := (TempRemote{}).CheckoutRepository(repoURL); err != nil {
			t.ErrorNow(err)
		} else {
			defer repo.Delete()
			CheckDirectoryContent(t, repo.Dir(), []string{".git"})
		}

	})
}
Beispiel #16
0
func TestListCategories(t *testing.T) {
	TournamentTest(t, func(t *testutil.T, tm *Tournament) {
		categories := tm.ListCategories()
		if len(categories) != 3 {
			t.ErrorNowf("expected 3 category, got %v", len(categories))
		}
		as := []string{}
		bs := []string{string(CategoryBattlecode2014), string(CategoryBattlecode2015), string(CategoryBattlecode2016)}
		for _, category := range categories {
			as = append(as, string(category))
		}
		t.CompareStringsUnsorted(as, bs)
	})
}
Beispiel #17
0
func TestSubmitCommit(t *testing.T) {
	TournamentTest(t, func(t *testutil.T, tm *Tournament) {
		if _, err := tm.CreateUser("NameFoo", "PublicKeyFoo", CategoryTest); err != nil {
			t.ErrorNow(err)
		}
		t.CheckError(tm.SubmitCommit("NameFoo", CategoryTest, "abcdef", time.Now()))
		if commits, err := tm.ListCommits("NameFoo", CategoryTest); err != nil {
			t.ErrorNow(err)
		} else if len(commits) != 1 {
			t.ErrorNow(len(commits), "but expected", 1)
		} else if commits[0] != "abcdef" {
			t.ErrorNow(commits[0], "but expected", "abcdef")
		}
	})
}
Beispiel #18
0
func TestMaps(t *testing.T) {
	ServerTest(t, func(t *testutil.T, server *ServerState) {
		if r := sendGet(t, server, "/maps?category="+string(tournament.CategoryTest)); Json(t, r).Key("data").Key("maps").Len() > 0 {
			t.Error("expected no maps", r)
			t.FailNow()
		}
		sendJSONPost(t, server, "/map/create", map[string]string{"name": "NameFoo", "source": "SourceFoo", "category": string(tournament.CategoryTest)})
		if r := sendGet(t, server, "/maps?category="+string(tournament.CategoryTest)); !compareStrings(Json(t, r).Key("data").Key("maps").Array(), []string{"NameFoo"}) {
			t.Error("expected single player NameFoo", r)
			t.FailNow()
		}
		sendJSONPost(t, server, "/map/create", map[string]string{"name": "NameBar", "source": "SourceBar", "category": string(tournament.CategoryTest)})
		if r := sendGet(t, server, "/maps?category="+string(tournament.CategoryTest)); !compareStringsUnordered(Json(t, r).Key("data").Key("maps").Array(), []string{"NameFoo", "NameBar"}) {
			t.ErrorNow("expected two maps NameFoo, NameBar", r)
		}
	})
}
Beispiel #19
0
func TestDeleteLocalRepo(t *testing.T) {
	LocalDirHostTest(t, func(t *testutil.T, local *LocalDirHost) {
		t.CheckError(local.InitRepository("foo", nil, nil))
		t.CheckError(local.DeleteRepository("foo"))
		repoURL := local.RepositoryURL("foo")
		if _, err := os.Stat(repoURL); err == nil {
			t.FailNow()
		} else if !os.IsNotExist(err) {
			t.ErrorNow(err)
		}
	})
}
Beispiel #20
0
func TestForkLocalRepo(t *testing.T) {
	LocalDirHostTest(t, func(t *testutil.T, local *LocalDirHost) {
		t.CheckError(local.InitRepository("foo", nil, nil))
		t.CheckError(local.ForkRepository("foo", "bar", nil, nil))
		repoURL := local.RepositoryURL("bar")
		if stat, err := os.Stat(repoURL); err != nil {
			t.ErrorNow(err)
		} else if !stat.IsDir() {
			t.ErrorNowf("%s is not a directory", repoURL)
		} else {
			CheckDirectoryContent(t, repoURL, []string{"HEAD", "branches", "config", "description", "hooks", "info", "objects", "refs"})
		}
	})
}
Beispiel #21
0
func TestDeleteGitoliteRepo(t *testing.T) {
	GitoliteHostTest(t, func(t *testutil.T, host *GitoliteHost) {
		t.CheckError(createRepo(host, "foo"))
		t.CheckError(host.DeleteRepository("foo"))
		cmd := exec.Command("ssh", "-v", "-v", "-i", host.SSHKey, host.User+"@"+host.InternalHostname, "[[ ! -d repositories/foo.git ]]")
		t.CheckError(cmd.Run())
	})
}
Beispiel #22
0
func TestPublicKeyParsing(t *testing.T) {
	padding := strings.Repeat("1", 256)
	for _, key := range []string{
		"ssh-rsa AAAA1234" + padding,
		"ssh-rsa AAAA1234" + padding + " [email protected]",
		"ssh-rsa AAAA1234" + padding + " other text",
		"ssh-rsa AAAA1234" + padding + "\n",
		"ssh-rsa AAAA1234" + padding + " other text\n",
	} {
		if !PublicKeyRegex.MatchString(key) {
			t.Errorf("'%v' is not a public key", key)
		}
	}
	for _, key := range []string{
		"ssh-rsa AAAA1234",
		"ssh-rsa AAAA1234" + " [email protected]",
		"ssh-rsa AAAA12!34" + " [email protected]",
	} {
		if PublicKeyRegex.MatchString(key) {
			t.Errorf("Expected failure for invalid key '%v'", key)
		}
	}
}
Beispiel #23
0
func TestSimilarPublicKeys(t *testing.T) {
	ServerTest(t, func(t *testutil.T, server *ServerState) {
		r := sendPost(t, server, "/register", strings.NewReader("name=NameFoo&category="+string(tournament.CategoryTest)+"&public_key="+url.QueryEscape(SamplePublicKey)))
		if Json(t, r).Key("data").Key("name").String() != "NameFoo" {
			t.FailNow()
		}
		if Json(t, r).Key("data").Key("public_key").String() != SamplePublicKey {
			t.FailNow()
		}
		r = sendPost(t, server, "/register", strings.NewReader("name=NameBar&category="+string(tournament.CategoryTest)+"&public_key="+url.QueryEscape(SimilarPublicKey)))
		if Json(t, r).Key("data").Key("name").String() != "NameBar" {
			t.FailNow()
		}
		if Json(t, r).Key("data").Key("public_key").String() != SimilarPublicKey {
			t.FailNow()
		}
	})
}
Beispiel #24
0
func TestSubmit(t *testing.T) {
	ServerTest(t, func(t *testutil.T, server *ServerState) {
		sendJSONPost(t, server, "/register", map[string]string{"name": "NameFoo", "public_key": SamplePublicKey, "category": string(tournament.CategoryTest)})
		if r := sendJSONPost(t, server, "/submit", map[string]string{"name": "NameFoo", "category": string(tournament.CategoryTest), "commit_hash": SampleCommitHash}); Json(t, r).Key("data").Key("name").String() != "NameFoo" {
			t.ErrorNow(r["name"], " expected ", "NameFoo")
		} else if Json(t, r).Key("data").Key("category").String() != string(tournament.CategoryTest) {
			t.ErrorNow(r["category"], " expected ", string(tournament.CategoryTest))
		} else if Json(t, r).Key("data").Key("commit_hash").String() != SampleCommitHash {
			t.ErrorNow(r["commit_hash"], " expected ", SampleCommitHash)
		}
	})
}
Beispiel #25
0
func TestCreateMatch(t *testing.T) {
	TournamentTest(t, func(t *testutil.T, tm *Tournament) {
		p1 := Submission{"p1", "c1"}
		p2 := Submission{"p2", "c2"}
		if id, err := tm.CreateMatch(CategoryTest, "MapFoo", p1, p2, time.Now()); err != nil {
			t.FailNow()
		} else if result, err := tm.GetMatchResult(id); err != nil {
			t.ErrorNow(err)
		} else if result != MatchResultInProgress {
			t.FailNow()
		}
	})
}
Beispiel #26
0
func TestPlayers(t *testing.T) {
	ServerTest(t, func(t *testutil.T, server *ServerState) {
		if r := sendGet(t, server, "/players"); Json(t, r).Key("data").Key("players").Len() > 0 {
			t.ErrorNow("expected no players", r)
		}
		sendJSONPost(t, server, "/register", map[string]string{"name": "NameFoo", "public_key": SamplePublicKey, "category": string(tournament.CategoryTest)})
		if r := sendGet(t, server, "/players"); !compareStrings(Json(t, r).Key("data").Key("players").Array(), []string{"NameFoo"}) {
			t.ErrorNow("expected single player NameFoo", r)
		}
		sendJSONPost(t, server, "/register", map[string]string{"name": "NameBar", "public_key": SamplePublicKey2, "category": string(tournament.CategoryTest)})
		if r := sendGet(t, server, "/players"); !compareStringsUnordered(Json(t, r).Key("data").Key("players").Array(), []string{"NameFoo", "NameBar"}) {
			t.ErrorNow("expected two players NameFoo, NameBar", r)
		}
	})
}
Beispiel #27
0
func TestCommits(t *testing.T) {
	ServerTest(t, func(t *testutil.T, server *ServerState) {
		sendJSONPost(t, server, "/register", map[string]string{"name": "NameFoo", "public_key": SamplePublicKey, "category": string(tournament.CategoryTest)})
		if r := sendGet(t, server, "/commits?name=NameFoo&category=General"); Json(t, r).Key("data").Key("commits").Len() > 0 {
			t.ErrorNow("expected no commits", r)
		}
		sendJSONPost(t, server, "/submit", map[string]string{"name": "NameFoo", "category": "General", "commit_hash": "abcdef"})
		if r := sendGet(t, server, "/commits?name=NameFoo&category=General"); !compareStringsUnordered(Json(t, r).Key("data").Key("commits").Array(), []string{"abcdef"}) {
			t.ErrorNow("expected single commit abcdef", r)
		}
		sendJSONPost(t, server, "/submit", map[string]string{"name": "NameFoo", "category": "General", "commit_hash": "012345"})
		if r := sendGet(t, server, "/commits?name=NameFoo&category=General"); !compareStringsUnordered(Json(t, r).Key("data").Key("commits").Array(), []string{"abcdef", "012345"}) {
			t.ErrorNow("expected two commits abcdef, 012345", r)
		}
	})
}
Beispiel #28
0
func TestUpdateMatch(t *testing.T) {
	TournamentTest(t, func(t *testutil.T, tm *Tournament) {
		p1 := Submission{"p1", "c1"}
		p2 := Submission{"p2", "c2"}
		if id, err := tm.CreateMatch(CategoryTest, "MapFoo", p1, p2, time.Now()); err != nil {
			t.ErrorNow(err)
		} else {
			t.CheckError(tm.UpdateMatch(CategoryTest, "MapFoo", p1, p2, time.Now(), MatchResultWinA, []byte("LogFoo")))
			if result, err := tm.GetMatchResult(id); err != nil {
				t.ErrorNow(t, err)
			} else if result != MatchResultWinA {
				t.ErrorNow(result, " expected ", MatchResultWinA)
			} else if replay, err := tm.GetMatchReplayRaw(id); err != nil {
				t.ErrorNow(err)
			} else if string(replay) != "LogFoo" {
				t.ErrorNow(replay, " expected LogFoo")
			}
		}
	})
}
Beispiel #29
0
func TestCalculateLeaderboard(t *testing.T) {
	TournamentTest(t, func(t *testutil.T, tm *Tournament) {
		runLatestMatches(t, tm)
		rank := LeaderboardStats{12, 6, 0, 6}
		if err := tm.CalculateLeaderboard(CategoryTest); err != nil {
			t.ErrorNow(err)
		} else if ranks, matches, err := tm.GetLeaderboard(CategoryTest); err != nil {
			t.ErrorNow(err)
		} else if len(ranks) != 3 {
			t.ErrorNow("Expected 3 ranks", len(ranks))
		} else if v, _ := ranks["Name1"]; v != rank {
			t.ErrorNow("Unexpected rank", ranks["Name1"])
		} else if ranks["Name2"] != rank {
			t.ErrorNow("Unexpected rank", ranks["Name2"])
		} else if ranks["Name3"] != rank {
			t.ErrorNow("Unexpected rank", ranks["Name3"])
		} else if len(matches) != 18 {
			t.ErrorNow("Expected 18 matches", len(matches))
		}

	})
}
Beispiel #30
0
func TestRunMatch(t *testing.T) {
	TournamentTest(t, func(t *testutil.T, tm *Tournament) {
		p1 := Submission{"p1", "c1"}
		p2 := Submission{"p2", "c2"}
		t.CheckError(tm.CreateMap("MapFoo", "SourceFoo", CategoryTest))
		if id, result, err := tm.RunMatch(CategoryTest, "MapFoo", p1, p2, SystemClock()); err != nil {
			t.ErrorNow(err)
		} else if result != "WinA" {
			t.ErrorNowf("Expected WinA not %v\n", result)
		} else if result2, err := tm.GetMatchResult(id); err != nil {
			t.ErrorNow(err)
		} else if result != result2 {
			t.ErrorNowf("Expected %v not %v\n", result, result2)
		} else if matches, err := tm.ListMatches(CategoryTest); err != nil {
			t.ErrorNow(err)
		} else if len(matches) != 1 {
			t.ErrorNowf("Expected 1 match not %v\n", len(matches))
		} else if matches[0].Result != result {
			t.ErrorNowf("Expected %v not %v\n", result, matches[0].Result)
		}
	})
}