Max has uploaded this change for review. ( https://gerrit.osmocom.org/12017
Change subject: msgb: add test helper ...................................................................... msgb: add test helper It's often handy to compare msgb to a given array and print the position where they differ. Change-Id: I3bc95f2f5ab6e3f4b502647fb3e0aaaf1f7c4cf5 --- M include/osmocom/core/msgb.h M src/msgb.c 2 files changed, 83 insertions(+), 0 deletions(-) git pull ssh://gerrit.osmocom.org:29418/libosmocore refs/changes/17/12017/1 diff --git a/include/osmocom/core/msgb.h b/include/osmocom/core/msgb.h index 2449151..6e344f1 100644 --- a/include/osmocom/core/msgb.h +++ b/include/osmocom/core/msgb.h @@ -565,6 +565,8 @@ void *msgb_talloc_ctx_init(void *root_ctx, unsigned int pool_size); void msgb_set_talloc_ctx(void *ctx) OSMO_DEPRECATED("Use msgb_talloc_ctx_init() instead"); int msgb_printf(struct msgb *msgb, const char *format, ...); +bool msgb_cmpr_l3(const char *f, size_t l, const struct msgb *msg, const uint8_t *data, size_t len, bool print); +bool msgb_cmpr(const char *f, size_t l, const struct msgb *msg, const uint8_t *data, size_t len, bool print); static inline const char *msgb_hexdump_l2(const struct msgb *msg) { diff --git a/src/msgb.c b/src/msgb.c index 844cfc6..ebecc3e 100644 --- a/src/msgb.c +++ b/src/msgb.c @@ -173,6 +173,87 @@ return msg->data; } +static inline bool compare_n_print(const char *f, size_t l, uint8_t level, + const uint8_t *m, const uint8_t *d, size_t len, bool print) +{ + size_t i; + if (memcmp(m, d, len) == 0) + return true; + + if (!print) + return false; + + printf("%s:%zu L%u data mismatch:\nexpected %s\n ", + f, l, level, osmo_hexdump(d, len)); + + for(i = 0; i < len; i++) + if (d[i] != m[i]) { + printf("!!\n"); + break; + } else + printf(".. "); + + /* N. B: that's intended to be used bymsgb_cmpr*() so length check is already passed, + so we can use len for both m and d */ + printf(" msgb %s\n", osmo_hexdump(m, len)); + + return false; +} + +/*! Compare and print: check L3 data in msgb against given data and print erros if any + * \param[in] f text prefix, usually __func__, ignored if print == false + * \param[in] l numeric prefix, usually __LINE__, ignored if print == false + * \param[in] msg message buffer + * \param[in] data expected L3 data + * \param[in] len length of data + * \param[in] print boolean indicating whether we should print anything to stdout + * \returns boolean indicating whether msgb L3 content is equal to a given data + */ +bool msgb_cmpr_l3(const char *f, size_t l, const struct msgb *msg, const uint8_t *data, size_t len, bool print) +{ + if (!msg) { + if (print) + printf("%s:%zu NULL msg comparison\n", f, l); + return false; + } + + if (msgb_l3len(msg) != len) { + if (print) + printf("%s:%zu Length mismatch: %d != %zu, %s\n", + f, l, msgb_l3len(msg), len, msgb_hexdump_l3(msg)); + return false; + } + + return compare_n_print(f, l, 3, msgb_l3(msg), data, len, print); +} + +/*! Compare and print: check data in msgb against given data and print erros if any + * \param[in] f text prefix, usually __func__, ignored if print == false + * \param[in] l numeric prefix, usually __LINE__, ignored if print == false + * \param[in] msg message buffer + * \param[in] data expected data + * \param[in] len length of data + * \param[in] print boolean indicating whether we should print anything to stdout + * \returns boolean indicating whether msgb content is equal to a given data + */ +bool msgb_cmpr(const char *f, size_t l, const struct msgb *msg, const uint8_t *data, size_t len, bool print) +{ + if (!msg) { + if (print) + printf("%s:%zu NULL msg comparison\n", f, l); + return false; + } + + if (msgb_length(msg) != len) { + if (print) + printf("%s:%zu Length mismatch: %d != %zu, %s\n", + f, l, msgb_length(msg), len, msgb_hexdump(msg)); + return false; + } + + return compare_n_print(f, l, 0, msgb_data(msg), data, len, print); +} + /*! get length of message buffer * \param[in] msg message buffer * \returns length of data section in message buffer -- To view, visit https://gerrit.osmocom.org/12017 To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings Gerrit-Project: libosmocore Gerrit-Branch: master Gerrit-MessageType: newchange Gerrit-Change-Id: I3bc95f2f5ab6e3f4b502647fb3e0aaaf1f7c4cf5 Gerrit-Change-Number: 12017 Gerrit-PatchSet: 1 Gerrit-Owner: Max <msur...@sysmocom.de>