diff options
author | Feross Aboukhadijeh <feross@feross.org> | 2014-09-21 05:41:24 +0400 |
---|---|---|
committer | Feross Aboukhadijeh <feross@feross.org> | 2014-09-21 05:41:24 +0400 |
commit | 93686505fbc90522c75b6c151ec7261aa76098de (patch) | |
tree | 5b474b920e79b7b39b6804fac5e6a641de6ae843 /test/storage.js | |
parent | 2e14192c311f64c20496a72af7ffce36495be92b (diff) |
merge `bittorrent-client` into this module
When I started the WebTorrent project I thought there were going to
need to be two separate client implementations (bittorrent-client and
webtorrent-client) that would get tied together in a higher-level
module.
Fortunately, this was not necessary because of the awesome “browser”
field support in browserify. By substituting just a few modules, we can
make the same module (webtorrent) work in node AND the browser, with
the same codebase!
So, from now on, you can just `require(‘webtorrent’)` in node or the
browser, and it will just work. You can also `npm install webtorrent`
if you want to use bittorrent in a node app or script. Lastly, you can
`npm install webtorrent -g` if you want to use webtorrent as a command
line app (it installs a `webtorrent` command).
Diffstat (limited to 'test/storage.js')
-rw-r--r-- | test/storage.js | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/test/storage.js b/test/storage.js new file mode 100644 index 0000000..6dfc04b --- /dev/null +++ b/test/storage.js @@ -0,0 +1,60 @@ +var Storage = require('../lib/storage') +var parseTorrent = require('parse-torrent') +var test = require('tape') +var fs = require('fs') + +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, pieces = 0 + + storage.pieces.forEach(function (piece, index) { + 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, index) { + 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() + }) +}) |