2014-06-07 22:15:00 +00:00
|
|
|
var Client = require('../')
|
2016-03-01 02:09:04 +00:00
|
|
|
var common = require('./common')
|
2014-03-27 07:17:49 +00:00
|
|
|
var test = require('tape')
|
2016-04-01 06:35:26 +00:00
|
|
|
// var wrtc = require('electron-webrtc')()
|
2016-03-24 08:35:19 +00:00
|
|
|
|
2016-04-01 06:35:26 +00:00
|
|
|
// var wrtcReady = false
|
|
|
|
// wrtc.electronDaemon.once('ready', function () {
|
|
|
|
// wrtcReady = true
|
|
|
|
// })
|
2014-03-27 07:17:49 +00:00
|
|
|
|
2014-12-12 10:02:11 +00:00
|
|
|
var infoHash = '4cb67059ed6bd08362da625b3ae77f6f4a075705'
|
|
|
|
var peerId = new Buffer('01234567890123456789')
|
|
|
|
var peerId2 = new Buffer('12345678901234567890')
|
2014-03-27 07:17:49 +00:00
|
|
|
|
2016-03-24 08:35:19 +00:00
|
|
|
function serverTest (t, serverType, serverFamily) {
|
2016-03-17 00:58:47 +00:00
|
|
|
t.plan(30)
|
2014-03-27 07:17:49 +00:00
|
|
|
|
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'
|
2014-03-27 07:17:49 +00:00
|
|
|
|
2016-03-01 02:09:04 +00:00
|
|
|
common.createServer(t, serverType, function (server) {
|
2015-01-29 22:59:08 +00:00
|
|
|
var port = server[serverType].address().port
|
2016-03-01 02:09:04 +00:00
|
|
|
var announceUrl = serverType + '://' + hostname + ':' + port + '/announce'
|
2014-03-27 07:17:49 +00:00
|
|
|
|
BREAKING: Client() takes single opts object now
To use the client, you used to pass in four arguments:
`new Client(peerId, port, parsedTorrent, opts)`
Now, passing in the torrent is no longer required, just the `announce`
and `infoHash` properties. This decouples this package from
`parse-torrent`.
All options get passed in together now:
new Client({
infoHash: '', // hex string or Buffer
peerId: '', // hex string or Buffer
announce: [], // list of tracker server urls
port: 6881 // torrent client port, (in browser, optional)
})
All the normal optional arguments (rtcConfig, wrtc, etc.) can still be
passed in with the rest of these options.
Fixes #118. Fixes #115.
Added ws tests for scrape.
2016-04-01 04:37:51 +00:00
|
|
|
var client1 = new Client({
|
2014-03-27 07:17:49 +00:00
|
|
|
infoHash: infoHash,
|
BREAKING: Client() takes single opts object now
To use the client, you used to pass in four arguments:
`new Client(peerId, port, parsedTorrent, opts)`
Now, passing in the torrent is no longer required, just the `announce`
and `infoHash` properties. This decouples this package from
`parse-torrent`.
All options get passed in together now:
new Client({
infoHash: '', // hex string or Buffer
peerId: '', // hex string or Buffer
announce: [], // list of tracker server urls
port: 6881 // torrent client port, (in browser, optional)
})
All the normal optional arguments (rtcConfig, wrtc, etc.) can still be
passed in with the rest of these options.
Fixes #118. Fixes #115.
Added ws tests for scrape.
2016-04-01 04:37:51 +00:00
|
|
|
announce: [ announceUrl ],
|
|
|
|
peerId: peerId,
|
2016-04-01 06:35:26 +00:00
|
|
|
port: 6881
|
|
|
|
// wrtc: wrtc
|
BREAKING: Client() takes single opts object now
To use the client, you used to pass in four arguments:
`new Client(peerId, port, parsedTorrent, opts)`
Now, passing in the torrent is no longer required, just the `announce`
and `infoHash` properties. This decouples this package from
`parse-torrent`.
All options get passed in together now:
new Client({
infoHash: '', // hex string or Buffer
peerId: '', // hex string or Buffer
announce: [], // list of tracker server urls
port: 6881 // torrent client port, (in browser, optional)
})
All the normal optional arguments (rtcConfig, wrtc, etc.) can still be
passed in with the rest of these options.
Fixes #118. Fixes #115.
Added ws tests for scrape.
2016-04-01 04:37:51 +00:00
|
|
|
})
|
2014-03-27 07:17:49 +00:00
|
|
|
|
2015-05-17 06:24:20 +00:00
|
|
|
client1.start()
|
2014-03-27 07:17:49 +00:00
|
|
|
|
2014-05-19 01:35:12 +00:00
|
|
|
server.once('start', function () {
|
|
|
|
t.pass('got start message from client1')
|
|
|
|
})
|
|
|
|
|
2015-05-17 06:24:20 +00:00
|
|
|
client1.once('update', function (data) {
|
2014-03-27 07:17:49 +00:00
|
|
|
t.equal(data.announce, announceUrl)
|
|
|
|
t.equal(data.complete, 0)
|
|
|
|
t.equal(data.incomplete, 1)
|
|
|
|
|
2016-01-03 18:50:23 +00:00
|
|
|
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-13 22:57:39 +00:00
|
|
|
|
2016-03-17 00:58:47 +00:00
|
|
|
var id = serverType === 'ws'
|
|
|
|
? peerId.toString('hex')
|
|
|
|
: hostname + ':6881'
|
|
|
|
|
|
|
|
t.equal(swarm.peers[id].type, serverType)
|
|
|
|
t.equal(swarm.peers[id].ip, clientIp)
|
|
|
|
t.equal(swarm.peers[id].peerId, peerId.toString('hex'))
|
|
|
|
t.equal(swarm.peers[id].complete, false)
|
|
|
|
if (serverType === 'ws') {
|
|
|
|
t.equal(typeof swarm.peers[id].port, 'number')
|
|
|
|
t.ok(swarm.peers[id].socket)
|
2016-03-13 22:57:39 +00:00
|
|
|
} else {
|
2016-03-17 00:58:47 +00:00
|
|
|
t.equal(swarm.peers[id].port, 6881)
|
|
|
|
t.notOk(swarm.peers[id].socket)
|
2016-03-13 22:57:39 +00:00
|
|
|
}
|
2014-04-19 06:00:40 +00:00
|
|
|
|
2016-01-03 18:50:23 +00:00
|
|
|
client1.complete()
|
2014-03-27 07:17:49 +00:00
|
|
|
|
2016-01-03 18:50:23 +00:00
|
|
|
client1.once('update', function (data) {
|
|
|
|
t.equal(data.announce, announceUrl)
|
|
|
|
t.equal(data.complete, 1)
|
|
|
|
t.equal(data.incomplete, 0)
|
2014-03-27 07:17:49 +00:00
|
|
|
|
2016-01-03 18:50:23 +00:00
|
|
|
client1.scrape()
|
2014-05-12 00:32:57 +00:00
|
|
|
|
2016-01-03 18:50:23 +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
|
|
|
|
BREAKING: Client() takes single opts object now
To use the client, you used to pass in four arguments:
`new Client(peerId, port, parsedTorrent, opts)`
Now, passing in the torrent is no longer required, just the `announce`
and `infoHash` properties. This decouples this package from
`parse-torrent`.
All options get passed in together now:
new Client({
infoHash: '', // hex string or Buffer
peerId: '', // hex string or Buffer
announce: [], // list of tracker server urls
port: 6881 // torrent client port, (in browser, optional)
})
All the normal optional arguments (rtcConfig, wrtc, etc.) can still be
passed in with the rest of these options.
Fixes #118. Fixes #115.
Added ws tests for scrape.
2016-04-01 04:37:51 +00:00
|
|
|
var client2 = new Client({
|
2016-01-03 18:50:23 +00:00
|
|
|
infoHash: infoHash,
|
BREAKING: Client() takes single opts object now
To use the client, you used to pass in four arguments:
`new Client(peerId, port, parsedTorrent, opts)`
Now, passing in the torrent is no longer required, just the `announce`
and `infoHash` properties. This decouples this package from
`parse-torrent`.
All options get passed in together now:
new Client({
infoHash: '', // hex string or Buffer
peerId: '', // hex string or Buffer
announce: [], // list of tracker server urls
port: 6881 // torrent client port, (in browser, optional)
})
All the normal optional arguments (rtcConfig, wrtc, etc.) can still be
passed in with the rest of these options.
Fixes #118. Fixes #115.
Added ws tests for scrape.
2016-04-01 04:37:51 +00:00
|
|
|
announce: [ announceUrl ],
|
|
|
|
peerId: peerId2,
|
2016-04-01 06:35:26 +00:00
|
|
|
port: 6882
|
|
|
|
// wrtc: wrtc
|
BREAKING: Client() takes single opts object now
To use the client, you used to pass in four arguments:
`new Client(peerId, port, parsedTorrent, opts)`
Now, passing in the torrent is no longer required, just the `announce`
and `infoHash` properties. This decouples this package from
`parse-torrent`.
All options get passed in together now:
new Client({
infoHash: '', // hex string or Buffer
peerId: '', // hex string or Buffer
announce: [], // list of tracker server urls
port: 6881 // torrent client port, (in browser, optional)
})
All the normal optional arguments (rtcConfig, wrtc, etc.) can still be
passed in with the rest of these options.
Fixes #118. Fixes #115.
Added ws tests for scrape.
2016-04-01 04:37:51 +00:00
|
|
|
})
|
2014-05-19 01:35:12 +00:00
|
|
|
|
2016-01-03 18:50:23 +00:00
|
|
|
client2.start()
|
2014-05-19 01:35:12 +00:00
|
|
|
|
2016-01-03 18:50:23 +00:00
|
|
|
server.once('start', function () {
|
|
|
|
t.pass('got start message from client2')
|
|
|
|
})
|
2014-05-12 00:32:57 +00:00
|
|
|
|
2016-01-03 18:50:23 +00:00
|
|
|
client2.once('peer', function (addr) {
|
2016-03-13 22:57:39 +00:00
|
|
|
t.ok(addr === hostname + ':6881' || addr === hostname + ':6882' || addr.id === peerId.toString('hex'))
|
2014-05-09 06:38:41 +00:00
|
|
|
|
2016-01-03 18:50:23 +00:00
|
|
|
client2.stop()
|
|
|
|
client2.once('update', function (data) {
|
2014-05-19 01:35:12 +00:00
|
|
|
t.equal(data.announce, announceUrl)
|
2016-01-03 18:50:23 +00:00
|
|
|
t.equal(data.complete, 1)
|
2014-05-19 01:35:12 +00:00
|
|
|
t.equal(data.incomplete, 0)
|
2016-01-03 18:50:23 +00:00
|
|
|
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
|
|
|
|
2016-03-27 03:29:05 +00:00
|
|
|
client1.destroy(function () {
|
|
|
|
server.close()
|
2016-04-01 06:35:26 +00:00
|
|
|
// if (serverType === 'ws') wrtc.close()
|
2016-03-27 03:29:05 +00:00
|
|
|
})
|
2016-01-03 18:50:23 +00:00
|
|
|
})
|
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
|
|
|
}
|
|
|
|
|
2016-04-01 06:35:26 +00:00
|
|
|
// test('websocket server', function (t) {
|
|
|
|
// if (wrtcReady) {
|
|
|
|
// runTest()
|
|
|
|
// } else {
|
|
|
|
// wrtc.electronDaemon.once('ready', runTest)
|
|
|
|
// }
|
|
|
|
// function runTest () {
|
|
|
|
// serverTest(t, 'ws', 'inet')
|
|
|
|
// }
|
|
|
|
// })
|
2016-03-13 22:57:39 +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')
|
2014-03-27 07:17:49 +00:00
|
|
|
})
|