#include #include #include #include #include #include "HeartBeatListenerTests.h" using namespace std; /** Creates two commmodels */ void HeartBeatListenerTests::setup() { cm = new MKCommModel(); } /** Deletes two commmodels */ void HeartBeatListenerTests::tear_down() { delete cm; } void HeartBeatListenerTests::testStart() { // setup() will start and tear_down() will stop the thread } /** Creates one HeartBeatPacket and sends it to the commmodel. Used to test the commmodel update function */ void HeartBeatListenerTests::testUpdateHB1() { MKNode *node; HeartBeatPacket pkt; ip4_t ip = MKLib::parseIP4Addr("10.0.0.1"); pkt.pkt_vermaj = 0; pkt.pkt_vermin = 0; pkt.pkt_size = 0; pkt.uptime = 0; pkt.mac[0] = 1; pkt.mac[1] = 2; pkt.mac[2] = 3; pkt.mac[3] = 4; pkt.mac[4] = 5; pkt.mac[5] = 6; pkt.ip_addr = htonl(ip); pkt.num_stations = 0; // number of neighbors pkt.mp = 0; TEST_ASSERT(cm->size() == 0); cm->update(&pkt); TEST_ASSERT(cm->size() == 1); node = cm->find(MacAddr(pkt.mac)); TEST_ASSERT(node != NULL); TEST_ASSERT(node->getIP4() == ip); } /** Creates one HeartBeatPacket with two neighboring packets, and returns the HeartBeatPacket */ HeartBeatPacket *HeartBeatListenerTests::makeHB2() { HeartBeatPacket pkt, *hbPkt; NeighborInfo nb1; NeighborInfo nb2; NeighborInfo nbPkts[2]; ip4_t ip1 = MKLib::parseIP4Addr("10.0.0.1"), ip2 = MKLib::parseIP4Addr("10.0.0.2"), ip3 = MKLib::parseIP4Addr("10.0.0.3"); nb1.zsize = 0; nb1.mac[0] = 1; nb1.mac[1] = 2; nb1.mac[2] = 3; nb1.mac[3] = 4; nb1.mac[4] = 5; nb1.mac[5] = 2; nb1.ip_addr = htonl(ip2); nb1.metric = 0; nb1.inactive = htonl(15); nb1.rx_bytes = 0; nb1.rx_packets = 0; nb1.tx_bytes = 0; nb1.tx_packets = 0; nb1.signal = htons(55); nb1.tx_rate = 0; nb1.throughput = htonl(30 * 1024 * 1024); // 30 mb nbPkts[0] = nb1; nb2.zsize = 0; nb2.mac[0] = 1; nb2.mac[1] = 2; nb2.mac[2] = 3; nb2.mac[3] = 4; nb2.mac[4] = 5; nb2.mac[5] = 3; nb2.ip_addr = htonl(ip3); nb2.metric = 0; nb2.inactive = htonl(35); nb2.rx_bytes = 0; nb2.rx_packets = 0; nb2.tx_bytes = 0; nb2.tx_packets = 0; nb2.signal = htons(75); nb2.tx_rate = 0; nb2.throughput = htonl(25 * 1024 * 1024); nbPkts[1] = nb2; pkt.pkt_vermaj = 0; pkt.pkt_vermin = 0; pkt.pkt_size = 0; pkt.uptime = 0; pkt.mac[0] = 1; pkt.mac[1] = 2; pkt.mac[2] = 3; pkt.mac[3] = 4; pkt.mac[4] = 5; pkt.mac[5] = 1; pkt.ip_addr = htonl(ip1); pkt.num_stations = 2; // number of neighbors return makeHeartBeat(&pkt, &nbPkts[0]); } /** Creates one HeartBeatPacket with nine neighboring packets, and returns the HeartBeatPacket */ HeartBeatPacket *HeartBeatListenerTests::makeHB3() { HeartBeatPacket pkt, *hbPkt; NeighborInfo nb1; NeighborInfo nb2; NeighborInfo nbPkts[9]; ip4_t ip1 = MKLib::parseIP4Addr("10.0.0.1"); int inactive_itr = 5; int throughput_itr = 5 * 1024 * 1024; int signal_itr = 25; int i = 0; /* itr also for changing mac */ ip4_t ip_arr[9] = { MKLib::parseIP4Addr("10.0.0.2"), MKLib::parseIP4Addr("10.0.0.3"), MKLib::parseIP4Addr("10.0.0.4"), MKLib::parseIP4Addr("10.0.0.5"), MKLib::parseIP4Addr("10.0.0.6"), MKLib::parseIP4Addr("10.0.0.7"), MKLib::parseIP4Addr("10.0.0.8"), MKLib::parseIP4Addr("10.0.0.9"), MKLib::parseIP4Addr("10.0.0.10")}; for(i = 0; i < 10; i++) { nbPkts[i].zsize = 0; nbPkts[i].mac[0] = 1; nbPkts[i].mac[1] = 2; nbPkts[i].mac[2] = 3; nbPkts[i].mac[3] = 4; nbPkts[i].mac[4] = 5; nbPkts[i].mac[5] = i; nbPkts[i].metric = 0; nbPkts[i].rx_bytes = 0; nbPkts[i].rx_packets = 0; nbPkts[i].tx_bytes = 0; nbPkts[i].tx_packets = 0; nbPkts[i].tx_rate = 0; nbPkts[i].ip_addr = htonl(ip_arr[i]); nbPkts[i].signal = signal_itr; nbPkts[i].inactive = inactive_itr; nbPkts[i].throughput = throughput_itr; inactive_itr += 5; nb1.signal += 5; throughput_itr += 2 * 1024 * 1024; } pkt.pkt_vermaj = 0; pkt.pkt_vermin = 0; pkt.pkt_size = 0; pkt.uptime = 0; pkt.mac[0] = 1; pkt.mac[1] = 2; pkt.mac[2] = 3; pkt.mac[3] = 4; pkt.mac[4] = 5; pkt.mac[5] = 0; pkt.ip_addr = htonl(ip1); pkt.num_stations = 9; // number of neighbors return makeHeartBeat(&pkt, &nbPkts[0]); } /** Creates one HeartBeatPacket with one neighboring packet, and returns the HeartBeatPacket */ HeartBeatPacket *HeartBeatListenerTests::makeHB4() { HeartBeatPacket pkt, *hbPkt; NeighborInfo nb1; ip4_t ip1 = MKLib::parseIP4Addr("127.0.0.1"), ip2 = MKLib::parseIP4Addr("127.0.0.2"); NeighborInfo nbPkts[1]; nb1.zsize = 0; nb1.mac[0] = 0; nb1.mac[1] = 0; nb1.mac[2] = 0; nb1.mac[3] = 0; nb1.mac[4] = 0; nb1.mac[5] = 2; nb1.ip_addr = htonl(ip2); nb1.metric = 0; nb1.inactive = htonl(35); nb1.rx_bytes = 0; nb1.rx_packets = 0; nb1.tx_bytes = 0; nb1.tx_packets = 0; nb1.signal = htons(75); nb1.tx_rate = 0; nb1.throughput = htonl(25 * 1024 * 1024); nbPkts[0] = nb1; pkt.pkt_vermaj = 0; pkt.pkt_vermin = 0; pkt.pkt_size = 0; pkt.uptime = 0; pkt.mac[0] = 0; pkt.mac[1] = 0; pkt.mac[2] = 0; pkt.mac[3] = 0; pkt.mac[4] = 0; pkt.mac[5] = 1; pkt.ip_addr = htonl(ip1); pkt.num_stations = 1; // number of neighbors return makeHeartBeat(&pkt, &nbPkts[0]); } /** Uses the HeartBeatPacket returned from makeHB2, and sends it to the commmodel using the update function */ void HeartBeatListenerTests::testUpdateHB2() { MKNode *node; ip4_t ip1 = MKLib::parseIP4Addr("10.0.0.1"), ip2 = MKLib::parseIP4Addr("10.0.0.2"), ip3 = MKLib::parseIP4Addr("10.0.0.3"); mac_t mac1 = MKLib::parseMACAddr("01:02:03:04:05:01"), mac2 = MKLib::parseMACAddr("01:02:03:04:05:02"), mac3 = MKLib::parseMACAddr("01:02:03:04:05:03"); HeartBeatPacket *hbPkt = makeHB2(); TEST_ASSERT(cm->size() == 0); cm->update(hbPkt); TEST_ASSERT(cm->size() == 3); node = cm->find(mac1); TEST_ASSERT(node != NULL); if(node != NULL) { TEST_ASSERT(node->getIP4() == ip1); } node = cm->find(mac2); TEST_ASSERT(node != NULL); if(node != NULL) { TEST_ASSERT(node->getIP4() == ip2); } node = cm->find(mac3); TEST_ASSERT(node != NULL); if(node != NULL) { TEST_ASSERT(node->getIP4() == ip3); } } void HeartBeatListenerTests::testListen1() { TEST_FAIL("todo: heartbeat listener test"); } HeartBeatPacket *HeartBeatListenerTests::makeHeartBeat(HeartBeatPacket *pkt, NeighborInfo *nbs) { HeartBeatPacket *hb = (HeartBeatPacket *) malloc(sizeof(HeartBeatPacket) + pkt->num_stations * sizeof(NeighborInfo)); memcpy(hb, pkt, sizeof(HeartBeatPacket)); // copy neighbor array to start at hb->mp memcpy(&hb->mp, nbs, pkt->num_stations * sizeof(NeighborInfo)); return hb; }