tracker/cmd/chihaya/e2e.go

138 lines
2.8 KiB
Go
Raw Normal View History

2018-01-21 18:35:02 +01:00
package main
import (
"crypto/rand"
"fmt"
"net/http"
"time"
"github.com/anacrolix/torrent/tracker"
"github.com/pkg/errors"
"github.com/spf13/cobra"
2018-01-21 18:35:02 +01:00
"github.com/chihaya/chihaya/bittorrent"
"github.com/chihaya/chihaya/pkg/log"
2018-01-21 18:35:02 +01:00
)
// EndToEndRunCmdFunc implements a Cobra command that runs the end-to-end test
// suite for a Chihaya build.
func EndToEndRunCmdFunc(cmd *cobra.Command, args []string) error {
delay, err := cmd.Flags().GetDuration("delay")
if err != nil {
return err
}
2018-01-21 18:35:02 +01:00
// Test the HTTP tracker
httpAddr, err := cmd.Flags().GetString("httpaddr")
if err != nil {
return err
}
2018-01-21 18:35:02 +01:00
if len(httpAddr) != 0 {
log.Info("testing HTTP...")
err := test(httpAddr, delay)
2018-01-21 18:35:02 +01:00
if err != nil {
return err
2018-01-21 18:35:02 +01:00
}
log.Info("success")
2018-01-21 18:35:02 +01:00
}
// Test the UDP tracker.
udpAddr, err := cmd.Flags().GetString("udpaddr")
if err != nil {
return err
}
if len(udpAddr) != 0 {
log.Info("testing UDP...")
err := test(udpAddr, delay)
2018-01-21 18:35:02 +01:00
if err != nil {
return err
2018-01-21 18:35:02 +01:00
}
log.Info("success")
2018-01-21 18:35:02 +01:00
}
return nil
2018-01-21 18:35:02 +01:00
}
func generateInfohash() [20]byte {
b := make([]byte, 20)
n, err := rand.Read(b)
if err != nil {
panic(err)
}
if n != 20 {
panic(fmt.Errorf("not enough randomness? Got %d bytes", n))
}
return [20]byte(bittorrent.InfoHashFromBytes(b))
}
func test(addr string, delay time.Duration) error {
2018-01-21 18:35:02 +01:00
ih := generateInfohash()
return testWithInfohash(ih, addr, delay)
2018-01-21 18:35:02 +01:00
}
func testWithInfohash(infoHash [20]byte, url string, delay time.Duration) error {
2018-01-21 18:35:02 +01:00
req := tracker.AnnounceRequest{
InfoHash: infoHash,
PeerId: [20]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20},
Downloaded: 50,
Left: 100,
Uploaded: 50,
Event: tracker.Started,
IPAddress: uint32(50<<24 | 10<<16 | 12<<8 | 1),
2018-01-21 18:35:02 +01:00
NumWant: 50,
Port: 10001,
}
resp, err := tracker.Announce{
TrackerUrl: url,
Request: req,
UserAgent: "chihaya-e2e",
HttpClient: &http.Client{},
}.Do()
2018-01-21 18:35:02 +01:00
if err != nil {
return errors.Wrap(err, "announce failed")
}
if len(resp.Peers) != 1 {
return fmt.Errorf("expected one peer, got %d", len(resp.Peers))
}
time.Sleep(delay)
req = tracker.AnnounceRequest{
InfoHash: infoHash,
PeerId: [20]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 21},
Downloaded: 50,
Left: 100,
Uploaded: 50,
Event: tracker.Started,
IPAddress: uint32(50<<24 | 10<<16 | 12<<8 | 2),
2018-01-21 18:35:02 +01:00
NumWant: 50,
Port: 10002,
}
resp, err = tracker.Announce{
TrackerUrl: url,
Request: req,
UserAgent: "chihaya-e2e",
HttpClient: &http.Client{},
}.Do()
2018-01-21 18:35:02 +01:00
if err != nil {
return errors.Wrap(err, "announce failed")
}
if len(resp.Peers) != 1 {
return fmt.Errorf("expected 1 peers, got %d", len(resp.Peers))
}
if resp.Peers[0].Port != 10001 {
return fmt.Errorf("expected port 10001, got %d ", resp.Peers[0].Port)
}
return nil
}