androguard/tests/test_misc.py

42 lines
1.9 KiB
Python
Raw Normal View History

2018-04-25 09:48:13 +00:00
# -*- coding: utf8- -*-
import unittest
2018-04-25 11:32:59 +00:00
import os
2018-04-25 09:48:13 +00:00
from androguard.misc import clean_file_name
import tempfile
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))
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, force_nt=True))
self.assertEqual("C:\\" + "a" * 226 + ".foo", clean_file_name("C:\\" + "a" * 999 + ".foo", unique=False, force_nt=True))
else:
self.assertEqual("/some/path/" + "a" * 230, clean_file_name("/some/path/" + "a" * 999, unique=False, force_nt=True))
self.assertEqual("/some/path/" + "a" * 226 + ".foo", clean_file_name("/some/path/" + "a" * 999 + ".foo", unique=False, force_nt=True))
2018-04-25 09:48:13 +00:00
2018-04-25 12:01:27 +00:00
with tempfile.NamedTemporaryFile() as fp:
2018-04-25 09:48:13 +00:00
self.assertEqual(fp.name + "_0", clean_file_name(fp.name, unique=True))
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('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')
2018-04-25 09:48:13 +00:00
if __name__ == '__main__':
unittest.main()