void testPathInfo() { UtlString testDir = "../../filename.ext"; OsPath testPath = testDir; UtlString parentPath; parentPath.append("..").append(OsPath::separator).append("..").append(OsPath::separator); ASSERT_STR_EQUAL_MESSAGE("Extension", ".ext", testPath.getExt().data()); ASSERT_STR_EQUAL_MESSAGE("Parent Path", parentPath, testPath.getDirName().data()); ASSERT_STR_EQUAL_MESSAGE("Volume", "", testPath.getVolume().data()); ASSERT_STR_EQUAL_MESSAGE("Filename no extension", "filename", testPath.getFilename().data()); // little risky, may not have permissions, but '../../' is too // good a test to not try. Refactor if this is bad assumption OsFile file(testPath); file.touch(); OsPath nativePath; testPath.getNativePath(nativePath); // dont' know what's right, but know whats wrong CPPUNIT_ASSERT_MESSAGE("Resolved relative path", !nativePath.contains("..")); //printf("Native path is %s\n", nativePath.data()); file.remove(); }
void testAccessors() { const char* name = "xxx"; const char* name_different_case = "XXX"; const char* value = "xxx-value"; const char* valueRef; NameValuePair* nv = new NameValuePair(name); NameValuePair* nv_different_case = new NameValuePair(name_different_case); valueRef = nv->getValue(); CPPUNIT_ASSERT_MESSAGE("value should be null", NULL == valueRef); nv->setValue(value); valueRef = nv->getValue(); ASSERT_STR_EQUAL_MESSAGE("incorrect value retrieved", value, valueRef); nv->setValue(name); valueRef = nv->getValue(); ASSERT_STR_EQUAL_MESSAGE("incorrect value retrieved", valueRef, name); CPPUNIT_ASSERT_MESSAGE("nv should be == nv", nv->compareTo(nv) == 0); CPPUNIT_ASSERT_MESSAGE("nv should be != nv_different_case", nv->compareTo(nv_different_case) != 0); delete nv; delete nv_different_case; }
void testLoadAlarms() { OsSysLog::add(FAC_ALARM, PRI_DEBUG, "AlarmServerTest::testLoadAlarms"); cAlarmData* alarmData; for (size_t i=0; i<sizeof(expectedResult)/sizeof(alarmRowData); i++) { char msg[1000]; UtlString tempStr(expectedResult[i].id); alarmData = cAlarmServer::getInstance()->lookupAlarm(tempStr); sprintf(msg, "in definition of alarm %s", expectedResult[i].id); CPPUNIT_ASSERT_MESSAGE(msg, alarmData!=0); ASSERT_STR_EQUAL_MESSAGE(msg, expectedResult[i].code, alarmData->getCode().data()); CPPUNIT_ASSERT_EQUAL_MESSAGE(msg, (int)expectedResult[i].severity, (int)alarmData->getSeverity()); ASSERT_STR_EQUAL_MESSAGE(msg, expectedResult[i].shorttitle, alarmData->getShortTitle().data()); ASSERT_STR_EQUAL_MESSAGE(msg, expectedResult[i].description, alarmData->getDescription().data()); ASSERT_STR_EQUAL_MESSAGE(msg, expectedResult[i].resolution, alarmData->getResolution().data()); for (int j=0; j<cAlarmData::eActionMax; j++) { sprintf(msg, "alarm %s, action %d", expectedResult[i].id, j); CPPUNIT_ASSERT_EQUAL_MESSAGE(msg, (int)expectedResult[i].actions[(int)j], (int)alarmData->getAction((cAlarmData::eAlarmActions)j)); } //CPPUNIT_ASSERT_EQUAL(expectedResult[i].min_threshold, alarmData.getMinThreshold()); } }
/** * Start a client and server and send 2 messages over TCP thru them * * NOTE: This can/will fail if /etc/hosts defines localhost as ::1 (as * opposed to 127.0.0.1). */ void testWriteAndAcceptMsg() { // Create/Verify Sockets OsServerSocket* server = new OsServerSocket(50, 8021); KNOWN_BUG("This can fail if there is a port conflict on the test system", "XECS-1924"); CPPUNIT_ASSERT_MESSAGE("server socket failure", server->isOk()); OsSocket* client = new OsConnectionSocket(8021, "localhost"); CPPUNIT_ASSERT_MESSAGE("client socket failure", client->isOk()); OsSocket* serverClient = server->accept(1000); CPPUNIT_ASSERT_MESSAGE("socket server failed to accept connection", serverClient != NULL); // Begin read/write test const char* msg = "hello\n"; int len = strlen(msg) + 1; // +1 for NULL int bytesWritten = client->write(msg, len); CPPUNIT_ASSERT_EQUAL_MESSAGE("write correct number of bytes", bytesWritten, len); char recvBuf[1024]; int bytesRead = serverClient->read(recvBuf, sizeof(recvBuf) - 1); CPPUNIT_ASSERT_EQUAL_MESSAGE("read correct number of bytes", len, bytesRead); ASSERT_STR_EQUAL_MESSAGE("message same as was sent", msg, recvBuf); const char *resp = "bye"; len = strlen(resp) + 1; // +1 for NULL bytesWritten = serverClient->write(resp, len); CPPUNIT_ASSERT_EQUAL_MESSAGE("write correct number of bytes on 2nd msg", len, bytesWritten); bytesRead = client->read(recvBuf, sizeof(recvBuf) - 1); CPPUNIT_ASSERT_EQUAL_MESSAGE("read correct number of bytes on 2nd msg", len, bytesRead); CPPUNIT_ASSERT_EQUAL_MESSAGE("write correct number of bytes on 2nd msg", len, bytesWritten); ASSERT_STR_EQUAL_MESSAGE("2nd message same as was sent", resp, recvBuf); serverClient->close(); client->close(); server->close(); delete client; delete server; }
void testQuotemeta() { struct test { const char* literal; const char* expected_regexp; } tests[] = { { "", "" }, { "1", "1" }, { "a", "a" }, { "!", "\\!" }, { " ", "\\ " }, { "a1", "a1" }, { "a! ", "a\\!\\ " }, { "this is", "this\\ is" }, { "this_is", "this\\_is" }, }; for (unsigned int i = 0; i < sizeof (tests) / sizeof (tests[0]); i++) { char message[100]; sprintf(message, "Test %d: '%s'", i, tests[i].literal); UtlString literal(tests[i].literal); UtlString regexp; RegEx::Quotemeta(literal, regexp); ASSERT_STR_EQUAL_MESSAGE(message, tests[i].expected_regexp, regexp.data()); } }
void testCreator() { const char *hdr = "\r\nContent-Length: 0\r\n\r\n"; ssize_t expectedLen = strlen(hdr); HttpMessage* msg = new HttpMessage(); UtlString buf; ssize_t bufLen = 0; msg->getBytes(&buf, &bufLen); ASSERT_STR_EQUAL_MESSAGE("Set header comes back intact", hdr, (char *)buf.data()); CPPUNIT_ASSERT_MESSAGE("bytes should not be null", !buf.isNull()); CPPUNIT_ASSERT_EQUAL_MESSAGE("buffer should contain only content length field", expectedLen, bufLen); ASSERT_STR_EQUAL_MESSAGE("buffer should contain only content length field", buf.data(), hdr); delete msg; }
void testGetValue() { MockCgiInput input((char*)"CgiValuesTestInput.txt"); cgicc::Cgicc cgi(&input); CgiValues values(&cgi); ASSERT_STR_EQUAL_MESSAGE("Expected form value", "B", values.valueOf("A")); CPPUNIT_ASSERT_MESSAGE("Expected missing value", NULL == values.valueOf("NotInForm")); }
void testCreateMultipart() { #define BODY1 \ "Now is the time for all good men to come to the aid of their party." #define HEADER \ "Content-Type: multipart/related;" \ "boundary=\"[boundary]\"\r\n" \ "\r\n" #define PART1 \ "--[boundary]\r\n" \ "Content-ID: <*****@*****.**>\r\n" \ "CONTENT-TYPE: text/plain\r\n" \ "CONTENT-TRANSFER-ENCODING: binary\r\n" \ "\r\n" \ BODY1 \ "\r\n" #define FORMAT2 \ "--[boundary]\r\n" \ "CONTENT-TYPE: application/octet-stream\r\n" \ "CONTENT-TRANSFER-ENCODING: binary\r\n" \ "\r\n" \ "%s" \ "\r\n" #define TRAILER \ "--[boundary]--\r\n" HttpBodyMultipart body("multipart/related"); ASSERT_STR_EQUAL_MESSAGE("Zero body parts", HEADER TRAILER, extract_contents(&body).data()); HttpBody bodyPart1(BODY1, sizeof (BODY1) - 1, "text/plain"); UtlDList parameters1; NameValuePair nvp("Content-ID", "<*****@*****.**>"); parameters1.append(&nvp); body.appendBodyPart(bodyPart1, parameters1); ASSERT_STR_EQUAL_MESSAGE("One body part", HEADER PART1 TRAILER, extract_contents(&body).data()); // Find the current boundary string and use it as a body, // forcing the HttpBody to change the boundary string. char boundary_string[10]; strcpy(boundary_string, body.getMultipartBoundary()); HttpBody bodyPart2(boundary_string, strlen(boundary_string), "application/octet-stream"); UtlDList parameters2; body.appendBodyPart(bodyPart2, parameters2); char part2[1024]; sprintf(part2, HEADER PART1 FORMAT2 TRAILER, boundary_string); ASSERT_STR_EQUAL_MESSAGE("Two body parts", part2, extract_contents(&body).data()); const char* new_boundary_string = body.getMultipartBoundary(); CPPUNIT_ASSERT_MESSAGE("Changed boundary string", strcmp(boundary_string, new_boundary_string)); }
/** * Test header, message, body, message contructor */ void testMessage() { // TODO break this up into several tests. Too intertwined const char* name = "Content-Type"; const char* value = "text/plain"; const char* httpTopLine = "GET /index.html HTTP/1.0"; const char* valueRef = NULL; const char* n2 = "yyy"; const char* v2 = "yyy-value"; const char* v2a = "yyy-value2"; UtlString messageBytes; UtlString messageBytes2; ssize_t messageLen = 0; ssize_t messageLen2 = 0; const char* body = "<HTML>\n<H3>Hello\n<BR>\n</HTML>\n"; const HttpBody *bodyRef; ssize_t bodyLength = 0; UtlString headerLinePart; HttpMessage *msg; HttpMessage *msg2; msg = new HttpMessage(); // H E A D E R int fieldCount = msg->getCountHeaderFields(); CPPUNIT_ASSERT_EQUAL_MESSAGE("field count should be zero", 0, fieldCount); msg->addHeaderField(name, value); fieldCount = msg->getCountHeaderFields(); CPPUNIT_ASSERT_EQUAL_MESSAGE("field count should be zero", 1, fieldCount); valueRef = msg->getHeaderValue(0); CPPUNIT_ASSERT_MESSAGE("NULL field value", valueRef != NULL); ASSERT_STR_EQUAL_MESSAGE("incorrect field value", value, valueRef); msg->setFirstHeaderLine(httpTopLine); valueRef = msg->getFirstHeaderLine(); ASSERT_STR_EQUAL_MESSAGE("incorrect top header line value", valueRef, httpTopLine); valueRef = msg->getHeaderValue(0, name); CPPUNIT_ASSERT_MESSAGE("NULL field value", valueRef != NULL); ASSERT_STR_EQUAL_MESSAGE("incorrect field value", value, valueRef); msg->addHeaderField(n2, v2); fieldCount = msg->getCountHeaderFields(); CPPUNIT_ASSERT_EQUAL_MESSAGE("field count should be 2", 2, fieldCount); valueRef = msg->getHeaderValue(0, n2); CPPUNIT_ASSERT_MESSAGE("NULL field value", valueRef != NULL); ASSERT_STR_EQUAL_MESSAGE("incorrect field value", v2, valueRef); msg->addHeaderField(n2, v2a); fieldCount = msg->getCountHeaderFields(); CPPUNIT_ASSERT_EQUAL_MESSAGE("field count should be 3", 3, fieldCount); valueRef = msg->getHeaderValue(1, n2); CPPUNIT_ASSERT_MESSAGE("NULL field value", valueRef != NULL); ASSERT_STR_EQUAL_MESSAGE("incorrect field value", v2a, valueRef); // B O D Y HttpBody *httpBody = new HttpBody(body, strlen(body)); msg->setBody(httpBody); bodyRef = msg->getBody(); CPPUNIT_ASSERT_MESSAGE("bad body pointer", httpBody == bodyRef); bodyRef->getBytes(&valueRef, &bodyLength); CPPUNIT_ASSERT_MESSAGE("bad body pointer", valueRef != NULL); CPPUNIT_ASSERT_EQUAL_MESSAGE("incorrect body len", (ssize_t)strlen(body), bodyLength); ASSERT_STR_EQUAL_MESSAGE("incorrect body value", body, valueRef); const char* expectedLinePart[] = { "GET", "/index.html", "HTTP/1.0" }; size_t n = sizeof(expectedLinePart) / sizeof(expectedLinePart[0]); for (size_t i = 0; i < n; i++) { msg->getFirstHeaderLinePart(i, &headerLinePart); CPPUNIT_ASSERT_MESSAGE("NULL header line part pointer", !headerLinePart.isNull()); ASSERT_STR_EQUAL_MESSAGE("incorrect hdr line", expectedLinePart[i], headerLinePart.data()); headerLinePart.remove(0); } msg->getBytes(&messageBytes, &messageLen); CPPUNIT_ASSERT_MESSAGE("NULL body pointer", !messageBytes.isNull()); // message constructor msg2 = new HttpMessage(messageBytes.data(), messageLen); msg2->getBytes(&messageBytes2, &messageLen2); valueRef = msg2->getHeaderValue(0, name); ASSERT_STR_EQUAL_MESSAGE("incorrect message bytes", value, valueRef); CPPUNIT_ASSERT_EQUAL_MESSAGE("incorrect message byte length", messageLen, messageLen2); delete msg2; delete msg; // AS DESIGNED: body delete is handled by delete msg // delete httpBody; }
/** * Test header */ void testHeader() { const char* name1 = "yyy"; const char* value1 = "yyy-value"; const char* valueRef = NULL; const char* name2 = "yyy1"; const char* value2 = "yyy-value1"; const char* value2a = "yyy-value2"; const char* value2b = "yyy-value3"; UtlBoolean rc; HttpMessage *msg; msg = new HttpMessage(); // H E A D E R int fieldCount = msg->getCountHeaderFields(); CPPUNIT_ASSERT_EQUAL_MESSAGE("field count should be zero", 0, fieldCount); // add header field name1 msg->addHeaderField(name1, value1); // get overall header field count fieldCount = msg->getCountHeaderFields(); CPPUNIT_ASSERT_EQUAL_MESSAGE("field count should be zero", 1, fieldCount); // get header field count for name1 fieldCount = msg->getCountHeaderFields(name1); CPPUNIT_ASSERT_EQUAL_MESSAGE("field count should be zero", 1, fieldCount); // get header field by index valueRef = msg->getHeaderValue(0); CPPUNIT_ASSERT_MESSAGE("NULL field value", valueRef != NULL); ASSERT_STR_EQUAL_MESSAGE("incorrect field value", value1, valueRef); // get header field by index and name valueRef = msg->getHeaderValue(0, name1); CPPUNIT_ASSERT_MESSAGE("NULL field value", valueRef != NULL); ASSERT_STR_EQUAL_MESSAGE("incorrect field value", value1, valueRef); // add header field name2 msg->addHeaderField(name2, value2); // get header field by name and index valueRef = msg->getHeaderValue(0, name2); CPPUNIT_ASSERT_MESSAGE("NULL field value", valueRef != NULL); ASSERT_STR_EQUAL_MESSAGE("incorrect field value", value2, valueRef); // add second header field name2 msg->addHeaderField(name2, value2); // get header field by name and index valueRef = msg->getHeaderValue(0, name2); CPPUNIT_ASSERT_MESSAGE("NULL field value", valueRef != NULL); ASSERT_STR_EQUAL_MESSAGE("incorrect field value", value2, valueRef); // set second header field name2 msg->setHeaderValue(name2, value2b, 1); // get header field by name and index valueRef = msg->getHeaderValue(0, name2); CPPUNIT_ASSERT_MESSAGE("NULL field value", valueRef != NULL); ASSERT_STR_EQUAL_MESSAGE("incorrect field value", value2, valueRef); // insert header field name2 as a second header with name2 msg->insertHeaderField(name2, value2a, 2); // get header field by name and index valueRef = msg->getHeaderValue(0, name2); CPPUNIT_ASSERT_MESSAGE("NULL field value", valueRef != NULL); ASSERT_STR_EQUAL_MESSAGE("incorrect field value", value2, valueRef); // get overall header field count fieldCount = msg->getCountHeaderFields(); CPPUNIT_ASSERT_EQUAL_MESSAGE("field count should be 4", 4, fieldCount); // get name1 header field count fieldCount = msg->getCountHeaderFields(name1); CPPUNIT_ASSERT_EQUAL_MESSAGE("field count should be 1", 1, fieldCount); // get name2 header field count fieldCount = msg->getCountHeaderFields(name2); CPPUNIT_ASSERT_EQUAL_MESSAGE("field count should be 3", 3, fieldCount); // get header field by name and index valueRef = msg->getHeaderValue(0, name2); CPPUNIT_ASSERT_MESSAGE("NULL field value", valueRef != NULL); ASSERT_STR_EQUAL_MESSAGE("incorrect field value", value2, valueRef); // get header field by name and index valueRef = msg->getHeaderValue(1, name2); CPPUNIT_ASSERT_MESSAGE("NULL field value", valueRef != NULL); ASSERT_STR_EQUAL_MESSAGE("incorrect field value", value2a, valueRef); // get header field by index valueRef = msg->getHeaderValue(3); CPPUNIT_ASSERT_MESSAGE("NULL field value", valueRef != NULL); ASSERT_STR_EQUAL_MESSAGE("incorrect field value", value2b, valueRef); // remove non-existing header field rc = msg->removeHeader("non-exist", 36); CPPUNIT_ASSERT_MESSAGE("incorrect return code", rc == FALSE); // get overall header field count fieldCount = msg->getCountHeaderFields(); CPPUNIT_ASSERT_EQUAL_MESSAGE("field count should be 4", 4, fieldCount); // remove header field name1 rc = msg->removeHeader(name1, 0); CPPUNIT_ASSERT_MESSAGE("incorrect return code", rc == TRUE); // get header field by index valueRef = msg->getHeaderValue(0); CPPUNIT_ASSERT_MESSAGE("NULL field value", valueRef != NULL); ASSERT_STR_EQUAL_MESSAGE("incorrect field value", value2, valueRef); // get name1 header field count fieldCount = msg->getCountHeaderFields(name1); CPPUNIT_ASSERT_EQUAL_MESSAGE("field count should be 0", 0, fieldCount); // remove second header field name2 rc = msg->removeHeader(name2, 1); CPPUNIT_ASSERT_MESSAGE("incorrect return code", rc == TRUE); // get overall header field count fieldCount = msg->getCountHeaderFields(); CPPUNIT_ASSERT_EQUAL_MESSAGE("field count should be 2", 2, fieldCount); // get name2 header field count fieldCount = msg->getCountHeaderFields(name2); CPPUNIT_ASSERT_EQUAL_MESSAGE("field count should be 2", 2, fieldCount); // get header field by index valueRef = msg->getHeaderValue(0); CPPUNIT_ASSERT_MESSAGE("NULL field value", valueRef != NULL); ASSERT_STR_EQUAL_MESSAGE("incorrect field value", value2, valueRef); // get header field by name and index valueRef = msg->getHeaderValue(1, name2); CPPUNIT_ASSERT_MESSAGE("NULL field value", valueRef != NULL); ASSERT_STR_EQUAL_MESSAGE("incorrect field value", value2b, valueRef); // get non-exist header field by name and index valueRef = msg->getHeaderValue(2, name2); CPPUNIT_ASSERT_MESSAGE("non-NULL field value", valueRef == NULL); // remove header field name2 rc = msg->removeHeader(name2, 1); CPPUNIT_ASSERT_MESSAGE("incorrect return code", rc == TRUE); // get overall header field count fieldCount = msg->getCountHeaderFields(); CPPUNIT_ASSERT_EQUAL_MESSAGE("field count should be 1", 1, fieldCount); // get header field by index valueRef = msg->getHeaderValue(0); CPPUNIT_ASSERT_MESSAGE("NULL field value", valueRef != NULL); ASSERT_STR_EQUAL_MESSAGE("incorrect field value", value2, valueRef); // remove header field name2 rc = msg->removeHeader(name2, 0); CPPUNIT_ASSERT_MESSAGE("incorrect return code", rc == TRUE); // get overall header field count fieldCount = msg->getCountHeaderFields(); CPPUNIT_ASSERT_EQUAL_MESSAGE("field count should be 0", 0, fieldCount); delete msg; }