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_bind_on_zero.py

Revision 5535, 7.7 KB (checked in by cmatthew, 5 years ago)

Fixes:

  • made port reservation thread safe
  • made a port reservation debugger
  • Two binds now no longer release a port
  • When port reservation fails, retry until you get one
  • if a connection fails, return the port to the pool
  • Fixed an overwritten constant which was incorrent
  • added a unit test to check binds on 0.0.0.0
  • made the socket option test more beefy, since it was not catching one of my errors.
Line 
1"""
2
3Bind to 0.0.0.0. Then make connections on 127.0.0.1 and the public IP.
4
5Make sure the server port can handle them both at the same time.
6
7"""
8
9import lind_test_server
10import emultimer
11import emulcomm
12
13from lind_net_constants import *
14
15SyscallError = lind_test_server.SyscallError
16
17#Both client and server are run from this file, hence opening sockets for both
18serversockfd = lind_test_server.socket_syscall(AF_INET, SOCK_STREAM, 0)
19
20clientsockfd = lind_test_server.socket_syscall(AF_INET, SOCK_STREAM, 0)
21clientsockfd2 = lind_test_server.socket_syscall(AF_INET, SOCK_STREAM, 0)
22
23#Bind & listen with backlog of 1, since I am creating only 1 client.
24lind_test_server.bind_syscall(serversockfd, '0.0.0.0', 50300)
25lind_test_server.listen_syscall(serversockfd, 1)
26
27
28def process_request():
29  """
30  <Purpose>
31    Process the incoming data using above specifed tests
32  """
33
34  rip, rport, newsockfd = lind_test_server.accept_syscall(serversockfd) 
35 
36  #Process first test...
37  try:
38    msg = lind_test_server.recvfrom_syscall(newsockfd, 100, MSG_PEEK)
39    assert len(msg[2]) == 100, "Length expected 100 bytes to peek, but only" \
40      + " got " + str(len(msg[2])) + " bytes."
41    msg = lind_test_server.recvfrom_syscall(newsockfd, 100, 0)
42    assert len(msg[2]) == 100, "Length expected 100 bytes to read, but only" \
43      + " got " + str(len(msg[2])) + " bytes."
44  except Exception, e:
45    print 'TEST:- W:100, P:100, R:100 : ', e
46
47  emultimer.sleep(0.2)
48
49  #Process second test...
50  try:
51    msg = lind_test_server.recvfrom_syscall(newsockfd, 20, 0)
52    assert len(msg[2]) == 20, "Length expected 20 bytes to read, but only" \
53      + " got " + str(len(msg[2])) + " bytes."
54    msg = lind_test_server.recvfrom_syscall(newsockfd, 20, MSG_PEEK)
55    assert len(msg[2]) == 20, "Length expected 20 bytes to peek, but only" \
56      + " got " + str(len(msg[2])) + " bytes."
57    msg = lind_test_server.recvfrom_syscall(newsockfd, 80, 0)
58    assert len(msg[2]) == 80, "Length expected 80 bytes to read, but only" \
59      + " got " + str(len(msg[2])) + " bytes."
60  except Exception, e:
61    print 'Test:- W:100, R:20, P:20, R:80 : ', e
62
63  emultimer.sleep(0.2)
64
65  #Process thrid test...
66  try:
67    for i in range(0,4):
68      msg = lind_test_server.recvfrom_syscall(newsockfd, 10, MSG_PEEK)
69      assert len(msg[2]) == 10, "Length expected 10 bytes to peek, but only" \
70        + " got " + str(len(msg[2])) + " bytes."
71    for i in range(0,4):
72      msg = lind_test_server.recvfrom_syscall(newsockfd, 20, MSG_PEEK)
73      assert len(msg[2]) == 20, "Length expected 20 bytes to peek, but only" \
74        + " got " + str(len(msg[2])) + " bytes."
75    for i in range(0,4):
76      msg = lind_test_server.recvfrom_syscall(newsockfd, 30, MSG_PEEK)
77      assert len(msg[2]) == 30, "Length expected 30 bytes to peek, but only" \
78        + " got " + str(len(msg[2])) + " bytes."
79    for i in range(0,4):
80      msg = lind_test_server.recvfrom_syscall(newsockfd, 40, MSG_PEEK)
81      assert len(msg[2]) == 40, "Length expected 40 bytes to peek, but only" \
82        + " got " + str(len(msg[2])) + " bytes."
83    msg = lind_test_server.recvfrom_syscall(newsockfd, 100, 0)
84    assert len(msg[2]) == 100, "Length expected 100 bytes to read, but only" \
85      + " got " + str(len(msg[2])) + " bytes."
86  except Exception, e:
87    print 'Test:- W:100, Peek several times : ', e
88
89  emultimer.sleep(0.2)
90
91  #Process fourth test...
92  try:
93    msg = lind_test_server.recvfrom_syscall(newsockfd, 100, MSG_PEEK)
94    assert len(msg[2]) == 50, "Length expected 50 bytes to peek, but only" \
95      + " got " + str(len(msg[2])) + " bytes."
96  except Exception, e:
97    print 'Test:- W:100 P:50 : ', e
98 
99  #Gracefully close the socket
100  lind_test_server.close_syscall(newsockfd)
101
102
103  rip, rport, newsockfd = lind_test_server.accept_syscall(serversockfd) 
104 
105  #Process first test...
106  try:
107    msg = lind_test_server.recvfrom_syscall(newsockfd, 100, MSG_PEEK)
108    assert len(msg[2]) == 100, "Length expected 100 bytes to peek, but only" \
109      + " got " + str(len(msg[2])) + " bytes."
110    msg = lind_test_server.recvfrom_syscall(newsockfd, 100, 0)
111    assert len(msg[2]) == 100, "Length expected 100 bytes to read, but only" \
112      + " got " + str(len(msg[2])) + " bytes."
113  except Exception, e:
114    print 'TEST:- W:100, P:100, R:100 : ', e
115
116  emultimer.sleep(0.2)
117
118  #Process second test...
119  try:
120    msg = lind_test_server.recvfrom_syscall(newsockfd, 20, 0)
121    assert len(msg[2]) == 20, "Length expected 20 bytes to read, but only" \
122      + " got " + str(len(msg[2])) + " bytes."
123    msg = lind_test_server.recvfrom_syscall(newsockfd, 20, MSG_PEEK)
124    assert len(msg[2]) == 20, "Length expected 20 bytes to peek, but only" \
125      + " got " + str(len(msg[2])) + " bytes."
126    msg = lind_test_server.recvfrom_syscall(newsockfd, 80, 0)
127    assert len(msg[2]) == 80, "Length expected 80 bytes to read, but only" \
128      + " got " + str(len(msg[2])) + " bytes."
129  except Exception, e:
130    print 'Test:- W:100, R:20, P:20, R:80 : ', e
131
132  emultimer.sleep(0.2)
133
134  #Process thrid test...
135  try:
136    for i in range(0,4):
137      msg = lind_test_server.recvfrom_syscall(newsockfd, 10, MSG_PEEK)
138      assert len(msg[2]) == 10, "Length expected 10 bytes to peek, but only" \
139        + " got " + str(len(msg[2])) + " bytes."
140    for i in range(0,4):
141      msg = lind_test_server.recvfrom_syscall(newsockfd, 20, MSG_PEEK)
142      assert len(msg[2]) == 20, "Length expected 20 bytes to peek, but only" \
143        + " got " + str(len(msg[2])) + " bytes."
144    for i in range(0,4):
145      msg = lind_test_server.recvfrom_syscall(newsockfd, 30, MSG_PEEK)
146      assert len(msg[2]) == 30, "Length expected 30 bytes to peek, but only" \
147        + " got " + str(len(msg[2])) + " bytes."
148    for i in range(0,4):
149      msg = lind_test_server.recvfrom_syscall(newsockfd, 40, MSG_PEEK)
150      assert len(msg[2]) == 40, "Length expected 40 bytes to peek, but only" \
151        + " got " + str(len(msg[2])) + " bytes."
152    msg = lind_test_server.recvfrom_syscall(newsockfd, 100, 0)
153    assert len(msg[2]) == 100, "Length expected 100 bytes to read, but only" \
154      + " got " + str(len(msg[2])) + " bytes."
155  except Exception, e:
156    print 'Test:- W:100, Peek several times : ', e
157
158  emultimer.sleep(0.2)
159
160  #Process fourth test...
161  try:
162    msg = lind_test_server.recvfrom_syscall(newsockfd, 100, MSG_PEEK)
163    assert len(msg[2]) == 50, "Length expected 50 bytes to peek, but only" \
164      + " got " + str(len(msg[2])) + " bytes."
165  except Exception, e:
166    print 'Test:- W:100 P:50 : ', e
167 
168  #Gracefully close the socket
169  lind_test_server.close_syscall(newsockfd)
170
171
172
173#Run the server in a seperate thread, since client/server should be started
174#simultaneously.
175emultimer.createthread(process_request)
176
177#connect to server
178lind_test_server.connect_syscall(clientsockfd, '127.0.0.1', 50300)
179
180#send each test with some delay, so that server processes each test cleanly.
181lind_test_server.send_syscall(clientsockfd, "A" * 100, 0)
182emultimer.sleep(0.1)
183lind_test_server.send_syscall(clientsockfd, "A" * 100, 0)
184emultimer.sleep(0.1)
185lind_test_server.send_syscall(clientsockfd, "A" * 100, 0)
186emultimer.sleep(0.1)
187lind_test_server.send_syscall(clientsockfd, "A" * 50, 0)
188emultimer.sleep(0.1)
189
190lind_test_server.close_syscall(clientsockfd)
191
192
193lind_test_server.connect_syscall(clientsockfd2, emulcomm.getmyip(), 50300)
194
195#send each test with some delay, so that server processes each test cleanly.
196lind_test_server.send_syscall(clientsockfd2, "A" * 100, 0)
197emultimer.sleep(0.1)
198lind_test_server.send_syscall(clientsockfd2, "A" * 100, 0)
199emultimer.sleep(0.1)
200lind_test_server.send_syscall(clientsockfd2, "A" * 100, 0)
201emultimer.sleep(0.1)
202lind_test_server.send_syscall(clientsockfd2, "A" * 50, 0)
203emultimer.sleep(0.1)
204
205
206
207#close the client & server sockets... 
208lind_test_server.close_syscall(clientsockfd2)
209lind_test_server.close_syscall(serversockfd)
Note: See TracBrowser for help on using the browser.