Jacob Erlbeck | 626369c | 2015-06-15 11:04:25 +0200 | [diff] [blame] | 1 | /* |
| 2 | * LlcTest.cpp |
| 3 | * |
| 4 | * Copyright (C) 2015 by Sysmocom s.f.m.c. GmbH |
| 5 | * |
| 6 | * All Rights Reserved |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU Affero General Public License as published by |
| 10 | * the Free Software Foundation; either version 3 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU Affero General Public License |
| 19 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 20 | * |
| 21 | */ |
| 22 | |
| 23 | extern "C" { |
| 24 | #include <osmocom/core/linuxlist.h> |
| 25 | } |
| 26 | |
| 27 | #include "llc.h" |
| 28 | #include "gprs_debug.h" |
| 29 | |
| 30 | extern "C" { |
| 31 | #include "pcu_vty.h" |
| 32 | |
| 33 | #include <osmocom/core/application.h> |
| 34 | #include <osmocom/core/msgb.h> |
| 35 | #include <osmocom/core/talloc.h> |
| 36 | #include <osmocom/core/utils.h> |
| 37 | #include <osmocom/vty/vty.h> |
| 38 | } |
| 39 | |
| 40 | |
| 41 | void *tall_pcu_ctx; |
| 42 | int16_t spoof_mnc = 0, spoof_mcc = 0; |
| 43 | |
Jacob Erlbeck | b671dbf | 2015-06-15 14:32:33 +0200 | [diff] [blame] | 44 | static void enqueue_data(gprs_llc_queue *queue, const uint8_t *data, size_t len, |
| 45 | gprs_llc_queue::MetaInfo *info = 0) |
Jacob Erlbeck | 626369c | 2015-06-15 11:04:25 +0200 | [diff] [blame] | 46 | { |
| 47 | struct timeval *tv; |
| 48 | uint8_t *msg_data; |
| 49 | struct msgb *llc_msg = msgb_alloc(len + sizeof(*tv) * 2, |
| 50 | "llc_pdu_queue"); |
| 51 | |
Jacob Erlbeck | 626369c | 2015-06-15 11:04:25 +0200 | [diff] [blame] | 52 | msg_data = (uint8_t *)msgb_put(llc_msg, len); |
| 53 | |
| 54 | memcpy(msg_data, data, len); |
| 55 | |
Jacob Erlbeck | b671dbf | 2015-06-15 14:32:33 +0200 | [diff] [blame] | 56 | queue->enqueue(llc_msg, info); |
Jacob Erlbeck | 626369c | 2015-06-15 11:04:25 +0200 | [diff] [blame] | 57 | } |
| 58 | |
Jacob Erlbeck | b671dbf | 2015-06-15 14:32:33 +0200 | [diff] [blame] | 59 | static void dequeue_and_check(gprs_llc_queue *queue, const uint8_t *exp_data, |
| 60 | size_t len, const gprs_llc_queue::MetaInfo *exp_info = 0) |
Jacob Erlbeck | 626369c | 2015-06-15 11:04:25 +0200 | [diff] [blame] | 61 | { |
| 62 | struct msgb *llc_msg; |
| 63 | uint8_t *msg_data; |
Jacob Erlbeck | b671dbf | 2015-06-15 14:32:33 +0200 | [diff] [blame] | 64 | const gprs_llc_queue::MetaInfo *info_res; |
Jacob Erlbeck | 626369c | 2015-06-15 11:04:25 +0200 | [diff] [blame] | 65 | |
Jacob Erlbeck | b671dbf | 2015-06-15 14:32:33 +0200 | [diff] [blame] | 66 | llc_msg = queue->dequeue(&info_res); |
Jacob Erlbeck | 626369c | 2015-06-15 11:04:25 +0200 | [diff] [blame] | 67 | OSMO_ASSERT(llc_msg != NULL); |
| 68 | |
Jacob Erlbeck | b671dbf | 2015-06-15 14:32:33 +0200 | [diff] [blame] | 69 | fprintf(stderr, "dequeued msg, length %d (expected %d), data %s\n", |
| 70 | msgb_length(llc_msg), len, msgb_hexdump(llc_msg)); |
Jacob Erlbeck | 626369c | 2015-06-15 11:04:25 +0200 | [diff] [blame] | 71 | |
| 72 | OSMO_ASSERT(msgb_length(llc_msg) == len); |
| 73 | msg_data = msgb_data(llc_msg); |
| 74 | |
| 75 | OSMO_ASSERT(memcmp(msg_data, exp_data, len) == 0); |
Jacob Erlbeck | b671dbf | 2015-06-15 14:32:33 +0200 | [diff] [blame] | 76 | |
| 77 | if (exp_info) |
| 78 | OSMO_ASSERT(memcmp(exp_info, info_res, sizeof(*exp_info)) == 0); |
| 79 | |
Jacob Erlbeck | 626369c | 2015-06-15 11:04:25 +0200 | [diff] [blame] | 80 | msgb_free(llc_msg); |
| 81 | } |
| 82 | |
Jacob Erlbeck | b671dbf | 2015-06-15 14:32:33 +0200 | [diff] [blame] | 83 | static void enqueue_data(gprs_llc_queue *queue, const char *message, |
| 84 | gprs_llc_queue::MetaInfo *info = 0) |
Jacob Erlbeck | 626369c | 2015-06-15 11:04:25 +0200 | [diff] [blame] | 85 | { |
Jacob Erlbeck | b671dbf | 2015-06-15 14:32:33 +0200 | [diff] [blame] | 86 | enqueue_data(queue, (uint8_t *)(message), strlen(message), info); |
Jacob Erlbeck | 626369c | 2015-06-15 11:04:25 +0200 | [diff] [blame] | 87 | } |
| 88 | |
Jacob Erlbeck | b671dbf | 2015-06-15 14:32:33 +0200 | [diff] [blame] | 89 | static void dequeue_and_check(gprs_llc_queue *queue, const char *exp_message, |
| 90 | const gprs_llc_queue::MetaInfo *exp_info = 0) |
Jacob Erlbeck | 626369c | 2015-06-15 11:04:25 +0200 | [diff] [blame] | 91 | { |
Jacob Erlbeck | b671dbf | 2015-06-15 14:32:33 +0200 | [diff] [blame] | 92 | dequeue_and_check(queue, |
| 93 | (uint8_t *)(exp_message), strlen(exp_message), exp_info); |
Jacob Erlbeck | 626369c | 2015-06-15 11:04:25 +0200 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | static void test_llc_queue() |
| 97 | { |
| 98 | gprs_llc_queue queue; |
| 99 | |
| 100 | printf("=== start %s ===\n", __func__); |
| 101 | |
| 102 | queue.init(); |
| 103 | OSMO_ASSERT(queue.size() == 0); |
Jacob Erlbeck | 07eb655 | 2015-06-15 11:05:44 +0200 | [diff] [blame] | 104 | OSMO_ASSERT(queue.octets() == 0); |
Jacob Erlbeck | 626369c | 2015-06-15 11:04:25 +0200 | [diff] [blame] | 105 | |
| 106 | enqueue_data(&queue, "LLC message"); |
| 107 | OSMO_ASSERT(queue.size() == 1); |
Jacob Erlbeck | 07eb655 | 2015-06-15 11:05:44 +0200 | [diff] [blame] | 108 | OSMO_ASSERT(queue.octets() == 11); |
Jacob Erlbeck | 626369c | 2015-06-15 11:04:25 +0200 | [diff] [blame] | 109 | |
| 110 | enqueue_data(&queue, "other LLC message"); |
| 111 | OSMO_ASSERT(queue.size() == 2); |
Jacob Erlbeck | 07eb655 | 2015-06-15 11:05:44 +0200 | [diff] [blame] | 112 | OSMO_ASSERT(queue.octets() == 28); |
Jacob Erlbeck | 626369c | 2015-06-15 11:04:25 +0200 | [diff] [blame] | 113 | |
| 114 | dequeue_and_check(&queue, "LLC message"); |
| 115 | OSMO_ASSERT(queue.size() == 1); |
Jacob Erlbeck | 07eb655 | 2015-06-15 11:05:44 +0200 | [diff] [blame] | 116 | OSMO_ASSERT(queue.octets() == 17); |
Jacob Erlbeck | 626369c | 2015-06-15 11:04:25 +0200 | [diff] [blame] | 117 | |
| 118 | dequeue_and_check(&queue, "other LLC message"); |
| 119 | OSMO_ASSERT(queue.size() == 0); |
Jacob Erlbeck | 07eb655 | 2015-06-15 11:05:44 +0200 | [diff] [blame] | 120 | OSMO_ASSERT(queue.octets() == 0); |
Jacob Erlbeck | 626369c | 2015-06-15 11:04:25 +0200 | [diff] [blame] | 121 | |
| 122 | enqueue_data(&queue, "LLC"); |
| 123 | OSMO_ASSERT(queue.size() == 1); |
Jacob Erlbeck | 07eb655 | 2015-06-15 11:05:44 +0200 | [diff] [blame] | 124 | OSMO_ASSERT(queue.octets() == 3); |
Jacob Erlbeck | 626369c | 2015-06-15 11:04:25 +0200 | [diff] [blame] | 125 | |
| 126 | queue.clear(NULL); |
| 127 | OSMO_ASSERT(queue.size() == 0); |
Jacob Erlbeck | 07eb655 | 2015-06-15 11:05:44 +0200 | [diff] [blame] | 128 | OSMO_ASSERT(queue.octets() == 0); |
Jacob Erlbeck | 626369c | 2015-06-15 11:04:25 +0200 | [diff] [blame] | 129 | |
| 130 | printf("=== end %s ===\n", __func__); |
| 131 | } |
| 132 | |
Jacob Erlbeck | b671dbf | 2015-06-15 14:32:33 +0200 | [diff] [blame] | 133 | static void test_llc_meta() |
| 134 | { |
| 135 | gprs_llc_queue queue; |
Jacob Erlbeck | 34cf156 | 2015-06-29 10:21:52 +0200 | [diff] [blame] | 136 | gprs_llc_queue::MetaInfo info1; |
| 137 | gprs_llc_queue::MetaInfo info2; |
| 138 | |
| 139 | info1.recv_time.tv_sec = 123456777; |
| 140 | info1.recv_time.tv_usec = 123456; |
| 141 | info1.expire_time.tv_sec = 123456789; |
| 142 | info1.expire_time.tv_usec = 987654; |
| 143 | |
| 144 | info2.recv_time.tv_sec = 987654321; |
| 145 | info2.recv_time.tv_usec = 547352; |
| 146 | info2.expire_time.tv_sec = 987654327; |
| 147 | info2.expire_time.tv_usec = 867252; |
Jacob Erlbeck | b671dbf | 2015-06-15 14:32:33 +0200 | [diff] [blame] | 148 | |
| 149 | printf("=== start %s ===\n", __func__); |
| 150 | |
| 151 | queue.init(); |
| 152 | OSMO_ASSERT(queue.size() == 0); |
| 153 | OSMO_ASSERT(queue.octets() == 0); |
| 154 | |
| 155 | enqueue_data(&queue, "LLC message 1", &info1); |
| 156 | enqueue_data(&queue, "LLC message 2", &info2); |
| 157 | |
| 158 | dequeue_and_check(&queue, "LLC message 1", &info1); |
| 159 | dequeue_and_check(&queue, "LLC message 2", &info2); |
| 160 | |
| 161 | queue.clear(NULL); |
| 162 | OSMO_ASSERT(queue.size() == 0); |
| 163 | OSMO_ASSERT(queue.octets() == 0); |
| 164 | |
| 165 | printf("=== end %s ===\n", __func__); |
| 166 | } |
| 167 | |
Jacob Erlbeck | 626369c | 2015-06-15 11:04:25 +0200 | [diff] [blame] | 168 | static const struct log_info_cat default_categories[] = { |
| 169 | {"DPCU", "", "GPRS Packet Control Unit (PCU)", LOGL_INFO, 1}, |
| 170 | }; |
| 171 | |
| 172 | static int filter_fn(const struct log_context *ctx, |
| 173 | struct log_target *tar) |
| 174 | { |
| 175 | return 1; |
| 176 | } |
| 177 | |
| 178 | const struct log_info debug_log_info = { |
| 179 | filter_fn, |
| 180 | (struct log_info_cat*)default_categories, |
| 181 | ARRAY_SIZE(default_categories), |
| 182 | }; |
| 183 | |
| 184 | int main(int argc, char **argv) |
| 185 | { |
| 186 | struct vty_app_info pcu_vty_info = {0}; |
| 187 | |
| 188 | tall_pcu_ctx = talloc_named_const(NULL, 1, "LlcTest context"); |
| 189 | if (!tall_pcu_ctx) |
| 190 | abort(); |
| 191 | |
| 192 | msgb_set_talloc_ctx(tall_pcu_ctx); |
| 193 | osmo_init_logging(&debug_log_info); |
| 194 | log_set_use_color(osmo_stderr_target, 0); |
| 195 | log_set_print_filename(osmo_stderr_target, 0); |
| 196 | log_set_log_level(osmo_stderr_target, LOGL_INFO); |
| 197 | |
| 198 | vty_init(&pcu_vty_info); |
| 199 | pcu_vty_init(&debug_log_info); |
| 200 | |
| 201 | test_llc_queue(); |
Jacob Erlbeck | b671dbf | 2015-06-15 14:32:33 +0200 | [diff] [blame] | 202 | test_llc_meta(); |
Jacob Erlbeck | 626369c | 2015-06-15 11:04:25 +0200 | [diff] [blame] | 203 | |
| 204 | if (getenv("TALLOC_REPORT_FULL")) |
| 205 | talloc_report_full(tall_pcu_ctx, stderr); |
| 206 | |
| 207 | return EXIT_SUCCESS; |
| 208 | } |
| 209 | |
| 210 | extern "C" { |
| 211 | void l1if_pdch_req() { abort(); } |
| 212 | void l1if_connect_pdch() { abort(); } |
| 213 | void l1if_close_pdch() { abort(); } |
| 214 | void l1if_open_pdch() { abort(); } |
| 215 | } |