Ejemplo n.º 1
0
    def onDataModelGetValue(self, action, dataModel):
        """
        Called before getting a value from a data model

        @type	action: Action
        @param	action: Action we are starting
        @type	dataModel: Template
        @param	dataModel: Data model we are using
        """
        if action.data is not None and action.data.multipleFiles:
            self.action = action
            self.context = action.getRoot()
            # If a file fails to parse, don't exit the run, instead re-crack
            # until we find a working file.
            while True:
                if self.firstFile:
                    action.data.gotoFirstFile()
                    self.firstFile = False
                # Locate fresh copy of template with no data
                obj = self.GetRef(action.template.ref)
                cracker = DataCracker(obj.getRoot())
                cracker.optmizeModelForCracking(obj)
                template = obj.copy(action)
                template.ref = action.template.ref
                template.parent = action
                template.name = action.template.name
                # Switch any references to old name
                oldName = template.ref
                for relation in template._genRelationsInDataModelFromHere():
                    if relation.of == oldName:
                        relation.of = template.name
                    elif relation.From == oldName:
                        relation.From = template.name
                # Crack file
                try:
                    template.setDefaults(action.data, False, True)
                    print("")
                    break
                except:
                    pass
            # Cache default values
            action.template = template
            template.getValue()
            # Re-create state engine copy.  We do this to avoid have
            # optmizeModelForCracking called over and over.
            if hasattr(action, "origionalTemplate"):
                #delattr(action, "origionalTemplate")
                action.origionalTemplate = action.template
                action.origionalTemplate.BuildRelationCache()
                action.origionalTemplate.resetDataModel()
                action.origionalTemplate.getValue()
                action.template = action.template.copy(action)
            # Regenerate mutator state
            self._isFirstTestCase = True
            self._dataModels = {}
            self._fieldMutators = {}
        if self._isFirstTestCase:
            fullName = dataModel.getFullname()
            if fullName not in self._dataModels:
                self._dataModels[fullName] = self._getNodeCount(dataModel)
                nodes = dataModel.getAllChildDataElements()
                nodes.append(dataModel)
                for node in nodes:
                    mutators = []
                    self._fieldMutators[node.getFullname()] = mutators
                    for m in Engine.context.mutators:
                        if m.supportedDataElement(node):
                            # Need to create new instance from class
                            for i in range(m.weight**4):
                                mutators.append(m(Engine.context, node))
            return
Ejemplo n.º 2
0
	def onDataModelGetValue(self, action, dataModel):
		'''
		Called before getting a value from a data model
		
		@type	action: Action
		@param	action: Action we are starting
		@type	dataModel: Template
		@param	dataModel: Data model we are using
		'''
		
		if action.data != None and action.data.multipleFiles:
			self.action = action
			self.context = action.getRoot()
			
			# If a file fails to parse, don't exit the
			# run, instead re-crack until we find a working
			# file.
			while True:
				# Time to switch to another file!
				if self.firstFile:
					action.data.gotoFirstFile()
					self.firstFile = False
				
				# Locate fresh copy of template with no data
				obj = self.GetRef(action.template.ref)
				cracker = DataCracker(obj.getRoot())
				cracker.optmizeModelForCracking(obj)
				
				template = obj.copy(action)
				template.ref = action.template.ref
				template.parent = action
				template.name = action.template.name
				
				# Switch any references to old name
				oldName = template.ref
				for relation in template._genRelationsInDataModelFromHere():
					if relation.of == oldName:
						relation.of = template.name
					
					elif relation.From == oldName:
						relation.From = template.name
				
				# Crack file
				try:
					template.setDefaults(action.data, False, True)
					print ""
					break
				
				except:
					pass
			
			# Cache default values
			action.template = template
			template.getValue()
			
			# Re-create state engine copy.  We do this to
			# avoid have optmizeModelForCracking called over
			# and over...
			if hasattr(action, "origionalTemplate"):
				#delattr(action, "origionalTemplate")
				action.origionalTemplate = action.template
				action.origionalTemplate.BuildRelationCache()
				action.origionalTemplate.resetDataModel()
				action.origionalTemplate.getValue()
				action.template = action.template.copy(action)
			
			# Regenerate mutator state
			self._isFirstTestCase = True
			self._dataModels = {}
			self._fieldMutators = {}
		
		
		if self._isFirstTestCase:
			
			fullName = dataModel.getFullname()
			
			if fullName not in self._dataModels:
				self._dataModels[fullName] = self._getNodeCount(dataModel)
				
				nodes = dataModel.getAllChildDataElements()
				nodes.append(dataModel)
				
				for node in nodes:
					mutators = []
					self._fieldMutators[node.getFullname()] = mutators
					
					for m in Engine.context.mutators:
						if m.supportedDataElement(node):
							# Need to create new instance from class
							for i in range(m.weight**4):
								mutators.append( m(Engine.context,node) )
			
			return
