Max | 7918f84 | 2018-12-10 10:57:59 +0100 | [diff] [blame] | 1 | /* |
| 2 | * (C) 2018 by sysmocom - s.f.m.c. GmbH |
| 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 | * |
Max | 7918f84 | 2018-12-10 10:57:59 +0100 | [diff] [blame] | 15 | */ |
| 16 | |
| 17 | #include <osmocom/gsm/gsm29205.h> |
| 18 | #include <osmocom/core/msgb.h> |
| 19 | #include <osmocom/gsm/protocol/gsm_08_08.h> |
| 20 | #include <osmocom/gsm/gsm0808.h> |
| 21 | #include <osmocom/gsm/tlv.h> |
| 22 | #include <osmocom/core/logging.h> |
| 23 | #include <osmocom/core/application.h> |
| 24 | |
| 25 | #include <stdio.h> |
| 26 | #include <stdlib.h> |
| 27 | #include <stdbool.h> |
| 28 | #include <string.h> |
| 29 | #include <errno.h> |
| 30 | |
Harald Welte | e61d459 | 2022-11-03 11:05:58 +0100 | [diff] [blame] | 31 | static void test_gcr(void) |
Max | 7918f84 | 2018-12-10 10:57:59 +0100 | [diff] [blame] | 32 | { |
| 33 | static const uint8_t res[] = { |
| 34 | 0x03, /* .net_len */ |
| 35 | 0x51, 0x52, 0x53, /* .net */ |
| 36 | 0x02, /* .node length */ |
| 37 | 0xde, 0xad, /* .node */ |
| 38 | 0x05, /* length of Call. Ref. */ |
| 39 | 0x41, 0x42, 0x43, 0x44, 0x45 /* .cr - Call. Ref. */ |
| 40 | }; |
| 41 | uint8_t len; |
| 42 | struct msgb *msg; |
Vadim Yanitskiy | 14b87b2 | 2019-09-24 15:51:03 +0700 | [diff] [blame] | 43 | struct osmo_gcr_parsed p = { |
| 44 | .net_len = 0, |
| 45 | .net = { 0 }, |
| 46 | .node = 0x00, |
| 47 | .cr = { 0 }, |
| 48 | }; |
| 49 | struct osmo_gcr_parsed g = { |
Max | 7918f84 | 2018-12-10 10:57:59 +0100 | [diff] [blame] | 50 | .net_len = 3, |
| 51 | .net = { 0x51, 0x52, 0x53 }, |
| 52 | .node = 0xDEAD, |
| 53 | .cr = { 0x41, 0x42, 0x43, 0x44, 0x45 } |
| 54 | }; |
| 55 | int rc; |
| 56 | |
| 57 | msg = msgb_alloc_headroom(BSSMAP_MSG_SIZE, BSSMAP_MSG_HEADROOM, "global call reference"); |
| 58 | if (!msg) |
| 59 | return; |
| 60 | |
| 61 | len = osmo_enc_gcr(msg, &g); |
| 62 | printf("Testing Global Call Reference encoder...\n\t%d bytes added: %s\n", |
| 63 | len, len == ARRAY_SIZE(res) ? "OK" : "FAIL"); |
| 64 | |
| 65 | if (!msgb_eq_data_print(msg, res, ARRAY_SIZE(res))) |
| 66 | abort(); |
| 67 | |
| 68 | rc = osmo_dec_gcr(&p, msgb_data(msg), msgb_length(msg)); |
| 69 | if (rc < 0) { |
| 70 | printf("decoding failed: %s [%s]\n", strerror(-rc), msgb_hexdump(msg)); |
| 71 | abort(); |
| 72 | } |
| 73 | |
| 74 | if (p.net_len != g.net_len) { |
| 75 | printf("Network ID length parsed wrong: %u != %u\n", p.net_len, g.net_len); |
| 76 | abort(); |
| 77 | } |
| 78 | |
| 79 | if (p.node != g.node) { |
| 80 | printf("Node ID parsed wrong: 0x%X != 0x%X\n", p.node, g.node); |
| 81 | abort(); |
| 82 | } |
| 83 | |
| 84 | if (memcmp(p.net, g.net, g.net_len) != 0) { |
| 85 | printf("Network ID parsed wrong: %s\n", osmo_hexdump(p.net, p.net_len)); |
| 86 | abort(); |
| 87 | } |
| 88 | |
| 89 | if (memcmp(p.cr, g.cr, 5) != 0) { |
| 90 | printf("Call ref. ID parsed wrong: %s\n", osmo_hexdump(p.cr, 5)); |
| 91 | abort(); |
| 92 | } |
| 93 | |
| 94 | printf("\tdecoded %d bytes: %s\n", rc, rc == len ? "OK" : "FAIL"); |
| 95 | msgb_free(msg); |
| 96 | } |
| 97 | |
| 98 | int main(int argc, char **argv) |
| 99 | { |
| 100 | osmo_init_logging2(talloc_named_const(NULL, 0, "gsm29205 test"), NULL); |
| 101 | |
| 102 | printf("Testing 3GPP TS 29.205 routines...\n"); |
| 103 | |
| 104 | test_gcr(); |
| 105 | |
| 106 | printf("Done.\n"); |
| 107 | |
| 108 | return EXIT_SUCCESS; |
| 109 | } |