bittorrent-tracker/test/client.js

220 lines
5.4 KiB
JavaScript
Raw Normal View History

var Client = require('../')
var common = require('./common')
2014-03-26 08:17:59 +00:00
var fs = require('fs')
var parseTorrent = require('parse-torrent')
var test = require('tape')
var torrent = fs.readFileSync(__dirname + '/torrents/bitlove-intro.torrent')
var parsedTorrent = parseTorrent(torrent)
2014-07-11 10:50:35 +00:00
var peerId1 = new Buffer('01234567890123456789')
2014-07-11 10:50:48 +00:00
var peerId2 = new Buffer('12345678901234567890')
var peerId3 = new Buffer('23456789012345678901')
2014-03-26 08:17:59 +00:00
var port = 6881
2014-07-11 10:50:35 +00:00
function testClientStart (t, serverType) {
2014-07-11 04:00:02 +00:00
t.plan(5)
common.createServer(t, serverType, function (server, announceUrl) {
parsedTorrent.announce = [ announceUrl ]
2014-07-11 10:50:35 +00:00
var client = new Client(peerId1, port, parsedTorrent)
2014-03-26 08:17:59 +00:00
2014-05-24 04:14:19 +00:00
client.on('error', function (err) {
2014-07-11 04:00:02 +00:00
t.error(err)
2014-05-24 04:14:19 +00:00
})
2014-03-26 08:17:59 +00:00
2014-07-11 04:58:19 +00:00
client.on('warning', function (err) {
t.error(err)
})
2014-03-26 08:17:59 +00:00
client.once('update', function (data) {
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-11-26 12:11:49 +00:00
client.once('peer', function () {
2014-05-24 04:14:19 +00:00
t.pass('there is at least one peer')
client.stop()
client.once('update', function () {
server.close(function () {
t.pass('server close')
})
})
})
2014-03-26 08:17:59 +00:00
2014-05-24 04:14:19 +00:00
client.start()
})
2014-07-11 10:50:35 +00:00
}
test('http: client.start()', function (t) {
testClientStart(t, 'http')
2014-05-24 04:14:19 +00:00
})
2014-03-26 08:17:59 +00:00
2014-07-11 10:50:35 +00:00
test('udp: client.start()', function (t) {
testClientStart(t, 'udp')
})
2014-03-26 08:17:59 +00:00
2014-07-11 10:50:35 +00:00
function testClientStop (t, serverType) {
t.plan(4)
common.createServer(t, serverType, function (server, announceUrl) {
parsedTorrent.announce = [ announceUrl ]
2014-07-11 10:50:35 +00:00
var client = new Client(peerId1, port, parsedTorrent)
2014-03-26 08:17:59 +00:00
2014-05-24 04:14:19 +00:00
client.on('error', function (err) {
2014-07-11 04:00:02 +00:00
t.error(err)
2014-05-24 04:14:19 +00:00
})
2014-03-26 08:17:59 +00:00
2014-07-11 04:58:19 +00:00
client.on('warning', function (err) {
t.error(err)
})
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)
})
2014-07-11 10:50:35 +00:00
}
test('http: client.stop()', function (t) {
testClientStop(t, 'http')
2014-05-24 04:14:19 +00:00
})
2014-07-11 10:50:35 +00:00
test('udp: client.stop()', function (t) {
testClientStop(t, 'udp')
})
2014-05-24 04:14:19 +00:00
2014-07-11 10:50:35 +00:00
function testClientUpdate (t, serverType) {
t.plan(4)
common.createServer(t, serverType, function (server, announceUrl) {
parsedTorrent.announce = [ announceUrl ]
2015-03-24 08:02:10 +00:00
var client = new Client(peerId1, port, parsedTorrent, { interval: 2000 })
2014-05-24 04:14:19 +00:00
client.on('error', function (err) {
2014-07-11 04:00:02 +00:00
t.error(err)
2014-03-26 08:17:59 +00:00
})
2014-07-11 04:58:19 +00:00
client.on('warning', function (err) {
t.error(err)
})
2014-05-24 04:14:19 +00:00
client.start()
client.once('update', function () {
2015-03-24 08:02:10 +00:00
// after interval (2s), we should get another update
2014-05-24 04:14:19 +00:00
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-07-11 10:50:35 +00:00
}
test('http: client.update()', function (t) {
testClientUpdate(t, 'http')
2014-03-26 08:17:59 +00:00
})
2014-07-11 10:50:35 +00:00
test('udp: client.update()', function (t) {
testClientUpdate(t, 'udp')
})
2014-07-11 10:50:35 +00:00
function testClientScrape (t, serverType) {
t.plan(5)
common.createServer(t, serverType, function (server, announceUrl) {
parsedTorrent.announce = [ announceUrl ]
2014-07-11 10:50:35 +00:00
var client = new Client(peerId1, port, parsedTorrent)
2014-05-24 04:14:19 +00:00
client.on('error', function (err) {
2014-07-11 04:00:02 +00:00
t.error(err)
2014-05-24 04:14:19 +00:00
})
2014-07-11 04:58:19 +00:00
client.on('warning', function (err) {
t.error(err)
})
2014-05-24 04:14:19 +00:00
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-07-11 10:50:35 +00:00
}
test('http: client.scrape()', function (t) {
testClientScrape(t, 'http')
})
test('udp: client.scrape()', function (t) {
testClientScrape(t, 'udp')
})
2014-07-11 10:50:48 +00:00
function testClientAnnounceWithNumWant (t, serverType) {
t.plan(1)
common.createServer(t, serverType, function (server, announceUrl) {
parsedTorrent.announce = [ announceUrl ]
2014-07-11 10:50:48 +00:00
var client1 = new Client(peerId1, port, parsedTorrent)
client1.on('error', function (err) {
t.error(err)
})
client1.start()
2014-11-26 12:11:49 +00:00
client1.once('update', function () {
2014-07-11 10:50:48 +00:00
var client2 = new Client(peerId2, port + 1, parsedTorrent)
client2.on('error', function (err) {
t.error(err)
})
client2.start()
client2.once('update', function () {
var client3 = new Client(peerId3, port + 2, parsedTorrent, { numWant: 1 })
client3.on('error', function (err) {
t.error(err)
})
client3.start()
client3.on('peer', function () {
t.pass('got one peer (this should only fire once)')
client1.stop()
client2.stop()
client3.stop()
server.close()
})
})
})
})
}
test('http: client announce with numWant', function (t) {
testClientAnnounceWithNumWant(t, 'http')
})
test('udp: client announce with numWant', function (t) {
testClientAnnounceWithNumWant(t, 'udp')
})