summaryrefslogtreecommitdiff
path: root/epan/oids_test.c
diff options
context:
space:
mode:
authorMichael Mann <mmann78@netscape.net>2013-10-19 18:52:13 +0000
committerMichael Mann <mmann78@netscape.net>2013-10-19 18:52:13 +0000
commit3635bea0a4bccd33956cc10a9750f248cfcb4efc (patch)
tree1a51248375c3c4d8ca09f3d01891bff5a8623f4c /epan/oids_test.c
parentb752cfa88dcd001ab6d591a9e337d48c885861c8 (diff)
downloadwireshark-3635bea0a4bccd33956cc10a9750f248cfcb4efc.tar.gz
Add OID unit tests. Bug 9294 (https://bugs.wireshark.org/bugzilla/show_bug.cgi?id=9294)
From Ed Beroset svn path=/trunk/; revision=52692
Diffstat (limited to 'epan/oids_test.c')
-rw-r--r--epan/oids_test.c481
1 files changed, 481 insertions, 0 deletions
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:
+ */