blob: 9532846f44f32ebc37f1597e32ce9ac4a820401c [file] [log] [blame]
Neels Hofmeyrb4552052019-10-21 03:00:26 +02001#include <stdio.h>
2#include <string.h>
3#include <errno.h>
4#include <osmocom/core/application.h>
5#include <osmocom/core/logging.h>
6#include <osmocom/msc/debug.h>
7#include <osmocom/msc/mncc.h>
8
9#define _test_sdp_termination(LABEL, MNCC, MNCC_MSG_LEN, RC) do { \
10 int sdp_len = ((int)(MNCC_MSG_LEN)) - ((MNCC)->sdp - (char*)MNCC); \
11 size_t sdp_strlen = strnlen(MNCC->sdp, sizeof(MNCC->sdp)); \
12 int rc = mncc_check_sdp_termination("<" LABEL ">", (struct gsm_mncc*)MNCC, MNCC_MSG_LEN, MNCC->sdp); \
13 printf("%s: len=%d sdplen=%d sdp=%s rc=%d\n", \
14 LABEL, (int)(MNCC_MSG_LEN), sdp_len, \
15 sdp_len > 0? osmo_quote_str((MNCC)->sdp, OSMO_MIN(sdp_len, sdp_strlen+1)) : "-", rc); \
16 if (RC != rc) \
17 printf("ERROR!\n"); \
18 } while (0)
19
20#define test_sdp_termination_cases(MNCC) \
21 _test_sdp_termination("empty SDP", MNCC, sizeof(*MNCC), 0); \
22 _test_sdp_termination("empty SDP, shortest possible", MNCC, MNCC->sdp - ((char*)MNCC) + 1, 0); \
23 _test_sdp_termination("empty SDP, zero len", MNCC, MNCC->sdp - ((char*)MNCC), -EINVAL); \
24 OSMO_STRLCPY_ARRAY(MNCC->sdp, "Privacy is a desirable marketing option"); \
25 _test_sdp_termination("terminated SDP str", MNCC, sizeof(*MNCC), 0); \
26 _test_sdp_termination("terminated SDP str, shortest possible", MNCC, \
27 MNCC->sdp - ((char*)MNCC) + strlen(MNCC->sdp) + 1, 0); \
28 _test_sdp_termination("terminated SDP str, but len excludes nul", MNCC, \
29 MNCC->sdp - ((char*)MNCC) + strlen(MNCC->sdp), -EINVAL); \
30 _test_sdp_termination("terminated SDP str, but len too short", MNCC, \
31 MNCC->sdp - ((char*)MNCC) + 23, -EINVAL); \
32 _test_sdp_termination("len way too short", MNCC, 10, -EINVAL); \
33 _test_sdp_termination("len zero", MNCC, 0, -EINVAL);
34
35
36void test_sdp_termination(void)
37{
38 struct gsm_mncc _mncc = {};
39 struct gsm_mncc_rtp _mncc_rtp = {};
40
41 struct gsm_mncc *mncc = &_mncc;
42 struct gsm_mncc_rtp *mncc_rtp = &_mncc_rtp;
43
44 printf("%s()\n", __func__);
45 printf("\nstruct gsm_mncc:\n");
46 test_sdp_termination_cases(mncc);
47
48 _mncc = (struct gsm_mncc){};
49 _mncc_rtp = (struct gsm_mncc_rtp){};
50 printf("\nstruct gsm_mncc_rtp:\n");
51 test_sdp_termination_cases(mncc_rtp);
52}
53
54static const struct log_info_cat default_categories[] = {
55 [DMNCC] = {
56 .name = "DMNCC",
57 .description = "MNCC API for Call Control application",
58 .color = "\033[1;39m",
59 .enabled = 1, .loglevel = LOGL_NOTICE,
60 },
61};
62
63const struct log_info log_info = {
64 .cat = default_categories,
65 .num_cat = ARRAY_SIZE(default_categories),
66};
67
68int main(void)
69{
Oliver Smith8d0aa262023-07-06 12:57:55 +020070 void *ctx = talloc_named_const(NULL, 0, "mncc_test");
Neels Hofmeyrb4552052019-10-21 03:00:26 +020071 osmo_init_logging2(ctx, &log_info);
72 log_set_use_color(osmo_stderr_target, 0);
Pau Espin Pedrolcad22fd2021-02-19 13:37:00 +010073 log_set_print_filename2(osmo_stderr_target, LOG_FILENAME_NONE);
Neels Hofmeyrb4552052019-10-21 03:00:26 +020074 log_set_print_category(osmo_stderr_target, 1);
75 log_set_print_category_hex(osmo_stderr_target, 0);
76
77 test_sdp_termination();
78 return 0;
79}