| 1 |
/* |
| 2 |
* Test D-Bus Message Sender Stitching |
| 3 |
*/ |
| 4 |
|
| 5 |
#undef NDEBUG |
| 6 |
#include <c-dvar.h> |
| 7 |
#include <c-dvar-type.h> |
| 8 |
#include <c-stdaux.h> |
| 9 |
#include <stdlib.h> |
| 10 |
#include "dbus/address.h" |
| 11 |
#include "dbus/message.h" |
| 12 |
#include "dbus/protocol.h" |
| 13 |
|
| 14 |
static const CDVarType test_message_type[] = { |
| 15 |
C_DVAR_T_INIT( |
| 16 |
/* ((yyyyuua(yv))(uuu)) */ |
| 17 |
C_DVAR_T_TUPLE2( |
| 18 |
C_DVAR_T_TUPLE7( |
| 19 |
C_DVAR_T_y, |
| 20 |
C_DVAR_T_y, |
| 21 |
C_DVAR_T_y, |
| 22 |
C_DVAR_T_y, |
| 23 |
C_DVAR_T_u, |
| 24 |
C_DVAR_T_u, |
| 25 |
C_DVAR_T_ARRAY( |
| 26 |
C_DVAR_T_TUPLE2( |
| 27 |
C_DVAR_T_y, |
| 28 |
C_DVAR_T_v |
| 29 |
) |
| 30 |
) |
| 31 |
), |
| 32 |
C_DVAR_T_TUPLE3( |
| 33 |
C_DVAR_T_u, |
| 34 |
C_DVAR_T_u, |
| 35 |
C_DVAR_T_u |
| 36 |
) |
| 37 |
) |
| 38 |
) |
| 39 |
}; |
| 40 |
|
| 41 |
static Message *test_new_message(size_t before, const char *sender_early, size_t after, const char *sender_late) { |
| 42 |
_c_cleanup_(c_dvar_deinit) CDVar v = C_DVAR_INIT; |
| 43 |
Message *message; |
| 44 |
size_t n_data; |
| 45 |
void *data; |
| 46 |
char *p; |
| 47 |
int r; |
| 48 |
|
| 49 |
c_dvar_begin_write(&v, (__BYTE_ORDER == __BIG_ENDIAN), test_message_type, 1); |
| 50 |
c_dvar_write(&v, "((yyyyuu[", |
| 51 |
c_dvar_is_big_endian(&v) ? 'B' : 'l', |
| 52 |
128, |
| 53 |
1, |
| 54 |
1, |
| 55 |
0, |
| 56 |
(uint32_t)-1); |
| 57 |
c_dvar_write(&v, "(y<g>)", |
| 58 |
DBUS_MESSAGE_FIELD_SIGNATURE, |
| 59 |
&c_dvar_type_g, |
| 60 |
"uuu"); |
| 61 |
|
| 62 |
if (before) { |
| 63 |
p = malloc(before + 1); |
| 64 |
c_assert(p); |
| 65 |
c_memset(p, 'a', before); |
| 66 |
p[before] = 0; |
| 67 |
|
| 68 |
c_dvar_write(&v, "(y<s>)", |
| 69 |
DBUS_MESSAGE_FIELD_MEMBER, |
| 70 |
&c_dvar_type_s, |
| 71 |
p); |
| 72 |
free(p); |
| 73 |
} |
| 74 |
|
| 75 |
if (sender_early) { |
| 76 |
c_dvar_write(&v, "(y<s>)", |
| 77 |
DBUS_MESSAGE_FIELD_SENDER, |
| 78 |
&c_dvar_type_s, |
| 79 |
sender_early); |
| 80 |
} |
| 81 |
|
| 82 |
if (after) { |
| 83 |
p = malloc(after + 1); |
| 84 |
c_assert(p); |
| 85 |
c_memset(p, 'a', after); |
| 86 |
p[0] = '/'; |
| 87 |
p[after] = 0; |
| 88 |
|
| 89 |
c_dvar_write(&v, "(y<o>)", |
| 90 |
DBUS_MESSAGE_FIELD_PATH, |
| 91 |
&c_dvar_type_o, |
| 92 |
p); |
| 93 |
|
| 94 |
free(p); |
| 95 |
} |
| 96 |
|
| 97 |
if (sender_late) |
| 98 |
c_dvar_write(&v, "(y<s>)", |
| 99 |
DBUS_MESSAGE_FIELD_SENDER, |
| 100 |
&c_dvar_type_s, |
| 101 |
sender_late); |
| 102 |
|
| 103 |
c_dvar_write(&v, "])(uuu))", 7, 8, 9); |
| 104 |
r = c_dvar_end_write(&v, &data, &n_data); |
| 105 |
c_assert(!r); |
| 106 |
|
| 107 |
r = message_new_outgoing(&message, data, n_data); |
| 108 |
c_assert(!r); |
| 109 |
|
| 110 |
r = message_parse_metadata(message); |
| 111 |
c_assert(!r); |
| 112 |
|
| 113 |
return message; |
| 114 |
} |
| 115 |
|
| 116 |
static void test_assert_message(Message *message, size_t before, const char *sender, size_t after) { |
| 117 |
_c_cleanup_(message_unrefp) Message *expected = NULL; |
| 118 |
_c_cleanup_(c_freep) void *p = NULL; |
| 119 |
size_t i, n; |
| 120 |
|
| 121 |
expected = test_new_message(before, NULL, after, sender); |
| 122 |
|
| 123 |
for (n = 0, i = 0; i < C_ARRAY_SIZE(message->vecs); ++i) |
| 124 |
n += message->vecs[i].iov_len; |
| 125 |
|
| 126 |
p = malloc(n); |
| 127 |
c_assert(p); |
| 128 |
|
| 129 |
for (n = 0, i = 0; i < C_ARRAY_SIZE(message->vecs); ++i) { |
| 130 |
c_memcpy(p + n, message->vecs[i].iov_base, message->vecs[i].iov_len); |
| 131 |
n += message->vecs[i].iov_len; |
| 132 |
} |
| 133 |
|
| 134 |
c_assert(n == expected->n_data); |
| 135 |
c_assert(!memcmp(p, expected->data, n)); |
| 136 |
} |
| 137 |
|
| 138 |
static void test_stitching(void) { |
| 139 |
Message *message; |
| 140 |
Address addr; |
| 141 |
size_t i, n; |
| 142 |
char *from, *to; |
| 143 |
|
| 144 |
/* |
| 145 |
* To test sender stitching, we repeatedly create messages with |
| 146 |
* different header fields, stitch them, and compare them with what we |
| 147 |
* expect as result. |
| 148 |
* |
| 149 |
* The different modulo-calculations here are used to get varying |
| 150 |
* combinations of string-lengths, so their size and padding differs on |
| 151 |
* each run. |
| 152 |
*/ |
| 153 |
|
| 154 |
for (i = 0; i < 1024; ++i) { |
| 155 |
n = 8 + i % 8; |
| 156 |
from = malloc(n + 1); |
| 157 |
c_assert(from); |
| 158 |
c_memset(from, '1', n); |
| 159 |
from[0] = ':'; |
| 160 |
from[1] = '1'; |
| 161 |
from[2] = '.'; |
| 162 |
from[n] = 0; |
| 163 |
|
| 164 |
n = 8 + i % 11; |
| 165 |
to = malloc(n + 1); |
| 166 |
c_assert(to); |
| 167 |
c_memset(to, '2', n); |
| 168 |
to[0] = ':'; |
| 169 |
to[1] = '1'; |
| 170 |
to[2] = '.'; |
| 171 |
to[n] = 0; |
| 172 |
|
| 173 |
address_from_string(&addr, to); |
| 174 |
c_assert(addr.type == ADDRESS_TYPE_ID); |
| 175 |
|
| 176 |
message = test_new_message(i % 13, from, i / 17, NULL); |
| 177 |
message_stitch_sender(message, addr.id); |
| 178 |
test_assert_message(message, i % 13, to, i / 17); |
| 179 |
message_unref(message); |
| 180 |
|
| 181 |
free(to); |
| 182 |
free(from); |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
int main(int argc, char **argv) { |
| 187 |
test_stitching(); |
| 188 |
return 0; |
| 189 |
} |