forked from aria2/aria2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BtSuggestPieceMessageTest.cc
79 lines (65 loc) · 1.99 KB
/
BtSuggestPieceMessageTest.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include "BtSuggestPieceMessage.h"
#include <cstring>
#include <cppunit/extensions/HelperMacros.h>
#include "bittorrent_helper.h"
namespace aria2 {
class BtSuggestPieceMessageTest : public CppUnit::TestFixture {
CPPUNIT_TEST_SUITE(BtSuggestPieceMessageTest);
CPPUNIT_TEST(testCreate);
CPPUNIT_TEST(testCreateMessage);
CPPUNIT_TEST(testToString);
CPPUNIT_TEST_SUITE_END();
private:
public:
void setUp() {}
void testCreate();
void testCreateMessage();
void testToString();
};
CPPUNIT_TEST_SUITE_REGISTRATION(BtSuggestPieceMessageTest);
void BtSuggestPieceMessageTest::testCreate()
{
unsigned char msg[9];
bittorrent::createPeerMessageString(msg, sizeof(msg), 5, 13);
bittorrent::setIntParam(&msg[5], 12345);
auto pm = BtSuggestPieceMessage::create(&msg[4], 5);
CPPUNIT_ASSERT(BtSuggestPieceMessage::ID == pm->getId());
CPPUNIT_ASSERT_EQUAL((size_t)12345, pm->getIndex());
// case: payload size is wrong
try {
unsigned char msg[10];
bittorrent::createPeerMessageString(msg, sizeof(msg), 6, 13);
BtSuggestPieceMessage::create(&msg[4], 2);
CPPUNIT_FAIL("exception must be thrown.");
}
catch (...) {
}
// case: id is wrong
try {
unsigned char msg[9];
bittorrent::createPeerMessageString(msg, sizeof(msg), 5, 14);
BtSuggestPieceMessage::create(&msg[4], 1);
CPPUNIT_FAIL("exception must be thrown.");
}
catch (...) {
}
}
void BtSuggestPieceMessageTest::testCreateMessage()
{
BtSuggestPieceMessage msg;
msg.setIndex(12345);
unsigned char data[9];
bittorrent::createPeerMessageString(data, sizeof(data), 5, 13);
bittorrent::setIntParam(&data[5], 12345);
auto rawmsg = msg.createMessage();
CPPUNIT_ASSERT_EQUAL((size_t)9, rawmsg.size());
CPPUNIT_ASSERT(std::equal(std::begin(rawmsg), std::end(rawmsg), data));
}
void BtSuggestPieceMessageTest::testToString()
{
BtSuggestPieceMessage msg;
msg.setIndex(12345);
CPPUNIT_ASSERT_EQUAL(std::string("suggest piece index=12345"),
msg.toString());
}
} // namespace aria2