|
1 // t_wsp.cpp |
|
2 // |
|
3 // Copyright (c) 2002 - 2010 Accenture. All rights reserved. |
|
4 // This component and the accompanying materials are made available |
|
5 // under the terms of the "Eclipse Public License v1.0" |
|
6 // which accompanies this distribution, and is available |
|
7 // at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 // |
|
9 // Initial Contributors: |
|
10 // Accenture - Initial contribution |
|
11 // |
|
12 |
|
13 #include <e32std.h> |
|
14 #include <e32base.h> |
|
15 #include <e32test.h> |
|
16 #include <e32svr.h> |
|
17 #include <e32math.h> |
|
18 #include <f32file.h> |
|
19 #include <es_sock.h> |
|
20 #include <in_sock.h> |
|
21 |
|
22 |
|
23 // |
|
24 // Constants. |
|
25 // |
|
26 |
|
27 //#define _HOME |
|
28 #ifdef _HOME |
|
29 const TInt KWebPort = 80; |
|
30 const TUint32 KSymbianWebServerIpAddress = INET_ADDR(192, 168, 0, 240); |
|
31 const TInt KLocalHostPort = 9999; |
|
32 const TUint32 KLocalHostIpAddress = INET_ADDR(127, 0, 0, 1); |
|
33 _LIT(KSymbianWebServerName, "main"); |
|
34 _LIT(KAltSymbianWebServerName, "sunshare.office"); |
|
35 _LIT8(KSmallWebPage,"GET http://sunshare.office/index.html \r\n\r\n"); |
|
36 _LIT(KLogFile, "c:\\t_wsp.txt"); |
|
37 #else |
|
38 const TInt KWebPort = 80; |
|
39 const TUint32 KSymbianWebServerIpAddress = INET_ADDR(81, 89, 143, 203); |
|
40 const TInt KLocalHostPort = 9999; |
|
41 const TUint32 KLocalHostIpAddress = INET_ADDR(127, 0, 0, 1); |
|
42 _LIT(KSymbianWebServerName, "www.symbian.com"); |
|
43 _LIT(KAltSymbianWebServerName, "www.symbian.com"); |
|
44 _LIT8(KSmallWebPage,"GET http://www.symbian.com/index.html \r\n\r\n"); |
|
45 _LIT(KLogFile, "c:\\t_wsp.txt"); |
|
46 #endif |
|
47 |
|
48 const TInt KTestTransferDataSize = 8192; |
|
49 |
|
50 // |
|
51 // Globals. |
|
52 // |
|
53 |
|
54 RFs FileServerSession; |
|
55 RFile LogFile; |
|
56 RSocketServ SocketServerSession; |
|
57 RTest TestConsole(_L("t_wsp")); |
|
58 |
|
59 |
|
60 // |
|
61 // Functions. |
|
62 // |
|
63 |
|
64 void RandomFill(TDes8& aBuf) |
|
65 { |
|
66 while (aBuf.Length() < (aBuf.MaxLength() - TInt(sizeof(TUint32)))) |
|
67 { |
|
68 TUint32 rand = Math::Random(); |
|
69 TPckgC<TUint32> randPckg(rand); |
|
70 aBuf.Append(randPckg); |
|
71 } |
|
72 while (aBuf.Length() < (aBuf.MaxLength())) |
|
73 { |
|
74 aBuf.Append(_L("p")); // Pack out last few bytes. |
|
75 } |
|
76 } |
|
77 |
|
78 void ReceiveHtmlL(RSocket& aSock) |
|
79 { |
|
80 HBufC8* webPage = HBufC8::NewMaxLC(4096); |
|
81 |
|
82 TPtr8 des(webPage->Des()); |
|
83 des = KSmallWebPage; |
|
84 |
|
85 TRequestStatus stat; |
|
86 aSock.Write(*webPage, stat); |
|
87 User::WaitForRequest(stat); |
|
88 User::LeaveIfError(stat.Int()); |
|
89 |
|
90 TSockXfrLength HTTPHeaderLen; |
|
91 TInt rcount = 0; |
|
92 |
|
93 TBuf<128> infoPrintBuf; |
|
94 FOREVER |
|
95 { |
|
96 des.Zero(); |
|
97 aSock.RecvOneOrMore(des,0,stat,HTTPHeaderLen); |
|
98 User::WaitForRequest(stat); |
|
99 if (stat != KErrNone) |
|
100 break; |
|
101 |
|
102 LogFile.Write(des); |
|
103 LogFile.Flush(); |
|
104 rcount += webPage->Length(); |
|
105 infoPrintBuf.Format(_L("\rRecved:%10d Bytes"), rcount); |
|
106 TestConsole.Printf(infoPrintBuf); |
|
107 } |
|
108 if (stat != KErrEof) |
|
109 { |
|
110 User::Leave(stat.Int()); |
|
111 } |
|
112 |
|
113 CleanupStack::PopAndDestroy(webPage); |
|
114 } |
|
115 |
|
116 void TcpOpenAndConnectC(RSocket& aSocket) |
|
117 { |
|
118 RHostResolver resolver; |
|
119 TestConsole(resolver.Open(SocketServerSession, KAfInet, KProtocolInetTcp) == KErrNone); |
|
120 CleanupClosePushL(resolver); |
|
121 TNameEntry nameEntry; |
|
122 TestConsole(resolver.GetByName(KSymbianWebServerName, nameEntry) == KErrNone); |
|
123 TNameRecord nameRecord(nameEntry()); |
|
124 TInetAddr addr(TInetAddr::Cast(nameRecord.iAddr).Address(), KWebPort); |
|
125 CleanupStack::PopAndDestroy(); // resolver. |
|
126 TestConsole(aSocket.Open(SocketServerSession, KAfInet, KSockStream, KProtocolInetTcp) == KErrNone); |
|
127 CleanupClosePushL(aSocket); |
|
128 TRequestStatus stat; |
|
129 aSocket.Connect(addr, stat); |
|
130 User::WaitForRequest(stat); |
|
131 TestConsole(stat.Int() == KErrNone); |
|
132 } |
|
133 |
|
134 void GetByAddress() |
|
135 { |
|
136 RHostResolver resolver; |
|
137 TestConsole(resolver.Open(SocketServerSession, KAfInet, KProtocolInetTcp) == KErrNone); |
|
138 CleanupClosePushL(resolver); |
|
139 TNameEntry nameEntry; |
|
140 TInetAddr testAddr(KSymbianWebServerIpAddress, KWebPort); |
|
141 testAddr.SetFamily(KAfInet); |
|
142 TestConsole(resolver.GetByAddress(testAddr, nameEntry) == KErrNone); |
|
143 TNameRecord nameRecord(nameEntry()); |
|
144 TestConsole(nameRecord.iName.CompareF(KAltSymbianWebServerName) == KErrNone); |
|
145 CleanupStack::PopAndDestroy(); // resolver. |
|
146 } |
|
147 |
|
148 void TcpActiveOpen() |
|
149 { |
|
150 RSocket socket; |
|
151 TcpOpenAndConnectC(socket); |
|
152 TRAPD(err, ReceiveHtmlL(socket)); |
|
153 TestConsole(err == KErrNone); |
|
154 CleanupStack::PopAndDestroy(); // socket. |
|
155 } |
|
156 |
|
157 void TcpGetLocalName() |
|
158 { |
|
159 RSocket socket; |
|
160 TcpOpenAndConnectC(socket); |
|
161 TSockAddr localName; |
|
162 socket.LocalName(localName); |
|
163 CleanupStack::PopAndDestroy(); // socket. |
|
164 } |
|
165 |
|
166 void TcpGetRemoteName() |
|
167 { |
|
168 RSocket socket; |
|
169 TcpOpenAndConnectC(socket); |
|
170 TSockAddr remoteName; |
|
171 socket.RemoteName(remoteName); |
|
172 CleanupStack::PopAndDestroy(); // socket. |
|
173 } |
|
174 |
|
175 void TcpListenAccept() |
|
176 { |
|
177 // Create a listening server socket and a socket to accept connections. |
|
178 RSocket listeningSocket; |
|
179 TestConsole(listeningSocket.Open(SocketServerSession, KAfInet, KSockStream, KProtocolInetTcp) == KErrNone); |
|
180 TInetAddr addr(KLocalHostIpAddress, KLocalHostPort); |
|
181 TestConsole(listeningSocket.Bind(addr) == KErrNone); |
|
182 TestConsole(listeningSocket.Listen(1) == KErrNone); |
|
183 RSocket serverConnectionSocket; |
|
184 TestConsole(serverConnectionSocket.Open(SocketServerSession) == KErrNone); |
|
185 TRequestStatus acceptStatus; |
|
186 listeningSocket.Accept(serverConnectionSocket, acceptStatus); |
|
187 |
|
188 // Create a client socket. |
|
189 RSocket clientSocket; |
|
190 TestConsole(clientSocket.Open(SocketServerSession, KAfInet, KSockStream, KProtocolInetTcp) == KErrNone); |
|
191 TRequestStatus connectStatus; |
|
192 clientSocket.Connect(addr,connectStatus); |
|
193 User::WaitForRequest(connectStatus); |
|
194 User::WaitForRequest(acceptStatus); |
|
195 TestConsole(connectStatus.Int() == KErrNone); |
|
196 TestConsole(acceptStatus.Int() == KErrNone); |
|
197 |
|
198 // Write some data from the client to the server. |
|
199 HBufC8* out = HBufC8::NewLC(KTestTransferDataSize); |
|
200 TPtr8 outPtr(out->Des()); |
|
201 RandomFill(outPtr); |
|
202 TRequestStatus writeStatus; |
|
203 TRequestStatus readStatus; |
|
204 HBufC8* in = HBufC8::NewLC(KTestTransferDataSize); |
|
205 TPtr8 inPtr(in->Des()); |
|
206 clientSocket.Write(*out, writeStatus); |
|
207 serverConnectionSocket.Read(inPtr, readStatus); |
|
208 User::WaitForRequest(writeStatus); |
|
209 User::WaitForRequest(readStatus); |
|
210 TestConsole(writeStatus.Int() == KErrNone); |
|
211 TestConsole(readStatus.Int() == KErrNone); |
|
212 TestConsole(in->Compare(*out) == KErrNone); |
|
213 clientSocket.Close(); |
|
214 serverConnectionSocket.Close(); |
|
215 |
|
216 // Re-connect the client socket. |
|
217 TestConsole(clientSocket.Open(SocketServerSession, KAfInet, KSockStream, KProtocolInetTcp) == KErrNone); |
|
218 TestConsole(serverConnectionSocket.Open(SocketServerSession) == KErrNone); |
|
219 listeningSocket.Accept(serverConnectionSocket, acceptStatus); |
|
220 clientSocket.Connect(addr,connectStatus); |
|
221 User::WaitForRequest(connectStatus); |
|
222 User::WaitForRequest(acceptStatus); |
|
223 TestConsole(connectStatus.Int() == KErrNone); |
|
224 TestConsole(acceptStatus.Int() == KErrNone); |
|
225 listeningSocket.Close(); |
|
226 |
|
227 // Re-sent the data. |
|
228 inPtr.Zero(); |
|
229 clientSocket.Read(inPtr, readStatus); |
|
230 serverConnectionSocket.Write(*out, writeStatus); |
|
231 User::WaitForRequest(writeStatus); |
|
232 User::WaitForRequest(readStatus); |
|
233 TestConsole(writeStatus.Int() == KErrNone); |
|
234 TestConsole(readStatus.Int() == KErrNone); |
|
235 TestConsole(in->Compare(*out) == KErrNone); |
|
236 |
|
237 // Cleanup. |
|
238 clientSocket.Close(); |
|
239 listeningSocket.Close(); |
|
240 CleanupStack::PopAndDestroy(2, out); |
|
241 } |
|
242 |
|
243 void UdpSendReceive() |
|
244 { |
|
245 // Create a socket to receive data. |
|
246 RSocket receiveSocket; |
|
247 TestConsole(receiveSocket.Open(SocketServerSession, KAfInet, KSockDatagram, KProtocolInetUdp) == KErrNone); |
|
248 TInetAddr receiveAddress(KLocalHostIpAddress, KLocalHostPort); |
|
249 TestConsole(receiveSocket.Bind(receiveAddress) == KErrNone); |
|
250 TBuf8<32> receiveBuf; |
|
251 receiveBuf.SetMax(); |
|
252 receiveBuf.Fill('a'); |
|
253 TRequestStatus receiveStatus; |
|
254 TInetAddr sendAddress; |
|
255 receiveSocket.RecvFrom(receiveBuf, sendAddress, 0, receiveStatus); |
|
256 |
|
257 // Create a socket to send data. |
|
258 RSocket sendSocket; |
|
259 TestConsole(sendSocket.Open(SocketServerSession, KAfInet, KSockDatagram, KProtocolInetUdp) == KErrNone); |
|
260 TBuf8<32> sendBuf; |
|
261 sendBuf.SetMax(); |
|
262 sendBuf.Fill('b'); |
|
263 TRequestStatus sendStatus; |
|
264 sendSocket.SendTo(sendBuf, receiveAddress, 0, sendStatus); |
|
265 |
|
266 // Wait and check. |
|
267 User::WaitForRequest(receiveStatus); |
|
268 User::WaitForRequest(sendStatus); |
|
269 TestConsole(receiveStatus.Int() == KErrNone); |
|
270 TestConsole(sendStatus.Int() == KErrNone); |
|
271 TestConsole(receiveBuf.Compare(sendBuf) == KErrNone); |
|
272 |
|
273 // Cleanup. |
|
274 receiveSocket.Close(); |
|
275 sendSocket.Close(); |
|
276 } |
|
277 |
|
278 void GetSetOption() |
|
279 { |
|
280 RSocket socket; |
|
281 TcpOpenAndConnectC(socket); |
|
282 TInt val; |
|
283 TestConsole(socket.GetOpt(KSoTcpNoDelay, KSolInetTcp, val) == KErrNone); |
|
284 val = !val; |
|
285 TestConsole(socket.SetOpt(KSoTcpNoDelay, KSolInetTcp, val) == KErrNone); |
|
286 TInt newVal; |
|
287 TestConsole(socket.GetOpt(KSoTcpNoDelay, KSolInetTcp, newVal) == KErrNone); |
|
288 TestConsole(val == newVal); |
|
289 TRAPD(err, ReceiveHtmlL(socket)); |
|
290 TestConsole(err == KErrNone); |
|
291 CleanupStack::PopAndDestroy(); // socket. |
|
292 } |
|
293 |
|
294 TInt InitialiseLC() |
|
295 { |
|
296 User::LeaveIfError(FileServerSession.Connect()); |
|
297 CleanupClosePushL(FileServerSession); |
|
298 User::LeaveIfError(LogFile.Replace(FileServerSession, KLogFile,EFileShareAny | EFileWrite | EFileStreamText)); |
|
299 CleanupClosePushL(LogFile); |
|
300 User::LeaveIfError(SocketServerSession.Connect()); |
|
301 CleanupClosePushL(SocketServerSession); |
|
302 return 3; |
|
303 } |
|
304 |
|
305 GLDEF_C void RunTestsL() |
|
306 { |
|
307 TInt numItemsOnCleanupStack = InitialiseLC(); |
|
308 // GetByAddress(); |
|
309 TcpActiveOpen(); |
|
310 TcpGetLocalName(); |
|
311 TcpGetRemoteName(); |
|
312 TcpListenAccept(); |
|
313 UdpSendReceive(); |
|
314 GetSetOption(); |
|
315 CleanupStack::PopAndDestroy(numItemsOnCleanupStack); |
|
316 TestConsole.Close(); |
|
317 } |
|
318 |
|
319 GLDEF_C TInt E32Main() |
|
320 { |
|
321 __UHEAP_MARK; |
|
322 CActiveScheduler* scheduler = new CActiveScheduler; |
|
323 if (scheduler) |
|
324 { |
|
325 CActiveScheduler::Install(scheduler); |
|
326 CTrapCleanup* cleanup = CTrapCleanup::New(); |
|
327 if (cleanup) |
|
328 { |
|
329 TRAPD(err, RunTestsL()); |
|
330 delete cleanup; |
|
331 } |
|
332 delete scheduler; |
|
333 } |
|
334 __UHEAP_MARKEND; |
|
335 return KErrNone; |
|
336 } |