| 1 |
/* HEADER Testing read_config_read_octet_string_const(). */ |
| 2 |
|
| 3 |
struct read_config_testcase { |
| 4 |
/* |
| 5 |
* inputs |
| 6 |
*/ |
| 7 |
const char *(*pf) (const char * readfrom, u_char ** str, |
| 8 |
size_t * len); |
| 9 |
const char *readfrom; |
| 10 |
size_t obuf_len; |
| 11 |
|
| 12 |
/* |
| 13 |
* expected outputs |
| 14 |
*/ |
| 15 |
size_t expected_offset; |
| 16 |
const u_char *expected_output; |
| 17 |
size_t expected_len; |
| 18 |
}; |
| 19 |
|
| 20 |
static const u_char obuf1[] = { 1, 0, 2 }; |
| 21 |
static const u_char obuf2[] = { 'a', 'b', 'c', 0 }; |
| 22 |
|
| 23 |
static const struct read_config_testcase test_input[] = { |
| 24 |
{ &read_config_read_octet_string_const, "", 1, -1, NULL, 0 }, |
| 25 |
{ &read_config_read_octet_string_const, "0x0", 1, -1, NULL, 1 }, |
| 26 |
{ &read_config_read_octet_string_const, "0x0 0", 1, -1, NULL, 1 }, |
| 27 |
|
| 28 |
{ &read_config_read_octet_string_const, "0x010002", 1, -1, NULL, 0 }, |
| 29 |
{ &read_config_read_octet_string_const, "0x010002", 2, -1, NULL, 0 }, |
| 30 |
{ &read_config_read_octet_string_const, "0x010002", 3, -1, obuf1, 0 }, |
| 31 |
{ &read_config_read_octet_string_const, "0x010002", 4, -1, obuf1, 3 }, |
| 32 |
{ &read_config_read_octet_string_const, "0x010002 0", 4, 9, obuf1, 3 }, |
| 33 |
{ &read_config_read_octet_string_const, "0x010002", 0, -1, obuf1, 3 }, |
| 34 |
|
| 35 |
{ &read_config_read_octet_string_const, "abc", 1, -1, NULL, 0 }, |
| 36 |
{ &read_config_read_octet_string_const, "abc z", 1, 4, NULL, 0 }, |
| 37 |
{ &read_config_read_octet_string_const, "abc", 2, -1, NULL, 1 }, |
| 38 |
{ &read_config_read_octet_string_const, "abc", 3, -1, obuf2, 2 }, |
| 39 |
{ &read_config_read_octet_string_const, "abc", 4, -1, obuf2, 3 }, |
| 40 |
{ &read_config_read_octet_string_const, "abc z", 4, 4, obuf2, 3 }, |
| 41 |
{ &read_config_read_octet_string_const, "abc", 0, -1, obuf2, 3 }, |
| 42 |
}; |
| 43 |
|
| 44 |
unsigned int i, j, ok; |
| 45 |
|
| 46 |
for (i = 0; i < sizeof(test_input) / sizeof(test_input[0]); i++) { |
| 47 |
const struct read_config_testcase *const p = &test_input[i]; |
| 48 |
size_t len = p->obuf_len; |
| 49 |
u_char *str = len > 0 ? malloc(len) : NULL; |
| 50 |
const char *result; |
| 51 |
size_t offset; |
| 52 |
|
| 53 |
fflush(stdout); |
| 54 |
result = (p->pf) (p->readfrom, &str, &len); |
| 55 |
offset = result ? result - p->readfrom : -1; |
| 56 |
OKF(offset == p->expected_offset, |
| 57 |
("test %d: expected offset %" NETSNMP_PRIz "d, got offset %" |
| 58 |
NETSNMP_PRIz "d", i, p->expected_offset, offset)); |
| 59 |
if (offset == p->expected_offset) { |
| 60 |
OKF(len == p->expected_len, |
| 61 |
("test %d: expected length %" NETSNMP_PRIz "d, got length %" |
| 62 |
NETSNMP_PRIz "d", i, p->expected_len, len)); |
| 63 |
if (len == p->expected_len) { |
| 64 |
ok = !p->expected_output |
| 65 |
|| memcmp(str, p->expected_output, len) == 0 |
| 66 |
|| p->expected_output[len] != 0; |
| 67 |
OKF(ok, ("test %d: output buffer mismatch", i)); |
| 68 |
if (!ok) { |
| 69 |
printf("Expected: "); |
| 70 |
for (j = 0; j < p->expected_len; ++j) |
| 71 |
printf("%02x ", p->expected_output[j]); |
| 72 |
printf("\nActual: "); |
| 73 |
for (j = 0; j < len; ++j) |
| 74 |
printf("%02x ", str[j]); |
| 75 |
printf("\n"); |
| 76 |
} |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
if (str) |
| 81 |
free(str); |
| 82 |
} |