void RequestSuite::TestMultilineHeaders() { SiteOptions so; so.SetPort(50); Site site(&so); const int NR = 2; const char *getstring[NR] = { "GET /good.html HTTP/1.1\r\nHost: keep-alive\r\nMyHeader: apa\r\n Gnu\r\n\r\n", "GET /good.html HTTP/1.1\r\nHost: keep-alive\r\nMyHeader: apa\r\n Gnu\r\n Gnu2\r\n\r\n", }; ByteBuffer* buf = new ByteBuffer(2000); for (int i = 0; i < NR; i++) { Request *r = new Request(NULL, &site); buf->Clear(); buf->Add(getstring[i], strlen(getstring[i])); bool result = Request::ParseRequest(r, buf); TEST_ASSERT(result==true); TEST_ASSERT(r->GetStatus() == Http::HTTP_OK); delete r; } delete buf; }
void RequestSuite::TestEvilRequests() { SiteOptions so; so.SetPort(50); Site site(&so); const int NR = 3; const char *getstring[NR] = { ", \r\n", "\r\n\r\n\r\n", "\0" }; ByteBuffer* buf = new ByteBuffer(2000); for (int i = 0; i < NR; i++) { Request *r = new Request(NULL, &site); buf->Clear(); buf->Add(getstring[i], strlen(getstring[i])); bool result = Request::ParseRequest(r, buf); TEST_ASSERT(result==false); TEST_ASSERT(r->GetStatus() != Http::HTTP_OK); delete r; } delete buf; }
result Enrollment::ReceiveDataUDP(void) { result res = E_SUCCESS; ByteBuffer txBuffer; unsigned long buflen = 0; __pSocket->Ioctl(NET_SOCKET_FIONREAD, buflen); txBuffer.Construct(buflen + 1); Ip4Address peerAddr1(NET_SOCKET_INADDR_ANY); unsigned short port = 0; NetEndPoint point(peerAddr1, port); res = __pSocket->ReceiveFrom(txBuffer, point); if (__pUdpEndpoint == null) { __pUdpEndpoint = new (std::nothrow) NetEndPoint(point); } else { if (__pUdpEndpoint->GetNetEndPoint() != point.GetNetEndPoint()) { AppLog("It's a data from other endpoint so discard"); return res; } } TryReturn(res == E_SUCCESS, res, "Receive Failed %d", res); res = DisplayText(txBuffer, buflen); txBuffer.Clear(); return res; }
result Enrollment::DisplayText(ByteBuffer& txBuffer, unsigned long buflen) { String data; char* pBuffer = null; result res = E_SUCCESS; txBuffer.Flip(); pBuffer = (char*) (txBuffer.GetPointer()); data.SetCapacity(buflen + 20); res = data.Append(pBuffer); TryReturn(res == E_SUCCESS, res, "Append Failed"); txBuffer.Clear(); if (data == L"__HELLO__") { EnableControl(true); __isConnected = true; return res; } if (__isConnected == false) { AppLog( "Server hasn't received Initial message so all incoming content is discarded"); // Remove created NetEndPoint delete __pUdpEndpoint; __pUdpEndpoint = null; return res; } if (data == L"__CLOSE__") { SceneManager* pSceneManager = SceneManager::GetInstance(); AppAssert(pSceneManager); pSceneManager->GoBackward(BackwardSceneTransition(SCENE_CALENDAR)); OnClose(); return res; } int i = 0; if (data.Contains(__pWorkList[i]->GetText())) { __pWorkList[i++]->SetSelected(true); } if (data.Contains(__pWorkList[i]->GetText())) { __pWorkList[i++]->SetSelected(true); } if (data.Contains(__pWorkList[i]->GetText())) { __pWorkList[i++]->SetSelected(true); } if (data.Contains(__pWorkList[i]->GetText())) { __pWorkList[i++]->SetSelected(true); } TryReturn(res == E_SUCCESS, res, "AppendText Failed"); for (int k = 0; k < MAX_WORK_COUNT; k++) { __pWorkList[k]->RequestRedraw(); } return res; }
result Enrollment::SendData() { String data = ""; ByteBuffer* pTxBuffer = null; ByteBuffer buffer; result res = E_SUCCESS; for (int i = 0; i < 4; i++) { data.Append(__pWorkList[i]->GetText()); data.Append("\n"); } if (data.IsEmpty()) { AppLog("There is no data to send"); return E_SUCCESS; } pTxBuffer = Tizen::Base::Utility::StringUtil::StringToUtf8N(data); TryReturn(pTxBuffer != null, E_OUT_OF_MEMORY, "Failed to allocate ByteBuffer"); AppLog("Text Length %d", pTxBuffer->GetLimit()); buffer.Construct(pTxBuffer->GetLimit()); buffer.CopyFrom(*pTxBuffer); buffer.Flip(); delete pTxBuffer; res = __pSocket->SendTo(buffer, *__pUdpEndpoint); if (res != E_SUCCESS && res != E_WOULD_BLOCK) AppLog("Error sending data"); buffer.Clear(); return res; }
void RequestSuite::TestReqeustsByteForByte() { SiteOptions so; so.SetPort(50); Site site(&so); const int NR = 11; const char *getstring[NR] = { "GET / HTTP/1.0\r\n\r\n", "GET /good.html HTTP/1.1\r\nConnection: keep-alive\r\nHost: \t localhost:80\r\n\r\n", "GET /good.html HTTP/1.1\r\nConnection: close\r\nHost: localhost:81\r\n\r\n", "GET /good.html HTTP/1.0\r\nConnection: keep-alive\r\n\r\n", "GET /good.html HTTP/1.0\r\nConnection: close\r\n\r\n", "GET /good.html HTTP/1.0\r\n\r\n", "GET / HTTP/1.1\r\nHost: localhost:8080\r\n\r\n", "GET /last.html HTTP/1.0\r\nHost: localhost:8080\r\n\r\n", "GET /last.html HTTP/1.0\r\n\r\n", "GET /last.html HTTP/1.1\r\nHost: localhost:8080\r\nConnection: \t close\r\n\r\n", "GET /last.html HTTP/1.0\r\nConnection: \t close\r\n\r\n" }; ByteBuffer* buf = new ByteBuffer(2000); for (int i = 0; i < NR; i++) { Request *r = new Request(NULL, &site); buf->Clear(); size_t len=strlen(getstring[i]); for (size_t j=0;j<len;j++) { buf->Add(getstring[i]+j, 1); bool result = Request::ParseRequest(r, buf); if (j+1<len) { TEST_ASSERT_MSG(result==false,getstring[i]); } else { TEST_ASSERT_MSG(result,getstring[i]); TEST_ASSERT_MSG(r->GetStatus() == Http::HTTP_OK,getstring[i]); } } delete r; } delete buf; }
result Enrollment::ReceiveDataTCP(void) { result res = E_SUCCESS; ByteBuffer txBuffer; String data; unsigned long buflen = 0; // Get the size of data received __pSocket->Ioctl(NET_SOCKET_FIONREAD, buflen); txBuffer.Construct(buflen + 1); res = __pSocket->Receive(txBuffer); TryReturn(res == E_SUCCESS, res, "Receive Failed"); res = DisplayText(txBuffer, buflen); txBuffer.Clear(); return res; }
void RequestSuite::TestRequestConnectionKeepAlive() { SiteOptions so; so.SetPort(50); Site site(&so); const int NR = 6; const char *gs[] = { "GET /good.html HTTP/1.1\r\nConnection: keep-alive\r\nHost: localhost:8080\r\n\r\n", "GET /good.html HTTP/1.1\r\nConnection: close\r\nHost: localhost:8080\r\n\r\n", "GET /good.html HTTP/1.1\r\nHost: localhost:8080\r\n\r\n", "GET /good.html HTTP/1.0\r\nConnection: \t keep-alive\r\n\r\n", "GET /good.html HTTP/1.0\r\nConnection: close\r\n\r\n", "GET /good.html HTTP/1.0\r\n\r\n" }; typedef struct { const char * getstring; bool keepalive; } test_data_t; test_data_t data[NR] = { { gs[0], true }, { gs[1], false }, { gs[2], true }, { gs[3], true }, { gs[4], false }, { gs[5], false } }; ByteBuffer* buf = new ByteBuffer(2000); for (int i = 0; i < NR; i++) { Request *r = new Request(NULL, &site); buf->Clear(); buf->Add(data[i].getstring, strlen(data[i].getstring)); bool result = Request::ParseRequest(r, buf); TEST_ASSERT(r->GetStatus() == Http::HTTP_OK); TEST_ASSERT(r->GetKeepAlive() == data[i].keepalive); delete r; } delete buf; }
void RequestSuite::TestMandatoryHeaderMissing() { SiteOptions so; so.SetPort(50); Site site(&so); const int NR = 1; const char *getstring[NR] = { "GET /good.html HTTP/1.1\r\nConnection: keep-alive\r\n\r\n", }; ByteBuffer* buf = new ByteBuffer(2000); for (int i = 0; i < NR; i++) { Request *r = new Request(NULL, &site); buf->Clear(); buf->Add(getstring[i], strlen(getstring[i])); bool result = Request::ParseRequest(r, buf); TEST_ASSERT(result==true); TEST_ASSERT(r->GetStatus() == Http::HTTP_BAD_REQUEST); delete r; } delete buf; }
void RequestSuite::TestManyGoodGets() { SiteOptions so; so.SetPort(50); Site site(&so); const int NR = 13; const char *getstring[NR] = { "GET /good.html HTTP/1.1\r\nConnection: keep-alive\r\nHost: \t localhost:80\r\n\r\n", "GET /good.html HTTP/1.1\r\nConnection: close\r\nHost: localhost:81\r\n\r\n", "GET /good.html HTTP/1.0\r\nConnection: keep-alive\r\n\r\n", "GET /good.html HTTP/1.0\r\nConnection: close\r\n\r\n", "GET /good.html HTTP/1.0\r\n\r\n", "GET /good.html HTTP/1.0\r\n\r\n", "GET / HTTP/1.1\r\nHost: localhost:8080\r\n\r\n", "GET / HTTP/1.0\r\n\r\n", "GET /last.html HTTP/1.0\r\nHost: localhost:8080\r\n\r\n", "GET /last.html HTTP/1.0\r\n\r\n", "GET /last.html HTTP/1.0\r\n\r\n", "GET /last.html HTTP/1.1\r\nHost: localhost:8080\r\nConnection: \t close\r\n\r\n", "GET /last.html HTTP/1.0\r\nConnection: \t close\r\n\r\n" }; ByteBuffer* buf = new ByteBuffer(2000); for (int i = 0; i < NR; i++) { Request *r = new Request(NULL, &site); buf->Clear(); buf->Add(getstring[i], strlen(getstring[i])); bool result = Request::ParseRequest(r, buf); TEST_ASSERT(result); TEST_ASSERT(r->GetStatus() == Http::HTTP_OK); delete r; } delete buf; }
result Enrollment::SendDataCommand(String command) { ByteBuffer* pTxBuffer = null; ByteBuffer buffer; result res = E_SUCCESS; TryReturn(__isTcp == false, E_SYSTEM, "Sending Command not required for TCP connection"); pTxBuffer = Tizen::Base::Utility::StringUtil::StringToUtf8N(command); TryReturn(pTxBuffer != null, E_OUT_OF_MEMORY, "Failed to allocate ByteBuffer"); buffer.Construct(1024); buffer.CopyFrom(*pTxBuffer); delete pTxBuffer; buffer.Flip(); do { if (__pUdpEndpoint != null) { res = __pSocket->SendTo(buffer, *__pUdpEndpoint); } if (res == E_WOULD_BLOCK) { AppLog("Would block for data sending will try to retransmit"); Tizen::Base::Runtime::Thread::Sleep(500); } else if (res != E_SUCCESS) { AppLog("Error sending data"); break; } else { AppLog("Command successfully sent"); break; } } while (1); AppLog("Loop completed"); buffer.Clear(); return res; }