Ejemplo n.º 3
0
    def onDataModelGetValue(self, action, dataModel):
        '''
		Called before getting a value from a data model
		
		@type	action: Action
		@param	action: Action we are starting
		@type	dataModel: Template
		@param	dataModel: Data model we are using
		'''

        if action.data != None and action.data.multipleFiles and action.data.switchCount != None:
            self.switchCount = action.data.switchCount

        if action.data != None and action.data.multipleFiles and \
           self.iterationCount % self.switchCount == 0:

            self.context = action.getRoot()

            # If a file fails to parse, don't exit the
            # run, instead re-crack until we find a working
            # file.
            while True:
                # Time to switch to another file!
                action.data.gotoRandomFile()

                # Locate fresh copy of template with no data
                obj = self.GetRef(action.template.ref)
                cracker = DataCracker(obj.getRoot())
                cracker.optmizeModelForCracking(obj)

                template = obj.copy(action)
                template.ref = action.template.ref
                template.parent = action
                template.name = action.template.name

                # Switch any references to old name
                oldName = template.ref
                for relation in template._genRelationsInDataModelFromHere():
                    if relation.of == oldName:
                        relation.of = template.name

                    elif relation.From == oldName:
                        relation.From = template.name

                # Crack file
                try:
                    template.setDefaults(action.data, False, True)
                    print ""
                    break

                except:
                    pass

            # Cache default values
            action.template = template
            template.getValue()

            # Re-create state engine copy.  We do this to
            # avoid have optmizeModelForCracking called over
            # and over...
            if hasattr(action, "origionalTemplate"):
                #delattr(action, "origionalTemplate")
                action.origionalTemplate = action.template
                action.origionalTemplate.BuildRelationCache()
                action.origionalTemplate.resetDataModel()
                action.origionalTemplate.getValue()
                action.template = action.template.copy(action)

            # Regenerate mutator state
            self._isFirstTestCase = True
            self._dataModels = {}
            self._fieldMutators = {}

        if self._isFirstTestCase:

            fullName = dataModel.getFullname()

            if fullName not in self._dataModels:
                self._dataModels[fullName] = self._getNodeCount(dataModel)

                nodes = dataModel.getAllChildDataElements()
                nodes.append(dataModel)

                nonMutableNodes = []

                for node in nodes:
                    if not node.isMutable:
                        nonMutableNodes.append(node)
                    mutators = []
                    self._fieldMutators[node.getFullname()] = mutators

                    for m in Engine.context.mutators:
                        if m.supportedDataElement(node):
                            # Need to create new instance from class
                            for i in range(m.weight**4):
                                mutators.append(m(Engine.context, node))

                for node in nonMutableNodes:
                    nodes.remove(node)

                nonMutableNodes = None

            return

        else:
            ## Is this data model we are changing?
            if dataModel.getFullname() != self._dataModelToChange:
                return

            ## Select fields to modify
            nodes = dataModel.getAllChildDataElements()
            nodes.append(dataModel)

            nodesToRemove = []
            # Remove non-mutable fields
            for node in nodes:
                if not node.isMutable:
                    nodesToRemove.append(node)

            for node in nodesToRemove:
                nodes.remove(node)

            ## Quick to check to see if we failed.
            #for node in nodes:
            #	if not self._fieldMutators.has_key(node.getFullname()) or len(self._fieldMutators[node.getFullname()]) == 0:
            #		raise Exception("Found element with no mutations!")

            print "The following elements are mutating: "
            # Select nodes we will modify
            if len(nodes) <= self._n:

                fields = nodes
                maxN = self._n - len(fields)
                if maxN <= 0:
                    maxN = self._n / 2

                for i in range(self._random.randint(1, maxN)):
                    # Now perform mutations on fields
                    if len(fields) < 3:
                        sampleset = fields
                    else:
                        sampleset = self._random.sample(
                            fields, self._random.randint(1, len(fields)))

                    for node in sampleset:
                        try:
                            mutator = self._random.choice(
                                self._fieldMutators[node.getFullname()])

                            fullName = node.getFullnameInDataModel(
                            )[len(dataModel.name) + 1:]
                            print " %s" % (fullName)
                            print "   %s" % (mutator.name)

                            # Note: Since we are applying multiple mutations
                            #       sometimes a mutation will fail.  We should
                            #       ignore those failures.
                            try:
                                mutator.randomMutation(node, self._random)
                            except:
                                pass

                        except:
                            pass
            else:

                fields = self._random.sample(nodes,
                                             self._random.randint(1, self._n))

                # Now perform mutations on fields
                for node in fields:
                    try:
                        mutator = self._random.choice(
                            self._fieldMutators[node.getFullname()])

                        #print "   > %s: %s" % (node.getFullnameInDataModel(), mutator.name)
                        fullName = node.getFullnameInDataModel(
                        )[len(dataModel.name) + 1:]
                        print " %s" % (fullName)
                        print "   %s" % (mutator.name)

                        # Note: Since we are applying multiple mutations
                        #       sometimes a mutation will fail.  We should
                        #       ignore those failures.
                        try:
                            mutator.randomMutation(node, self._random)
                        except:
                            pass

                    except:
                        pass

            print ""
