diff options
Diffstat (limited to 'openbsc/tests')
-rw-r--r-- | openbsc/tests/Makefile.am | 2 | ||||
-rw-r--r-- | openbsc/tests/bsc-nat-trie/Makefile.am | 18 | ||||
-rw-r--r-- | openbsc/tests/bsc-nat-trie/bsc_nat_trie_test.c | 87 | ||||
-rw-r--r-- | openbsc/tests/bsc-nat-trie/bsc_nat_trie_test.ok | 20 | ||||
-rw-r--r-- | openbsc/tests/bsc-nat-trie/prefixes.csv | 25 | ||||
-rw-r--r-- | openbsc/tests/bsc-nat/Makefile.am | 3 | ||||
-rw-r--r-- | openbsc/tests/bsc-nat/bsc_nat_test.c | 117 | ||||
-rw-r--r-- | openbsc/tests/bsc-nat/prefixes.csv | 2 | ||||
-rw-r--r-- | openbsc/tests/testsuite.at | 9 | ||||
-rw-r--r-- | openbsc/tests/vty_test_runner.py | 37 |
10 files changed, 316 insertions, 4 deletions
diff --git a/openbsc/tests/Makefile.am b/openbsc/tests/Makefile.am index c9afeedd4..0597c14d0 100644 --- a/openbsc/tests/Makefile.am +++ b/openbsc/tests/Makefile.am @@ -1,7 +1,7 @@ SUBDIRS = gsm0408 db channel mgcp gprs si abis if BUILD_NAT -SUBDIRS += bsc-nat +SUBDIRS += bsc-nat bsc-nat-trie endif if BUILD_SMPP diff --git a/openbsc/tests/bsc-nat-trie/Makefile.am b/openbsc/tests/bsc-nat-trie/Makefile.am new file mode 100644 index 000000000..355ed6441 --- /dev/null +++ b/openbsc/tests/bsc-nat-trie/Makefile.am @@ -0,0 +1,18 @@ +AM_CPPFLAGS = $(all_includes) -I$(top_srcdir)/include +AM_CFLAGS=-Wall -ggdb3 $(LIBOSMOCORE_CFLAGS) $(LIBOSMOGSM_CFLAGS) $(LIBOSMOSCCP_CFLAGS) $(LIBOSMOABIS_CFLAGS) $(COVERAGE_CFLAGS) +AM_LDFLAGS = $(COVERAGE_LDFLAGS) + +EXTRA_DIST = bsc_nat_trie_test.ok prefixes.csv + +noinst_PROGRAMS = bsc_nat_trie_test + +bsc_nat_trie_test_SOURCES = bsc_nat_trie_test.c \ + $(top_srcdir)/src/osmo-bsc_nat/bsc_nat_rewrite_trie.c +bsc_nat_trie_test_LDADD = $(top_builddir)/src/libbsc/libbsc.a \ + $(top_srcdir)/src/libctrl/libctrl.a \ + $(top_srcdir)/src/libmgcp/libmgcp.a \ + $(top_srcdir)/src/libtrau/libtrau.a \ + $(top_srcdir)/src/libcommon/libcommon.a \ + $(LIBOSMOCORE_LIBS) $(LIBOSMOGSM_LIBS) -lrt \ + $(LIBOSMOSCCP_LIBS) $(LIBOSMOVTY_LIBS) \ + $(LIBOSMOABIS_LIBS) diff --git a/openbsc/tests/bsc-nat-trie/bsc_nat_trie_test.c b/openbsc/tests/bsc-nat-trie/bsc_nat_trie_test.c new file mode 100644 index 000000000..4b4df2faf --- /dev/null +++ b/openbsc/tests/bsc-nat-trie/bsc_nat_trie_test.c @@ -0,0 +1,87 @@ +/* + * (C) 2013 by On-Waves + * (C) 2013 by Holger Hans Peter Freyther <zecke@selfish.org> + * All Rights Reserved + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ + +#include <openbsc/nat_rewrite_trie.h> +#include <openbsc/debug.h> + +#include <osmocom/core/application.h> +#include <osmocom/core/backtrace.h> +#include <osmocom/core/talloc.h> +#include <osmocom/core/utils.h> + +#include <string.h> + +int main(int argc, char **argv) +{ + struct nat_rewrite *trie; + + osmo_init_logging(&log_info); + + printf("Testing the trie\n"); + + trie = nat_rewrite_parse(NULL, "prefixes.csv"); + OSMO_ASSERT(trie); + + /* verify that it has been parsed */ + OSMO_ASSERT(trie->prefixes == 17); + printf("Dumping the internal trie\n"); + nat_rewrite_dump(trie); + + /* now do the matching... */ + OSMO_ASSERT(!nat_rewrite_lookup(trie, "")); + OSMO_ASSERT(!nat_rewrite_lookup(trie, "2")); + + OSMO_ASSERT(strcmp(nat_rewrite_lookup(trie, "1")->rewrite, "1") == 0); + OSMO_ASSERT(strcmp(nat_rewrite_lookup(trie, "12")->rewrite, "2") == 0); + OSMO_ASSERT(strcmp(nat_rewrite_lookup(trie, "123")->rewrite, "3") == 0); + OSMO_ASSERT(strcmp(nat_rewrite_lookup(trie, "1234")->rewrite, "4") == 0); + OSMO_ASSERT(strcmp(nat_rewrite_lookup(trie, "12345")->rewrite, "5") == 0); + OSMO_ASSERT(strcmp(nat_rewrite_lookup(trie, "123456")->rewrite, "6") == 0); + OSMO_ASSERT(strcmp(nat_rewrite_lookup(trie, "1234567")->rewrite, "7") == 0); + OSMO_ASSERT(strcmp(nat_rewrite_lookup(trie, "12345678")->rewrite, "8") == 0); + OSMO_ASSERT(strcmp(nat_rewrite_lookup(trie, "123456789")->rewrite, "9") == 0); + OSMO_ASSERT(strcmp(nat_rewrite_lookup(trie, "1234567890")->rewrite, "10") == 0); + OSMO_ASSERT(strcmp(nat_rewrite_lookup(trie, "13")->rewrite, "11") == 0); + OSMO_ASSERT(strcmp(nat_rewrite_lookup(trie, "14")->rewrite, "12") == 0); + OSMO_ASSERT(strcmp(nat_rewrite_lookup(trie, "15")->rewrite, "13") == 0); + OSMO_ASSERT(strcmp(nat_rewrite_lookup(trie, "16")->rewrite, "14") == 0); + OSMO_ASSERT(strcmp(nat_rewrite_lookup(trie, "823455")->rewrite, "15") == 0); + OSMO_ASSERT(strcmp(nat_rewrite_lookup(trie, "82")->rewrite, "16") == 0); + OSMO_ASSERT(strcmp(nat_rewrite_lookup(trie, "+49123445")->rewrite, "17") == 0); + + /* match a prefix */ + OSMO_ASSERT(strcmp(nat_rewrite_lookup(trie, "121")->rewrite, "2") == 0); + OSMO_ASSERT(strcmp(nat_rewrite_lookup(trie, "1292323")->rewrite, "2") == 0); + OSMO_ASSERT(strcmp(nat_rewrite_lookup(trie, "12345678901")->rewrite, "10") == 0); + OSMO_ASSERT(strcmp(nat_rewrite_lookup(trie, "160")->rewrite, "14") == 0); + + OSMO_ASSERT(strcmp(nat_rewrite_lookup(trie, "12345678901123452123123")->rewrite, "10") == 0); + + /* invalid input */ + OSMO_ASSERT(!nat_rewrite_lookup(trie, "12abc")); + + talloc_free(trie); + + trie = nat_rewrite_parse(NULL, "does_not_exist.csv"); + OSMO_ASSERT(!trie); + + printf("Done with the tests.\n"); + return 0; +} diff --git a/openbsc/tests/bsc-nat-trie/bsc_nat_trie_test.ok b/openbsc/tests/bsc-nat-trie/bsc_nat_trie_test.ok new file mode 100644 index 000000000..4d4cc9949 --- /dev/null +++ b/openbsc/tests/bsc-nat-trie/bsc_nat_trie_test.ok @@ -0,0 +1,20 @@ +Testing the trie +Dumping the internal trie +1,1 +12,2 +123,3 +1234,4 +12345,5 +123456,6 +1234567,7 +12345678,8 +123456789,9 +1234567890,10 +13,11 +14,12 +15,13 +16,14 +82,16 +823455,15 ++49123,17 +Done with the tests. diff --git a/openbsc/tests/bsc-nat-trie/prefixes.csv b/openbsc/tests/bsc-nat-trie/prefixes.csv new file mode 100644 index 000000000..35485b1a3 --- /dev/null +++ b/openbsc/tests/bsc-nat-trie/prefixes.csv @@ -0,0 +1,25 @@ +1,1 +12,2 +123,3 +1234,4 +12345,5 +123456,6 +1234567,7 +12345678,8 +123456789,9 +1234567890,10 +13,11 +14,12 +15,13 +16,14 +823455,15 +82,16 ++49123,17 +1ABC,18 +12345678901234567890,19 +,20 +14A,21 +124,324324324234 +1234567890,10 +no line +99, diff --git a/openbsc/tests/bsc-nat/Makefile.am b/openbsc/tests/bsc-nat/Makefile.am index 084767829..1078d9bda 100644 --- a/openbsc/tests/bsc-nat/Makefile.am +++ b/openbsc/tests/bsc-nat/Makefile.am @@ -2,7 +2,7 @@ AM_CPPFLAGS = $(all_includes) -I$(top_srcdir)/include AM_CFLAGS=-Wall -ggdb3 $(LIBOSMOCORE_CFLAGS) $(LIBOSMOGSM_CFLAGS) $(LIBOSMOSCCP_CFLAGS) $(LIBOSMOABIS_CFLAGS) $(COVERAGE_CFLAGS) AM_LDFLAGS = $(COVERAGE_LDFLAGS) -EXTRA_DIST = bsc_nat_test.ok bsc_data.c barr.cfg barr_dup.cfg +EXTRA_DIST = bsc_nat_test.ok bsc_data.c barr.cfg barr_dup.cfg prefixes.csv noinst_PROGRAMS = bsc_nat_test @@ -12,6 +12,7 @@ bsc_nat_test_SOURCES = bsc_nat_test.c \ $(top_srcdir)/src/osmo-bsc_nat/bsc_nat_utils.c \ $(top_srcdir)/src/osmo-bsc_nat/bsc_nat_filter.c \ $(top_srcdir)/src/osmo-bsc_nat/bsc_nat_rewrite.c \ + $(top_srcdir)/src/osmo-bsc_nat/bsc_nat_rewrite_trie.c \ $(top_srcdir)/src/osmo-bsc_nat/bsc_mgcp_utils.c bsc_nat_test_LDADD = $(top_builddir)/src/libbsc/libbsc.a \ $(top_srcdir)/src/libctrl/libctrl.a \ diff --git a/openbsc/tests/bsc-nat/bsc_nat_test.c b/openbsc/tests/bsc-nat/bsc_nat_test.c index 4a244dba3..5158f46cf 100644 --- a/openbsc/tests/bsc-nat/bsc_nat_test.c +++ b/openbsc/tests/bsc-nat/bsc_nat_test.c @@ -26,6 +26,7 @@ #include <openbsc/gsm_data.h> #include <openbsc/bsc_nat.h> #include <openbsc/bsc_nat_sccp.h> +#include <openbsc/nat_rewrite_trie.h> #include <osmocom/core/application.h> #include <osmocom/core/backtrace.h> @@ -451,6 +452,8 @@ static void test_paging(void) printf("Should have found it.\n"); abort(); } + + talloc_free(nat); } static void test_mgcp_allocations(void) @@ -819,6 +822,7 @@ static void test_cr_filter() } msgb_free(msg); + talloc_free(nat); } static void test_dt_filter() @@ -1039,6 +1043,117 @@ static void test_setup_rewrite() verify_msg(out, cc_setup_national_again, ARRAY_SIZE(cc_setup_national_again)); msgb_free(out); + bsc_nat_num_rewr_entry_adapt(nat, &nat->num_rewr, NULL); + talloc_free(nat); +} + +static void test_setup_rewrite_prefix(void) +{ + 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 osmo_config_list entries; + struct osmo_config_entry entry; + + INIT_LLIST_HEAD(&entries.entry); + entry.mcc = "274"; + entry.mnc = "08"; + entry.option = "^0([1-9])"; + entry.text = "prefix_lookup"; + llist_add_tail(&entry.list, &entries.entry); + bsc_nat_num_rewr_entry_adapt(nat, &nat->num_rewr, &entries); + + nat->num_rewr_trie = nat_rewrite_parse(nat, "prefixes.csv"); + + msgb_reset(msg); + copy_to_msg(msg, cc_setup_national, ARRAY_SIZE(cc_setup_national)); + parsed = bsc_nat_parse(msg); + if (!parsed) { + printf("FAIL: Could not parse ID resp\n"); + abort(); + } + + out = bsc_nat_rewrite_msg(nat, msg, parsed, imsi); + if (!out) { + printf("FAIL: A new message should be created.\n"); + abort(); + } + + if (msg == out) { + printf("FAIL: The message should have changed\n"); + abort(); + } + + verify_msg(out, cc_setup_national_patched, ARRAY_SIZE(cc_setup_national_patched)); + msgb_free(out); + + bsc_nat_num_rewr_entry_adapt(nat, &nat->num_rewr, NULL); + talloc_free(nat); +} + +static void test_setup_rewrite_post(void) +{ + 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 osmo_config_list entries; + struct osmo_config_entry entry; + struct osmo_config_list entries_post; + struct osmo_config_entry entry_post; + + 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); + bsc_nat_num_rewr_entry_adapt(nat, &nat->num_rewr, &entries); + + /* attempt to undo the previous one */ + INIT_LLIST_HEAD(&entries_post.entry); + entry_post.mcc = "274"; + entry_post.mnc = "08"; + entry_post.option = "^\\+49([1-9])"; + entry_post.text = "prefix_lookup"; + llist_add_tail(&entry_post.list, &entries_post.entry); + bsc_nat_num_rewr_entry_adapt(nat, &nat->num_rewr_post, &entries_post); + + nat->num_rewr_trie = nat_rewrite_parse(nat, "prefixes.csv"); + + msgb_reset(msg); + copy_to_msg(msg, cc_setup_national, ARRAY_SIZE(cc_setup_national)); + parsed = bsc_nat_parse(msg); + if (!parsed) { + printf("FAIL: Could not parse ID resp\n"); + abort(); + } + + out = bsc_nat_rewrite_msg(nat, msg, parsed, imsi); + if (!out) { + printf("FAIL: A new message should be created.\n"); + abort(); + } + + if (msg == out) { + printf("FAIL: The message should have changed\n"); + abort(); + } + + verify_msg(out, cc_setup_national, ARRAY_SIZE(cc_setup_national)); + msgb_free(out); + + bsc_nat_num_rewr_entry_adapt(nat, &nat->num_rewr, NULL); + talloc_free(nat); } static void test_sms_smsc_rewrite() @@ -1322,6 +1437,8 @@ int main(int argc, char **argv) test_cr_filter(); test_dt_filter(); test_setup_rewrite(); + test_setup_rewrite_prefix(); + test_setup_rewrite_post(); test_sms_smsc_rewrite(); test_sms_number_rewrite(); test_mgcp_allocations(); diff --git a/openbsc/tests/bsc-nat/prefixes.csv b/openbsc/tests/bsc-nat/prefixes.csv new file mode 100644 index 000000000..0c7660f10 --- /dev/null +++ b/openbsc/tests/bsc-nat/prefixes.csv @@ -0,0 +1,2 @@ +0172,0049 ++49,0 diff --git a/openbsc/tests/testsuite.at b/openbsc/tests/testsuite.at index ea3fa1ce5..e54d4b57a 100644 --- a/openbsc/tests/testsuite.at +++ b/openbsc/tests/testsuite.at @@ -34,6 +34,7 @@ AT_CLEANUP AT_SETUP([bsc-nat]) AT_KEYWORDS([bsc-nat]) AT_CHECK([test "$enable_nat_test" != no || exit 77]) +cp $abs_srcdir/bsc-nat/prefixes.csv . cp $abs_srcdir/bsc-nat/barr.cfg . cp $abs_srcdir/bsc-nat/barr_dup.cfg . cat $abs_srcdir/bsc-nat/bsc_nat_test.ok > expout @@ -48,6 +49,14 @@ cat $abs_srcdir/smpp/smpp_test.err > experr AT_CHECK([$abs_top_builddir/tests/smpp/smpp_test], [], [expout], [experr]) AT_CLEANUP +AT_SETUP([bsc-nat-trie]) +AT_KEYWORDS([bsc-nat-trie]) +AT_CHECK([test "$enable_nat_test" != no || exit 77]) +cp $abs_srcdir/bsc-nat-trie/prefixes.csv . +cat $abs_srcdir/bsc-nat-trie/bsc_nat_trie_test.ok > expout +AT_CHECK([$abs_top_builddir/tests/bsc-nat-trie/bsc_nat_trie_test], [], [expout], [ignore]) +AT_CLEANUP + AT_SETUP([si]) AT_KEYWORDS([si]) cat $abs_srcdir/si/si_test.ok > expout diff --git a/openbsc/tests/vty_test_runner.py b/openbsc/tests/vty_test_runner.py index 778cde04c..730b8ba67 100644 --- a/openbsc/tests/vty_test_runner.py +++ b/openbsc/tests/vty_test_runner.py @@ -96,8 +96,41 @@ class TestVTYNAT(TestVTYBase): def vty_app(self): return (4244, "src/osmo-bsc_nat/osmo-bsc_nat", "OsmoBSCNAT", "nat") - def testMoo(self): - pass + def testRewriteNoRewrite(self): + self.vty.enable() + res = self.vty.command("configure terminal") + res = self.vty.command("nat") + res = self.vty.command("number-rewrite rewrite.cfg") + res = self.vty.command("no number-rewrite") + + def testRewritePostNoRewrite(self): + self.vty.enable() + self.vty.command("configure terminal") + self.vty.command("nat") + self.vty.verify("number-rewrite-post rewrite.cfg", ['']) + self.vty.verify("no number-rewrite-post", ['']) + + + def testPrefixTreeLoading(self): + cfg = os.path.join(confpath, "tests/bsc-nat-trie/prefixes.csv") + + self.vty.enable() + self.vty.command("configure terminal") + self.vty.command("nat") + res = self.vty.command("prefix-tree %s" % cfg) + self.assertEqual(res, "% prefix-tree loaded 17 rules.") + self.vty.command("end") + + res = self.vty.command("show prefix-tree") + self.assertEqual(res, '1,1\r\n12,2\r\n123,3\r\n1234,4\r\n12345,5\r\n123456,6\r\n1234567,7\r\n12345678,8\r\n123456789,9\r\n1234567890,10\r\n13,11\r\n14,12\r\n15,13\r\n16,14\r\n82,16\r\n823455,15\r\n+49123,17') + + self.vty.command("configure terminal") + self.vty.command("nat") + self.vty.command("no prefix-tree") + self.vty.command("end") + + res = self.vty.command("show prefix-tree") + self.assertEqual(res, "% there is now prefix tree loaded.") def add_nat_test(suite, workdir): |