DHTTokenTrackerTest.cc 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  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. 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. DHTUtil::generateRandomData(reinterpret_cast<char*>(infohash), DHT_ID_LENGTH);
  21. string ipaddr = "192.168.0.1";
  22. uint16_t port = 6881;
  23. DHTTokenTracker tracker;
  24. 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. 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. }