Commit | Line | Data |
---|---|---|
f7add556 JO |
1 | #include "util.h" |
2 | ||
3 | #include <stdlib.h> | |
4 | #include <sys/types.h> | |
5 | #include <sys/stat.h> | |
6 | #include <fcntl.h> | |
7 | #include <string.h> | |
8 | ||
9 | #include "symbol.h" | |
10 | ||
11 | #define TEST_ASSERT_VAL(text, cond) \ | |
12 | do { \ | |
13 | if (!(cond)) { \ | |
14 | pr_debug("FAILED %s:%d %s\n", __FILE__, __LINE__, text); \ | |
15 | return -1; \ | |
16 | } \ | |
17 | } while (0) | |
18 | ||
19 | static char *test_file(int size) | |
20 | { | |
21 | static char buf_templ[] = "/tmp/test-XXXXXX"; | |
22 | char *templ = buf_templ; | |
23 | int fd, i; | |
24 | unsigned char *buf; | |
25 | ||
7b45f21c | 26 | fd = mkstemp(templ); |
f7add556 JO |
27 | |
28 | buf = malloc(size); | |
29 | if (!buf) { | |
30 | close(fd); | |
31 | return NULL; | |
32 | } | |
33 | ||
34 | for (i = 0; i < size; i++) | |
35 | buf[i] = (unsigned char) ((int) i % 10); | |
36 | ||
37 | if (size != write(fd, buf, size)) | |
38 | templ = NULL; | |
39 | ||
40 | close(fd); | |
41 | return templ; | |
42 | } | |
43 | ||
44 | #define TEST_FILE_SIZE (DSO__DATA_CACHE_SIZE * 20) | |
45 | ||
46 | struct test_data_offset { | |
47 | off_t offset; | |
48 | u8 data[10]; | |
49 | int size; | |
50 | }; | |
51 | ||
52 | struct test_data_offset offsets[] = { | |
53 | /* Fill first cache page. */ | |
54 | { | |
55 | .offset = 10, | |
56 | .data = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }, | |
57 | .size = 10, | |
58 | }, | |
59 | /* Read first cache page. */ | |
60 | { | |
61 | .offset = 10, | |
62 | .data = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }, | |
63 | .size = 10, | |
64 | }, | |
65 | /* Fill cache boundary pages. */ | |
66 | { | |
67 | .offset = DSO__DATA_CACHE_SIZE - DSO__DATA_CACHE_SIZE % 10, | |
68 | .data = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }, | |
69 | .size = 10, | |
70 | }, | |
71 | /* Read cache boundary pages. */ | |
72 | { | |
73 | .offset = DSO__DATA_CACHE_SIZE - DSO__DATA_CACHE_SIZE % 10, | |
74 | .data = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }, | |
75 | .size = 10, | |
76 | }, | |
77 | /* Fill final cache page. */ | |
78 | { | |
79 | .offset = TEST_FILE_SIZE - 10, | |
80 | .data = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }, | |
81 | .size = 10, | |
82 | }, | |
83 | /* Read final cache page. */ | |
84 | { | |
85 | .offset = TEST_FILE_SIZE - 10, | |
86 | .data = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }, | |
87 | .size = 10, | |
88 | }, | |
89 | /* Read final cache page. */ | |
90 | { | |
91 | .offset = TEST_FILE_SIZE - 3, | |
92 | .data = { 7, 8, 9, 0, 0, 0, 0, 0, 0, 0 }, | |
93 | .size = 3, | |
94 | }, | |
95 | }; | |
96 | ||
97 | int dso__test_data(void) | |
98 | { | |
99 | struct machine machine; | |
100 | struct dso *dso; | |
101 | char *file = test_file(TEST_FILE_SIZE); | |
102 | size_t i; | |
103 | ||
104 | TEST_ASSERT_VAL("No test file", file); | |
105 | ||
106 | memset(&machine, 0, sizeof(machine)); | |
107 | ||
108 | dso = dso__new((const char *)file); | |
109 | ||
110 | /* Basic 10 bytes tests. */ | |
111 | for (i = 0; i < ARRAY_SIZE(offsets); i++) { | |
112 | struct test_data_offset *data = &offsets[i]; | |
113 | ssize_t size; | |
114 | u8 buf[10]; | |
115 | ||
116 | memset(buf, 0, 10); | |
117 | size = dso__data_read_offset(dso, &machine, data->offset, | |
118 | buf, 10); | |
119 | ||
120 | TEST_ASSERT_VAL("Wrong size", size == data->size); | |
121 | TEST_ASSERT_VAL("Wrong data", !memcmp(buf, data->data, 10)); | |
122 | } | |
123 | ||
124 | /* Read cross multiple cache pages. */ | |
125 | { | |
126 | ssize_t size; | |
127 | int c; | |
128 | u8 *buf; | |
129 | ||
130 | buf = malloc(TEST_FILE_SIZE); | |
131 | TEST_ASSERT_VAL("ENOMEM\n", buf); | |
132 | ||
133 | /* First iteration to fill caches, second one to read them. */ | |
134 | for (c = 0; c < 2; c++) { | |
135 | memset(buf, 0, TEST_FILE_SIZE); | |
136 | size = dso__data_read_offset(dso, &machine, 10, | |
137 | buf, TEST_FILE_SIZE); | |
138 | ||
139 | TEST_ASSERT_VAL("Wrong size", | |
140 | size == (TEST_FILE_SIZE - 10)); | |
141 | ||
142 | for (i = 0; i < (size_t)size; i++) | |
143 | TEST_ASSERT_VAL("Wrong data", | |
144 | buf[i] == (i % 10)); | |
145 | } | |
146 | ||
147 | free(buf); | |
148 | } | |
149 | ||
150 | dso__delete(dso); | |
151 | unlink(file); | |
152 | return 0; | |
153 | } |