| 1 |
#include "../../../src/lib/serialize.h" |
| 2 |
|
| 3 |
#include "../../lib/runner.h" |
| 4 |
|
| 5 |
TEST_MODULE(lib_serialize); |
| 6 |
|
| 7 |
/****************************************************************************** |
| 8 |
* |
| 9 |
* Simple schema with stock fields. |
| 10 |
* |
| 11 |
******************************************************************************/ |
| 12 |
|
| 13 |
#define PERSON(X, ...) \ |
| 14 |
X(text, name, ##__VA_ARGS__) \ |
| 15 |
X(uint64, age, ##__VA_ARGS__) |
| 16 |
|
| 17 |
SERIALIZE__DEFINE(person, PERSON); |
| 18 |
SERIALIZE__IMPLEMENT(person, PERSON); |
| 19 |
|
| 20 |
/****************************************************************************** |
| 21 |
* |
| 22 |
* Complex schema with a custom field. |
| 23 |
* |
| 24 |
******************************************************************************/ |
| 25 |
|
| 26 |
struct pages |
| 27 |
{ |
| 28 |
uint16_t n; /* Number of pages */ |
| 29 |
uint16_t size; /* Size of each page */ |
| 30 |
uint32_t __unused__; |
| 31 |
void **bufs; /* Array of page buffers */ |
| 32 |
}; |
| 33 |
|
| 34 |
static void create_pages(unsigned n, unsigned size, struct pages *pages) |
| 35 |
{ |
| 36 |
unsigned i; |
| 37 |
pages->n = n; |
| 38 |
pages->size = size; |
| 39 |
pages->bufs = munit_malloc(n * sizeof *pages->bufs); |
| 40 |
for (i = 0; i < pages->n; i++) { |
| 41 |
pages->bufs[i] = munit_malloc(size); |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
static void destroy_pages(struct pages *pages) |
| 46 |
{ |
| 47 |
unsigned i; |
| 48 |
for (i = 0; i < pages->n; i++) { |
| 49 |
free(pages->bufs[i]); |
| 50 |
} |
| 51 |
free(pages->bufs); |
| 52 |
} |
| 53 |
|
| 54 |
/* Opaque pointer to a struct pages object. */ |
| 55 |
typedef struct pages pages_t; |
| 56 |
typedef struct person person_t; |
| 57 |
|
| 58 |
static size_t pages__sizeof(const pages_t *pages) |
| 59 |
{ |
| 60 |
return uint16__sizeof(&pages->n) + uint16__sizeof(&pages->size) + |
| 61 |
uint32__sizeof(&pages->__unused__) + |
| 62 |
pages->size * pages->n /* bufs */; |
| 63 |
} |
| 64 |
|
| 65 |
static void pages__encode(const pages_t *pages, void **cursor) |
| 66 |
{ |
| 67 |
unsigned i; |
| 68 |
uint16__encode(&pages->n, cursor); |
| 69 |
uint16__encode(&pages->size, cursor); |
| 70 |
uint32__encode(&pages->__unused__, cursor); |
| 71 |
for (i = 0; i < pages->n; i++) { |
| 72 |
memcpy(*cursor, pages->bufs[i], pages->size); |
| 73 |
*cursor += pages->size; |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
static int pages__decode(struct cursor *cursor, pages_t *pages) |
| 78 |
{ |
| 79 |
unsigned i; |
| 80 |
uint16__decode(cursor, &pages->n); |
| 81 |
uint16__decode(cursor, &pages->size); |
| 82 |
uint32__decode(cursor, &pages->__unused__); |
| 83 |
pages->bufs = munit_malloc(pages->n * sizeof *pages->bufs); |
| 84 |
for (i = 0; i < pages->n; i++) { |
| 85 |
pages->bufs[i] = (void *)cursor->p; |
| 86 |
cursor->p += pages->size; |
| 87 |
cursor->cap -= pages->size; |
| 88 |
} |
| 89 |
return 0; |
| 90 |
} |
| 91 |
|
| 92 |
#define BOOK(X, ...) \ |
| 93 |
X(text, title, ##__VA_ARGS__) \ |
| 94 |
X(person, author, ##__VA_ARGS__) \ |
| 95 |
X(pages, pages, ##__VA_ARGS__) |
| 96 |
|
| 97 |
SERIALIZE__DEFINE(book, BOOK); |
| 98 |
SERIALIZE__IMPLEMENT(book, BOOK); |
| 99 |
|
| 100 |
/****************************************************************************** |
| 101 |
* |
| 102 |
* Fixture |
| 103 |
* |
| 104 |
******************************************************************************/ |
| 105 |
|
| 106 |
struct fixture |
| 107 |
{ |
| 108 |
struct person person; |
| 109 |
struct book book; |
| 110 |
}; |
| 111 |
|
| 112 |
static void *setup(const MunitParameter params[], void *user_data) |
| 113 |
{ |
| 114 |
struct fixture *f = munit_malloc(sizeof *f); |
| 115 |
(void)params; |
| 116 |
(void)user_data; |
| 117 |
return f; |
| 118 |
} |
| 119 |
|
| 120 |
static void tear_down(void *data) |
| 121 |
{ |
| 122 |
free(data); |
| 123 |
} |
| 124 |
|
| 125 |
/****************************************************************************** |
| 126 |
* |
| 127 |
* Fields definition. |
| 128 |
* |
| 129 |
******************************************************************************/ |
| 130 |
|
| 131 |
TEST_SUITE(fields); |
| 132 |
TEST_SETUP(fields, setup); |
| 133 |
TEST_TEAR_DOWN(fields, tear_down); |
| 134 |
|
| 135 |
/* The expected fields are defined on the struct. */ |
| 136 |
TEST_CASE(fields, define, NULL) |
| 137 |
{ |
| 138 |
struct fixture *f = data; |
| 139 |
(void)params; |
| 140 |
f->person.name = "John Doh"; |
| 141 |
f->person.age = 40; |
| 142 |
return MUNIT_OK; |
| 143 |
} |
| 144 |
|
| 145 |
/****************************************************************************** |
| 146 |
* |
| 147 |
* Sizeof method. |
| 148 |
* |
| 149 |
******************************************************************************/ |
| 150 |
|
| 151 |
TEST_SUITE(sizeof); |
| 152 |
TEST_SETUP(sizeof, setup); |
| 153 |
TEST_TEAR_DOWN(sizeof, tear_down); |
| 154 |
|
| 155 |
/* Padding is added if needed. */ |
| 156 |
TEST_CASE(sizeof, padding, NULL) |
| 157 |
{ |
| 158 |
struct fixture *f = data; |
| 159 |
size_t size; |
| 160 |
(void)params; |
| 161 |
f->person.name = "John Doh"; |
| 162 |
f->person.age = 40; |
| 163 |
size = person__sizeof(&f->person); |
| 164 |
munit_assert_int(size, ==, 16 /* name */ + 8 /* age */); |
| 165 |
return MUNIT_OK; |
| 166 |
} |
| 167 |
|
| 168 |
/* Padding is not added if a string ends exactly at word boundary. */ |
| 169 |
TEST_CASE(sizeof, no_padding, NULL) |
| 170 |
{ |
| 171 |
struct fixture *f = data; |
| 172 |
size_t size; |
| 173 |
(void)params; |
| 174 |
f->person.name = "Joe Doh"; |
| 175 |
f->person.age = 40; |
| 176 |
size = person__sizeof(&f->person); |
| 177 |
munit_assert_int(size, ==, 8 /* name */ + 8 /* age */); |
| 178 |
return MUNIT_OK; |
| 179 |
} |
| 180 |
|
| 181 |
/****************************************************************************** |
| 182 |
* |
| 183 |
* Encode method. |
| 184 |
* |
| 185 |
******************************************************************************/ |
| 186 |
|
| 187 |
TEST_SUITE(encode); |
| 188 |
TEST_SETUP(encode, setup); |
| 189 |
TEST_TEAR_DOWN(encode, tear_down); |
| 190 |
|
| 191 |
/* Padding is added if needed. */ |
| 192 |
TEST_CASE(encode, padding, NULL) |
| 193 |
{ |
| 194 |
struct fixture *f = data; |
| 195 |
size_t size; |
| 196 |
void *buf; |
| 197 |
void *cursor; |
| 198 |
(void)params; |
| 199 |
f->person.name = "John Doh"; |
| 200 |
f->person.age = 40; |
| 201 |
size = person__sizeof(&f->person); |
| 202 |
buf = munit_malloc(size); |
| 203 |
cursor = buf; |
| 204 |
person__encode(&f->person, &cursor); |
| 205 |
munit_assert_string_equal(buf, "John Doh"); |
| 206 |
munit_assert_int(ByteFlipLe64(*(uint64_t *)(buf + 16)), ==, 40); |
| 207 |
free(buf); |
| 208 |
return MUNIT_OK; |
| 209 |
} |
| 210 |
|
| 211 |
/* Padding is not added if a string ends exactly at word boundary. */ |
| 212 |
TEST_CASE(encode, no_padding, NULL) |
| 213 |
{ |
| 214 |
struct fixture *f = data; |
| 215 |
size_t size; |
| 216 |
void *buf; |
| 217 |
void *cursor; |
| 218 |
(void)params; |
| 219 |
f->person.name = "Joe Doh"; |
| 220 |
f->person.age = 40; |
| 221 |
size = person__sizeof(&f->person); |
| 222 |
buf = munit_malloc(size); |
| 223 |
cursor = buf; |
| 224 |
person__encode(&f->person, &cursor); |
| 225 |
munit_assert_string_equal(buf, "Joe Doh"); |
| 226 |
munit_assert_int(ByteFlipLe64(*(uint64_t *)(buf + 8)), ==, 40); |
| 227 |
free(buf); |
| 228 |
return MUNIT_OK; |
| 229 |
} |
| 230 |
|
| 231 |
/* Encode a custom complex field. */ |
| 232 |
TEST_CASE(encode, custom, NULL) |
| 233 |
{ |
| 234 |
struct fixture *f = data; |
| 235 |
size_t size; |
| 236 |
void *buf; |
| 237 |
void *cursor; |
| 238 |
(void)params; |
| 239 |
f->book.title = "Les miserables"; |
| 240 |
f->book.author.name = "Victor Hugo"; |
| 241 |
f->book.author.age = 40; |
| 242 |
create_pages(2, 8, &f->book.pages); |
| 243 |
strcpy(f->book.pages.bufs[0], "Fantine"); |
| 244 |
strcpy(f->book.pages.bufs[1], "Cosette"); |
| 245 |
|
| 246 |
size = book__sizeof(&f->book); |
| 247 |
munit_assert_int(size, ==, |
| 248 |
16 + /* title */ |
| 249 |
16 + /* author name */ |
| 250 |
8 + /* author age */ |
| 251 |
2 + /* n pages */ |
| 252 |
2 + /* page size */ |
| 253 |
4 + /* unused */ |
| 254 |
8 * 2 /* page buffers */); |
| 255 |
|
| 256 |
buf = munit_malloc(size); |
| 257 |
cursor = buf; |
| 258 |
book__encode(&f->book, &cursor); |
| 259 |
|
| 260 |
cursor = buf; |
| 261 |
|
| 262 |
munit_assert_string_equal(cursor, "Les miserables"); |
| 263 |
cursor += 16; |
| 264 |
|
| 265 |
munit_assert_string_equal(cursor, "Victor Hugo"); |
| 266 |
cursor += 16; |
| 267 |
|
| 268 |
munit_assert_int(ByteFlipLe64(*(uint64_t *)cursor), ==, 40); |
| 269 |
cursor += 8; |
| 270 |
|
| 271 |
munit_assert_int(ByteFlipLe16(*(uint16_t *)cursor), ==, 2); |
| 272 |
cursor += 2; |
| 273 |
|
| 274 |
munit_assert_int(ByteFlipLe16(*(uint16_t *)cursor), ==, 8); |
| 275 |
cursor += 2; |
| 276 |
|
| 277 |
cursor += 4; /* Unused */ |
| 278 |
|
| 279 |
munit_assert_string_equal(cursor, "Fantine"); |
| 280 |
cursor += 8; |
| 281 |
|
| 282 |
munit_assert_string_equal(cursor, "Cosette"); |
| 283 |
|
| 284 |
free(buf); |
| 285 |
destroy_pages(&f->book.pages); |
| 286 |
|
| 287 |
return MUNIT_OK; |
| 288 |
} |
| 289 |
|
| 290 |
/****************************************************************************** |
| 291 |
* |
| 292 |
* Decode method. |
| 293 |
* |
| 294 |
******************************************************************************/ |
| 295 |
|
| 296 |
TEST_SUITE(decode); |
| 297 |
TEST_SETUP(decode, setup); |
| 298 |
TEST_TEAR_DOWN(decode, tear_down); |
| 299 |
|
| 300 |
/* Padding is added if needed. */ |
| 301 |
TEST_CASE(decode, padding, NULL) |
| 302 |
{ |
| 303 |
struct fixture *f = data; |
| 304 |
void *buf = munit_malloc(16 + 8); |
| 305 |
struct cursor cursor = {buf, 16 + 8}; |
| 306 |
(void)params; |
| 307 |
strcpy(buf, "John Doh"); |
| 308 |
*(uint64_t *)(buf + 16) = ByteFlipLe64(40); |
| 309 |
person__decode(&cursor, &f->person); |
| 310 |
munit_assert_string_equal(f->person.name, "John Doh"); |
| 311 |
munit_assert_int(f->person.age, ==, 40); |
| 312 |
free(buf); |
| 313 |
return MUNIT_OK; |
| 314 |
} |
| 315 |
|
| 316 |
/* Padding is not added if a string ends exactly at word boundary. */ |
| 317 |
TEST_CASE(decode, no_padding, NULL) |
| 318 |
{ |
| 319 |
struct fixture *f = data; |
| 320 |
void *buf = munit_malloc(16 + 8); |
| 321 |
struct cursor cursor = {buf, 16 + 8}; |
| 322 |
(void)params; |
| 323 |
strcpy(buf, "Joe Doh"); |
| 324 |
*(uint64_t *)(buf + 8) = ByteFlipLe64(40); |
| 325 |
person__decode(&cursor, &f->person); |
| 326 |
munit_assert_string_equal(f->person.name, "Joe Doh"); |
| 327 |
munit_assert_int(f->person.age, ==, 40); |
| 328 |
free(buf); |
| 329 |
return MUNIT_OK; |
| 330 |
} |
| 331 |
|
| 332 |
/* The given buffer has not enough data. */ |
| 333 |
TEST_CASE(decode, short, NULL) |
| 334 |
{ |
| 335 |
struct fixture *f = data; |
| 336 |
void *buf = munit_malloc(16); |
| 337 |
struct cursor cursor = {buf, 16}; |
| 338 |
int rc; |
| 339 |
(void)params; |
| 340 |
strcpy(buf, "John Doh"); |
| 341 |
rc = person__decode(&cursor, &f->person); |
| 342 |
munit_assert_int(rc, ==, COWSQL_PARSE); |
| 343 |
free(buf); |
| 344 |
return MUNIT_OK; |
| 345 |
} |
| 346 |
|
| 347 |
/* Decode a custom complex field. */ |
| 348 |
TEST_CASE(decode, custom, NULL) |
| 349 |
{ |
| 350 |
struct fixture *f = data; |
| 351 |
size_t len = 16 + /* title */ |
| 352 |
16 + /* author name */ |
| 353 |
8 + /* author age */ |
| 354 |
2 + /* n pages */ |
| 355 |
2 + /* page size */ |
| 356 |
4 + /* unused */ |
| 357 |
8 * 2 /* page buffers */; |
| 358 |
void *buf = munit_malloc(len); |
| 359 |
void *p = buf; |
| 360 |
struct cursor cursor = {buf, len}; |
| 361 |
(void)params; |
| 362 |
|
| 363 |
strcpy(p, "Les miserables"); |
| 364 |
p += 16; |
| 365 |
|
| 366 |
strcpy(p, "Victor Hugo"); |
| 367 |
p += 16; |
| 368 |
|
| 369 |
*(uint64_t *)p = ByteFlipLe64(40); |
| 370 |
p += 8; |
| 371 |
|
| 372 |
*(uint16_t *)p = ByteFlipLe16(2); |
| 373 |
p += 2; |
| 374 |
|
| 375 |
*(uint16_t *)p = ByteFlipLe16(8); |
| 376 |
p += 2; |
| 377 |
|
| 378 |
p += 4; /* Unused */ |
| 379 |
|
| 380 |
strcpy(p, "Fantine"); |
| 381 |
p += 8; |
| 382 |
|
| 383 |
strcpy(p, "Cosette"); |
| 384 |
|
| 385 |
book__decode(&cursor, &f->book); |
| 386 |
|
| 387 |
munit_assert_string_equal(f->book.title, "Les miserables"); |
| 388 |
munit_assert_string_equal(f->book.author.name, "Victor Hugo"); |
| 389 |
munit_assert_int(f->book.author.age, ==, 40); |
| 390 |
munit_assert_int(f->book.pages.n, ==, 2); |
| 391 |
munit_assert_int(f->book.pages.size, ==, 8); |
| 392 |
munit_assert_string_equal(f->book.pages.bufs[0], "Fantine"); |
| 393 |
munit_assert_string_equal(f->book.pages.bufs[1], "Cosette"); |
| 394 |
|
| 395 |
free(f->book.pages.bufs); |
| 396 |
|
| 397 |
free(buf); |
| 398 |
return MUNIT_OK; |
| 399 |
} |