The Assimilation Project  based on Assimilation version 1.1.7.1474836767
pcap_test.py
Go to the documentation of this file.
1 # vim: smartindent tabstop=4 shiftwidth=4 expandtab
2 #
3 #
4 # This file is part of the Assimilation Project.
5 #
6 # Copyright (C) 2011, 2012 - Alan Robertson <alanr@unix.sh>
7 #
8 # The Assimilation software is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation, either version 3 of the License, or
11 # (at your option) any later version.
12 #
13 # The Assimilation software is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with the Assimilation Project software. If not, see http://www.gnu.org/licenses/
20 #
21 #
22 _suites = ['all']
23 import sys, os, subprocess
24 import traceback
25 #traceback.print_exc()
26 sys.path.append("../cma")
27 sys.path.append('..')
28 os.environ['G_MESSAGES_DEBUG'] = 'all'
29 
30 from frameinfo import *
31 from AssimCclasses import *
32 import gc
33 import re
34 from AssimCtypes import proj_class_incr_debug, proj_class_decr_debug
35 
36 
37 CheckForDanglingClasses = True
38 WorstDanglingCount = 0
39 DEBUG=True
40 DEBUG=False
41 BROKENDNS=False
42 if 'BROKENDNS' in os.environ:
43  BROKENDNS=True
44 
45 CheckForDanglingClasses = True
46 AssertOnDanglingClasses = True
47 
48 if not CheckForDanglingClasses:
49  print >> sys.stderr, 'WARNING: Memory Leak Detection disabled.'
50 elif not AssertOnDanglingClasses:
51  print >> sys.stderr, 'WARNING: Memory Leak assertions disabled (detection still enabled).'
52 
54  global CheckForDanglingClasses
55  global WorstDanglingCount
56  sys._clear_type_cache()
57  gc.collect()
59  # Avoid cluttering the output up with redundant messages...
60  if count > WorstDanglingCount and CheckForDanglingClasses:
61  WorstDanglingCount = count
62  if AssertOnDanglingClasses:
64  raise AssertionError, "Dangling C-class objects - %d still around" % count
65  else:
66  print >> sys.stderr, ("*****ERROR: Dangling C-class objects - %d still around" % count)
67 
68 class TestCase(object):
69  def assertEqual(self, a, b):
70  assert a == b
71 
72  def assertNotEqual(self, a, b):
73  assert a != b
74 
75  def assertTrue(self, a):
76  assert a is True
77 
78  def assertFalse(self, a):
79  assert a is False
80 
81  def assertRaises(self, exception, function, *args):
82  try:
83  function(*args)
84  raise Exception('Did not raise exception %s: %s(%s)', exception, function, str(args))
85  except exception as e:
86  return True
87 
88  def teardown_method(self, method):
89  print '__del__ CALL for %s' % str(method)
91 
92 def findfile(f):
93  for first in ('.', '..', '../..', '../../..'):
94  for second in ('.', 'pcap', 'src/pcap', 'root_of_source_tree/pcap', 'root_of_binary_tree/pcap'):
95  if os.access(os.path.join(first, second, f), os.R_OK):
96  return os.path.join(first, second, f)
97  return f
98 
99 def output_json(json):
100  process=subprocess.Popen(('jsonlint', '-f'), stdin=subprocess.PIPE)
101  process.communicate(str(json))
102  process.wait()
103 
104 def compare_json(lhs, rhs):
105  #print >> sys.stderr, '----> LHS', lhs
106  #print >> sys.stderr, '----> RHS', rhs
107  lhs = str(pyConfigContext(lhs))
108  rhs = str(pyConfigContext(rhs))
109  if lhs == rhs:
110  return True
111  print 'LHS::::::::::'
112  output_json(lhs)
113  print 'RHS::::::::::'
114  output_json(rhs)
115  return False
116 
117 
118 
120  "A pyNetAddr is a network address of some kind... - let's test it"
121  def validate_switch_discovery(self, filename, pkt, pktend):
122  packet_validation_count = 0
123  for method in (is_valid_lldp_packet, is_valid_cdp_packet):
124  if method(pkt, pktend):
125  packet_validation_count += 1
126  assert packet_validation_count == 1
127  return pySwitchDiscovery.decode_discovery('me', 'eth0', filename, '<now>', pkt, pktend)['data']
128 
130  discovery_files = (('lldp.detailed.pcap','''{
131  "ChassisId" : "00-01-30-f9-ad-a0",
132  "ManagementAddress" : "00-01-30-f9-ad-a0",
133  "ports" : { "1/1" : {
134  "autoneg_enabled" : false,
135  "autoneg_supported" : true,
136  "ConnectsToHost" : "me",
137  "ConnectsToInterface" : "eth0",
138  "duplex" : "half",
139  "media" : "2 pair category 5 UTP",
140  "mtu" : 1522,
141  "PortDescription" : "Summit300-48-Port 1001",
142  "PortId" : "1/1",
143  "ppvid" : 0,
144  "pp_vlan_capable" : false,
145  "pp_vlan_enabled" : false,
146  "pvid" : 488,
147  "sourceMAC" : "00-01-30-f9-ad-a0",
148  "speed" : 100,
149  "vid" : 488,
150  "vlan_name" : "v2-0488-03-0505"
151  } },
152  "SystemCapabilities" : {
153  "bridge" : true,
154  "router" : true
155  },
156  "SystemDescription" : "Summit300-48 - Version 7.4e.1 (Build 5) by Release_Master 05/27/05 04:53:11",
157  "SystemName" : "Summit300-48"
158 }'''),
159 ('lldpmed_civicloc.pcap','''{
160  "ChassisId" : "00-13-21-57-ca-40",
161  "ManagementAddress" : "15.255.122.148",
162  "ports" : { "1" : {
163  "autoneg_enabled" : false,
164  "autoneg_supported" : true,
165  "ConnectsToHost" : "me",
166  "ConnectsToInterface" : "eth0",
167  "duplex" : "half",
168  "media" : "2 pair category 5 UTP",
169  "PortDescription" : "1",
170  "PortId" : "1",
171  "sourceMAC" : "00-13-21-57-ca-7f",
172  "speed" : 100
173  } },
174  "SystemCapabilities" : {
175  "bridge" : true,
176  "router" : false
177  },
178  "SystemDescription" : "ProCurve J8762A Switch 2600-8-PWR, revision H.08.89, ROM H.08.5X (/sw/code/build/fish(ts_08_5))",
179  "SystemName" : "ProCurve Switch 2600-8-PWR"
180 }'''),
181 ('procurve.lldp.pcap','''{
182  "ChassisId" : "00-25-61-94-32-40",
183  "ports" : { "22" : {
184  "ConnectsToHost" : "me",
185  "ConnectsToInterface" : "eth0",
186  "PortDescription" : "22",
187  "PortId" : "22",
188  "sourceMAC" : "00-25-61-94-32-4a"
189  } },
190  "SystemName" : "ProCurve Switch 2824"
191 }'''),
192 ('cdp-BCM1100.pcap','''{
193  "ChassisId" : "0060B9C14027",
194  "ports" : { },
195  "SystemCapabilities" : [ "host" ],
196  "SystemPlatform" : "BCM91100",
197  "SystemVersion" : "BCM1100"
198 }'''),
199 ('cdp_v2_voice.pcap','''{
200  "ChassisId" : "myswitch",
201  "ManagementAddress" : "192.168.0.4",
202  "ports" : { "FastEthernet0/1" : {
203  "CiscoUnTrustedPortCOS" : 0,
204  "ConnectsToHost" : "me",
205  "ConnectsToInterface" : "eth0",
206  "duplex" : "full",
207  "PortId" : "FastEthernet0/1",
208  "sourceMAC" : "00-0b-be-18-9a-41",
209  "VlanId" : 1
210  } },
211  "SystemAddress" : "192.168.0.4",
212  "SystemCapabilities" : [
213  "bridge",
214  "igmp-filter"
215  ],
216  "SystemPlatform" : "cisco WS-C2950-12",
217  "SystemVersion" : "Cisco Internetwork Operating System Software \nIOS (tm) C2950 Software (C2950-I6K2L2Q4-M), Version 12.1(22)EA14, RELEASE SOFTWARE (fc1)\nTechnical Support: http://www.cisco.com/techsupport\nCopyright (c) 1986-2010 by cisco Systems, Inc.\nCompiled Tue 26-Oct-10 10:35 by nburra",
218  "VLANManagementDomain" : "MYDOMAIN"
219 }'''),
220 ('n0.eth2.cdp-2.pcap','''
221 {
222  "ChassisId" : "csr706a.pbm.ihost.com",
223  "ports" : { "GigabitEthernet1/0/4" : {
224  "CiscoUnTrustedPortCOS" : 0,
225  "ConnectsToHost" : "me",
226  "ConnectsToInterface" : "eth0",
227  "duplex" : "full",
228  "PortId" : "GigabitEthernet1/0/4",
229  "sourceMAC" : "00-0f-23-b0-62-84",
230  "VlanId" : 77
231  } },
232  "SystemAddress" : "129.40.0.4",
233  "SystemCapabilities" : [
234  "router",
235  "bridge",
236  "igmp-filter"
237  ],
238  "SystemPlatform" : "cisco WS-C3750G-24TS",
239  "SystemVersion" : "Cisco Internetwork Operating System Software \nIOS (tm) C3750 Software (C3750-I5-M), Version 12.2(20)SE4, RELEASE SOFTWARE (fc1)\nCopyright (c) 1986-2005 by cisco Systems, Inc.\nCompiled Sun 09-Jan-05 00:09 by antonino",
240  "VLANManagementDomain" : "spbm"
241 }'''),
242 ('cdp.pcap','''
243 {
244  "ChassisId" : "R1",
245  "ports" : { "Ethernet0" : {
246  "ConnectsToHost" : "me",
247  "ConnectsToInterface" : "eth0",
248  "PortId" : "Ethernet0",
249  "sourceMAC" : "00-e0-1e-d5-d5-15"
250  } },
251  "SystemAddress" : "192.168.0.4",
252  "SystemCapabilities" : [ "router" ],
253  "SystemPlatform" : "cisco 1601",
254  "SystemVersion" : "Cisco Internetwork Operating System Software \nIOS (tm) 1600 Software (C1600-NY-L), Version 11.2(12)P, RELEASE SOFTWARE (fc1)\nCopyright (c) 1986-1998 by cisco Systems, Inc.\nCompiled Tue 03-Mar-98 06:33 by dschwart"
255 }'''),
256 ('cdp_v2.pcap','''{
257  "ChassisId" : "myswitch",
258  "ManagementAddress" : "192.168.0.4",
259  "ports" : { "FastEthernet0/1" : {
260  "CiscoUnTrustedPortCOS" : 0,
261  "ConnectsToHost" : "me",
262  "ConnectsToInterface" : "eth0",
263  "duplex" : "full",
264  "PortId" : "FastEthernet0/1",
265  "sourceMAC" : "00-0b-be-18-9a-41",
266  "VlanId" : 1
267  } },
268  "SystemAddress" : "192.168.0.4",
269  "SystemCapabilities" : [
270  "bridge",
271  "igmp-filter"
272  ],
273  "SystemPlatform" : "cisco WS-C2950-12",
274  "SystemVersion" : "Cisco Internetwork Operating System Software \nIOS (tm) C2950 Software (C2950-I6K2L2Q4-M), Version 12.1(22)EA14, RELEASE SOFTWARE (fc1)\nTechnical Support: http://www.cisco.com/techsupport\nCopyright (c) 1986-2010 by cisco Systems, Inc.\nCompiled Tue 26-Oct-10 10:35 by nburra",
275  "VLANManagementDomain" : "MYDOMAIN"
276 }'''),
277  )
278  for (f, out) in discovery_files:
279  for pcap_entry in pyPcapCapture(findfile(f)):
280  pktstart, pktend, pktlen = pcap_entry
281  #print >> sys.stderr, 'Got %d bytes from %s' % (pktlen, f)
282  #print >> sys.stderr, '----> Got %d bytes from %s' % (pktlen, f)
283  json = self.validate_switch_discovery(f, pktstart, pktend)
284  #print >> sys.stderr, '<---- Done processing %d bytes from %s' % (pktlen, f)
285  assert compare_json(out, json)
286  print 'Passed %d switch discovery tests' % (len(discovery_files))
287 
288  def not_a_test_output(self):
289  files = (
290  'lldp.detailed.pcap',
291  'lldpmed_civicloc.pcap',
292  'procurve.lldp.pcap',
293  'cdp-BCM1100.pcap',
294  # 'pcap/cdp_v2_hdlc.pcap',
295  'cdp_v2_voice.pcap',
296  'n0.eth2.cdp-2.pcap',
297  'cdp.pcap',
298  'cdp_v2.pcap',
299  #'pcap/cdp_v2_voice_vlan.pcap'
300  )
301  for f in files:
302  for pcap_entry in pyPcapCapture(findfile(f)):
303  pktstart, pktend, pktlen = pcap_entry
304  print ("('%s','''" % f)
305  sys.stdout.flush()
306  output_json(self.validate_switch_discovery(f, pktstart, pktend))
307  print("'''),")
308  sys.stdout.flush()
309 
310 if __name__ == "__main__":
311  run()
def assertEqual(self, a, b)
Definition: pcap_test.py:69
def compare_json(lhs, rhs)
Definition: pcap_test.py:104
def assertRaises(self, exception, function, args)
Definition: pcap_test.py:81
def teardown_method(self, method)
Definition: pcap_test.py:88
guint32 proj_class_live_object_count(void)
Return the count of live C class objects.
Definition: proj_classes.c:406
def assert_no_dangling_Cclasses()
Definition: pcap_test.py:53
def assertNotEqual(self, a, b)
Definition: pcap_test.py:72
def output_json(json)
Definition: pcap_test.py:99
def validate_switch_discovery(self, filename, pkt, pktend)
Definition: pcap_test.py:121