2016-03-29 17:54:23 +02:00
|
|
|
// Copyright 2016 The Chihaya Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by the BSD 2-Clause license,
|
|
|
|
// which can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package infohash
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
|
|
|
"github.com/chihaya/chihaya"
|
|
|
|
"github.com/chihaya/chihaya/tracker"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestWhitelistAnnounceMiddleware(t *testing.T) {
|
|
|
|
var (
|
|
|
|
achain tracker.AnnounceChain
|
|
|
|
req chihaya.AnnounceRequest
|
|
|
|
resp chihaya.AnnounceResponse
|
|
|
|
)
|
|
|
|
|
|
|
|
achain.Append(whitelistAnnounceInfohash)
|
|
|
|
handler := achain.Handler()
|
|
|
|
|
|
|
|
err := handler(nil, &req, &resp)
|
|
|
|
assert.Equal(t, ErrBlockedInfohash, err)
|
|
|
|
|
2016-05-17 05:48:23 +02:00
|
|
|
req.InfoHash = chihaya.InfoHash(ih2)
|
2016-03-29 17:54:23 +02:00
|
|
|
err = handler(nil, &req, &resp)
|
|
|
|
assert.Equal(t, ErrBlockedInfohash, err)
|
|
|
|
|
2016-05-17 05:48:23 +02:00
|
|
|
req.InfoHash = chihaya.InfoHash(ih1)
|
2016-03-29 17:54:23 +02:00
|
|
|
err = handler(nil, &req, &resp)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWhitelistScrapeMiddlewareBlock(t *testing.T) {
|
|
|
|
var (
|
|
|
|
schain tracker.ScrapeChain
|
|
|
|
req chihaya.ScrapeRequest
|
|
|
|
resp chihaya.ScrapeResponse
|
|
|
|
)
|
|
|
|
|
|
|
|
mw, err := whitelistScrapeInfohash(chihaya.MiddlewareConfig{
|
|
|
|
Name: "whitelist_infohash",
|
|
|
|
Config: Config{
|
|
|
|
Mode: ModeBlock,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
assert.Nil(t, err)
|
|
|
|
schain.Append(mw)
|
|
|
|
handler := schain.Handler()
|
|
|
|
|
|
|
|
err = handler(nil, &req, &resp)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
2016-05-17 05:48:23 +02:00
|
|
|
req.InfoHashes = []chihaya.InfoHash{chihaya.InfoHash(ih1), chihaya.InfoHash(ih2)}
|
2016-03-29 17:54:23 +02:00
|
|
|
err = handler(nil, &req, &resp)
|
|
|
|
assert.Equal(t, ErrBlockedInfohash, err)
|
|
|
|
|
2016-05-17 05:48:23 +02:00
|
|
|
req.InfoHashes = []chihaya.InfoHash{chihaya.InfoHash(ih1)}
|
2016-03-29 17:54:23 +02:00
|
|
|
err = handler(nil, &req, &resp)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWhitelistScrapeMiddlewareFilter(t *testing.T) {
|
|
|
|
var (
|
|
|
|
schain tracker.ScrapeChain
|
|
|
|
req chihaya.ScrapeRequest
|
|
|
|
resp chihaya.ScrapeResponse
|
|
|
|
)
|
|
|
|
|
|
|
|
mw, err := whitelistScrapeInfohash(chihaya.MiddlewareConfig{
|
|
|
|
Name: "whitelist_infohash",
|
|
|
|
Config: Config{
|
|
|
|
Mode: ModeFilter,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
assert.Nil(t, err)
|
|
|
|
schain.Append(mw)
|
|
|
|
handler := schain.Handler()
|
|
|
|
|
|
|
|
err = handler(nil, &req, &resp)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
2016-05-17 05:48:23 +02:00
|
|
|
req.InfoHashes = []chihaya.InfoHash{chihaya.InfoHash(ih1), chihaya.InfoHash(ih2)}
|
2016-03-29 17:54:23 +02:00
|
|
|
err = handler(nil, &req, &resp)
|
|
|
|
assert.Nil(t, err)
|
2016-05-17 05:48:23 +02:00
|
|
|
assert.Equal(t, []chihaya.InfoHash{chihaya.InfoHash(ih1)}, req.InfoHashes)
|
2016-03-29 17:54:23 +02:00
|
|
|
|
2016-05-17 05:48:23 +02:00
|
|
|
req.InfoHashes = []chihaya.InfoHash{chihaya.InfoHash(ih1)}
|
2016-03-29 17:54:23 +02:00
|
|
|
err = handler(nil, &req, &resp)
|
|
|
|
assert.Nil(t, err)
|
2016-05-17 05:48:23 +02:00
|
|
|
assert.Equal(t, []chihaya.InfoHash{chihaya.InfoHash(ih1)}, req.InfoHashes)
|
2016-03-29 17:54:23 +02:00
|
|
|
}
|