1 /*
2  * Copyright (C) 2009-2020 Codership Oy <info@codership.com>
3  */
4 
5 #include "gcomm/util.hpp"
6 #include "gcomm/protonet.hpp"
7 #include "gcomm/datagram.hpp"
8 #include "gcomm/conf.hpp"
9 
10 #include "check_gcomm.hpp"
11 
12 #include "gu_logger.hpp"
13 
14 #ifdef HAVE_ASIO_HPP
15 #include "asio_protonet.hpp"
16 #endif // HAVE_ASIO_HPP
17 
18 
19 #include <vector>
20 #include <fstream>
21 #include <limits>
22 #include <cstdlib>
23 #include <check.h>
24 
25 using std::vector;
26 using std::numeric_limits;
27 using std::string;
28 
29 using namespace gcomm;
30 using gu::Exception;
31 using gu::byte_t;
32 using gu::Buffer;
33 
34 #if defined(HAVE_ASIO_HPP)
START_TEST(test_asio)35 START_TEST(test_asio)
36 {
37     gu::Config conf;
38     gu::ssl_register_params(conf);
39     gcomm::Conf::register_params(conf);
40     AsioProtonet pn(conf);
41     string uri_str("tcp://127.0.0.1:0");
42 
43     auto acc(pn.acceptor(uri_str));
44     acc->listen(uri_str);
45     uri_str = acc->listen_addr();
46 
47     SocketPtr cl = pn.socket(uri_str);
48     cl->connect(uri_str);
49     pn.event_loop(gu::datetime::Sec);
50 
51     SocketPtr sr = acc->accept();
52     ck_assert(sr->state() == Socket::S_CONNECTED);
53 
54     vector<byte_t> buf(cl->mtu());
55     for (size_t i = 0; i < buf.size(); ++i)
56     {
57         buf[i] = static_cast<byte_t>(i & 0xff);
58     }
59 
60     for (size_t i = 0; i < 13; ++i)
61     {
62         Datagram dg(Buffer(&buf[0], &buf[0] + buf.size()));
63         cl->send(0, dg);
64     }
65     pn.event_loop(gu::datetime::Sec);
66 }
67 END_TEST
68 #endif // HAVE_ASIO_HPP
69 
START_TEST(test_protonet)70 START_TEST(test_protonet)
71 {
72     gu::Config conf;
73     gu::ssl_register_params(conf);
74     gcomm::Conf::register_params(conf);
75     mark_point();
76     Protonet* pn(Protonet::create(conf));
77     ck_assert(pn != NULL);
78     pn->event_loop(1);
79     mark_point();
80     delete pn;
81 }
82 END_TEST
83 
84 
util_nondet_suite()85 Suite* util_nondet_suite()
86 {
87     Suite* s = suite_create("util_nondet");
88     TCase* tc;
89 
90 #ifdef HAVE_ASIO_HPP
91     tc = tcase_create("test_asio");
92     tcase_add_test(tc, test_asio);
93     suite_add_tcase(s, tc);
94 #endif // HAVE_ASIO_HPP
95 
96     tc = tcase_create("test_protonet");
97     tcase_add_test(tc, test_protonet);
98     suite_add_tcase(s, tc);
99 
100 
101     return s;
102 }
103