File: MSEHandshakeTest.cc

package info (click to toggle)
aria2 1.37.0%2Bdebian-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 15,128 kB
  • sloc: cpp: 115,006; ansic: 9,140; makefile: 1,466; ruby: 475; python: 373; sh: 260; xml: 176
file content (182 lines) | stat: -rw-r--r-- 4,930 bytes parent folder | download | duplicates (6)
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#include "MSEHandshake.h"

#include <cstring>

#include <cppunit/extensions/HelperMacros.h>

#include "Exception.h"
#include "util.h"
#include "prefs.h"
#include "SocketCore.h"
#include "Option.h"
#include "DownloadContext.h"
#include "FileEntry.h"
#include "array_fun.h"
#include "bittorrent_helper.h"

namespace aria2 {

class MSEHandshakeTest : public CppUnit::TestFixture {

  CPPUNIT_TEST_SUITE(MSEHandshakeTest);
  CPPUNIT_TEST(testHandshake);
  CPPUNIT_TEST_SUITE_END();

private:
  std::shared_ptr<DownloadContext> dctx_;

  void doHandshake(const std::shared_ptr<MSEHandshake>& initiator,
                   const std::shared_ptr<MSEHandshake>& receiver);

public:
  void setUp()
  {
    dctx_.reset(new DownloadContext());
    unsigned char infoHash[20];
    memset(infoHash, 0, sizeof(infoHash));
    {
      auto torrentAttrs = make_unique<TorrentAttribute>();
      torrentAttrs->infoHash.assign(std::begin(infoHash), std::end(infoHash));
      dctx_->setAttribute(CTX_ATTR_BT, std::move(torrentAttrs));
    }
  }

  void testHandshake();
};

CPPUNIT_TEST_SUITE_REGISTRATION(MSEHandshakeTest);

namespace {
std::pair<std::shared_ptr<SocketCore>, std::shared_ptr<SocketCore>>
createSocketPair()
{
  std::shared_ptr<SocketCore> initiatorSock(new SocketCore());

  SocketCore receiverServerSock;
  receiverServerSock.bind(0);
  receiverServerSock.beginListen();
  receiverServerSock.setBlockingMode();

  auto endpoint = receiverServerSock.getAddrInfo();
  initiatorSock->establishConnection("localhost", endpoint.port);
  initiatorSock->setBlockingMode();

  std::shared_ptr<SocketCore> receiverSock(
      receiverServerSock.acceptConnection());
  receiverSock->setBlockingMode();

  return std::pair<std::shared_ptr<SocketCore>, std::shared_ptr<SocketCore>>(
      initiatorSock, receiverSock);
}
} // namespace

void MSEHandshakeTest::doHandshake(
    const std::shared_ptr<MSEHandshake>& initiator,
    const std::shared_ptr<MSEHandshake>& receiver)
{
  initiator->sendPublicKey();
  while (initiator->getWantWrite()) {
    initiator->send();
  }
  while (!receiver->receivePublicKey()) {
    receiver->read();
  }
  receiver->sendPublicKey();
  while (receiver->getWantWrite()) {
    receiver->send();
  }

  while (!initiator->receivePublicKey()) {
    initiator->read();
  }
  initiator->initCipher(bittorrent::getInfoHash(dctx_));
  initiator->sendInitiatorStep2();
  while (initiator->getWantWrite()) {
    initiator->send();
  }

  while (!receiver->findReceiverHashMarker()) {
    receiver->read();
  }
  std::vector<std::shared_ptr<DownloadContext>> contexts;
  contexts.push_back(dctx_);
  while (!receiver->receiveReceiverHashAndPadCLength(contexts)) {
    receiver->read();
  }
  while (!receiver->receivePad()) {
    receiver->read();
  }
  while (!receiver->receiveReceiverIALength()) {
    receiver->read();
  }
  while (!receiver->receiveReceiverIA()) {
    receiver->read();
  }
  receiver->sendReceiverStep2();
  while (receiver->getWantWrite()) {
    receiver->send();
  }

  while (!initiator->findInitiatorVCMarker()) {
    initiator->read();
  }
  while (!initiator->receiveInitiatorCryptoSelectAndPadDLength()) {
    initiator->read();
  }
  while (!initiator->receivePad()) {
    initiator->read();
  }
}

namespace {
std::shared_ptr<MSEHandshake>
createMSEHandshake(std::shared_ptr<SocketCore> socket, bool initiator,
                   const Option* option)
{
  std::shared_ptr<MSEHandshake> h(new MSEHandshake(1, socket, option));
  h->initEncryptionFacility(initiator);
  return h;
}
} // namespace

void MSEHandshakeTest::testHandshake()
{
  {
    Option op;
    op.put(PREF_BT_MIN_CRYPTO_LEVEL, V_PLAIN);

    std::pair<std::shared_ptr<SocketCore>, std::shared_ptr<SocketCore>>
        sockPair = createSocketPair();
    std::shared_ptr<MSEHandshake> initiator =
        createMSEHandshake(sockPair.first, true, &op);
    std::shared_ptr<MSEHandshake> receiver =
        createMSEHandshake(sockPair.second, false, &op);

    doHandshake(initiator, receiver);

    CPPUNIT_ASSERT_EQUAL(MSEHandshake::CRYPTO_PLAIN_TEXT,
                         initiator->getNegotiatedCryptoType());
    CPPUNIT_ASSERT_EQUAL(MSEHandshake::CRYPTO_PLAIN_TEXT,
                         receiver->getNegotiatedCryptoType());
  }
  {
    Option op;
    op.put(PREF_BT_MIN_CRYPTO_LEVEL, V_ARC4);

    std::pair<std::shared_ptr<SocketCore>, std::shared_ptr<SocketCore>>
        sockPair = createSocketPair();
    std::shared_ptr<MSEHandshake> initiator =
        createMSEHandshake(sockPair.first, true, &op);
    std::shared_ptr<MSEHandshake> receiver =
        createMSEHandshake(sockPair.second, false, &op);

    doHandshake(initiator, receiver);

    CPPUNIT_ASSERT_EQUAL(MSEHandshake::CRYPTO_ARC4,
                         initiator->getNegotiatedCryptoType());
    CPPUNIT_ASSERT_EQUAL(MSEHandshake::CRYPTO_ARC4,
                         receiver->getNegotiatedCryptoType());
  }
}

} // namespace aria2