Welcome to mirror list, hosted at ThFree Co, Russian Federation.

storage.js « test - github.com/webtorrent/webtorrent.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 15c39875bab43b250c8bf14c813f53fef141122d (plain)
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
var fs = require('fs')
var parseTorrent = require('parse-torrent')
var Storage = require('../lib/storage')
var test = require('tape')

var torrents = [ 'leaves', 'pride' ].map(function (name) {
  var torrent = fs.readFileSync(__dirname + '/torrents/' + name + '.torrent')

  return {
    name: name,
    torrent: torrent,
    parsedTorrent: parseTorrent(torrent)
  }
})

torrents.forEach(function (torrent) {
  test('sanity check backing storage for ' + torrent.name + ' torrent', function (t) {
    var parsedTorrent = torrent.parsedTorrent
    var storage = new Storage(parsedTorrent)

    t.equal(storage.files.length, parsedTorrent.files.length)
    t.equal(storage.pieces.length, parsedTorrent.pieces.length)

    var length = 0
    var pieces = 0

    storage.pieces.forEach(function (piece) {
      t.notOk(piece.verified)
      length += piece.length

      // ensure all blocks start out empty
      for (var i = 0; i < piece.blocks.length; ++i) {
        t.equal(piece.blocks[i], 0)
      }
    })

    t.equal(length, parsedTorrent.length)
    length = 0

    storage.files.forEach(function (file) {
      t.notOk(file.done)
      length += file.length
      pieces += file.pieces.length

      t.assert(file.length >= 0)
      t.assert(file.pieces.length >= 0)
    })

    t.equal(length, parsedTorrent.length)

    if (parsedTorrent.files.length > 1) {
      // if the torrent contains multiple files, the pieces may overlap file boundaries,
      // so the aggregate number of file pieces will be at least the number of pieces.
      t.assert(pieces >= parsedTorrent.pieces.length)
    } else {
      t.equal(pieces, parsedTorrent.pieces.length)
    }

    t.end()
  })
})