PageRenderTime 57ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/client/net/net_utils_unittest.py

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