|
1 // Copyright (c) 1997-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
2 // All rights reserved. |
|
3 // This component and the accompanying materials are made available |
|
4 // under the terms of "Eclipse Public License v1.0" |
|
5 // which accompanies this distribution, and is available |
|
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
7 // |
|
8 // Initial Contributors: |
|
9 // Nokia Corporation - initial contribution. |
|
10 // |
|
11 // Contributors: |
|
12 // |
|
13 // Description: |
|
14 // |
|
15 |
|
16 // #include <es_inet.h> |
|
17 #include <e32test.h> |
|
18 |
|
19 |
|
20 #define TCP_PORT_CHARGEN (TUint16)19 |
|
21 #define TCP_PORT_ECHO (TUint16)7 |
|
22 #define TCP_PORT_DISCARD (TUint16)9 |
|
23 #define TCP_PORT_FINGER (TUint16)79 |
|
24 #define TCP_PORT_TEST (TUint16)20000 |
|
25 #define TCP_CONNECT_BACK_PORT (TUint16)20003 |
|
26 |
|
27 #define IPADDR(a,b,c,d) (TUint32)(((a)<<24)|((b)<<16)|((c)<<8)|(d)) |
|
28 |
|
29 #define BUFFER_SIZE 8192 |
|
30 |
|
31 const TUint KDefaultHeapSize=0x1000; |
|
32 const TUint KNumSockets=10; |
|
33 |
|
34 TInetAddr HostAddr; |
|
35 |
|
36 // #define TEST_ADDR IPADDR(194,129,1,226) |
|
37 #define TEST_ADDR IPADDR(194,129,1,6) |
|
38 #define NULL_ADDR IPADDR(0,0,0,0) |
|
39 #define TEST_PORT TCP_PORT_TEST |
|
40 |
|
41 |
|
42 typedef TBuf8<0x4000> TBuf_4000; |
|
43 |
|
44 RTest test(_L("eSock Emulation test")); |
|
45 |
|
46 |
|
47 struct TSocketThreadArg |
|
48 { |
|
49 RSemaphore* iSem; |
|
50 TUint iNumSockets; |
|
51 }; |
|
52 |
|
53 TInt socketThread(TAny * anArg) |
|
54 // |
|
55 // |
|
56 // |
|
57 { |
|
58 |
|
59 RTest test(_L("eSock client test sub thread - creating sockets")); |
|
60 RSocketServ ss; |
|
61 TInt ret=ss.Connect(); |
|
62 test(ret==KErrNone); |
|
63 |
|
64 TInt numSockets=((TSocketThreadArg *)anArg)->iNumSockets; |
|
65 |
|
66 RSocket* sock=new RSocket[numSockets]; |
|
67 RHostResolver* hostR=new RHostResolver[numSockets]; |
|
68 RServiceResolver* servR=new RServiceResolver[numSockets]; |
|
69 |
|
70 for (TInt i=0;i<numSockets;i++) |
|
71 { |
|
72 ret=sock[i].Open(ss,KAFInet,KSockStream,KProtocolInetTCP); |
|
73 test(ret==KErrNone); |
|
74 } |
|
75 |
|
76 ((TSocketThreadArg *)anArg)->iSem->Signal(); |
|
77 User::WaitForAnyRequest(); |
|
78 |
|
79 return KErrNone; |
|
80 } |
|
81 |
|
82 |
|
83 void StripeMem(TDes8 &aBuf,TUint aStartChar,TUint anEndChar) |
|
84 // |
|
85 // Mark a buffer with repeating byte pattern |
|
86 // |
|
87 { |
|
88 |
|
89 // __ASSERT_ALWAYS(aStartChar<=anEndChar,Panic(EBadArg)); |
|
90 |
|
91 if (aStartChar==anEndChar) |
|
92 { |
|
93 aBuf.Fill(aStartChar); |
|
94 return; |
|
95 } |
|
96 |
|
97 TUint character=aStartChar; |
|
98 for (TInt i=0;i<aBuf.Length();i++) |
|
99 { |
|
100 aBuf[i]=(TText8)character; |
|
101 if(++character>anEndChar) |
|
102 character=aStartChar; |
|
103 } |
|
104 } |
|
105 |
|
106 TInt test0() |
|
107 // |
|
108 // Name resolver tests |
|
109 // |
|
110 { |
|
111 test.Start(_L("eSock Emulation test - Name resolution")); |
|
112 |
|
113 // Connect to the actual socket server |
|
114 RSocketServ ss; |
|
115 TInt ret=ss.Connect(); |
|
116 test(ret==KErrNone); |
|
117 |
|
118 test.Next(_L("Create host resolver")); |
|
119 RHostResolver hr; |
|
120 ret=hr.Open(ss,KAFInet,KSockStream); |
|
121 test(ret==KErrNone); |
|
122 |
|
123 test.Next(_L("Get host name")); |
|
124 TBuf<20> name; |
|
125 ret=hr.GetHostName(name); |
|
126 test(ret==KErrNone); |
|
127 test.Printf(_L("Hostname: %S\r\n"),&name); |
|
128 |
|
129 TNameRecord t; |
|
130 TNameEntry addrEnt(t); |
|
131 ret=hr.GetByName(name,addrEnt); |
|
132 test(ret==KErrNone); |
|
133 |
|
134 HostAddr=addrEnt().iAddr; |
|
135 TUint32 ad=ByteOrder::Swap32(HostAddr.Address()); |
|
136 test.Printf(_L("Address: %d.%d.%d.%d"),(ad&0x000000ff),(ad&0x0000ff00)>>8,(ad&0x00ff0000)>>16,(ad&0xff000000)>>24); |
|
137 test.Printf(_L(" (0x%X.0x%X.0x%X.0x%X) \r\n"),(ad&0x000000ff),(ad&0x0000ff00)>>8,(ad&0x00ff0000)>>16,(ad&0xff000000)>>24); |
|
138 hr.Close(); |
|
139 ss.Close(); |
|
140 test.End(); // } |
|
141 |
|
142 return 0; |
|
143 |
|
144 } |
|
145 |
|
146 |
|
147 |
|
148 TInt test1() |
|
149 // |
|
150 // Test connection and disconnection |
|
151 // |
|
152 { |
|
153 test.Start(_L("eSock Emulation test - connect/disconnect")); |
|
154 |
|
155 // Connect to the actual socket server |
|
156 RSocketServ ss; |
|
157 TInt ret = ss.Connect(); |
|
158 test(ret==KErrNone); |
|
159 |
|
160 test.Next(_L("Create socket")); // { |
|
161 |
|
162 RSocket sock; |
|
163 ret = sock.Open(ss, KAFInet, KSockStream, KProtocolInetTCP); |
|
164 test(ret==KErrNone); |
|
165 |
|
166 test.Next(_L("Connecting")); |
|
167 HostAddr.SetPort(TEST_PORT); |
|
168 TRequestStatus status; |
|
169 sock.Connect(HostAddr, status); |
|
170 User::WaitForRequest(status); |
|
171 test(status==KErrNone); |
|
172 |
|
173 test.Next(_L("Closing")); |
|
174 sock.Close(); |
|
175 |
|
176 ss.Close(); |
|
177 |
|
178 test.End(); // } |
|
179 |
|
180 return 0; |
|
181 |
|
182 } |
|
183 |
|
184 |
|
185 TInt test2() |
|
186 // |
|
187 // Test read and write |
|
188 // |
|
189 { |
|
190 test.Start(_L("eSock Emulation test - read/write")); |
|
191 |
|
192 // Connect to the actual socket server |
|
193 RSocketServ ss; |
|
194 TInt ret = ss.Connect(); |
|
195 test(ret==KErrNone); |
|
196 |
|
197 test.Next(_L("Create socket")); // { |
|
198 |
|
199 RSocket sock; |
|
200 ret = sock.Open(ss, KAFInet, KSockStream, KProtocolInetTCP); |
|
201 test(ret==KErrNone); |
|
202 |
|
203 test.Next(_L("Connecting")); |
|
204 HostAddr.SetPort(TEST_PORT); |
|
205 TRequestStatus status; |
|
206 sock.Connect(HostAddr, status); |
|
207 User::WaitForRequest(status); |
|
208 test(status==KErrNone); |
|
209 |
|
210 TRequestStatus readStatus; |
|
211 TRequestStatus writeStatus; |
|
212 |
|
213 test.Next(_L("Writing")); |
|
214 TBuf_4000* temp=new TBuf_4000; |
|
215 TBuf_4000& outBuf=*temp; |
|
216 outBuf.SetLength(100); |
|
217 StripeMem(outBuf, 'A', 'Z'); |
|
218 sock.Write(outBuf,writeStatus); |
|
219 |
|
220 TRequestStatus stat; |
|
221 sock.SetOpt(KSOBlockingIO,KSOLSocket); |
|
222 User::WaitForRequest(writeStatus); |
|
223 test(writeStatus==KErrNone); |
|
224 |
|
225 test.Next(_L("Reading")); |
|
226 TBuf_4000* temp2=new TBuf_4000; |
|
227 TBuf_4000& inBuf=*temp2; |
|
228 inBuf.SetLength(100); |
|
229 sock.Read(inBuf,readStatus); |
|
230 User::WaitForRequest(readStatus); |
|
231 test(status==KErrNone); |
|
232 test(readStatus==KErrNone); |
|
233 |
|
234 test.Next(_L("Checking Reply")); |
|
235 test(outBuf.Compare(inBuf)==0); |
|
236 |
|
237 test.Next(_L("Closing")); |
|
238 sock.Close(); |
|
239 |
|
240 delete temp; |
|
241 delete temp2; |
|
242 test.End(); // } |
|
243 |
|
244 return 0; |
|
245 |
|
246 } |
|
247 |
|
248 void test3() |
|
249 // |
|
250 // Test that everything is funky when we close sockets by killing a thread |
|
251 // |
|
252 { |
|
253 |
|
254 test.Start(_L("Checking thread termintaion clean up code")); |
|
255 RSemaphore s; |
|
256 s.CreateLocal(0); |
|
257 RThread t; |
|
258 |
|
259 TSocketThreadArg a; |
|
260 a.iSem=&s; |
|
261 a.iNumSockets=KNumSockets; |
|
262 |
|
263 test.Next(_L("Creating sub thread")); |
|
264 t.Create(_L("socketThread"),socketThread,KDefaultStackSize,KDefaultHeapSize,KDefaultHeapSize,&a); |
|
265 t.Resume(); |
|
266 s.Wait(); |
|
267 test.Next(_L("Killing Sub thread")); |
|
268 t.Kill(KErrNone); |
|
269 t.Close(); |
|
270 test.End(); |
|
271 } |
|
272 |
|
273 |
|
274 TInt test4() |
|
275 // |
|
276 // Stuff about a megabyte down its neck. |
|
277 // |
|
278 { |
|
279 test.Start(_L("Read/write multiple")); |
|
280 |
|
281 User::After(400000); |
|
282 |
|
283 // Connect to the actual socket server |
|
284 RSocketServ ss; |
|
285 TInt ret = ss.Connect(); |
|
286 test(ret==KErrNone); |
|
287 |
|
288 test.Next(_L("Create socket")); // { |
|
289 |
|
290 RSocket sock; |
|
291 ret = sock.Open(ss, KAFInet, KSockStream, KProtocolInetTCP); |
|
292 test(ret==KErrNone); |
|
293 |
|
294 test.Next(_L("Connecting")); |
|
295 TInetAddr addr(HostAddr); |
|
296 TRequestStatus status; |
|
297 sock.Connect(addr, status); |
|
298 User::WaitForRequest(status); |
|
299 test(status==KErrNone); |
|
300 |
|
301 TRequestStatus stat; |
|
302 sock.SetOpt(KSOBlockingIO,KSOLSocket); |
|
303 |
|
304 TRequestStatus writeStatus; |
|
305 TBuf_4000* temp=new TBuf_4000; |
|
306 TBuf_4000& outBuf=*temp; |
|
307 outBuf.SetLength(BUFFER_SIZE); |
|
308 StripeMem(outBuf, 'A', 'Z'); |
|
309 |
|
310 TRequestStatus readStatus; |
|
311 TBuf_4000* temp2=new TBuf_4000; |
|
312 TBuf_4000& inBuf=*temp2; |
|
313 inBuf.SetLength(BUFFER_SIZE); |
|
314 |
|
315 test.Next(_L("Reading and Writing")); |
|
316 for (int i=0; i<128; i++) |
|
317 { |
|
318 |
|
319 sock.Write(outBuf,writeStatus); |
|
320 User::WaitForRequest(writeStatus); |
|
321 |
|
322 sock.Read(inBuf,readStatus); |
|
323 User::WaitForRequest(readStatus); |
|
324 |
|
325 test(writeStatus==KErrNone); |
|
326 test(readStatus==KErrNone); |
|
327 |
|
328 test(outBuf.Compare(inBuf)==0); |
|
329 test.Printf(_L("Written %d K\r"),(i*BUFFER_SIZE)/1024); |
|
330 } |
|
331 |
|
332 test.Printf(_L("\n")); |
|
333 |
|
334 test.Next(_L("Closing")); |
|
335 sock.Close(); |
|
336 |
|
337 test.End(); // } |
|
338 delete temp; |
|
339 delete temp2; |
|
340 |
|
341 return 0; |
|
342 |
|
343 } |
|
344 |
|
345 void test5() |
|
346 { |
|
347 test.Start(_L("Dual socket Read/write multiple")); |
|
348 |
|
349 RSocketServ ss; |
|
350 TInt ret = ss.Connect(); |
|
351 test(ret==KErrNone); |
|
352 |
|
353 test.Next(_L("Create sockets")); |
|
354 |
|
355 RSocket sock1; |
|
356 ret = sock1.Open(ss, KAFInet, KSockStream, KProtocolInetTCP); |
|
357 test(ret==KErrNone); |
|
358 |
|
359 RSocket listener; |
|
360 ret = listener.Open(ss, KAFInet, KSockStream, KProtocolInetTCP); |
|
361 test(ret==KErrNone); |
|
362 TInetAddr addr(HostAddr); |
|
363 addr.SetPort(TCP_CONNECT_BACK_PORT+1); |
|
364 |
|
365 TRequestStatus status; |
|
366 ret=listener.Bind(addr); |
|
367 test(ret==KErrNone); |
|
368 |
|
369 test.Next(_L("Start listening")); |
|
370 |
|
371 ret=listener.Listen(5); |
|
372 test(ret==KErrNone); |
|
373 |
|
374 RSocket sock2; |
|
375 ret = sock2.Open(ss); |
|
376 test(ret==KErrNone); |
|
377 |
|
378 test.Next(_L("Accept")); |
|
379 |
|
380 TRequestStatus acceptStat; |
|
381 TInetAddr from; |
|
382 sock2.Accept(listener,from,acceptStat); |
|
383 |
|
384 test.Next(_L("Start connect")); |
|
385 |
|
386 addr.SetPort(TCP_CONNECT_BACK_PORT); |
|
387 TRequestStatus connectStat; |
|
388 sock1.Connect(addr,connectStat); |
|
389 |
|
390 User::WaitForRequest(connectStat); |
|
391 test(connectStat==KErrNone); |
|
392 User::WaitForRequest(acceptStat); |
|
393 test(acceptStat==KErrNone); |
|
394 |
|
395 TBuf_4000* temp=new TBuf_4000; |
|
396 TBuf_4000& outBuf=*temp; |
|
397 outBuf.SetLength(BUFFER_SIZE); |
|
398 |
|
399 TRequestStatus readStat; |
|
400 TRequestStatus writeStat; |
|
401 TBuf_4000* temp2=new TBuf_4000; |
|
402 TBuf_4000& inBuf=*temp2; |
|
403 inBuf.SetLength(BUFFER_SIZE); |
|
404 test.Printf(_L("\n")); |
|
405 |
|
406 test.Next(_L("Reading and Writing")); |
|
407 for (int i=0; i<128; i++) |
|
408 { |
|
409 |
|
410 sock1.Write(outBuf,writeStat); |
|
411 User::WaitForRequest(writeStat); |
|
412 test.Printf(_L("\rWritten %d K"),(i*BUFFER_SIZE)/1024); |
|
413 |
|
414 sock2.Read(inBuf,readStat); |
|
415 User::WaitForRequest(readStat); |
|
416 |
|
417 test(writeStat==KErrNone); |
|
418 test(readStat==KErrNone); |
|
419 |
|
420 test(outBuf.Compare(inBuf)==0); |
|
421 StripeMem(outBuf, 'A'+i%13, 'Z'); |
|
422 |
|
423 test.Printf(_L(" read %d K"),(i*BUFFER_SIZE)/1024); |
|
424 } |
|
425 |
|
426 test.Printf(_L("\n")); |
|
427 |
|
428 sock1.Close(); |
|
429 |
|
430 sock2.Close(); |
|
431 |
|
432 listener.Close(); |
|
433 |
|
434 delete temp; |
|
435 delete temp2; |
|
436 test.End(); |
|
437 } |
|
438 |
|
439 TInt E32Main() |
|
440 // |
|
441 // |
|
442 // |
|
443 { |
|
444 |
|
445 test.Title(); |
|
446 |
|
447 test.Start(_L("ESock tests")); |
|
448 |
|
449 test0(); |
|
450 test1(); |
|
451 test2(); |
|
452 test3(); |
|
453 test4(); |
|
454 test5(); |
|
455 |
|
456 test.End(); |
|
457 return 0; |
|
458 } |
|
459 |
|
460 |