Jacob Erlbeck | 7cd8a1b | 2015-11-27 13:26:16 +0100 | [diff] [blame] | 1 | /* |
| 2 | * (C) 2014 by On-Waves |
| 3 | * All Rights Reserved |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License along |
| 16 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | * |
| 19 | */ |
| 20 | |
| 21 | #include <stdlib.h> |
| 22 | #include <osmocom/core/application.h> |
| 23 | #include <osmocom/core/logging.h> |
| 24 | #include <osmocom/core/utils.h> |
| 25 | #include <osmocom/core/msgb.h> |
Jacob Erlbeck | cbefa08 | 2015-11-27 13:26:17 +0100 | [diff] [blame^] | 26 | #include <setjmp.h> |
Jacob Erlbeck | 7cd8a1b | 2015-11-27 13:26:16 +0100 | [diff] [blame] | 27 | |
| 28 | #include <errno.h> |
| 29 | |
| 30 | #include <string.h> |
| 31 | |
| 32 | #define CHECK_RC(rc) \ |
| 33 | if (rc != 0) { \ |
| 34 | printf("Operation failed rc=%d on %s:%d\n", rc, __FILE__, __LINE__); \ |
| 35 | abort(); \ |
| 36 | } |
| 37 | |
Jacob Erlbeck | cbefa08 | 2015-11-27 13:26:17 +0100 | [diff] [blame^] | 38 | static jmp_buf jmp_env; |
| 39 | static int jmp_env_valid = 0; |
| 40 | static void osmo_panic_raise(const char *fmt, va_list args) |
| 41 | { |
| 42 | /* |
| 43 | * The args can include pointer values which are not suitable for |
| 44 | * regression testing. So just write the (hopefully constant) format |
| 45 | * string to stdout and write the full message to stderr. |
| 46 | */ |
| 47 | printf("%s", fmt); |
| 48 | vfprintf(stderr, fmt, args); |
| 49 | if (!jmp_env_valid) |
| 50 | abort(); |
| 51 | longjmp(jmp_env, 1); |
| 52 | } |
| 53 | |
| 54 | /* Note that this does not nest */ |
| 55 | #define OSMO_PANIC_TRY(pE) (osmo_panic_try(pE, setjmp(jmp_env))) |
| 56 | |
| 57 | static int osmo_panic_try(volatile int *exception, int setjmp_result) |
| 58 | { |
| 59 | jmp_env_valid = setjmp_result == 0; |
| 60 | *exception = setjmp_result; |
| 61 | |
| 62 | if (setjmp_result) |
| 63 | fprintf(stderr, "Exception caught: %d\n", setjmp_result); |
| 64 | |
| 65 | return *exception == 0; |
| 66 | } |
| 67 | |
Jacob Erlbeck | 7cd8a1b | 2015-11-27 13:26:16 +0100 | [diff] [blame] | 68 | static void test_msgb_api() |
| 69 | { |
| 70 | struct msgb *msg = msgb_alloc_headroom(4096, 128, "data"); |
| 71 | unsigned char *cptr = NULL; |
| 72 | int rc; |
| 73 | |
| 74 | printf("Testing the msgb API\n"); |
| 75 | |
| 76 | printf("Buffer: %s\n", msgb_hexdump(msg)); |
| 77 | OSMO_ASSERT(msgb_test_invariant(msg)); |
| 78 | cptr = msg->l1h = msgb_put(msg, 4); |
| 79 | printf("put(4) -> data%+d\n", cptr - msg->data); |
| 80 | printf("Buffer: %s\n", msgb_hexdump(msg)); |
| 81 | OSMO_ASSERT(msgb_test_invariant(msg)); |
| 82 | cptr = msg->l2h = msgb_put(msg, 4); |
| 83 | printf("put(4) -> data%+d\n", cptr - msg->data); |
| 84 | printf("Buffer: %s\n", msgb_hexdump(msg)); |
| 85 | OSMO_ASSERT(msgb_test_invariant(msg)); |
| 86 | cptr = msg->l3h = msgb_put(msg, 4); |
| 87 | printf("put(4) -> data%+d\n", cptr - msg->data); |
| 88 | printf("Buffer: %s\n", msgb_hexdump(msg)); |
| 89 | OSMO_ASSERT(msgb_test_invariant(msg)); |
| 90 | cptr = msg->l4h = msgb_put(msg, 4); |
| 91 | printf("put(4) -> data%+d\n", cptr - msg->data); |
| 92 | printf("Buffer: %s\n", msgb_hexdump(msg)); |
| 93 | OSMO_ASSERT(msgb_test_invariant(msg)); |
| 94 | OSMO_ASSERT(msgb_length(msg) == 16); |
| 95 | cptr = msgb_push(msg, 4); |
| 96 | printf("push(4) -> data%+d\n", cptr - msg->data); |
| 97 | printf("Buffer: %s\n", msgb_hexdump(msg)); |
| 98 | OSMO_ASSERT(msgb_test_invariant(msg)); |
| 99 | OSMO_ASSERT(msgb_length(msg) == 20); |
| 100 | rc = msgb_trim(msg, 16); |
| 101 | printf("trim(16) -> %d\n", rc); |
| 102 | CHECK_RC(rc); |
| 103 | OSMO_ASSERT(msgb_test_invariant(msg)); |
| 104 | printf("Buffer: %s\n", msgb_hexdump(msg)); |
| 105 | OSMO_ASSERT(msgb_length(msg) == 16); |
| 106 | |
| 107 | cptr = msgb_get(msg, 4); |
| 108 | printf("get(4) -> data%+d\n", cptr - msg->data); |
| 109 | printf("Buffer: %s\n", msgb_hexdump(msg)); |
| 110 | OSMO_ASSERT(msgb_test_invariant(msg)); |
| 111 | OSMO_ASSERT(msgb_length(msg) == 12); |
| 112 | |
| 113 | printf("Test msgb_hexdump\n"); |
| 114 | msg->l1h = msg->head; |
| 115 | printf("Buffer: %s\n", msgb_hexdump(msg)); |
| 116 | msg->l3h = msg->data; |
| 117 | printf("Buffer: %s\n", msgb_hexdump(msg)); |
| 118 | msg->l3h = msg->head - 1; |
| 119 | printf("Buffer: %s\n", msgb_hexdump(msg)); |
| 120 | |
| 121 | msgb_free(msg); |
| 122 | } |
| 123 | |
| 124 | static struct log_info info = {}; |
| 125 | |
| 126 | int main(int argc, char **argv) |
| 127 | { |
| 128 | osmo_init_logging(&info); |
| 129 | |
| 130 | test_msgb_api(); |
| 131 | |
| 132 | printf("Success.\n"); |
| 133 | |
| 134 | return 0; |
| 135 | } |