Skip to content

Commit 4e032f2

Browse files
committed
test: add array test
1 parent c909ee2 commit 4e032f2

4 files changed

Lines changed: 134 additions & 0 deletions

File tree

test/binaries/array_test

16.3 KB
Binary file not shown.

test/run_suite.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@
88

99
from unittest import TestSuite, TextTestRunner, TestLoader
1010

11+
from scripts.array_test import ArrayTest
1112
from scripts.basic_test import BasicTest
1213
from scripts.basic_struct_test import BasicStructTest
1314
from scripts.string_test import StringTest
1415

1516
def test_suite():
1617
suite = TestSuite()
1718

19+
suite.addTest(TestLoader().loadTestsFromTestCase(ArrayTest))
1820
suite.addTest(TestLoader().loadTestsFromTestCase(BasicTest))
1921
suite.addTest(TestLoader().loadTestsFromTestCase(BasicStructTest))
2022
suite.addTest(TestLoader().loadTestsFromTestCase(StringTest))

test/scripts/array_test.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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 libdebug import debugger
10+
from libdestruct import array, array_of, inflater, c_int, c_long, struct
11+
12+
class ArrayTest(unittest.TestCase):
13+
def test_linear_arrays_1(self):
14+
d = debugger("binaries/array_test")
15+
16+
d.run()
17+
18+
bp = d.bp("do_nothing")
19+
20+
d.cont()
21+
22+
libdestruct = inflater(d.memory)
23+
24+
assert bp.hit_on(d)
25+
26+
test1 = libdestruct.inflate(array_of(c_int, 10), d.regs.rdi)
27+
28+
for i in range(10):
29+
assert test1[i].value == i ** 2
30+
31+
d.cont()
32+
33+
class test2_t(struct):
34+
a: c_int
35+
36+
test2 = libdestruct.inflate(array_of(test2_t, 10), d.regs.rdi)
37+
38+
for i in range(10):
39+
assert test2[i].a.value == i ** 3
40+
41+
d.cont()
42+
43+
class test3_t(struct):
44+
a: c_int
45+
b: c_long
46+
47+
test3 = libdestruct.inflate(array_of(test3_t, 10), d.regs.rdi)
48+
49+
for i in range(10):
50+
assert test3[i].a.value == i * 100
51+
assert test3[i].b.value == i * 1000
52+
53+
d.cont()
54+
55+
class test4_t(struct):
56+
a: c_int
57+
b: array = array_of(c_int, 10)
58+
59+
test4 = libdestruct.inflate(array_of(test4_t, 10), d.regs.rdi)
60+
61+
for i in range(10):
62+
assert test4[i].a.value == i ** 4
63+
for j in range(10):
64+
assert test4[i].b[j].value == (i + 1) * j
65+
66+
d.kill()
67+
d.terminate()

test/srcs/array_test.c

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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/array_test srcs/array_test.c
8+
9+
#include <stdlib.h>
10+
11+
#pragma pack(push, 1)
12+
int test1[10];
13+
14+
struct test2 {
15+
int a;
16+
};
17+
18+
struct test2 test2[10];
19+
20+
struct test3 {
21+
int a;
22+
long b;
23+
};
24+
25+
struct test3 test3[10];
26+
27+
struct test4 {
28+
int a;
29+
int b[10];
30+
};
31+
32+
struct test4 test4[10];
33+
#pragma pack(pop)
34+
35+
void do_nothing(void* addr)
36+
{
37+
38+
}
39+
40+
int main()
41+
{
42+
for (int i = 0; i < 10; i++)
43+
test1[i] = i * i;
44+
45+
for (int i = 0; i < 10; i++)
46+
test2[i].a = i * i * i;
47+
48+
for (int i = 0; i < 10; i++) {
49+
test3[i].a = 100 * i;
50+
test3[i].b = 1000 * i;
51+
}
52+
53+
for (int i = 0; i < 10; i++) {
54+
test4[i].a = i * i * i * i;
55+
for (int j = 0; j < 10; j++)
56+
test4[i].b[j] = (i + 1) * j;
57+
}
58+
59+
do_nothing(test1);
60+
do_nothing(test2);
61+
do_nothing(test3);
62+
do_nothing(test4);
63+
64+
return 0;
65+
}

0 commit comments

Comments
 (0)