diff options
Diffstat (limited to 'epan')
-rw-r--r-- | epan/Makefile.am | 11 | ||||
-rw-r--r-- | epan/oids.c | 14 | ||||
-rw-r--r-- | epan/oids.h | 6 | ||||
-rw-r--r-- | epan/oids_test.c | 481 |
4 files changed, 503 insertions, 9 deletions
diff --git a/epan/Makefile.am b/epan/Makefile.am index 55bb3c78a2..fdad7df024 100644 --- a/epan/Makefile.am +++ b/epan/Makefile.am @@ -109,6 +109,7 @@ EXTRA_DIST = \ reassemble_test.c \ uat_load.l \ exntest.c \ + oids_test.c \ doxygen.cfg.in \ CMakeLists.txt @@ -155,7 +156,7 @@ libwireshark_la_DEPENDENCIES = \ ${top_builddir}/wsutil/libwsutil.la \ ${top_builddir}/wiretap/libwiretap.la -EXTRA_PROGRAMS = reassemble_test tvbtest +EXTRA_PROGRAMS = reassemble_test tvbtest oids_test reassemble_test_LDADD = \ libwireshark.la \ $(GLIB_LIBS) \ @@ -166,6 +167,12 @@ tvbtest_LDADD = \ $(GLIB_LIBS) \ -lz +oids_test_LDADD = \ + libwireshark.la \ + wmem/libwmem.la \ + $(GLIB_LIBS) \ + -lz + exntest: exntest.o except.o $(LINK) $^ $(GLIB_LIBS) @@ -189,7 +196,7 @@ dtd_grammar.h: dtd_grammar.c dtd_grammar.c : $(LEMON)/lemon$(EXEEXT) $(srcdir)/$(LEMON)/lempar.c $(srcdir)/dtd_grammar.lemon $(AM_V_LEMON)$(LEMON)/lemon$(EXEEXT) t=$(srcdir)/$(LEMON)/lempar.c $(srcdir)/dtd_grammar.lemon -tvbtest.o exntest.o: exceptions.h +tvbtest.o exntest.o oids_test.o: exceptions.h sminmpec.c: enterprise-numbers ../tools/make-sminmpec.pl $(PERL) $(srcdir)/../tools/make-sminmpec.pl $(srcdir)/enterprise-numbers sminmpec.c diff --git a/epan/oids.c b/epan/oids.c index bb89144077..848098235c 100644 --- a/epan/oids.c +++ b/epan/oids.c @@ -88,10 +88,7 @@ static const oid_value_type_t unknown_type = { FT_BYTES, BASE_NONE, BER_CLAS static oid_info_t oid_root = { 0, NULL, OID_KIND_UNKNOWN, NULL, &unknown_type, -2, NULL, NULL, NULL}; -static oid_info_t* add_oid(const char* name, oid_kind_t kind, const oid_value_type_t* type, oid_key_t* key, guint oid_len, guint32 *subids) { - guint i = 0; - oid_info_t* c = &oid_root; - +static void prepopulate_oids(void) { if (!oid_root.children) { char* debug_env = getenv("WIRESHARK_DEBUG_MIBS"); guint32 subid; @@ -108,7 +105,15 @@ static oid_info_t* add_oid(const char* name, oid_kind_t kind, const oid_value_ty subid = 1; oid_add("iso",1,&subid); subid = 2; oid_add("joint-iso-itu-t",1,&subid); } +} + + + +static oid_info_t* add_oid(const char* name, oid_kind_t kind, const oid_value_type_t* type, oid_key_t* key, guint oid_len, guint32 *subids) { + guint i = 0; + oid_info_t* c = &oid_root; + prepopulate_oids(); oid_len--; do { @@ -813,6 +818,7 @@ void oid_pref_init(module_t *nameres) } void oids_init(void) { + prepopulate_oids(); #ifdef HAVE_LIBSMI register_mibs(); #else diff --git a/epan/oids.h b/epan/oids.h index 3297202ce0..1c19bbf330 100644 --- a/epan/oids.h +++ b/epan/oids.h @@ -109,11 +109,11 @@ typedef struct _oid_info_t { struct _oid_info_t* parent; } oid_info_t; -/** init funcion called from epan.h */ +/** init function called from prefs.c */ WS_DLL_PUBLIC void oids_init(void); extern void oid_pref_init(module_t *nameres); -/** init funcion called from epan.h */ +/** init function called from epan.h */ WS_DLL_PUBLIC void oids_cleanup(void); /* @@ -150,7 +150,7 @@ guint oid_string2subid(const gchar *oid_str, guint32** subids_p); WS_DLL_PUBLIC const gchar* oid_encoded2string(const guint8* encoded, guint len); WS_DLL_PUBLIC const gchar* rel_oid_encoded2string(const guint8* encoded, guint len); WS_DLL_PUBLIC const gchar* oid_subid2string(guint32 *subids, guint len); -WS_DLL_PUBLIC const gchar* rel_oid_subid2string(guint32 *subids, guint len, gboolean is_relative); +WS_DLL_PUBLIC const gchar* rel_oid_subid2string(guint32 *subids, guint len, gboolean is_absolute); /* these return a formated string as human readable as posible */ WS_DLL_PUBLIC const gchar *oid_resolved(guint len, guint32 *subids); diff --git a/epan/oids_test.c b/epan/oids_test.c new file mode 100644 index 0000000000..6dd07c3cd2 --- /dev/null +++ b/epan/oids_test.c @@ -0,0 +1,481 @@ +/* oids_test.c + * ASN.1 Object Identifier handling tests + * Copyright 2013, Edward J. Beroset <beroset@ieee.org> + * + * $Id: oids_test.c$ + * + * Wireshark - Network traffic analyzer + * By Gerald Combs <gerald@wireshark.org> + * Copyright 1998 Gerald Combs + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include "config.h" + +#include <stdio.h> +#include <string.h> +#include <glib.h> + +#include "emem.h" +#include "oids.h" +#include "wmem/wmem.h" + +typedef struct +{ + const gchar *string; + const gchar *resolved; + guint encoded_len; + const gchar *encoded; + guint subids_len; + guint32 subids[]; +} example_s; + +example_s ex1 = {"2.1.1", "joint-iso-itu-t.1.1", 2, "\x51\x01", 3, {2,1,1} }; +example_s ex2rel = {".81.1", ".81.1", 2, "\x51\x01", 2, {81,1} }; +example_s ex3 = {"2.1.127.16383.2097151.268435455.128.16384.2097152.268435456", + "joint-iso-itu-t.1.127.16383.2097151.268435455.128.16384.2097152.268435456", + 25, "\x51\x7f\xff\x7f\xff\xff\x7f\xff\xff\xff\x7f\x81\x00\x81\x80\x00\x81\x80\x80\x00\x81\x80\x80\x80\x00", + 10, { 2, 1, 0x7F, 0x3FFF, 0x1FFFFF, 0x0FFFFFFF, 1+0x7F, 1+0x3FFF, 1+0x1FFFFF, 1+0x0FFFFFFF} }; + +example_s ex4 = {"2.1", "joint-iso-itu-t.1", 1, "\x51", 2, {2,1} }; +example_s ex5 = {"2", "joint-iso-itu-t", 0, NULL, 1, {2} }; +example_s ex6rel = {".81.127.16383.2097151.268435455.128.16384.2097152.268435456", + ".81.127.16383.2097151.268435455.128.16384.2097152.268435456", + 25, "\x51\x7f\xff\x7f\xff\xff\x7f\xff\xff\xff\x7f\x81\x00\x81\x80\x00\x81\x80\x80\x00\x81\x80\x80\x80\x00", + 9, { 81, 0x7F, 0x3FFF, 0x1FFFFF, 0x0FFFFFFF, 1+0x7F, 1+0x3FFF, 1+0x1FFFFF, 1+0x0FFFFFFF} }; + +/* + * These test are organized in order of the appearance, in oids.h, of + * the basic oids.c functions that they test. This makes it easier to + * get a quick understanding of both the testing and the organization + * of oids.h. + * + * Tests are named /oids/2<desttype>/<srctype>[<extra>] + * where <desttype> is the resulting type of the conversion, + * <srctype> is the source type and <extra> is any additional + * information to make the test name unique. + * + * The types, for the purpose of this naming convention, are + * encoded, subids, string and resolved, both, struct. + */ + +/* OIDS TESTING FUNCTIONS (/oids/2subids/) */ + +static void +oids_test_2subids_encoded(void) +{ + guint32 *subids = NULL; + guint len; + guint i; + + len = oid_encoded2subid(ex1.encoded, ex1.encoded_len, &subids); + g_assert_cmpint(len, ==, ex1.subids_len); + for (i=0; i < len; i++) + g_assert_cmpint(subids[i], ==, ex1.subids[i]); +} + +static void +oids_test_2subids_encoded_long(void) +{ + guint32 *subids = NULL; + guint len; + guint i; + + len = oid_encoded2subid(ex3.encoded, ex3.encoded_len, &subids); + g_assert_cmpint(len, ==, ex3.subids_len); + for (i=0; i < len; i++) + g_assert_cmpint(subids[i], ==, ex3.subids[i]); +} + +static void +oids_test_2subids_encoded_absviasub(void) +{ + guint32 *subids = NULL; + guint len; + guint i; + + len = oid_encoded2subid_sub(ex1.encoded, ex1.encoded_len, &subids, TRUE); + g_assert_cmpint(len, ==, ex1.subids_len); + for (i=0; i < len; i++) + g_assert_cmpint(subids[i], ==, ex1.subids[i]); +} + +static void +oids_test_2subids_encoded_relviasub(void) +{ + guint32 *subids = NULL; + guint len; + guint i; + + len = oid_encoded2subid_sub(ex2rel.encoded, ex2rel.encoded_len, &subids, FALSE); + g_assert_cmpint(len, ==, ex2rel.subids_len); + for (i=0; i < len; i++) + g_assert_cmpint(subids[i], ==, ex2rel.subids[i]); +} + +static void +oids_test_2subids_string(void) +{ + guint32 *subids = NULL; + guint len, i; + + len = oid_string2subid(ex1.string, &subids); + g_assert_cmpint(len, ==, ex1.subids_len); + for (i=0; i < len; i++) + g_assert_cmpint(subids[i], ==, ex1.subids[i]); +} + +static void +oids_test_2subids_string_tooshort(void) +{ + guint32 *subids = NULL; + guint len, i; + + len = oid_string2subid(ex5.string, &subids); + g_assert_cmpint(len, ==, ex5.subids_len); + for (i=0; i < len; i++) + g_assert_cmpint(subids[i], ==, ex5.subids[i]); +} + +/* OIDS TESTING FUNCTIONS (/oids/2encoded/) */ + +static void +oids_test_2encoded_string_simple(void) +{ + guint8 *encoded = NULL; + guint len; + + len = oid_string2encoded(ex1.string, &encoded); + g_assert_cmpint(len, ==, ex1.encoded_len); + g_assert(0 == memcmp(encoded, ex1.encoded, len)); +} + +static void +oids_test_2encoded_string_short(void) +{ + guint8 *encoded = NULL; + guint len; + + len = oid_string2encoded(ex4.string, &encoded); + g_assert_cmpint(len, ==, ex4.encoded_len); + g_assert(0 == memcmp(encoded, ex4.encoded, len)); +} + +static void +oids_test_2encoded_string_long(void) +{ + guint8 *encoded = NULL; + guint len; + + len = oid_string2encoded(ex3.string, &encoded); + g_assert_cmpint(len, ==, ex3.encoded_len); + g_assert(0 == memcmp(encoded, ex3.encoded, len)); +} + +static void +oids_test_2encoded_string_tooshort(void) +{ + guint8 *encoded = NULL; + guint len; + + len = oid_string2encoded(ex5.string, &encoded); + g_assert_cmpint(len, ==, ex5.encoded_len); + g_assert(0 == memcmp(encoded, ex5.encoded, len)); +} + +static void +oids_test_2encoded_subids_simple(void) +{ + guint8 *encoded = NULL; + guint len; + + len = oid_subid2encoded(ex1.subids_len, ex1.subids, &encoded); + g_assert_cmpint(len, ==, ex1.encoded_len); + g_assert(0 == memcmp(encoded, ex1.encoded, len)); +} + +static void +oids_test_2encoded_subids_bad(void) +{ + guint8 *encoded = NULL; + guint len; + + len = oid_subid2encoded(ex5.subids_len, ex5.subids, &encoded); + g_assert_cmpint(len, ==, ex5.encoded_len); + g_assert(0 == memcmp(encoded, ex5.encoded, len)); +} + +/* OIDS TESTING FUNCTIONS (/oids/2string/) */ + +static void +oids_test_2string_encoded(void) +{ + const gchar* oid; + + oid = oid_encoded2string(ex3.encoded, ex3.encoded_len); + g_assert_cmpstr(oid, ==,ex3.string); +} + +static void +oids_test_2string_encoded_rel(void) +{ + const gchar* oid; + + oid = rel_oid_encoded2string(ex6rel.encoded, ex3.encoded_len); + g_assert_cmpstr(oid, ==,ex6rel.string); +} + + +static void +oids_test_2string_subids_abs(void) +{ + const gchar* oid; + + oid = oid_subid2string(ex1.subids, ex1.subids_len); + g_assert_cmpstr(oid, ==, ex1.string); +} + +static void +oids_test_2string_subids_rel(void) +{ + const gchar* oid; + + oid = rel_oid_subid2string(ex2rel.subids, ex2rel.subids_len, FALSE); + g_assert_cmpstr(oid, ==, ex2rel.string); +} + +static void +oids_test_2string_subids_absviarel(void) +{ + const gchar* oid; + + oid = rel_oid_subid2string(ex1.subids, ex1.subids_len, TRUE); + g_assert_cmpstr(oid, ==, ex1.string); +} + +static void +oids_test_2string_subids_relsizes(void) +{ + const gchar* oid; + + oid = rel_oid_subid2string(ex6rel.subids, ex6rel.subids_len, FALSE); + g_assert_cmpstr(oid, ==,ex6rel.string); +} + +/* OIDS TESTING FUNCTIONS (/oids/2resolved/) */ + +static void +oids_test_2resolved_subids(void) +{ + const gchar* oid; + + oid = oid_resolved(ex1.subids_len, ex1.subids); + g_assert_cmpstr(oid, ==,ex1.resolved); +} + +static void +oids_test_2resolved_encoded(void) +{ + const gchar* oid; + + oid = oid_resolved_from_encoded(ex1.encoded, ex1.encoded_len); + g_assert_cmpstr(oid, ==,ex1.resolved); +} + +static void +oids_test_2resolved_encoded_rel(void) +{ + const gchar* oid; + + oid = rel_oid_resolved_from_encoded(ex2rel.encoded, ex2rel.encoded_len); + g_assert_cmpstr(oid, ==,ex2rel.string); +} + +static void +oids_test_2resolved_string(void) +{ + const gchar* oid; + + oid = oid_resolved_from_string(ex1.string); + g_assert_cmpstr(oid, ==,ex1.resolved); +} + +/* OIDS TESTING FUNCTIONS (/oids/2both/) */ + +static void +oids_test_2both_subids(void) +{ + gchar* resolved; + gchar* oid; + + oid_both(ex1.subids_len, ex1.subids, &resolved, &oid); + g_assert_cmpstr(resolved, ==,ex1.resolved); + g_assert_cmpstr(oid, ==,ex1.string); +} + +static void +oids_test_2both_encoded(void) +{ + gchar* resolved; + gchar* oid; + + oid_both_from_encoded(ex1.encoded, ex1.encoded_len, &resolved, &oid); + g_assert_cmpstr(resolved, ==,ex1.resolved); + g_assert_cmpstr(oid, ==,ex1.string); +} + +static void +oids_test_2both_string(void) +{ + gchar* resolved; + gchar* oid; + + oid_both_from_string(ex1.string, &resolved, &oid); + g_assert_cmpstr(resolved, ==,ex1.resolved); + g_assert_cmpstr(oid, ==,ex1.string); +} + +/* OIDS TESTING FUNCTIONS (/oids/2both/) */ + +static void +oids_test_2struct_subids(void) +{ + guint matched; + guint left; + oid_info_t *st; + + st = oid_get(ex1.subids_len, ex1.subids, &matched, &left); + g_assert_cmpint(matched, ==,1); + g_assert_cmpint(left, ==,ex1.subids_len - 1); + g_assert(st != NULL); + g_assert_cmpstr(st->name, ==, "joint-iso-itu-t"); +} + +static void +oids_test_2struct_encoded(void) +{ + guint matched; + guint left; + guint32 *subids; + oid_info_t *st; + guint len, i; + + st = oid_get_from_encoded(ex1.encoded, ex1.encoded_len, &subids, &matched, &left); + g_assert_cmpint(matched, ==,1); + g_assert_cmpint(left, ==,ex1.subids_len - 1); + g_assert(st != NULL); + g_assert_cmpstr(st->name, ==, "joint-iso-itu-t"); + len = matched + left; + g_assert_cmpint(len, ==, ex1.subids_len); + for (i=0; i < len; i++) + g_assert_cmpint(subids[i], ==, ex1.subids[i]); +} + +static void +oids_test_2struct_string(void) +{ + guint matched; + guint left; + guint32 *subids; + oid_info_t *st; + guint len, i; + + st = oid_get_from_string(ex1.string, &subids, &matched, &left); + g_assert_cmpint(matched, ==,1); + g_assert_cmpint(left, ==,ex1.subids_len - 1); + g_assert(st != NULL); + g_assert_cmpstr(st->name, ==, "joint-iso-itu-t"); + len = matched + left; + g_assert_cmpint(len, ==, ex1.subids_len); + for (i=0; i < len; i++) + g_assert_cmpint(subids[i], ==, ex1.subids[i]); +} + +int +main(int argc, char **argv) +{ + int result; + + g_test_init(&argc, &argv, NULL); + + /* /oids/2encoded */ + g_test_add_func("/oids/2encoded/subids/simple", oids_test_2encoded_subids_simple); + g_test_add_func("/oids/2encoded/subids/bad", oids_test_2encoded_subids_bad); + g_test_add_func("/oids/2encoded/string/simple", oids_test_2encoded_string_simple); + g_test_add_func("/oids/2encoded/string/short", oids_test_2encoded_string_short); + g_test_add_func("/oids/2encoded/string/long", oids_test_2encoded_string_long); + g_test_add_func("/oids/2encoded/string/tooshort", oids_test_2encoded_string_tooshort); + + /* /oids/2subids */ + g_test_add_func("/oids/2subids/string", oids_test_2subids_string); + g_test_add_func("/oids/2subids/string/tooshort", oids_test_2subids_string_tooshort); + g_test_add_func("/oids/2subids/encoded", oids_test_2subids_encoded); + g_test_add_func("/oids/2subids/encoded/long", oids_test_2subids_encoded_long); + g_test_add_func("/oids/2subids/encoded/absviasub", oids_test_2subids_encoded_absviasub); + g_test_add_func("/oids/2subids/encoded/relviasub", oids_test_2subids_encoded_relviasub); + + + /* /oids/2string */ + g_test_add_func("/oids/2string/subids/abs", oids_test_2string_subids_abs); + g_test_add_func("/oids/2string/subids/rel", oids_test_2string_subids_rel); + g_test_add_func("/oids/2string/subids/absviarel", oids_test_2string_subids_absviarel); + g_test_add_func("/oids/2string/subids/relsizes", oids_test_2string_subids_relsizes); + g_test_add_func("/oids/2string/encoded", oids_test_2string_encoded); + g_test_add_func("/oids/2string/encoded/rel", oids_test_2string_encoded_rel); + + /* /oids/2resolved */ + g_test_add_func("/oids/2resolved/subids", oids_test_2resolved_subids); + g_test_add_func("/oids/2resolved/encoded", oids_test_2resolved_encoded); + g_test_add_func("/oids/2resolved/encoded/rel", oids_test_2resolved_encoded_rel); + g_test_add_func("/oids/2resolved/string", oids_test_2resolved_string); + + /* /oids/2both */ + g_test_add_func("/oids/2both/subids", oids_test_2both_subids); + g_test_add_func("/oids/2both/encoded", oids_test_2both_encoded); + g_test_add_func("/oids/2both/string", oids_test_2both_string); + + /* /oids/2struct */ + g_test_add_func("/oids/2struct/subids", oids_test_2struct_subids); + g_test_add_func("/oids/2struct/encoded", oids_test_2struct_encoded); + g_test_add_func("/oids/2struct/string", oids_test_2struct_string); + + /* TODO: add tests for varios oid_add functions and retest name resolution */ + + emem_init(); + wmem_init(); + oids_init(); + result = g_test_run(); + oids_cleanup(); + wmem_cleanup(); + /* + * This might have been a good place for a call to ep_free_all() but that is not part of the + * public interface for emem.h + */ + return result; +} + +/* + * Editor modelines - http://www.wireshark.org/tools/modelines.html + * + * Local variables: + * c-basic-offset: 4 + * tab-width: 8 + * indent-tabs-mode: nil + * End: + * + * vi: set shiftwidth=4 tabstop=8 expandtab: + * :indentSize=4:tabSize=8:noTabs=true: + */ |