void test_PacketReader_parseBinaryFile_append() { /* * Add our 2 packets */ pl->LogPacket(*mp1); pl->LogPacket(*mp2); pl->flush(0); /* * First parse, should have 2 */ unsigned int i = p->parseBinaryFile(pl->getFile()); CPPUNIT_ASSERT( i == 2 ); /* * Trigger append mode */ p->setAppend(true); /* * Second parse. Since no reset, should have 4 */ i = p->parseBinaryFile(pl->getFile()); CPPUNIT_ASSERT( i = 4 ); /* * And back again */ p->setAppend(false); i = p->parseBinaryFile(pl->getFile()); CPPUNIT_ASSERT( i == 2 ); }
void test_PacketReader_parseBinaryFile_empty() { unsigned int i = p->parseBinaryFile("/tmp/file_does_not_exist"); /* * Any sort of file operation that would fail, should produce * a 'zero packets parsed' return. */ CPPUNIT_ASSERT( i == 0 ); }
int main(int argc, char** argv) { /** * Argument Wrangling * */ boost::program_options::options_description desc( "PacketPlayer" ); boost::program_options::variables_map vm; boost::asio::io_service io_service; //boost::array<char, MAX_PACKET_BYTES> recvBuffer; boost::asio::ip::udp::endpoint sender_endpoint; //size_t bytes_recvd; PacketReader* p; /** * Note: options inside the configuration file that are NOT listed here * become ignored and are not accessible. */ desc.add_options() ( "help,h", "Display help message" ) ( "server", boost::program_options::value<std::string>()->default_value("localhost"), "MetaServer host. \nDefault:localhost" ) ( "port", boost::program_options::value<int>()->default_value(8453), "MetaServer port. \nDefault:8453" ) ( "playonly", boost::program_options::value<std::string>(), "Play output info only.\nDefault: false" ) ( "file", boost::program_options::value<std::string>(), "Binary Packet logfile.\nDefault: none" ) ; try { boost::program_options::store( boost::program_options::parse_command_line(argc, argv, desc), vm ); boost::program_options::notify(vm); /** * Special case for help */ if ( vm.count("help") ) { std::cout << desc << std::endl; return 0; } std::cout << "Server : " << vm["server"].as<std::string>() << std::endl; std::cout << "Port : " << vm["port"].as<int>() << std::endl; std::cout << "---------------" << std::endl; for (boost::program_options::variables_map::iterator it=vm.begin(); it!=vm.end(); ++it ) { if ( it->second.value().type() == typeid(int) ) { std::cout << it->first.c_str() << "=" << it->second.as<int>() << std::endl; } else if (it->second.value().type() == typeid(std::string) ) { std::cout << it->first.c_str() << "=" << it->second.as<std::string>().c_str() << std::endl; } else if (it->second.value().type() == typeid(attribute_list) ) { std::cout << it->first.c_str() << "=Attribute List" << std::endl; } } std::cout << "-------------------------" << std::endl; /** * because boost query is too stupid to take port as an int */ std::stringstream port_str; port_str << vm["port"].as<int>(); /** * Wrangle the file etc options */ std::string file = ""; bool playOnly = false; if( vm.count("playonly") ) { std::string s = vm["playonly"].as<std::string>(); if ( boost::iequals(s,"true") ) { playOnly = true; } } if( vm.count("file") ) { file = vm["file"].as<std::string>(); } if ( file.empty() ) { std::cout << "--file option not specified, but is required" << std::endl; return 13; } /** * Initialize reader */ p = new PacketReader(); int pkts = p->parseBinaryFile(file); if( playOnly ) { while(p->hasPacket()) { MetaServerPacket msp = p->pop(); unsigned int i = msp.getPacketType(); std::string s = msp.getOutBound() ? ">>>>" : "<<<<"; std::cout << "Packet ["; std::cout << std::setfill('0') << std::setw(10) << msp.getSequence(); std::cout << "] " << s << " : " << NMT_PRETTY[i] << std::endl; } } else { std::cout << "TODO: play packets to listed metaserver" << std::endl; } std::cout << "Packet File : " << file << std::endl; std::cout << "Packet Count: " << pkts << std::endl; } catch (std::exception& e) { std::cerr << "Exception: " << e.what() << std::endl; } std::cout << "All Done!" << std::endl; return 0; }
void test_PacketReader_parseBinaryFile() { MetaServerPacket* mp3 = new MetaServerPacket(); std::string attr_name = "foobar"; std::string attr_val = "waka waka"; mp3->setPacketType(NMT_SERVERATTR); mp3->setSequence(3); mp3->setTimeOffset(3); mp3->addPacketData(attr_name.length()); mp3->addPacketData(attr_val.length()); mp3->addPacketData(attr_name); mp3->addPacketData(attr_val); MetaServerPacket* mp4 = new MetaServerPacket(); mp4->setPacketType(NMT_SERVERSHAKE); mp4->addPacketData(666); mp4->setSequence(4); mp4->setTimeOffset(4); pl->LogPacket(*mp1); pl->LogPacket(*mp2); pl->LogPacket(*mp3); // pl->LogPacket(*mp4); pl->flush(0); unsigned int i = p->parseBinaryFile(pl->getFile()); CPPUNIT_ASSERT( i == 3 ); MetaServerPacket mr1 = p->pop(); MetaServerPacket mr2 = p->pop(); MetaServerPacket mr3 = p->pop(); // MetaServerPacket mr4 = p->pop(); /* * Now we make sure that we've got a good round trip */ std::cout << "mr1.getPacketType(): " << mr1.getPacketType() << " / " << NMT_SERVERKEEPALIVE << std::endl; CPPUNIT_ASSERT( mr1.getPacketType() == NMT_SERVERKEEPALIVE ); CPPUNIT_ASSERT( mr1.getSequence() == 1 ); CPPUNIT_ASSERT( mr1.getTimeOffset() == 1 ); CPPUNIT_ASSERT( mr1.getSize() == 4 ); std::cout << "mr2.getPacketType(): " << mr2.getPacketType() << " / " << NMT_SERVERSHAKE << std::endl; CPPUNIT_ASSERT( mr2.getPacketType() == NMT_SERVERSHAKE ); CPPUNIT_ASSERT( mr2.getSequence() == 2 ); CPPUNIT_ASSERT( mr2.getTimeOffset() == 2 ); CPPUNIT_ASSERT( mr2.getSize() == 8 ); /* * I have no idea why this doesn't work !? */ // unsigned int mr2data = mr2.getIntData(4); // std::cout << "mr2.getIntData(4): " << mr2data << std::endl; // CPPUNIT_ASSERT( mr2data == 22 ); //unsigned int mr4data = mr4.getIntData(4); //std::cout << "mr4.getIntData(4): " << mr4data << std::endl; //CPPUNIT_ASSERT( mr2data == 22 ); std::cout << "mr3.getPacketType(): " << mr3.getPacketType() << " / " << NMT_SERVERATTR << std::endl; std::cout << "mr3.getSize(): " << mr3.getSize() << std::endl; unsigned int n = mr3.getIntData(4); unsigned int v = mr3.getIntData(8); std::string msg = mr3.getPacketMessage(12); std::cout << "mr3:n:" << n << std::endl; std::cout << "mr3:v:" << v << std::endl; std::cout << "mr3:msg:" << msg << std::endl; CPPUNIT_ASSERT( mr3.getPacketType() == NMT_SERVERATTR ); CPPUNIT_ASSERT( mr3.getSequence() == 3 ); CPPUNIT_ASSERT( mr3.getTimeOffset() == 3 ); CPPUNIT_ASSERT( msg.substr(0,n) == "foobar" ); CPPUNIT_ASSERT( msg.substr(n) == "waka waka" ); // unsigned int name_length = in.getIntData(4); // unsigned int value_length = in.getIntData(8); // std::string msg = in.getPacketMessage(12); // std::string name = msg.substr(0,name_length); // std::string value = msg.substr(name_length); // std::string ip = in.getAddressStr(); // m_Logger.debug("processSERVERATTR(%s,%s)", name.c_str(), value.c_str() ); // msdo.addServerAttribute(ip,name,value); }