]> git.ipfire.org Git - thirdparty/lldpd.git/blobdiff - tests/check_snmp.c
travis: disable integration tests
[thirdparty/lldpd.git] / tests / check_snmp.c
index c82ad15c2cafba971da67f2c33bae8b96ea8e9ca..7ff6d1340cae6221d6f877240a06f194b2642f2a 100644 (file)
@@ -1,3 +1,20 @@
+/* -*- mode: c; c-file-style: "openbsd" -*- */
+/*
+ * Copyright (c) 2015 Vincent Bernat <bernat@luffy.cx>
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
 #include <check.h>
 
 #include "../src/daemon/lldpd.h"
@@ -24,7 +41,7 @@ struct timeval test_starttime = { .tv_sec = 100, .tv_usec = 0 };
 /* First chassis */
 struct lldpd_mgmt mgmt1 = {
                .m_family = LLDPD_AF_IPV4,
-               .m_addr = { .inet = { 251789504 } }, /* 192.0.2.15 */
+               .m_addr = { .octets = { 0xc0, 0, 0x2, 0xf } }, /* 192.0.2.15 */
                .m_addrsize = sizeof(struct in_addr),
                .m_iface = 3
 };
@@ -56,7 +73,7 @@ struct lldpd_chassis chassis1 = {
 /* Second chassis */
 struct lldpd_mgmt mgmt2 = {
                .m_family = LLDPD_AF_IPV4,
-               .m_addr = { .inet = { 285343936 } }, /* 192.0.2.17 */
+               .m_addr = { .octets = { 0xc0, 0, 0x2, 0x11 } }, /* 192.0.2.17 */
                .m_addrsize = sizeof(struct in_addr),
                .m_iface = 5
 };
@@ -260,7 +277,7 @@ struct lldpd_pi pi88cc = {
        .p_pi_len = 2,
 };
 struct lldpd_pi pi888e01 = {
-       .p_pi = "\x88\x8e\e01",
+       .p_pi = "\x88\x8e\x01",
        .p_pi_len = 3,
 };
 #endif
@@ -279,7 +296,6 @@ struct lldpd_port port2 = {
 void
 snmp_config()
 {
-       
        starttime = test_starttime;
        agent_scfg = &test_cfg;
        TAILQ_INIT(&test_cfg.g_chassis);
@@ -316,7 +332,6 @@ snmp_config()
        TAILQ_INSERT_TAIL(&hardware1.h_rports, &hardware2.h_lport, p_entries);
        TAILQ_INIT(&hardware2.h_rports);
        TAILQ_INSERT_TAIL(&hardware2.h_rports, &hardware1.h_lport, p_entries);
-       
 }
 
 /* Convert OID to a string. Static buffer. */
@@ -325,20 +340,21 @@ snmp_oidrepr(oid *name, size_t namelen)
 {
        static char *buffer[4] = {NULL, NULL, NULL, NULL};
        static int current = 0;
-       int i;
+       size_t i;
 
        current = (current + 1)%4;
-       if (buffer[current]) free(buffer[current]); buffer[current] = NULL;
+       free(buffer[current]); buffer[current] = NULL;
 
        for (i = 0; i < namelen; i++) {
                /* Not very efficient... */
                char *newbuffer = NULL;
-               if (asprintf(&newbuffer, "%s.%lu", buffer[current]?buffer[current]:"", name[i]) == -1) {
+               if (asprintf(&newbuffer, "%s.%lu", buffer[current]?buffer[current]:"",
+                       (unsigned long)name[i]) == -1) {
                        free(buffer[current]);
                        buffer[current] = NULL;
                        return NULL;
                }
-               if (buffer[current]) free(buffer[current]);
+               free(buffer[current]);
                buffer[current] = newbuffer;
        }
        return buffer[current++];
@@ -798,10 +814,10 @@ struct tree_node snmp_tree[] = {
        { { 1, 5, 32962, 1, 2, 3, 1, 2, 3, 1449}, 10, ASN_OCTET_STR,
          { .string = { .octet = "VLAN #1449", .len = 10 }} },
        /* lldpXdot1LocProtocolId */
-       { { 1, 5, 32962, 1, 2, 4, 1, 2, 3, 13175}, 10, ASN_OCTET_STR,
+       { { 1, 5, 32962, 1, 2, 4, 1, 2, 3, 30321}, 10, ASN_OCTET_STR,
+         { .string = { .octet = "\x88\x8e\x01", .len = 3 } }},
+       { { 1, 5, 32962, 1, 2, 4, 1, 2, 3, 30515}, 10, ASN_OCTET_STR,
          { .string = { .octet = "\x88\xcc", .len = 2 } }},
-       { { 1, 5, 32962, 1, 2, 4, 1, 2, 3, 29020}, 10, ASN_OCTET_STR,
-         { .string = { .octet = "\x88\x8e\e01", .len = 3 } }},
 
        /* lldpXdot1RemPortVlanId */
        { { 1, 5, 32962, 1, 3, 1, 1, 1, 0, 3, 1}, 11, ASN_INTEGER, { .integer = 0 }},
@@ -821,10 +837,10 @@ struct tree_node snmp_tree[] = {
        { { 1, 5, 32962, 1, 3, 3, 1, 2, 10000, 4, 1, 1449}, 12, ASN_OCTET_STR,
          { .string = { .octet = "VLAN #1449", .len = 10 }} },
        /* lldpXdot1RemProtocolId */
-       { { 1, 5, 32962, 1, 3, 4, 1, 2, 10000, 4, 1, 13175}, 12, ASN_OCTET_STR,
-         { .string = { .octet = "\x88\xcc", .len = 2 } }},
-       { { 1, 5, 32962, 1, 3, 4, 1, 2, 10000, 4, 1, 29020}, 12, ASN_OCTET_STR,
-         { .string = { .octet = "\x88\x8e\e01", .len = 3 } }},
+       { { 1, 5, 32962, 1, 3, 4, 1, 2, 10000, 4, 1, 30321}, 12, ASN_OCTET_STR,
+         { .string = { .octet = "\x88\x8e\x01", .len = 3 } }},
+       { { 1, 5, 32962, 1, 3, 4, 1, 2, 10000, 4, 1, 30515}, 12, ASN_OCTET_STR,
+         { .string = { .octet = "\x88\xcc", .len = 2 } }}
 #endif
 };
 
@@ -833,10 +849,10 @@ tohex(char *str, size_t len)
 {
        static char *hex[] = { NULL, NULL };
        static int which = 0;
-       if (hex[which]) free(hex[which]); hex[which] = NULL;
+       free(hex[which]); hex[which] = NULL;
        hex[which] = malloc(len * 3 + 1);
        fail_unless(hex[which] != NULL, "Not enough memory?");
-       for (int i = 0; i < len; i++)
+       for (size_t i = 0; i < len; i++)
                snprintf(hex[which] + 3*i, 4, "%02X ", (unsigned char)str[i]);
        which = 1 - which;
        return hex[1 - which];
@@ -862,7 +878,7 @@ snmp_merge(struct variable8 *v1, struct tree_node *n, struct variable *vp,
        vp->type = v1->type;
        vp->acl = v1->acl;
        vp->findVar = v1->findVar;
-       vp->namelen = v1->namelen + 
+       vp->namelen = v1->namelen +
                sizeof(lldp_oid)/sizeof(oid);
        memcpy(vp->name, lldp_oid, sizeof(lldp_oid));
        memcpy(vp->name + sizeof(lldp_oid)/sizeof(oid),
@@ -881,6 +897,7 @@ snmp_compare(struct tree_node *n,
             u_char *result, size_t varlen,
             oid *target, size_t targetlen, char *repr)
 {
+       unsigned long int value;
        fail_unless((targetlen == sizeof(lldp_oid)/sizeof(oid) + n->namelen) &&
                    !memcmp(target, lldp_oid, sizeof(lldp_oid)) &&
                    !memcmp(target + sizeof(lldp_oid)/sizeof(oid),
@@ -896,12 +913,12 @@ snmp_compare(struct tree_node *n,
                fail_unless(varlen == sizeof(unsigned long int),
                            "Inappropriate length for integer type for OID %s",
                            repr);
-               fail_unless(n->value.integer ==
-                           *(unsigned long int *)result,
+               memcpy(&value, result, sizeof(value));
+               fail_unless(n->value.integer == value,
                            "For OID %s, expected value %u but got %u instead",
                            repr,
                            n->value.integer,
-                           *(unsigned long int *)result);
+                           value);
                break;
        default:
                fail_unless(((n->value.string.len == varlen) &&
@@ -916,7 +933,7 @@ snmp_compare(struct tree_node *n,
 
 START_TEST (test_variable_order)
 {
-       int i;
+       size_t i;
        for (i = 0; i < agent_lldp_vars_size() - 1; i++) {
                fail_unless(snmp_oid_compare(agent_lldp_vars[i].name,
                                             agent_lldp_vars[i].namelen,
@@ -931,11 +948,11 @@ END_TEST
 
 START_TEST (test_get)
 {
-       int j;
+       size_t j;
        for (j = 0;
             j < sizeof(snmp_tree)/sizeof(struct tree_node);
             j++) {
-               int             i;
+               size_t          i;
                int             found = 0;
                struct variable vp;
                oid             target[MAX_OID_LEN];
@@ -958,7 +975,7 @@ START_TEST (test_get)
 
                        /* Invoke the function */
                        result = vp.findVar(&vp, target, &targetlen, 1, &varlen, &wmethod);
-                       
+
                        /* Check the result */
                        fail_unless(result != NULL,
                                    "No result when querying %s", repr);
@@ -975,12 +992,12 @@ END_TEST
 
 START_TEST (test_getnext)
 {
-       int j;
-       int end = sizeof(snmp_tree)/sizeof(struct tree_node);
+       size_t j;
+       size_t end = sizeof(snmp_tree)/sizeof(struct tree_node);
        for (j = 0;
             j < end;
             j++) {
-               int             i;
+               size_t          i;
                struct variable vp;
                oid             target[MAX_OID_LEN];
                size_t          targetlen;