about summary refs log tree commit diff stats
path: root/test
diff options
context:
space:
mode:
Diffstat (limited to 'test')
-rwxr-xr-xtest/os_dep/common.py35
-rwxr-xr-xtest/os_dep/linux/stdlib.py43
-rwxr-xr-xtest/os_dep/win_api_x86_32.py85
-rwxr-xr-xtest/test_all.py4
4 files changed, 166 insertions, 1 deletions
diff --git a/test/os_dep/common.py b/test/os_dep/common.py
new file mode 100755
index 00000000..5d525e32
--- /dev/null
+++ b/test/os_dep/common.py
@@ -0,0 +1,35 @@
+#! /usr/bin/env python2
+#-*- coding:utf-8 -*-
+
+import unittest
+import logging
+from miasm2.analysis.machine import Machine
+import miasm2.os_dep.common as commonapi
+from miasm2.core.utils import pck32
+from miasm2.jitter.csts import PAGE_READ, PAGE_WRITE
+
+machine = Machine("x86_32")
+
+jit = machine.jitter()
+jit.init_stack()
+
+class TestCommonAPI(unittest.TestCase):
+
+    def test_get_size(self):
+        heap = commonapi.heap()
+        with self.assertRaises(AssertionError):
+            heap.get_size(jit.vm, 0)
+        heap.alloc(jit, 20)
+        heap.alloc(jit, 40)
+        heap.alloc(jit, 50)
+        heap.alloc(jit, 60)
+        ptr = heap.alloc(jit, 10)
+        heap.alloc(jit, 80)
+        for i in xrange(10):
+            self.assertEqual(heap.get_size(jit.vm, ptr+i), 10)
+
+if __name__ == '__main__':
+    testsuite = unittest.TestLoader().loadTestsFromTestCase(TestCommonAPI)
+    report = unittest.TextTestRunner(verbosity=2).run(testsuite)
+    exit(len(report.errors + report.failures))
+
diff --git a/test/os_dep/linux/stdlib.py b/test/os_dep/linux/stdlib.py
new file mode 100755
index 00000000..ab39a487
--- /dev/null
+++ b/test/os_dep/linux/stdlib.py
@@ -0,0 +1,43 @@
+#! /usr/bin/env python2
+#-*- coding:utf-8 -*-
+
+import unittest
+import logging
+from miasm2.analysis.machine import Machine
+import miasm2.os_dep.linux_stdlib as stdlib
+from miasm2.core.utils import pck32
+from miasm2.jitter.csts import PAGE_READ, PAGE_WRITE
+
+machine = Machine("x86_32")
+
+jit = machine.jitter()
+jit.init_stack()
+
+heap = stdlib.linobjs.heap
+
+class TestLinuxStdlib(unittest.TestCase):
+
+    def test_xxx_sprintf(self):
+        def alloc_str(s):
+            s += "\x00"
+            ptr = heap.alloc(jit, len(s))
+            jit.vm.set_mem(ptr, s)
+            return ptr
+        fmt  = alloc_str("'%s' %d")
+        str_ = alloc_str("coucou")
+        buf = heap.alloc(jit,1024)
+
+        jit.push_uint32_t(1111)
+        jit.push_uint32_t(str_)
+        jit.push_uint32_t(fmt)
+        jit.push_uint32_t(buf)
+        jit.push_uint32_t(0) # ret_ad
+        stdlib.xxx_sprintf(jit)
+        ret = jit.get_str_ansi(buf)
+        self.assertEqual(ret, "'coucou' 1111")
+
+
+if __name__ == '__main__':
+    testsuite = unittest.TestLoader().loadTestsFromTestCase(TestLinuxStdlib)
+    report = unittest.TextTestRunner(verbosity=2).run(testsuite)
+    exit(len(report.errors + report.failures))
diff --git a/test/os_dep/win_api_x86_32.py b/test/os_dep/win_api_x86_32.py
index 2e22ccea..f080ba89 100755
--- a/test/os_dep/win_api_x86_32.py
+++ b/test/os_dep/win_api_x86_32.py
@@ -6,12 +6,14 @@ import logging
 from miasm2.analysis.machine import Machine
 import miasm2.os_dep.win_api_x86_32 as winapi
 from miasm2.core.utils import pck32
+from miasm2.jitter.csts import PAGE_READ, PAGE_WRITE
 
 machine = Machine("x86_32")
 
 jit = machine.jitter()
 jit.init_stack()
 
+heap = winapi.winobjs.heap
 
 class TestWinAPI(unittest.TestCase):
 
