/* Try SOCK_htonl(), SOCK_gethostbyname() and SOCK_gethostbyaddr() */ static void TEST_gethostby(void) { CORE_LOG(eLOG_Note, "==============================="); assert( SOCK_HostToNetLong(0) == 0 ); assert( SOCK_HostToNetLong(0xFFFFFFFF) == 0xFFFFFFFF ); assert( !TEST_gethostbyname(" ") ); assert( !TEST_gethostbyname("a1....b1") ); assert( !TEST_gethostbyname("boo.foo.bar.doo") ); (void) TEST_gethostbyname("localhost"); (void) TEST_gethostbyname("ncbi.nlm.nih.gov"); (void) TEST_gethostbyname("127.0.0.1"); (void) TEST_gethostbyname("130.14.25.1"); (void) TEST_gethostbyaddr(0); (void) TEST_gethostbyaddr(SOCK_gethostbyname("127.0.0.1")); (void) TEST_gethostbyaddr(SOCK_gethostbyname("130.14.25.1")); (void) TEST_gethostbyaddr(SOCK_gethostbyname("234.234.234.234")); (void) TEST_gethostbyaddr(0xFFFFFFFF); CORE_LOG(eLOG_Note, "==============================="); }
int createEntityWithSize(const char* entityResource, LuaPlus::LuaObject luaPosition, LuaPlus::LuaObject luaSize) { if(!luaPosition.IsTable()) { CORE_LOG("LUA", "Invalid position object passed to create_entity."); return INVALID_ENTITY_ID; } if(!luaSize.IsTable()) { CORE_LOG("LUA", "Invalid size object passed to create_entity."); return INVALID_ENTITY_ID; } sf::Vector2f position = tableToVec2<sf::Vector2f>(luaPosition); sf::Vector2f size = tableToVec2<sf::Vector2f>(luaSize); artemis::Entity& entity = WorldLocator::getObject()->createEntity(); // Create entity if(!EntityFactory::get().loadFromFile(entityResource, entity)) { CORE_ERROR("Failed to load entity from file: " + std::string(entityResource)); return INVALID_ENTITY_ID; } Transform* pTransform = safeGetComponent<Transform>(&entity); if(!pTransform) { entity.addComponent(new Transform(position.x, position.y)); } else { pTransform->position = position; } // Get physics comp DynamicBody* pDynamicBody = safeGetComponent<DynamicBody>(&entity); if(pDynamicBody) { // Set dimensions, will be initialized later pDynamicBody->setDimensions(size.x, size.y); } else { entity.addComponent(new DynamicBody(size.x, size.y)); } // commit entity changes entity.refresh(); CORE_LOG("LUA", "Created entity from: " + std::string(entityResource)); return entity.getId(); }
/* Try SOCK_isip() */ static void TEST_SOCK_isip(void) { CORE_LOG(eLOG_Note, "==============================="); assert(TEST_isip("0") && TEST_isip("0.0.0.0")); assert(TEST_gethostbyname("0") == 0); assert(TEST_isip("1")); assert(TEST_gethostbyname("1") == SOCK_HostToNetLong(1)); assert(TEST_isip("0x7F000002")); assert(TEST_gethostbyname("0x7F000002") == TEST_gethostbyname("127.0.0.2")); assert(TEST_isip("127.234")); assert(TEST_gethostbyname("127.234") == TEST_gethostbyname("127.0.0.234")); assert(TEST_isip("127.16777215")); assert(TEST_gethostbyname("127.16777215") == TEST_gethostbyname("127.255.255.255")); assert(TEST_isip("127.234.0x345")); assert(TEST_gethostbyname("127.234.3.69") == TEST_gethostbyname("127.234.3.69")); assert(TEST_isip("127.234.0xFFFF")); assert(TEST_gethostbyname("127.234.0xFFFF") == TEST_gethostbyname("127.234.255.255")); assert(TEST_isip("127.012344321")); assert(TEST_gethostbyname("127.012344321") == TEST_gethostbyname("127.41.200.209")); assert(TEST_isip("127.077777777")); assert(TEST_gethostbyname("127.077777777") == TEST_gethostbyname("127.255.255.255")); assert(TEST_isip("0.0321.0xAB.123")); assert(TEST_isip("255.255.255.255")); assert(!TEST_isip("a")); assert(!TEST_isip("-1")); assert(!TEST_isip("1.2.3a")); assert(!TEST_isip("1.0xDEATH")); assert(!TEST_isip("1.2.3.256")); assert(!TEST_isip("1.2.0200000")); assert(!TEST_isip("1.1.1.1.")); assert(!TEST_isip("1.1.-1.1")); assert(!TEST_isip("1.0x100.1.1")); assert(!TEST_isip("1.0100000000")); assert(!TEST_isip("0x100000000")); CORE_LOG(eLOG_Note, "==============================="); }
static void TEST_MIME(void) { /* MIME API */ EMIME_Type type; EMIME_SubType subtype; EMIME_Encoding encoding; char str[MAX_CONTENT_TYPE_LEN]; int i,j,k; CORE_LOG(eLOG_Note, "MIME test started"); *str = '\0'; for (k = 0, type = (EMIME_Type) k; k <= (int) eMIME_T_Unknown; k++, type = (EMIME_Type) k) { for (i = 0, subtype = (EMIME_SubType) i; i <= (int) eMIME_Unknown; i++, subtype = (EMIME_SubType) i) { for (j = 0, encoding = (EMIME_Encoding) j; j < (int) eENCOD_Unknown; j++, encoding = (EMIME_Encoding) j) { assert(!s_Try_MIME(str, type, subtype, encoding)); MIME_ComposeContentTypeEx(type, subtype, encoding, str, sizeof(str)); assert(s_Try_MIME(str, type, subtype, encoding)); } } } assert(s_Try_MIME("content-type: x-ncbi-data/x-asn-binary ", eMIME_T_NcbiData, eMIME_AsnBinary, eENCOD_None)); assert(s_Try_MIME("content-type: application/x-www-form-urlencoded ", eMIME_T_Application, eMIME_WwwForm, eENCOD_Url)); assert(s_Try_MIME("content-TYPE: \t x-ncbi-data/x-asn-text-urlencoded\r", eMIME_T_NcbiData, eMIME_AsnText, eENCOD_Url)); assert(s_Try_MIME("x-ncbi-data/x-eeee", eMIME_T_NcbiData, eMIME_Unknown, eENCOD_None)); assert(s_Try_MIME("x-ncbi-data/plain-", eMIME_T_NcbiData, eMIME_Unknown, eENCOD_None)); assert(!s_Try_MIME("content-TYPE : x-ncbi-data/x-unknown\r", eMIME_T_NcbiData, eMIME_Unknown, eENCOD_None)); assert(s_Try_MIME("text/html", eMIME_T_Text, eMIME_Html, eENCOD_None)); assert(s_Try_MIME("application/xml+soap", eMIME_T_Application, eMIME_XmlSoap, eENCOD_None)); assert(!s_Try_MIME("", eMIME_T_NcbiData, eMIME_Unknown, eENCOD_Unknown)); assert(!s_Try_MIME(0, eMIME_T_NcbiData, eMIME_Unknown, eENCOD_Unknown)); CORE_LOG(eLOG_Note, "MIME test completed"); }
int main(void) { static STimeout timeout/* = 0 */; CONNECTOR connector; FILE* data_file; /* Log and data-log streams */ CORE_SetLOGFormatFlags(fLOG_None | fLOG_Level | fLOG_OmitNoteLevel | fLOG_DateTime); CORE_SetLOGFILE(stderr, 0/*false*/); data_file = fopen("test_ncbi_memory_connector.log", "wb"); assert(data_file); /* Run the tests */ connector = MEMORY_CreateConnector(); CONN_TestConnector(connector, &timeout, data_file, fTC_SingleBounceCheck); connector = MEMORY_CreateConnectorEx(0, 0); CONN_TestConnector(connector, &timeout, data_file, fTC_Everything); /* Cleanup and Exit */ fclose(data_file); CORE_LOG(eLOG_Note, "TEST completed successfully"); CORE_SetLOG(0); return 0; }
static int/*bool*/ TEST_gethostbyaddr(unsigned int host) { const char* name; char buf[1024]; CORE_LOG(eLOG_Note, "------------"); name = SOCK_gethostbyaddr(host, buf, sizeof(buf)); if ( name ) { assert(name == buf); assert(0 < strlen(buf) && strlen(buf) < sizeof(buf)); CORE_LOGF(eLOG_Note, ("SOCK_gethostbyaddr(0x%08X [%s]): \"%s\"", (unsigned int) SOCK_NetToHostLong(host), s_ntoa(host), name)); } else { CORE_LOGF(eLOG_Note, ("SOCK_gethostbyaddr(0x%08X [%s]): <not found>", (unsigned int) SOCK_NetToHostLong(host), s_ntoa(host))); return 0/*false*/; } host = SOCK_gethostbyname(name); CORE_LOGF(eLOG_Note, ("SOCK_gethostbyname(\"%s\"): 0x%08X [%s]", name, (unsigned int) SOCK_NetToHostLong(host), s_ntoa(host))); return 1/*true*/; }
static unsigned int TEST_gethostbyname(const char* name) { char buf[256]; unsigned int host; CORE_LOG(eLOG_Note, "------------"); host = SOCK_gethostbyname(name); CORE_LOGF(eLOG_Note, ("SOCK_gethostbyname(\"%s\"): 0x%08X [%s]", name, (unsigned int) SOCK_NetToHostLong(host), s_ntoa(host))); if ( host ) { name = SOCK_gethostbyaddr(host, buf, sizeof(buf)); if ( name ) { assert(name == buf); assert(0 < strlen(buf) && strlen(buf) < sizeof(buf)); CORE_LOGF(eLOG_Note, ("SOCK_gethostbyaddr(0x%08X [%s]): \"%s\"", (unsigned int) SOCK_NetToHostLong(host), s_ntoa(host), name)); } else { CORE_LOGF(eLOG_Note, ("SOCK_gethostbyaddr(0x%08X [%s]): <not found>", (unsigned int) SOCK_NetToHostLong(host), s_ntoa(host))); } } return host; }
void removeEntity(int entityId) { // Receive entity artemis::Entity& entity = WorldLocator::getObject()->getEntity(entityId); CORE_LOG("LUA", "Removing entity: " + entity.toString()); // Remove entity from world entity.remove(); }
/* Return locked block number {0, 1}, or -1 on error */ static int/*tri-state*/ s_Shmem_RLock(int which) { #ifdef LBSM_DEBUG CORE_LOG(eLOG_Trace, "LBSM R-lock acquire (%d)", which + 1); #endif /*LBSM_DEBUG*/ /* For block 1 then block 0: check [1] first, then continue with [2] */ if (s_Shmem_Lock(which, 1, which/*1:no-wait,0:wait*/) == 0) return which/*block locked*/; return !which || errno == EINVAL ? -1 : s_Shmem_Lock(0, 1, 0/*wait*/); }
extern int g_NCBI_CoreCheckUnlock(void) { /* check that unlock operates on the same lock */ if (s_CoreLock != g_CORE_MT_Lock) { CORE_LOG(eLOG_Critical, "Inconsistent use of CORE MT-Lock detected"); assert(0); return 0/*failure*/; } return 1/*success*/; }
int main(void) { g_NCBI_ConnectRandomSeed = (int) time(0) ^ NCBI_CONNECT_SRAND_ADDEND; srand(g_NCBI_ConnectRandomSeed); CORE_SetLOGFormatFlags(fLOG_None | fLOG_Level | fLOG_OmitNoteLevel | fLOG_DateTime); CORE_SetLOGFILE(stderr, 0/*false*/); CORE_LOG(eLOG_Note, "Miscellaneous tests started"); TEST_URL_Encoding(); TEST_MIME(); TEST_ConnNetInfo(); CORE_LOG(eLOG_Note, "All tests completed successfully"); CORE_SetLOG(0); return 0; }
/* Return locked block number {0, 1}, or -1 in error */ static int/*tri-state*/ s_Shmem_RLock(void) { #ifdef LBSM_DEBUG CORE_LOG(eLOG_Trace, "LBSM R-lock acquire"); #endif /*LBSM_DEBUG*/ /* For block 1, then block 0: check [1] first, then continue with [2] */ if (s_Shmem_Lock(1, 1, 1/*no-wait*/) == 0) return 1/*block 1 locked*/; return errno == EINVAL ? -1 : s_Shmem_Lock(0, 1, 0/*wait*/); }
static int/*bool*/ TEST_isip(const char* ip) { int retval = SOCK_isip(ip); CORE_LOG(eLOG_Note, "------------"); CORE_LOGF(eLOG_Note, ("SOCK_isip(\"%s\"): %s", ip, retval ? "True" : "False")); return retval; }
int createEntity(const char* entityResource, LuaPlus::LuaObject luaPosition) { if(!luaPosition.IsTable()) { CORE_LOG("LUA", "Invalid position object passed to create_entity function. Must be a table"); return INVALID_ENTITY_ID; } sf::Vector2f position = tableToVec2<sf::Vector2f>(luaPosition); artemis::Entity& entity = WorldLocator::getObject()->createEntity(); // Create entity if(!EntityFactory::get().loadFromFile(entityResource, entity)) { CORE_ERROR("Failed to load entity from file: " + std::string(entityResource)); return INVALID_ENTITY_ID; } Transform* transformComp = safeGetComponent<Transform>(&entity); if(!transformComp) { // Create new transform component if it doesn't exist entity.addComponent(new Transform(position.x, position.y)); } else { CORE_DEBUG("Overriding transform with lua object"); transformComp->position = position; } // Commit entity changes entity.refresh(); CORE_LOG("LUA", "Created entity from: " + std::string(entityResource)); return entity.getId(); }
/* Replace strings like: \"expires\":\"__________+00:00:27_\" with strings like: \"expires\":\"2017-04-01T11:04:52Z\" Note that in the input string, the time is an offset [+-]hh:mm:ss from now. For simplicity, the input and adjusted time strings will be equal length, so the max offset is +/- 99:99:99. Also, note that the input mock data must be minified. To support free-form JSON would require more complex parsing. */ static void adjust_mock_times(const char* mock_body, char** mock_body_adj_p) { const char* cp; char sign; int hh, mm, ss; size_t spec_len = strlen("\"expires\":\"__________+00:00:27_\""); size_t prfx_len = strlen("\"expires\":\""); size_t notz_len = strlen("2017-04-01T11:04:52"); assert(mock_body_adj_p); for (cp = mock_body; cp && *cp; cp += spec_len) { /* Get next "expires" offset, if any. */ const char* exp = strstr(cp, "\"expires\":\"__________"); if ( ! exp) break; if (strlen(exp) < spec_len) break; if (sscanf(exp, "\"expires\":\"__________%c%2d:%2d:%2d_\"", &sign, &hh, &mm, &ss) != 4) break; if (sign != '-' && sign != '+') break; if (hh < 0 || mm < 0 || ss < 0) break; ss += 60 * mm + 3600 * hh; /* Get the current UTC epoch time. */ time_t tt_now = time(0); struct tm tm_now; CORE_LOCK_WRITE; tm_now = *gmtime(&tt_now); CORE_UNLOCK; /* Make the adjustment. */ struct tm tm_adj = tm_now; tm_adj.tm_sec += ss; mktime(&tm_adj); /* normalize */ /* Put the adjusted time. */ if ( ! *mock_body_adj_p) *mock_body_adj_p = strdup(mock_body); char* cp2 = *mock_body_adj_p + (exp - mock_body) + prfx_len; if (strftime(cp2, notz_len + 1, "%Y-%m-%dT%H:%M:%S", &tm_adj) != notz_len) { CORE_LOG(eLOG_Fatal, "Unexpected result from strftime(mock_body)"); } cp2[notz_len] = 'Z'; /* undo strftime's overwriting of 'Z' by NIL */ } }
HEAP LBSM_Shmem_Attach(void) { int fallback = 0; /*FIXME: to become a parameter*/ int which; HEAP heap; #ifdef LBSM_DEBUG CORE_LOG(eLOG_Trace, "LBSM ATTACHING%s", fallback ? " (fallback)" : ""); #endif /*LBSM_DEBUG*/ if ((which = s_Shmem_RLock(!fallback)) < 0) { CORE_LOG_ERRNO_X(10, eLOG_Warning, errno, "Cannot lock LBSM shmem to attach"); return 0; } #ifdef LBSM_DEBUG CORE_LOGF(eLOG_Trace, ("LBSM R-lock[%d] acquired", which + 1)); #endif /*LBSM_DEBUG*/ if (!(heap = s_Shmem_Attach(which))) { int/*bool*/ attached = s_Shmem[which] != 0 ? 1/*true*/ : 0/*false*/; s_Shmem_RUnlock(which); CORE_LOGF_ERRNO_X(11, eLOG_Error, errno, ("Cannot %s LBSM shmem[%d]", attached ? "access" : "attach", which + 1)); } #ifdef LBSM_DEBUG else { CORE_LOGF(eLOG_Trace, ("LBSM heap[%p, %p, %d] attached", heap, HEAP_Base(heap), which + 1)); assert(HEAP_Serial(heap) == which + 1); } #endif /*LBSM_DEBUG*/ if (s_Shmem[which = !which]) { #ifdef LBSM_DEBUG CORE_LOGF(eLOG_Trace, ("LBSM shmem[%d] detached", which + 1)); #endif /*LBSM_DEBUG*/ shmdt(s_Shmem[which]); s_Shmem[which] = 0; s_Shmid[which] = -1; } else assert(s_Shmid[which] < 0); s_ShmemSize[which] = 0; return heap; }
void LBSM_Shmem_Detach(HEAP heap) { int which; if (!heap) return; which = HEAP_Serial(heap); #ifdef LBSM_DEBUG CORE_LOGF(eLOG_Trace, ("LBSM shmem[%d] detaching", which)); #endif /*LBSM_DEBUG*/ if (which != 1 && which != 2) { CORE_LOGF_X(12, eLOG_Error, ("Bad block number (%d) for LBSM shmem to unlock", which)); } else s_Shmem_RUnlock(which - 1); HEAP_Detach(heap); #ifdef LBSM_DEBUG CORE_LOG(eLOG_Trace, "LBSM DETACHED"); #endif /*LBSM_DEBUG*/ }
/* Skeletons for the socket i/o test: * TEST__client(...) * TEST__server(...) * establish and close connection; call test i/o functions like * TEST__[client|server]_[1|2|...] (...) */ static void TEST__client(const char* server_host, unsigned short server_port, const STimeout* timeout) { SOCK sock; EIO_Status status; char tmo[80]; if ( timeout ) sprintf(tmo, "%u.%06u", timeout->sec, timeout->usec); else strcpy(tmo, "INFINITE"); CORE_LOGF(eLOG_Note, ("TEST__client(host = \"%s\", port = %hu, timeout = %s", server_host, server_port, tmo)); /* Connect to server */ status = SOCK_Create(server_host, server_port, timeout, &sock); assert(status == eIO_Success); verify(SOCK_SetTimeout(sock, eIO_ReadWrite, timeout) == eIO_Success); verify(SOCK_SetTimeout(sock, eIO_Close, timeout) == eIO_Success); /* Test the simplest randezvous(plain request-reply) * The two peer functions are: * "TEST__[client|server]_1(SOCK sock)" */ TEST__client_1(sock); /* Test a more complex case * The two peer functions are: * "TEST__[client|server]_2(SOCK sock)" */ TEST__client_2(sock); /* Close connection and exit */ status = SOCK_Close(sock); assert(status == eIO_Success || status == eIO_Closed); CORE_LOG(eLOG_Note, "TEST completed successfully"); }
static void TEST__client_1(SOCK sock) { EIO_Status status; size_t n_io, n_io_done; char buf[TEST_BUFSIZE]; CORE_LOG(eLOG_Note, "TEST__client_1(TC1)"); /* Send a short string */ SOCK_SetDataLoggingAPI(eOn); {{ const char* x_C1 = s_C1; n_io = strlen(x_C1) + 1; status = SOCK_Write(sock, x_C1, n_io, &n_io_done, eIO_WritePersist); }} assert(status == eIO_Success && n_io == n_io_done); /* Read the string back (it must be bounced by the server) */ SOCK_SetDataLoggingAPI(eOff); SOCK_SetDataLogging(sock, eOn); n_io = strlen(s_S1) + 1; status = SOCK_Read(sock, buf, n_io, &n_io_done, eIO_ReadPeek); status = SOCK_Read(sock, buf, n_io, &n_io_done, eIO_ReadPlain); if (status == eIO_Closed) CORE_LOG(eLOG_Fatal, "TC1:: connection closed"); assert(status == eIO_Success && n_io == n_io_done); assert(strcmp(buf, s_S1) == 0); assert(SOCK_PushBack(sock, buf, n_io_done) == eIO_Success); memset(buf, '\xFF', n_io_done); assert(SOCK_Read(sock, buf, n_io_done, &n_io_done, eIO_ReadPlain) == eIO_Success); assert(SOCK_Status(sock, eIO_Read) == eIO_Success); assert(strcmp(buf, s_S1) == 0); SOCK_SetDataLogging(sock, eDefault); /* Send a very big binary blob */ {{ size_t i; unsigned char* blob = (unsigned char*) malloc(BIG_BLOB_SIZE); for (i = 0; i < BIG_BLOB_SIZE; blob[i] = (unsigned char) i, i++) continue; for (i = 0; i < 10; i++) { status = SOCK_Write(sock, blob + i * SUB_BLOB_SIZE, SUB_BLOB_SIZE, &n_io_done, eIO_WritePersist); assert(status == eIO_Success && n_io_done==SUB_BLOB_SIZE); } free(blob); }} /* Send a very big binary blob with read on write */ /* (it must be bounced by the server) */ {{ size_t i; unsigned char* blob = (unsigned char*) malloc(BIG_BLOB_SIZE); SOCK_SetReadOnWrite(sock, eOn); for (i = 0; i < BIG_BLOB_SIZE; blob[i] = (unsigned char) i, i++) continue; for (i = 0; i < 10; i++) { status = SOCK_Write(sock, blob + i * SUB_BLOB_SIZE, SUB_BLOB_SIZE, &n_io_done, eIO_WritePersist); assert(status == eIO_Success && n_io_done == SUB_BLOB_SIZE); } /* Receive back a very big binary blob, and check its content */ memset(blob,0,BIG_BLOB_SIZE); for (i = 0; i < 10; i++) { status = SOCK_Read(sock, blob + i * SUB_BLOB_SIZE, SUB_BLOB_SIZE, &n_io_done, eIO_ReadPersist); assert(status == eIO_Success && n_io_done == SUB_BLOB_SIZE); } for (n_io = 0; n_io < BIG_BLOB_SIZE; n_io++) assert(blob[n_io] == (unsigned char) n_io); free(blob); }} /* Try to read more data (must hit EOF as the peer is shut down) */ assert(SOCK_Read(sock, buf, 1, &n_io_done, eIO_ReadPeek) == eIO_Closed); assert(SOCK_Status(sock, eIO_Read) == eIO_Closed); assert(SOCK_Read(sock, buf, 1, &n_io_done, eIO_ReadPlain) == eIO_Closed); assert(SOCK_Status(sock, eIO_Read) == eIO_Closed); /* Shutdown on read */ assert(SOCK_Shutdown(sock, eIO_Read) == eIO_Success); assert(SOCK_Status (sock, eIO_Write) == eIO_Success); assert(SOCK_Status (sock, eIO_Read) == eIO_Closed); assert(SOCK_Read (sock, 0, 0, &n_io_done, eIO_ReadPlain) == eIO_Closed); assert(SOCK_Read (sock, 0, 0, &n_io_done, eIO_ReadPeek) == eIO_Closed); assert(SOCK_Status (sock, eIO_Read) == eIO_Closed); assert(SOCK_Status (sock, eIO_Write) == eIO_Success); assert(SOCK_Read (sock, buf, 1,&n_io_done,eIO_ReadPlain) == eIO_Closed); assert(SOCK_Read (sock, buf, 1,&n_io_done,eIO_ReadPeek) == eIO_Closed); assert(SOCK_Status (sock, eIO_Read) == eIO_Closed); assert(SOCK_Status (sock, eIO_Write) == eIO_Success); /* Shutdown on write */ assert(SOCK_Shutdown(sock, eIO_Write) == eIO_Success); assert(SOCK_Status (sock, eIO_Write) == eIO_Closed); assert(SOCK_Write (sock, 0, 0, &n_io_done, eIO_WritePersist) == eIO_Closed); assert(SOCK_Status (sock, eIO_Write) == eIO_Closed); assert(SOCK_Write (sock, buf, 1, &n_io_done, eIO_WritePersist) == eIO_Closed); assert(SOCK_Status (sock, eIO_Write) == eIO_Closed); /* Double shutdown should be okay */ assert(SOCK_Shutdown(sock, eIO_Read) == eIO_Success); assert(SOCK_Shutdown(sock, eIO_ReadWrite) == eIO_Success); assert(SOCK_Shutdown(sock, eIO_Write) == eIO_Success); assert(SOCK_Status (sock, eIO_Read) == eIO_Closed); assert(SOCK_Status (sock, eIO_Write) == eIO_Closed); assert(SOCK_Status (sock, eIO_ReadWrite) == eIO_InvalidArg); }
int main(int argc, const char* argv[]) { const char custom_body[] = "Subject: Custom sized body\n" "\n" "Custom sized body\n" "0123456789\n"; /* these 11 chars to ignore */ const char* body[] = { "This is a simple test", "This is a test with\n.", 0, ".", "\n.\n", ".\n", "", "a\nb\nc\nd\n.", "a\r\n\rb\r\nc\r\nd\r\n.", ".\na" }; const char* subject[] = { 0, "CORE_SendMail Test", "", }; const char* to[] = { "lavr", "lavr@pavo", " \"Anton Lavrentiev\" <lavr@pavo> , lavr, <lavr> ", }; size_t i, j, k, n, m; const char* mx_host; SSendMailInfo info; const char* retval; STimeout mx_tmo; char* huge_body; short mx_port; char val[32]; FILE* fp; g_NCBI_ConnectRandomSeed = (int) time(0) ^ NCBI_CONNECT_SRAND_ADDEND; srand(g_NCBI_ConnectRandomSeed); CORE_SetLOGFormatFlags(fLOG_None | fLOG_Level | fLOG_OmitNoteLevel | fLOG_DateTime); CORE_SetLOGFILE(stderr, 0/*false*/); ConnNetInfo_GetValue(0, REG_CONN_DEBUG_PRINTOUT, val, sizeof(val), DEF_CONN_DEBUG_PRINTOUT); if (ConnNetInfo_Boolean(val) || (*val && (strcasecmp(val, "all") == 0 || strcasecmp(val, "some") == 0 || strcasecmp(val, "data") == 0))) { SOCK_SetDataLoggingAPI(eOn); } strcpy(val, "@"); SendMailInfo_InitEx(&info, val, eCORE_UsernameCurrent); CORE_LOGF(eLOG_Note, ("@ - <%s>", info.from)); strcpy(info.from, "user0"); SendMailInfo_InitEx(&info, info.from, eCORE_UsernameCurrent); CORE_LOGF(eLOG_Note, ("user0 - <%s>", info.from)); strcpy(info.from, "user1@"); SendMailInfo_InitEx(&info, info.from, eCORE_UsernameCurrent); CORE_LOGF(eLOG_Note, ("user1@ - <%s>", info.from)); strcpy(val, "@host2.net"); SendMailInfo_InitEx(&info, val, eCORE_UsernameLogin); CORE_LOGF(eLOG_Note, ("@host2.net - <%s>", info.from)); strcpy(val, "*****@*****.**"); SendMailInfo_InitEx(&info, val, eCORE_UsernameReal); CORE_LOGF(eLOG_Note, ("[email protected] - <%s>", info.from)); strcpy(info.from, "*****@*****.**"); SendMailInfo_InitEx(&info, info.from, eCORE_UsernameReal); CORE_LOGF(eLOG_Note, ("[email protected] - <%s>", info.from)); SendMailInfo_InitEx(&info, 0, eCORE_UsernameReal); CORE_LOGF(eLOG_Note, ("NULL - <%s>", info.from)); if ((huge_body = malloc(TEST_HUGE_BODY_SIZE)) != 0) { strcpy(huge_body, "user5@"); for (i = 0; i < TEST_HUGE_BODY_SIZE - 6; i++) huge_body[i + 6] = "abcdefghijklmnopqrstuvwxyz."[rand() % 27]; huge_body[TEST_HUGE_BODY_SIZE - 1] = '\0'; SendMailInfo_InitEx(&info, huge_body, eCORE_UsernameCurrent); CORE_LOGF(eLOG_Note, ("HUGE user5@host - <%s>", info.from)); SendMailInfo_InitEx(&info, huge_body + 5, eCORE_UsernameLogin); CORE_LOGF(eLOG_Note, ("HUGE @host - <%s>", info.from)); huge_body[4] = '6'; huge_body[5] = '_'; for (i = 6; i < sizeof(info.from) + 1; i++) { if (huge_body[i] == '.') huge_body[i] = '_'; } huge_body[sizeof(info.from) + 1] = '@'; SendMailInfo_InitEx(&info, huge_body, eCORE_UsernameReal); CORE_LOGF(eLOG_Note, ("HUGE user6 - <%s>", info.from)); huge_body[4] = '7'; huge_body[sizeof(info.from) - 10] = '@'; SendMailInfo_InitEx(&info, huge_body, eCORE_UsernameReal); CORE_LOGF(eLOG_Note, ("LONG user7 - <%s>", info.from)); memcpy(huge_body + sizeof(info.from) - 10, "user8", 5); huge_body[sizeof(info.from) << 1] = '\0'; SendMailInfo_InitEx(&info, huge_body + sizeof(info.from) - 10, eCORE_UsernameReal); CORE_LOGF(eLOG_Note, ("LONG user8 - <%s>", info.from)); SendMailInfo_InitEx(&info, huge_body + sizeof(info.from) + 1, eCORE_UsernameReal); CORE_LOGF(eLOG_Note, ("LONG @host - <%s>", info.from)); free(huge_body); } if (argc > 1) { CORE_LOG(eLOG_Note, "Special test requested"); if ((fp = fopen(argv[1], "rb")) != 0 && fseek(fp, 0, SEEK_END) == 0 && (m = ftell(fp)) != (size_t)(-1) && fseek(fp, 0, SEEK_SET) == 0 && (huge_body = (char*) malloc(m + 1)) != 0 && fread(huge_body, m, 1, fp) == 1) { huge_body[m] = '\0'; CORE_LOGF(eLOG_Note, ("Sending file (%lu bytes)", (unsigned long) m)); retval = CORE_SendMail("lavr", "File", huge_body); if (retval) { CORE_LOGF(eLOG_Fatal, ("Test failed: %s", retval)); } else { CORE_LOG(eLOG_Note, "Test passed"); } } else CORE_LOG_ERRNO(eLOG_Error, errno, "Test failed"); return 0; } #if 1 CORE_LOG(eLOG_Note, "Phase 1 of 2: Testing CORE_SendMail"); n = (sizeof(to)/sizeof(to[0]))* (sizeof(subject)/sizeof(subject[0]))* (sizeof(body)/sizeof(body[0])); m = 0; for (i = 0; i < sizeof(to)/sizeof(to[0]); i++) { for (j = 0; j < sizeof(subject)/sizeof(subject[0]); j++) for (k = 0; k < sizeof(body)/sizeof(body[0]); k++) { CORE_LOGF(eLOG_Note, ("Test %u of %u", (unsigned)(++m), (unsigned)n)); retval = CORE_SendMail(to[i], subject[j], body[k]); if (retval != 0) CORE_LOGF(eLOG_Fatal, ("Test failed: %s", retval)); } } #else CORE_LOG(eLOG_Note, "Phase 1 of 2: Skipping CORE_SendMail tests"); #endif CORE_LOG(eLOG_Note, "Phase 2 of 2: Testing CORE_SendMailEx"); SendMailInfo_Init(&info); mx_host = info.mx_host; mx_port = info.mx_port; mx_tmo = info.mx_timeout; info.mx_host = "localhost"; CORE_LOG(eLOG_Note, "Testing bad port"); info.mx_port = 10/*BAD*/; retval = CORE_SendMailEx("lavr", "CORE_SendMailEx", "Bad port", &info); if (!retval) CORE_LOG(eLOG_Fatal, "Test failed"); CORE_LOGF(eLOG_Note, ("Test passed: %s", retval)); CORE_LOG(eLOG_Note, "Testing bad protocol"); info.mx_port = CONN_PORT_FTP; retval = CORE_SendMailEx("lavr", "CORE_SendMailEx", "Protocol", &info); if (!retval) CORE_LOG(eLOG_Fatal, "Test failed"); CORE_LOGF(eLOG_Note, ("Test passed: %s", retval)); CORE_LOG(eLOG_Note, "Testing timeout"); info.mx_host = "www.ncbi.nlm.nih.gov"; info.mx_timeout.sec = 5; info.mx_port = CONN_PORT_HTTP; retval = CORE_SendMailEx("lavr", "CORE_SendMailEx", "Timeout", &info); if (!retval) CORE_LOG(eLOG_Error, "Test failed"); else CORE_LOGF(eLOG_Note, ("Test passed: %s", retval)); info.mx_port = mx_port; info.mx_timeout = mx_tmo; CORE_LOG(eLOG_Note, "Testing bad host"); info.mx_host = "abrakadabra"; retval = CORE_SendMailEx("lavr", "CORE_SendMailEx", "Bad host", &info); if (!retval) CORE_LOG(eLOG_Fatal, "Test failed"); CORE_LOGF(eLOG_Note, ("Test passed: %s", retval)); info.mx_host = "localhost"; CORE_LOG(eLOG_Note, "Testing cc"); info.cc = "vakatov"; retval = CORE_SendMailEx("", "CORE_SendMailEx", "CC", &info); if (retval) CORE_LOGF(eLOG_Fatal, ("Test failed: %s", retval)); CORE_LOG(eLOG_Note, "Test passed"); CORE_LOG(eLOG_Note, "Testing bcc"); info.cc = 0; info.bcc = "vakatov"; retval = CORE_SendMailEx(0, "CORE_SendMailEx", "Bcc", &info); if (retval) CORE_LOGF(eLOG_Fatal, ("Test failed: %s", retval)); CORE_LOG(eLOG_Note, "Test passed"); CORE_LOG(eLOG_Note, "Testing huge body"); info.cc = 0; info.bcc = 0; if (!(huge_body = (char*) malloc(TEST_HUGE_BODY_SIZE))) CORE_LOG(eLOG_Fatal, "Test failed: Cannot allocate memory"); for (i = 0; i < TEST_HUGE_BODY_SIZE - 1; i++) huge_body[i] = "0123456789\nABCDEFGHIJKLMNOPQRSTUVWXYZ ."[rand() % 39]; huge_body[i] = 0; retval = CORE_SendMailEx("lavr", "CORE_SendMailEx", huge_body, &info); if (retval) CORE_LOGF(eLOG_Fatal, ("Test failed: %s", retval)); if (!(fp = fopen("test_ncbi_sendmail.out", "w")) || fwrite(huge_body, TEST_HUGE_BODY_SIZE - 1, 1, fp) != 1) { CORE_LOG(eLOG_Error, "Test failed: Cannot store huge body to file"); } else { fclose(fp); CORE_LOG(eLOG_Note, "Success: Check test_ncbi_sendmail.out"); } free(huge_body); CORE_LOG(eLOG_Note, "Testing custom headers"); info.header = "Organization: NCBI/NLM/NIH\nReference: abcdefghijk"; retval = CORE_SendMailEx("lavr", "CORE_SendMailEx", "Custom header",&info); if (retval) CORE_LOGF(eLOG_Fatal, ("Test failed: %s", retval)); CORE_LOG(eLOG_Note, "Test passed"); CORE_LOG(eLOG_Note, "Testing no recipients"); retval = CORE_SendMailEx(0, "CORE_SendMailEx", "No recipients", &info); if (!retval) CORE_LOG(eLOG_Fatal, "Test failed"); CORE_LOGF(eLOG_Note, ("Test passed: %s", retval)); CORE_LOG(eLOG_Note, "Testing AS-IS message"); info.mx_options = fSendMail_NoMxHeader; retval = CORE_SendMailEx("lavr", "BAD SUBJECT SHOULD NOT APPEAR BUT IGNORED", "From: yourself\n" "To: yourself\n" "Subject: AS-IS message\n" "\n" "AS-IS", &info); if (retval) CORE_LOGF(eLOG_Fatal, ("Test failed: %s", retval)); CORE_LOG(eLOG_Note, "Test passed"); CORE_LOG(eLOG_Note, "Testing AS-IS custom sized message"); info.body_size = strlen(custom_body) - 11/*to ignore*/; retval = CORE_SendMailEx("<lavr@pavo>", "BAD SUBJECT SHOULD NOT APPEAR BUT IGNORED", custom_body, &info); if (retval) CORE_LOGF(eLOG_Fatal, ("Test failed: %s", retval)); CORE_LOG(eLOG_Note, "Test passed"); info.body_size = 0; info.mx_options = 0; info.mx_host = mx_host; CORE_LOG(eLOG_Note, "Testing bad from"); strcpy(info.from, "blahblah@blahblah"); retval = CORE_SendMailEx("lavr", "CORE_SendMailEx", "Bad from",&info); if (!retval) CORE_LOG(eLOG_Error, "Test failed"); else CORE_LOGF(eLOG_Note, ("Test passed: %s", retval)); SendMailInfo_Init(&info); CORE_LOG(eLOG_Note, "Testing drop no FQDN option"); info.mx_options |= fSendMail_StripNonFQDNHost; retval = CORE_SendMailEx("lavr", "CORE_SendMailEx", "No FQDN", &info); if (retval) CORE_LOGF(eLOG_Error, ("Test failed: %s", retval)); else CORE_LOG(eLOG_Note, "Test passed"); CORE_LOG(eLOG_Note, "Testing bad magic"); info.magic_cookie = 0; retval = CORE_SendMailEx("lavr", "CORE_SendMailEx", "Bad Magic", &info); if (!retval) CORE_LOG(eLOG_Fatal, "Test failed"); CORE_LOGF(eLOG_Note, ("Test passed: %s", retval)); CORE_LOG(eLOG_Note, "TEST completed successfully"); CORE_SetLOG(0); return 0; }
int main(int argc, const char* argv[]) { SHEAP_Block* blk; int r, j, i, n; HEAP heap; char* c; /* CORE_SetLOGFormatFlags(fLOG_None | fLOG_Level | fLOG_OmitNoteLevel); */ CORE_SetLOGFILE(stderr, 0/*false*/); if (argc > 1) g_NCBI_ConnectRandomSeed = atoi(argv[1]); else g_NCBI_ConnectRandomSeed = (int) time(0) ^ NCBI_CONNECT_SRAND_ADDEND; CORE_LOGF(eLOG_Note, ("Using seed %d", g_NCBI_ConnectRandomSeed)); HEAP_Options(eOff/*slow*/, eOn/*newalk*/); srand(g_NCBI_ConnectRandomSeed); for (j = 1; j <= 3; j++) { CORE_LOGF(eLOG_Note, ("Creating heap %d", j)); if (!(heap = HEAP_Create(0, 0, 4096, s_Expand, 0))) CORE_LOG(eLOG_Error, "Cannot create heap"); for (n = 0; n < 1000 && (rand() & 0xFFFF) != 0x1234; n++) { r = rand() & 7; if (r == 1 || r == 3) { int/*bool*/ fast = rand() & 1; i = rand() & 0xFF; if (i) { CORE_LOGF(eLOG_Note, ("Allocating%s data size %d", fast ? " fast" : "", i)); blk = fast ? HEAP_AllocFast(heap, i) : HEAP_Alloc(heap, i); if (blk) { CORE_LOGF(eLOG_Note, ("Done @%u, size %u", HEAP_ADDR(blk, heap), blk->size)); } else CORE_LOG(eLOG_Error, "Allocation failed"); c = (char*) blk + sizeof(*blk); while (i--) *c++ = rand(); s_Walk(heap, 0); } else { assert(!(fast ? HEAP_AllocFast(heap, i) : HEAP_Alloc(heap, i))); } } else if (r == 2 || r == 4) { blk = 0; do { if (!(blk = HEAP_Walk(heap, blk))) break; } while (rand() & 7); if (blk && (short) blk->flag) { unsigned size = blk->size; unsigned data_size = size - sizeof(*blk); CORE_LOGF(eLOG_Note, ("Freeing @%u, size %u, data size %u", HEAP_ADDR(blk, heap), size, data_size)); assert(data_size); HEAP_Free(heap, blk); CORE_LOG(eLOG_Note, "Done"); s_Walk(heap, 0); } } else if (r == 5) { const SHEAP_Block* prev = 0; unsigned ok = 0; blk = 0; for (;;) { if (!(blk = HEAP_Walk(heap, blk))) break; if ((short) blk->flag && (rand() & 7)) { char buf[32]; unsigned size = blk->size; int/*bool*/ fast = rand() & 1; unsigned data_size = size - sizeof(*blk); if (!fast || !prev) *buf = '\0'; else sprintf(buf, ", prev @%u", HEAP_ADDR(prev, heap)); CORE_LOGF(eLOG_Note, ("Freeing%s%s @%u%s in walk," " size %u, data size %u", fast ? " fast" : "", ok ? " more" : "", HEAP_ADDR(blk,heap), buf, size, data_size)); assert(data_size); if (fast) HEAP_FreeFast(heap, blk, prev); else HEAP_Free(heap, blk); CORE_LOG(eLOG_Note, "Done"); s_Walk(heap, 0); ok = 1; if (prev && !((short) prev->flag)) continue; } prev = blk; } if (!ok) s_Walk(heap, "the"); else CORE_LOG(eLOG_Note, "Done with freeing while walking"); } else if (r == 6 || r == 7) { HEAP newheap; if (r == 6) { int/*bool*/ fast = rand() & 1; if (fast) { CORE_LOG(eLOG_Note, "Attaching heap fast"); newheap = HEAP_AttachFast(HEAP_Base(heap), HEAP_Size(heap), 0); } else { CORE_LOG(eLOG_Note, "Attaching heap"); newheap = HEAP_Attach(HEAP_Base(heap), 0); } } else { CORE_LOG(eLOG_Note, "Copying heap"); newheap = HEAP_Copy(heap, 0, 0); } if (!newheap) { CORE_LOGF(eLOG_Error, ("%s failed", r == 6 ? "Attach" : "Copy")); } else s_Walk(newheap, r == 6 ? "attached" : "copied"); HEAP_Detach(newheap); } else { TNCBI_Size size = HEAP_Size(heap); HEAP newheap; CORE_LOG(eLOG_Note, "Trimming heap"); newheap = HEAP_Trim(heap); CORE_LOGF(eLOG_Note, ("Heap %strimmed: %u -> %u", newheap ? "" : "NOT ", size, HEAP_Size(newheap))); if (newheap) { heap = newheap; s_Walk(heap, "trimmed"); } } } HEAP_Destroy(heap); CORE_LOGF(eLOG_Note, ("Heap %d done", j)); } CORE_LOG(eLOG_Note, "Test completed"); CORE_SetLOG(0); return 0; }
int main(int argc, const char* argv[]) { const char* service = argc > 1 && *argv[1] ? argv[1] : "bounce"; static char obuf[8192 + 2]; SConnNetInfo* net_info; CONNECTOR connector; EIO_Status status; char ibuf[1024]; CONN conn; size_t n; setlocale(LC_ALL, ""); g_NCBI_ConnectRandomSeed = (int) time(0) ^ NCBI_CONNECT_SRAND_ADDEND; srand(g_NCBI_ConnectRandomSeed); CORE_SetLOGFormatFlags(fLOG_None | fLOG_Level | fLOG_OmitNoteLevel | fLOG_DateTime); CORE_SetLOGFILE(stderr, 0/*false*/); net_info = ConnNetInfo_Create(service); ConnNetInfo_AppendArg(net_info, "testarg", "val"); ConnNetInfo_AppendArg(net_info, "service", "none"); ConnNetInfo_AppendArg(net_info, "platform", "none"); ConnNetInfo_AppendArg(net_info, "address", "2010"); ConnNetInfo_Log(net_info, eLOG_Note, CORE_GetLOG()); connector = SERVICE_CreateConnectorEx(service, fSERV_Any, net_info, 0); if (!connector) CORE_LOG(eLOG_Fatal, "Failed to create service connector"); if (CONN_Create(connector, &conn) != eIO_Success) CORE_LOG(eLOG_Fatal, "Failed to create connection"); if (argc > 2) { strncpy0(obuf, argv[2], sizeof(obuf) - 2); obuf[n = strlen(obuf)] = '\n'; obuf[++n] = '\0'; if (CONN_Write(conn, obuf, strlen(obuf), &n, eIO_WritePersist) != eIO_Success) { CONN_Close(conn); CORE_LOG(eLOG_Fatal, "Cannot write to connection"); } assert(n == strlen(obuf)); } else { for (n = 0; n < 10; n++) { size_t m; for (m = 0; m < sizeof(obuf) - 2; m++) obuf[m] = "0123456789\n"[rand() % 11]; obuf[m++] = '\n'; obuf[m] = '\0'; if (CONN_Write(conn, obuf, strlen(obuf), &m, eIO_WritePersist) != eIO_Success) { if (!n) { CONN_Close(conn); CORE_LOG(eLOG_Fatal, "Cannot write to connection"); } else break; } assert(m == strlen(obuf)); } } for (;;) { if (CONN_Wait(conn, eIO_Read, net_info->timeout) != eIO_Success) { CONN_Close(conn); CORE_LOG(eLOG_Fatal, "Failed to wait for reading"); } status = CONN_Read(conn, ibuf, sizeof(ibuf), &n, eIO_ReadPersist); if (n) { char* descr = CONN_Description(conn); CORE_DATAF(eLOG_Note, ibuf, n, ("%lu bytes read from service (%s%s%s):", (unsigned long) n, CONN_GetType(conn), descr ? ", " : "", descr ? descr : "")); if (descr) free(descr); } if (status != eIO_Success) { if (status != eIO_Closed) CORE_LOGF(n ? eLOG_Error : eLOG_Fatal, ("Read error: %s", IO_StatusStr(status))); break; } } ConnNetInfo_Destroy(net_info); CONN_Close(conn); #if 0 CORE_LOG(eLOG_Note, "Trying ID1 service"); net_info = ConnNetInfo_Create(service); connector = SERVICE_CreateConnectorEx("ID1", fSERV_Any, net_info); ConnNetInfo_Destroy(net_info); if (!connector) CORE_LOG(eLOG_Fatal, "Service ID1 not available"); if (CONN_Create(connector, &conn) != eIO_Success) CORE_LOG(eLOG_Fatal, "Failed to create connection"); if (CONN_Write(conn, "\xA4\x80\x02\x01\x02\x00", 7, &n, eIO_WritePersist) != eIO_Success) { CONN_Close(conn); CORE_LOG(eLOG_Fatal, "Cannot write to service ID1"); } assert(n == 7); if (CONN_Read(conn, ibuf, sizeof(ibuf), &n, eIO_ReadPlain) != eIO_Success){ CONN_Close(conn); CORE_LOG(eLOG_Fatal, "Cannot read from service ID1"); } CORE_LOGF(eLOG_Note, ("%d bytes read from service ID1", n)); CONN_Close(conn); #endif CORE_LOG(eLOG_Note, "TEST completed successfully"); CORE_SetLOG(0); return 0/*okay*/; }
int main(int argc, const char* argv[]) { CONN conn; CONNECTOR connector; EIO_Status status; const char* inp_file; /* cmd.-line args */ if (argc != 2) { Usage(argv[0], "Must specify the input file name"); } inp_file = argv[1]; /* log and data log streams */ CORE_SetLOGFormatFlags(fLOG_None | fLOG_Level | fLOG_OmitNoteLevel | fLOG_DateTime); CORE_SetLOGFILE(stderr, 0/*false*/); /* run the test */ fprintf(stderr, "Starting the FILE CONNECTOR test...\n" "Copy data from file \"%s\" to file \"%s\".\n\n", inp_file, OUT_FILE); /* create connector, and bind it to the connection */ connector = FILE_CreateConnector(inp_file, OUT_FILE); if ( !connector ) { Usage(argv[0], "Failed to create FILE connector"); } verify(CONN_Create(connector, &conn) == eIO_Success); /* pump the data from one file to another */ for (;;) { char buf[100]; size_t n_read, n_written; /* read */ status = CONN_Read(conn, buf, sizeof(buf), &n_read, eIO_ReadPlain); if (status != eIO_Success) { fprintf(stderr, "CONN_Read() failed (status: %s)\n", IO_StatusStr(status)); break; } fprintf(stderr, "READ: %ld bytes\n", (long) n_read); /* write */ status = CONN_Write(conn, buf, n_read, &n_written, eIO_WritePersist); if (status != eIO_Success) { fprintf(stderr, "CONN_Write() failed (status: %s)\n", IO_StatusStr(status)); assert(0); break; } assert(n_written == n_read); } assert(status == eIO_Closed); /* cleanup, exit */ verify(CONN_Close(conn) == eIO_Success); CORE_LOG(eLOG_Note, "TEST completed successfully"); CORE_SetLOG(0); return 0; }
int main(int argc, const char *argv[]) { const char *retmsg = ""; const char *svc = ""; const char *test_nums = ""; int retval = 1; int live = 0; int i; for (i=1; i < argc; ++i) { if (strcmp(argv[i], "-f") == 0 && i < argc-1) { ++i; s_json_file = argv[i]; } else if (strcmp(argv[i], "-l") == 0) { live = 1; } else if (strcmp(argv[i], "-n") == 0 && i < argc-1) { ++i; test_nums = argv[i]; retmsg = "Not all tests run."; } else if (strcmp(argv[i], "-s") == 0 && i < argc-1) { ++i; svc = argv[i]; retmsg = "Run for a given service instead of standard tests."; } else if (strcmp(argv[i], "-u") == 0 && i < argc-1) { ++i; s_user_header = argv[i]; retmsg = "User header overridden."; } else { fprintf(stderr, "USAGE: %s [OPTIONS...]\n", argv[0]); fprintf(stderr, " [-h] help\n" " [-f ARG] test file\n" " [-l] live test data (default is mock data)\n" " [-n ARG] comma-separated test selections (eg 1,2,5)\n" " [-s ARG] service\n" " [-u ARG] user header\n"); goto out; } } CORE_SetLOGFormatFlags(fLOG_None | fLOG_Level | fLOG_OmitNoteLevel); CORE_SetLOGFILE(stderr, 0/*false*/); if (*svc) { test_service(live, svc); } else { if ( ! run_tests(live, test_nums) && ! *retmsg) retmsg = "Not all tests passed."; } out: CORE_LOG(eLOG_Note, ""); if (strcmp(retmsg, "") == 0) { /* The only successful condition is a run of all tests. */ CORE_LOG(eLOG_Note, "SUCCESS - All tests passed."); retval = 0; } else { CORE_LOGF(eLOG_Note, ("FAIL - %s", retmsg)); } CORE_SetLOG(0); return retval; }
int main(int argc, char** argv) { /* Prepare to connect: parse and check cmd.-line args, etc. */ const char* host = argc > 1 ? argv[1] : ""; unsigned short port = argc > 2 ? atoi(argv[2]) : CONN_PORT_HTTP; const char* path = argc > 3 ? argv[3] : ""; const char* args = argc > 4 ? argv[4] : ""; const char* inp_file = argc > 5 ? argv[5] : ""; const char* user_header = argc > 6 ? argv[6] : ""; size_t content_length; STimeout timeout; SOCK sock; EIO_Status status; char buffer[10000]; CORE_SetLOGFormatFlags(fLOG_None | fLOG_Level | fLOG_OmitNoteLevel | fLOG_DateTime); CORE_SetLOGFILE(stderr, 0/*false*/); SOCK_SetupSSL(NcbiSetupTls); fprintf(stderr, "Running...\n" " Executable: '%s'\n" " URL host: '%s'\n" " URL port: %hu\n" " URL path: '%s'\n" " URL args: '%s'\n" " Input data file: '%s'\n" " User header: '%s'\n" "Response(if any) from the hit URL goes to standard output.\n\n", argv[0], host, port, path, args, inp_file, user_header); if ( argc < 4 ) { fprintf(stderr, "Usage: %s host port path args inp_file [user_header]\n", argv[0]); CORE_LOG(eLOG_Fatal, "Two few arguments"); return 1; } {{ FILE *fp = fopen(inp_file, "rb"); long offset; if ( !fp ) { CORE_LOGF(eLOG_Fatal, ("Non-existent file '%s'", inp_file)); return 2; } if ( fseek(fp, 0, SEEK_END) != 0 || (offset = ftell(fp)) < 0 ) { CORE_LOGF(eLOG_Fatal, ("Cannot obtain size of file '%s'", inp_file)); return 2; } fclose(fp); content_length = (size_t) offset; }} timeout.sec = 10; timeout.usec = 0; /* Connect */ sock = URL_Connect(host, port, path, args, /*NCBI_FAKE_WARNING*/ eReqMethod_Any, content_length, &timeout, &timeout, user_header, 1/*true*/, port == CONN_PORT_HTTPS ? fSOCK_LogDefault | fSOCK_Secure : fSOCK_LogDefault); if ( !sock ) return 3; {{ /* Pump data from the input file to socket */ FILE* fp = fopen(inp_file, "rb"); if ( !fp ) { CORE_LOGF(eLOG_Fatal, ("Cannot open file '%s' for reading", inp_file)); return 4; } for (;;) { size_t n_written; size_t n_read = fread(buffer, 1, sizeof(buffer), fp); if ( n_read <= 0 ) { if ( content_length ) { CORE_LOGF(eLOG_Fatal, ("Cannot read last %lu bytes from file '%s'", (unsigned long) content_length, inp_file)); return 5; } break; } assert(content_length >= n_read); content_length -= n_read; status = SOCK_Write(sock, buffer, n_read, &n_written, eIO_WritePersist); if ( status != eIO_Success ) { CORE_LOGF(eLOG_Fatal, ("Error writing to socket: %s", IO_StatusStr(status))); return 6; } } fclose(fp); }} /* Read reply from socket, write it to STDOUT */ {{ size_t n_read; for (;;) { status = SOCK_Read(sock, buffer, sizeof(buffer), &n_read, eIO_ReadPlain); if (status != eIO_Success) break; fwrite(buffer, 1, n_read, stdout); } if ( status != eIO_Closed ) { CORE_LOGF(eLOG_Error, ("Read error after %ld byte(s) from socket: %s", (long) content_length, IO_StatusStr(status))); } fprintf(stdout, "\n"); }} /* Success: close the socket, cleanup, and exit */ SOCK_Close(sock); CORE_LOG(eLOG_Note, "TEST completed successfully"); CORE_SetLOG(0); return 0; }
static void TEST__server_1(SOCK sock) { EIO_Status status; size_t n_io, n_io_done; char buf[TEST_BUFSIZE]; CORE_LOG(eLOG_Note, "TEST__server_1(TS1)"); /* Receive and send back a short string */ SOCK_SetDataLogging(sock, eOn); n_io = strlen(s_C1) + 1; status = SOCK_Read(sock, buf, n_io, &n_io_done, eIO_ReadPlain); assert(status == eIO_Success && n_io == n_io_done); assert(strcmp(buf, s_C1) == 0 || strcmp(buf, s_M1) == 0); SOCK_SetDataLogging(sock, eDefault); SOCK_SetDataLoggingAPI(eOn); n_io = strlen(s_S1) + 1; status = SOCK_Write(sock, s_S1, n_io, &n_io_done, eIO_WritePersist); assert(status == eIO_Success && n_io == n_io_done); SOCK_SetDataLoggingAPI(eOff); /* Receive a very big binary blob, and check its content */ {{ #define DO_LOG_SIZE 300 #define DONT_LOG_SIZE BIG_BLOB_SIZE - DO_LOG_SIZE unsigned char* blob = (unsigned char*) malloc(BIG_BLOB_SIZE); status = SOCK_Read(sock,blob,DONT_LOG_SIZE,&n_io_done,eIO_ReadPersist); assert(status == eIO_Success && n_io_done == DONT_LOG_SIZE); SOCK_SetDataLogging(sock, eOn); status = SOCK_Read(sock, blob + DONT_LOG_SIZE, DO_LOG_SIZE, &n_io_done, eIO_ReadPersist); assert(status == eIO_Success && n_io_done == DO_LOG_SIZE); SOCK_SetDataLogging(sock, eDefault); for (n_io = 0; n_io < BIG_BLOB_SIZE; n_io++) assert(blob[n_io] == (unsigned char) n_io); free(blob); }} /* Receive a very big binary blob, and write data back */ {{ unsigned char* blob = (unsigned char*) malloc(BIG_BLOB_SIZE); int i; for (i = 0; i < 10; i++) { /* X_SLEEP(1);*/ status = SOCK_Read(sock, blob + i * SUB_BLOB_SIZE, SUB_BLOB_SIZE, &n_io_done, eIO_ReadPersist); assert(status == eIO_Success && n_io_done == SUB_BLOB_SIZE); status = SOCK_Write(sock, blob + i * SUB_BLOB_SIZE, SUB_BLOB_SIZE, &n_io_done, eIO_WritePersist); assert(status == eIO_Success && n_io_done == SUB_BLOB_SIZE); } for (n_io = 0; n_io < BIG_BLOB_SIZE; n_io++) assert(blob[n_io] == (unsigned char) n_io); free(blob); }} /* Shutdown on write */ #ifdef NCBI_OS_MSWIN assert(SOCK_Shutdown(sock, eIO_ReadWrite) == eIO_Success); #else assert(SOCK_Shutdown(sock, eIO_Write) == eIO_Success); #endif assert(SOCK_Status (sock, eIO_Write) == eIO_Closed); assert(SOCK_Write (sock, 0, 0, &n_io_done, eIO_WritePersist) == eIO_Closed); assert(SOCK_Status (sock, eIO_Write) == eIO_Closed); #ifdef NCBI_OS_MSWIN assert(SOCK_Status (sock, eIO_Read) == eIO_Closed); #else assert(SOCK_Status (sock, eIO_Read) == eIO_Success); #endif assert(SOCK_Close (sock) == eIO_Success); }
/* mostly error-handling-free for the moment :-/ */ static int run_tests(int live, const char *test_nums) { x_JSON_Value *root_value = NULL; x_JSON_Object *root_obj; x_JSON_Array *test_arr; const char *test_num = test_nums; size_t n_tests = 0, n_pass = 0, n_fail = 0, n_skip = 0; size_t n_comm_unset, n_comm_set; int all_enabled = 0; CORE_LOG(eLOG_Note, "============================================================"); CORE_LOGF(eLOG_Note, ("Test file: %s", s_json_file)); root_value = x_json_parse_file(s_json_file); root_obj = x_json_value_get_object(root_value); if (x_json_object_has_value_of_type(root_obj, "all_enabled", JSONNumber) && (int)x_json_object_get_number(root_obj, "all_enabled") != 0) { all_enabled = 1; } test_arr = x_json_object_get_array(root_obj, "tests"); n_tests = x_json_array_get_count(test_arr); assert(n_tests == x_json_array_get_count(test_arr)); size_t it; for (it = 0; it < n_tests; ++it) { x_JSON_Object *test_obj; x_JSON_Array *hit_arr; const char *svc, *hdr; int err = 0; test_obj = x_json_array_get_object(test_arr, it); s_results[it].name = x_json_object_get_string(test_obj, "alias"); /* Skip tests not in user-supplied test numbers list. */ if (test_nums && *test_nums) { size_t next_test; if ( ! test_num) break; if (sscanf(test_num, FMT_SIZE_T, &next_test) == 1) { if (it+1 != next_test) { s_results[it].result = "-"; continue; } else { test_num = strchr(test_num, ','); if (test_num && *test_num) test_num++; } } else { CORE_LOGF(eLOG_Error, ("Invalid test numbers list: %s", test_num)); return 0; } } CORE_LOG(eLOG_Note, "============================================================"); /* Skip disabled tests. */ if ( ! all_enabled) { if (x_json_object_has_value_of_type(test_obj, "disabled", JSONNumber)) { if ((int)x_json_object_get_number(test_obj, "disabled") == 1) { ++n_skip; CORE_LOGF(eLOG_Note, ("Skipping test " FMT_SIZE_T ": %s", it+1, x_json_object_get_string(test_obj, "alias"))); s_results[it].result = "skip"; continue; } } } CORE_LOGF(eLOG_Note, ("Running test " FMT_SIZE_T ": %s", it+1, s_results[it].name)); svc = x_json_object_get_string(test_obj, "service"); if (x_json_object_has_value_of_type(test_obj, "http_user_header", JSONString)) hdr = x_json_object_get_string(test_obj, "http_user_header"); else hdr = ""; if (x_json_object_has_value_of_type(test_obj, "expect_error", JSONString)) { if (strcmp(x_json_object_get_string(test_obj, "expect_error"), "yes") == 0) { err = 1; } } if (x_json_object_has_value_of_type(test_obj, "url", JSONString)) CORE_LOGF(eLOG_Note, (" url: %s", x_json_object_get_string(test_obj, "url"))); CORE_LOGF(eLOG_Note, (" service: %s", svc)); CORE_LOGF(eLOG_Note, (" header: %s", *hdr ? hdr : "(none)")); CORE_LOGF(eLOG_Note, (" expected error: %s", err ? "yes" : "no")); /* Redo common unset/set - they could have been changed by a prior test. */ if (x_json_object_dothas_value_of_type(root_obj, "common.env_unset", JSONArray)) { x_JSON_Array *comm_unset_arr; comm_unset_arr = x_json_object_dotget_array(root_obj, "common.env_unset"); n_comm_unset = x_json_array_get_count(comm_unset_arr); for (size_t it2 = 0; it2 < n_comm_unset; ++it2) { const char *name = x_json_array_get_string( comm_unset_arr, it2); CORE_LOGF(eLOG_Note, (" Unsetting common var: %s", name)); unsetenv(name); assert(getenv(name) == NULL); } } if (x_json_object_dothas_value_of_type(root_obj, "common.env_set", JSONArray)) { x_JSON_Array *comm_set_arr; comm_set_arr = x_json_object_dotget_array(root_obj, "common.env_set"); n_comm_set = x_json_array_get_count(comm_set_arr); for (size_t it2 = 0; it2 < n_comm_set; ++it2) { x_JSON_Object *env_obj = x_json_array_get_object(comm_set_arr, it2); const char *name = x_json_object_get_name(env_obj, 0); const char *val = x_json_value_get_string( x_json_object_get_value_at(env_obj, 0)); CORE_LOGF(eLOG_Note, (" Setting common var: %s=%s", name, val)); setenv(name, val, 1); assert(strcmp(val, getenv(name)) == 0); } } /* Per-test unset/set */ if (x_json_object_has_value_of_type(test_obj, "env_unset", JSONArray)) { x_JSON_Array *unset_arr; size_t n_unset; unset_arr = x_json_object_get_array(test_obj, "env_unset"); n_unset = x_json_array_get_count(unset_arr); CORE_LOGF(eLOG_Note, ( " Unset per-test environment variables: " FMT_SIZE_T, n_unset)); if (n_unset) { for (size_t it2 = 0; it2 < n_unset; ++it2) { const char *name = x_json_array_get_string(unset_arr, it2); CORE_LOGF(eLOG_Note, (" %s", name)); unsetenv(name); assert(getenv(name) == NULL); } } } if (x_json_object_has_value_of_type(test_obj, "env_set", JSONArray)) { x_JSON_Array *set_arr; size_t n_set; set_arr = x_json_object_get_array(test_obj, "env_set"); n_set = x_json_array_get_count(set_arr); CORE_LOGF(eLOG_Note, ( " Set per-test environment variables: " FMT_SIZE_T, n_set)); if (n_set) { for (size_t it2 = 0; it2 < n_set; ++it2) { x_JSON_Object *env_obj = x_json_array_get_object(set_arr, it2); const char *name = x_json_object_get_name(env_obj, 0); const char *val = x_json_value_get_string( x_json_object_get_value_at(env_obj, 0)); CORE_LOGF(eLOG_Note, (" %s=%s", name, val)); setenv(name, val, 1); assert(strcmp(val, getenv(name)) == 0); } } } hit_arr = x_json_object_get_array(test_obj, "expect_hits"); s_n_hits_exp = (int)x_json_array_get_count(hit_arr); assert(s_n_hits_exp < MAX_HITS); CORE_LOGF(eLOG_Note, (" Expected hits: %d", s_n_hits_exp)); int it2; for (it2 = 0; it2 < s_n_hits_exp; ++it2) { x_JSON_Object *hit_obj = x_json_array_get_object(hit_arr, it2); const char *type = "HTTP", *loc = "no", *priv = "no"; const char *xtra = "", *stfl = "no"; unsigned short port = 0; if (x_json_object_has_value_of_type(hit_obj, "type", JSONString)) type = x_json_object_get_string(hit_obj, "type"); if (x_json_object_has_value_of_type(hit_obj, "xtra", JSONString)) xtra = x_json_object_get_string(hit_obj, "xtra"); if (x_json_object_has_value_of_type(hit_obj, "loc", JSONString)) loc = x_json_object_get_string(hit_obj, "loc" ); if (x_json_object_has_value_of_type(hit_obj, "priv", JSONString)) priv = x_json_object_get_string(hit_obj, "priv"); if (x_json_object_has_value_of_type(hit_obj, "stfl", JSONString)) stfl = x_json_object_get_string(hit_obj, "stfl"); assert(strlen(type) <= LEN_TYPE); assert(strlen(xtra) <= LEN_XTRA); assert(strlen(loc ) <= LEN_LOC ); assert(strlen(priv) <= LEN_PRIV); assert(strlen(stfl) <= LEN_STFL); strcpy(s_hits_exp[it2].type, type); strcpy(s_hits_exp[it2].xtra, xtra); strcpy(s_hits_exp[it2].loc , loc ); strcpy(s_hits_exp[it2].priv, priv); strcpy(s_hits_exp[it2].stfl, stfl); assert(x_json_object_has_value_of_type(hit_obj, "host", JSONString)); assert(strlen(x_json_object_get_string(hit_obj, "host")) <= LEN_HOST); strcpy(s_hits_exp[it2].host, x_json_object_get_string(hit_obj, "host")); assert(strcmp(s_hits_exp[it2].loc , "no" ) == 0 || strcmp(s_hits_exp[it2].loc , "yes") == 0); assert(strcmp(s_hits_exp[it2].priv, "no" ) == 0 || strcmp(s_hits_exp[it2].priv, "yes") == 0); assert(strcmp(s_hits_exp[it2].stfl, "no" ) == 0 || strcmp(s_hits_exp[it2].stfl, "yes") == 0); assert(x_json_object_has_value_of_type(hit_obj, "port", JSONNumber)); port = (unsigned short)x_json_object_get_number(hit_obj, "port"); assert(port > 0); s_hits_exp[it2].port = port; s_hits_exp[it2].match = 0; CORE_LOGF(eLOG_Note, ( " Expected server %d: %s %s:%hu L=%s P=%s S=%s %s", it2, s_hits_exp[it2].type, s_hits_exp[it2].host, s_hits_exp[it2].port, s_hits_exp[it2].loc, s_hits_exp[it2].priv, s_hits_exp[it2].stfl, s_hits_exp[it2].xtra)); } const char *mock_body = NULL; if (x_json_object_has_value_of_type(test_obj, "mock_body", JSONString)) { mock_body = x_json_object_get_string(test_obj, "mock_body"); if (mock_body && *mock_body) CORE_LOG(eLOG_Note, " Mock HTTP body found."); else CORE_LOG(eLOG_Note, " Empty mock HTTP body found."); } int repop = 0, reset = 0; if (x_json_object_has_value_of_type( test_obj, "iter-repop", JSONString) && strcmp(x_json_object_get_string( test_obj, "iter-repop"), "yes") == 0) { repop = 1; } if (x_json_object_has_value_of_type( test_obj, "iter-reset", JSONString) && strcmp(x_json_object_get_string( test_obj, "iter-reset"), "yes") == 0) { reset = 1; } /* Run the test */ if (run_a_test(it, live, svc, hdr, 1, err, mock_body, repop, reset)) { ++n_pass; s_results[it].result = "ok"; } else { ++n_fail; s_results[it].result = "FAIL"; } } CORE_LOG (eLOG_Note, "============================================================"); CORE_LOGF(eLOG_Note, (FMT_SIZE_T " tests: " FMT_SIZE_T " passed, " FMT_SIZE_T " failed, " FMT_SIZE_T " skipped", n_tests, n_pass, n_fail, n_skip)); CORE_LOG (eLOG_Note, "============================================================"); CORE_LOG (eLOG_Note, "Test Source Result Description"); CORE_LOG (eLOG_Note, "---- ------ ------ ----------------------------------------------"); for (it = 0; it < n_tests; ++it) { if ( ! s_results[it].result || ! *s_results[it].result || *s_results[it].result == '-') { continue; } char tests_buf[6]; if (it+1 > 9999) strcpy (tests_buf, "####"); else sprintf(tests_buf, FMT_SIZE_T, it+1); CORE_LOGF(eLOG_Note, ("%4s %6s %6s %s", tests_buf, s_results[it].live ? "live" : "mock", s_results[it].result, s_results[it].name)); } if (root_value) x_json_value_free(root_value); return (n_tests > 0 && n_pass == n_tests - n_skip) ? 1 : 0; }
static void TEST__client_2(SOCK sock) { #define W_FIELD 10 #define N_FIELD 1000 #define N_REPEAT 10 #define N_RECONNECT 3 EIO_Status status; size_t n_io, n_io_done, i; char buf[W_FIELD * N_FIELD + 1]; CORE_LOGF(eLOG_Note, ("TEST__client_2(TC2) @:%hu", SOCK_GetLocalPort(sock, eNH_HostByteOrder))); /* fill out a buffer to send to server */ memset(buf, 0, sizeof(buf)); for (i = 0; i < N_FIELD; i++) { sprintf(buf + i * W_FIELD, "%10lu", (unsigned long)i); } /* send the buffer to server, then get it back */ for (i = 0; i < N_REPEAT; i++) { char buf1[sizeof(buf)]; STimeout w_to, r_to; int/*bool*/ w_timeout_on = (int)(i%2); /* if to start from */ int/*bool*/ r_timeout_on = (int)(i%2); /* zero or inf. timeout */ char* x_buf; /* set timeout */ w_to.sec = 0; w_to.usec = 0; status = SOCK_SetTimeout(sock, eIO_Write, w_timeout_on ? &w_to : 0); assert(status == eIO_Success); /* reconnect */ if ((i % N_RECONNECT) == 0) { size_t j = i / N_RECONNECT; do { SOCK_SetDataLogging(sock, eOn); status = SOCK_Reconnect(sock, 0, 0, 0); SOCK_SetDataLogging(sock, eDefault); CORE_LOGF(eLOG_Note, ("TC2::reconnect @:%hu: i=%lu, j=%lu, status=%s", SOCK_GetLocalPort(sock, eNH_HostByteOrder), (unsigned long) i, (unsigned long) j, IO_StatusStr(status))); assert(status == eIO_Success); assert(SOCK_Status(sock, eIO_Read) == eIO_Success); assert(SOCK_Status(sock, eIO_Write) == eIO_Success); /* give a break to let server reset the listening socket */ X_SLEEP(1); } while ( j-- ); } /* send */ x_buf = buf; n_io = sizeof(buf); do { X_SLEEP(1); status = SOCK_Write(sock, x_buf, n_io, &n_io_done, eIO_WritePersist); if (status == eIO_Closed) CORE_LOG(eLOG_Fatal, "TC2::write: connection closed"); CORE_LOGF(eLOG_Note, ("TC2::write:" " i=%d, status=%7s, n_io=%5lu, n_io_done=%5lu" " timeout(%d): %5u.%06us", (int) i, IO_StatusStr(status), (unsigned long) n_io, (unsigned long) n_io_done, (int) w_timeout_on, w_to.sec, w_to.usec)); if ( !w_timeout_on ) { assert(status == eIO_Success && n_io_done == n_io); } else { const STimeout* x_to; assert(status == eIO_Success || status == eIO_Timeout); x_to = SOCK_GetTimeout(sock, eIO_Write); assert(w_to.sec == x_to->sec && w_to.usec == x_to->usec); } n_io -= n_io_done; x_buf += n_io_done; if (status == eIO_Timeout) s_DoubleTimeout(&w_to); status = SOCK_SetTimeout(sock, eIO_Write, &w_to); assert(status == eIO_Success); w_timeout_on = 1/*true*/; } while ( n_io ); /* get back the just sent data */ r_to.sec = 0; r_to.usec = 0; status = SOCK_SetTimeout(sock, eIO_Read, r_timeout_on ? &r_to : 0); assert(status == eIO_Success); x_buf = buf1; n_io = sizeof(buf1); do { if (i%2 == 0) { /* peek a little piece twice and compare */ char xx_buf1[128], xx_buf2[128]; size_t xx_io_done1, xx_io_done2; if (SOCK_Read(sock, xx_buf1, sizeof(xx_buf1), &xx_io_done1, eIO_ReadPeek) == eIO_Success && SOCK_Read(sock, xx_buf2, xx_io_done1, &xx_io_done2, eIO_ReadPeek) == eIO_Success) { assert(xx_io_done1 >= xx_io_done2); assert(memcmp(xx_buf1, xx_buf2, xx_io_done2) == 0); } } status = SOCK_Read(sock, x_buf, n_io, &n_io_done, eIO_ReadPlain); if (status == eIO_Closed) { assert(SOCK_Status(sock, eIO_Read) == eIO_Closed); CORE_LOG(eLOG_Fatal, "TC2::read: connection closed"); } CORE_LOGF(eLOG_Note, ("TC2::read: " " i=%d, status=%7s, n_io=%5lu, n_io_done=%5lu" " timeout(%d): %5u.%06us", (int) i, IO_StatusStr(status), (unsigned long) n_io, (unsigned long) n_io_done, (int) r_timeout_on, r_to.sec, r_to.usec)); if ( !r_timeout_on ) { assert(status == eIO_Success && n_io_done > 0); } else { const STimeout* x_to; assert(status == eIO_Success || status == eIO_Timeout); x_to = SOCK_GetTimeout(sock, eIO_Read); assert(r_to.sec == x_to->sec && r_to.usec == x_to->usec); } n_io -= n_io_done; x_buf += n_io_done; if (status == eIO_Timeout) s_DoubleTimeout(&r_to); status = SOCK_SetTimeout(sock, eIO_Read, &r_to); assert(status == eIO_Success); r_timeout_on = 1/*true*/; } while ( n_io ); assert(memcmp(buf, buf1, sizeof(buf)) == 0); } }
static void TEST__server_2(SOCK sock, LSOCK lsock) { EIO_Status status; size_t n_io, n_io_done; char buf[TEST_BUFSIZE]; STimeout r_to, w_to, rc_to; size_t i; CORE_LOG(eLOG_Note, "TEST__server_2(TS2)"); r_to.sec = 0; r_to.usec = 0; w_to = r_to; rc_to.sec = 30; rc_to.usec = 123456; /* goto */ l_reconnect: /* reconnection loopback */ SOCK_SetDataLogging(sock, eOn); status = SOCK_SetTimeout(sock, eIO_Read, &r_to); assert(status == eIO_Success); status = SOCK_SetTimeout(sock, eIO_Write, &w_to); assert(status == eIO_Success); for (i = 0; ; i++) { char* x_buf; /* read data from socket */ n_io = sizeof(buf); status = SOCK_Read(sock, buf, n_io, &n_io_done, eIO_ReadPlain); switch ( status ) { case eIO_Success: CORE_LOGF(eLOG_Note, ("TS2::read:" " [%lu], status=%7s, n_io=%5lu, n_io_done=%5lu", (unsigned long)i, IO_StatusStr(status), (unsigned long)n_io, (unsigned long)n_io_done)); assert(n_io_done > 0); break; case eIO_Closed: CORE_LOG(eLOG_Note, "TS2::read: connection closed"); assert(SOCK_Status(sock, eIO_Read) == eIO_Closed); /* close connection */ status = SOCK_Close(sock); assert(status == eIO_Success || status == eIO_Closed); /* reconnect */ if ( !lsock ) return; CORE_LOG(eLOG_Note, "TS2::reconnect"); if ((status = LSOCK_Accept(lsock, &rc_to, &sock)) != eIO_Success) return; assert(SOCK_Status(sock, eIO_Read) == eIO_Success); /* !!! */ goto l_reconnect; case eIO_Timeout: CORE_LOGF(eLOG_Note, ("TS2::read:" " [%lu] timeout expired: %5u sec, %6u usec", (unsigned long)i, r_to.sec, r_to.usec)); assert(n_io_done == 0); s_DoubleTimeout(&r_to); status = SOCK_SetTimeout(sock, eIO_Read, &r_to); assert(status == eIO_Success); assert(SOCK_Status(sock, eIO_Read) == eIO_Timeout); break; default: CORE_LOGF(eLOG_Fatal, ("TS2::read: status = %d", (int) status)); } /* switch */ /* write(just the same) data back to client */ n_io = n_io_done; x_buf = buf; while ( n_io ) { status = SOCK_Write(sock, buf, n_io, &n_io_done, eIO_WritePersist); switch ( status ) { case eIO_Success: CORE_LOGF(eLOG_Note, ("TS2::write:" " [%lu], status=%7s, n_io=%5lu, n_io_done=%5lu", (unsigned long)i, IO_StatusStr(status), (unsigned long)n_io, (unsigned long)n_io_done)); assert(n_io_done > 0); break; case eIO_Closed: CORE_LOG(eLOG_Fatal, "TS2::write: connection closed"); return; case eIO_Timeout: CORE_LOGF(eLOG_Note, ("TS2::write:" " [%lu] timeout expired: %5u sec, %6u usec", (unsigned long)i, w_to.sec, w_to.usec)); assert(n_io_done == 0); s_DoubleTimeout(&w_to); status = SOCK_SetTimeout(sock, eIO_Write, &w_to); assert(status == eIO_Success); break; default: CORE_LOGF(eLOG_Fatal, ("TS2::write: status = %d", (int) status)); } /* switch */ n_io -= n_io_done; x_buf += n_io_done; } } }
static int run_a_test(size_t test_idx, int live, const char *svc, const char *hdr, int check_for_match, int exp_err, const char *mock_body_in, int repop, int reset) { const SSERV_Info *info = NULL; SConnNetInfo *net_info; SERV_ITER iter; const char *mock_body = NULL; char *mock_body_adj = NULL; int n_matches_perfect = 0, n_matches_near = 0; int success = 0, errors = 0; int retval = -1; s_n_hits_got = 0; /* Adjust mock data for current time, if necessary. */ adjust_mock_times(mock_body_in, &mock_body_adj); mock_body = mock_body_adj ? mock_body_adj : mock_body_in; /* Select the HTTP data source (live or mock). */ s_results[test_idx].live = live; if ( ! s_results[test_idx].live && ( ! mock_body || ! *mock_body)) { CORE_TRACE("Mock HTTP data source unavailable."); s_results[test_idx].live = 1; } if (s_results[test_idx].live) { CORE_TRACE("Using a live HTTP data source."); SERV_NAMERD_SetConnectorSource(NULL); /* use live HTTP */ } else { CORE_TRACE("Using a mock HTTP data source."); if ( ! SERV_NAMERD_SetConnectorSource(mock_body)) { CORE_LOG(eLOG_Error, "Unable to create mock HTTP data source."); retval = 1; goto out; } } /* Set up the server iterator. */ net_info = ConnNetInfo_Create(svc); if (*hdr) ConnNetInfo_SetUserHeader(net_info, hdr); iter = SERV_OpenP(svc, fSERV_All | (strpbrk(svc, "?*") ? fSERV_Promiscuous : 0), SERV_LOCALHOST, 0/*port*/, 0.0/*preference*/, net_info, 0/*skip*/, 0/*n_skip*/, 0/*external*/, 0/*arg*/, 0/*val*/); ConnNetInfo_Destroy(net_info); /* Fetch the server hits from namerd. */ if (iter) { for (; s_n_hits_got < MAX_HITS && (info = SERV_GetNextInfo(iter)); ++s_n_hits_got) { if (info->type & fSERV_Http) { CORE_LOGF(eLOG_Note, (" HTTP extra (path): %s", SERV_HTTP_PATH(&info->u.http))); } strcpy(s_hits_got[s_n_hits_got].type, SERV_TypeStr(info->type)); strcpy(s_hits_got[s_n_hits_got].xtra, (info->type & fSERV_Http) ? SERV_HTTP_PATH(&info->u.http) : ""); strcpy(s_hits_got[s_n_hits_got].loc , (info->site & fSERV_Local ) ? "yes" : "no"); strcpy(s_hits_got[s_n_hits_got].priv, (info->site & fSERV_Private ) ? "yes" : "no"); strcpy(s_hits_got[s_n_hits_got].stfl, (info->mode & fSERV_Stateful) ? "yes" : "no"); SOCK_ntoa(info->host, s_hits_got[s_n_hits_got].host, LEN_HOST); s_hits_got[s_n_hits_got].port = info->port; s_hits_got[s_n_hits_got].match = 0; char *info_str; info_str = SERV_WriteInfo(info); CORE_LOGF(eLOG_Note, (" Found server %d: %s", s_n_hits_got, info_str ? info_str : "?")); if (info_str) free(info_str); } /* Make sure endpoint data can be repopulated and reset. */ if (repop && s_n_hits_got) { /* repopulate */ CORE_LOG(eLOG_Trace, "Repopulating the service mapper."); if ( ! info && ! SERV_GetNextInfo(iter)) { CORE_LOG(eLOG_Error, "Unable to repopulate endpoint data."); errors = 1; } } if (reset && s_n_hits_got) { /* reset */ CORE_LOG(eLOG_Trace, "Resetting the service mapper."); SERV_Reset(iter); if ( ! SERV_GetNextInfo(iter)) { CORE_LOG(eLOG_Error, "No services found after reset."); errors = 1; } } SERV_Close(iter); } else { errors = 1; } /* Search for matches unless this is a standalone run. */ if (check_for_match) { /* Search for perfect matches first (order is unknown). */ int it_exp, it_got; for (it_got=0; it_got < s_n_hits_got; ++it_got) { for (it_exp=0; it_exp < s_n_hits_exp; ++it_exp) { if (s_hits_exp[it_exp].match) continue; /*if (check_match(fMatch_Default, it_exp, it_got)) {*/ if (check_match(fMatch_All, it_exp, it_got)) { CORE_LOGF(eLOG_Note, ( " Found server %d perfectly matched expected server " "%d.", it_got, it_exp)); s_hits_exp[it_exp].match = 1; s_hits_got[it_got].match = 1; ++n_matches_perfect; break; } } } /* If not all found, search again but exclude host:port from match. */ for (it_got=0; it_got < s_n_hits_got; ++it_got) { if (s_hits_got[it_got].match) continue; for (it_exp=0; it_exp < s_n_hits_exp; ++it_exp) { if (s_hits_exp[it_exp].match) continue; if (check_match(fMatch_NoHostPort, it_exp, it_got)) { CORE_LOGF(eLOG_Note, ( " Found server %d nearly matched expected server %d.", it_got, it_exp)); s_hits_exp[it_exp].match = 1; s_hits_got[it_got].match = 1; ++n_matches_near; log_match_diffs(it_exp, it_got); break; } } } /* List any non-matching servers. */ for (it_exp=0; it_exp < s_n_hits_exp; ++it_exp) { if ( ! s_hits_exp[it_exp].match) CORE_LOGF(eLOG_Note, ( " Expected server %d didn't match any found servers.", it_exp)); } for (it_got=0; it_got < s_n_hits_got; ++it_got) { if ( ! s_hits_got[it_got].match) CORE_LOGF(eLOG_Note, ( " Found server %d didn't match any expected servers.", it_got)); } CORE_LOGF(n_matches_perfect + n_matches_near == s_n_hits_got ? eLOG_Note : eLOG_Error, ("Expected %d servers; found %d (%d perfect matches, %d near " "matches, and %d non-matches).", s_n_hits_exp, s_n_hits_got, n_matches_perfect, n_matches_near, s_n_hits_got - n_matches_perfect - n_matches_near)); if (!errors && s_n_hits_got == s_n_hits_exp && s_n_hits_got == n_matches_perfect + n_matches_near) { success = 1; } retval = (success != exp_err ? 1 : 0); CORE_LOGF(eLOG_Note, ("Test result: %s.", retval ? (success ? "PASS" : "PASS (with expected error)") : (success ? "FAIL (success when error expected)" : "FAIL"))); } out: if (mock_body_adj) free(mock_body_adj); return retval == -1 ? (success != exp_err ? 1 : 0) : retval; }