DHTTokenTrackerTest.cc 1.2 KB

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