aboutsummaryrefslogtreecommitdiff
path: root/tests/main.cpp
blob: 61f95f499d42a74689486ef8dfe8f11bf9c7fb21 (plain)
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
#include "../include/sibs/BootstrapConnection.hpp"
#include "../include/sibs/BootstrapNode.hpp"
#include <thread>
#include <stdio.h>
#include <string.h>

const int PORT = 35231;

#define REQUIRE(expr) do { if(!(expr)) { fprintf(stderr, "Assert failed: %s\n", #expr); exit(1); } }while(0)

void testP2p()
{
    const sibs::PubsubKey key("abcdefghijklmnopqrstuvxyz0123456789", 35);
    const sibs::PubsubKey key2("zbcdefghcjklmn3pqrs5uvx2z0123F56789", 35);
    sibs::BootstrapNode boostrapNode(sibs::Ipv4(nullptr, PORT));

    std::unique_ptr<sibs::BootstrapConnection> connection1 = sibs::BootstrapConnection::connect(sibs::Ipv4("127.0.0.1", PORT)).get();
    bool gotData1 = false;
    bool gotAsdf1 = false;
    connection1->listen(key, [&gotData1, &gotAsdf1](const sibs::DirectConnectionPeer *peer, const void *data, const sibs::usize size)
    {
        if(size == 5 && strncmp((const char*)data, "hello", 5) == 0)
            gotData1 = true;
        if(size == 4 && strncmp((const char*)data, "asdf", 4) == 0)
            gotAsdf1 = true;
        return true;
    });

    std::unique_ptr<sibs::BootstrapConnection> connection2 = sibs::BootstrapConnection::connect(sibs::Ipv4("127.0.0.1", PORT)).get();
    bool gotData2 = false;
    bool gotAsdf2 = false;
    connection2->listen(key, [&gotData2, &gotAsdf2](const sibs::DirectConnectionPeer *peer, const void *data, const sibs::usize size)
    {
        if(size == 5 && strncmp((const char*)data, "hello", 5) == 0)
            gotData2 = true;
        if(size == 4 && strncmp((const char*)data, "asdf", 4) == 0)
            gotAsdf2 = true;
        return true;
    });

    bool gotListen = false;
    connection1->listen(key2, [&gotListen](const sibs::DirectConnectionPeer *peer, const void *data, const sibs::usize size)
    {
        if(size == 14 && strncmp((const char*)data, "secondListener", 14) == 0)
            gotListen = true;
        return true;
    });

    bool gotListen2 = false;
    connection2->listen(key2, [&gotListen2](const sibs::DirectConnectionPeer *peer, const void *data, const sibs::usize size)
    {
        if(size == 14 && strncmp((const char*)data, "secondListener", 14) == 0)
            gotListen2 = true;
        return true;
    });
    
    connection1->put(key, "hello", 5);
    connection2->put(key, "asdf", 4);
    connection1->put(key2, "secondListener", 14);
    std::this_thread::sleep_for(std::chrono::seconds(6));

    REQUIRE(gotData1);
    REQUIRE(gotAsdf1);
    REQUIRE(gotData2);
    REQUIRE(gotAsdf2);

    REQUIRE(gotListen);
    REQUIRE(gotListen2);
}

void testRouted()
{
    sibs::ConnectionOptions options;
    options.useP2p = false;

    const sibs::PubsubKey key("abcdefghijklmnopqrstuvxyz0123456789", 35);
    const sibs::PubsubKey key2("zbcdefghcjklmn3pqrs5uvx2z0123F56789", 35);
    sibs::BootstrapNode boostrapNode(sibs::Ipv4(nullptr, PORT));

    std::unique_ptr<sibs::BootstrapConnection> connection1 = sibs::BootstrapConnection::connect(sibs::Ipv4("127.0.0.1", PORT), options).get();
    bool gotData1 = false;
    bool gotAsdf1 = false;
    connection1->listen(key, [&gotData1, &gotAsdf1](const sibs::DirectConnectionPeer *peer, const void *data, const sibs::usize size)
    {
        if(size == 5 && strncmp((const char*)data, "hello", 5) == 0)
            gotData1 = true;
        if(size == 4 && strncmp((const char*)data, "asdf", 4) == 0)
            gotAsdf1 = true;
        return true;
    });

    std::unique_ptr<sibs::BootstrapConnection> connection2 = sibs::BootstrapConnection::connect(sibs::Ipv4("127.0.0.1", PORT), options).get();
    bool gotData2 = false;
    bool gotAsdf2 = false;
    connection2->listen(key, [&gotData2, &gotAsdf2](const sibs::DirectConnectionPeer *peer, const void *data, const sibs::usize size)
    {
        if(size == 5 && strncmp((const char*)data, "hello", 5) == 0)
            gotData2 = true;
        if(size == 4 && strncmp((const char*)data, "asdf", 4) == 0)
            gotAsdf2 = true;
        return true;
    });

    bool gotListen = false;
    connection1->listen(key2, [&gotListen](const sibs::DirectConnectionPeer *peer, const void *data, const sibs::usize size)
    {
        if(size == 14 && strncmp((const char*)data, "secondListener", 14) == 0)
            gotListen = true;
        return true;
    });

    bool gotListen2 = false;
    connection2->listen(key2, [&gotListen2](const sibs::DirectConnectionPeer *peer, const void *data, const sibs::usize size)
    {
        if(size == 14 && strncmp((const char*)data, "secondListener", 14) == 0)
            gotListen2 = true;
        return true;
    });
    
    connection1->put(key, "hello", 5);
    connection2->put(key, "asdf", 4);
    connection1->put(key2, "secondListener", 14);
    std::this_thread::sleep_for(std::chrono::seconds(6));

    REQUIRE(gotData1);
    REQUIRE(gotAsdf1);
    REQUIRE(gotData2);
    REQUIRE(gotAsdf2);

    REQUIRE(gotListen);
    REQUIRE(gotListen2);
}

int main()
{
    testP2p();
    testRouted();
    return 0;
}