Please note that the CVS and issue trackers have moved to GitHub. These Trac pages are no longer kept up-to-date.

root/seattle/branches/nacl_repy/seattlelib/tests/ut_lind_net_recvfrom.py

Revision 5515, 4.6 KB (checked in by nitin, 5 years ago)

added few sleeps between send and recv, so that it works on every machine

Line 
1"""
2File : ut_lind_net_recvfrom.py
3
4Unit test for recvfrom_syscall(), this applies for recv_sycall() as well,
5since recv_syscall() calls recvfrom_syscall(). This test is for TCP only.
6Tests include both reads & peeks from socket in recvfrom_syscall().
7Test 1# write 100 bytes --> peek 100 bytes, read 100 bytes.
8Test 2# write 100 bytes --> read 20 bytes, peek 20, read 80.
9Test 3# wrtie 100 bytes --> repeated peeks of {10, 20, 30 , 40}bytes, read 100
10Test 4# write  50 bytes --> peek 100 bytes.
11
12"""
13
14import lind_test_server
15import emultimer
16
17from lind_net_constants import *
18
19SyscallError = lind_test_server.SyscallError
20
21#Both client and server are run from this file, hence opening sockets for both
22serversockfd = lind_test_server.socket_syscall(AF_INET, SOCK_STREAM, 0)
23clientsockfd = lind_test_server.socket_syscall(AF_INET, SOCK_STREAM, 0)
24
25#Bind & listen with backlog of 1, since I am creating only 1 client.
26lind_test_server.bind_syscall(serversockfd, '127.0.0.1', 50300)
27lind_test_server.listen_syscall(serversockfd, 1)
28
29
30def process_request():
31  """
32  <Purpose>
33    Process the incoming data using above specifed tests
34  """
35
36  rip, rport, newsockfd = lind_test_server.accept_syscall(serversockfd) 
37 
38  #Process first test...
39  try:
40    msg = lind_test_server.recvfrom_syscall(newsockfd, 100, MSG_PEEK)
41    assert len(msg[2]) == 100, "Length expected 100 bytes to peek, but only" \
42      + " got " + str(len(msg[2])) + " bytes."
43    msg = lind_test_server.recvfrom_syscall(newsockfd, 100, 0)
44    assert len(msg[2]) == 100, "Length expected 100 bytes to read, but only" \
45      + " got " + str(len(msg[2])) + " bytes."
46  except Exception, e:
47    print 'TEST:- W:100, P:100, R:100 : ', e
48
49  emultimer.sleep(0.2)
50
51  #Process second test...
52  try:
53    msg = lind_test_server.recvfrom_syscall(newsockfd, 20, 0)
54    assert len(msg[2]) == 20, "Length expected 20 bytes to read, but only" \
55      + " got " + str(len(msg[2])) + " bytes."
56    msg = lind_test_server.recvfrom_syscall(newsockfd, 20, MSG_PEEK)
57    assert len(msg[2]) == 20, "Length expected 20 bytes to peek, but only" \
58      + " got " + str(len(msg[2])) + " bytes."
59    msg = lind_test_server.recvfrom_syscall(newsockfd, 80, 0)
60    assert len(msg[2]) == 80, "Length expected 80 bytes to read, but only" \
61      + " got " + str(len(msg[2])) + " bytes."
62  except Exception, e:
63    print 'Test:- W:100, R:20, P:20, R:80 : ', e
64
65  emultimer.sleep(0.2)
66
67  #Process thrid test...
68  try:
69    for i in range(0,4):
70      msg = lind_test_server.recvfrom_syscall(newsockfd, 10, MSG_PEEK)
71      assert len(msg[2]) == 10, "Length expected 10 bytes to peek, but only" \
72        + " got " + str(len(msg[2])) + " bytes."
73    for i in range(0,4):
74      msg = lind_test_server.recvfrom_syscall(newsockfd, 20, MSG_PEEK)
75      assert len(msg[2]) == 20, "Length expected 20 bytes to peek, but only" \
76        + " got " + str(len(msg[2])) + " bytes."
77    for i in range(0,4):
78      msg = lind_test_server.recvfrom_syscall(newsockfd, 30, MSG_PEEK)
79      assert len(msg[2]) == 30, "Length expected 30 bytes to peek, but only" \
80        + " got " + str(len(msg[2])) + " bytes."
81    for i in range(0,4):
82      msg = lind_test_server.recvfrom_syscall(newsockfd, 40, MSG_PEEK)
83      assert len(msg[2]) == 40, "Length expected 40 bytes to peek, but only" \
84        + " got " + str(len(msg[2])) + " bytes."
85    msg = lind_test_server.recvfrom_syscall(newsockfd, 100, 0)
86    assert len(msg[2]) == 100, "Length expected 100 bytes to read, but only" \
87      + " got " + str(len(msg[2])) + " bytes."
88  except Exception, e:
89    print 'Test:- W:100, Peek several times : ', e
90
91  emultimer.sleep(0.2)
92
93  #Process fourth test...
94  try:
95    msg = lind_test_server.recvfrom_syscall(newsockfd, 100, MSG_PEEK)
96    assert len(msg[2]) == 50, "Length expected 50 bytes to peek, but only" \
97      + " got " + str(len(msg[2])) + " bytes."
98  except Exception, e:
99    print 'Test:- W:100 P:50 : ', e
100 
101  #Gracefully close the socket
102  lind_test_server.close_syscall(newsockfd)
103
104#Run the server in a seperate thread, since client/server should be started
105#simultaneously.
106emultimer.createthread(process_request)
107
108#connect to server
109lind_test_server.connect_syscall(clientsockfd, '127.0.0.1', 50300)
110
111#send each test with some delay, so that server processes each test cleanly.
112lind_test_server.send_syscall(clientsockfd, "A" * 100, 0)
113emultimer.sleep(0.1)
114lind_test_server.send_syscall(clientsockfd, "A" * 100, 0)
115emultimer.sleep(0.1)
116lind_test_server.send_syscall(clientsockfd, "A" * 100, 0)
117emultimer.sleep(0.1)
118lind_test_server.send_syscall(clientsockfd, "A" * 50, 0)
119emultimer.sleep(0.1)
120
121#close the client & server sockets... 
122lind_test_server.close_syscall(clientsockfd)
123lind_test_server.close_syscall(serversockfd)
Note: See TracBrowser for help on using the browser.