2014-06-07 22:15:00 +00:00
|
|
|
var Client = require('../')
|
2014-03-27 07:17:49 +00:00
|
|
|
var Server = require('../').Server
|
|
|
|
var test = require('tape')
|
|
|
|
|
2014-12-09 22:18:47 +00:00
|
|
|
var infoHash = new Buffer('4cb67059ed6bd08362da625b3ae77f6f4a075705', 'hex')
|
2014-05-19 01:35:12 +00:00
|
|
|
var peerId = '01234567890123456789'
|
|
|
|
var peerId2 = '12345678901234567890'
|
2014-03-27 07:17:49 +00:00
|
|
|
var torrentLength = 50000
|
|
|
|
|
2014-07-11 10:49:45 +00:00
|
|
|
function serverTest (t, serverType) {
|
2014-08-17 02:05:56 +00:00
|
|
|
t.plan(26)
|
2014-03-27 07:17:49 +00:00
|
|
|
|
2014-07-11 10:49:45 +00:00
|
|
|
var opts = serverType === 'http' ? { udp: false } : { http: false }
|
|
|
|
var server = new Server(opts)
|
2014-03-27 07:17:49 +00:00
|
|
|
|
|
|
|
server.on('error', function (err) {
|
|
|
|
t.fail(err.message)
|
|
|
|
})
|
|
|
|
|
2014-05-24 01:03:50 +00:00
|
|
|
server.on('warning', function (err) {
|
|
|
|
t.fail(err.message)
|
|
|
|
})
|
|
|
|
|
2014-03-27 07:17:49 +00:00
|
|
|
server.on('listening', function () {
|
|
|
|
t.pass('server listening')
|
|
|
|
})
|
|
|
|
|
2014-08-17 02:05:56 +00:00
|
|
|
server.listen(function (port) {
|
2014-12-05 01:41:57 +00:00
|
|
|
var announceUrl = serverType + '://127.0.0.1:' + port + '/announce'
|
2014-03-27 07:17:49 +00:00
|
|
|
|
|
|
|
var client = new Client(peerId, 6881, {
|
|
|
|
infoHash: infoHash,
|
|
|
|
length: torrentLength,
|
|
|
|
announce: [ announceUrl ]
|
|
|
|
})
|
|
|
|
|
|
|
|
client.start()
|
|
|
|
|
2014-05-19 01:35:12 +00:00
|
|
|
server.once('start', function () {
|
|
|
|
t.pass('got start message from client1')
|
|
|
|
})
|
|
|
|
|
2014-03-27 07:17:49 +00:00
|
|
|
client.once('update', function (data) {
|
|
|
|
t.equal(data.announce, announceUrl)
|
|
|
|
t.equal(data.complete, 0)
|
|
|
|
t.equal(data.incomplete, 1)
|
|
|
|
|
2014-04-19 06:00:40 +00:00
|
|
|
t.equal(Object.keys(server.torrents).length, 1)
|
2014-07-11 05:05:56 +00:00
|
|
|
t.equal(server.getSwarm(infoHash).complete, 0)
|
|
|
|
t.equal(server.getSwarm(infoHash).incomplete, 1)
|
|
|
|
t.equal(Object.keys(server.getSwarm(infoHash).peers).length, 1)
|
|
|
|
t.deepEqual(server.getSwarm(infoHash).peers['127.0.0.1:6881'], {
|
2014-04-19 06:00:40 +00:00
|
|
|
ip: '127.0.0.1',
|
|
|
|
port: 6881,
|
|
|
|
peerId: peerId
|
|
|
|
})
|
|
|
|
|
2014-03-27 07:17:49 +00:00
|
|
|
client.complete()
|
|
|
|
|
|
|
|
client.once('update', function (data) {
|
|
|
|
t.equal(data.announce, announceUrl)
|
|
|
|
t.equal(data.complete, 1)
|
|
|
|
t.equal(data.incomplete, 0)
|
|
|
|
|
2014-05-09 06:38:41 +00:00
|
|
|
client.scrape()
|
2014-05-12 00:32:57 +00:00
|
|
|
|
2014-05-09 06:38:41 +00:00
|
|
|
client.once('scrape', function (data) {
|
2014-03-27 07:17:49 +00:00
|
|
|
t.equal(data.announce, announceUrl)
|
2014-05-09 06:38:41 +00:00
|
|
|
t.equal(typeof data.complete, 'number')
|
|
|
|
t.equal(typeof data.incomplete, 'number')
|
|
|
|
t.equal(typeof data.downloaded, 'number')
|
|
|
|
|
2014-05-19 01:35:12 +00:00
|
|
|
var client2 = new Client(peerId2, 6882, {
|
2014-05-18 16:15:20 +00:00
|
|
|
infoHash: infoHash,
|
|
|
|
length: torrentLength,
|
|
|
|
announce: [ announceUrl ]
|
|
|
|
})
|
|
|
|
|
2014-05-19 01:35:12 +00:00
|
|
|
client2.start()
|
|
|
|
|
|
|
|
server.once('start', function () {
|
|
|
|
t.pass('got start message from client2')
|
|
|
|
})
|
|
|
|
|
2014-05-18 16:15:20 +00:00
|
|
|
client2.once('peer', function (addr) {
|
|
|
|
t.equal(addr, '127.0.0.1:6881')
|
2014-05-12 00:32:57 +00:00
|
|
|
|
2014-05-19 01:35:12 +00:00
|
|
|
client2.stop()
|
|
|
|
client2.once('update', function (data) {
|
2014-05-18 16:15:20 +00:00
|
|
|
t.equal(data.announce, announceUrl)
|
2014-05-19 01:35:12 +00:00
|
|
|
t.equal(data.complete, 1)
|
2014-05-18 16:15:20 +00:00
|
|
|
t.equal(data.incomplete, 0)
|
2014-05-09 06:38:41 +00:00
|
|
|
|
2014-05-19 01:35:12 +00:00
|
|
|
client.stop()
|
|
|
|
client.once('update', function (data) {
|
|
|
|
t.equal(data.announce, announceUrl)
|
|
|
|
t.equal(data.complete, 0)
|
|
|
|
t.equal(data.incomplete, 0)
|
|
|
|
|
2014-05-24 03:43:45 +00:00
|
|
|
server.close(function () {
|
|
|
|
t.pass('server closed')
|
|
|
|
})
|
2014-05-19 01:35:12 +00:00
|
|
|
})
|
2014-05-18 16:15:20 +00:00
|
|
|
})
|
2014-05-09 06:38:41 +00:00
|
|
|
})
|
2014-03-27 07:17:49 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2014-07-11 10:49:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
test('http server', function (t) {
|
|
|
|
serverTest(t, 'http')
|
|
|
|
})
|
|
|
|
|
|
|
|
test('udp server', function (t) {
|
2014-12-04 16:38:03 +00:00
|
|
|
serverTest(t, 'udp')
|
2014-03-27 07:17:49 +00:00
|
|
|
})
|