PageRenderTime 89ms CodeModel.GetById 2ms app.highlight 77ms RepoModel.GetById 1ms app.codeStats 1ms

/client/net/net_utils_unittest.py

https://github.com/manul7/autotest
Python | 1325 lines | 1264 code | 49 blank | 12 comment | 0 complexity | 65ce8e45280bb0d0b1eab3c65a0a5522 MD5 | raw file

Large files files are truncated, but you can click here to view the full file

   1#!/usr/bin/python
   2import unittest, os, socket, time, sys, struct
   3try:
   4    import autotest.common as common
   5except ImportError:
   6    import common
   7from autotest.client import utils
   8from autotest.client.net import net_utils, net_utils_mock
   9from autotest.client.shared.test_utils import mock
  10from autotest.client.shared import error
  11
  12
  13class TestNetUtils(unittest.TestCase):
  14    class network_interface_mock(net_utils_mock.network_interface_mock):
  15        def __init__(self, iface='some_name', test_init=False):
  16            super(TestNetUtils.network_interface_mock,
  17                  self).__init__(iface=iface, test_init=test_init)
  18
  19
  20    def setUp(self):
  21        self.god = mock.mock_god()
  22        self.god.stub_function(utils, "system")
  23        self.god.stub_function(utils, "system_output")
  24        self.god.stub_function(utils, "module_is_loaded")
  25        self.god.stub_function(net_utils, "open")
  26        self.god.stub_function(time, 'sleep')
  27
  28        self.god.stub_with(net_utils, "bond", net_utils.bonding)
  29        self.god.stub_with(os, 'open', net_utils_mock.os_open)
  30        self.god.stub_with(net_utils, 'netif', net_utils_mock.netutils_netif)
  31
  32        os.environ['AUTODIR'] = "autodir"
  33
  34
  35    def tearDown(self):
  36        self.god.unstub_all()
  37        del os.environ['AUTODIR']
  38
  39
  40    #
  41    # test network_util
  42    #
  43    def test_network_util_reset(self):
  44        utils.system.expect_call('service network restart', ignore_status=False)
  45        net_utils.network_utils().reset()
  46        self.god.check_playback()
  47
  48
  49    def test_network_util_start(self):
  50        utils.system.expect_call('service network start', ignore_status=False)
  51
  52        net_utils.network_utils().start()
  53        self.god.check_playback()
  54
  55
  56    def test_network_util_stop(self):
  57        utils.system.expect_call('service network stop', ignore_status=False)
  58
  59        net_utils.network_utils().stop()
  60        self.god.check_playback()
  61
  62
  63    def test_network_util_disable_ip_local_loopback(self):
  64        msg = "echo '1' > /proc/sys/net/ipv4/route/no_local_loopback"
  65        utils.system.expect_call(msg, ignore_status=False)
  66        msg = 'echo 1 > /proc/sys/net/ipv4/route/flush'
  67        utils.system.expect_call(msg, ignore_status=False)
  68
  69        net_utils.network_utils().disable_ip_local_loopback()
  70        self.god.check_playback()
  71
  72
  73    def test_network_util_enable_ip_local_loopback(self):
  74        msg = "echo '0' > /proc/sys/net/ipv4/route/no_local_loopback"
  75        utils.system.expect_call(msg, ignore_status=False)
  76        msg = 'echo 1 > /proc/sys/net/ipv4/route/flush'
  77        utils.system.expect_call(msg, ignore_status=False)
  78
  79        net_utils.network_utils().enable_ip_local_loopback()
  80        self.god.check_playback()
  81
  82
  83    #
  84    # test network_interface
  85    #
  86    def test_network_interface_init(self):
  87        self.god.stub_function(socket, 'socket')
  88        s = net_utils_mock.socket_stub('eth0', socket, socket)
  89        socket.socket.expect_call(socket.PF_PACKET,
  90                                  socket.SOCK_RAW).and_return(s)
  91        self.god.stub_function(s, 'bind')
  92        self.god.stub_function(s, 'settimeout')
  93        s.settimeout.expect_call(net_utils.TIMEOUT)
  94        s.bind.expect_call(('eth0', net_utils.raw_socket.ETH_P_ALL))
  95        mock_netif = self.network_interface_mock(iface='eth0', test_init=True)
  96        self.god.check_playback()
  97        self.assertEquals(mock_netif.ethtool, 'ethtool')
  98        self.assertEquals(mock_netif._name, 'eth0')
  99        self.assertEquals(mock_netif.was_down, 'is_down')
 100        self.assertEquals(mock_netif.orig_ipaddr, 'get_ipaddr')
 101        self.assertEquals(mock_netif.was_loopback_enabled,
 102                          'is_loopback_enabled')
 103        self.assertEquals(mock_netif._socket, s)
 104
 105
 106    def test_network_interface_restore(self):
 107        mock_netif = self.network_interface_mock('eth0')
 108
 109        mock_netif.was_loopback_enabled = False
 110        mock_netif.loopback_enabled = True
 111        mock_netif.was_down = False
 112
 113        # restore using phyint
 114        cmd = 'ifconfig %s %s' % (mock_netif._name, mock_netif.orig_ipaddr)
 115        utils.system.expect_call(cmd)
 116
 117        cmd = '%s -L %s %s %s' % (mock_netif.ethtool, mock_netif._name,
 118                                  'phyint', 'disable')
 119        utils.system.expect_call(cmd, ignore_status=True).and_return(0)
 120        mock_netif.restore()
 121        self.god.check_playback()
 122
 123
 124        # restore using mac
 125        cmd = 'ifconfig %s %s' % (mock_netif._name, mock_netif.orig_ipaddr)
 126        utils.system.expect_call(cmd)
 127
 128        cmd = '%s -L %s %s %s' % (mock_netif.ethtool, mock_netif._name,
 129                                  'phyint', 'disable')
 130        utils.system.expect_call(cmd, ignore_status=True).and_return(1)
 131
 132        cmd = '%s -L %s %s %s' % (mock_netif.ethtool,
 133                                  mock_netif._name, 'mac', 'disable')
 134        utils.system.expect_call(cmd, ignore_status=True).and_return(0)
 135        mock_netif.restore()
 136        self.god.check_playback()
 137
 138        # check that down is restored
 139        mock_netif.was_loopback_enabled = False
 140        mock_netif.loopback_enabled = True
 141        mock_netif.was_down = True
 142
 143        cmd = 'ifconfig %s %s' % (mock_netif._name, mock_netif.orig_ipaddr)
 144        utils.system.expect_call(cmd)
 145
 146        cmd = '%s -L %s %s %s' % (mock_netif.ethtool, mock_netif._name,
 147                                  'phyint', 'disable')
 148        utils.system.expect_call(cmd, ignore_status=True).and_return(0)
 149
 150        cmd = 'ifconfig %s down' % mock_netif._name
 151        utils.system.expect_call(cmd)
 152
 153        mock_netif.restore()
 154        self.god.check_playback()
 155
 156        # check that loopback, down are done in sequence
 157        mock_netif.was_loopback_enabled = True
 158        mock_netif.loopback_enabled = True
 159        mock_netif.was_down = True
 160
 161        cmd = 'ifconfig %s %s' % (mock_netif._name,
 162                                  mock_netif.orig_ipaddr)
 163
 164        utils.system.expect_call(cmd)
 165        cmd = 'ifconfig %s down' % mock_netif._name
 166        utils.system.expect_call(cmd)
 167
 168        mock_netif.restore()
 169        self.god.check_playback()
 170
 171        # prior loopback matches current loopback
 172        mock_netif.was_loopback_enabled = False
 173        mock_netif.loopback_enabled = False
 174        mock_netif.was_down = True
 175
 176        cmd = 'ifconfig %s %s' % (mock_netif._name,
 177                                  mock_netif.orig_ipaddr)
 178        utils.system.expect_call(cmd)
 179        cmd = 'ifconfig %s down' % mock_netif._name
 180        utils.system.expect_call(cmd)
 181
 182        mock_netif.restore()
 183        self.god.check_playback()
 184
 185
 186    def test_network_interface_get_name(self):
 187        mock_netif = self.network_interface_mock(iface='eth0')
 188        self.assertEquals(mock_netif.get_name(), 'eth0')
 189
 190
 191    def test_network_interface_parse_ethtool(self):
 192        mock_netif = self.network_interface_mock()
 193        cmd = '%s %s %s' % (mock_netif.ethtool, '', mock_netif._name)
 194
 195        utils.system_output.expect_call(cmd).and_return('\n field: match')
 196        self.assertEquals(mock_netif.parse_ethtool('field', 'some|match'),
 197                          'match')
 198
 199        self.god.check_playback()
 200
 201        utils.system_output.expect_call(cmd).and_return(None)
 202        self.assertEquals(mock_netif.parse_ethtool('field',
 203                                                   'some|match'), '')
 204
 205        utils.system_output.expect_call(cmd).and_return(' field: match')
 206        self.assertEquals(mock_netif.parse_ethtool('field',
 207                                                   'some|match'), '')
 208        self.god.check_playback()
 209
 210
 211    def test_network_interface_get_stats(self):
 212        mock_netif = self.network_interface_mock()
 213        self.god.stub_function(os, 'listdir')
 214        stat_path = '/sys/class/net/%s/statistics/' % mock_netif._name
 215
 216        # no stat found
 217        os.listdir.expect_call(stat_path).and_return(())
 218        self.assertEquals(mock_netif.get_stats(), {})
 219        self.god.check_playback()
 220
 221        # can not open stat file
 222        os.listdir.expect_call(stat_path).and_return(('some_stat',))
 223        f = self.god.create_mock_class(file, 'file')
 224        net_utils.open.expect_call(stat_path + 'some_stat', 'r').and_return(None)
 225        self.assertEquals(mock_netif.get_stats(), {})
 226        self.god.check_playback()
 227
 228        # found a single stat
 229        os.listdir.expect_call(stat_path).and_return(('some_stat',))
 230        f = self.god.create_mock_class(file, 'file')
 231        net_utils.open.expect_call(stat_path + 'some_stat', 'r').and_return(f)
 232        f.read.expect_call().and_return(1234)
 233        f.close.expect_call()
 234        self.assertEquals(mock_netif.get_stats(), {'some_stat':1234})
 235        self.god.check_playback()
 236
 237        # found multiple stats
 238        os.listdir.expect_call(stat_path).and_return(('stat1','stat2'))
 239        f = self.god.create_mock_class(file, 'file')
 240        net_utils.open.expect_call(stat_path + 'stat1', 'r').and_return(f)
 241        f.read.expect_call().and_return(1234)
 242        f.close.expect_call()
 243        net_utils.open.expect_call(stat_path + 'stat2', 'r').and_return(f)
 244        f.read.expect_call().and_return(5678)
 245        f.close.expect_call()
 246
 247        self.assertEquals(mock_netif.get_stats(), {'stat1':1234, 'stat2':5678})
 248        self.god.check_playback()
 249
 250
 251    def test_network_interface_get_stats_diff(self):
 252        mock_netif = self.network_interface_mock()
 253        self.god.stub_function(os, 'listdir')
 254        stat_path = '/sys/class/net/%s/statistics/' % mock_netif._name
 255
 256        os.listdir.expect_call(stat_path).and_return(('stat1', 'stat2', 'stat4'))
 257        f = self.god.create_mock_class(file, 'file')
 258        net_utils.open.expect_call(stat_path + 'stat1', 'r').and_return(f)
 259        f.read.expect_call().and_return(1234)
 260        f.close.expect_call()
 261        net_utils.open.expect_call(stat_path + 'stat2', 'r').and_return(f)
 262        f.read.expect_call().and_return(0)
 263        f.close.expect_call()
 264        net_utils.open.expect_call(stat_path + 'stat4', 'r').and_return(f)
 265        f.read.expect_call().and_return(10)
 266        f.close.expect_call()
 267        self.assertEquals(mock_netif.get_stats_diff({'stat1':1, 'stat2':2,
 268                                                     'stat3':0}),
 269                          {'stat1':1233, 'stat2':-2, 'stat4':10})
 270        self.god.check_playback()
 271
 272
 273    def test_network_interface_get_driver(self):
 274        mock_netif = self.network_interface_mock()
 275        mock_netif.get_driver = net_utils.network_interface.get_driver
 276        self.god.stub_function(os, 'readlink')
 277        stat_path = '/sys/class/net/%s/device/driver' % mock_netif._name
 278        os.readlink.expect_call(stat_path).and_return((
 279                                                  stat_path+'/driver_name'))
 280        self.assertEquals(mock_netif.get_driver(mock_netif), 'driver_name')
 281        self.god.check_playback()
 282
 283
 284    def test_network_interface_get_carrier(self):
 285        mock_netif = self.network_interface_mock()
 286        self.god.stub_function(os, 'readlink')
 287        stat_path = '/sys/class/net/%s/carrier' % mock_netif._name
 288        f = self.god.create_mock_class(file, 'file')
 289        net_utils.open.expect_call(stat_path).and_return(f)
 290        f.read.expect_call().and_return(' 1 ')
 291        f.close.expect_call()
 292        self.assertEquals(mock_netif.get_carrier(), '1')
 293        self.god.check_playback()
 294
 295        net_utils.open.expect_call(stat_path).and_return(f)
 296        f.read.expect_call().and_return(' 0 ')
 297        f.close.expect_call()
 298        self.assertEquals(mock_netif.get_carrier(), '0')
 299        self.god.check_playback()
 300
 301        net_utils.open.expect_call(stat_path).and_return(f)
 302        f.read.expect_call().and_return('')
 303        f.close.expect_call()
 304        self.assertEquals(mock_netif.get_carrier(), '')
 305        self.god.check_playback()
 306
 307        net_utils.open.expect_call(stat_path).and_return(None)
 308        self.assertEquals(mock_netif.get_carrier(), '')
 309        self.god.check_playback()
 310
 311
 312    def test_network_interface_is_autoneg_advertised(self):
 313        mock_netif = self.network_interface_mock()
 314        cmd = '%s %s %s' % (mock_netif.ethtool, '', mock_netif._name)
 315
 316        utils.system_output.expect_call(cmd).and_return(
 317            '\n Advertised auto-negotiation: Yes')
 318        self.assertEquals(mock_netif.is_autoneg_advertised(), True)
 319        self.god.check_playback()
 320
 321        utils.system_output.expect_call(cmd).and_return(
 322            '\n Advertised auto-negotiation: No')
 323        self.assertEquals(mock_netif.is_autoneg_advertised(), False)
 324        self.god.check_playback()
 325
 326        utils.system_output.expect_call(cmd).and_return('')
 327        self.assertEquals(mock_netif.is_autoneg_advertised(), False)
 328        self.god.check_playback()
 329
 330
 331    def test_network_interface_get_speed(self):
 332        mock_netif = self.network_interface_mock()
 333        cmd = '%s %s %s' % (mock_netif.ethtool, '', mock_netif._name)
 334
 335        utils.system_output.expect_call(cmd).and_return(
 336            '\n Speed: 1000')
 337        self.assertEquals(mock_netif.get_speed(), 1000)
 338        self.god.check_playback()
 339
 340        utils.system_output.expect_call(cmd).and_return(
 341            '\n Speed: 10000')
 342        self.assertEquals(mock_netif.get_speed(), 10000)
 343        self.god.check_playback()
 344
 345        utils.system_output.expect_call(cmd).and_return('')
 346
 347        try:
 348            mock_netif.get_speed()
 349        except ValueError:
 350            pass
 351        else:
 352            self.assertEquals(0, 1)
 353        self.god.check_playback()
 354
 355
 356    def test_network_interface_is_full_duplex(self):
 357        mock_netif = self.network_interface_mock()
 358        cmd = '%s %s %s' % (mock_netif.ethtool, '', mock_netif._name)
 359
 360        utils.system_output.expect_call(cmd).and_return(
 361            '\n Duplex: Full')
 362        self.assertEquals(mock_netif.is_full_duplex(), True)
 363        self.god.check_playback()
 364
 365        utils.system_output.expect_call(cmd).and_return(
 366            '\n Duplex: Half')
 367        self.assertEquals(mock_netif.is_full_duplex(), False)
 368        self.god.check_playback()
 369
 370        utils.system_output.expect_call(cmd).and_return('')
 371        self.assertEquals(mock_netif.is_full_duplex(), False)
 372        self.god.check_playback()
 373
 374
 375    def test_network_interface_is_autoneg_on(self):
 376        mock_netif = self.network_interface_mock()
 377        cmd = '%s %s %s' % (mock_netif.ethtool, '', mock_netif._name)
 378
 379        utils.system_output.expect_call(cmd).and_return(
 380            '\n Auto-negotiation: on')
 381        self.assertEquals(mock_netif.is_autoneg_on(), True)
 382        self.god.check_playback()
 383
 384        utils.system_output.expect_call(cmd).and_return(
 385            '\n Auto-negotiation: off')
 386        self.assertEquals(mock_netif.is_autoneg_on(), False)
 387        self.god.check_playback()
 388
 389        utils.system_output.expect_call(cmd).and_return('')
 390        self.assertEquals(mock_netif.is_autoneg_on(), False)
 391        self.god.check_playback()
 392
 393
 394    def test_network_interface_get_wakeon(self):
 395        mock_netif = self.network_interface_mock()
 396        cmd = '%s %s %s' % (mock_netif.ethtool, '', mock_netif._name)
 397
 398        utils.system_output.expect_call(cmd).and_return(
 399            '\n Wake-on: g')
 400        self.assertEquals(mock_netif.get_wakeon(), 'g')
 401        self.god.check_playback()
 402
 403
 404    def test_network_interface_is_rx_summing_on(self):
 405        mock_netif = self.network_interface_mock()
 406        cmd = '%s %s %s' % (mock_netif.ethtool, '-k', mock_netif._name)
 407
 408        utils.system_output.expect_call(cmd).and_return(
 409            '\n rx-checksumming: on')
 410        self.assertEquals(mock_netif.is_rx_summing_on(), True)
 411        self.god.check_playback()
 412
 413        utils.system_output.expect_call(cmd).and_return(
 414            '\n rx-checksumming: off')
 415        self.assertEquals(mock_netif.is_rx_summing_on(), False)
 416        self.god.check_playback()
 417
 418        utils.system_output.expect_call(cmd).and_return('')
 419        self.assertEquals(mock_netif.is_rx_summing_on(), False)
 420        self.god.check_playback()
 421
 422
 423    def test_network_interface_is_tx_summing_on(self):
 424        mock_netif = self.network_interface_mock()
 425        cmd = '%s %s %s' % (mock_netif.ethtool, '-k', mock_netif._name)
 426
 427        utils.system_output.expect_call(cmd).and_return(
 428            '\n tx-checksumming: on')
 429        self.assertEquals(mock_netif.is_tx_summing_on(), True)
 430        self.god.check_playback()
 431
 432        utils.system_output.expect_call(cmd).and_return(
 433            '\n tx-checksumming: off')
 434        self.assertEquals(mock_netif.is_tx_summing_on(), False)
 435        self.god.check_playback()
 436
 437        utils.system_output.expect_call(cmd).and_return('')
 438        self.assertEquals(mock_netif.is_tx_summing_on(), False)
 439        self.god.check_playback()
 440
 441
 442    def test_network_interface_is_scatter_gather_on(self):
 443        mock_netif = self.network_interface_mock()
 444        cmd = '%s %s %s' % (mock_netif.ethtool, '-k', mock_netif._name)
 445
 446        utils.system_output.expect_call(cmd).and_return(
 447            '\n scatter-gather: on')
 448        self.assertEquals(mock_netif.is_scatter_gather_on(), True)
 449        self.god.check_playback()
 450
 451        utils.system_output.expect_call(cmd).and_return(
 452            '\n scatter-gather: off')
 453        self.assertEquals(mock_netif.is_scatter_gather_on(), False)
 454        self.god.check_playback()
 455
 456        utils.system_output.expect_call(cmd).and_return('')
 457        self.assertEquals(mock_netif.is_scatter_gather_on(), False)
 458        self.god.check_playback()
 459
 460
 461    def test_network_interface_is_tso_on(self):
 462        mock_netif = self.network_interface_mock()
 463        cmd = '%s %s %s' % (mock_netif.ethtool, '-k', mock_netif._name)
 464
 465        utils.system_output.expect_call(cmd).and_return(
 466            '\n tcp segmentation offload: on')
 467        self.assertEquals(mock_netif.is_tso_on(), True)
 468        self.god.check_playback()
 469
 470        utils.system_output.expect_call(cmd).and_return(
 471            '\n tcp segmentation offload: off')
 472        self.assertEquals(mock_netif.is_tso_on(), False)
 473        self.god.check_playback()
 474
 475        utils.system_output.expect_call(cmd).and_return('')
 476        self.assertEquals(mock_netif.is_tso_on(), False)
 477        self.god.check_playback()
 478
 479
 480    def test_network_interface_is_pause_autoneg_on(self):
 481        mock_netif = self.network_interface_mock()
 482        cmd = '%s %s %s' % (mock_netif.ethtool, '-a', mock_netif._name)
 483
 484        utils.system_output.expect_call(cmd).and_return(
 485            '\n Autonegotiate: on')
 486        self.assertEquals(mock_netif.is_pause_autoneg_on(), True)
 487        self.god.check_playback()
 488
 489        utils.system_output.expect_call(cmd).and_return(
 490            '\n Autonegotiate: off')
 491        self.assertEquals(mock_netif.is_pause_autoneg_on(), False)
 492        self.god.check_playback()
 493
 494        utils.system_output.expect_call(cmd).and_return('')
 495        self.assertEquals(mock_netif.is_pause_autoneg_on(), False)
 496        self.god.check_playback()
 497
 498
 499    def test_network_interface_is_tx_pause_on(self):
 500        mock_netif = self.network_interface_mock()
 501        cmd = '%s %s %s' % (mock_netif.ethtool, '-a', mock_netif._name)
 502
 503        utils.system_output.expect_call(cmd).and_return(
 504            '\n TX: on')
 505        self.assertEquals(mock_netif.is_tx_pause_on(), True)
 506        self.god.check_playback()
 507
 508        utils.system_output.expect_call(cmd).and_return(
 509            '\n TX: off')
 510        self.assertEquals(mock_netif.is_tx_pause_on(), False)
 511        self.god.check_playback()
 512
 513        utils.system_output.expect_call(cmd).and_return('')
 514        self.assertEquals(mock_netif.is_tx_pause_on(), False)
 515        self.god.check_playback()
 516
 517
 518    def test_network_interface_is_rx_pause_on(self):
 519        mock_netif = self.network_interface_mock()
 520        cmd = '%s %s %s' % (mock_netif.ethtool, '-a', mock_netif._name)
 521
 522        utils.system_output.expect_call(cmd).and_return(
 523            '\n RX: on')
 524        self.assertEquals(mock_netif.is_rx_pause_on(), True)
 525        self.god.check_playback()
 526
 527        utils.system_output.expect_call(cmd).and_return(
 528            '\n RX: off')
 529        self.assertEquals(mock_netif.is_rx_pause_on(), False)
 530        self.god.check_playback()
 531
 532        utils.system_output.expect_call(cmd).and_return('')
 533        self.assertEquals(mock_netif.is_rx_pause_on(), False)
 534        self.god.check_playback()
 535
 536
 537    def test_network_interface_enable_loopback(self):
 538        mock_netif = self.network_interface_mock('eth0')
 539
 540        mock_netif.was_loopback_enabled = False
 541        mock_netif.loopback_enabled = False
 542        mock_netif.was_down = False
 543
 544        self.god.stub_function(net_utils.bonding, 'is_enabled')
 545
 546        # restore using phyint
 547        net_utils.bonding.is_enabled.expect_call().and_return(False)
 548        cmd = '%s -L %s %s %s' % (mock_netif.ethtool, mock_netif._name,
 549                                  'phyint', 'enable')
 550        utils.system.expect_call(cmd, ignore_status=True).and_return(0)
 551        mock_netif.enable_loopback()
 552        self.god.check_playback()
 553
 554        # restore using mac
 555        net_utils.bonding.is_enabled.expect_call().and_return(False)
 556        cmd = '%s -L %s %s %s' % (mock_netif.ethtool, mock_netif._name,
 557                                  'phyint', 'enable')
 558        utils.system.expect_call(cmd, ignore_status=True).and_return(1)
 559        cmd = '%s -L %s %s %s' % (mock_netif.ethtool, mock_netif._name,
 560                                  'mac', 'enable')
 561        utils.system.expect_call(cmd, ignore_status=True).and_return(0)
 562        mock_netif.enable_loopback()
 563        self.god.check_playback()
 564
 565        # catch exception on phyint and mac failures
 566        net_utils.bonding.is_enabled.expect_call().and_return(False)
 567        cmd = '%s -L %s %s %s' % (mock_netif.ethtool, mock_netif._name,
 568                                  'phyint', 'enable')
 569        utils.system.expect_call(cmd, ignore_status=True).and_return(1)
 570        cmd = '%s -L %s %s %s' % (mock_netif.ethtool, mock_netif._name,
 571                                  'mac', 'enable')
 572        utils.system.expect_call(cmd, ignore_status=True).and_return(1)
 573        try:
 574            mock_netif.enable_loopback()
 575        except error.TestError:
 576            pass
 577        else:
 578            self.assertEquals(0, 1)
 579        self.god.check_playback()
 580
 581        # catch exception on bond enabled
 582        net_utils.bonding.is_enabled.expect_call().and_return(True)
 583        try:
 584            mock_netif.enable_loopback()
 585        except error.TestError:
 586            pass
 587        else:
 588            self.assertEquals(0, 1)
 589        self.god.check_playback()
 590
 591        # check that setting tg3 and bnx2x driver have a sleep call
 592        mock_netif.driver = 'tg3'
 593        net_utils.bonding.is_enabled.expect_call().and_return(False)
 594        cmd = '%s -L %s %s %s' % (mock_netif.ethtool, mock_netif._name,
 595                                  'phyint', 'enable')
 596        utils.system.expect_call(cmd, ignore_status=True).and_return(0)
 597        time.sleep.expect_call(1)
 598        mock_netif.enable_loopback()
 599        self.god.check_playback()
 600
 601        mock_netif.driver = 'bnx2x'
 602        net_utils.bonding.is_enabled.expect_call().and_return(False)
 603        cmd = '%s -L %s %s %s' % (mock_netif.ethtool, mock_netif._name,
 604                                  'phyint', 'enable')
 605        utils.system.expect_call(cmd, ignore_status=True).and_return(0)
 606        time.sleep.expect_call(1)
 607        mock_netif.enable_loopback()
 608        self.god.check_playback()
 609
 610
 611    def test_network_interface_disable_loopback(self):
 612        mock_netif = self.network_interface_mock('eth0')
 613
 614        mock_netif.was_loopback_enabled = False
 615        mock_netif.loopback_enabled = True
 616        mock_netif.was_down = False
 617
 618        # restore using phyint
 619        cmd = '%s -L %s %s %s' % (mock_netif.ethtool, mock_netif._name,
 620                                  'phyint', 'disable')
 621        utils.system.expect_call(cmd, ignore_status=True).and_return(0)
 622        mock_netif.disable_loopback()
 623        self.god.check_playback()
 624
 625        # restore using mac
 626        cmd = '%s -L %s %s %s' % (mock_netif.ethtool, mock_netif._name,
 627                                  'phyint', 'disable')
 628        utils.system.expect_call(cmd, ignore_status=True).and_return(1)
 629        cmd = '%s -L %s %s %s' % (mock_netif.ethtool, mock_netif._name,
 630                                  'mac', 'disable')
 631        utils.system.expect_call(cmd, ignore_status=True).and_return(0)
 632        mock_netif.disable_loopback()
 633        self.god.check_playback()
 634
 635        # catch exception on phyint and mac failures
 636        cmd = '%s -L %s %s %s' % (mock_netif.ethtool, mock_netif._name,
 637                                  'phyint', 'disable')
 638        utils.system.expect_call(cmd, ignore_status=True).and_return(1)
 639        cmd = '%s -L %s %s %s' % (mock_netif.ethtool, mock_netif._name,
 640                                  'mac', 'disable')
 641        utils.system.expect_call(cmd, ignore_status=True).and_return(1)
 642        try:
 643            mock_netif.disable_loopback()
 644        except error.TestError:
 645            pass
 646        else:
 647            self.assertEquals(0, 1)
 648        self.god.check_playback()
 649
 650
 651    def test_network_interface_is_loopback_enabled(self):
 652        mock_netif = self.network_interface_mock('eth0')
 653        mock_netif.is_loopback_enabled = \
 654            net_utils.network_interface.is_loopback_enabled
 655        try:
 656            mock_netif.is_loopback_enabled(mock_netif)
 657        except error.TestError:
 658            pass
 659        else:
 660            self.assertEquals(0, 1)
 661        self.god.check_playback()
 662
 663        self.god.stub_function(net_utils.bonding, 'is_enabled')
 664        mock_netif._name = 'eth0'
 665        net_utils.bonding.is_enabled.expect_call().and_return(False)
 666        cmd = '%s -l %s' % (mock_netif.ethtool, mock_netif._name)
 667        utils.system_output.expect_call(cmd).and_return('')
 668        self.assertEquals(mock_netif.is_loopback_enabled(mock_netif), False)
 669        self.god.check_playback()
 670
 671        for ifname in ('eth0', 'eth1', 'eth2', 'eth3', 'eth4'):
 672            mock_netif._name = ifname
 673            for bond_enable in (True, False):
 674                for state in (('disabled', 'disabled', 'enabled'),
 675                              ('disabled', 'enabled', 'disabled'),
 676                              ('enabled', 'disabled', 'disabled'),
 677                              ('disabled', 'disabled', 'disabled')):
 678                    net_utils.bonding.is_enabled.expect_call().and_return(
 679                        bond_enable)
 680                    if bond_enable:
 681                        self.assertEquals(mock_netif.is_loopback_enabled(
 682                            mock_netif), False)
 683                    else:
 684                        cmd = '%s -l %s' % (mock_netif.ethtool, mock_netif._name)
 685                        out = 'MAC loopback is %s\n'\
 686                              'PHY internal loopback is %s\n'\
 687                              'PHY external loopback is %s' % (
 688                            state[0], state[1], state[2])
 689                        utils.system_output.expect_call(cmd).and_return(out)
 690                        self.assertEquals(mock_netif.is_loopback_enabled(
 691                            mock_netif), 'enabled' in state)
 692                    self.god.check_playback()
 693
 694
 695    def test_network_interface_enable_promisc(self):
 696        mock_netif = self.network_interface_mock('eth0')
 697        cmd = 'ifconfig %s promisc' % mock_netif._name
 698        utils.system.expect_call(cmd)
 699        mock_netif.enable_promisc()
 700        self.god.check_playback()
 701
 702
 703    def test_network_interface_disable_promisc(self):
 704        mock_netif = self.network_interface_mock()
 705        cmd = 'ifconfig %s -promisc' % mock_netif._name
 706        utils.system.expect_call(cmd)
 707        mock_netif.disable_promisc()
 708        self.god.check_playback()
 709
 710
 711    def test_network_interface_get_hwaddr(self):
 712        mock_netif = self.network_interface_mock()
 713        f = self.god.create_mock_class(file, 'file')
 714        net_utils.open.expect_call('/sys/class/net/%s/address'
 715                                       % mock_netif._name).and_return(f)
 716        hw_addr = '00:0e:0c:c3:7d:a8'
 717        f.read.expect_call().and_return(' ' + hw_addr + ' ')
 718        f.close.expect_call()
 719        self.assertEquals(mock_netif.get_hwaddr(), hw_addr)
 720        self.god.check_playback()
 721
 722
 723    def test_network_interface_set_hwaddr(self):
 724        mock_netif = self.network_interface_mock()
 725        hw_addr = '00:0e:0c:c3:7d:a8'
 726        cmd = 'ifconfig %s hw ether %s' % (mock_netif._name,
 727                                           hw_addr)
 728        utils.system.expect_call(cmd)
 729        mock_netif.set_hwaddr(hw_addr)
 730        self.god.check_playback()
 731
 732
 733    def test_network_interface_add_maddr(self):
 734        mock_netif = self.network_interface_mock()
 735        maddr = '01:00:5e:00:00:01'
 736        cmd = 'ip maddr add %s dev %s' % (maddr, mock_netif._name)
 737        utils.system.expect_call(cmd)
 738        mock_netif.add_maddr(maddr)
 739        self.god.check_playback()
 740
 741
 742    def test_network_interface_del_maddr(self):
 743        mock_netif = self.network_interface_mock()
 744        maddr = '01:00:5e:00:00:01'
 745        cmd = 'ip maddr del %s dev %s' % (maddr, mock_netif._name)
 746        utils.system.expect_call(cmd)
 747        mock_netif.del_maddr(maddr)
 748        self.god.check_playback()
 749
 750
 751    def test_network_interface_get_ipaddr(self):
 752        mock_netif = self.network_interface_mock()
 753        ip_addr = '110.211.112.213'
 754        out_format = \
 755          'eth0      Link encap:Ethernet  HWaddr 00:0E:0C:C3:7D:A8\n'\
 756          '          inet addr:%s  Bcast:10.246.90.255'\
 757          ' Mask:255.255.255.0\n'\
 758          '          UP BROADCAST RUNNING MASTER MULTICAST  MTU:1500'\
 759          ' Metric:1\n'\
 760          '          RX packets:463070 errors:0 dropped:0 overruns:0'\
 761          ' frame:0\n'\
 762          '          TX packets:32548 errors:0 dropped:0 overruns:0'\
 763          ' carrier:0\n'\
 764          '          collisions:0 txqueuelen:0'
 765        out = out_format % ip_addr
 766
 767        cmd = 'ifconfig %s' % mock_netif._name
 768        utils.system_output.expect_call(cmd).and_return(out)
 769        self.assertEquals(mock_netif.get_ipaddr(), ip_addr)
 770        self.god.check_playback()
 771
 772        cmd = 'ifconfig %s' % mock_netif._name
 773        utils.system_output.expect_call(cmd).and_return('some output')
 774        self.assertEquals(mock_netif.get_ipaddr(), '0.0.0.0')
 775        self.god.check_playback()
 776
 777        cmd = 'ifconfig %s' % mock_netif._name
 778        utils.system_output.expect_call(cmd).and_return(None)
 779        self.assertEquals(mock_netif.get_ipaddr(), '0.0.0.0')
 780        self.god.check_playback()
 781
 782        ip_addr = '1.2.3.4'
 783        out = out_format % ip_addr
 784        cmd = 'ifconfig %s' % mock_netif._name
 785        utils.system_output.expect_call(cmd).and_return(out)
 786        self.assertEquals(mock_netif.get_ipaddr(), ip_addr)
 787        self.god.check_playback()
 788
 789
 790    def test_network_interface_set_ipaddr(self):
 791        mock_netif = self.network_interface_mock()
 792        ip_addr = '1.2.3.4'
 793        cmd = 'ifconfig %s %s' % (mock_netif._name, ip_addr)
 794        utils.system.expect_call(cmd)
 795        mock_netif.set_ipaddr(ip_addr)
 796        self.god.check_playback()
 797
 798
 799    def test_network_interface_is_down(self):
 800        mock_netif = self.network_interface_mock()
 801        out_format = \
 802          'eth0      Link encap:Ethernet  HWaddr 00:0E:0C:C3:7D:A8\n'\
 803          '          inet addr:1.2.3.4  Bcast:10.246.90.255'\
 804          ' Mask:255.255.255.0\n'\
 805          '          %s BROADCAST RUNNING MASTER MULTICAST  MTU:1500'\
 806          ' Metric:1\n'\
 807          '          RX packets:463070 errors:0 dropped:0 overruns:0'\
 808          ' frame:0\n'\
 809          '          TX packets:32548 errors:0 dropped:0 overruns:0'\
 810          ' carrier:0\n'\
 811          '          collisions:0 txqueuelen:0'
 812        for state in ('UP', 'DOWN', 'NONE', ''):
 813            out = out_format % state
 814            cmd = 'ifconfig %s' % mock_netif._name
 815            utils.system_output.expect_call(cmd).and_return(out)
 816            self.assertEquals(mock_netif.is_down(), state != 'UP')
 817            self.god.check_playback()
 818
 819        cmd = 'ifconfig %s' % mock_netif._name
 820        utils.system_output.expect_call(cmd).and_return(None)
 821        self.assertEquals(mock_netif.is_down(), False)
 822        self.god.check_playback()
 823
 824
 825    def test_network_interface_up(self):
 826        mock_netif = self.network_interface_mock()
 827        cmd = 'ifconfig %s up' % mock_netif._name
 828        utils.system.expect_call(cmd)
 829        mock_netif.up()
 830        self.god.check_playback()
 831
 832
 833    def test_network_interface_down(self):
 834        mock_netif = self.network_interface_mock()
 835        cmd = 'ifconfig %s down' % mock_netif._name
 836        utils.system.expect_call(cmd)
 837        mock_netif.down()
 838        self.god.check_playback()
 839
 840
 841    def test_network_interface_wait_for_carrier(self):
 842        mock_netif = self.network_interface_mock()
 843        mock_netif.wait_for_carrier = \
 844                             net_utils.network_interface.wait_for_carrier
 845        f = self.god.create_mock_class(file, 'file')
 846        spath = '/sys/class/net/%s/carrier' % mock_netif._name
 847        # y = 0 - test that an exception is thrown
 848        # y = 1, 100 - check that carrier is checked until timeout
 849        for y in (0, 1, 100):
 850            max_timeout = y
 851            if y:
 852                for x in xrange(max_timeout - 1):
 853                    net_utils.open.expect_call(spath).and_return(f)
 854                    f.read.expect_call().and_return(' ' + '0' + ' ')
 855                    f.close.expect_call()
 856                    time.sleep.expect_call(1)
 857
 858                net_utils.open.expect_call(spath).and_return(f)
 859                f.read.expect_call().and_return(' ' + '1' + ' ')
 860                f.close.expect_call()
 861            try:
 862                mock_netif.wait_for_carrier(mock_netif, max_timeout)
 863            except Exception:
 864                pass
 865            else:
 866                if not y:
 867                    self.assertEquals(0, 1)
 868            self.god.check_playback()
 869
 870
 871    def test_network_interface_send(self):
 872        mock_netif = self.network_interface_mock()
 873        mock_netif.send('test buffer')
 874        self.assertEquals(mock_netif._socket.send_val, 'test buffer')
 875
 876
 877    def test_network_interface_recv(self):
 878        mock_netif = self.network_interface_mock()
 879        test_str = 'test string'
 880        mock_netif._socket.recv_val = test_str
 881        rcv_str = mock_netif.recv(len(test_str))
 882        self.assertEquals(rcv_str, test_str)
 883
 884
 885    def test_network_interface_flush(self):
 886        mock_netif = self.network_interface_mock()
 887        self.god.stub_function(mock_netif._socket, 'close')
 888        mock_netif._socket.close.expect_call()
 889        s = self.god.create_mock_class(socket.socket, "socket")
 890        self.god.stub_function(socket, 'socket')
 891        socket.socket.expect_call(socket.PF_PACKET,
 892                                  socket.SOCK_RAW).and_return(s)
 893        s.settimeout.expect_call(net_utils.TIMEOUT)
 894        s.bind.expect_call(('eth0', net_utils.raw_socket.ETH_P_ALL))
 895
 896
 897    #
 898    # bonding tests
 899    #
 900    def test_bonding_is_enabled(self):
 901        try:
 902            net_utils.bond().is_enabled()
 903        except error.TestError:
 904            pass
 905        else:
 906            self.assertEquals(1, 0)
 907
 908
 909    def test_bonding_is_bondable(self):
 910        try:
 911            net_utils.bond().is_enabled()
 912        except error.TestError:
 913            pass
 914        else:
 915            self.assertEquals(1, 0)
 916
 917
 918    def test_bonding_enable(self):
 919        try:
 920            net_utils.bond().is_enabled()
 921        except error.TestError:
 922            pass
 923        else:
 924            self.assertEquals(1, 0)
 925
 926
 927    def test_bonding_disable(self):
 928        try:
 929            net_utils.bond().is_enabled()
 930        except error.TestError:
 931            pass
 932        else:
 933            self.assertEquals(1, 0)
 934
 935
 936    def test_bonding_get_mii_status(self):
 937        self.assertEquals(net_utils.bond().get_mii_status(), {})
 938
 939
 940    def test_get_mode_bonding(self):
 941        self.assertEquals(net_utils.bond().get_mode(), net_utils.bonding.NO_MODE)
 942
 943
 944    def test_bonding_wait_for_state_change(self):
 945        self.god.stub_function(utils, "ping_default_gateway")
 946
 947        time.sleep.expect_call(10)
 948        utils.ping_default_gateway.expect_call().and_return(False)
 949        self.assertEquals(net_utils.bond().wait_for_state_change(), True)
 950
 951        for x in xrange(9):
 952            time.sleep.expect_call(10)
 953            utils.ping_default_gateway.expect_call().and_return(True)
 954
 955        time.sleep.expect_call(10)
 956        utils.ping_default_gateway.expect_call().and_return(False)
 957        self.assertEquals(net_utils.bond().wait_for_state_change(), True)
 958
 959        for x in xrange(10):
 960            time.sleep.expect_call(10)
 961            utils.ping_default_gateway.expect_call().and_return(True)
 962
 963        self.assertEquals(net_utils.bond().wait_for_state_change(), False)
 964
 965        self.god.check_playback()
 966
 967
 968    def test_bonding_get_active_interfaces(self):
 969        self.assertEquals(net_utils.bond().get_active_interfaces(), [])
 970        self.god.check_playback()
 971
 972
 973    def test_bonding_get_slave_interfaces(self):
 974        self.assertEquals(net_utils.bond().get_slave_interfaces(), [])
 975        self.god.check_playback()
 976
 977
 978    #
 979    # ethernet tests
 980    #
 981
 982    def test_ethernet_mac_string_to_binary(self):
 983        mac_bin = net_utils.ethernet.mac_string_to_binary('00:01:02:03:04:05')
 984        self.assertEqual(mac_bin, '\x00\x01\x02\x03\x04\x05')
 985
 986
 987    def test_ethernet_mac_binary_to_string(self):
 988        mac_str = net_utils.ethernet.mac_binary_to_string(
 989            '\x00\x01\x02\x03\x04\x05')
 990        self.assertEqual(mac_str, '00:01:02:03:04:05')
 991
 992
 993    def test_ethernet_pack(self):
 994        dst = net_utils.ethernet.mac_string_to_binary('00:01:02:03:04:05')
 995        src = net_utils.ethernet.mac_string_to_binary('16:17:18:19:1A:1B')
 996        protocol = 2030
 997        payload = 'some payload'
 998        frame = struct.pack("!6s6sH", dst, src, protocol) + payload
 999        self.assertEquals(net_utils.ethernet.pack(dst, src, protocol, payload),
1000                          frame)
1001
1002
1003    def test_ethernet_unpack(self):
1004        dst = net_utils.ethernet.mac_string_to_binary('00:01:02:03:04:05')
1005        src = net_utils.ethernet.mac_string_to_binary('16:17:18:19:1A:1B')
1006        protocol = 2030
1007        payload = 'some payload'
1008        frame = net_utils.ethernet.pack(dst, src, protocol, payload)
1009        uframe = net_utils.ethernet.unpack(frame)
1010        self.assertEquals(uframe[net_utils.ethernet.FRAME_KEY_DST_MAC], dst)
1011        self.assertEquals(uframe[net_utils.ethernet.FRAME_KEY_SRC_MAC], src)
1012        self.assertEquals(uframe[net_utils.ethernet.FRAME_KEY_PROTO], protocol)
1013        self.assertEquals(uframe[net_utils.ethernet.FRAME_KEY_PAYLOAD], payload)
1014
1015
1016    # raw_socket tests
1017    #
1018    def test_raw_socket_open(self):
1019        self.god.stub_function(socket, 'setdefaulttimeout')
1020
1021        s = self.god.create_mock_class(socket.socket, "socket")
1022        self.god.stub_function(socket, 'socket')
1023
1024        # open without a protocol
1025        socket.setdefaulttimeout.expect_call(1)
1026        socket.socket.expect_call(socket.PF_PACKET,
1027                                  socket.SOCK_RAW).and_return(s)
1028        s.bind.expect_call(('eth0', net_utils.raw_socket.ETH_P_ALL))
1029        s.settimeout.expect_call(1)
1030        sock = net_utils.raw_socket('eth0')
1031        sock.open(protocol=None)
1032
1033        self.god.check_playback()
1034
1035        # double open should throw an exception
1036        try:
1037            sock.open()
1038        except error.TestError:
1039            pass
1040        else:
1041            self.assertEquals(1, 0)
1042
1043        self.god.check_playback()
1044
1045        # open a protocol
1046        socket.setdefaulttimeout.expect_call(1)
1047        socket.socket.expect_call(socket.PF_PACKET,
1048                                  socket.SOCK_RAW,
1049                                  socket.htons(1234)).and_return(s)
1050        s.bind.expect_call(('eth0', net_utils.raw_socket.ETH_P_ALL))
1051        s.settimeout.expect_call(1)
1052        sock = net_utils.raw_socket('eth0')
1053        sock.open(protocol=1234)
1054
1055        self.god.check_playback()
1056
1057
1058    def test_raw_socket_close(self):
1059        self.god.stub_function(socket, 'setdefaulttimeout')
1060
1061        s = self.god.create_mock_class(socket.socket, "socket")
1062        self.god.stub_function(socket, 'socket')
1063
1064        # close without open
1065        socket.setdefaulttimeout.expect_call(1)
1066        sock = net_utils.raw_socket('eth0')
1067        try:
1068            sock.close()
1069        except error.TestError:
1070            pass
1071        else:
1072            self.assertEquals(1, 0)
1073
1074        # close after open
1075        socket.setdefaulttimeout.expect_call(1)
1076        socket.socket.expect_call(socket.PF_PACKET,
1077                                  socket.SOCK_RAW).and_return(s)
1078        s.bind.expect_call(('eth0', net_utils.raw_socket.ETH_P_ALL))
1079        s.settimeout.expect_call(1)
1080        sock = net_utils.raw_socket('eth0')
1081        sock.open(protocol=None)
1082
1083        s.close.expect_call()
1084        sock.close()
1085        self.god.check_playback()
1086
1087
1088    def test_raw_socket_recv(self):
1089        self.god.stub_function(socket, 'setdefaulttimeout')
1090
1091        self.god.create_mock_class(socket.socket, "socket")
1092        self.god.stub_function(socket, 'socket')
1093
1094        # rcv without open
1095        socket.setdefaulttimeout.expect_call(1)
1096        sock = net_utils.raw_socket('eth0')
1097        try:
1098            sock.recv(10)
1099        except error.TestError:
1100            pass
1101        else:
1102            self.assertEquals(1, 0)
1103
1104        self.god.check_playback()
1105
1106        # open a protocol and try to get packets of varying sizes
1107        # I could not get socket.recv to get a mock expect_call. To keep
1108        # on going, added a socket stub
1109        s = net_utils_mock.socket_stub('eth0', socket, socket)
1110        socket.socket.expect_call(socket.PF_PACKET,
1111                                  socket.SOCK_RAW,
1112                                  socket.htons(1234)).and_return(s)
1113
1114        self.god.stub_function(s, 'bind')
1115        self.god.stub_function(s, 'settimeout')
1116        s.bind.expect_call(('eth0', net_utils.raw_socket.ETH_P_ALL))
1117        s.settimeout.expect_call(1)
1118        sock.open(protocol=1234)
1119
1120        s.recv_val = ''
1121        self.assertEquals(sock.recv(1), (None, 0))
1122
1123        s.recv_val = '\xFF' * (net_utils.ethernet.ETH_PACKET_MIN_SIZE-5)
1124        self.assertEquals(sock.recv(1), (None, 0))
1125
1126        # when receiving a packet, make sure the timeout is not change
1127        s.recv_val = '\xEE' * (net_utils.ethernet.ETH_PACKET_MIN_SIZE-4)
1128        self.assertEquals(sock.recv(1), (s.recv_val, 1))
1129
1130        s.recv_val = '\xDD' * (net_utils.ethernet.ETH_PACKET_MIN_SIZE)
1131        self.assertEquals(sock.recv(1), (s.recv_val, 1))
1132
1133        s.recv_val = '\xCC' * (net_utils.ethernet.ETH_PACKET_MAX_SIZE)
1134        self.assertEquals(sock.recv(1), (s.recv_val, 1))
1135
1136        s.recv_val = '\xBB' * (net_utils.ethernet.ETH_PACKET_MAX_SIZE+1)
1137        packet, time_left = sock.recv(1)
1138        self.assertEquals(len(packet), net_utils.ethernet.ETH_PACKET_MAX_SIZE)
1139        self.assertEquals(packet,
1140                          s.recv_val[:net_utils.ethernet.ETH_PACKET_MAX_SIZE])
1141
1142
1143        # test timeout
1144        s.recv_val = ''
1145        s.throw_timeout = False
1146        sock.recv(5)
1147        self.assertEquals(sock.recv(1), (None, 0))
1148        s.throw_timeout = True
1149        sock.recv(5)
1150        self.assertEquals(sock.recv(1), (None, 0))
1151
1152        self.god.check_playback()
1153
1154
1155    def test_raw_socket_send(self):
1156        self.god.stub_function(socket, 'setdefaulttimeout')
1157        self.god.create_mock_class(socket.socket, "socket")
1158        self.god.stub_function(socket, 'socket')
1159        self.god.stub_function(socket, 'send')
1160
1161        # send without open
1162        socket.setdefaulttimeout.expect_call(1)
1163        sock = net_utils.raw_socket('eth0')
1164        try:
1165            sock.send('test this packet')
1166        except error.TestError:
1167            pass
1168        else:
1169            self.assertEquals(1, 0)
1170        self.god.check_playback()
1171
1172        # open a protocol and try to send a packet
1173        s = net_utils_mock.socket_stub('eth0', socket, socket)
1174        self.god.stub_function(s, 'bind')
1175        self.god.stub_function(s, 'settimeout')
1176        socket.socket.expect_call(socket.PF_PACKET,
1177                                  socket.SOCK_RAW,
1178                                  socket.htons(1234)).and_return(s)
1179        s.bind.expect_call(('eth0', net_utils.raw_socket.ETH_P_ALL))
1180        s.settimeout.expect_call(1)
1181        packet = '\xFF\xAA\xBB\xCC\xDD\x11packet data\x00\x00'
1182        s.send.expect_call(packet)
1183        sock.open(protocol=1234)
1184        sock.send(packet)
1185        self.god.check_playback()
1186
1187
1188    def test_raw_socket_send_to(self):
1189        self.god.stub_function(socket, 'setdefaulttimeout')
1190        self.god.create_mock_class(socket.socket, "socket")
1191        self.god.stub_function(socket, 'socket')
1192        self.god.stub_function(socket, 'send')
1193
1194        # send without open
1195        socket.setdefaulttimeout.expect_call(1)
1196        sock = net_utils.raw_socket('eth0')
1197        try:
1198            sock.send_to('0', '1', 1, 'test this packet')
1199        except error.TestError:
1200            pass
1201        else:
1202            self.assertEquals(1, 0)
1203        self.god.check_playback()
1204
1205        # open a protocol and try to send a packet
1206        s = net_utils_mock.socket_stub('eth0', socket, socket)
1207        self.god.stub_function(s, 'bind')
1208        self.god.stub_function(s, 'settimeout')
1209        socket.socket.expect_call(socket.PF_PACKET,
1210                                  socket.SOCK_RAW,
1211                                  socket.htons(1234)).and_return(s)
1212        s.bind.expect_call(('eth0', net_utils.raw_socket.ETH_P_ALL))
1213        s.settimeout.expect_call(1)
1214        packet = '\x00\x00packet data\x00\x00'
1215        s.send.expect_call(packet)
1216        sock.open(protocol=1234)
1217        try:
1218            sock.send_to(None, None, 1, packet)
1219        except error.TestError:
1220            pass
1221        else:
1222            self.assertEquals(1, 0)
1223        self.god.check_playback()
1224
1225        dst_mac = '\x00\x01\x02\x03\x04\x05'
1226        src_mac = '\xFF\xEE\xDD\xCC\xBB\xAA'
1227        protocol = 1234
1228        s.send.expect_call(dst_mac+src_mac+'%d'%protocol+packet)
1229        sock.send_to(dst_mac, src_mac, protocol, packet)
1230        self.god.check_playback()
1231
1232
1233    def test_raw_socket_recv_from(self):
1234
1235        def __set_clock(sock):
1236            time.clock.expect_call().and_return(0.0)
1237            time.clock.expect_call().and_return(0.0)
1238            time.clock.expect_call().and_return(float(sock.socket_timeout()) + 0.5)
1239
1240        self.god.stub_function(socket, 'setdefaulttimeout')
1241
1242        self.god.create_mock_class(socket.socket, "socket")
1243        self.god.stub_function(socket, 'socket')
1244
1245        # rcv without open
1246        socket.setdefaulttimeout.expect_call(1)
1247        sock = net_utils.raw_socket('eth0')
1248        try:
1249            sock.recv_from(None, None, None)
1250        except error.TestError:
1251            pass
1252        else:
1253            self.assertEquals(1, 0)
1254
1255        self.god.check_playback()
1256
1257        # open a protocol and try to get packets of varying sizes
1258        # I could not get socket.recv to get a mock expect_call. To keep
1259        # on going, added a socket stub
1260        s = net_utils_mock.socket_stub('eth0', socket, socket)
1261        socket.socket.expect_call(socket.PF_PACKET,
1262                                  socket.SOCK_RAW,
1263                                  socket.htons(1234)).and_return(s)
1264
1265        self.god.stub_function(s, 'bind')
1266        self.god.stub_function(s, 'settimeout')
1267        s.bind.expect_call(('eth0', net_utils.raw_socket.ETH_P_ALL))
1268        s.settimeout.expect_call(1)
1269        sock.open(protocol=1234)
1270
1271        s.recv_val = ''
1272        dst_mac = net_utils.ethernet.mac_string_to_binary('00:01:02:03:04:05')
1273        src_mac = net_utils.ethernet.mac_string_to_binary('16:17:18:19:1A:1B')
1274        t_mac = net_utils.ethernet.mac_string_to_binary('E6:E7:E8:E9:EA:EB')
1275        protocol = 2030
1276        t_protocol = 1234
1277        data = '\xEE' * (net_utils.ethernet.ETH_PACKET_MIN_SIZE)
1278
1279        # no data to receive at socket
1280        self.assertEquals(sock.recv_from(None, None, None), None)
1281        self.assertEquals(sock.recv_from(dst_mac, None, None), None)
1282        self.assertEquals(sock.recv_from(None, src_mac, None), None)
1283        self.assertEquals(sock.recv_from(None, None, protocol), None)
1284
1285        # receive packet < min size
1286        s.recv_val = (struct.pack("!6s6sH", dst_mac, src_mac, protocol) +
1287                      'packet_to_short')
1288        self.assertEquals(sock.recv_from(None, None, None), None)
1289
1290        # receive packet, filtering on mac address and protocol
1291        s.recv_val = struct.pack("!6s6sH", dst_mac, t_mac, t_protocol) + data
1292        frame = net_utils.ethernet.unpack(s.recv_val)
1293        self.assertEquals(sock.recv_from(None, None, None), frame)
1294        self.assertEquals(sock.recv_from(dst_mac, None, None), frame)
1295
1296        # use time clock to speed up the timeout in send_to()
1297        self.god.stub_function(time, 'clock')
1298        __set_clock(sock)
1299        self.assertEquals(sock.recv_from(dst_mac, src_mac, None), None)
1300        __set_clock(sock)
1301        self.assertEquals(sock.recv_from(dst_mac, None, protocol), None)
1302        __set_clock(sock)
1303        self.assertEquals(sock.recv_from(dst_mac, src_mac, protocol), None)
1304        self.god.unstub(time, 'clock')
1305
1306        s.recv_val = struct.pack("!6s6sH", dst_mac, src_mac, protocol) + data
1307        frame = net_utils.ethernet.unpack(s.recv_val)
1308        self.assertEquals(sock.recv_from(dst_mac, None, None), frame)
1309        self.assertEquals(sock.recv_from(dst_mac, src_mac, None), frame)
1310        self.assertEquals(sock.recv_from(dst_mac, src_mac, protocol), frame)
1311        self.assertEquals(sock.recv_from(None, None, protocol), frame)
1312        self.assertEquals(sock.recv_from(None, src_mac, None), frame)
1313        self.god.stub_function(time, 'clock')
1314        __set_clock(sock)
1315       

Large files files are truncated, but you can click here to view the full file