2018-04-25 09:48:13 +00:00
|
|
|
# -*- coding: utf8- -*-
|
2018-04-25 11:32:59 +00:00
|
|
|
import os
|
2024-10-31 14:50:52 +00:00
|
|
|
import tempfile
|
|
|
|
import unittest
|
2018-04-25 09:48:13 +00:00
|
|
|
|
|
|
|
from androguard.misc import clean_file_name
|
2024-10-31 14:50:52 +00:00
|
|
|
|
2018-04-25 09:48:13 +00:00
|
|
|
|
|
|
|
class MiscTest(unittest.TestCase):
|
|
|
|
def testCleanPath(self):
|
2024-10-31 14:50:52 +00:00
|
|
|
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":
|
2024-10-31 14:50:52 +00:00
|
|
|
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:
|
2024-10-31 14:50:52 +00:00
|
|
|
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:
|
2024-10-31 14:50:52 +00:00
|
|
|
self.assertEqual(
|
|
|
|
fp.name + "_0", clean_file_name(fp.name, unique=True)
|
|
|
|
)
|
2018-04-25 09:48:13 +00:00
|
|
|
|
2019-02-12 14:34:04 +00:00
|
|
|
def testClassNameFormatting(self):
|
|
|
|
from androguard.core.bytecode import get_package_class_name
|
|
|
|
|
2024-10-31 14:50:52 +00:00
|
|
|
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'),
|
|
|
|
)
|
2019-02-12 14:34:04 +00:00
|
|
|
self.assertEqual(get_package_class_name('LFoobar;'), ('', 'Foobar'))
|
2024-10-31 14:50:52 +00:00
|
|
|
self.assertEqual(
|
|
|
|
get_package_class_name(
|
|
|
|
'Lsdflkjdsklfjsdkjfklsdjfkljsdkflsd/shdfjksdhkjfhsdkjfsh;'
|
|
|
|
),
|
|
|
|
('sdflkjdsklfjsdkjfklsdjfkljsdkflsd', 'shdfjksdhkjfhsdkjfsh'),
|
|
|
|
)
|
2019-02-12 14:34:04 +00:00
|
|
|
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')
|
|
|
|
|
2019-05-09 09:56:06 +00:00
|
|
|
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;')
|
2019-02-12 14:34:04 +00:00
|
|
|
|
2018-04-25 09:48:13 +00:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|