-
-
Notifications
You must be signed in to change notification settings - Fork 85
Expand file tree
/
Copy pathbasic-node.js
More file actions
105 lines (89 loc) · 2.81 KB
/
basic-node.js
File metadata and controls
105 lines (89 loc) · 2.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
var fixtures = require('webtorrent-fixtures')
var http = require('http')
var DHT = require('bittorrent-dht')
var ed = require('ed25519-supercop')
var parseTorrent = require('../')
var crypto = require('crypto')
var test = require('tape')
test('http url to a torrent file, string', function (t) {
t.plan(3)
var server = http.createServer(function (req, res) {
t.pass('server got request')
res.end(fixtures.leaves.torrent)
})
server.listen(0, function () {
var port = server.address().port
var url = 'http://127.0.0.1:' + port
parseTorrent.remote(url, function (err, parsedTorrent) {
t.error(err)
t.deepEqual(parsedTorrent, fixtures.leaves.parsedTorrent)
server.close()
})
})
})
test('filesystem path to a torrent file, string', function (t) {
t.plan(2)
parseTorrent.remote(fixtures.leaves.torrentPath, function (err, parsedTorrent) {
t.error(err)
t.deepEqual(parsedTorrent, fixtures.leaves.parsedTorrent)
})
})
test('dht put/get of torrent (BEP46)', function (t) {
t.plan(6)
var infoHashBuf = Buffer.from(fixtures.numbers.parsedTorrent.infoHash, 'hex')
t.equal(infoHashBuf.length, 20, 'infoHashBuf is 20 bytes')
var keypair = ed.createKeyPair(ed.createSeed())
var dht = new DHT({ bootstrap: false, verify: ed.verify })
t.once('end', function () {
dht.destroy()
})
dht.on('warning', function (err) { t.fail(err) })
dht.on('error', function (err) { t.fail(err) })
dht.on('ready', function () {
var opts = {
k: keypair.publicKey,
sign: function (buf) {
return ed.sign(buf, keypair.publicKey, keypair.secretKey)
},
seq: 0,
v: {
ih: infoHashBuf
}
}
var expectedHash = crypto.createHash('sha1').update(opts.k).digest()
dht.put(opts, function (_, hash) {
t.equal(
hash.toString('hex'),
expectedHash.toString('hex'),
'hash of the public key'
)
// should perform a dht.get
parseTorrent.remote({
torrentId: 'magnet:?xs=urn:btpk:' + keypair.publicKey.toString('hex'),
dht: dht
}, function (err, parsedTorrent) {
t.ifError(err)
t.equal(infoHashBuf.toString('hex'), parsedTorrent.infoHash,
'got back what we put in'
)
// put a value without infohash (should throw)
opts.v = 'foo'
opts.seq++
dht.put(opts, function (_, hash) {
t.equal(
hash.toString('hex'),
expectedHash.toString('hex'),
'hash of the public key'
)
parseTorrent.remote({
torrentId: 'magnet:?xs=urn:btpk:' + keypair.publicKey.toString('hex'),
dht: dht
}, function (err, parsedTorrent) {
if (err) t.pass(err)
else t.fail('should have errored')
})
})
})
})
})
})