From 73bbf8924588f5dc9088ac34ac3da2747b18f9dc Mon Sep 17 00:00:00 2001 From: Holger Hans Peter Freyther Date: Thu, 21 Oct 2010 14:46:57 +0200 Subject: nat: Implement rewriting, have a very basic test for that feature --- openbsc/tests/bsc-nat/bsc_data.c | 25 +++++++++++ openbsc/tests/bsc-nat/bsc_nat_test.c | 82 ++++++++++++++++++++++++++++++++++++ 2 files changed, 107 insertions(+) (limited to 'openbsc/tests') diff --git a/openbsc/tests/bsc-nat/bsc_data.c b/openbsc/tests/bsc-nat/bsc_data.c index f4dba0a3c..3cc22af7e 100644 --- a/openbsc/tests/bsc-nat/bsc_data.c +++ b/openbsc/tests/bsc-nat/bsc_data.c @@ -160,3 +160,28 @@ static const struct mgcp_patch_test mgcp_messages[] = { .port = 5555, }, }; + +/* CC Setup messages */ +static const uint8_t cc_setup_national[] = { + 0x00, 0x20, 0xfd, 0x06, 0x01, 0x12, + 0x6d, 0x00, 0x01, 0x19, 0x01, 0x00, 0x16, 0x03, + 0x05, 0x04, 0x06, 0x60, 0x04, 0x02, 0x00, 0x05, + 0x81, 0x5e, 0x06, 0x81, 0x10, 0x27, 0x33, 0x63, + 0x66, 0x15, 0x02, 0x11, 0x01 +}; + +static const uint8_t cc_setup_national_patched[] = { + 0x00, 0x22, 0xfd, 0x06, 0x01, 0x12, + 0x6d, 0x00, 0x01, 0x1b, 0x01, 0x00, 0x18, 0x03, + 0x05, 0x04, 0x06, 0x60, 0x04, 0x02, 0x00, 0x05, + 0x81, 0x5e, 0x08, 0x81, 0x00, 0x94, 0x71, 0x32, + 0x33, 0x66, 0xf6, 0x15, 0x02, 0x11, 0x01 +}; + +static const uint8_t cc_setup_international[] = { + 0x00, 0x22, 0xfd, 0x06, 0x01, 0x13, + 0xe7, 0x00, 0x01, 0x1b, 0x01, 0x00, 0x18, 0x03, + 0x45, 0x04, 0x06, 0x60, 0x04, 0x02, 0x00, 0x05, + 0x81, 0x5e, 0x08, 0x81, 0x00, 0x94, 0x71, 0x33, + 0x63, 0x66, 0x03, 0x15, 0x02, 0x11, 0x01 +}; diff --git a/openbsc/tests/bsc-nat/bsc_nat_test.c b/openbsc/tests/bsc-nat/bsc_nat_test.c index 141775c7a..7a2557fe6 100644 --- a/openbsc/tests/bsc-nat/bsc_nat_test.c +++ b/openbsc/tests/bsc-nat/bsc_nat_test.c @@ -799,6 +799,87 @@ static void test_dt_filter() } } +static void test_setup_rewrite() +{ + struct msgb *msg = msgb_alloc(4096, "test_dt_filter"); + struct msgb *out; + struct bsc_nat_parsed *parsed; + const char *imsi = "27408000001234"; + + struct bsc_nat *nat = bsc_nat_alloc(); + + /* a fake list */ + struct msg_entries entries; + struct msg_entry entry; + + INIT_LLIST_HEAD(&entries.entry); + entry.mcc = "274"; + entry.mnc = "08"; + entry.option = "^0([1-9])"; + entry.text = "0049"; + llist_add_tail(&entry.list, &entries.entry); + nat->num_rewr = &entries; + + /* verify that nothing changed */ + msgb_reset(msg); + copy_to_msg(msg, cc_setup_international, ARRAY_SIZE(cc_setup_international)); + parsed = bsc_nat_parse(msg); + if (!parsed) { + fprintf(stderr, "FAIL: Could not parse ID resp\n"); + abort(); + } + + out = bsc_nat_rewrite_setup(nat, msg, parsed, imsi); + if (msg != out) { + fprintf(stderr, "FAIL: The message should not have been changed\n"); + abort(); + } + + if (out->len != ARRAY_SIZE(cc_setup_international)) { + fprintf(stderr, "FAIL: Length of message changed\n"); + abort(); + } + + if (memcmp(out->data, cc_setup_international, out->len) != 0) { + fprintf(stderr, "FAIL: Content modified..\n"); + abort(); + } + talloc_free(parsed); + + /* verify that something in the message changes */ + msgb_reset(msg); + copy_to_msg(msg, cc_setup_national, ARRAY_SIZE(cc_setup_national)); + parsed = bsc_nat_parse(msg); + if (!parsed) { + fprintf(stderr, "FAIL: Could not parse ID resp\n"); + abort(); + } + + out = bsc_nat_rewrite_setup(nat, msg, parsed, imsi); + if (!out) { + fprintf(stderr, "FAIL: A new message should be created.\n"); + abort(); + } + + if (msg == out) { + fprintf(stderr, "FAIL: The message should have changed\n"); + abort(); + } + + if (out->len != ARRAY_SIZE(cc_setup_national_patched)) { + fprintf(stderr, "FAIL: Length is wrong.\n"); + abort(); + } + + if (memcmp(cc_setup_national_patched, out->data, out->len) != 0) { + fprintf(stderr, "FAIL: Data is wrong.\n"); + fprintf(stderr, "Data was: %s\n", hexdump(out->data, out->len)); + abort(); + } + + msgb_free(out); +} + int main(int argc, char **argv) { struct log_target *stderr_target; @@ -818,6 +899,7 @@ int main(int argc, char **argv) test_mgcp_parse(); test_cr_filter(); test_dt_filter(); + test_setup_rewrite(); return 0; } -- cgit v1.2.3