Fix edge cases for peer ID parsing

Fixes 
This commit is contained in:
Justin Li 2013-12-13 11:19:22 -05:00
parent 0ceefca9c1
commit 140a738162
4 changed files with 22 additions and 31 deletions

View file

@ -205,7 +205,7 @@ func (s Server) serveAnnounce(w http.ResponseWriter, r *http.Request) {
} else { } else {
// If they're leeching, prioritize giving them seeders // If they're leeching, prioritize giving them seeders
count += writeSeeders(w, user, torrent, numWant, compact) count += writeSeeders(w, user, torrent, numWant, compact)
count += writeLeechers(w, user, torrent, numWant - count, compact) count += writeLeechers(w, user, torrent, numWant-count, compact)
} }
if compact && peerCount != count { if compact && peerCount != count {

View file

@ -127,10 +127,15 @@ func validateUser(conn tracker.Conn, dir string) (*storage.User, error) {
// Takes a peer_id and returns a ClientID // Takes a peer_id and returns a ClientID
func parsePeerID(peerID string) (clientID string) { func parsePeerID(peerID string) (clientID string) {
if peerID[0] == '-' { length := len(peerID)
clientID = peerID[1:7] if length >= 6 {
} else { if peerID[0] == '-' {
clientID = peerID[0:6] if length >= 7 {
clientID = peerID[1:7]
}
} else {
clientID = peerID[0:6]
}
} }
return return
} }

View file

@ -14,7 +14,7 @@ type PeerClientPair struct {
} }
var ( var (
AzTestClients = []PeerClientPair{ TestClients = []PeerClientPair{
{"-AZ3034-6wfG2wk6wWLc", "AZ3034"}, {"-AZ3034-6wfG2wk6wWLc", "AZ3034"},
{"-AZ3042-6ozMq5q6Q3NX", "AZ3042"}, {"-AZ3042-6ozMq5q6Q3NX", "AZ3042"},
{"-BS5820-oy4La2MWGEFj", "BS5820"}, {"-BS5820-oy4La2MWGEFj", "BS5820"},
@ -33,15 +33,11 @@ var (
{"-A~0010-a9mn9DFkj39J", "A~0010"}, {"-A~0010-a9mn9DFkj39J", "A~0010"},
{"-UT2300-MNu93JKnm930", "UT2300"}, {"-UT2300-MNu93JKnm930", "UT2300"},
{"-UT2300-KT4310KT4301", "UT2300"}, {"-UT2300-KT4310KT4301", "UT2300"},
}
ShadowTestClients = []PeerClientPair{
{"T03A0----f089kjsdf6e", "T03A0-"}, {"T03A0----f089kjsdf6e", "T03A0-"},
{"S58B-----nKl34GoNb75", "S58B--"}, {"S58B-----nKl34GoNb75", "S58B--"},
{"M4-4-0--9aa757Efd5Bl", "M4-4-0"}, {"M4-4-0--9aa757Efd5Bl", "M4-4-0"},
}
OtherTestClients = []PeerClientPair{
{"AZ2500BTeYUzyabAfo6U", "AZ2500"}, // BitTyrant {"AZ2500BTeYUzyabAfo6U", "AZ2500"}, // BitTyrant
{"exbc0JdSklm834kj9Udf", "exbc0J"}, // Old BitComet {"exbc0JdSklm834kj9Udf", "exbc0J"}, // Old BitComet
{"FUTB0L84j542mVc84jkd", "FUTB0L"}, // Alt BitComet {"FUTB0L84j542mVc84jkd", "FUTB0L"}, // Alt BitComet
@ -53,29 +49,20 @@ var (
{"Q1-10-0-Yoiumn39BDfO", "Q1-10-"}, // Queen Bee Alt {"Q1-10-0-Yoiumn39BDfO", "Q1-10-"}, // Queen Bee Alt
{"346------SDFknl33408", "346---"}, // TorreTopia {"346------SDFknl33408", "346---"}, // TorreTopia
{"QVOD0054ABFFEDCCDEDB", "QVOD00"}, // Qvod {"QVOD0054ABFFEDCCDEDB", "QVOD00"}, // Qvod
{"", ""},
{"-", ""},
{"12345", ""},
{"-12345", ""},
{"123456", "123456"},
{"-123456", "123456"},
} }
) )
func TestParseAZClientID(t *testing.T) { func TestParseClientID(t *testing.T) {
for _, pair := range AzTestClients { for _, pair := range TestClients {
if pair.clientId != parsePeerID(pair.peerId) { if parsedId := parsePeerID(pair.peerId); parsedId != pair.clientId {
t.Error("PeerID incorrectly parsed", pair) t.Error("Incorrectly parsed peer ID", pair.peerId, "as", parsedId)
}
}
}
func TestParseShadowClientID(t *testing.T) {
for _, pair := range ShadowTestClients {
if pair.clientId != parsePeerID(pair.peerId) {
t.Error("PeerID incorrectly parsed", pair)
}
}
}
func TestParseOtherClientID(t *testing.T) {
for _, pair := range OtherTestClients {
if pair.clientId != parsePeerID(pair.peerId) {
t.Error("PeerID incorrectly parsed", pair)
} }
} }
} }

View file

@ -26,4 +26,3 @@ func (d *driver) New(conf *config.DataStore) tracker.Pool {
func init() { func init() {
tracker.Register("mock", &driver{}) tracker.Register("mock", &driver{})
} }