/** * Once loaded, an agent will always read the byte[] * of the loaded classes. Here we tell it if those byte[] * should be instrumented */ public static void activate(){ checkTransformerState(); MockFramework.enable(); transformer.activate(); LoopCounter.getInstance().setActive(true); }
@Test(timeout = 10000) public void testInfiniteLoop() { Assert.assertTrue(RuntimeSettings.maxNumberOfIterationsPerLoop > 0);//should be on by default int first = LoopCounter.getInstance().getNewIndex(); int second = LoopCounter.getInstance().getNewIndex(); while (true) { LoopCounter.getInstance().checkLoop(first); for (int i = 0; i < 100; i++) { try { LoopCounter.getInstance().checkLoop(second); //this should fail first } catch (TooManyResourcesException e) { //expected return; } } } }
@Before public void init() { LoopCounter.getInstance().reset(); }
private void addInstrumentation(){ int index = LoopCounter.getInstance().getNewIndex(); mv.visitMethodInsn(Opcodes.INVOKESTATIC,LOOP_COUNTER, "getInstance", "()L"+LOOP_COUNTER+";" , false); mv.visitLdcInsn(index); mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, LOOP_COUNTER, "checkLoop", "(I)V", false); } }
@Test public void testNoNewIndex(){ LoopCounter.getInstance().checkLoop(0); LoopCounter.getInstance().checkLoop(1); LoopCounter.getInstance().checkLoop(2); LoopCounter.getInstance().checkLoop(5); LoopCounter.getInstance().checkLoop(6); } }
boolean wasLoopCheckOn = LoopCounter.getInstance().isActivated(); LoopCounter.getInstance().setActive(false); TARGET_CLASS_INSTANCE = Class.forName(TARGET_CLASS, initialise, TestGenerationContext.getInstance().getClassLoaderForSUT()); "* Could not find class under test " + Properties.TARGET_CLASS + ": " + e); } finally { LoopCounter.getInstance().setActive(wasLoopCheckOn);
boolean wasLoopCheckOn = LoopCounter.getInstance().isActivated(); LoopCounter.getInstance().setActive(false); DBManager.getInstance().initDB(); LoopCounter.getInstance().setActive(wasLoopCheckOn); LoopCounter.getInstance().reset();
counters.set(index, value); if(value >= RuntimeSettings.maxNumberOfIterationsPerLoop && !isInStaticInit()) { this.reset(); throw new TooManyResourcesException("Loop has been executed more times than the allowed " + RuntimeSettings.maxNumberOfIterationsPerLoop);
private static List<Class<?>> loadClasses(ClassLoader classLoader, String... classNames) { List<Class<?>> classes = new ArrayList<>(); InstrumentingAgent.activate(); boolean safe = Sandbox.isSafeToExecuteSUTCode(); //assert !Sandbox.isSecurityManagerInitialized() || Sandbox.isOnAndExecutingSUTCode(); for (int i=0; i< classNames.length;i++) { org.evosuite.runtime.Runtime.getInstance().resetRuntime(); String classNameToLoad = classNames[i]; Sandbox.goingToExecuteSUTCode(); boolean wasLoopCheckOn = LoopCounter.getInstance().isActivated(); try { if(!safe){ Sandbox.goingToExecuteUnsafeCodeOnSameThread(); } LoopCounter.getInstance().setActive(false); Class<?> aClass = Class.forName(classNameToLoad, true, classLoader); classes.add(aClass); } catch (Exception | Error ex) { AtMostOnceLogger.error(logger,"Could not initialize " + classNameToLoad + ": " + ex.getMessage()); } finally { if(!safe){ Sandbox.doneWithExecutingUnsafeCodeOnSameThread(); } Sandbox.doneWithExecutingSUTCode(); LoopCounter.getInstance().setActive(wasLoopCheckOn); } } InstrumentingAgent.deactivate(); return classes; }
boolean wasLoopCheckOn = LoopCounter.getInstance().isActivated(); LoopCounter.getInstance().setActive(false); resetMethod.invoke(null, (Object[]) null); TestGenerationContext.getInstance().doneWithExecutingSUTCode(); MutationObserver.activateMutation(mutationActive); LoopCounter.getInstance().setActive(wasLoopCheckOn);
/** * Stop instrumenting classes */ public static void deactivate(){ checkTransformerState(); MockFramework.disable(); transformer.deactivate(); LoopCounter.getInstance().setActive(false); }
@After public void tearDown() { LoopCounter.getInstance().reset(); }
boolean wasLoopCheckOn = LoopCounter.getInstance().isActivated(); Sandbox.goingToExecuteUnsafeCodeOnSameThread(); LoopCounter.getInstance().setActive(false); m.invoke(null, (Object[]) null); } catch (IllegalAccessException | IllegalArgumentException e) { Sandbox.doneWithExecutingUnsafeCodeOnSameThread(); LoopCounter.getInstance().setActive(wasLoopCheckOn);
LoopCounter.getInstance().setActive(true); //be sure it is active here, as JUnit checks might have left it to false
boolean loopCounter = LoopCounter.getInstance().isActivated(); while (isInStaticInit()) { LoopCounter.getInstance().setActive(false); ExecutionTracer.setKillSwitch(false); logger.info("Run still not finished, but awaiting for static initializer to finish."); LoopCounter.getInstance().setActive(loopCounter); ExecutionTracer.setKillSwitch(true);
LoopCounter.getInstance().setActive(false); ExceptionMapGenerator.initializeExceptionMap(Properties.TARGET_CLASS); LoopCounter.getInstance().setActive(true);