11#!/usr/bin/env python
22
33import sys , unittest
4-
4+ from interfacefinder import mac_addr , local_ip , loopback_intf
55sys .path .insert (0 , './build' )
66import dnet
77
@@ -24,7 +24,7 @@ def test_addr_bcast(self):
2424 for bits in d :
2525 a = dnet .addr ('%s/%d' % (d [32 ], bits ))
2626 b = a .bcast ()
27- self .failUnless (b .__str__ () == d [bits ],
27+ self .assertTrue (b .__repr__ () == d [bits ],
2828 'wrong bcast for /%d' % bits )
2929
3030 def test_addr_net (self ):
@@ -36,49 +36,53 @@ def test_addr_net(self):
3636 for bits in d :
3737 a = dnet .addr ('%s/%d' % (d [32 ], bits ))
3838 b = a .net ()
39- self .failUnless (b .__str__ () == d [bits ],
39+ self .assertTrue (b .__repr__ () == d [bits ],
4040 'wrong net for /%d' % bits )
4141
4242 def test_addr_properties (self ):
4343 atxt = '1.2.3.4/24'
4444 a = dnet .addr (atxt )
4545 assert a .type == dnet .ADDR_TYPE_IP and a .bits == 24
46- assert a .ip == '\x01 \x02 \x03 \x04 ' and a .__str__ () == atxt
47- try : self .failUnless (a .eth == 'xxx' , 'invalid eth property' )
46+ assert a .ip == b '\x01 \x02 \x03 \x04 ' and a .__repr__ () == atxt
47+ try : self .assertTrue (a .eth == 'xxx' , 'invalid eth property' )
4848 except ValueError : pass
4949
5050 atxt = '00:0d:0e:0a:0d:00'
5151 a = dnet .addr (atxt )
5252 assert a == dnet .addr ('0:d:E:a:D:0' )
5353 assert a .type == dnet .ADDR_TYPE_ETH and a .bits == 48
54- assert a .eth == '\x00 \x0d \x0e \x0a \x0d \x00 ' and a .__str__ () == atxt
55- try : self .failUnless (a .ip6 == 'xxx' , 'invalid ip6 property' )
54+ assert a .eth == b '\x00 \x0d \x0e \x0a \x0d \x00 ' and a .__repr__ () == atxt
55+ try : self .assertTrue (a .ip6 == 'xxx' , 'invalid ip6 property' )
5656 except ValueError : pass
5757
5858 atxt = 'fe80::dead:beef:feed:face/48'
5959 a = dnet .addr (atxt )
6060 assert a == dnet .addr ('fe80:0:0::dead:beef:feed:face/48' )
6161 assert a .type == dnet .ADDR_TYPE_IP6 and a .bits == 48
62- assert a .ip6 == '\xfe \x80 \x00 \x00 \x00 \x00 \x00 \x00 \xde \xad \xbe \xef \xfe \xed \xfa \xce ' and a .__str__ () == atxt
63- try : self .failUnless (a .ip == 'xxx' , 'invalid ip property' )
62+ assert a .ip6 == b '\xfe \x80 \x00 \x00 \x00 \x00 \x00 \x00 \xde \xad \xbe \xef \xfe \xed \xfa \xce ' and a .__repr__ () == atxt
63+ try : self .assertTrue (a .ip == 'xxx' , 'invalid ip property' )
6464 except ValueError : pass
6565
6666class ArpTestCase (unittest .TestCase ):
6767 def setUp (self ):
6868 self .arp = dnet .arp ()
69- self .failUnless (self .arp , "couldn't open ARP handle" )
69+ self .assertTrue (self .arp , "couldn't open ARP handle" )
7070 def tearDown (self ):
7171 del self .arp
7272
7373 def test_arp (self ):
7474 # XXX - site-specific values here!
75- pa = dnet .addr ('192.168.0.123' )
76- ha = dnet .addr ('0:d:e:a:d:0' )
77- self .failUnless (self .arp .add (pa , ha ) == None , "couldn't add ARP entry" )
78- self .failUnless (self .arp .get (pa ) == ha , "couldn't find ARP entry" )
79- self .failUnless (self .arp .delete (pa ) == None , "couldn't delete ARP entry" )
75+ pa = dnet .addr (local_ip )
76+ ha = dnet .addr (mac_addr )
77+ self .assertTrue (self .arp .add (pa , ha ) == None , "couldn't add ARP entry" )
78+ self .assertTrue (self .arp .get (pa ) == ha , "couldn't find ARP entry" )
79+ self .assertTrue (self .arp .delete (pa ) == None , "couldn't delete ARP entry" )
80+ self .assertTrue (self .arp .get (pa ) == None , "wrong ARP entry present" )
81+ self .assertTrue (self .arp .add (pa , ha ) == None , "couldn't add ARP entry" )
82+ self .assertTrue (self .arp .get (pa ) == ha , "couldn't find ARP entry" )
83+
8084
81- def __arp_cb (self , pa , ha , arg ):
85+ def __arp_cb (self , pa , arg ):
8286 # XXX - do nothing
8387 return arg
8488
@@ -87,39 +91,39 @@ def test_arp_loop(self):
8791 assert self .arp .loop (self .__arp_cb , 123 ) == 123
8892
8993 def test_arp_misc (self ):
90- sha = '\x00 \x0d \x0e \x0a \x0d \x00 '
91- spa = '\x01 \x02 \x03 \x04 '
92- dha = '\x00 \x0b \x0e \x0e \x0f \x00 '
93- dpa = '\x05 \x06 \x07 \x08 '
94+ sha = b '\x00 \x0d \x0e \x0a \x0d \x00 '
95+ spa = b '\x01 \x02 \x03 \x04 '
96+ dha = b '\x00 \x0b \x0e \x0e \x0f \x00 '
97+ dpa = b '\x05 \x06 \x07 \x08 '
9498 msg = dnet .arp_pack_hdr_ethip (dnet .ARP_OP_REQUEST , sha , spa , dha , dpa )
95- assert msg == '\x00 \x01 \x08 \x00 \x06 \x04 \x00 \x01 \x00 \r \x0e \n \r \x00 \x01 \x02 \x03 \x04 \x00 \x0b \x0e \x0e \x0f \x00 \x05 \x06 \x07 \x08 '
99+ assert msg == b '\x00 \x01 \x08 \x00 \x06 \x04 \x00 \x01 \x00 \r \x0e \n \r \x00 \x01 \x02 \x03 \x04 \x00 \x0b \x0e \x0e \x0f \x00 \x05 \x06 \x07 \x08 '
96100
97101class EthTestCase (unittest .TestCase ):
98102 def setUp (self ):
99103 self .dev = dnet .intf ().get_dst (dnet .addr ('1.2.3.4' ))['name' ]
100104 self .eth = dnet .eth (self .dev )
101- self .failUnless (self .eth , "couldn't open Ethernet handle" )
105+ self .assertTrue (self .eth , "couldn't open Ethernet handle" )
102106 def tearDown (self ):
103107 del self .eth
104108
105109 def test_eth_get (self ):
106110 mac = self .eth .get ()
107- self .failUnless (mac , "couldn't get Ethernet address for %s" % self .dev )
111+ self .assertTrue (mac , "couldn't get Ethernet address for %s" % self .dev )
108112
109113 def test_eth_misc (self ):
110- n = " \x00 \x0d \x0e \x0a \x0d \x00 "
114+ n = b' \x00 \x0d \x0e \x0a \x0d \x00 '
111115 a = '00:0d:0e:0a:0d:00'
112- self .failUnless (dnet .eth_ntoa (n ) == a )
113- self .failUnless (dnet .eth_aton (a ) == n )
114- dst = " \x00 \x0d \x0e \x0a \x0d \x01 "
115- self .failUnless (dnet .eth_pack_hdr (n , dst , dnet .ETH_TYPE_IP ) ==
116- '\x00 \r \x0e \n \r \x00 \x00 \r \x0e \n \r \x01 \x08 \x00 ' )
116+ self .assertTrue (dnet .eth_ntoa (n ) == a )
117+ self .assertTrue (dnet .eth_aton (a ) == n )
118+ dst = b' \x00 \x0d \x0e \x0a \x0d \x01 '
119+ self .assertTrue (dnet .eth_pack_hdr (n , dst , dnet .ETH_TYPE_IP ) ==
120+ b '\x00 \r \x0e \n \r \x00 \x00 \r \x0e \n \r \x01 \x08 \x00 ' )
117121
118122class FwTestCase (unittest .TestCase ):
119123 def setUp (self ):
120124 self .dev = dnet .intf ().get_dst (dnet .addr ('1.2.3.4' ))['name' ]
121125 self .fw = dnet .fw ()
122- self .failUnless (self .fw , "couldn't open firewall handle" )
126+ self .assertTrue (self .fw , "couldn't open firewall handle" )
123127 def tearDown (self ):
124128 del self .fw
125129
@@ -134,9 +138,9 @@ def test_fw(self):
134138 'dst' :dst ,
135139 'dport' :(660 , 666 )
136140 }
137- self .failUnless (self .fw .add (d ) == None ,
141+ self .assertTrue (self .fw .add (d ) == None ,
138142 "couldn't add firewall rule: %s" % d )
139- self .failUnless (self .fw .delete (d ) == None ,
143+ self .assertTrue (self .fw .delete (d ) == None ,
140144 "couldn't delete firewall rule: %s" % d )
141145
142146 def __fw_cb (self , rule , arg ):
@@ -151,25 +155,25 @@ def test_fw_loop(self):
151155class IntfTestCase (unittest .TestCase ):
152156 def setUp (self ):
153157 self .intf = dnet .intf ()
154- self .failUnless (self .intf , "couldn't open interface handle" )
158+ self .assertTrue (self .intf , "couldn't open interface handle" )
155159 def tearDown (self ):
156160 del self .intf
157161
158162 def test_intf_get (self ):
159- lo0 = self .intf .get ('lo0' )
160- self .failUnless (lo0 ['name' ] == 'lo0' , "couldn't get loopback config" )
161- self .failUnless (self .intf .get_src (dnet .addr ('127.0.0.1' )) == lo0 ,
163+ lo0 = self .intf .get (loopback_intf )
164+ self .assertTrue (lo0 ['name' ] == loopback_intf , "couldn't get loopback config" )
165+ self .assertTrue (self .intf .get_src (dnet .addr ('127.0.0.1' )) == lo0 ,
162166 "couldn't get_src 127.0.0.1" )
163167 gw = self .intf .get_dst (dnet .addr ('1.2.3.4' ))
164- self .failUnless (gw , "couldn't get outgoing interface" )
168+ self .assertTrue (gw , "couldn't get outgoing interface" )
165169
166170 def test_intf_set (self ):
167- lo0 = self .intf .get ('lo0' )
171+ lo0 = self .intf .get (loopback_intf )
168172 old_mtu = lo0 ['mtu' ]
169173 new_mtu = 1234
170174 lo0 ['mtu' ] = new_mtu
171175 self .intf .set (lo0 )
172- lo0 = self .intf .get ('lo0' )
176+ lo0 = self .intf .get (loopback_intf )
173177 assert lo0 ['mtu' ] == new_mtu
174178 lo0 ['mtu' ] = old_mtu
175179 self .intf .set (lo0 )
@@ -185,44 +189,43 @@ def test_intf_loop(self):
185189class IpTestCase (unittest .TestCase ):
186190 def setUp (self ):
187191 self .ip = dnet .ip ()
188- self .failUnless (self .ip , "couldn't open raw IP handle" )
192+ self .assertTrue (self .ip , "couldn't open raw IP handle" )
189193 def tearDown (self ):
190194 del self .ip
191195
192196 def test_ip_misc (self ):
193- n = '\x01 \x02 \x03 \x04 '
197+ n = b '\x01 \x02 \x03 \x04 '
194198 a = '1.2.3.4'
195- self .failUnless (dnet .ip_ntoa (n ) == a )
196- self .failUnless (dnet .ip_aton (a ) == n )
197- dst = '\x05 \x06 \x07 \x08 '
198- hdr = dnet .ip_pack_hdr (0 , dnet .IP_HDR_LEN , 666 , 0 , 255 ,
199- dnet .IP_PROTO_UDP , n , dst )
200- assert hdr == 'E\x00 \x00 \x14 \x02 \x9a \x00 \x00 \xff \x11 \x00 \x00 \x01 \x02 \x03 \x04 \x05 \x06 \x07 \x08 '
199+ self .assertTrue (dnet .ip_ntoa (n ) == a )
200+ self .assertTrue (dnet .ip_aton (a ) == n )
201+ dst = b'\x05 \x06 \x07 \x08 '
202+ hdr = dnet .ip_pack_hdr (0 , dnet .IP_HDR_LEN , 666 , 0 , 255 ,dnet .IP_PROTO_UDP , n , dst )
203+ self .assertTrue (hdr == b'E\x00 \x00 \x14 \x02 \x9a \x00 \x00 \xff \x11 \x00 \x00 \x01 \x02 \x03 \x04 \x05 \x06 \x07 \x08 ' )
201204 hdr = dnet .ip_checksum (hdr )
202- assert hdr == 'E \x00 \x00 \x14 \x02 \x9a \x00 \x00 \xff \x11 \xa9 + \x01 \x02 \x03 \x04 \x05 \x06 \x07 \x08 '
205+ self . assertTrue ( hdr == '\x00 \x00 \x14 \x02 \x9a \x00 \x00 \xff \x11 \xa9 \x01 \x02 \x03 \x04 \x05 \x06 \x07 \x08 ' )
203206
204207class RandTestCase (unittest .TestCase ):
205208 def setUp (self ):
206209 self .rand = dnet .rand ()
207- self .failUnless (self .rand , "couldn't open random handle" )
210+ self .assertTrue (self .rand , "couldn't open random handle" )
208211 def tearDown (self ):
209212 del self .rand
210213
211214class RouteTestCase (unittest .TestCase ):
212215 def setUp (self ):
213216 self .route = dnet .route ()
214- self .failUnless (self .route , "couldn't open route handle" )
217+ self .assertTrue (self .route , "couldn't open route handle" )
215218 def tearDown (self ):
216219 del self .route
217220
218221 def test_route (self ):
219222 dst = dnet .addr ('1.2.3.4/24' )
220223 gw = dnet .addr ('127.0.0.1' )
221224 self .route .add (dst , gw )
222- self .failUnless (self .route .get (dst ) == gw )
225+ self .assertTrue (self .route .get (dst ) == gw )
223226 self .route .delete (dst )
224227
225- def __route_cb (self , dst , gw , arg ):
228+ def __route_cb (self , dst , arg ):
226229 # XXX - do nothing
227230 return arg
228231
0 commit comments