|
1 // Copyright (c) 2003-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 <minputstream.h> |
|
17 #include <moutputstream.h> |
|
18 |
|
19 #include "CTestServerStreamManager.h" |
|
20 #include "httptestutils.h" |
|
21 #include "MPipeliningTestCase.h" |
|
22 #include "ctestcasepipelinefallback.h" |
|
23 |
|
24 const TInt KTimeOut = 50000000; |
|
25 const TInt KResponseBatchSize = 5; |
|
26 _LIT8(KTxtConnectionClose, "Connection: Close"); |
|
27 |
|
28 CTestServerStreamManager* CTestServerStreamManager::NewL(CHTTPTestUtils& aTestUtils, TInt aConnectionIndex, MPipeliningTestCase* aTestCase, MInputStream* aInputStream, MOutputStream* aOutputStream) |
|
29 { |
|
30 CTestServerStreamManager* self = new (ELeave) CTestServerStreamManager(aTestUtils, aConnectionIndex, aTestCase, aInputStream, aOutputStream); |
|
31 CleanupStack::PushL(self); |
|
32 self->ConstructL(); |
|
33 CleanupStack::Pop(self); |
|
34 return self; |
|
35 } |
|
36 |
|
37 CTestServerStreamManager::CTestServerStreamManager(CHTTPTestUtils& aTestUtils, TInt aConnectionIndex, MPipeliningTestCase* aTestCase, MInputStream* aInputStream, MOutputStream* aOutputStream) |
|
38 : CTimer(EPriorityNormal), iTestUtils(aTestUtils), iInputStream(aInputStream), iOutputStream(aOutputStream), iTestCase(aTestCase), iConnectionIndex(aConnectionIndex) |
|
39 { |
|
40 CActiveScheduler::Add(this); |
|
41 |
|
42 iTransCount = iTestCase->TransactionCount(iConnectionIndex); |
|
43 |
|
44 iInputStream->Bind(*this); |
|
45 iOutputStream->Bind(*this); |
|
46 } |
|
47 |
|
48 CTestServerStreamManager::~CTestServerStreamManager() |
|
49 { |
|
50 Cancel(); |
|
51 if(iInputStream!=NULL) |
|
52 iInputStream->Close(); |
|
53 if(iOutputStream!=NULL) |
|
54 iOutputStream->Close(); |
|
55 delete iDataStore; |
|
56 delete iDataToSend; |
|
57 |
|
58 delete iHttpTimer; |
|
59 delete iASW; |
|
60 } |
|
61 |
|
62 void CTestServerStreamManager::ConstructL() |
|
63 { |
|
64 CTimer::ConstructL(); |
|
65 iHttpTimer = new(ELeave) CHttpTimer(*this); |
|
66 iASW = new(ELeave) CActiveSchedulerWait(); |
|
67 } |
|
68 |
|
69 // From MInputStreamObserver |
|
70 void CTestServerStreamManager::ReceivedDataIndL(const TDesC8& aBuffer) |
|
71 { |
|
72 // Got data |
|
73 _LIT(KTxtGotData, "Server - received request data."); |
|
74 iTestUtils.LogIt(KTxtGotData()); |
|
75 iTestUtils.DumpData(aBuffer, ETrue); |
|
76 |
|
77 if(iDataStore==NULL) |
|
78 iDataStore = aBuffer.AllocL(); // First data chunk received |
|
79 else |
|
80 { |
|
81 // Already got data before so append the data to the existing data |
|
82 TInt newLength = (iDataStore->Length()) + aBuffer.Length(); |
|
83 iDataStore = iDataStore->ReAllocL( newLength ); |
|
84 TPtr8 buf = iDataStore->Des(); |
|
85 buf.Append(aBuffer); |
|
86 } |
|
87 |
|
88 iInputStream->ReceivedDataRes(); |
|
89 |
|
90 _LIT(KTxtTitle, "Defect Fix INC036954"); |
|
91 if (iTestCase->TestCaseName().Match(KTxtTitle) == 0) |
|
92 { |
|
93 iInputStream->ShutdownReq(); |
|
94 } |
|
95 else |
|
96 { |
|
97 // Try processing the requests |
|
98 SendDataL(); |
|
99 } |
|
100 } |
|
101 |
|
102 void CTestServerStreamManager::SecureServerCnf() |
|
103 { |
|
104 } |
|
105 |
|
106 void CTestServerStreamManager::InputStreamCloseInd(TInt aError) |
|
107 { |
|
108 _LIT(KTxtInputClosed, "Server - Input stream %d closed. Error: %d."); |
|
109 iTestUtils.LogIt(KTxtInputClosed(), iConnectionIndex+1, aError); |
|
110 |
|
111 iInputStream = NULL; |
|
112 } |
|
113 |
|
114 void CTestServerStreamManager::MInputStreamObserver_Reserved() |
|
115 { |
|
116 User::Invariant(); |
|
117 } |
|
118 |
|
119 // From MOutputStreamObserver |
|
120 void CTestServerStreamManager::SendDataCnfL() |
|
121 { |
|
122 _LIT(KTxtDataSent, "Server - Data Sent."); |
|
123 iTestUtils.LogIt(KTxtDataSent()); |
|
124 if( iMoreResponseBatches ) |
|
125 { |
|
126 // If there is more batches to process try and process them |
|
127 iMoreResponseBatches = EFalse; |
|
128 TRAPD(err, SendDataL()); |
|
129 if(err!=KErrNone) |
|
130 { |
|
131 _LIT(KTxtSendError, "Server - Error %d sending data. Closing stream."); |
|
132 iTestUtils.LogIt(KTxtSendError(), err); |
|
133 iInputStream->ShutdownReq(); |
|
134 } |
|
135 } |
|
136 else |
|
137 { |
|
138 delete iDataToSend; |
|
139 iDataToSend = NULL; |
|
140 |
|
141 // Do we need to close the connection |
|
142 if(iCloseConnection) |
|
143 { |
|
144 _LIT(KTxtCloseConn, "Server - Closing connection."); |
|
145 iTestUtils.LogIt(KTxtCloseConn()); |
|
146 |
|
147 if( (iTestCase->TestCaseName().Match(_L("Test Case 3")) == 0) || (iTestCase->TestCaseName().Match(_L("CINC073400")) == 0) || (iTestCase->TestCaseName().Match(_L("Test Case 19")) == 0) ) |
|
148 { |
|
149 TTimeIntervalMicroSeconds32 time(5000000); |
|
150 iHttpTimer->After(time); |
|
151 iASW->Start(); |
|
152 } |
|
153 |
|
154 if(iInputStream) |
|
155 iInputStream->ShutdownReq(); |
|
156 } |
|
157 _LIT(KTxtTitle, "Defect Fix CINC077703"); |
|
158 if (iTestCase->TestCaseName().Match(KTxtTitle) == 0) |
|
159 { |
|
160 iInputStream->ShutdownReq(); |
|
161 } |
|
162 |
|
163 } |
|
164 } |
|
165 |
|
166 void CTestServerStreamManager::SecureClientCnf() |
|
167 { |
|
168 } |
|
169 |
|
170 void CTestServerStreamManager::OutputStreamCloseInd(TInt aError) |
|
171 { |
|
172 _LIT(KTxtOutputClosed, "Server - Output stream %d closed. Error: %d."); |
|
173 iTestUtils.LogIt(KTxtOutputClosed(), iConnectionIndex+1, aError); |
|
174 |
|
175 iOutputStream = NULL; |
|
176 } |
|
177 |
|
178 void CTestServerStreamManager::MOutputStreamObserver_Reserved() |
|
179 { |
|
180 } |
|
181 |
|
182 TBool CTestServerStreamManager::ProcessRequestL() |
|
183 { |
|
184 TBool processingRequest = ETrue; |
|
185 TInt currentBatch = 0; |
|
186 while( (processingRequest && (iCurrentTrans < iTransCount)) && (currentBatch<KResponseBatchSize) ) |
|
187 { |
|
188 // Do we have enough data to respond to the current transaction? |
|
189 TPtrC8 rawRequest = iTestCase->GetRawRequest(iConnectionIndex, iCurrentTrans); |
|
190 TInt requestLength = rawRequest.Length(); |
|
191 TPtrC8 dataWindow = iDataStore->Mid(iDataPos); |
|
192 // Test pipeline fallback scenario |
|
193 _LIT(KTxtTitle, "Test Case Pipeline Fallback"); |
|
194 if (iTestCase->TestCaseName().Match(KTxtTitle) == 0) |
|
195 { |
|
196 CTestCasePipelineFallback* pipelineFallback = (CTestCasePipelineFallback*)(iTestCase); |
|
197 if(pipelineFallback->FallingBack()) |
|
198 { |
|
199 _LIT(KTxtSendError, "Fallingback ."); |
|
200 iTestUtils.LogIt(KTxtSendError()); |
|
201 |
|
202 // we are fallingback. We should receive the exact request size as we |
|
203 // will be processing the request one by one |
|
204 if(dataWindow.Length() > requestLength) |
|
205 { |
|
206 User::Leave(KErrCancel); |
|
207 } |
|
208 } |
|
209 else |
|
210 { |
|
211 _LIT(KTxtSendError, "Not Fallingback."); |
|
212 iTestUtils.LogIt(KTxtSendError()); |
|
213 |
|
214 // If we are not fallingback then we need to close the connection twice |
|
215 // We are pipelining as well as batching. Shutdown the connection |
|
216 // First time we get only one request. Just process. |
|
217 if(dataWindow.Length() > requestLength) |
|
218 { |
|
219 _LIT(KTxtSendError, "Incremented and shutdown."); |
|
220 iTestUtils.LogIt(KTxtSendError()); |
|
221 |
|
222 // We have to shut down 2 times |
|
223 iInputStream->ShutdownReq(); |
|
224 pipelineFallback->IncFallingBack(); |
|
225 break; |
|
226 } |
|
227 } |
|
228 } |
|
229 |
|
230 if( requestLength <= dataWindow.Length() ) |
|
231 { |
|
232 // Check that the raw request and the actual request match |
|
233 if( dataWindow.FindF(rawRequest) != 0 ) |
|
234 { |
|
235 _LIT(KTxtRequestDataMismatch, "Server - Fail. Request data for transaction %d does not match expected data."); |
|
236 iTestUtils.LogIt(KTxtRequestDataMismatch(), iCurrentTrans+1); |
|
237 _LIT(KTxtExpectedData, "Server - Expected data: %S"); |
|
238 iTestUtils.LogIt(KTxtExpectedData(), &rawRequest); |
|
239 iTestUtils.LogIt(KTxtExpectedData(), &dataWindow); |
|
240 |
|
241 User::Leave(KErrNotFound); |
|
242 } |
|
243 |
|
244 // Prepare the response data to send |
|
245 iDataPos += requestLength; |
|
246 processingRequest = ETrue; |
|
247 TPtrC8 rawResponse = iTestCase->GetRawResponse(iConnectionIndex, iCurrentTrans); |
|
248 if(iDataToSend==NULL) |
|
249 iDataToSend = rawResponse.AllocL(); |
|
250 else |
|
251 { |
|
252 TInt responseLength = rawResponse.Length(); |
|
253 iDataToSend = iDataToSend->ReAllocL( (iDataToSend->Length()) + responseLength ); |
|
254 TPtr8 buffer = iDataToSend->Des(); |
|
255 buffer.Append(rawResponse); |
|
256 } |
|
257 |
|
258 // Check for a Connection: Close in the request |
|
259 iCloseConnection = IsConnectionCloseInData(rawRequest, rawResponse); |
|
260 if(iCloseConnection) |
|
261 processingRequest = EFalse; |
|
262 |
|
263 ++iCurrentTrans; |
|
264 ++currentBatch; |
|
265 } |
|
266 else |
|
267 { |
|
268 // No more requests can be processed |
|
269 processingRequest = EFalse; |
|
270 } |
|
271 |
|
272 // Flag that we have more processing to do with we are still processing the data |
|
273 // but we have reached the maximum batch size. |
|
274 if( processingRequest && currentBatch==KResponseBatchSize) |
|
275 iMoreResponseBatches = ETrue; |
|
276 else |
|
277 iMoreResponseBatches = EFalse; |
|
278 |
|
279 } |
|
280 |
|
281 if(iDataToSend!=NULL) |
|
282 return ETrue; |
|
283 |
|
284 return EFalse; |
|
285 } |
|
286 |
|
287 TBool CTestServerStreamManager::IsConnectionCloseInData(const TDesC8& aRequest, const TDesC8& aResponse) const |
|
288 { |
|
289 if( (aRequest.FindF(KTxtConnectionClose()) != KErrNotFound) || (aResponse.FindF(KTxtConnectionClose()) != KErrNotFound) ) |
|
290 { |
|
291 // Either the request or response has Connection: Close header |
|
292 _LIT(KTxtConnectionCloseDetected, "Server - Connection: Close detected in request or response, transaction: %d"); |
|
293 iTestUtils.LogIt(KTxtConnectionCloseDetected(), iCurrentTrans+1); |
|
294 |
|
295 return ETrue; |
|
296 } |
|
297 |
|
298 return EFalse; |
|
299 } |
|
300 |
|
301 void CTestServerStreamManager::SendDataL() |
|
302 { |
|
303 Cancel(); |
|
304 |
|
305 _LIT(KTcTitle, "CRecvTimeOut"); |
|
306 if (iTestCase->TestCaseName().Match(KTcTitle) == 0) |
|
307 { |
|
308 _LIT(KTxtTimeout, "Server - Data Send Timeout."); |
|
309 iTestUtils.LogIt(KTxtTimeout()); |
|
310 After(61000000); |
|
311 return; |
|
312 } |
|
313 |
|
314 if( !iMoreResponseBatches ) |
|
315 { |
|
316 // Try processing the requests |
|
317 if( ProcessRequestL() ) |
|
318 { |
|
319 // We have enough, send the response. |
|
320 iOutputStream->SendDataReqL(*iDataToSend); |
|
321 |
|
322 _LIT(KTxtSendData, "Server - Sending response data."); |
|
323 iTestUtils.LogIt(KTxtSendData()); |
|
324 iTestUtils.DumpData(*iDataToSend, ETrue); |
|
325 } |
|
326 else |
|
327 { |
|
328 After(KTimeOut); // Start the inactivity timer |
|
329 } |
|
330 |
|
331 } |
|
332 } |
|
333 |
|
334 void CTestServerStreamManager::RunL() |
|
335 { |
|
336 _LIT(KTcTitle, "CRecvTimeOut"); |
|
337 if (iTestCase->TestCaseName().Match(KTcTitle) == 0) |
|
338 { |
|
339 _LIT(KTxtTimeout, "TC-Receive Timeout Timer RunL."); |
|
340 iTestUtils.LogIt(KTxtTimeout()); |
|
341 return; |
|
342 } |
|
343 |
|
344 |
|
345 if( iStatus.Int() == KErrNone ) |
|
346 { |
|
347 // The connection has timed out. |
|
348 _LIT(KTxtTimedOut, "Server - Fail, Connection %d timed out. Current transaction: %d"); |
|
349 iTestUtils.LogIt(KTxtTimedOut(), iConnectionIndex+1, iCurrentTrans+1); |
|
350 |
|
351 iInputStream->ShutdownReq(); |
|
352 } |
|
353 } |
|
354 |
|
355 MHttpResponse* CTestServerStreamManager::CurrentResponse() |
|
356 { |
|
357 return NULL; |
|
358 } |
|
359 |
|
360 void CTestServerStreamManager::OnReceiveTimeOut() |
|
361 { |
|
362 |
|
363 } |
|
364 |
|
365 void CTestServerStreamManager::OnSendTimeOut() |
|
366 { |
|
367 |
|
368 } |
|
369 |
|
370 TInt CTestServerStreamManager::SendTimeOutVal() |
|
371 { |
|
372 return 0; |
|
373 } |
|
374 |
|
375 void CTestServerStreamManager::TimeOut() |
|
376 { |
|
377 iASW->AsyncStop(); |
|
378 } |
|
379 |
|
380 |
|
381 CHttpTimer::CHttpTimer(MTimerClient& aClient) |
|
382 :CActive(EPriorityStandard), iClient(aClient) |
|
383 { |
|
384 CActiveScheduler::Add(this); |
|
385 iTimer.CreateLocal(); |
|
386 } |
|
387 |
|
388 CHttpTimer::~CHttpTimer() |
|
389 { |
|
390 Cancel(); |
|
391 iTimer.Close(); |
|
392 } |
|
393 |
|
394 void CHttpTimer::After(TTimeIntervalMicroSeconds32 anInterval) |
|
395 { |
|
396 iTimer.After(iStatus, anInterval); |
|
397 SetActive(); |
|
398 } |
|
399 |
|
400 void CHttpTimer::DoCancel() |
|
401 { |
|
402 |
|
403 } |
|
404 |
|
405 void CHttpTimer::RunL() |
|
406 { |
|
407 iClient.TimeOut(); |
|
408 } |