2018-01-21 18:35:02 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/rand"
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/anacrolix/torrent/tracker"
|
|
|
|
"github.com/pkg/errors"
|
2018-08-31 02:50:04 +02:00
|
|
|
"github.com/spf13/cobra"
|
2018-01-21 18:35:02 +01:00
|
|
|
|
|
|
|
"github.com/chihaya/chihaya/bittorrent"
|
2018-08-31 02:50:04 +02:00
|
|
|
"github.com/chihaya/chihaya/pkg/log"
|
2018-01-21 18:35:02 +01:00
|
|
|
)
|
|
|
|
|
2018-08-31 02:50:04 +02: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
|
|
|
|
2018-08-31 02:50:04 +02:00
|
|
|
// Test the HTTP tracker
|
|
|
|
httpAddr, err := cmd.Flags().GetString("httpaddr")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-01-21 18:35:02 +01:00
|
|
|
|
2018-08-31 02:50:04 +02:00
|
|
|
if len(httpAddr) != 0 {
|
|
|
|
log.Info("testing HTTP...")
|
|
|
|
err := test(httpAddr, delay)
|
2018-01-21 18:35:02 +01:00
|
|
|
if err != nil {
|
2018-08-31 02:50:04 +02:00
|
|
|
return err
|
2018-01-21 18:35:02 +01:00
|
|
|
}
|
2018-08-31 02:50:04 +02:00
|
|
|
log.Info("success")
|
2018-01-21 18:35:02 +01:00
|
|
|
}
|
|
|
|
|
2018-08-31 02:50:04 +02: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 {
|
2018-08-31 02:50:04 +02:00
|
|
|
return err
|
2018-01-21 18:35:02 +01:00
|
|
|
}
|
2018-08-31 02:50:04 +02:00
|
|
|
log.Info("success")
|
2018-01-21 18:35:02 +01:00
|
|
|
}
|
2018-08-31 02:50:04 +02: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))
|
|
|
|
}
|
|
|
|
|
2018-08-31 02:50:04 +02:00
|
|
|
func test(addr string, delay time.Duration) error {
|
2018-01-21 18:35:02 +01:00
|
|
|
ih := generateInfohash()
|
2018-08-31 02:50:04 +02:00
|
|
|
return testWithInfohash(ih, addr, delay)
|
2018-01-21 18:35:02 +01:00
|
|
|
}
|
|
|
|
|
2018-08-31 02:50:04 +02: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,
|
2018-08-31 02:50:04 +02:00
|
|
|
IPAddress: uint32(50<<24 | 10<<16 | 12<<8 | 1),
|
2018-01-21 18:35:02 +01:00
|
|
|
NumWant: 50,
|
|
|
|
Port: 10001,
|
|
|
|
}
|
|
|
|
|
2018-08-31 02:50:04 +02:00
|
|
|
resp, err := tracker.Announce{
|
|
|
|
TrackerUrl: url,
|
|
|
|
Request: req,
|
|
|
|
UserAgent: "chihaya-e2e",
|
|
|
|
}.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,
|
2018-08-31 02:50:04 +02:00
|
|
|
IPAddress: uint32(50<<24 | 10<<16 | 12<<8 | 2),
|
2018-01-21 18:35:02 +01:00
|
|
|
NumWant: 50,
|
|
|
|
Port: 10002,
|
|
|
|
}
|
|
|
|
|
2018-08-31 02:50:04 +02:00
|
|
|
resp, err = tracker.Announce{
|
|
|
|
TrackerUrl: url,
|
|
|
|
Request: req,
|
|
|
|
UserAgent: "chihaya-e2e",
|
|
|
|
}.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
|
|
|
|
}
|