Esempio n. 1
0
	def test_bad_hex(self):
		# Send bad hex to function.
		try:
			searchbin.hex_to_pattern("AAGG")
			self.assertFalse(True) # This statement should never be reached.
		except:
			pass
		
		try:
			s1 = str('{:x}'.format(random.randint(0,1000000))).zfill(6)
			s2 = str('{:x}'.format(random.randint(0,1000000))).zfill(6)
			# It attempts to send a hex string with "0x" in the middle.
			searchbin.hex_to_pattern(s1+"0x"+s2)
			self.assertFalse(True) # This statement should never be reached.
		except(TypeError, ValueError):
			pass
Esempio n. 2
0
	def test_hex(self):
		"""Test the hex_to_pattern() function"""
		
		for i in range(1000):
			hex_str1 = get_rand_hex_str()
			
			# Second hex starting with "0x". It should pass same tests.
			hex_str2 = "0x" + hex_str1
			
			# Make sure hex string encodes to pattern and decodes to same hex string.
			try:               # Python 3.
				self.assertEqual(searchbin.hex_to_pattern(hex_str1)[0], bytes.fromhex(hex_str1))
				self.assertEqual(searchbin.hex_to_pattern(hex_str2)[0], bytes.fromhex(hex_str1))
			except AttributeError: # Python 2.
				self.assertEqual(searchbin.hex_to_pattern(hex_str1)[0], hex_str1.decode("hex"))
				self.assertEqual(searchbin.hex_to_pattern(hex_str2)[0], hex_str1.decode("hex"))
			
			# Insert a couple "??" at random places in the hex string.
			for i in range(2):
				r = random.randint(0, 10) * 2
				if len(hex_str1) > r:
					hex_str1 = hex_str1[:r] + "??" + hex_str1[r:]
			
			# Make sure the pattern can match the random number.
			self.assertTrue(searchbin.hex_to_pattern(hex_str1))
			
			try:
				# Make sure this fails. It attempts to send a hex string with "0x" in the middle.
				try:               # Python 3.
					searchbin.hex_to_pattern(str(random.randint(0,1000000)).decode("hex")+hex_str2)
					self.assertFalse(True) # This statement should never be reached.
				except ImportError: # Python 2.
					searchbin.hex_to_pattern(str(random.randint(0,1000000)).decode("hex")+hex_str2)
					self.assertFalse(True) # This statement should never be reached.
			except:
				pass