androguard/tests/test_misc.py

99 lines
3.1 KiB
Python
Raw Permalink Normal View History

2018-04-25 09:48:13 +00:00
# -*- coding: utf8- -*-
import os
import tempfile
import unittest
2018-04-25 09:48:13 +00:00
from androguard.misc import clean_file_name
2018-04-25 09:48:13 +00:00
class MiscTest(unittest.TestCase):
def testCleanPath(self):
self.assertEqual(
"foobarfoo_", clean_file_name("foobarfoo ", unique=False)
)
self.assertEqual(
"foobarsdf_", clean_file_name("foobarsdf.", unique=False)
)
2018-04-25 09:48:13 +00:00
self.assertEqual("_init_", clean_file_name("<init>", unique=False))
2018-04-25 11:32:59 +00:00
if os.name == "nt":
self.assertEqual(
"C:\\" + "a" * 230,
clean_file_name("C:\\" + "a" * 999, unique=False),
)
self.assertEqual(
"C:\\" + "a" * 226 + ".foo",
clean_file_name("C:\\" + "a" * 999 + ".foo", unique=False),
)
2018-04-25 11:32:59 +00:00
else:
self.assertEqual(
"/some/path/" + "a" * 230,
clean_file_name("/some/path/" + "a" * 999, unique=False),
)
self.assertEqual(
"/some/path/" + "a" * 226 + ".foo",
clean_file_name(
"/some/path/" + "a" * 999 + ".foo", unique=False
),
)
2018-04-25 09:48:13 +00:00
2018-04-25 12:01:27 +00:00
with tempfile.NamedTemporaryFile() as fp:
self.assertEqual(
fp.name + "_0", clean_file_name(fp.name, unique=True)
)
2018-04-25 09:48:13 +00:00
def testClassNameFormatting(self):
from androguard.core.bytecode import get_package_class_name
self.assertEqual(
get_package_class_name('Ljava/lang/Object;'),
('java.lang', 'Object'),
)
self.assertEqual(
get_package_class_name('[Ljava/lang/Object;'),
('java.lang', 'Object'),
)
self.assertEqual(
get_package_class_name('[[Ljava/lang/Object;'),
('java.lang', 'Object'),
)
self.assertEqual(
get_package_class_name(
'[[[[[[[[[[[[[[[[[[[[[[[Ljava/lang/Object;'
),
('java.lang', 'Object'),
)
self.assertEqual(
get_package_class_name('[[[[[[[[[[[[[[[[[[[[[[[LObject;'),
('', 'Object'),
)
self.assertEqual(get_package_class_name('LFoobar;'), ('', 'Foobar'))
self.assertEqual(
get_package_class_name(
'Lsdflkjdsklfjsdkjfklsdjfkljsdkflsd/shdfjksdhkjfhsdkjfsh;'
),
('sdflkjdsklfjsdkjfklsdjfkljsdkflsd', 'shdfjksdhkjfhsdkjfsh'),
)
self.assertEqual(get_package_class_name('L;'), ('', ''))
with self.assertRaises(ValueError):
get_package_class_name('Foobar')
with self.assertRaises(ValueError):
get_package_class_name('java.lang.Object')
with self.assertRaises(ValueError):
get_package_class_name('LOLjava.lang.Object')
with self.assertRaises(ValueError):
get_package_class_name('[[LOLjava.lang.Object')
with self.assertRaises(ValueError):
get_package_class_name('java.lang.Object;')
with self.assertRaises(ValueError):
get_package_class_name('[java.lang.Object;')
2018-04-25 09:48:13 +00:00
if __name__ == '__main__':
unittest.main()