test-lldp.c revision a8eaaee72a2f06e0fb64fb71de3b71ecba31dafb
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright (C) 2014 Tom Gundersen
Copyright (C) 2014 Susant Sahani
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <net/ethernet.h>
#include <stdio.h>
#include <string.h>
#include "sd-event.h"
#include "sd-lldp.h"
#include "alloc-util.h"
#include "event-util.h"
#include "fd-util.h"
#include "lldp-network.h"
#include "lldp-tlv.h"
#include "lldp.h"
#include "macro.h"
#include "string-util.h"
#define TEST_LLDP_PORT "em1"
#define TEST_LLDP_TYPE_SYSTEM_NAME "systemd-lldp"
#define TEST_LLDP_TYPE_SYSTEM_DESC "systemd-lldp-desc"
static int test_fd[2];
static struct ether_addr mac_addr = {
};
struct ether_header ether = {
};
/* Append Ethernet header */
assert_se(tlv_packet_new(&m) >= 0);
assert_se(lldp_tlv_packet_close_container(m) >= 0);
/* port name */
assert_se(lldp_tlv_packet_close_container(m) >= 0);
/* ttl */
assert_se(lldp_tlv_packet_close_container(m) >= 0);
/* system name */
strlen(TEST_LLDP_TYPE_SYSTEM_NAME)) >= 0);
assert_se(lldp_tlv_packet_close_container(m) >= 0);
/* system descrition */
strlen(TEST_LLDP_TYPE_SYSTEM_DESC)) >= 0);
assert_se(lldp_tlv_packet_close_container(m) >= 0);
/* Mark end of packet */
assert_se(lldp_tlv_packet_close_container(m) >= 0);
*ret = m;
m = NULL;
return 0;
}
switch (subtype) {
break;
default:
assert_not_reached("Unhandled option");
}
assert_se(lldp_tlv_packet_exit_container(m) >= 0);
return 0;
}
static int lldp_parse_port_id_tlv(tlv_packet *m) {
_cleanup_free_ char *p = NULL;
switch (subtype) {
assert_se(p);
break;
default:
assert_not_reached("Unhandled option");
}
assert_se(lldp_tlv_packet_exit_container(m) >= 0);
return 0;
}
static int lldp_parse_system_name_tlv(tlv_packet *m) {
_cleanup_free_ char *p = NULL;
assert_se(p);
assert_se(lldp_tlv_packet_exit_container(m) >= 0);
return 1;
}
static int lldp_parse_system_desc_tlv(tlv_packet *m) {
_cleanup_free_ char *p = NULL;
assert_se(p);
assert_se(lldp_tlv_packet_exit_container(m) >= 0);
return 0;
}
static int lldp_parse_ttl_tlv(tlv_packet *m) {
assert_se(lldp_tlv_packet_exit_container(m) >= 0);
return 0;
}
static int lldp_get_destination_type(tlv_packet *m) {
int dest;
return 0;
}
assert_se(lldp_parse_port_id_tlv(m) >= 0);
assert_se(lldp_parse_system_name_tlv(m) >= 0);
assert_se(lldp_parse_ttl_tlv(m) >= 0);
assert_se(lldp_parse_system_desc_tlv(m) >= 0);
assert_se(lldp_get_destination_type(m) >= 0);
return 0;
}
static void test_parser(void) {
/* form a packet */
/* parse the packet */
/* verify */
}
int lldp_network_bind_raw_socket(int ifindex) {
return -errno;
return test_fd[0];
}
static int lldp_handler_calls;
}
int r;
if (r)
return r;
r = sd_lldp_attach_event(*lldp, e, 0);
if (r)
return r;
if (r)
return r;
r = sd_lldp_start(*lldp);
if (r)
return r;
return 0;
}
int r;
r = sd_lldp_stop(lldp);
if (r)
return r;
r = sd_lldp_detach_event(lldp);
if (r)
return r;
return 0;
}
static void test_receive_basic_packet(sd_event *e) {
int dest_type;
char *str;
/* Ethernet header */
0x01, 0x80, 0xc2, 0x00, 0x00, 0x03, /* Destination MAC*/
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, /* Source MAC */
0x88, 0xcc, /* Ethertype */
/* LLDP mandatory TLVs */
0x02, 0x07, 0x04, 0x00, 0x01, 0x02, /* Chassis: MAC, 00:01:02:03:04:05 */
0x03, 0x04, 0x05,
0x04, 0x04, 0x05, 0x31, 0x2f, 0x33, /* Port: interface name, "1/3" */
0x06, 0x02, 0x00, 0x78, /* TTL: 120 seconds*/
/* LLDP optional TLVs */
0x08, 0x04, 0x50, 0x6f, 0x72, 0x74, /* Port Description: "Port" */
0x0a, 0x03, 0x53, 0x59, 0x53, /* System Name: "SYS" */
0x0c, 0x04, 0x66, 0x6f, 0x6f, 0x00, /* System Description: "foo" (NULL-terminated) */
0x00, 0x00 /* End Of LLDPDU */
};
lldp_handler_calls = 0;
sd_event_run(e, 0);
}
static void test_receive_incomplete_packet(sd_event *e) {
/* Ethernet header */
0x01, 0x80, 0xc2, 0x00, 0x00, 0x03, /* Destination MAC*/
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, /* Source MAC */
0x88, 0xcc, /* Ethertype */
/* LLDP mandatory TLVs */
0x02, 0x07, 0x04, 0x00, 0x01, 0x02, /* Chassis: MAC, 00:01:02:03:04:05 */
0x03, 0x04, 0x05,
0x04, 0x04, 0x05, 0x31, 0x2f, 0x33, /* Port: interface name, "1/3" */
/* Missing TTL */
0x00, 0x00 /* End Of LLDPDU */
};
lldp_handler_calls = 0;
sd_event_run(e, 0);
assert_se(lldp_handler_calls == 0);
}
static void test_receive_oui_packet(sd_event *e) {
char *str;
/* Ethernet header */
0x01, 0x80, 0xc2, 0x00, 0x00, 0x03, /* Destination MAC*/
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, /* Source MAC */
0x88, 0xcc, /* Ethertype */
/* LLDP mandatory TLVs */
0x02, 0x07, 0x04, 0x00, 0x01, 0x02, /* Chassis: MAC, 00:01:02:03:04:05 */
0x03, 0x04, 0x05,
0x04, 0x04, 0x05, 0x31, 0x2f, 0x33, /* Port TLV: interface name, "1/3" */
0x06, 0x02, 0x00, 0x78, /* TTL: 120 seconds*/
/* LLDP optional TLVs */
0xfe, 0x06, 0x00, 0x80, 0xc2, 0x01, /* Port VLAN ID: 0x1234 */
0x12, 0x34,
0xfe, 0x07, 0x00, 0x80, 0xc2, 0x02, /* Port and protocol: flag 1, PPVID 0x7788 */
0x01, 0x77, 0x88,
0xfe, 0x0d, 0x00, 0x80, 0xc2, 0x03, /* VLAN Name: ID 0x1234, name "Vlan51" */
0x12, 0x34, 0x06, 0x56, 0x6c, 0x61,
0x6e, 0x35, 0x31,
0xfe, 0x06, 0x00, 0x80, 0xc2, 0x06, /* Management VID: 0x0102 */
0x01, 0x02,
0xfe, 0x09, 0x00, 0x80, 0xc2, 0x07, /* Link aggregation: status 1, ID 0x00140012 */
0x01, 0x00, 0x14, 0x00, 0x12,
0x00, 0x00 /* End of LLDPDU */
};
lldp_handler_calls = 0;
sd_event_run(e, 0);
}
test_parser();
/* LLDP reception tests */
assert_se(sd_event_new(&e) == 0);
return 0;
}