SuccessfulState implements DoSState { @Override public void update( final IntelligentDoSLibraryImpl STATE_CONTEXT, final AttackModel attackModel ) { SuccessDecider successDecider = STATE_CONTEXT.getSuccessDecider(); if ( untampered == null ) { untampered = attackModel; double ratio = successDecider.calculateRatio( STATE_CONTEXT.getCurrentUntampered(), attackModel.getDurationArray() ); if ( SuspiciousState.isSuspicious( ratio ) ) { STATE_CONTEXT.setCurrentAttack( STATE_CONTEXT.createNewUntampered( false ) ); STATE_CONTEXT.setHasFurtherAttack( true ); STATE_CONTEXT.setDoSState( new SuspiciousState() ); } else { STATE_CONTEXT.setCurrentAttack( STATE_CONTEXT.createNewTampered( false ) ); STATE_CONTEXT.setHasFurtherAttack( true ); } } else { Long[] currentUntampered = untampered.getDurationArray(); Long[] currentTampered = attackModel.getDurationArray(); boolean successful = successDecider.wasSuccessful( currentUntampered, currentTampered ); Efficiency efficency = successDecider.getEfficency( currentUntampered, currentTampered ); double ratio = successDecider.calculateRatio( currentUntampered, currentTampered ); double ratioFormated = Math.round( ratio * 100.0 ) / 100.0; if ( successful ) { String dName = attackModel.getDoSAttack() != null ? attackModel.getDoSAttack().getName() : "???"; String msg = String.format( "%s was successful (ratio: %s)", dName, ratioFormated ); logger.info( msg ); SuccessfulAttack successfulAttack = createSuccessfulAttack( attackModel ); successfulAttack.setEfficiency( efficency ); successfulAttack.setRatio( ratio ); STATE_CONTEXT.addSuccessful( ( successfulAttack ) ); AttackModel createNextAttack = STATE_CONTEXT.createNextAttack( true ); STATE_CONTEXT.setCurrentAttack( createNextAttack ); STATE_CONTEXT.setHasFurtherAttack( createNextAttack != null ); if ( createNextAttack != null ) { if ( createNextAttack.getRequestType() == UNTAMPERED ) { STATE_CONTEXT.setDoSState( new UntamperedState() ); } else { STATE_CONTEXT.setDoSState( new TamperedState() ); } } else { STATE_CONTEXT.setDoSState( new FinishState() ); } } else { String dName = attackModel.getDoSAttack() != null ? attackModel.getDoSAttack().getName() : "???"; String msg = String.format( "%s was not successful (ratio: %s).", dName, ratioFormated ); logger.info( msg ); STATE_CONTEXT.setCurrentAttack( STATE_CONTEXT.createNewUntampered( true ) ); STATE_CONTEXT.setHasFurtherAttack( true ); STATE_CONTEXT.setDoSState( new UntamperedState() ); } } }  @Override String getName(); @Override void update( final IntelligentDoSLibraryImpl STATE_CONTEXT, final AttackModel attackModel ); @Override void updateTestProbes( Metric metric );  }
@Test public void testFirstResponse() { IntelligentDoSLibraryImpl context = Mockito.mock( IntelligentDoSLibraryImpl.class ); SuccessDecider sd = mock( SuccessDecider.class ); when( context.getSuccessDecider() ).thenReturn( sd ); when( sd.calculateRatio( null, new Long[0] ) ).thenReturn( 1d ); AttackModel model = new AttackModel(); successfulState.update( context, model ); verify( context, times( 1 ) ).createNewTampered( false ); verify( context, times( 1 ) ).setHasFurtherAttack( true ); }
@Test public void testSecondResponse() { IntelligentDoSLibraryImpl context = mock( IntelligentDoSLibraryImpl.class ); SuccessDecider sd = mock( SuccessDecider.class ); when( context.getSuccessDecider() ).thenReturn( sd ); when( sd.calculateRatio( null, new Long[0] ) ).thenReturn( 1d ); AttackModel model = new AttackModel(); successfulState.update( context, model ); successfulState.update( context, model ); verify( context, times( 2 ) ).getSuccessDecider(); }
@Test public void testSuccessfulWithNoFurther() { IntelligentDoSLibraryImpl context = mock( IntelligentDoSLibraryImpl.class ); SuccessDecider sd = mock( SuccessDecider.class ); when( sd.wasSuccessful( new Long[0], new Long[0] ) ).thenReturn( true ); when( context.getSuccessDecider() ).thenReturn( sd ); AttackModel model = new AttackModel(); model.setPosition( new AnyElementPosition( null, null ) ); successfulState.update( context, model ); successfulState.update( context, model ); verify( context, times( 2 ) ).getSuccessDecider(); when( context.addSuccessful( Mockito.isA( SuccessfulAttack.class ) ) ).thenReturn( true ); verify( context ).addSuccessful( Mockito.isA( SuccessfulAttack.class ) ); assertThat( context.hasFurtherAttack(), is( false ) ); }
@Test public void testSuccessfulWithFurther() { IntelligentDoSLibraryImpl context = mock( IntelligentDoSLibraryImpl.class ); SuccessDecider sd = mock( SuccessDecider.class ); when( sd.wasSuccessful( new Long[0], new Long[0] ) ).thenReturn( true ); when( context.getSuccessDecider() ).thenReturn( sd ); when( context.addSuccessful( Mockito.isA( SuccessfulAttack.class ) ) ).thenReturn( true ); AttackModel attack = mock( AttackModel.class ); DoSAttack doSAttack = mock( DoSAttack.class ); when( attack.getDoSAttack() ).thenReturn( doSAttack ); stub( context.createNextAttack( true ) ).toReturn( attack ); AttackModel model = new AttackModel(); model.setPosition( new AnyElementPosition( null, null ) ); successfulState.update( context, model ); successfulState.update( context, model ); verify( context, times( 2 ) ).getSuccessDecider(); verify( context ).addSuccessful( Mockito.isA( SuccessfulAttack.class ) ); verify( context, times( 2 ) ).setHasFurtherAttack( true ); }
@Test public void testSuccessfulWithFurtherUntampered() { IntelligentDoSLibraryImpl context = mock( IntelligentDoSLibraryImpl.class ); SuccessDecider sd = mock( SuccessDecider.class ); when( sd.wasSuccessful( new Long[0], new Long[0] ) ).thenReturn( true ); when( context.getSuccessDecider() ).thenReturn( sd ); when( context.addSuccessful( Mockito.isA( SuccessfulAttack.class ) ) ).thenReturn( true ); AttackModel attack = mock( AttackModel.class ); when( attack.getRequestType() ).thenReturn( RequestType.UNTAMPERED ); when( context.createNextAttack( true ) ).thenReturn( attack ); AttackModel model = new AttackModel(); model.setPosition( new AnyElementPosition( null, null ) ); successfulState.update( context, model ); successfulState.update( context, model ); verify( context, times( 2 ) ).getSuccessDecider(); verify( context ).addSuccessful( Mockito.isA( SuccessfulAttack.class ) ); verify( context, times( 2 ) ).setHasFurtherAttack( true ); }