#include #include #include #include #include #include #include "../../src/mesh-model/MKLib.h" #include "MKControlTests.h" #include "HeartBeatListenerTests.h" using namespace std; const string mongo::BSONObjBuilder::numStrs[100]; // apparently not defined void MKControlTests::setup() { cm = new MKCommModel(); } void MKControlTests::tear_down() { delete cm; } pid_t MKControlTests::setupResponder() { pid_t respondProc; errno = 0; respondProc = fork(); if(errno) { perror("setupResponder fork"); return 0; } if(respondProc == 0) { execlp(MK_PYTHON_BIN, MK_PYTHON_BIN, "../../src/heartbeat-sender/CCResponse.py", (char *) 0); perror("setupResponder exec"); exit(EXIT_FAILURE); } else { usleep(500000); // wait 1/2 a sec } return respondProc; } void MKControlTests::killResponder(pid_t respondProc) { if(respondProc != 0) { kill(respondProc, SIGKILL); waitpid(respondProc, NULL, NULL); } } void MKControlTests::testStart() { // setup() will start and tear_down() will stop the thread } void MKControlTests::testSendTraceRouteRequest() { HeartBeatPacket *hb2 = HeartBeatListenerTests::makeHB2(); MKNode *n1, *n2; MKRequest *req; cm->update(hb2); free(hb2); TEST_ASSERT(cm->size() == 3); n1 = cm->find(MKLib::parseMACAddr("01:02:03:04:05:01")); n2 = cm->find(MKLib::parseMACAddr("01:02:03:04:05:02")); req = MKRequest::route(n1, n2); req->send(); } void MKControlTests::testUpdateBSONLatency1() { /* MKNode *node; mac_t mac = MKLib::parseMACAddr("0a:0b:0c:0d:0e:0f"); ip4_t ip = MKLib::parseIP4Addr("10.0.0.1"); mongo::BSONObjBuilder build; mongo::BSONObj bData; // append mac array build.appendBinData("src_mac", MAC_ADDR_BYTES, mongo::BinDataGeneral,(unsigned char *) &mac.addr[0]); // bson cpp DOES NOT automatically serializes into little-endian (network byte order) build.append("src_ip", htonl(ip)); bData = build.done(); TEST_ASSERT(cm->size() == 0); cm->update(&bData, ip); TEST_ASSERT(cm->size() == 1); node = cm->find(mac); TEST_ASSERT(node != NULL); if(node != NULL) { TEST_ASSERT(node->getIP4() == ip); } */ } void MKControlTests::testUpdateBSONRoute1() { HeartBeatPacket *hb = HeartBeatListenerTests::makeHB4(); MKNode *n1, *n2; MKNode::MKRoute *rt; MKRequest *req; //vector route; int len = 2; pid_t respond; cm->update(hb); free(hb); TEST_ASSERT(cm->size() == 2); n1 = cm->find(MKLib::parseMACAddr("00:00:00:00:00:01")); n2 = cm->find(MKLib::parseMACAddr("00:00:00:00:00:02")); respond = setupResponder(); req = MKRequest::route(n1, n2); req->send(); usleep(500000); // wait 1/2 a sec //respondMockTraceRoute(req->id(), "127.0.0.1", "127.0.0.1"); // someone set up us the route // 10.0.0.1-3 /* route.push_back(MKLib::parseIP4Addr("10.0.0.1")); route.push_back(MKLib::parseIP4Addr("10.0.0.3")); route.push_back(MKLib::parseIP4Addr("10.0.0.2")); */ //receiveMockResponse(req->id(), route); rt = n1->route(n2->getMAC()); TEST_ASSERT(rt != NULL); if(rt != NULL) { TEST_ASSERT(rt->length() == len); } killResponder(respond); } void MKControlTests::testCCListen1() { HeartBeatPacket *hb2 = HeartBeatListenerTests::makeHB2(); MKNode *n1, *n2; MKNode::MKRoute *rt; MKRequest *req; int lat = 50; cm->update(hb2); free(hb2); TEST_ASSERT(cm->size() == 3); n1 = cm->find(MKLib::parseMACAddr("01:02:03:04:05:01")); n2 = cm->find(MKLib::parseMACAddr("01:02:03:04:05:02")); req = MKRequest::latency(n1, n2); respondMockLatency(req->id(), lat); rt = n1->route(n2->getMAC()); TEST_ASSERT(rt != NULL) if(rt != NULL) { TEST_ASSERT(rt->latency == lat); } } bool MKControlTests::respondMockLatency(int requestID, int latency) { int status; ostringstream text1, text2; pid_t pid = fork(); assert(pid >= 0); if(pid == 0) { text1 << requestID; text2 << latency; errno = 0; execl(MOCK_CC_SCRIPT, MOCK_CC_SCRIPT, text1.str().c_str(), text2.str().c_str(), (char *) 0); perror("mockcc"); exit(EXIT_FAILURE); } waitpid(pid, &status, 0); // wait to send the packet if(WEXITSTATUS(status) == 0) { usleep(100000); // wait 100 more ms to process it return true; } else { return false; } } bool MKControlTests::respondMockTraceRoute(int requestID, char *target) { int status; ostringstream text1; pid_t pid = fork(); assert(pid >= 0); if(pid == 0) { text1 << requestID; errno = 0; execl(MOCK_CC_TR_SCRIPT, MOCK_CC_TR_SCRIPT, text1.str().c_str(), target, (char *) 0); perror("mockcctr"); exit(EXIT_FAILURE); } waitpid(pid, &status, 0); // wait to send the packet if(WEXITSTATUS(status) == 0) { usleep(100000); // wait 100 more ms to process it return true; } else { return false; } return true; }