@@ -23,6 +25,89 @@ class TestWinAPI(unittest.TestCase):
         vBool = jit.cpu.EAX
         self.assertFalse(vBool)
 
+    def test_msvcrt_sprintf(self):
+        def alloc_str(s):
+            s += "\x00"
+            ptr = heap.alloc(jit, len(s))
+            jit.vm.set_mem(ptr, s)
+            return ptr
+        fmt  = alloc_str("'%s' %d")
+        str_ = alloc_str("coucou")
+        buf = heap.alloc(jit,1024)
+
+        jit.push_uint32_t(1111)
+        jit.push_uint32_t(str_)
+        jit.push_uint32_t(fmt)
+        jit.push_uint32_t(buf)
+        jit.push_uint32_t(0) # ret_ad
+        winapi.msvcrt_sprintf(jit)
+        ret = jit.get_str_ansi(buf)
+        self.assertEqual(ret, "'coucou' 1111")
+
+
+    def test_msvcrt_swprintf(self):
+        def alloc_str(s):
+            s = s.encode("utf-16le")
+            s += "\x00\x00"
+            ptr = heap.alloc(jit, len(s))
+            jit.vm.set_mem(ptr, s)
+            return ptr
+        fmt  = alloc_str("'%s' %d")
+        str_ = alloc_str("coucou")
+        buf = heap.alloc(jit,1024)
+
+        jit.push_uint32_t(1111)
+        jit.push_uint32_t(str_)
+        jit.push_uint32_t(fmt)
+        jit.push_uint32_t(buf)
+        jit.push_uint32_t(0) # ret_ad
+        winapi.msvcrt_swprintf(jit)
+        ret = jit.get_str_unic(buf)
+        self.assertEqual(ret, "'coucou' 1111")
+
+
+    def test_msvcrt_realloc(self):
+        jit.push_uint32_t(10)
+        jit.push_uint32_t(0) # ret_ad
+        winapi.msvcrt_malloc(jit)
+        ptr = jit.cpu.EAX
+
+        jit.push_uint32_t(20)
+        jit.push_uint32_t(ptr)
+        jit.push_uint32_t(0) # ret_ad
+        winapi.msvcrt_realloc(jit)
+        ptr2 = jit.cpu.EAX
+
+        self.assertNotEqual(ptr, ptr2)
+        self.assertEqual(heap.get_size(jit.vm,ptr2), 20)
+
+    def test_GetCurrentDirectory(self):
+
+        # DWORD WINAPI GetCurrentDirectory(size, buf)
+
+        # Test with a buffer long enough
+        addr = 0x80000
+        size = len(winapi.winobjs.cur_dir)+1
+        jit.vm.add_memory_page(addr, PAGE_READ | PAGE_WRITE, "\x00" * (size), "")
+        jit.push_uint32_t(addr)   # buf
+        jit.push_uint32_t(size)   # size
+        jit.push_uint32_t(0)      # @return
+        winapi.kernel32_GetCurrentDirectoryA(jit)
+        dir_ = jit.get_str_ansi(addr)
+        size_ret = jit.cpu.EAX
+        self.assertEqual(len(dir_), size_ret)
+
+        # Test with a buffer too small
+        jit.vm.set_mem(addr, "\xFF"*size)
+        jit.push_uint32_t(addr)   # buf
+        jit.push_uint32_t(5)      # size
+        jit.push_uint32_t(0)      # @return
+        winapi.kernel32_GetCurrentDirectoryA(jit)
+        size_ret = jit.cpu.EAX
+        self.assertEqual(len(dir_)+1, size_ret)
+        dir_short = jit.get_str_ansi(addr)
+        self.assertEqual(dir_short, dir_[:4])
+
     def test_MemoryManagementFunctions(self):
 
         # HGLOBAL WINAPI GlobalAlloc(_In_ UINT uFlags, _In_ SIZE_T dwBytes);
diff --git a/test/test_all.py b/test/test_all.py
index d2ae4fce..23937366 100755
--- a/test/test_all.py
+++ b/test/test_all.py
@@ -265,7 +265,9 @@ testset += RegressionTest(["z3_ir.py"], base_dir="ir/translators",
 testset += RegressionTest(["smt2.py"], base_dir="ir/translators",
                           tags=[TAGS["z3"]])
 ## OS_DEP
-for script in ["win_api_x86_32.py",
+for script in ["common.py",
+               "win_api_x86_32.py",
+               os.path.join("linux", "stdlib.py"),
                ]:
     testset += RegressionTest([script], base_dir="os_dep", tags=[TAGS['gcc']])