aria2/test/ChokeMessageTest.cc

56 lines
1.3 KiB
C++

#include "ChokeMessage.h"
#include "PeerMessageUtil.h"
#include <cppunit/extensions/HelperMacros.h>
using namespace std;
class ChokeMessageTest:public CppUnit::TestFixture {
CPPUNIT_TEST_SUITE(ChokeMessageTest);
CPPUNIT_TEST(testCreate);
CPPUNIT_TEST(testGetMessage);
CPPUNIT_TEST_SUITE_END();
private:
public:
void setUp() {
}
void testCreate();
void testGetMessage();
};
CPPUNIT_TEST_SUITE_REGISTRATION(ChokeMessageTest);
void ChokeMessageTest::testCreate() {
char msg[5];
PeerMessageUtil::createPeerMessageString(msg, sizeof(msg), 1, 0);
PeerMessage* pm = ChokeMessage::create(&msg[4], 1);
CPPUNIT_ASSERT_EQUAL(0, pm->getId());
// case: payload size is wrong
try {
char msg[6];
PeerMessageUtil::createPeerMessageString(msg, sizeof(msg), 2, 0);
ChokeMessage::create(&msg[4], 2);
CPPUNIT_FAIL("exception must be threw.");
} catch(...) {
}
// case: id is wrong
try {
char msg[5];
PeerMessageUtil::createPeerMessageString(msg, sizeof(msg), 1, 1);
ChokeMessage::create(&msg[4], 1);
CPPUNIT_FAIL("exception must be threw.");
} catch(...) {
}
}
void ChokeMessageTest::testGetMessage() {
ChokeMessage msg;
char data[5];
PeerMessageUtil::createPeerMessageString(data, sizeof(data), 1, 0);
CPPUNIT_ASSERT(memcmp(msg.getMessage(), data, 5) == 0);
}