|
| 1 | +/* eslint-env mocha */ |
| 2 | +'use strict' |
| 3 | + |
| 4 | +const isIpfs = require('is-ipfs') |
| 5 | +const loadFixture = require('aegir/fixtures') |
| 6 | +const hat = require('hat') |
| 7 | +const { getDescribe, getIt, expect } = require('../utils/mocha') |
| 8 | + |
| 9 | +module.exports = (createCommon, options) => { |
| 10 | + const describe = getDescribe(options) |
| 11 | + const it = getIt(options) |
| 12 | + const common = createCommon() |
| 13 | + |
| 14 | + describe('.resolve', () => { |
| 15 | + let ipfs |
| 16 | + |
| 17 | + before(function (done) { |
| 18 | + // CI takes longer to instantiate the daemon, so we need to increase the |
| 19 | + // timeout for the before step |
| 20 | + this.timeout(60 * 1000) |
| 21 | + |
| 22 | + common.setup((err, factory) => { |
| 23 | + expect(err).to.not.exist() |
| 24 | + factory.spawnNode((err, node) => { |
| 25 | + expect(err).to.not.exist() |
| 26 | + ipfs = node |
| 27 | + done() |
| 28 | + }) |
| 29 | + }) |
| 30 | + }) |
| 31 | + |
| 32 | + after((done) => { |
| 33 | + common.teardown(done) |
| 34 | + }) |
| 35 | + |
| 36 | + it('should resolve an IPFS hash', (done) => { |
| 37 | + const content = loadFixture('js/test/fixtures/testfile.txt', 'interface-ipfs-core') |
| 38 | + |
| 39 | + ipfs.files.add(content, (err, res) => { |
| 40 | + expect(err).to.not.exist() |
| 41 | + expect(isIpfs.cid(res[0].hash)).to.be.true() |
| 42 | + |
| 43 | + ipfs.resolve(`/ipfs/${res[0].hash}`, (err, path) => { |
| 44 | + expect(err).to.not.exist() |
| 45 | + expect(path).to.equal(`/ipfs/${res[0].hash}`) |
| 46 | + done() |
| 47 | + }) |
| 48 | + }) |
| 49 | + }) |
| 50 | + |
| 51 | + // Test resolve turns /ipfs/QmRootHash/path/to/file into /ipfs/QmFileHash |
| 52 | + it('should resolve an IPFS path link', (done) => { |
| 53 | + const path = '/path/to/testfile.txt' |
| 54 | + const content = loadFixture('js/test/fixtures/testfile.txt', 'interface-ipfs-core') |
| 55 | + |
| 56 | + ipfs.files.add([{ path, content }], { wrapWithDirectory: true }, (err, res) => { |
| 57 | + expect(err).to.not.exist() |
| 58 | + |
| 59 | + const rootHash = res.find(r => r.path === '').hash |
| 60 | + const fileHash = res.find(r => r.path === path).hash |
| 61 | + |
| 62 | + ipfs.resolve(`/ipfs/${rootHash}${path}`, (err, path) => { |
| 63 | + expect(err).to.not.exist() |
| 64 | + expect(path).to.equal(`/ipfs/${fileHash}`) |
| 65 | + done() |
| 66 | + }) |
| 67 | + }) |
| 68 | + }) |
| 69 | + |
| 70 | + it('should not resolve an IPFS path non-link', (done) => { |
| 71 | + const content = { path: { to: { file: hat() } } } |
| 72 | + const options = { format: 'dag-cbor', hashAlg: 'sha2-256' } |
| 73 | + |
| 74 | + ipfs.dag.put(content, options, (err, cid) => { |
| 75 | + expect(err).to.not.exist() |
| 76 | + |
| 77 | + const path = `/ipfs/${cid.toBaseEncodedString()}/path/to/file` |
| 78 | + ipfs.resolve(path, (err, path) => { |
| 79 | + expect(err).to.exist() |
| 80 | + expect(err.message).to.equal('found non-link at given path') |
| 81 | + done() |
| 82 | + }) |
| 83 | + }) |
| 84 | + }) |
| 85 | + |
| 86 | + // Test resolve turns /ipns/domain.com into /ipfs/QmHash |
| 87 | + it('should resolve an IPNS DNS link', function (done) { |
| 88 | + this.timeout(20 * 1000) |
| 89 | + |
| 90 | + ipfs.resolve('/ipns/ipfs.io', (err, path) => { |
| 91 | + expect(err).to.not.exist() |
| 92 | + expect(isIpfs.ipfsPath(path)).to.be.true() |
| 93 | + done() |
| 94 | + }) |
| 95 | + }) |
| 96 | + |
| 97 | + // Test resolve turns /ipns/QmPeerHash into /ipns/domain.com into /ipfs/QmHash |
| 98 | + it('should resolve IPNS link recursively', function (done) { |
| 99 | + this.timeout(2 * 60 * 1000) |
| 100 | + |
| 101 | + ipfs.name.publish('/ipns/ipfs.io', { resolve: false }, (err, res) => { |
| 102 | + expect(err).to.not.exist() |
| 103 | + |
| 104 | + ipfs.resolve(`/ipns/${res.name}`, { recursive: true }, (err, res) => { |
| 105 | + expect(err).to.not.exist() |
| 106 | + expect(res).to.not.equal('/ipns/ipfs.io') |
| 107 | + expect(isIpfs.ipfsPath(res)).to.be.true() |
| 108 | + done() |
| 109 | + }) |
| 110 | + }) |
| 111 | + }) |
| 112 | + }) |
| 113 | +} |
0 commit comments