1 krisbash 1.1 /*
2 **==============================================================================
3 **
4 ** Open Management Infrastructure (OMI)
5 **
6 ** Copyright (c) Microsoft Corporation
7 **
8 ** Licensed under the Apache License, Version 2.0 (the "License"); you may not
9 ** use this file except in compliance with the License. You may obtain a copy
10 ** of the License at
11 **
12 ** http://www.apache.org/licenses/LICENSE-2.0
13 **
14 ** THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 ** KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
16 ** WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
17 ** MERCHANTABLITY OR NON-INFRINGEMENT.
18 **
19 ** See the Apache 2 License for the specific language governing permissions
20 ** and limitations under the License.
21 **
22 krisbash 1.1 **==============================================================================
23 */
24
25 #include <vector>
26 #include <cstdlib>
27 #include <ut/ut.h>
28 #include <pal/thread.h>
29 #include <http/httpcommon.h>
30 #include <base/result.h>
31 #include <base/Strand.h>
32 #include <base/log.h>
33
34 static MI_Uint16 PORT = ut::getUnittestPortNumber() + 10;
35
36 using namespace std;
37
38 STRAND_DEBUGNAME( TestHttp );
39
40 /*************************************/
41 /* local data */
42 static Http* s_http;
43 krisbash 1.1 static bool s_stop;
44 static Thread s_t;
45
46 #if defined(_MSC_VER)
47 #undef BEGIN_EXTERNC
48 #undef END_EXTERNC
49 #define BEGIN_EXTERNC
50 #define END_EXTERNC
51 #endif
52
53 NitsSetup(TestHttpSetup)
54 {
55 Sock_Start();
56 PORT++;
57 }
58 NitsEndSetup
59
60 NitsCleanup(TestHttpSetup)
61 {
62 Sock_Stop();
63 }
64 krisbash 1.1 NitsEndCleanup
65
66 /* helper functions */
67 BEGIN_EXTERNC
68 static void* MI_CALL _HTTPServerProc(void* )
69 {
70 Sock_Start();
71 // pump messages
72 for (; !s_stop; )
73 Http_Run( s_http, SELECT_BASE_TIMEOUT_MSEC * 1000 );
74
75 Sock_Stop();
76 return 0;
77 }
78 END_EXTERNC
79
80 BEGIN_EXTERNC
81 static void _DeleteHttp()
82 {
83 TEST_ASSERT( MI_RESULT_OK == Http_Delete(s_http) );
84
85 krisbash 1.1 memset(&s_t, 0, sizeof(Thread));
86 s_http = 0;
87 s_stop = false;
88 }
89
90 static MI_Result _StartHTTP_Server(
91 OpenCallback callbackOnNewConnection,
92 void* callbackData,
93 const char* sslCipherSuite,
94 const HttpOptions* options = 0)
95 {
96 /* create a server */
97 if (!TEST_ASSERT( MI_RESULT_OK == Http_New_Server(
98 &s_http, 0, PORT, 0, sslCipherSuite, (Server_SSL_Options) 0,
99 callbackOnNewConnection,
100 callbackData, options) ))
101 return MI_RESULT_FAILED;
102
103 /* create a thread for message consumption */
104 s_stop = false;
105
106 krisbash 1.1 if (!TEST_ASSERT(MI_RESULT_OK == Thread_CreateJoinable(
107 &s_t, (ThreadProc) _HTTPServerProc, NULL, 0)))
108 {
109 _DeleteHttp();
110 return MI_RESULT_FAILED;
111 }
112
113 return MI_RESULT_OK;
114 }
115
116 static void _StopHTTP_Server()
117 {
118 PAL_Uint32 ret;
119
120 s_stop = true;
121 TEST_ASSERT( Thread_Join(&s_t, &ret) == 0 );
122 Thread_Destroy(&s_t);
123 _DeleteHttp();
124 }
125 END_EXTERNC
126
127 krisbash 1.1 struct ThreadParam
128 {
129 string messageToSend;
130 size_t bytesToSendPerOperation;
131 string response;
132 bool gotRsp;
133 };
134
135 static Sock SockConnectLocal(unsigned short port)
136 {
137 Sock sock;
138 Addr addr;
139 MI_Result r;
140
141 Sock_Start();
142
143 // Initialize address (connect using loopback).
144 r = Addr_Init(&addr, "127.0.0.1", port, MI_FALSE);
145 TEST_ASSERT(r == MI_RESULT_OK);
146
147 // Create client socket.
148 krisbash 1.1 r = Sock_Create(&sock, MI_FALSE);
149 TEST_ASSERT(r == MI_RESULT_OK);
150
151 r = Sock_Connect(sock, &addr);
152 TEST_ASSERT(r == MI_RESULT_OK);
153 return sock;
154 }
155
156 /* simple http client */
157 BEGIN_EXTERNC
158 static void* MI_CALL _http_client_proc(void* param)
159 {
160 ThreadParam* p = (ThreadParam*)param;
161
162 Sock sock;
163 MI_Result r;
164
165 Sock_Start();
166
167 // Initialize address (connect using loopback).
168 sock = SockConnectLocal(PORT);
169 krisbash 1.1
170 size_t sent = 0;
171 size_t size_left = p->messageToSend.size();
172 const char* buf = p->messageToSend.c_str();
173
174 while ( size_left )
175 {
176 size_t wantToSend = min(p->bytesToSendPerOperation, size_left);
177 r = Sock_Write(sock, buf, wantToSend, &sent);
178
179 //printf("size_left %d, r %d, sent %d, want-send %d\n", size_left, r, sent, wantToSend);
180 if ( r != MI_RESULT_OK)
181 {
182 //printf("size_left %d, r %d, want write %d, sent %d, le %d\n", (int)size_left, (int)r, (int)wantToSend, (int)sent, Sock_GetLastError());
183 ut::sleep_ms( 7000 );
184
185 }
186
187 TEST_ASSERT(r == MI_RESULT_OK);
188 // printf("s: %d\n", (int)sent);
189 size_left -= sent;
190 krisbash 1.1 buf += sent;
191 ut::sleep_ms( 1 );
192 }
193
194 // read response
195 char r_buf[1024];
196 size_t read = 0;
197 r = Sock_Read(sock, r_buf, sizeof(r_buf), &read);
198
199
200 //if (r) printf("s,r: %d, err %d\n", (int)read, Sock_GetLastError());
201
202 TEST_ASSERT(r == MI_RESULT_OK);
203
204 p->response = string(r_buf, read);
205
206 p->gotRsp = true;
207 Sock_Close(sock);
208
209 Sock_Stop();
210 return 0;
211 krisbash 1.1 }
212 END_EXTERNC
213
214 BEGIN_EXTERNC
215 static void* MI_CALL http_client_proc(void* param)
216 {
217 try
218 {
219 return _http_client_proc(param);
220 }
221 catch (ut::UnittestException ex)
222 {
223 ex.m_testcase = "--http_client_proc"; UT_ASSERT_FAILED_MSG(ex.m_testcase.c_str());
224 }
225 return 0;
226 }
227 END_EXTERNC
228
229 struct CallbackStruct
230 {
231 Strand strand;
232 krisbash 1.1
233 string contentType;
234 string charset;
235 string authorization;
236 string username;
237 string password;
238 size_t contentLength;
239 string data;
240 string response;
241
242 CallbackStruct() : contentLength(0){}
243 };
244
245 BEGIN_EXTERNC
246 static void _StrandTestPost( _In_ Strand* self_, _In_ Message* msg )
247 {
248 CallbackStruct* data = (CallbackStruct*)self_;
249 HttpRequestMsg* request = (HttpRequestMsg *)msg;
250 HttpResponseMsg* msgRsp = NULL;
251
252 TEST_ASSERT( HttpRequestMsgTag == msg->tag );
253 krisbash 1.1
254 if (request->headers->contentType)
255 data->contentType = request->headers->contentType;
256
257 if (request->headers->charset)
258 data->charset = request->headers->charset;
259
260 if (request->headers->authorization)
261 data->authorization = request->headers->authorization;
262 if (request->headers->username)
263 data->username = request->headers->username;
264 if (request->headers->password)
265 data->password = request->headers->password;
266
267 data->contentLength = request->headers->contentLength;
268
269 data->data = string( (char*) ((request->page)+1), (size_t)(request->page)->u.s.size);
270
271 Page* rsp = (Page*)PAL_Malloc(sizeof(Page) + data->response.size());
272
273 TEST_ASSERT(rsp);
274 krisbash 1.1
275 if(rsp)
276 {
277 rsp->u.s.size = data->response.size();
278 memcpy(rsp+1, data->response.c_str(), data->response.size());
279
280 msgRsp = HttpResponseMsg_New(rsp, HTTP_ERROR_CODE_OK);
281
282 TEST_ASSERT( NULL != msg );
283 }
284 Strand_Ack( &data->strand );
285 if(msgRsp)
286 {
287 Strand_Post( &data->strand, &msgRsp->base );
288 }
289 else
290 {
291 Strand_Close(&data->strand);
292 }
293 }
294
295 krisbash 1.1 static void _StrandTestAck( _In_ Strand* self)
296 {
297 // do nothing
298 }
299
300 static void _StrandTestClose( _In_ Strand* self)
301 {
302 // close other
303 Strand_Close( self );
304 }
305
306 static void _StrandTestFinished( _In_ Strand* self)
307 {
308 // do nothing
309 }
310
311 static StrandFT strandUserFT1 = {
312 _StrandTestPost,
313 NULL,
314 _StrandTestAck,
315 NULL,
316 krisbash 1.1 _StrandTestClose,
317 _StrandTestFinished,
318 NULL,
319 NULL,
320 NULL,
321 NULL,
322 NULL,
323 NULL };
324
325 static void _callback(
326 _Inout_ InteractionOpenParams* interactionParams )
327 {
328 CallbackStruct* data = (CallbackStruct*)interactionParams->callbackData;
329
330 UT_ASSERT (data != NULL);
331 UT_ASSERT (interactionParams->msg == NULL);
332
333 Strand_Init( STRAND_DEBUG( TestHttp ) &data->strand, &strandUserFT1, 0, interactionParams );
334 }
335
336 static void _StrandTestPost2( _In_ Strand* self_, _In_ Message* msg )
337 krisbash 1.1 {
338 UT_ASSERT_FAILED_MSG("should be never called");
339 }
340
341 static StrandFT strandUserFT2 = {
342 _StrandTestPost2,
343 NULL,
344 _StrandTestAck,
345 NULL,
346 _StrandTestClose,
347 _StrandTestFinished,
348 NULL,
349 NULL,
350 NULL,
351 NULL,
352 NULL,
353 NULL };
354
355 static void _callback2(
356 _Inout_ InteractionOpenParams* interactionParams )
357 {
358 krisbash 1.1 CallbackStruct* data = (CallbackStruct*)interactionParams->callbackData;
359
360 UT_ASSERT (data != NULL);
361 UT_ASSERT (interactionParams->msg == NULL);
362
363 Strand_Init( STRAND_DEBUG( TestHttp ) &data->strand, &strandUserFT2, 0, interactionParams );
364 }
365 END_EXTERNC
366
367 NitsTestWithSetup(TestHttpHappyPass, TestHttpSetup)
368 {
369 NitsDisableFaultSim;
370
371 Http* http = 0;
372 CallbackStruct cb;
373
374 cb.response = "Response";
375
376 /* create a server */
377 if(!TEST_ASSERT( MI_RESULT_OK == Http_New_Server(
378 &http, 0, PORT, 0, NULL, (Server_SSL_Options) 0,
379 krisbash 1.1 _callback,
380 &cb,
381 NULL) ))
382 return;
383
384 /* create a client */
385 ThreadParam param;
386 Thread t;
387
388 param.messageToSend =
389 "POST /wsman HTTP/1.1\r\n"
390 "Content-Type: application/soap+xml;charset=UTF-8\r\n"
391 "User-Agent: Microsoft WinRM Client\r\n"
392 "Host: localhost:7778\r\n"
393 "Content-Length: 5\r\n"
394 "Authorization: auth\r\n"
395 "\r\n"
396 "Hello";
397 param.bytesToSendPerOperation = 30000;
398 param.gotRsp = false;
399
400 krisbash 1.1 int threadCreatedResult = Thread_CreateJoinable(
401 &t, (ThreadProc)http_client_proc, NULL, ¶m);
402 TEST_ASSERT(MI_RESULT_OK == threadCreatedResult);
403 if(threadCreatedResult != MI_RESULT_OK)
404 goto EndTest;
405
406
407 // pump messages
408 for (int i = 0; !param.gotRsp && i < 10000; i++ )
409 Http_Run( http, SELECT_BASE_TIMEOUT_MSEC * 1000 );
410
411 // wait for completion and check that
412 PAL_Uint32 ret;
413 TEST_ASSERT( Thread_Join( &t, &ret ) == 0 );
414 Thread_Destroy( &t );
415
416 // check messages
417
418 TEST_ASSERT( cb.authorization == "auth" );
419 TEST_ASSERT( cb.contentType == "application/soap+xml" );
420 TEST_ASSERT( cb.charset == "UTF-8" );
421 krisbash 1.1 TEST_ASSERT( cb.contentLength == 5 );
422 TEST_ASSERT( cb.data == "Hello" );
423
424 EndTest:
425 //TEST_ASSERT( param.response.find("Response") != string::npos );
426
427 TEST_ASSERT( MI_RESULT_OK == Http_Delete(http) );
428 }
429 NitsEndTest
430
431 NitsTestWithSetup(TestHttp_BigLoad, TestHttpSetup)
432 {
433 NitsDisableFaultSim;
434
435 Http* http = 0;
436 CallbackStruct cb;
437
438 cb.response = string(256 * 1024, '*');
439
440
441 /* create a server */
442 krisbash 1.1 if(!TEST_ASSERT( MI_RESULT_OK == Http_New_Server(
443 &http, 0, PORT, 0, NULL, (Server_SSL_Options) 0,
444 _callback,
445 &cb,
446 NULL) ))
447 return;
448
449 /* create a client */
450 ThreadParam param;
451 Thread t;
452
453 param.messageToSend =
454 "POST /wsman HTTP/1.1\r\n"
455 "Content-Type: text\r\n"
456 "User-Agent: Microsoft WinRM Client\r\n"
457 "Host: localhost:7778\r\n"
458 "Content-Length: 32999\r\n"
459 "Authorization: auth\r\n"
460 "\r\n";
461
462 param.messageToSend += string(32999, '.');
463 krisbash 1.1
464 param.bytesToSendPerOperation = 8000;
465 param.gotRsp = false;
466 int threadCreatedResult = Thread_CreateJoinable(
467 &t, (ThreadProc)http_client_proc, NULL, ¶m);
468 TEST_ASSERT(MI_RESULT_OK == threadCreatedResult);
469 if(threadCreatedResult != MI_RESULT_OK)
470 goto TestEnd;
471
472 // pump messages
473 for (int i = 0; !param.gotRsp && i < 10000; i++ )
474 Http_Run( http, SELECT_BASE_TIMEOUT_MSEC * 1000 );
475
476 //printf("out of run\n");
477
478 // wait for completion and check that
479 PAL_Uint32 ret;
480 TEST_ASSERT( Thread_Join( &t, &ret ) == 0 );
481 Thread_Destroy( &t );
482
483
484 krisbash 1.1 // check messages
485
486 TEST_ASSERT( cb.authorization == "auth" );
487 TEST_ASSERT( cb.password == "" );
488 TEST_ASSERT( cb.username == "" );
489 TEST_ASSERT( cb.contentType == "text" );
490 TEST_ASSERT( cb.charset == "" );
491 TEST_ASSERT( cb.contentLength == 32999 );
492 TEST_ASSERT( cb.data == string(32999, '.') );
493
494 //TEST_ASSERT( param.response.find("Response") != string::npos );
495 TestEnd:
496 TEST_ASSERT( MI_RESULT_OK == Http_Delete(http) );
497 }
498 NitsEndTest
499
500 NitsTestWithSetup(TestHttp_QuotedCharset, TestHttpSetup)
501 {
502 NitsDisableFaultSim;
503
504 Http* http = 0;
505 krisbash 1.1 CallbackStruct cb;
506
507 cb.response = "Response";
508
509 /* create a server */
510 if(!TEST_ASSERT( MI_RESULT_OK == Http_New_Server(
511 &http, 0, PORT, 0, NULL, (Server_SSL_Options) 0,
512 _callback,
513 &cb,
514 NULL) ))
515 return;
516
517 /* create a client */
518 ThreadParam param;
519 Thread t;
520
521 param.messageToSend =
522 "POST /wsman HTTP/1.1\r\n"
523 "Content-Type: application/soap+xml; \tcharset=\"UTF-16\"\t \r\n"
524 "User-Agent: Microsoft WinRM Client\r\n"
525 "Host: localhost:7778\r\n"
526 krisbash 1.1 "Content-Length: 5\r\n"
527 "Authorization: auth\r\n"
528 "\r\n"
529 "aloha";
530 param.bytesToSendPerOperation = 30000;
531 param.gotRsp = false;
532
533 int threadCreatedResult = Thread_CreateJoinable(
534 &t, (ThreadProc)http_client_proc, NULL, ¶m);
535 TEST_ASSERT(MI_RESULT_OK == threadCreatedResult);
536 if(threadCreatedResult != MI_RESULT_OK)
537 goto TestEnd;
538 // pump messages
539 for (int i = 0; !param.gotRsp && i < 10000; i++ )
540 Http_Run( http, SELECT_BASE_TIMEOUT_MSEC * 1000 );
541
542 // wait for completion and check that
543 PAL_Uint32 ret;
544 TEST_ASSERT( Thread_Join( &t, &ret ) == 0 );
545 Thread_Destroy( &t );
546
547 krisbash 1.1 // check messages
548
549 TEST_ASSERT( cb.authorization == "auth" );
550 TEST_ASSERT( cb.contentType == "application/soap+xml" );
551 TEST_ASSERT( cb.charset == "UTF-16" );
552 TEST_ASSERT( cb.contentLength == 5 );
553 TEST_ASSERT( cb.data == "aloha" );
554
555 //TEST_ASSERT( param.response.find("Response") != string::npos );
556 TestEnd:
557 TEST_ASSERT( MI_RESULT_OK == Http_Delete(http) );
558 }
559 NitsEndTest
560
561 NitsTestWithSetup(TestHttp_Base64Decoding, TestHttpSetup)
562 {
563 NitsDisableFaultSim;
564
565 Http* http = 0;
566 CallbackStruct cb;
567
568 krisbash 1.1 cb.response = "Response";
569
570 /* create a server */
571 if(!TEST_ASSERT( MI_RESULT_OK == Http_New_Server(
572 &http, 0, PORT, 0, NULL, (Server_SSL_Options) 0,
573 _callback,
574 &cb,
575 NULL) ))
576 return;
577
578 /* create a client */
579 ThreadParam param;
580 Thread t;
581
582 param.messageToSend =
583 "POST /wsman HTTP/1.1\r\n"
584 "Content-Type: application/soap+xml; \tcharset=\"UTF-16\"\t \r\n"
585 "User-Agent: Microsoft WinRM Client\r\n"
586 "Host: localhost:7778\r\n"
587 "Content-Length: 5\r\n"
588 "Authorization: Basic \t \tcm9vdDpPcHNNZ3IyMDA3UjI=\t \r\n"
589 krisbash 1.1 "\r\n"
590 "aloha";
591 param.bytesToSendPerOperation = 30000;
592 param.gotRsp = false;
593
594 int threadCreatedResult = Thread_CreateJoinable(
595 &t, (ThreadProc)http_client_proc, NULL, ¶m);
596 TEST_ASSERT(MI_RESULT_OK == threadCreatedResult);
597 if(threadCreatedResult != MI_RESULT_OK)
598 goto TestEnd;
599 // pump messages
600 for (int i = 0; !param.gotRsp && i < 10000; i++ )
601 Http_Run( http, SELECT_BASE_TIMEOUT_MSEC * 1000 );
602
603 // wait for completion and check that
604 PAL_Uint32 ret;
605 TEST_ASSERT( Thread_Join( &t, &ret ) == 0 );
606 Thread_Destroy( &t );
607
608
609 // check messages
610 krisbash 1.1
611 TEST_ASSERT( cb.authorization == "Basic" );
612 TEST_ASSERT( cb.username == "root" );
613 TEST_ASSERT( cb.password == "OpsMgr2007R2" );
614 TEST_ASSERT( cb.contentType == "application/soap+xml" );
615 TEST_ASSERT( cb.charset == "UTF-16" );
616 TEST_ASSERT( cb.contentLength == 5 );
617 TEST_ASSERT( cb.data == "aloha" );
618
619 //TEST_ASSERT( param.response.find("Response") != string::npos );
620 TestEnd:
621 TEST_ASSERT( MI_RESULT_OK == Http_Delete(http) );
622 }
623 NitsEndTest
624
625 NitsTestWithSetup(TestHttp_InvalidBase64Data, TestHttpSetup)
626 {
627 NitsDisableFaultSim;
628
629 Http* http = 0;
630 CallbackStruct cb;
631 krisbash 1.1
632 cb.response = "Response";
633
634 /* create a server */
635 if(!TEST_ASSERT( MI_RESULT_OK == Http_New_Server(
636 &http, 0, PORT, 0, NULL, (Server_SSL_Options) 0,
637 _callback,
638 &cb,
639 NULL) ))
640 return;
641
642 /* create a client */
643 ThreadParam param;
644 Thread t;
645
646 /* send request with invalid base64 data; server should close connection */
647 param.messageToSend =
648 "POST /wsman HTTP/1.1\r\n"
649 "Content-Type: application/soap+xml; \tcharset=\"UTF-16\"\t \r\n"
650 "User-Agent: Microsoft WinRM Client\r\n"
651 "Host: localhost:7778\r\n"
652 krisbash 1.1 "Content-Length: 5\r\n"
653 "Authorization: Basic \t \tcm9vd\xFE\xFF==\t \r\n"
654 "\r\n"
655 "aloha";
656 param.bytesToSendPerOperation = 30000;
657 param.gotRsp = false;
658 int threadCreatedResult = Thread_CreateJoinable(
659 &t, (ThreadProc)http_client_proc, NULL, ¶m);
660 TEST_ASSERT(MI_RESULT_OK == threadCreatedResult);
661 if(threadCreatedResult != MI_RESULT_OK)
662 goto TestEnd;
663
664 // pump messages
665 for (int i = 0; !param.gotRsp && i < 10000; i++ )
666 Http_Run( http, SELECT_BASE_TIMEOUT_MSEC * 1000 );
667
668 // wait for completion and check that
669 PAL_Uint32 ret;
670 TEST_ASSERT( Thread_Join( &t, &ret ) == 0 );
671 Thread_Destroy( &t );
672
673 krisbash 1.1
674 // check messages
675 TEST_ASSERT( param.response.empty() );
676 TEST_ASSERT( cb.authorization == "" );
677 TEST_ASSERT( cb.username == "" );
678 TEST_ASSERT( cb.password == "" );
679 TEST_ASSERT( cb.contentType == "" );
680 TEST_ASSERT( cb.charset == "" );
681 TEST_ASSERT( cb.contentLength == 0 );
682 TEST_ASSERT( cb.data == "" );
683 TestEnd:
684 TEST_ASSERT( MI_RESULT_OK == Http_Delete(http) );
685 }
686 NitsEndTest
687
688 BEGIN_EXTERNC
689 static void _ConnectToServerExpectConnectionDrop(const string& data, MI_Uint32 sleepInsideSendMS)
690 {
691 Sock sock;
692 MI_Result r;
693
694 krisbash 1.1 // Initialize address (connect using loopback).
695 sock = SockConnectLocal(PORT);
696
697 size_t sent = 0;
698 size_t size_left = data.size();
699 const char* buf = data.c_str();
700
701 while ( size_left )
702 {
703 size_t wantToSend = size_left;
704
705 /* send all but last bytes; sleep before sending last byte */
706 if (wantToSend > 1)
707 {
708 wantToSend--;
709 }
710 else
711 {
712 /* sleep before sending last byte */
713 ut::sleep_ms(sleepInsideSendMS);
714 }
715 krisbash 1.1
716 r = Sock_Write(sock, buf, wantToSend, &sent);
717
718 //if (r != MI_RESULT_OK)
719 //printf("errno %d\n", errno);
720
721 TEST_ASSERT(r == MI_RESULT_OK ||
722 (r == MI_RESULT_FAILED && 1 == wantToSend) );
723 //printf("s: %d\n", (int)sent);
724
725 if (0 == sent)
726 break;
727
728 size_left -= sent;
729 buf += sent;
730 }
731
732
733 if (sent != 0)
734 {
735 // read response
736 krisbash 1.1 char r_buf[1024];
737 size_t read = 0;
738 r = Sock_Read(sock, r_buf, sizeof(r_buf), &read);
739 //printf("s,r: %d, res = %d\n", (int)read, (int)r);
740
741 TEST_ASSERT(r == MI_RESULT_OK || r == MI_RESULT_FAILED);
742 TEST_ASSERT(read == 0);
743 }
744
745 // this is a negative case; so as long as we did not crash or hang
746 // we are fine to ignore the error
747 NitsIgnoringError();
748
749 Sock_Close(sock);
750 }
751 END_EXTERNC
752
753
754 NitsTestWithSetup(TestHttp_HeaderTooBig, TestHttpSetup)
755 {
756 CallbackStruct cb;
757 krisbash 1.1
758 // send huge http header and expecting connection to be dropped
759 if(!TEST_ASSERT(MI_RESULT_OK == _StartHTTP_Server(
760 _callback2,
761 &cb,
762 NULL,
763 0)))
764 return;
765
766 string data =
767 "POST /wsman HTTP/1.1\r\n"
768 "Content-Type: ct\r\n"
769 "User-Agent: Microsoft WinRM Client\r\n"
770 "Host: localhost:7778\r\n"
771 "Content-Length: 5\r\n"
772 "Authorization: auth\r\n";
773
774 data += string("hugeAttribute: ") + string(4096, 'x') + string("\r\n");
775 data += "\r\n"
776 "Hello";
777
778 krisbash 1.1 _ConnectToServerExpectConnectionDrop(data, 0);
779
780 _StopHTTP_Server();
781 }
782 NitsEndTest
783
784 NitsTestWithSetup(TestHttp_TimeoutMidPacket, TestHttpSetup)
785 {
786 CallbackStruct cb;
787 HttpOptions options = {1000}; /* 1 ms */
788
789 // send http header with delay in the middle of the package and expecting connection to be dropped
790 if (!TEST_ASSERT(MI_RESULT_OK == _StartHTTP_Server(
791 _callback2,
792 &cb,
793 NULL,
794 &options)))
795 return;
796
797 string data =
798 "POST /wsman HTTP/1.1\r\n"
799 krisbash 1.1 "Content-Type: ct\r\n"
800 "User-Agent: Microsoft WinRM Client\r\n"
801 "Host: localhost:7778\r\n"
802 "Content-Length: 5\r\n"
803 "Authorization: auth\r\n"
804 "\r\n"
805 "Hello";
806
807 _ConnectToServerExpectConnectionDrop(data, 1000);
808
809 _StopHTTP_Server();
810 }
811 NitsEndTest
812
813 NitsTestWithSetup(TestHttp_LongContent, TestHttpSetup)
814 {
815 CallbackStruct cb;
816 HttpOptions options = {1000}; /* 1 ms */
817
818 // send http header with delay in the middle of the package and expecting connection to be dropped
819 if (!TEST_ASSERT(MI_RESULT_OK == _StartHTTP_Server(
820 krisbash 1.1 _callback2,
821 &cb,
822 NULL,
823 &options)))
824 return;
825
826 string data =
827 "POST /wsman HTTP/1.1\r\n"
828 "Content-Type: ct\r\n"
829 "User-Agent: Microsoft WinRM Client\r\n"
830 "Host: localhost:7778\r\n"
831 "Content-Length: 5\r\n"
832 "Authorization: auth\r\n"
833 "\r\n"
834 "Hello (this part of the message is not accounted for n content-length and may cause buffer overrun)";
835
836 _ConnectToServerExpectConnectionDrop(data, 100);
837
838 _StopHTTP_Server();
839 }
840 NitsEndTest
841 krisbash 1.1
842 #ifdef CONFIG_POSIX
843
844 NitsTestWithSetup(TestHttp_ValidCipherList, TestHttpSetup)
845 {
846 CallbackStruct cb;
847 HttpOptions options = {1000}; /* 1 ms */
848
849 if (!TEST_ASSERT(MI_RESULT_OK == _StartHTTP_Server(
850 _callback2,
851 &cb,
852 "DES-CBC3-SHA:AES256-SHA256",
853 &options)))
854 NitsReturn;
855
856 _StopHTTP_Server();
857
858 NitsReturn;
859 }
860 NitsEndTest
861
862 krisbash 1.1 #endif
|