Debian Codegraph @

jsbeautifier/unpackers/tests/testjavascriptobfuscator.py

raw
1 #
2 # written by Stefano Sanfilippo <[email protected]>
3 #
4
5 """Tests for JavaScriptObfuscator unpacker."""
6
7 import unittest
8 from jsbeautifier.unpackers.javascriptobfuscator import unpack, detect, smartsplit
9
10 # pylint: disable=R0904
11
12
13 class TestJavascriptObfuscator(unittest.TestCase):
14 """JavascriptObfuscator.com test case."""
15
16 def test_smartsplit(self):
17 """Test smartsplit() function."""
18 split = smartsplit
19
20 def equals(data, result):
21 return self.assertEqual(split(data), result)
22
23 equals("", [])
24 equals('"a", "b"', ['"a"', '"b"'])
25 equals('"aaa","bbbb"', ['"aaa"', '"bbbb"'])
26 equals('"a", "b\\""', ['"a"', '"b\\""'])
27
28 def test_detect(self):
29 """Test detect() function."""
30
31 def positive(source):
32 return self.assertTrue(detect(source))
33
34 def negative(source):
35 return self.assertFalse(detect(source))
36
37 negative("")
38 negative("abcd")
39 negative("var _0xaaaa")
40 positive('var _0xaaaa = ["a", "b"]')
41 positive('var _0xaaaa=["a", "b"]')
42 positive('var _0x1234=["a","b"]')
43
44 def test_unpack(self):
45 """Test unpack() function."""
46
47 def decodeto(ob, original):
48 return self.assertEqual(unpack(ob), original)
49
50 decodeto("var _0x8df3=[];var a=10;", "var a=10;")
51 decodeto(
52 'var _0xb2a7=["\x74\x27\x65\x73\x74"];var i;for(i=0;i<10;++i)'
53 "{alert(_0xb2a7[0]);} ;",
54 "var i;for(i=0;i<10;++i){alert" '("t\'est");} ;',
55 )
56
57
58 if __name__ == "__main__":
59 unittest.main()