Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
tape('[Libp2pPeer]', t => {
const PeerInfo = td.replace('peer-info')
const PeerId = td.replace('peer-id')
const Libp2pNode = td.replace('../../../lib/net/peer/libp2pnode')
const Libp2pSender = td.replace('../../../lib/net/protocol/libp2psender')
const Libp2pPeer = require('../../../lib/net/peer/libp2ppeer')
const peerInfo = { multiaddrs: { add: td.func() }, id: { toB58String: td.func() } }
const peerInfo0 = { multiaddrs: { add: td.func() } }
td.when(PeerId.createFromB58String('id0')).thenReturn('peerId0')
td.when(PeerId.createFromB58String('id1')).thenReturn('peerId1')
td.when(PeerInfo.create('peerId0')).thenCallback(null, peerInfo0)
td.when(PeerInfo.create('peerId1')).thenCallback('error0')
td.when(PeerInfo.create()).thenCallback(null, peerInfo)
td.when(peerInfo.id.toB58String()).thenReturn('id')
t.test('should initialize correctly', async (t) => {
const peer = new Libp2pPeer({ multiaddrs: 'ma0,ma1' })
t.deepEquals(peer.multiaddrs, [ 'ma0', 'ma1' ], 'multiaddrs split')
t.equals(peer.address, 'ma0,ma1', 'address correct')
t.end()
})
t.test('should create PeerInfo', async (t) => {
t.test('should sync', async (t) => {
t.plan(3)
const sync = new LightSynchronizer({ interval: 1, pool: new PeerPool() })
sync.best = td.func()
sync.latest = td.func()
td.when(sync.best()).thenReturn({ les: { status: { headNum: new BN(2) } } })
td.when(HeaderFetcher.prototype.fetch(), { delay: 20 }).thenResolve()
sync.chain = { headers: { height: new BN(3) } }
t.notOk(await sync.sync(), 'local height > remote height')
sync.chain = { headers: { height: new BN(0) } }
t.ok(await sync.sync(), 'local height < remote height')
td.when(HeaderFetcher.prototype.fetch()).thenReject('err0')
try {
await sync.sync()
} catch (err) {
t.equals(err, 'err0', 'got error')
}
})
t.test('should connect to peer', async (t) => {
const peer = new Libp2pPeer()
peer.bindProtocols = td.func()
Libp2pNode.prototype.asyncStart = td.func()
Libp2pNode.prototype.asyncDial = td.func()
td.when(Libp2pNode.prototype.asyncStart()).thenResolve()
td.when(Libp2pNode.prototype.asyncDial(peerInfo)).thenResolve()
td.when(peer.bindProtocols(td.matchers.anything(), peerInfo)).thenResolve()
peer.connect()
peer.on('connected', () => {
t.pass('connected')
t.end()
})
})
tape('[BlockFetcher]', t => {
class PeerPool extends EventEmitter {}
PeerPool.prototype.idle = td.func()
PeerPool.prototype.ban = td.func()
td.replace('../../../lib/net/peerpool', PeerPool)
const BlockFetcher = require('../../../lib/sync/fetcher/blockfetcher')
t.test('should start/stop', async (t) => {
const fetcher = new BlockFetcher({
pool: new PeerPool(),
first: new BN(1),
count: 10,
maxPerRequest: 5,
timeout: 5
})
fetcher.next = () => false
t.notOk(fetcher.running, 'not started')
fetcher.fetch()
t.equals(fetcher.in.size(), 2, 'added 2 tasks')
it('can apply tagged rules for creating a machine', async () => {
const fetch = TestDouble.func();
const rule = Object.assign({}, firewallRule);
rule.rule = 'FROM tag bacon TO tag flavor=smokey ALLOW udp port 8675';
TestDouble.when(fetch('/fwrules')).thenResolve([rule]);
const tags = [{ name: 'bacon' }, { name: 'flavor', value: 'smokey' }];
const res = await FirewallRules.firewall_rules_create_machine(fetch, { tags });
expect(res).to.exist();
expect(res.length).to.equal(1);
expect(res[0].rule_obj.action).to.equal('allow');
const tag = res[0].rule_obj.from[0][1];
expect(tag).to.equal('bacon');
});
tape('[Node]', t => {
class EthereumService extends EventEmitter {}
EthereumService.prototype.open = td.func()
EthereumService.prototype.start = td.func()
EthereumService.prototype.stop = td.func()
td.when(EthereumService.prototype.open()).thenResolve()
td.when(EthereumService.prototype.start()).thenResolve()
td.when(EthereumService.prototype.stop()).thenResolve()
td.replace('../lib/service', { EthereumService })
class Server extends EventEmitter {}
Server.prototype.open = td.func()
Server.prototype.start = td.func()
Server.prototype.stop = td.func()
td.when(Server.prototype.start()).thenResolve()
td.when(Server.prototype.stop()).thenResolve()
td.replace('../lib/net/server/server', Server)
const Node = require('../lib/node')
t.test('should initialize correctly', t => {
const node = new Node()
t.ok(node.services[0] instanceof EthereumService, 'added service')
t.end()
})
t.test('should open', async (t) => {
t.plan(6)
const servers = [ new Server() ]
t.test('should sync', async (t) => {
t.plan(3)
const sync = new FastSynchronizer({ interval: 1, pool: new PeerPool() })
sync.best = td.func()
sync.latest = td.func()
td.when(sync.best()).thenReturn('peer')
td.when(sync.latest('peer')).thenResolve({ number: 2 })
td.when(BlockFetcher.prototype.fetch(), { delay: 20 }).thenResolve()
sync.chain = { blocks: { height: new BN(3) } }
t.notOk(await sync.sync(), 'local height > remote height')
sync.chain = { blocks: { height: new BN(0) } }
t.ok(await sync.sync(), 'local height < remote height')
td.when(BlockFetcher.prototype.fetch()).thenReject('err0')
try {
await sync.sync()
} catch (err) {
t.equals(err, 'err0', 'got error')
}
})
beforeEach(() => {
constantsStub = td.replace('../../../src/error/constants.json')
handlerStub = td.replace('../../../src/error/handler', td.func())
})
beforeEach(() => {
getMessageStub = td.func()
td.replace('../../../src/util/browser', () => ({
i18n: {
getMessage: getMessageStub
}
}))
td.replace('../../../src/error/constants.json')
handler = require('../../../src/error/handler')
})
protos.forEach(p => {
p.open = td.func()
td.when(p.open()).thenResolve()
})
server.getPeerInfo = td.func()