Best Python code snippet using autotest_python
net_utils_unittest.py
Source:net_utils_unittest.py
...763 self.god.check_playback()764 #765 # ethernet tests766 #767 def test_ethernet_mac_string_to_binary(self):768 mac_bin = net_utils.ethernet.mac_string_to_binary('00:01:02:03:04:05')769 self.assertEqual(mac_bin, '\x00\x01\x02\x03\x04\x05')770 def test_ethernet_mac_binary_to_string(self):771 mac_str = net_utils.ethernet.mac_binary_to_string(772 '\x00\x01\x02\x03\x04\x05')773 self.assertEqual(mac_str, '00:01:02:03:04:05')774 def test_ethernet_pack(self):775 dst = net_utils.ethernet.mac_string_to_binary('00:01:02:03:04:05')776 src = net_utils.ethernet.mac_string_to_binary('16:17:18:19:1A:1B')777 protocol = 2030778 payload = 'some payload'779 frame = struct.pack("!6s6sH", dst, src, protocol) + payload780 self.assertEquals(net_utils.ethernet.pack(dst, src,protocol, payload),781 frame)782 def test_ethernet_unpack(self):783 dst = net_utils.ethernet.mac_string_to_binary('00:01:02:03:04:05')784 src = net_utils.ethernet.mac_string_to_binary('16:17:18:19:1A:1B')785 protocol = 2030786 payload = 'some payload'787 frame = net_utils.ethernet.pack(dst, src, protocol, payload)788 uframe = net_utils.ethernet.unpack(frame)789 self.assertEquals(uframe[net_utils.ethernet.FRAME_KEY_DST_MAC], dst)790 self.assertEquals(uframe[net_utils.ethernet.FRAME_KEY_SRC_MAC], src)791 self.assertEquals(uframe[net_utils.ethernet.FRAME_KEY_PROTO], protocol)792 self.assertEquals(uframe[net_utils.ethernet.FRAME_KEY_PAYLOAD], payload)793 # raw_socket tests794 #795 def test_raw_socket_open(self):796 self.god.stub_function(socket, 'setdefaulttimeout')797 s = self.god.create_mock_class(socket.socket, "socket")798 self.god.stub_function(socket, 'socket')799 # open without a protocol800 socket.setdefaulttimeout.expect_call(1)801 socket.socket.expect_call(socket.PF_PACKET,802 socket.SOCK_RAW).and_return(s)803 s.bind.expect_call(('eth0', net_utils.raw_socket.ETH_P_ALL))804 s.settimeout.expect_call(1)805 sock = net_utils.raw_socket('eth0')806 sock.open(protocol=None)807 self.god.check_playback()808 # double open should throw an exception809 try:810 sock.open()811 except error.TestError:812 pass813 else:814 self.assertEquals(1, 0)815 self.god.check_playback()816 # open a protocol817 socket.setdefaulttimeout.expect_call(1)818 socket.socket.expect_call(socket.PF_PACKET,819 socket.SOCK_RAW,820 socket.htons(1234)).and_return(s)821 s.bind.expect_call(('eth0', net_utils.raw_socket.ETH_P_ALL))822 s.settimeout.expect_call(1)823 sock = net_utils.raw_socket('eth0')824 sock.open(protocol=1234)825 self.god.check_playback()826 def test_raw_socket_close(self):827 self.god.stub_function(socket, 'setdefaulttimeout')828 s = self.god.create_mock_class(socket.socket, "socket")829 self.god.stub_function(socket, 'socket')830 # close without open831 socket.setdefaulttimeout.expect_call(1)832 sock = net_utils.raw_socket('eth0')833 try:834 sock.close()835 except error.TestError:836 pass837 else:838 self.assertEquals(1, 0)839 # close after open840 socket.setdefaulttimeout.expect_call(1)841 socket.socket.expect_call(socket.PF_PACKET,842 socket.SOCK_RAW).and_return(s)843 s.bind.expect_call(('eth0', net_utils.raw_socket.ETH_P_ALL))844 s.settimeout.expect_call(1)845 sock = net_utils.raw_socket('eth0')846 sock.open(protocol=None)847 s.close.expect_call()848 sock.close()849 self.god.check_playback()850 def test_raw_socket_recv(self):851 self.god.stub_function(socket, 'setdefaulttimeout')852 self.god.create_mock_class(socket.socket, "socket")853 self.god.stub_function(socket, 'socket')854 # rcv without open855 socket.setdefaulttimeout.expect_call(1)856 sock = net_utils.raw_socket('eth0')857 try:858 sock.recv(10)859 except error.TestError:860 pass861 else:862 self.assertEquals(1, 0)863 self.god.check_playback()864 # open a protocol and try to get packets of varying sizes865 # I could not get socket.recv to get a mock expect_call. To keep866 # on going, added a socket stub867 s = net_utils_mock.socket_stub('eth0', socket, socket)868 socket.socket.expect_call(socket.PF_PACKET,869 socket.SOCK_RAW,870 socket.htons(1234)).and_return(s)871 self.god.stub_function(s, 'bind')872 self.god.stub_function(s, 'settimeout')873 s.bind.expect_call(('eth0', net_utils.raw_socket.ETH_P_ALL))874 s.settimeout.expect_call(1)875 sock.open(protocol=1234)876 s.recv_val = ''877 self.assertEquals(sock.recv(1), (None, 0))878 s.recv_val = '\xFF' * (net_utils.ethernet.ETH_PACKET_MIN_SIZE-5)879 self.assertEquals(sock.recv(1), (None, 0))880 # when receiving a packet, make sure the timeout is not change881 s.recv_val = '\xEE' * (net_utils.ethernet.ETH_PACKET_MIN_SIZE-4)882 self.assertEquals(sock.recv(1), (s.recv_val, 1))883 s.recv_val = '\xDD' * (net_utils.ethernet.ETH_PACKET_MIN_SIZE)884 self.assertEquals(sock.recv(1), (s.recv_val, 1))885 s.recv_val = '\xCC' * (net_utils.ethernet.ETH_PACKET_MAX_SIZE)886 self.assertEquals(sock.recv(1), (s.recv_val, 1))887 s.recv_val = '\xBB' * (net_utils.ethernet.ETH_PACKET_MAX_SIZE+1)888 packet, time_left = sock.recv(1)889 self.assertEquals(len(packet), net_utils.ethernet.ETH_PACKET_MAX_SIZE)890 self.assertEquals(packet,891 s.recv_val[:net_utils.ethernet.ETH_PACKET_MAX_SIZE])892 # test timeout893 s.recv_val = ''894 s.throw_timeout = False895 sock.recv(5)896 self.assertEquals(sock.recv(1), (None, 0))897 s.throw_timeout = True898 sock.recv(5)899 self.assertEquals(sock.recv(1), (None, 0))900 self.god.check_playback()901 def test_raw_socket_send(self):902 self.god.stub_function(socket, 'setdefaulttimeout')903 self.god.create_mock_class(socket.socket, "socket")904 self.god.stub_function(socket, 'socket')905 self.god.stub_function(socket, 'send')906 # send without open907 socket.setdefaulttimeout.expect_call(1)908 sock = net_utils.raw_socket('eth0')909 try:910 sock.send('test this packet')911 except error.TestError:912 pass913 else:914 self.assertEquals(1, 0)915 self.god.check_playback()916 # open a protocol and try to send a packet917 s = net_utils_mock.socket_stub('eth0', socket, socket)918 self.god.stub_function(s, 'bind')919 self.god.stub_function(s, 'settimeout')920 socket.socket.expect_call(socket.PF_PACKET,921 socket.SOCK_RAW,922 socket.htons(1234)).and_return(s)923 s.bind.expect_call(('eth0', net_utils.raw_socket.ETH_P_ALL))924 s.settimeout.expect_call(1)925 packet = '\xFF\xAA\xBB\xCC\xDD\x11packet data\x00\x00'926 s.send.expect_call(packet)927 sock.open(protocol=1234)928 sock.send(packet)929 self.god.check_playback()930 def test_raw_socket_send_to(self):931 self.god.stub_function(socket, 'setdefaulttimeout')932 self.god.create_mock_class(socket.socket, "socket")933 self.god.stub_function(socket, 'socket')934 self.god.stub_function(socket, 'send')935 # send without open936 socket.setdefaulttimeout.expect_call(1)937 sock = net_utils.raw_socket('eth0')938 try:939 sock.send_to('0', '1', 1, 'test this packet')940 except error.TestError:941 pass942 else:943 self.assertEquals(1, 0)944 self.god.check_playback()945 # open a protocol and try to send a packet946 s = net_utils_mock.socket_stub('eth0', socket, socket)947 self.god.stub_function(s, 'bind')948 self.god.stub_function(s, 'settimeout')949 socket.socket.expect_call(socket.PF_PACKET,950 socket.SOCK_RAW,951 socket.htons(1234)).and_return(s)952 s.bind.expect_call(('eth0', net_utils.raw_socket.ETH_P_ALL))953 s.settimeout.expect_call(1)954 packet = '\x00\x00packet data\x00\x00'955 s.send.expect_call(packet)956 sock.open(protocol=1234)957 try:958 sock.send_to(None, None, 1, packet)959 except error.TestError:960 pass961 else:962 self.assertEquals(1, 0)963 self.god.check_playback()964 dst_mac = '\x00\x01\x02\x03\x04\x05'965 src_mac = '\xFF\xEE\xDD\xCC\xBB\xAA'966 protocol = 1234967 s.send.expect_call(dst_mac+src_mac+'%d'%protocol+packet)968 sock.send_to(dst_mac, src_mac, protocol, packet)969 self.god.check_playback()970 def test_raw_socket_recv_from(self):971 def __set_clock(sock):972 time.clock.expect_call().and_return(0.0)973 time.clock.expect_call().and_return(0.0)974 time.clock.expect_call().and_return(float(sock.socket_timeout()) + 0.5)975 self.god.stub_function(socket, 'setdefaulttimeout')976 self.god.create_mock_class(socket.socket, "socket")977 self.god.stub_function(socket, 'socket')978 # rcv without open979 socket.setdefaulttimeout.expect_call(1)980 sock = net_utils.raw_socket('eth0')981 try:982 sock.recv_from(None, None, None)983 except error.TestError:984 pass985 else:986 self.assertEquals(1, 0)987 self.god.check_playback()988 # open a protocol and try to get packets of varying sizes989 # I could not get socket.recv to get a mock expect_call. To keep990 # on going, added a socket stub991 s = net_utils_mock.socket_stub('eth0', socket, socket)992 socket.socket.expect_call(socket.PF_PACKET,993 socket.SOCK_RAW,994 socket.htons(1234)).and_return(s)995 self.god.stub_function(s, 'bind')996 self.god.stub_function(s, 'settimeout')997 s.bind.expect_call(('eth0', net_utils.raw_socket.ETH_P_ALL))998 s.settimeout.expect_call(1)999 sock.open(protocol=1234)1000 s.recv_val = ''1001 dst_mac = net_utils.ethernet.mac_string_to_binary('00:01:02:03:04:05')1002 src_mac = net_utils.ethernet.mac_string_to_binary('16:17:18:19:1A:1B')1003 t_mac = net_utils.ethernet.mac_string_to_binary('E6:E7:E8:E9:EA:EB')1004 protocol = 20301005 t_protocol = 12341006 data = '\xEE' * (net_utils.ethernet.ETH_PACKET_MIN_SIZE)1007 # no data to receive at socket1008 self.assertEquals(sock.recv_from(None, None, None), None)1009 self.assertEquals(sock.recv_from(dst_mac, None, None), None)1010 self.assertEquals(sock.recv_from(None, src_mac, None), None)1011 self.assertEquals(sock.recv_from(None, None, protocol), None)1012 # receive packet < min size1013 s.recv_val = (struct.pack("!6s6sH", dst_mac, src_mac, protocol) +1014 'packet_to_short')1015 self.assertEquals(sock.recv_from(None, None, None), None)1016 # receive packet, filtering on mac address and protocol1017 s.recv_val = struct.pack("!6s6sH", dst_mac, t_mac, t_protocol) + data...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!