2014-07-11 04:30:30 +00:00
|
|
|
var bencode = require('bencode')
|
|
|
|
var Client = require('../')
|
2014-07-22 05:58:13 +00:00
|
|
|
var commonLib = require('../lib/common')
|
|
|
|
var commonTest = require('./common')
|
2014-07-11 04:30:30 +00:00
|
|
|
var fs = require('fs')
|
2015-01-22 22:23:53 +00:00
|
|
|
var get = require('simple-get')
|
2014-07-11 04:30:30 +00:00
|
|
|
var parseTorrent = require('parse-torrent')
|
|
|
|
var portfinder = require('portfinder')
|
|
|
|
var Server = require('../').Server
|
|
|
|
var test = require('tape')
|
|
|
|
|
2014-07-13 01:44:41 +00:00
|
|
|
function hexToBinary (str) {
|
|
|
|
return new Buffer(str, 'hex').toString('binary')
|
|
|
|
}
|
|
|
|
|
2014-07-11 04:30:30 +00:00
|
|
|
var infoHash1 = 'aaa67059ed6bd08362da625b3ae77f6f4a075aaa'
|
2014-07-13 01:44:41 +00:00
|
|
|
var binaryInfoHash1 = hexToBinary(infoHash1)
|
2014-07-11 04:30:30 +00:00
|
|
|
var infoHash2 = 'bbb67059ed6bd08362da625b3ae77f6f4a075bbb'
|
2014-07-13 01:44:41 +00:00
|
|
|
var binaryInfoHash2 = hexToBinary(infoHash2)
|
2014-07-11 04:30:30 +00:00
|
|
|
|
|
|
|
var bitlove = fs.readFileSync(__dirname + '/torrents/bitlove-intro.torrent')
|
|
|
|
var parsedBitlove = parseTorrent(bitlove)
|
2014-07-13 01:44:41 +00:00
|
|
|
var binaryBitlove = hexToBinary(parsedBitlove.infoHash)
|
|
|
|
|
2014-07-11 04:30:30 +00:00
|
|
|
var peerId = new Buffer('01234567890123456789')
|
|
|
|
|
2014-07-22 05:58:13 +00:00
|
|
|
function testSingle (t, serverType) {
|
|
|
|
commonTest.createServer(t, serverType, function (server, announceUrl) {
|
|
|
|
server.once('listening', function () {
|
|
|
|
Client.scrape(announceUrl, infoHash1, function (err, data) {
|
|
|
|
t.error(err)
|
|
|
|
t.equal(data.announce, announceUrl)
|
|
|
|
t.equal(typeof data.complete, 'number')
|
|
|
|
t.equal(typeof data.incomplete, 'number')
|
|
|
|
t.equal(typeof data.downloaded, 'number')
|
|
|
|
server.close(function () {
|
|
|
|
t.end()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2014-07-11 04:30:30 +00:00
|
|
|
})
|
2014-07-22 05:58:13 +00:00
|
|
|
}
|
2014-07-11 04:30:30 +00:00
|
|
|
|
2014-07-22 05:58:13 +00:00
|
|
|
test('http: single info_hash scrape', function (t) {
|
|
|
|
testSingle(t, 'http')
|
|
|
|
})
|
2014-07-11 04:30:30 +00:00
|
|
|
|
2014-07-22 05:58:13 +00:00
|
|
|
test('udp: single info_hash scrape', function (t) {
|
|
|
|
testSingle(t, 'udp')
|
|
|
|
})
|
2014-07-11 04:30:30 +00:00
|
|
|
|
2014-07-22 05:58:13 +00:00
|
|
|
function clientScrapeStatic (t, serverType) {
|
|
|
|
commonTest.createServer(t, serverType, function (server, announceUrl) {
|
|
|
|
server.once('listening', function () {
|
|
|
|
Client.scrape(announceUrl, infoHash1, function (err, data) {
|
2014-07-11 04:30:30 +00:00
|
|
|
t.error(err)
|
2014-07-22 05:58:13 +00:00
|
|
|
t.equal(data.announce, announceUrl)
|
|
|
|
t.equal(typeof data.complete, 'number')
|
|
|
|
t.equal(typeof data.incomplete, 'number')
|
|
|
|
t.equal(typeof data.downloaded, 'number')
|
|
|
|
server.close(function () {
|
|
|
|
t.end()
|
|
|
|
})
|
2014-07-11 04:30:30 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2014-07-22 05:58:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
test('http: scrape using Client.scrape static method', function (t) {
|
|
|
|
clientScrapeStatic(t, 'http')
|
|
|
|
})
|
|
|
|
|
|
|
|
test('udp: scrape using Client.scrape static method', function (t) {
|
|
|
|
clientScrapeStatic(t, 'udp')
|
2014-07-11 04:30:30 +00:00
|
|
|
})
|
|
|
|
|
2014-07-22 05:58:13 +00:00
|
|
|
// TODO: test client for multiple scrape for UDP trackers
|
|
|
|
|
2014-07-11 04:30:30 +00:00
|
|
|
test('server: multiple info_hash scrape', function (t) {
|
|
|
|
var server = new Server({ udp: false })
|
|
|
|
server.on('error', function (err) {
|
|
|
|
t.error(err)
|
|
|
|
})
|
|
|
|
server.on('warning', function (err) {
|
|
|
|
t.error(err)
|
|
|
|
})
|
|
|
|
|
|
|
|
portfinder.getPort(function (err, port) {
|
|
|
|
t.error(err)
|
|
|
|
server.listen(port)
|
|
|
|
var scrapeUrl = 'http://127.0.0.1:' + port + '/scrape'
|
|
|
|
|
|
|
|
server.once('listening', function () {
|
2014-07-22 05:58:13 +00:00
|
|
|
var url = scrapeUrl + '?' + commonLib.querystringStringify({
|
2014-07-13 01:44:41 +00:00
|
|
|
info_hash: [ binaryInfoHash1, binaryInfoHash2 ]
|
2014-07-11 04:30:30 +00:00
|
|
|
})
|
2015-01-22 22:23:53 +00:00
|
|
|
get.concat(url, function (err, data, res) {
|
|
|
|
if (err) throw err
|
2014-07-11 04:30:30 +00:00
|
|
|
t.equal(res.statusCode, 200)
|
|
|
|
|
2015-01-22 22:23:53 +00:00
|
|
|
data = bencode.decode(data)
|
|
|
|
t.ok(data.files)
|
|
|
|
t.equal(Object.keys(data.files).length, 2)
|
2014-07-11 04:30:30 +00:00
|
|
|
|
2015-01-22 22:23:53 +00:00
|
|
|
t.ok(data.files[binaryInfoHash1])
|
|
|
|
t.equal(typeof data.files[binaryInfoHash1].complete, 'number')
|
|
|
|
t.equal(typeof data.files[binaryInfoHash1].incomplete, 'number')
|
|
|
|
t.equal(typeof data.files[binaryInfoHash1].downloaded, 'number')
|
2014-07-11 04:30:30 +00:00
|
|
|
|
2015-01-22 22:23:53 +00:00
|
|
|
t.ok(data.files[binaryInfoHash2])
|
|
|
|
t.equal(typeof data.files[binaryInfoHash2].complete, 'number')
|
|
|
|
t.equal(typeof data.files[binaryInfoHash2].incomplete, 'number')
|
|
|
|
t.equal(typeof data.files[binaryInfoHash2].downloaded, 'number')
|
|
|
|
|
|
|
|
server.close(function () {
|
|
|
|
t.end()
|
|
|
|
})
|
2014-07-11 04:30:30 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
test('server: all info_hash scrape', function (t) {
|
|
|
|
var server = new Server({ udp: false })
|
|
|
|
server.on('error', function (err) {
|
|
|
|
t.error(err)
|
|
|
|
})
|
|
|
|
server.on('warning', function (err) {
|
|
|
|
t.error(err)
|
|
|
|
})
|
|
|
|
|
|
|
|
portfinder.getPort(function (err, port) {
|
|
|
|
t.error(err)
|
|
|
|
server.listen(port)
|
|
|
|
var announceUrl = 'http://127.0.0.1:' + port + '/announce'
|
|
|
|
var scrapeUrl = 'http://127.0.0.1:' + port + '/scrape'
|
|
|
|
|
|
|
|
parsedBitlove.announce = [ announceUrl ]
|
|
|
|
|
|
|
|
server.once('listening', function () {
|
|
|
|
|
|
|
|
// announce a torrent to the tracker
|
|
|
|
var client = new Client(peerId, port, parsedBitlove)
|
|
|
|
client.on('error', function (err) {
|
|
|
|
t.error(err)
|
|
|
|
})
|
|
|
|
client.start()
|
|
|
|
|
2014-11-26 12:11:49 +00:00
|
|
|
server.once('start', function () {
|
2014-07-11 04:30:30 +00:00
|
|
|
|
|
|
|
// now do a scrape of everything by omitting the info_hash param
|
2015-01-22 22:23:53 +00:00
|
|
|
get.concat(scrapeUrl, function (err, data, res) {
|
|
|
|
if (err) throw err
|
2014-07-11 04:30:30 +00:00
|
|
|
|
|
|
|
t.equal(res.statusCode, 200)
|
2015-01-22 22:23:53 +00:00
|
|
|
data = bencode.decode(data)
|
|
|
|
t.ok(data.files)
|
|
|
|
t.equal(Object.keys(data.files).length, 1)
|
|
|
|
|
|
|
|
t.ok(data.files[binaryBitlove])
|
|
|
|
t.equal(typeof data.files[binaryBitlove].complete, 'number')
|
|
|
|
t.equal(typeof data.files[binaryBitlove].incomplete, 'number')
|
|
|
|
t.equal(typeof data.files[binaryBitlove].downloaded, 'number')
|
|
|
|
|
|
|
|
client.stop()
|
|
|
|
server.close(function () {
|
|
|
|
t.end()
|
|
|
|
})
|
2014-07-11 04:30:30 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|