blob: da378dbebe3b7fb4a416523c5263340617bb085f [file] [log] [blame]
Renato Botelho do Coutoead1e532019-10-31 13:31:07 -05001#!/usr/bin/env python3
Filip Vargaeb601242018-10-31 07:58:05 +01002""" CDP tests """
3
4from scapy.packet import Packet
5from scapy.all import ShortField, StrField
6from scapy.layers.l2 import Dot3, LLC, SNAP
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02007from scapy.contrib.cdp import (
8 CDPMsgDeviceID,
9 CDPMsgSoftwareVersion,
10 CDPMsgPlatform,
11 CDPMsgPortID,
12 CDPv2_HDR,
13)
Filip Vargaeb601242018-10-31 07:58:05 +010014
15from framework import VppTestCase
16from scapy.all import raw
17from re import compile
18from time import sleep
19from util import ppp
20import platform
Ole Troanbd38f7f2019-10-22 08:57:31 +020021import sys
22import unittest
Filip Vargaeb601242018-10-31 07:58:05 +010023
24
25""" TestCDP is a subclass of VPPTestCase classes.
26
27CDP test.
28
29"""
30
31
32class CustomTLV(Packet):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020033 """Custom TLV protocol layer for scapy"""
Filip Vargaeb601242018-10-31 07:58:05 +010034
35 fields_desc = [
36 ShortField("type", 0),
37 ShortField("length", 4),
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020038 StrField("value", ""),
Filip Vargaeb601242018-10-31 07:58:05 +010039 ]
40
41
42class TestCDP(VppTestCase):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020043 """CDP Test Case"""
Filip Vargaeb601242018-10-31 07:58:05 +010044
45 nen_ptr = compile(r"not enabled")
46 cdp_ptr = compile(r"^([-\.\w]+)\s+([-\.\w]+)\s+([-\.\w]+)\s+([-\.\w]+)$")
47 err_ptr = compile(r"^([\d]+)\s+([-\w]+)\s+([ -\.\w)(]+)$")
48
49 @property
50 def device_id(self):
51 return platform.node()
52
53 @property
54 def version(self):
55 return platform.release()
56
57 @property
58 def port_id(self):
59 return self.interface.name
60
61 @property
62 def platform(self):
63 return platform.system()
64
65 @classmethod
66 def setUpClass(cls):
67 super(TestCDP, cls).setUpClass()
68 try:
69 cls.create_pg_interfaces(range(1))
70 cls.interface = cls.pg_interfaces[0]
71
72 cls.interface.admin_up()
73 cls.interface.config_ip4()
74 cls.interface.resolve_arp()
75
76 except Exception:
77 super(TestCDP, cls).tearDownClass()
78 raise
79
Paul Vinciguerraa7427ec2019-03-10 10:04:23 -070080 @classmethod
81 def tearDownClass(cls):
82 super(TestCDP, cls).tearDownClass()
83
Filip Vargaeb601242018-10-31 07:58:05 +010084 def test_enable_cdp(self):
Paul Vinciguerra76ef6092019-03-19 05:44:16 -070085 self.logger.info(self.vapi.cdp_enable_disable(enable_disable=1))
Filip Vargaeb601242018-10-31 07:58:05 +010086 ret = self.vapi.cli("show cdp")
87 self.logger.info(ret)
88 not_enabled = self.nen_ptr.search(ret)
89 self.assertFalse(not_enabled, "CDP isn't enabled")
90
91 def test_send_cdp_packet(self):
Paul Vinciguerra76ef6092019-03-19 05:44:16 -070092 self.logger.info(self.vapi.cdp_enable_disable(enable_disable=1))
Filip Vargaeb601242018-10-31 07:58:05 +010093 self.send_packet(self.create_packet())
94
95 neighbors = list(self.show_cdp())
96 self.assertTrue(neighbors, "CDP didn't register neighbor")
97
98 port, system = neighbors[0]
Lijian.Zhangca7f53a2018-12-10 02:08:02 -080099 length = min(len(system), len(self.device_id))
Filip Vargaeb601242018-10-31 07:58:05 +0100100
101 self.assert_equal(port, self.port_id, "CDP received invalid port id")
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200102 self.assert_equal(
103 system[:length], self.device_id[:length], "CDP received invalid device id"
104 )
Filip Vargaeb601242018-10-31 07:58:05 +0100105
Filip Varga3206bb12018-11-05 09:41:56 +0100106 def test_cdp_underflow_tlv(self):
107 self.send_bad_packet(3, ".")
108
109 def test_cdp_overflow_tlv(self):
110 self.send_bad_packet(8, ".")
111
112 def send_bad_packet(self, l, v):
Paul Vinciguerra76ef6092019-03-19 05:44:16 -0700113 self.logger.info(self.vapi.cdp_enable_disable(enable_disable=1))
Filip Varga3206bb12018-11-05 09:41:56 +0100114 self.send_packet(self.create_bad_packet(l, v))
Filip Vargaeb601242018-10-31 07:58:05 +0100115
Ole Troan148c7b72020-10-07 18:05:37 +0200116 err = self.statistics.get_err_counter(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200117 "/err/cdp-input/cdp packets with bad TLVs"
118 )
Ole Troan148c7b72020-10-07 18:05:37 +0200119 self.assertTrue(err >= 1, "CDP didn't drop bad packet")
Filip Vargaeb601242018-10-31 07:58:05 +0100120
121 def send_packet(self, packet):
122 self.logger.debug(ppp("Sending packet:", packet))
123 self.interface.add_stream(packet)
124 self.pg_start()
125
126 def create_base_packet(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200127 packet = (
128 Dot3(src=self.interface.remote_mac, dst="01:00:0c:cc:cc:cc")
129 / LLC(dsap=0xAA, ssap=0xAA, ctrl=0x03)
130 / SNAP()
131 / CDPv2_HDR()
132 )
Filip Vargaeb601242018-10-31 07:58:05 +0100133 return packet
134
135 def create_packet(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200136 packet = (
137 self.create_base_packet()
138 / CDPMsgDeviceID(val=self.device_id)
139 / CDPMsgSoftwareVersion(val=self.version)
140 / CDPMsgPortID(iface=self.port_id)
141 / CDPMsgPlatform(val=self.platform)
142 )
Filip Vargaeb601242018-10-31 07:58:05 +0100143 return packet
144
145 def create_bad_packet(self, tl=4, tv=""):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200146 packet = self.create_base_packet() / CustomTLV(type=1, length=tl, value=tv)
Filip Vargaeb601242018-10-31 07:58:05 +0100147 return packet
148
149 def process_cli(self, exp, ptr):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200150 for line in self.vapi.cli(exp).split("\n")[1:]:
Filip Vargaeb601242018-10-31 07:58:05 +0100151 m = ptr.match(line.strip())
152 if m:
153 yield m.groups()
154
155 def show_cdp(self):
156 for pack in self.process_cli("show cdp", self.cdp_ptr):
157 try:
158 port, system, _, _ = pack
159 except ValueError:
160 pass
161 else:
162 yield port, system