|
| 1 | +/* eslint-env mocha */ |
| 2 | + |
| 3 | +const expect = require('chai').expect |
| 4 | +const IPFS = require('../../src/core') |
| 5 | +const IPFSRepo = require('ipfs-repo') |
| 6 | + |
| 7 | +function createTestRepo () { |
| 8 | + const repoPath = '/tmp/ipfs-test-' + Math.random().toString().substring(2, 8) + '/' |
| 9 | + |
| 10 | + var store |
| 11 | + var teardown |
| 12 | + |
| 13 | + const isNode = !global.window |
| 14 | + if (isNode) { |
| 15 | + store = require('fs-blob-store') |
| 16 | + teardown = (done) => { |
| 17 | + const rimraf = require('rimraf') |
| 18 | + rimraf(repoPath, (err) => { |
| 19 | + expect(err).to.not.exist |
| 20 | + done() |
| 21 | + }) |
| 22 | + } |
| 23 | + } else { |
| 24 | + const idb = window.indexedDB || |
| 25 | + window.mozIndexedDB || |
| 26 | + window.webkitIndexedDB || |
| 27 | + window.msIndexedDB |
| 28 | + store = require('idb-plus-blob-store') |
| 29 | + teardown = (done) => { |
| 30 | + idb.deleteDatabase(repoPath) |
| 31 | + idb.deleteDatabase(repoPath + '/blocks') |
| 32 | + done() |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + const options = { |
| 37 | + bits: 64, |
| 38 | + stores: { |
| 39 | + keys: store, |
| 40 | + config: store, |
| 41 | + datastore: store, |
| 42 | + logs: store, |
| 43 | + locks: store, |
| 44 | + version: store |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + var repo = new IPFSRepo(repoPath, options) |
| 49 | + |
| 50 | + repo.teardown = teardown |
| 51 | + |
| 52 | + return repo |
| 53 | +} |
| 54 | + |
| 55 | +describe('init', function () { |
| 56 | + this.timeout(10000) |
| 57 | + |
| 58 | + it('basic', (done) => { |
| 59 | + var repo = createTestRepo() |
| 60 | + const ipfs = new IPFS(repo) |
| 61 | + ipfs.init({ emptyRepo: true }, (err) => { |
| 62 | + expect(err).to.not.exist |
| 63 | + |
| 64 | + repo.exists((err, res) => { |
| 65 | + expect(err).to.not.exist |
| 66 | + expect(res).to.equal(true) |
| 67 | + |
| 68 | + repo.config.get((err, config) => { |
| 69 | + expect(err).to.not.exist |
| 70 | + expect(config.Identity).to.exist |
| 71 | + |
| 72 | + repo.teardown(done) |
| 73 | + }) |
| 74 | + }) |
| 75 | + }) |
| 76 | + }) |
| 77 | + |
| 78 | + it('set # of bits in key', (done) => { |
| 79 | + var repo1 = createTestRepo() |
| 80 | + var repo2 = createTestRepo() |
| 81 | + const ipfsShort = new IPFS(repo1) |
| 82 | + const ipfsLong = new IPFS(repo2) |
| 83 | + ipfsShort.init({ bits: 128, emptyRepo: true }, (err) => { |
| 84 | + expect(err).to.not.exist |
| 85 | + |
| 86 | + ipfsLong.init({ bits: 256, emptyRepo: true }, (err) => { |
| 87 | + expect(err).to.not.exist |
| 88 | + |
| 89 | + repo1.config.get((err, config1) => { |
| 90 | + expect(err).to.not.exist |
| 91 | + |
| 92 | + repo2.config.get((err, config2) => { |
| 93 | + expect(err).to.not.exist |
| 94 | + expect(config1.Identity.PrivKey.length).is.below(config2.Identity.PrivKey.length) |
| 95 | + |
| 96 | + repo1.teardown(() => { |
| 97 | + repo2.teardown(done) |
| 98 | + }) |
| 99 | + }) |
| 100 | + }) |
| 101 | + }) |
| 102 | + }) |
| 103 | + }) |
| 104 | + |
| 105 | + it('force init (overwrite)', (done) => { |
| 106 | + var repo = createTestRepo() |
| 107 | + const ipfs1 = new IPFS(repo) |
| 108 | + const ipfs2 = new IPFS(repo) |
| 109 | + ipfs1.init({ bits: 128, emptyRepo: true }, (err) => { |
| 110 | + expect(err).to.not.exist |
| 111 | + |
| 112 | + ipfs2.init({ bits: 128, force: false }, (err) => { |
| 113 | + expect(err).to.exist |
| 114 | + |
| 115 | + ipfs2.init({ force: true }, (err) => { |
| 116 | + expect(err).to.not.exist |
| 117 | + |
| 118 | + repo.teardown(done) |
| 119 | + }) |
| 120 | + }) |
| 121 | + }) |
| 122 | + }) |
| 123 | +}) |
0 commit comments