Newer
Older
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
#include "catch.hpp"
#include <iostream>
#include <memory>
#include <map>
#include <ftl/net/protocol.hpp>
#include <ftl/net/socket.hpp>
using ftl::net::Socket;
// --- Mock --------------------------------------------------------------------
class MockSocket : public Socket {
public:
MockSocket() : Socket(0) {}
void mock_data() { data(); }
};
static std::string last_rpc;
void ftl::net::Protocol::dispatchRPC(Socket &s, const std::string &d) {
last_rpc = d;
// This should send a return value
}
void ftl::net::Protocol::dispatchRaw(uint32_t service, Socket &s) {
}
ftl::net::Protocol::Protocol(uint64_t id) {
}
ftl::net::Protocol::~Protocol() {
}
ftl::net::Protocol *ftl::net::Protocol::find(uint64_t p) {
return NULL;
}
// --- Support -----------------------------------------------------------------
static std::map<int, std::string> fakedata;
void fake_send(int sd, uint32_t service, const std::string &data) {
//std::cout << "HEX SEND: " << hexStr(data) << std::endl;
char buf[8+data.size()];
ftl::net::Header *h = (ftl::net::Header*)&buf;
h->size = data.size()+4;
h->service = service;
std::memcpy(&buf[8],data.data(),data.size());
fakedata[sd] = std::string(&buf[0], 8+data.size());
//std::cout << "HEX SEND2: " << hexStr(fakedata[sd]) << std::endl;
}
extern ssize_t recv(int sd, void *buf, size_t n, int f) {
if (fakedata.count(sd) == 0) {
std::cout << "Unrecognised socket" << std::endl;
return 0;
}
int l = fakedata[sd].size();
std::memcpy(buf, fakedata[sd].c_str(), l);
fakedata.erase(sd);
return l;
}
extern ssize_t writev(int sd, const struct iovec *v, int cnt) {
// TODO Use count incase more sources exist...
size_t len = v[0].iov_len+v[1].iov_len;
char buf[len];
std::memcpy(&buf[0],v[0].iov_base,v[0].iov_len);
std::memcpy(&buf[v[0].iov_len],v[1].iov_base,v[1].iov_len);
fakedata[sd] = std::string(&buf[0], len);
return 0;
}
static std::function<void()> waithandler;
namespace ftl {
namespace net {
bool wait() {
if (waithandler) waithandler();
//waithandler = nullptr;
return true;
}
};
};
// --- Files to test -----------------------------------------------------------
#include "../src/socket.cpp"
// --- Tests -------------------------------------------------------------------
using ftl::net::Protocol;
TEST_CASE("Socket::call()", "[rpc]") {
MockSocket s;
SECTION("no argument call") {
waithandler = [&]() {
// Read fakedata sent
// TODO Validate data
// Do a fake send
auto res_obj = std::make_tuple(1,0,msgpack::object(),66);
std::stringstream buf;
msgpack::pack(buf, res_obj);
fake_send(0, FTL_PROTOCOL_RPCRETURN, buf.str());
s.mock_data();
};
int res = s.call<int>("test1");
REQUIRE( res == 66 );
}
SECTION("one argument call") {
waithandler = [&]() {
// Read fakedata sent
// TODO Validate data
// Do a fake send
auto res_obj = std::make_tuple(1,1,msgpack::object(),43);
std::stringstream buf;
msgpack::pack(buf, res_obj);
fake_send(0, FTL_PROTOCOL_RPCRETURN, buf.str());
s.mock_data();
};
int res = s.call<int>("test1", 78);
REQUIRE( res == 43 );
}
waithandler = nullptr;
}
TEST_CASE("Socket receive RPC", "[rpc]") {
MockSocket s;
auto p = new Protocol(444);
s.setProtocol(p);
SECTION("no argument call") {
// Do a fake send
auto args_obj = std::make_tuple();
auto call_obj = std::make_tuple(0,0,"test1",args_obj);
std::stringstream buf;
msgpack::pack(buf, call_obj);
fake_send(0, FTL_PROTOCOL_RPC, buf.str());
s.mock_data(); // Force it to read the fake send...
REQUIRE( last_rpc == buf.str() );
}
SECTION("one argument call") {
// Do a fake send
auto args_obj = std::make_tuple(55);
auto call_obj = std::make_tuple(0,0,"test2",args_obj);
std::stringstream buf;
msgpack::pack(buf, call_obj);
fake_send(0, FTL_PROTOCOL_RPC, buf.str());
s.mock_data(); // Force it to read the fake send...
REQUIRE( last_rpc == buf.str() );
}
}
TEST_CASE("Socket::read()", "[io]") {
MockSocket s;
SECTION("read an int") {
int i = 99;
fake_send(0, 100, std::string((char*)&i,4));
i = 0;
s.mock_data(); // Force a message read, but no protocol...
REQUIRE( s.size() == sizeof(int) );
REQUIRE( s.read(i) == sizeof(int) );
REQUIRE( i == 99 );
}
SECTION("read two ints") {
int i[2];
i[0] = 99;
i[1] = 101;
fake_send(0, 100, std::string((char*)&i,2*sizeof(int)));
i[0] = 0;
i[1] = 0;
s.mock_data(); // Force a message read, but no protocol...
REQUIRE( s.size() == 2*sizeof(int) );
REQUIRE( s.read(&i,2) == 2*sizeof(int) );
REQUIRE( i[0] == 99 );
REQUIRE( i[1] == 101 );
}
SECTION("multiple reads") {
int i[2];
i[0] = 99;
i[1] = 101;
fake_send(0, 100, std::string((char*)&i,2*sizeof(int)));
i[0] = 0;
i[1] = 0;
s.mock_data(); // Force a message read, but no protocol...
REQUIRE( s.read(&i[0],1) == sizeof(int) );
REQUIRE( i[0] == 99 );
REQUIRE( s.read(&i[1],1) == sizeof(int) );
REQUIRE( i[1] == 101 );
}
SECTION("read a string") {
std::string str;
fake_send(0, 100, std::string("hello world"));
s.mock_data(); // Force a message read, but no protocol...
REQUIRE( s.size() == 11 );
REQUIRE( s.read(str) == 11 );
REQUIRE( str == "hello world" );
}
SECTION("read into existing string") {
std::string str;
str.reserve(11);
void *ptr = str.data();
fake_send(0, 100, std::string("hello world"));
s.mock_data(); // Force a message read, but no protocol...
REQUIRE( s.size() == 11 );
REQUIRE( s.read(str) == 11 );
REQUIRE( str == "hello world" );
REQUIRE( str.data() == ptr );
}
SECTION("read too much data") {
int i = 99;
fake_send(0, 100, std::string((char*)&i,4));
i = 0;
s.mock_data(); // Force a message read, but no protocol...
REQUIRE( s.size() == sizeof(int) );
REQUIRE( s.read(&i,2) == sizeof(int) );
REQUIRE( i == 99 );
}
}