DHTTokenTrackerTest.cc 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. #include "DHTTokenTracker.h"
  2. #include <cppunit/extensions/HelperMacros.h>
  3. #include "Exception.h"
  4. #include "util.h"
  5. #include "DHTConstants.h"
  6. namespace aria2 {
  7. class DHTTokenTrackerTest : public CppUnit::TestFixture {
  8. CPPUNIT_TEST_SUITE(DHTTokenTrackerTest);
  9. CPPUNIT_TEST(testGenerateToken);
  10. CPPUNIT_TEST_SUITE_END();
  11. public:
  12. void setUp() {}
  13. void tearDown() {}
  14. void testGenerateToken();
  15. };
  16. CPPUNIT_TEST_SUITE_REGISTRATION(DHTTokenTrackerTest);
  17. void DHTTokenTrackerTest::testGenerateToken()
  18. {
  19. unsigned char infohash[DHT_ID_LENGTH];
  20. util::generateRandomData(infohash, DHT_ID_LENGTH);
  21. std::string ipaddr = "192.168.0.1";
  22. uint16_t port = 6881;
  23. DHTTokenTracker tracker;
  24. std::string token = tracker.generateToken(infohash, ipaddr, port);
  25. CPPUNIT_ASSERT(tracker.validateToken(token, infohash, ipaddr, port));
  26. tracker.updateTokenSecret();
  27. CPPUNIT_ASSERT(tracker.validateToken(token, infohash, ipaddr, port));
  28. std::string newtoken = tracker.generateToken(infohash, ipaddr, port);
  29. tracker.updateTokenSecret();
  30. CPPUNIT_ASSERT(!tracker.validateToken(token, infohash, ipaddr, port));
  31. CPPUNIT_ASSERT(tracker.validateToken(newtoken, infohash, ipaddr, port));
  32. }
  33. } // namespace aria2