bittorrent-tracker/test/server.js

122 lines
3.3 KiB
JavaScript
Raw Normal View History

var Client = require('../')
2016-03-01 02:09:04 +00:00
var common = require('./common')
var test = require('tape')
var infoHash = '4cb67059ed6bd08362da625b3ae77f6f4a075705'
var peerId = new Buffer('01234567890123456789')
var peerId2 = new Buffer('12345678901234567890')
var torrentLength = 50000
2014-12-12 23:09:40 +00:00
function serverTest (t, serverType, serverFamily) {
2016-03-01 02:09:04 +00:00
t.plan(25)
2016-03-01 02:09:04 +00:00
var hostname = serverFamily === 'inet6'
? '[::1]'
: '127.0.0.1'
var clientIp = serverFamily === 'inet6'
? '::1'
: '127.0.0.1'
2016-03-01 02:09:04 +00:00
common.createServer(t, serverType, function (server) {
var port = server[serverType].address().port
2016-03-01 02:09:04 +00:00
var announceUrl = serverType + '://' + hostname + ':' + port + '/announce'
var client1 = new Client(peerId, 6881, {
infoHash: infoHash,
length: torrentLength,
announce: [ announceUrl ]
})
client1.start()
2014-05-19 01:35:12 +00:00
server.once('start', function () {
t.pass('got start message from client1')
})
client1.once('update', function (data) {
t.equal(data.announce, announceUrl)
t.equal(data.complete, 0)
t.equal(data.incomplete, 1)
server.getSwarm(infoHash, function (err, swarm) {
t.error(err)
t.equal(Object.keys(server.torrents).length, 1)
t.equal(swarm.complete, 0)
t.equal(swarm.incomplete, 1)
t.equal(Object.keys(swarm.peers).length, 1)
2016-03-01 02:09:04 +00:00
t.deepEqual(swarm.peers[hostname + ':6881'], {
ip: clientIp,
port: 6881,
peerId: peerId.toString('hex'),
complete: false,
socket: undefined
})
2014-04-19 06:00:40 +00:00
client1.complete()
client1.once('update', function (data) {
t.equal(data.announce, announceUrl)
t.equal(data.complete, 1)
t.equal(data.incomplete, 0)
client1.scrape()
2014-05-12 00:32:57 +00:00
client1.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-18 16:15:20 +00:00
var client2 = new Client(peerId2, 6882, {
infoHash: infoHash,
length: torrentLength,
announce: [ announceUrl ]
})
2014-05-19 01:35:12 +00:00
client2.start()
2014-05-19 01:35:12 +00:00
server.once('start', function () {
t.pass('got start message from client2')
})
2014-05-12 00:32:57 +00:00
client2.once('peer', function (addr) {
2016-03-01 02:09:04 +00:00
t.ok(addr === hostname + ':6881' || addr === hostname + ':6882')
client2.stop()
client2.once('update', function (data) {
2014-05-19 01:35:12 +00:00
t.equal(data.announce, announceUrl)
t.equal(data.complete, 1)
2014-05-19 01:35:12 +00:00
t.equal(data.incomplete, 0)
client2.destroy()
client1.stop()
client1.once('update', function (data) {
t.equal(data.announce, announceUrl)
t.equal(data.complete, 0)
t.equal(data.incomplete, 0)
2014-05-19 01:35:12 +00:00
client1.destroy()
server.close()
})
2014-05-19 01:35:12 +00:00
})
2014-05-18 16:15:20 +00:00
})
})
})
})
})
})
2014-07-11 10:49:45 +00:00
}
2014-12-12 23:09:40 +00:00
test('http ipv4 server', function (t) {
serverTest(t, 'http', 'inet')
})
test('http ipv6 server', function (t) {
serverTest(t, 'http', 'inet6')
2014-07-11 10:49:45 +00:00
})
test('udp server', function (t) {
2014-12-12 23:09:40 +00:00
serverTest(t, 'udp', 'inet')
})