-
Notifications
You must be signed in to change notification settings - Fork 1
/
pyobf_test.py
66 lines (55 loc) · 1.95 KB
/
pyobf_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import unittest
import pyobf
import sys
import re
import base64
from cStringIO import StringIO
class PyObfSpec(unittest.TestCase):
def runCode(self, code):
old = sys.stdout
redirected_output = sys.stdout = StringIO()
exec(code)
sys.stdout = old
return redirected_output.getvalue().strip()
def setUp(self):
pass
def test_assignment(self):
string = "print '123'\nprint '456'"
obf = pyobf.Obfuscator(string)
self.assertEqual(obf.simple(), "0 '2'\n0 '1'")
def test_indent(self):
string = "def main():\n\tprint 'hi'"
obf = pyobf.Obfuscator(string)
self.assertEqual(obf.simple(), "2 1():\n\t0 '3'")
def test_indent_space(self):
string = "def main():\n print 'hi'"
obf = pyobf.Obfuscator(string)
self.assertEqual(obf.simple(), "2 1():\n\t0 '3'")
def test_number_swap(self):
string = "for i in xrange(0, 10):\n print i"
obf = pyobf.Obfuscator(string)
self.assertEqual(obf.simple(), "2 4 3 6(0, 5):\n\t1 4")
def test_build_simple(self):
string = "print 'hello world'"
obf = pyobf.Obfuscator(string)
self.assertEqual(self.runCode(obf.build_simple()), "hello world")
def test_build_word(self):
string = 'print 1\nprint 2\nprint 3\nprint 2\nprint 1'
obf = pyobf.Obfuscator(string)
self.assertEqual(self.runCode(obf.build_simple()), "1\n2\n3\n2\n1")
def test_obf(self):
string = open("pyobf.py", "r").read()
obf = pyobf.Obfuscator(string)
obf_str = obf.build_simple()
self.runCode(obf.build_simple())
self.assertEqual(1, 1)
def tearDown(self):
pass
if __name__ == "__main__":
#unittest.main()
suite = unittest.TestLoader().loadTestsFromTestCase(PyObfSpec)
test_result = unittest.TextTestRunner(verbosity=2).run(suite)
if test_result.wasSuccessful():
sys.exit(0)
else:
sys.exit(1)