2014-03-26 08:17:59 +00:00
|
|
|
var Client = require('../').Client
|
|
|
|
var fs = require('fs')
|
|
|
|
var parseTorrent = require('parse-torrent')
|
2014-05-24 04:14:19 +00:00
|
|
|
var portfinder = require('portfinder')
|
|
|
|
var Server = require('../').Server
|
2014-03-26 08:17:59 +00:00
|
|
|
var test = require('tape')
|
|
|
|
|
2014-05-14 19:45:48 +00:00
|
|
|
// TODO: add test where tracker doesn't support compact
|
|
|
|
|
2014-03-26 08:17:59 +00:00
|
|
|
var torrent = fs.readFileSync(__dirname + '/torrents/bitlove-intro.torrent')
|
|
|
|
var parsedTorrent = parseTorrent(torrent)
|
|
|
|
var peerId = new Buffer('01234567890123456789')
|
2014-05-24 04:14:19 +00:00
|
|
|
var announceUrl = ''
|
2014-03-26 08:17:59 +00:00
|
|
|
var port = 6881
|
|
|
|
|
2014-05-24 04:14:19 +00:00
|
|
|
function createServer (cb) {
|
|
|
|
var server = new Server({ udp: false })
|
2014-03-26 08:17:59 +00:00
|
|
|
|
2014-05-24 04:14:19 +00:00
|
|
|
server.on('error', function (err) {
|
|
|
|
t.fail(err.message)
|
2014-03-26 08:17:59 +00:00
|
|
|
})
|
|
|
|
|
2014-05-24 04:14:19 +00:00
|
|
|
server.on('warning', function (err) {
|
|
|
|
t.fail(err.message)
|
2014-03-26 08:17:59 +00:00
|
|
|
})
|
|
|
|
|
2014-05-24 04:14:19 +00:00
|
|
|
portfinder.getPort(function (err, port) {
|
|
|
|
if (err) return cb(err)
|
2014-03-26 08:17:59 +00:00
|
|
|
|
2014-05-24 04:14:19 +00:00
|
|
|
announceUrl = 'http://127.0.0.1:' + port + '/announce'
|
|
|
|
parsedTorrent.announce = [ announceUrl ]
|
2014-03-26 08:17:59 +00:00
|
|
|
|
2014-05-24 04:14:19 +00:00
|
|
|
server.listen(port)
|
|
|
|
cb(null, server)
|
|
|
|
})
|
|
|
|
}
|
2014-03-26 08:17:59 +00:00
|
|
|
|
2014-05-24 04:14:19 +00:00
|
|
|
test('torrent: client.start()', function (t) {
|
|
|
|
t.plan(6)
|
2014-03-26 08:17:59 +00:00
|
|
|
|
2014-05-24 04:14:19 +00:00
|
|
|
createServer(function (err, server) {
|
2014-03-28 04:29:00 +00:00
|
|
|
t.error(err)
|
2014-03-26 08:17:59 +00:00
|
|
|
|
2014-05-24 04:14:19 +00:00
|
|
|
var client = new Client(peerId, port, parsedTorrent)
|
2014-03-26 08:17:59 +00:00
|
|
|
|
2014-05-24 04:14:19 +00:00
|
|
|
client.on('error', function (err) {
|
|
|
|
t.fail(err)
|
|
|
|
})
|
2014-03-26 08:17:59 +00:00
|
|
|
|
|
|
|
client.once('update', function (data) {
|
2014-05-09 06:38:41 +00:00
|
|
|
t.equal(data.announce, announceUrl)
|
2014-03-26 08:17:59 +00:00
|
|
|
t.equal(typeof data.complete, 'number')
|
|
|
|
t.equal(typeof data.incomplete, 'number')
|
|
|
|
})
|
2014-03-27 07:17:49 +00:00
|
|
|
|
2014-05-24 04:14:19 +00:00
|
|
|
client.once('peer', function (addr) {
|
|
|
|
t.pass('there is at least one peer')
|
|
|
|
client.stop()
|
|
|
|
|
|
|
|
client.once('update', function () {
|
|
|
|
server.close(function () {
|
|
|
|
t.pass('server close')
|
|
|
|
})
|
|
|
|
})
|
2014-03-27 07:17:49 +00:00
|
|
|
})
|
2014-03-26 08:17:59 +00:00
|
|
|
|
2014-05-24 04:14:19 +00:00
|
|
|
client.start()
|
|
|
|
})
|
|
|
|
})
|
2014-03-26 08:17:59 +00:00
|
|
|
|
2014-05-24 04:14:19 +00:00
|
|
|
test('torrent: client.stop()', function (t) {
|
|
|
|
t.plan(5)
|
2014-03-26 08:17:59 +00:00
|
|
|
|
2014-05-24 04:14:19 +00:00
|
|
|
createServer(function (err, server) {
|
2014-03-28 04:29:00 +00:00
|
|
|
t.error(err)
|
2014-05-24 04:14:19 +00:00
|
|
|
var client = new Client(peerId, port, parsedTorrent)
|
2014-03-26 08:17:59 +00:00
|
|
|
|
2014-05-24 04:14:19 +00:00
|
|
|
client.on('error', function (err) {
|
|
|
|
t.fail(err)
|
|
|
|
})
|
2014-03-26 08:17:59 +00:00
|
|
|
|
2014-05-24 04:14:19 +00:00
|
|
|
client.start()
|
2014-03-26 08:17:59 +00:00
|
|
|
|
2014-05-24 04:14:19 +00:00
|
|
|
setTimeout(function () {
|
2014-03-26 08:17:59 +00:00
|
|
|
client.stop()
|
2014-05-24 04:14:19 +00:00
|
|
|
|
|
|
|
client.once('update', function (data) {
|
|
|
|
// receive one final update after calling stop
|
|
|
|
t.equal(data.announce, announceUrl)
|
|
|
|
t.equal(typeof data.complete, 'number')
|
|
|
|
t.equal(typeof data.incomplete, 'number')
|
|
|
|
|
|
|
|
server.close(function () {
|
|
|
|
t.pass('server close')
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
}, 1000)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
test('torrent: client.update()', function (t) {
|
|
|
|
t.plan(5)
|
|
|
|
|
|
|
|
createServer(function (err, server) {
|
|
|
|
t.error(err)
|
|
|
|
var client = new Client(peerId, port, parsedTorrent, { interval: 5000 })
|
|
|
|
|
|
|
|
client.on('error', function (err) {
|
|
|
|
t.fail(err)
|
2014-03-26 08:17:59 +00:00
|
|
|
})
|
|
|
|
|
2014-05-24 04:14:19 +00:00
|
|
|
client.start()
|
|
|
|
|
|
|
|
client.once('update', function () {
|
|
|
|
|
|
|
|
client.once('update', function (data) {
|
|
|
|
// received an update!
|
|
|
|
t.equal(data.announce, announceUrl)
|
|
|
|
t.equal(typeof data.complete, 'number')
|
|
|
|
t.equal(typeof data.incomplete, 'number')
|
|
|
|
client.stop()
|
|
|
|
|
|
|
|
client.once('update', function () {
|
|
|
|
server.close(function () {
|
|
|
|
t.pass('server close')
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2014-03-26 08:17:59 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2014-05-14 19:45:48 +00:00
|
|
|
test('torrent: client.scrape()', function (t) {
|
2014-05-24 04:14:19 +00:00
|
|
|
t.plan(6)
|
2014-05-09 06:38:41 +00:00
|
|
|
|
2014-05-24 04:14:19 +00:00
|
|
|
createServer(function (err, server) {
|
2014-05-09 06:38:41 +00:00
|
|
|
t.error(err)
|
2014-05-24 04:14:19 +00:00
|
|
|
var client = new Client(peerId, port, parsedTorrent)
|
2014-05-09 06:38:41 +00:00
|
|
|
|
2014-05-24 04:14:19 +00:00
|
|
|
client.on('error', function (err) {
|
|
|
|
t.fail(err)
|
|
|
|
})
|
|
|
|
|
|
|
|
client.once('scrape', function (data) {
|
|
|
|
t.equal(data.announce, announceUrl)
|
|
|
|
t.equal(typeof data.complete, 'number')
|
|
|
|
t.equal(typeof data.incomplete, 'number')
|
|
|
|
t.equal(typeof data.downloaded, 'number')
|
2014-05-12 00:32:57 +00:00
|
|
|
|
2014-05-24 04:14:19 +00:00
|
|
|
server.close(function () {
|
|
|
|
t.pass('server close')
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
client.scrape()
|
|
|
|
})
|
2014-05-09 06:38:41 +00:00
|
|
|
})
|