Example #1
0
// TODO: Create a fake UDP tracker to make these requests to.
func TestAnnounceRandomInfoHash(t *testing.T) {
	if testing.Short() {
		t.SkipNow()
	}
	req := AnnounceRequest{
		Event: Stopped,
	}
	rand.Read(req.PeerId[:])
	rand.Read(req.InfoHash[:])
	wg := sync.WaitGroup{}
	for _, url := range []string{
		"udp://tracker.openbittorrent.com:80/announce",
		"udp://tracker.publicbt.com:80",
		"udp://tracker.istole.it:6969",
		"udp://tracker.ccc.de:80",
		"udp://tracker.open.demonii.com:1337",
	} {
		go func(url string) {
			defer wg.Done()
			tr, err := New(url)
			if err != nil {
				t.Fatal(err)
			}
			if err := tr.Connect(); err != nil {
				t.Log(err)
				return
			}
			resp, err := tr.Announce(&req)
			if err != nil {
				t.Logf("error announcing to %s: %s", url, err)
				return
			}
			if resp.Leechers != 0 || resp.Seeders != 0 || len(resp.Peers) != 0 {
				t.Fatal(resp)
			}
		}(url)
		wg.Add(1)
	}
	wg.Wait()
}
Example #2
0
func TestAnnounceRandomInfoHashThirdParty(t *testing.T) {
	if testing.Short() {
		// This test involves contacting third party servers that may have
		// unpreditable results.
		t.SkipNow()
	}
	req := AnnounceRequest{
		Event: Stopped,
	}
	rand.Read(req.PeerId[:])
	rand.Read(req.InfoHash[:])
	wg := sync.WaitGroup{}
	success := make(chan bool)
	fail := make(chan struct{})
	for _, url := range []string{
		"udp://tracker.openbittorrent.com:80/announce",
		"udp://tracker.publicbt.com:80",
		"udp://tracker.istole.it:6969",
		"udp://tracker.ccc.de:80",
		"udp://tracker.open.demonii.com:1337",
		"udp://open.demonii.com:1337",
		"udp://exodus.desync.com:6969",
	} {
		wg.Add(1)
		go func(url string) {
			defer wg.Done()
			tr, err := New(url)
			if err != nil {
				t.Fatal(err)
			}
			if err := tr.Connect(); err != nil {
				t.Log(err)
				return
			}
			resp, err := tr.Announce(&req)
			if err != nil {
				t.Logf("error announcing to %s: %s", url, err)
				return
			}
			if resp.Leechers != 0 || resp.Seeders != 0 || len(resp.Peers) != 0 {
				// The info hash we generated was random in 2^160 space. If we
				// get a hit, something is weird.
				t.Fatal(resp)
			}
			t.Logf("announced to %s", url)
			// TODO: Can probably get stuck here, but it's just a throwaway
			// test.
			success <- true
		}(url)
	}
	go func() {
		wg.Wait()
		close(fail)
	}()
	// Bail as quickly as we can.
	select {
	case <-fail:
		t.FailNow()
	case <-success:
	}
}