|  | 
|  | 1 | +/* eslint-env mocha */ | 
|  | 2 | +'use strict' | 
|  | 3 | + | 
|  | 4 | +const chai = require('chai') | 
|  | 5 | +chai.use(require('dirty-chai')) | 
|  | 6 | +const chaiAsPromised = require('chai-as-promised') | 
|  | 7 | +chai.use(chaiAsPromised) | 
|  | 8 | +const expect = chai.expect | 
|  | 9 | + | 
|  | 10 | +const path = require('path') | 
|  | 11 | +const keysMigration = require('../../migrations/migration-8/keys-encoding') | 
|  | 12 | +const blocksMigration = require('../../migrations/migration-8/blocks-to-multihash') | 
|  | 13 | +const Key = require('interface-datastore').Key | 
|  | 14 | +const Datastore = require('datastore-fs') | 
|  | 15 | +const core = require('datastore-core') | 
|  | 16 | +const ShardingStore = core.ShardingDatastore | 
|  | 17 | + | 
|  | 18 | +const keysFixtures = [ | 
|  | 19 | +  ['aAa', 'key_mfawc'], | 
|  | 20 | +  ['bbb', 'key_mjrge'], | 
|  | 21 | +  ['self', 'key_onswyzq'] | 
|  | 22 | +] | 
|  | 23 | + | 
|  | 24 | +const blocksFixtures = [ | 
|  | 25 | +  ['AFKREIBFG77IKIKDMBDUFDCSPK7H5TE5LNPMCSXYLPML27WSTT5YA5IUNU', 'CIQCKN76QUQUGYCHIKGFE6V6P3GJ2W26YFFPQW6YXV7NFHH3QB2RI3I'] | 
|  | 26 | +] | 
|  | 27 | + | 
|  | 28 | +async function bootstrapKeys (dir, encoded) { | 
|  | 29 | +  const store = new Datastore(path.join(dir, 'keys'), { extension: '.data', createIfMissing: true }) | 
|  | 30 | +  await store.open() | 
|  | 31 | + | 
|  | 32 | +  let name | 
|  | 33 | +  for (const keyNames of keysFixtures) { | 
|  | 34 | +    name = encoded ? keyNames[1] : keyNames[0] | 
|  | 35 | +    await store.put(new Key(`/pkcs8/${name}`), '') | 
|  | 36 | +    await store.put(new Key(`/info/${name}`), '') | 
|  | 37 | +  } | 
|  | 38 | + | 
|  | 39 | +  await store.close() | 
|  | 40 | +} | 
|  | 41 | + | 
|  | 42 | +async function validateKeys (dir, shouldBeEncoded) { | 
|  | 43 | +  const store = new Datastore(path.join(dir, 'keys'), { extension: '.data', createIfMissing: false }) | 
|  | 44 | +  await store.open() | 
|  | 45 | + | 
|  | 46 | +  let name | 
|  | 47 | +  for (const keyNames of keysFixtures) { | 
|  | 48 | +    name = shouldBeEncoded ? keyNames[1] : keyNames[0] | 
|  | 49 | +    expect(await store.has(new Key(`/pkcs8/${name}`))).to.be.true(name) | 
|  | 50 | +    expect(await store.has(new Key(`/info/${name}`))).to.be.true(name) | 
|  | 51 | +  } | 
|  | 52 | + | 
|  | 53 | +  await store.close() | 
|  | 54 | +} | 
|  | 55 | + | 
|  | 56 | +async function bootstrapBlocks (dir, encoded) { | 
|  | 57 | +  const baseStore = new Datastore(path.join(dir, 'blocks'), { extension: '.data', createIfMissing: true }) | 
|  | 58 | +  const shard = new core.shard.NextToLast(2) | 
|  | 59 | +  const store = await ShardingStore.createOrOpen(baseStore, shard) | 
|  | 60 | + | 
|  | 61 | +  let name | 
|  | 62 | +  for (const blocksNames of blocksFixtures) { | 
|  | 63 | +    name = encoded ? blocksNames[1] : blocksNames[0] | 
|  | 64 | +    await store.put(new Key(name), '') | 
|  | 65 | +  } | 
|  | 66 | + | 
|  | 67 | +  await store.close() | 
|  | 68 | +} | 
|  | 69 | + | 
|  | 70 | +async function validateBlocks (dir, shouldBeEncoded) { | 
|  | 71 | +  const baseStore = new Datastore(path.join(dir, 'blocks'), { extension: '.data', createIfMissing: false }) | 
|  | 72 | +  const shard = new core.shard.NextToLast(2) | 
|  | 73 | +  const store = await ShardingStore.createOrOpen(baseStore, shard) | 
|  | 74 | + | 
|  | 75 | +  let newName, oldName | 
|  | 76 | +  for (const blockNames of blocksFixtures) { | 
|  | 77 | +    newName = shouldBeEncoded ? blockNames[1] : blockNames[0] | 
|  | 78 | +    oldName = shouldBeEncoded ? blockNames[0] : blockNames[1] | 
|  | 79 | +    expect(await store.has(new Key(oldName))).to.be.false(oldName) | 
|  | 80 | +    expect(await store.has(new Key(newName))).to.be.true(newName) | 
|  | 81 | +  } | 
|  | 82 | + | 
|  | 83 | +  await store.close() | 
|  | 84 | +} | 
|  | 85 | + | 
|  | 86 | +module.exports = (setup, cleanup) => { | 
|  | 87 | +  describe('migration 8', () => { | 
|  | 88 | +    let dir | 
|  | 89 | + | 
|  | 90 | +    beforeEach(async () => { | 
|  | 91 | +      dir = await setup() | 
|  | 92 | +    }) | 
|  | 93 | +    afterEach(() => cleanup(dir)) | 
|  | 94 | + | 
|  | 95 | +    it('should migrate keys forward', async () => { | 
|  | 96 | +      await bootstrapKeys(dir, false) | 
|  | 97 | +      await keysMigration.migrate(dir) | 
|  | 98 | +      await validateKeys(dir, true) | 
|  | 99 | +    }) | 
|  | 100 | + | 
|  | 101 | +    it('should migrate keys backward', async () => { | 
|  | 102 | +      await bootstrapKeys(dir, true) | 
|  | 103 | +      await keysMigration.revert(dir) | 
|  | 104 | +      await validateKeys(dir, false) | 
|  | 105 | +    }) | 
|  | 106 | + | 
|  | 107 | +    it('should fail to migrate keys backward with invalid key name', async () => { | 
|  | 108 | +      const store = new Datastore(path.join(dir, 'keys'), { extension: '.data', createIfMissing: true }) | 
|  | 109 | +      await store.open() | 
|  | 110 | + | 
|  | 111 | +      await store.put(new Key('/pkcs8/mfawc'), '') | 
|  | 112 | +      await store.put(new Key('/info/mfawc'), '') | 
|  | 113 | + | 
|  | 114 | +      await store.close() | 
|  | 115 | + | 
|  | 116 | +      expect(keysMigration.revert(dir)).to.eventually.rejectedWith('Unknown format of key\'s name!') | 
|  | 117 | +    }) | 
|  | 118 | + | 
|  | 119 | +    it('should migrate blocks forward', async () => { | 
|  | 120 | +      await bootstrapBlocks(dir, false) | 
|  | 121 | +      await blocksMigration.migrate(dir) | 
|  | 122 | +      await validateBlocks(dir, true) | 
|  | 123 | +    }) | 
|  | 124 | +    // | 
|  | 125 | +    // it('should migrate blocks backward', async () => { | 
|  | 126 | +    //   await bootstrapKeys(dir, true) | 
|  | 127 | +    //   await blocksMigration.revert(dir) | 
|  | 128 | +    //   await validateKeys(dir, false) | 
|  | 129 | +    // }) | 
|  | 130 | +  }) | 
|  | 131 | +} | 
0 commit comments