From 1a39a495d72e81dde7499f0bf4b90856b2e09433 Mon Sep 17 00:00:00 2001 From: Leo Balduf Date: Sun, 21 Jan 2018 18:35:02 +0100 Subject: [PATCH 1/3] cmd/chihaya-e2e: add chihaya-e2e --- cmd/chihaya-e2e/README.md | 5 ++ cmd/chihaya-e2e/main.go | 127 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 132 insertions(+) create mode 100644 cmd/chihaya-e2e/README.md create mode 100644 cmd/chihaya-e2e/main.go diff --git a/cmd/chihaya-e2e/README.md b/cmd/chihaya-e2e/README.md new file mode 100644 index 0000000..6a5a290 --- /dev/null +++ b/cmd/chihaya-e2e/README.md @@ -0,0 +1,5 @@ +# chihaya-e2e +A very simple tool to black-box test a bittorrent tracker. + +This tool uses [github.com/anacrolix/torrent/tracker](github.com/anacrolix/torrent/tracker) to make a UDP and an HTTP announce to given trackers. +It is used by chihaya for end-to-end testing the tracker during CI. diff --git a/cmd/chihaya-e2e/main.go b/cmd/chihaya-e2e/main.go new file mode 100644 index 0000000..25c57d6 --- /dev/null +++ b/cmd/chihaya-e2e/main.go @@ -0,0 +1,127 @@ +package main + +import ( + "crypto/rand" + "flag" + "fmt" + "net/http" + "os" + "time" + + "github.com/anacrolix/torrent/tracker" + "github.com/pkg/errors" + + "github.com/chihaya/chihaya/bittorrent" +) + +func init() { + flag.StringVar(&httpTrackerURL, "http", "http://localhost:6881/announce", "the address of the HTTP tracker") + flag.StringVar(&udpTrackerURL, "udp", "udp://localhost:6881", "the address of the UDP tracker") + flag.DurationVar(&delay, "delay", 1*time.Second, "the delay between announces") +} + +var ( + httpTrackerURL string + udpTrackerURL string + delay time.Duration +) + +func main() { + flag.Parse() + + if len(httpTrackerURL) != 0 { + fmt.Println("testing HTTP...") + err := testHTTP() + if err != nil { + fmt.Println("failed:", err) + os.Exit(1) + } + fmt.Println("success") + } + + if len(udpTrackerURL) != 0 { + fmt.Println("testing UDP...") + err := testUDP() + if err != nil { + fmt.Println("failed:", err) + os.Exit(1) + } + fmt.Println("success") + } +} + +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 testUDP() error { + ih := generateInfohash() + return testWithInfohash(ih, udpTrackerURL) +} + +func testHTTP() error { + ih := generateInfohash() + return testWithInfohash(ih, httpTrackerURL) +} + +func testWithInfohash(infoHash [20]byte, url string) error { + 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: int32(50<<24 | 10<<16 | 12<<8 | 1), + NumWant: 50, + Port: 10001, + } + + resp, err := tracker.Announce(&http.Client{}, "ekop", url, &req) + 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: int32(50<<24 | 10<<16 | 12<<8 | 2), + NumWant: 50, + Port: 10002, + } + + resp, err = tracker.Announce(&http.Client{}, "ekop", url, &req) + 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 +} From 65704f47e1c5ab5da171d5b964bc99ff82ab99bc Mon Sep 17 00:00:00 2001 From: Leo Balduf Date: Sun, 21 Jan 2018 18:35:15 +0100 Subject: [PATCH 2/3] travis: add chihaya-e2e --- .travis.yml | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/.travis.yml b/.travis.yml index 03aeb7e..ef13a17 100644 --- a/.travis.yml +++ b/.travis.yml @@ -15,6 +15,13 @@ matrix: - go vet $(go list ./...) - diff <(goimports -d $(find . -type f -name '*.go' -not -path "./vendor/*")) <(printf "") - (for d in $(go list ./...); do diff <(golint $d) <(printf "") || exit 1; done) + - go install github.com/chihaya/chihaya/cmd/chihaya + - chihaya --config=example_config.yaml --debug& + - pid=$! + # we move the installation here so chihaya has enough time to start up + - go install github.com/chihaya/chihaya/cmd/chihaya-e2e + - chihaya-e2e + - kill $pid # Using HEAD of dependencies - install: - go get -t ./... @@ -25,6 +32,13 @@ matrix: - go vet $(go list ./...) - diff <(goimports -d $(find . -type f -name '*.go' -not -path "./vendor/*")) <(printf "") - (for d in $(go list ./...); do diff <(golint $d) <(printf "") || exit 1; done) + - go install github.com/chihaya/chihaya/cmd/chihaya + - chihaya --config=example_config.yaml --debug& + - pid=$! + # we move the installation here so chihaya has enough time to start up + - go install github.com/chihaya/chihaya/cmd/chihaya-e2e + - chihaya-e2e + - kill $pid allow_failures: # Using HEAD of dependencies - install: @@ -36,6 +50,13 @@ matrix: - go vet $(go list ./...) - diff <(goimports -d $(find . -type f -name '*.go' -not -path "./vendor/*")) <(printf "") - (for d in $(go list ./...); do diff <(golint $d) <(printf "") || exit 1; done) + - go install github.com/chihaya/chihaya/cmd/chihaya + - chihaya --config=example_config.yaml --debug& + - pid=$! + # we move the installation here so chihaya has enough time to start up + - go install github.com/chihaya/chihaya/cmd/chihaya-e2e + - chihaya-e2e + - kill $pid notifications: irc: channels: From 7022b541bc8ebfea811466a064597ed67a014e4d Mon Sep 17 00:00:00 2001 From: Leo Balduf Date: Sun, 4 Feb 2018 12:38:12 +0100 Subject: [PATCH 3/3] dep: add dependencies for e2e testing --- Gopkg.lock | 64 +++++++++++++++++++++++++++++++++++++++++++++++++----- Gopkg.toml | 8 +++++++ 2 files changed, 66 insertions(+), 6 deletions(-) diff --git a/Gopkg.lock b/Gopkg.lock index 1865dc5..df6d726 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -3,16 +3,49 @@ [[projects]] name = "github.com/SermoDigital/jose" - packages = [".","crypto","jws","jwt"] + packages = [ + ".", + "crypto", + "jws", + "jwt" + ] revision = "f6df55f235c24f236d11dbcf665249a59ac2021f" version = "1.1" +[[projects]] + branch = "master" + name = "github.com/anacrolix/missinggo" + packages = [ + ".", + "assert", + "httptoo", + "mime", + "pproffd" + ] + revision = "173db517b5f8002d630f815e2097a1858c27f8ef" + +[[projects]] + branch = "master" + name = "github.com/anacrolix/torrent" + packages = [ + "bencode", + "tracker", + "util" + ] + revision = "64d13d86a6ba04269e2abacedcdd890165901010" + [[projects]] branch = "master" name = "github.com/beorn7/perks" packages = ["quantile"] revision = "4c0e84591b9aa9e6dcfdf3e020114cd81f89d5f9" +[[projects]] + branch = "master" + name = "github.com/bradfitz/iter" + packages = ["."] + revision = "454541ec3da2a73fc34fd049b19ee5777bf19345" + [[projects]] name = "github.com/davecgh/go-spew" packages = ["spew"] @@ -55,6 +88,12 @@ packages = ["."] revision = "ad98a36ba0da87206e3378c556abbfeaeaa98668" +[[projects]] + branch = "master" + name = "github.com/pkg/errors" + packages = ["."] + revision = "30136e27e2ac8d167177e8a583aa4c3fea5be833" + [[projects]] name = "github.com/pmezard/go-difflib" packages = ["difflib"] @@ -76,13 +115,20 @@ [[projects]] branch = "master" name = "github.com/prometheus/common" - packages = ["expfmt","internal/bitbucket.org/ww/goautoneg","model"] + packages = [ + "expfmt", + "internal/bitbucket.org/ww/goautoneg", + "model" + ] revision = "2e54d0b93cba2fd133edc32211dcc32c06ef72ca" [[projects]] branch = "master" name = "github.com/prometheus/procfs" - packages = [".","xfs"] + packages = [ + ".", + "xfs" + ] revision = "8f918ac9ab4be3a790338bda8624fec5d71260b3" [[projects]] @@ -105,7 +151,10 @@ [[projects]] name = "github.com/stretchr/testify" - packages = ["assert","require"] + packages = [ + "assert", + "require" + ] revision = "69483b4bd14f5845b5a1e55bca19e954e827f1d0" version = "v1.1.4" @@ -118,7 +167,10 @@ [[projects]] branch = "master" name = "golang.org/x/sys" - packages = ["unix","windows"] + packages = [ + "unix", + "windows" + ] revision = "83801418e1b59fb1880e363299581ee543af32ca" [[projects]] @@ -130,6 +182,6 @@ [solve-meta] analyzer-name = "dep" analyzer-version = 1 - inputs-digest = "f73a01a852753ecab441d7901cbf324cdfbdbadf174e60fe0d162278f355ea21" + inputs-digest = "70d8adf87b226f38ffea02e5edd2a0adb03b5e1a567eb2ed74ab9d480b9fd68c" solver-name = "gps-cdcl" solver-version = 1 diff --git a/Gopkg.toml b/Gopkg.toml index 717790d..22e04dc 100644 --- a/Gopkg.toml +++ b/Gopkg.toml @@ -56,3 +56,11 @@ [[constraint]] branch = "v2" name = "gopkg.in/yaml.v2" + +[[constraint]] + name = "github.com/pkg/errors" + branch = "master" + +[[constraint]] + name = "github.com/anacrolix/torrent" + branch = "master" \ No newline at end of file