summaryrefslogtreecommitdiff
path: root/test/fakesockettest.cpp
blob: 1b5573359a0c793a27c7d08a5daf44b47560f16d (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
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

#include <config.h>

#include <algorithm>
#include <condition_variable>
#include <mutex>
#include <thread>
#include <regex>
#include <vector>

#include <test/lokassert.hpp>

#include <cppunit/BriefTestProgressListener.h>
#include <cppunit/CompilerOutputter.h>
#include <cppunit/TestResult.h>
#include <cppunit/TestResultCollector.h>
#include <cppunit/TestRunner.h>
#include <cppunit/TextTestProgressListener.h>
#include <cppunit/extensions/HelperMacros.h>
#include <cppunit/extensions/TestFactoryRegistry.h>

#undef MOBILEAPP
#define MOBILEAPP 1 // A bit ugly, but currently FakeSocket.hpp is surrounded by a MOBILEAPP ifdef,
                    // and probably it is not a good idea to remove that?
#include "FakeSocket.hpp"

class FakeSocketTest : public CPPUNIT_NS::TestFixture
{
    CPPUNIT_TEST_SUITE(FakeSocketTest);

    CPPUNIT_TEST(testBasic);

    CPPUNIT_TEST_SUITE_END();

    void testBasic();

public:
    FakeSocketTest()
    {
    }

    void setUp()
    {
        fakeSocketSetLoggingCallback([](const std::string& line)
                                     {
                                         std::cerr << line << "\n";
                                     });
    }

    void tearDown()
    {
    }
};

void FakeSocketTest::testBasic()
{
    int rc;
    char buf[100];

    // First check invalid fds.

    rc = fakeSocketListen(10);
    LOK_ASSERT(rc == -1);
    LOK_ASSERT(errno == EBADF);

    rc = fakeSocketWrite(20, "hah", 3);
    LOK_ASSERT(rc == -1);
    LOK_ASSERT(errno == EBADF);

    rc = fakeSocketRead(30, buf, 3);
    LOK_ASSERT(rc == -1);
    LOK_ASSERT(errno == EBADF);

    // Create three sockets: s0, s1 and s2.
    int s0 = fakeSocketSocket();
    LOK_ASSERT(s0 >= 0);
    int s1 = fakeSocketSocket();
    LOK_ASSERT(s1 >= 0);
    int s2 = fakeSocketSocket();
    LOK_ASSERT(s2 >= 0);

    LOK_ASSERT(s0 != s1);
    LOK_ASSERT(s1 != s2);

    // Close s1 and create it anew
    fakeSocketClose(s1);

    s1 = fakeSocketSocket();
    LOK_ASSERT(s1 >= 0);

    // Listen on s0
    rc = fakeSocketListen(s0);
    LOK_ASSERT(rc != -1);

    // Start a thread that accepts two connections to s0, producing sockets s3 and s4.
    int s3 = -1, s4 = -1;
    std::thread t0([&] {
            // Cannot use LOK_ASSERT here as that throws and this thread has no Cppunit
            // exception handler. We check below after joining this thread.
            s3 = fakeSocketAccept4(s0);
            s4 = fakeSocketAccept4(s0);
        });

    // Connect s1 and s2 to s0 (that is, to the sockets produced by accepting connections to
    // s0).
    rc = fakeSocketConnect(s1, s0);
    LOK_ASSERT(rc != -1);

    rc = fakeSocketConnect(s2, s0);
    LOK_ASSERT(rc != -1);

    // Verify that we got the accepts.
    t0.join();
    LOK_ASSERT(s3 != -1);
    LOK_ASSERT(s4 != -1);

    // s1 should now be connected to s3, and s2 to s4.
    LOK_ASSERT(fakeSocketPeer(s1) == s3);
    LOK_ASSERT(fakeSocketPeer(s3) == s1);
    LOK_ASSERT(fakeSocketPeer(s2) == s4);
    LOK_ASSERT(fakeSocketPeer(s4) == s2);

    // Some writing and reading
    rc = fakeSocketWrite(s1, "hello", 5);
    LOK_ASSERT(rc != -1);

    rc = fakeSocketWrite(s1, "greetings", 9);
    LOK_ASSERT(rc != -1);

    rc = fakeSocketWrite(s2, "moin", 4);
    LOK_ASSERT(rc != -1);

    rc = fakeSocketAvailableDataLength(s3);
    LOK_ASSERT(rc == 5);

    rc = fakeSocketRead(s3, buf, 100);
    LOK_ASSERT(rc == 5);

    rc = fakeSocketAvailableDataLength(s3);
    LOK_ASSERT(rc == 9);

    rc = fakeSocketRead(s4, buf, 100);
    LOK_ASSERT(rc == 4);

    rc = fakeSocketWrite(s3, "goodbye", 7);
    LOK_ASSERT(rc > 0);

    rc = fakeSocketRead(s1, buf, 4);
    LOK_ASSERT(rc == -1);
    LOK_ASSERT(errno == EAGAIN); // Note: not really the right errno, but what else? See
                                     // FakeSocket.cpp.

    rc = fakeSocketRead(s1, buf, 100);
    LOK_ASSERT(rc > 0);

    // Close s3. Reading from s1 should then return an EOF indication (0).
    fakeSocketClose(s3);

    rc = fakeSocketAvailableDataLength(s1);
    LOK_ASSERT(rc == 0);

    rc = fakeSocketRead(s1, buf, 100);
    LOK_ASSERT(rc == 0);

    rc = fakeSocketAvailableDataLength(s1);
    LOK_ASSERT(rc == 0);

    rc = fakeSocketRead(s1, buf, 100);
    LOK_ASSERT(rc == 0);

    // Test the "pipe" functionality, that creates an already connected socket pair.
    int pipe[2];
    rc = fakeSocketPipe2(pipe);
    LOK_ASSERT(rc == 0);

    rc = fakeSocketWrite(pipe[0], "x", 1);
    LOK_ASSERT(rc == 1);

    rc = fakeSocketAvailableDataLength(pipe[1]);
    LOK_ASSERT(rc == 1);

    rc = fakeSocketRead(pipe[1], buf, 1);
    LOK_ASSERT(rc == 1);

    LOK_ASSERT(buf[0] == 'x');

    rc = fakeSocketWrite(pipe[1], "y", 1);
    LOK_ASSERT(rc == 1);

    rc = fakeSocketRead(pipe[0], buf, 1);
    LOK_ASSERT(rc == 1);
    LOK_ASSERT(buf[0] == 'y');

    rc = fakeSocketWrite(pipe[0], "z", 1);
    LOK_ASSERT(rc == 1);

    rc = fakeSocketShutdown(pipe[0]);
    LOK_ASSERT(rc == 0);

    rc = fakeSocketRead(pipe[1], buf, 1);
    LOK_ASSERT(rc == 1);
    LOK_ASSERT(buf[0] == 'z');

    rc = fakeSocketWrite(pipe[0], "a", 1);
    LOK_ASSERT(rc == -1);
    LOK_ASSERT(errno == EPIPE);

    rc = fakeSocketRead(pipe[0], buf, 1);
    LOK_ASSERT(rc == 0);

    rc = fakeSocketRead(pipe[0], buf, 1);
    LOK_ASSERT(rc == 0);

    rc = fakeSocketClose(pipe[0]);
    LOK_ASSERT(rc == 0);

    rc = fakeSocketClose(pipe[0]);
    LOK_ASSERT(rc == -1);
    LOK_ASSERT(errno == EBADF);

    rc = fakeSocketClose(pipe[1]);
    LOK_ASSERT(rc == 0);

    rc = fakeSocketClose(pipe[1]);
    LOK_ASSERT(rc == -1);
    LOK_ASSERT(errno == EBADF);

    // Create a pipe again.

    rc = fakeSocketPipe2(pipe);
    LOK_ASSERT(rc == 0);

    rc = fakeSocketAvailableDataLength(pipe[0]);
    LOK_ASSERT(rc == -1);
    LOK_ASSERT(errno == EAGAIN);

    rc = fakeSocketAvailableDataLength(pipe[1]);
    LOK_ASSERT(rc == -1);
    LOK_ASSERT(errno == EAGAIN);

    // Test poll functionality.

    struct pollfd pollfds[4];

    pollfds[0].fd = s0;
    pollfds[0].events = POLLIN | POLLOUT;
    pollfds[1].fd = s1;
    pollfds[1].events = POLLIN | POLLOUT;
    pollfds[2].fd = s2;
    pollfds[2].events = POLLIN | POLLOUT;
    pollfds[3].fd = 40;
    pollfds[3].events = POLLIN | POLLOUT;

    rc = fakeSocketPoll(pollfds, 4, -1);
    // Hmm, does a real poll() set POLLIN for a listening socket? Probably only if there is a
    // connection in progress, and that is not the case here for s0.
    LOK_ASSERT(rc == 3);
    LOK_ASSERT(pollfds[0].revents == 0);
    LOK_ASSERT(pollfds[1].revents == POLLIN);
    LOK_ASSERT(pollfds[2].revents == POLLOUT);
    LOK_ASSERT(pollfds[3].revents == POLLNVAL);
}

CPPUNIT_TEST_SUITE_REGISTRATION(FakeSocketTest);

int main(int, char**)
{
    const char* envar = std::getenv("CPPUNIT_TEST_NAME");
    std::string testName;
    if (envar)
    {
        testName = std::string(envar);
    }
    if (!testName.empty() && testName != "FakeSocketTest")
    {
        return 0;
    }

    CPPUNIT_NS::TestResult controller;
    CPPUNIT_NS::TestResultCollector result;
    controller.addListener(&result);
    CPPUNIT_NS::BriefTestProgressListener progress;
    controller.addListener(&progress);
    CPPUNIT_NS::TextTestProgressListener listener;
    controller.addListener(&listener);

    CPPUNIT_NS::Test* testRegistry = CPPUNIT_NS::TestFactoryRegistry::getRegistry().makeTest();

    CPPUNIT_NS::TestRunner runner;
    runner.addTest(testRegistry);
    runner.run(controller);

    CPPUNIT_NS::CompilerOutputter outputter(&result, std::cerr);
    outputter.setNoWrap();
    outputter.write();

    fakeSocketDumpState();

    return result.wasSuccessful() ? 0 : 1;
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */