import { P2P , events } from '../../src/index' ; import { createNetwork , destroyNetwork , SEED_PEER_IP } from '../utils/network_setup' ; import { wait } from '../utils/helpers' ; import { constructPeerId } from '../../src/utils' ; const { EVENT_BAN_PEER } = events ; describe ( 'penalty sending malformed Peer List' , ( ) => { describe ( 'When Peer List is too long' , ( ) => { let p2pNodeList = [ ] ; const collectedEvents = new Map ( ) ; beforeEach ( async ( ) => { const customConfig = ( index ) => ( { maxPeerDiscoveryResponseLength : index === 1 ? 10 : 100 , } ) ; p2pNodeList = await createNetwork ( { networkSize : 2 , networkDiscoveryWaitTime : 1 , customConfig , } ) ; for ( let i = 0 ; i < 1000 ; i += 1 ) { const generatedIP = `${ Math . floor ( Math . random ( ) * 254 ) + 1 }.${ Math . floor ( Math . random ( ) * 254 ) + 1 }.${ Math . floor ( Math . random ( ) * 254 ) + 1 }.${ Math . floor ( Math . random ( ) * 254 ) + 1 }` ; p2pNodeList [ 0 ] [ '_peerBook' ] . addPeer ( { peerId : `${ generatedIP }:5000` , ipAddress : generatedIP , port : 1000 , sharedState : { networkVersion : '1.1' , networkIdentifier : '' , nonce : '' , options : { } , } , } ) ; } p2pNodeList [ 1 ] . on ( EVENT_BAN_PEER , peerId => { collectedEvents . set ( EVENT_BAN_PEER , peerId ) ; } ) ; await wait ( 1000 ) ; } ) ; afterEach ( async ( ) => { await destroyNetwork ( p2pNodeList ) ; } ) ; it ( 'should ban the emitter' , ( ) => { expect ( collectedEvents . get ( EVENT_BAN_PEER ) ) . toEqual ( constructPeerId ( SEED_PEER_IP , p2pNodeList [ 0 ] . config . port ) ) ; } ) ; } ) ; describe . skip ( 'When PeerBook contain malformed peerInfo' , ( ) => { let p2pNodeList = [ ] ; const collectedEvents = new Map ( ) ; beforeEach ( async ( ) => { p2pNodeList = await createNetwork ( { networkSize : 2 , networkDiscoveryWaitTime : 1 , customConfig : ( ) => ( { } ) , } ) ; p2pNodeList [ 0 ] [ '_peerBook' ] . addPeer ( { peerId : "'1.1.1.1:1000" , ipAddress : '1.1.1.1' , port : 1000 , sharedState : { networkVersion : '1.' . repeat ( 13000 ) , networkIdentifier : '' , nonce : '' , options : { networkVersion : '1.' . repeat ( 13000 ) } , } , } ) ; p2pNodeList [ 1 ] . on ( EVENT_BAN_PEER , peerId => { collectedEvents . set ( EVENT_BAN_PEER , peerId ) ; } ) ; await wait ( 1000 ) ; } ) ; afterEach ( async ( ) => { await destroyNetwork ( p2pNodeList ) ; } ) ; it ( 'should ban the emitter' , ( ) => { expect ( collectedEvents . get ( EVENT_BAN_PEER ) ) . toEqual ( constructPeerId ( SEED_PEER_IP , p2pNodeList [ 0 ] . config . port ) ) ; } ) ; } ) ; } ) ;