"""Subclass of Dialog_AskStartFuzzing, which is generated by wxFormBuilder."""

import wx
import FuzzingTool
from FuzzingTool_Dialog_AskStartFuzzing import FuzzingTool_Dialog_AskStartFuzzing
import subprocess

# Implementing Dialog_AskStartFuzzing
class FuzzingTool_Dialog_AskStartFuzzing_Child( FuzzingTool.Dialog_AskStartFuzzing ):
	def __init__( self, parent, program="Nothing", input_path="Nothing"):
		FuzzingTool.Dialog_AskStartFuzzing.__init__( self, parent )
		self.Text_FuzzingProgram.SetLabel("Program you selected: "+program)
		self.Text_FuzzingInput.SetLabel("Seed input: "+input_path)
		self.DoLayoutAdaptation()
		self.errorcode=""
		self.program=program
		self.input_path=input_path
		
	# Handlers for Dialog_AskStartFuzzing events.
	def bSizer_OKorCancelOnCancelButtonClick( self, event ):
		# TODO: Implement bSizer_OKorCancelOnCancelButtonClick
		self.EndModal(False)

	def bSizer_OKorCancelOnOKButtonClick( self, event ):
		# TODO: Implement bSizer_OKorCancelOnOKButtonClick
		self.bSizer_OKorCancelCancel.Disable()
		self.bSizer_OKorCancelOK.Disable()
		fuzzer=subprocess.Popen(["afl-fuzz","-i",self.input_path,"-o","./out",self.program],stdout=subprocess.PIPE,stderr=subprocess.PIPE)
		for line in fuzzer.stdout:
			line=line.decode('utf-8')
			if line.find('PROGRAM ABORT')!=-1:
				self.errorcode=line
				self.EndModal(False)
			elif line.find('All set and ready to roll')!=-1:
				fuzzer.terminate()
				self.EndModal(True)