Ejemplo n.º 4
0
	def onDataModelGetValue(self, action, dataModel):
		'''
		Called before getting a value from a data model
		
		@type	action: Action
		@param	action: Action we are starting
		@type	dataModel: Template
		@param	dataModel: Data model we are using
		'''
		
		if action.data != None and action.data.multipleFiles and action.data.switchCount != None:
			self.switchCount = action.data.switchCount
		
		if action.data != None and action.data.multipleFiles and \
					self.iterationCount % self.switchCount == 0:
			
			self.context = action.getRoot()
			
			# If a file fails to parse, don't exit the
			# run, instead re-crack until we find a working
			# file.
			while True:
				# Time to switch to another file!
				action.data.gotoRandomFile()
				
				# Locate fresh copy of template with no data
				obj = self.GetRef(action.template.ref)
				cracker = DataCracker(obj.getRoot())
				cracker.optmizeModelForCracking(obj)
				
				template = obj.copy(action)
				template.ref = action.template.ref
				template.parent = action
				template.name = action.template.name
				
				# Switch any references to old name
				oldName = template.ref
				for relation in template._genRelationsInDataModelFromHere():
					if relation.of == oldName:
						relation.of = template.name
					
					elif relation.From == oldName:
						relation.From = template.name
				
				# Crack file
				try:
					template.setDefaults(action.data, False, True)
					print ""
					break
				
				except:
					pass
			
			# Cache default values
			action.template = template
			template.getValue()
			
			# Re-create state engine copy.  We do this to
			# avoid have optmizeModelForCracking called over
			# and over...
			if hasattr(action, "origionalTemplate"):
				#delattr(action, "origionalTemplate")
				action.origionalTemplate = action.template
				action.origionalTemplate.BuildRelationCache()
				action.origionalTemplate.resetDataModel()
				action.origionalTemplate.getValue()
				action.template = action.template.copy(action)
			
			# Regenerate mutator state
			self._isFirstTestCase = True
			self._dataModels = {}
			self._fieldMutators = {}
		
		
		if self._isFirstTestCase:
			
			fullName = dataModel.getFullname()
			
			if fullName not in self._dataModels:
				self._dataModels[fullName] = self._getNodeCount(dataModel)
				
				nodes = dataModel.getAllChildDataElements()
				nodes.append(dataModel)
				
				nonMutableNodes = []
				
				for node in nodes:
					if not node.isMutable:
						nonMutableNodes.append(node)
					mutators = []
					self._fieldMutators[node.getFullname()] = mutators
					
					for m in Engine.context.mutators:
						if m.supportedDataElement(node):
							# Need to create new instance from class
							for i in range(m.weight**4):
								mutators.append( m(Engine.context,node) )
				
				for node in nonMutableNodes:
					nodes.remove(node)
				
				nonMutableNodes = None
				
			return
		
		else:
			## Is this data model we are changing?
			if dataModel.getFullname() != self._dataModelToChange:
				return
			
			## Select fields to modify
			nodes = dataModel.getAllChildDataElements()
			nodes.append(dataModel)
			
			nodesToRemove = []
			# Remove non-mutable fields
			for node in nodes:
				if not node.isMutable:
					nodesToRemove.append(node)
			
			for node in nodesToRemove:
				nodes.remove(node)
					
			## Quick to check to see if we failed.
			#for node in nodes:
			#	if not self._fieldMutators.has_key(node.getFullname()) or len(self._fieldMutators[node.getFullname()]) == 0:
			#		raise Exception("Found element with no mutations!")
			
			print "The following elements are mutating: "
			# Select nodes we will modify
			if len(nodes) <= self._n:
				
				fields = nodes
				maxN = self._n - len(fields)
				if maxN <= 0:
					maxN = self._n/2
				
				for i in range(self._random.randint(1, maxN)):
					# Now perform mutations on fields
					if len(fields) < 3:
						sampleset = fields
					else:
						sampleset = self._random.sample(fields, self._random.randint(1, len(fields)))
					
					for node in sampleset:
						try:
							mutator = self._random.choice(self._fieldMutators[node.getFullname()])
							
							fullName = node.getFullnameInDataModel()[len(dataModel.name)+1:]
							print " %s" % (fullName)
							print "   %s" % (mutator.name)
							
							# Note: Since we are applying multiple mutations
							#       sometimes a mutation will fail.  We should
							#       ignore those failures.
							try:
								mutator.randomMutation(node, self._random)
							except:
								pass
						
						except:
							pass
			else:
				
				fields = self._random.sample(nodes, self._random.randint(1, self._n))
				
				# Now perform mutations on fields
				for node in fields:
					try:
						mutator = self._random.choice(self._fieldMutators[node.getFullname()])
						
						#print "   > %s: %s" % (node.getFullnameInDataModel(), mutator.name)
						fullName = node.getFullnameInDataModel()[len(dataModel.name)+1:]
						print " %s" % (fullName)
						print "   %s" % (mutator.name)
						
						# Note: Since we are applying multiple mutations
						#       sometimes a mutation will fail.  We should
						#       ignore those failures.
						try:
							mutator.randomMutation(node, self._random)
						except:
							pass
					
					except:
						pass
			
			print ""
Ejemplo n.º 5
0
        dataModelName = args.check[0]
        samplesPath = args.check[1]
        samples = []
        if os.path.isdir(samplesPath):
            for fp in os.listdir(samplesPath):
                samples.append(os.path.join(samplesPath, fp))
        else:
            samples = glob.glob(samplesPath)
        peach = args.parser.asParser(args.pit)
        dataModel = peach.templates[dataModelName]
        for sample in samples:
            dataModel = peach.templates[dataModelName].copy(peach)
            with open(sample, "rb") as fd:
                data = fd.read()
            buff = PublisherBuffer(None, data, True)
            cracker = DataCracker(peach)
            cracker.optmizeModelForCracking(dataModel, True)
            cracker.crackData(dataModel, buff)
            if dataModel.getValue() == data:
                result = highlight.ok("passed")
            else:
                result = highlight.error("failed")
            logging.info("[%s] cracking: '%s'" % (result, sample))
        logging.info("Done.")
        sys.exit(0)

    if args.single:
        logging.info("Performing a single iteration.")
        Engine.justOne = True

    if args.range: