Skip to content
This repository was archived by the owner on Aug 12, 2020. It is now read-only.

Commit 444227c

Browse files
committed
test: add tests for offset and length
Also fixes an issue with the file resolver and 0 length export. License: MIT Signed-off-by: Alan Shaw <alan@tableflip.io>
1 parent 11885fa commit 444227c

File tree

3 files changed

+140
-28
lines changed

3 files changed

+140
-28
lines changed

src/exporter/file.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ module.exports = (cid, node, name, path, pathRest, resolve, size, dag, parent, d
1919
const fileSize = size || file.fileSize()
2020

2121
if (offset < 0) {
22-
return pull.error(new Error('Offset must be greater than 0'))
22+
return pull.error(new Error('Offset must be greater than or equal to 0'))
2323
}
2424

2525
if (offset > fileSize) {
@@ -31,7 +31,15 @@ module.exports = (cid, node, name, path, pathRest, resolve, size, dag, parent, d
3131
}
3232

3333
if (length === 0) {
34-
return pull.once(Buffer.alloc(0))
34+
return pull.once({
35+
depth: depth,
36+
content: pull.once(Buffer.alloc(0)),
37+
name: name,
38+
path: path,
39+
hash: cid,
40+
size: fileSize,
41+
type: 'file'
42+
})
3543
}
3644

3745
if (!offset) {

src/exporter/raw.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ module.exports = (cid, node, name, path, pathRest, resolve, size, dag, parent, d
1414
size = size || node.length
1515

1616
if (offset < 0) {
17-
return pull.error(new Error('Offset must be greater than 0'))
17+
return pull.error(new Error('Offset must be greater than or equal to 0'))
1818
}
1919

2020
if (offset > size) {

test/exporter.js

Lines changed: 129 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,15 @@ module.exports = (repo) => {
3636
describe('exporter', () => {
3737
let ipld
3838

39-
function addTestFile ({file, strategy = 'balanced', path = '/foo', maxChunkSize}, cb) {
39+
function addTestFile ({file, strategy = 'balanced', path = '/foo', maxChunkSize, rawLeaves}, cb) {
4040
pull(
4141
pull.values([{
4242
path,
4343
content: file
4444
}]),
4545
importer(ipld, {
4646
strategy,
47+
rawLeaves,
4748
chunkerOptions: {
4849
maxChunkSize
4950
}
@@ -54,8 +55,8 @@ module.exports = (repo) => {
5455
)
5556
}
5657

57-
function addAndReadTestFile ({file, offset, length, strategy = 'balanced', path = '/foo', maxChunkSize}, cb) {
58-
addTestFile({file, strategy, path, maxChunkSize}, (error, multihash) => {
58+
function addAndReadTestFile ({file, offset, length, strategy = 'balanced', path = '/foo', maxChunkSize, rawLeaves}, cb) {
59+
addTestFile({file, strategy, path, maxChunkSize, rawLeaves}, (error, multihash) => {
5960
if (error) {
6061
return cb(error)
6162
}
@@ -322,6 +323,21 @@ module.exports = (repo) => {
322323
)
323324
})
324325

326+
it('exports a zero length chunk of a large file', function (done) {
327+
this.timeout(30 * 1000)
328+
329+
addAndReadTestFile({
330+
file: bigFile,
331+
path: '1.2MiB.txt',
332+
rawLeaves: true,
333+
length: 0
334+
}, (err, data) => {
335+
expect(err).to.not.exist()
336+
expect(data).to.eql(Buffer.alloc(0))
337+
done()
338+
})
339+
})
340+
325341
it('exports a chunk of a large file > 5mb made from multiple blocks', function (done) {
326342
this.timeout(30 * 1000)
327343
const hash = 'QmRQgufjp9vLE8XK2LGKZSsPCFCF6e4iynCQtNB5X2HBKE'
@@ -424,31 +440,119 @@ module.exports = (repo) => {
424440
it('exports a small file imported with raw leaves', function (done) {
425441
this.timeout(30 * 1000)
426442

427-
pull(
428-
pull.values([{
429-
path: '200Bytes.txt',
430-
content: pull.values([smallFile])
431-
}]),
432-
importer(ipld, {
433-
rawLeaves: true
434-
}),
435-
pull.collect(collected)
436-
)
443+
addAndReadTestFile({
444+
file: smallFile,
445+
path: '200Bytes.txt',
446+
rawLeaves: true
447+
}, (err, data) => {
448+
expect(err).to.not.exist()
449+
expect(data).to.eql(smallFile)
450+
done()
451+
})
452+
})
437453

438-
function collected (err, files) {
454+
it('exports a chunk of a small file imported with raw leaves', function (done) {
455+
this.timeout(30 * 1000)
456+
457+
const length = 100
458+
459+
addAndReadTestFile({
460+
file: smallFile,
461+
path: '200Bytes.txt',
462+
rawLeaves: true,
463+
length
464+
}, (err, data) => {
439465
expect(err).to.not.exist()
440-
expect(files.length).to.equal(1)
466+
expect(data).to.eql(smallFile.slice(0, length))
467+
done()
468+
})
469+
})
441470

442-
pull(
443-
exporter(files[0].multihash, ipld),
444-
pull.collect((err, files) => {
445-
expect(err).to.not.exist()
446-
expect(new CID(files[0].hash).toBaseEncodedString()).to.equal('zb2rhXrz1gkCv8p4nUDZRohY6MzBE9C3HVTVDP72g6Du3SD9Q')
471+
it('exports a chunk of a small file imported with raw leaves with length', function (done) {
472+
this.timeout(30 * 1000)
447473

448-
fileEql(files[0], smallFile, done)
449-
})
450-
)
451-
}
474+
const offset = 100
475+
const length = 200
476+
477+
addAndReadTestFile({
478+
file: smallFile,
479+
path: '200Bytes.txt',
480+
rawLeaves: true,
481+
offset,
482+
length
483+
}, (err, data) => {
484+
expect(err).to.not.exist()
485+
expect(data).to.eql(smallFile.slice(offset))
486+
done()
487+
})
488+
})
489+
490+
it('exports a zero length chunk of a small file imported with raw leaves', function (done) {
491+
this.timeout(30 * 1000)
492+
493+
const length = 0
494+
495+
addAndReadTestFile({
496+
file: smallFile,
497+
path: '200Bytes.txt',
498+
rawLeaves: true,
499+
length
500+
}, (err, data) => {
501+
expect(err).to.not.exist()
502+
expect(data).to.eql(Buffer.alloc(0))
503+
done()
504+
})
505+
})
506+
507+
it('errors when exporting a chunk of a small file imported with raw leaves and negative length', function (done) {
508+
this.timeout(30 * 1000)
509+
510+
const length = -100
511+
512+
addAndReadTestFile({
513+
file: smallFile,
514+
path: '200Bytes.txt',
515+
rawLeaves: true,
516+
length
517+
}, (err, data) => {
518+
expect(err).to.exist()
519+
expect(err.message).to.equal('Length must be greater than or equal to 0')
520+
done()
521+
})
522+
})
523+
524+
it('errors when exporting a chunk of a small file imported with raw leaves and negative offset', function (done) {
525+
this.timeout(30 * 1000)
526+
527+
const offset = -100
528+
529+
addAndReadTestFile({
530+
file: smallFile,
531+
path: '200Bytes.txt',
532+
rawLeaves: true,
533+
offset
534+
}, (err, data) => {
535+
expect(err).to.exist()
536+
expect(err.message).to.equal('Offset must be greater than or equal to 0')
537+
done()
538+
})
539+
})
540+
541+
it('errors when exporting a chunk of a small file imported with raw leaves and offset greater than file size', function (done) {
542+
this.timeout(30 * 1000)
543+
544+
const offset = 201
545+
546+
addAndReadTestFile({
547+
file: smallFile,
548+
path: '200Bytes.txt',
549+
rawLeaves: true,
550+
offset
551+
}, (err, data) => {
552+
expect(err).to.exist()
553+
expect(err.message).to.equal('Offset must be less than the file size')
554+
done()
555+
})
452556
})
453557

454558
it('exports a large file > 1mb imported with raw leaves', function (done) {
@@ -512,7 +616,7 @@ module.exports = (repo) => {
512616
offset: -1
513617
}, (error, data) => {
514618
expect(error).to.be.ok()
515-
expect(error.message).to.contain('Offset must be greater than 0')
619+
expect(error.message).to.contain('Offset must be greater than or equal to 0')
516620

517621
done()
518622
})

0 commit comments

Comments
 (0)