Skip to content

Commit 77629f3

Browse files
committed
test: add basic tests for ctypes' types
1 parent 0b92cf6 commit 77629f3

4 files changed

Lines changed: 167 additions & 0 deletions

File tree

test/binaries/ctypes_test

16.2 KB
Binary file not shown.

test/run_suite.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from scripts.array_test import ArrayTest
1212
from scripts.basic_test import BasicTest
1313
from scripts.basic_struct_test import BasicStructTest
14+
from scripts.ctypes_test import CtypesTest
1415
from scripts.string_test import StringTest
1516

1617
def test_suite():
@@ -19,6 +20,7 @@ def test_suite():
1920
suite.addTest(TestLoader().loadTestsFromTestCase(ArrayTest))
2021
suite.addTest(TestLoader().loadTestsFromTestCase(BasicTest))
2122
suite.addTest(TestLoader().loadTestsFromTestCase(BasicStructTest))
23+
suite.addTest(TestLoader().loadTestsFromTestCase(CtypesTest))
2224
suite.addTest(TestLoader().loadTestsFromTestCase(StringTest))
2325

2426
return suite

test/scripts/ctypes_test.py

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
#
2+
# This file is part of libdestruct (https://github.com/mrindeciso/libdestruct).
3+
# Copyright (c) 2024 Roberto Alessandro Bertolini. All rights reserved.
4+
# Licensed under the MIT license. See LICENSE file in the project root for details.
5+
#
6+
7+
import unittest
8+
9+
from ctypes import *
10+
from libdebug import debugger
11+
from libdestruct import inflater, struct
12+
13+
class CtypesTest(unittest.TestCase):
14+
def test_all_types(self):
15+
d = debugger("binaries/ctypes_test")
16+
17+
d.run()
18+
19+
bp = d.breakpoint("leak")
20+
21+
class provola(struct):
22+
a: c_bool
23+
b: c_ushort
24+
c: c_char
25+
d: c_char_p
26+
e: c_double
27+
f: c_float
28+
g: c_int
29+
h: c_int16
30+
i: c_int32
31+
j: c_int64
32+
k: c_int8
33+
l: c_long
34+
m: c_longdouble
35+
n: c_longlong
36+
o: c_short
37+
p: c_size_t
38+
r: c_time_t
39+
s: c_void_p
40+
t: c_uint
41+
u: c_uint16
42+
v: c_uint32
43+
w: c_uint64
44+
x: c_uint8
45+
y: c_ulong
46+
z: c_ulonglong
47+
48+
libdestruct = inflater(d.memory)
49+
50+
d.cont()
51+
52+
self.assertTrue(bp.hit_on(d))
53+
54+
obj = libdestruct.inflate(provola, d.regs.rdi)
55+
56+
self.assertEqual(obj.a.value, True)
57+
self.assertEqual(obj.b.value, 1337)
58+
self.assertEqual(obj.c.value, b'\x17')
59+
self.assertAlmostEqual(obj.e.value, 1337.1337)
60+
self.assertAlmostEqual(obj.f.value, 123.456, places=5)
61+
self.assertEqual(obj.g.value, 12345)
62+
self.assertEqual(obj.h.value, 23456)
63+
self.assertEqual(obj.i.value, 0x0eadbeef)
64+
self.assertEqual(obj.j.value, 0x0eadbeefdeadbeef)
65+
self.assertEqual(obj.k.value, 123)
66+
self.assertEqual(obj.l.value, 0xeefd00dbeefd00d)
67+
self.assertAlmostEqual(obj.m.value, 123456.789)
68+
self.assertEqual(obj.n.value, 0xeefdeadbeefdead)
69+
self.assertEqual(obj.o.value, 9876)
70+
self.assertEqual(obj.p.value, 0xdeadbeefdeadbeef)
71+
self.assertEqual(obj.r.value, 0x123456)
72+
self.assertEqual(obj.s.value, 0x1234)
73+
self.assertEqual(obj.t.value, 0xdeadbeef)
74+
self.assertEqual(obj.u.value, 0x1234)
75+
self.assertEqual(obj.v.value, 0xd00dbeef)
76+
self.assertEqual(obj.w.value, 0xd00ddeadbeefbeef)
77+
self.assertEqual(obj.x.value, 0x46)
78+
self.assertEqual(obj.y.value, 0xbeefbeefbeef)
79+
self.assertEqual(obj.z.value, 0xd00dd00dd00dd00d)
80+
81+
d.kill()
82+
d.terminate()

test/srcs/ctypes_test.c

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
//
2+
// This file is part of libdestruct (https://github.com/mrindeciso/libdestruct).
3+
// Copyright (c) 2024 Roberto Alessandro Bertolini. All rights reserved.
4+
// Licensed under the MIT license. See LICENSE file in the project root for details.
5+
//
6+
7+
// gcc -o binaries/ctypes_test srcs/ctypes_test.c
8+
9+
#include <stdlib.h>
10+
#include <stdint.h>
11+
#include <string.h>
12+
#include <time.h>
13+
14+
#pragma pack(push, 1)
15+
struct provola {
16+
_Bool a;
17+
unsigned short b;
18+
char c;
19+
char *d;
20+
double e;
21+
float f;
22+
int g;
23+
int16_t h;
24+
int32_t i;
25+
int64_t j;
26+
int8_t k;
27+
long l;
28+
long double m;
29+
long long n;
30+
short o;
31+
size_t p;
32+
time_t r;
33+
void *s;
34+
unsigned int t;
35+
uint16_t u;
36+
uint32_t v;
37+
uint64_t w;
38+
uint8_t x;
39+
unsigned long y;
40+
unsigned long long z;
41+
};
42+
#pragma pack(pop)
43+
44+
char NAME[] = "LIBDESTRUCT\0";
45+
46+
void leak(struct provola *ptr)
47+
{
48+
49+
}
50+
51+
int main()
52+
{
53+
struct provola object;
54+
object.a = 1;
55+
object.b = 1337;
56+
object.c = 23;
57+
object.d = NAME;
58+
object.e = 1337.1337;
59+
object.f = 123.456;
60+
object.g = 12345;
61+
object.h = 23456;
62+
object.i = 0x0eadbeef;
63+
object.j = 0x0eadbeefdeadbeef;
64+
object.k = 123;
65+
object.l = 0xeefd00dbeefd00d;
66+
object.m = 123456.789;
67+
object.n = 0xeefdeadbeefdead;
68+
object.o = 9876;
69+
object.p = 0xdeadbeefdeadbeef;
70+
object.r = 0x123456;
71+
object.s = (void *) 0x1234;
72+
object.t = 0xdeadbeef;
73+
object.u = 0x1234;
74+
object.v = 0xd00dbeef;
75+
object.w = 0xd00ddeadbeefbeef;
76+
object.x = 0x46;
77+
object.y = 0xbeefbeefbeef;
78+
object.z = 0xd00dd00dd00dd00d;
79+
80+
leak(&object);
81+
82+
return 0;
83+
}

0 commit comments

Comments
 (0)