def test_detect(self): """Test detect() function.""" positive = lambda source: self.assertTrue(detect(source)) negative = lambda source: self.assertFalse(detect(source)) negative('') negative('var a = b') positive('eval(function(p,a,c,k,e,r') positive('eval ( function(p, a, c, k, e, r')
def is_artefact_packed(filename, byte_content): """ Returns a tuple (is_packed, unpacked_byte_content) if the artefact is packed using an algorithm similar to Dave Edward's p.a.c.k.e.r algorithm. This appears to generate syntax errors compounding feature analysis, but we try anyway... """ # dont load the file again if we already have the byte-content... if byte_content is None: with open(filename, 'rb') as fp: byte_content = fp.read() try: source = fast_decode(byte_content) is_packed = packer.detect(source) if is_packed: ret = packer.unpack(source) # TODO FIXME... yuk this is so silly! But we have to return bytes since extract-features.jar program expects that... # fortunately most artefacts arent packed so this is not as expensive as it could be... result = decode(encode(ret, 'utf-8', 'backslashreplace'), 'unicode-escape').encode() assert isinstance(result, bytes) assert len(result) > 0 return (True, result) # else return (False, byte_content) except Exception as e: # we fail an artefact that is purportedly packed, but which cannot be unpacked... raise e
def negative(source): return self.assertFalse(detect(source))
def positive(source): return self.assertTrue(detect(source))
def check(inp, out): return detect(inp) and self.assertEqual(unpack(inp), out)
def negative(source): return self.assertFalse(detect(source)) negative('')
def positive(source): return self.assertTrue(detect(source)) def negative(source): return self.assertFalse(detect(source))