summaryrefslogtreecommitdiff
path: root/indra/llcorehttp/tests/jsonrpcws_test.cpp
blob: c07cb6f4fd28a7bdd9932003649196fb0510ef9b (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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
/**
 * @file jsonrpcws_test.cpp
 * @brief Security-focused unit tests for LLJSONRPCConnection / LLJSONRPCServer
 *
 * $LicenseInfo:firstyear=2026&license=viewerlgpl$
 * Second Life Viewer Source Code
 * Copyright (C) 2026, Linden Research, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation;
 * version 2.1 of the License only.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
 * $/LicenseInfo$
 */

#include "linden_common.h"

#include "lljsonrpcws.h"
#include "llsdjson.h"

#include "../test/lltut.h"

#include <boost/json.hpp>
#include <vector>

namespace
{
    // Exposes the protected test-only members LLJSONRPCConnection provides
    // for exactly this purpose, on a connection with no real transport.
    class TestJSONRPCConnection : public LLJSONRPCConnection
    {
    public:
        TestJSONRPCConnection()
            : LLJSONRPCConnection(LLWebsocketMgr::WSServer::ptr_t(), LLWebsocketMgr::connection_h())
        {
        }

        using LLJSONRPCConnection::processMessage;
        using LLJSONRPCConnection::validateMessage;
        using LLJSONRPCConnection::testInjectPendingRequest;
        using LLJSONRPCConnection::testPendingRequestCount;
        using LLJSONRPCConnection::testSweepTimeouts;
        using LLJSONRPCConnection::setAuthenticated;
    };

    // Minimal concrete server, used only to register the built-in
    // system.* methods on a connection via setupConnectionMethods() --
    // LLJSONRPCServer itself is abstract (handleGetVersion is pure virtual).
    // Never started, so port 0 is fine.
    class TestJSONRPCServer : public LLJSONRPCServer
    {
    public:
        TestJSONRPCServer() : LLJSONRPCServer("test_jsonrpcws_server", 0, true) {}

        using LLJSONRPCServer::setupConnectionMethods;

        LLSD handleGetVersion(const LLJSONRPCConnection::ptr_t&, const LLSD&) const override
        {
            return LLSD();
        }
    };

    // Captures what would be sent to the peer, since there is no real
    // transport to observe it on.
    class CapturingJSONRPCConnection : public TestJSONRPCConnection
    {
    public:
        mutable std::vector<std::string> mSent;

        bool sendMessage(const std::string& message) const override
        {
            mSent.push_back(message);
            return true;
        }
    };
}

namespace tut
{
    struct jsonrpcws_data
    {
    };

    typedef test_group<jsonrpcws_data> jsonrpcws_test;
    typedef jsonrpcws_test::object jsonrpcws_object;
    jsonrpcws_test jsonrpcws_testcase("LLJSONRPCConnection");

    template<> template<>
    void jsonrpcws_object::test<1>()
    {
        set_test_name("an unauthenticated connection dispatches neither requests nor notifications");

        TestJSONRPCConnection conn;
        conn.setAuthenticated(false);

        int calls = 0;
        conn.registerMethod("object.list",
            [&](const std::string&, const LLSD&, const LLSD&) -> LLSD
            {
                ++calls;
                return LLSD();
            });

        const LLSD request = LLJSONRPCConnection::makeEnvelope(LLSD("req_1"), "object.list", LLSD::emptyMap(), LLSD(), LLSD());
        conn.processMessage(request);

        const LLSD notification = LLJSONRPCConnection::makeEnvelope(LLSD(), "object.list", LLSD::emptyMap(), LLSD(), LLSD());
        conn.processMessage(notification);

        ensure_equals("neither a request nor a notification reaches the handler", calls, 0);
    }

    template<> template<>
    void jsonrpcws_object::test<2>()
    {
        set_test_name("once authenticated, requests and notifications dispatch normally");

        TestJSONRPCConnection conn;
        conn.setAuthenticated(true);
        ensure("authenticated after the peer proves itself", conn.isAuthenticated());

        int calls = 0;
        conn.registerMethod("object.list",
            [&](const std::string&, const LLSD&, const LLSD&) -> LLSD
            {
                ++calls;
                return LLSD();
            });

        const LLSD request = LLJSONRPCConnection::makeEnvelope(LLSD("req_1"), "object.list", LLSD::emptyMap(), LLSD(), LLSD());
        conn.processMessage(request);
        ensure_equals("the request reaches the handler", calls, 1);

        const LLSD notification = LLJSONRPCConnection::makeEnvelope(LLSD(), "object.list", LLSD::emptyMap(), LLSD(), LLSD());
        conn.processMessage(notification);
        ensure_equals("the notification also reaches the handler", calls, 2);
    }

    template<> template<>
    void jsonrpcws_object::test<3>()
    {
        set_test_name("an unauthenticated connection still receives the answers to its own calls");

        TestJSONRPCConnection conn;
        ensure("unauthenticated by default", !conn.isAuthenticated());

        LLSD answer;
        conn.testInjectPendingRequest(
            "rpc_1",
            LLTimer::getTotalSeconds() + 60.0,
            [&](const LLSD& result, const LLSD& error)
            {
                ensure("no error", error.isUndefined());
                answer = result;
            });

        LLSD result;
        result["challenge_response"] = "secret";
        conn.processMessage(LLJSONRPCConnection::makeEnvelope(LLSD("rpc_1"), std::string(), LLSD(), result, LLSD()));
        ensure_equals("the answer reached the caller", answer["challenge_response"].asString(), std::string("secret"));
        ensure("and it is answered", conn.testPendingRequestCount() == 0);
    }

    template<> template<>
    void jsonrpcws_object::test<4>()
    {
        set_test_name("an unauthenticated request is answered -32002; a notification gets nothing back");

        CapturingJSONRPCConnection conn;
        ensure("unauthenticated by default", !conn.isAuthenticated());

        const LLSD request = LLJSONRPCConnection::makeEnvelope(LLSD("req_1"), "object.list", LLSD::emptyMap(), LLSD(), LLSD());
        conn.processMessage(request);
        ensure_equals("exactly one message sent for the request", conn.mSent.size(), (size_t)1);

        boost::system::error_code ec;
        const LLSD sent = LlsdFromJson(boost::json::parse(conn.mSent[0], ec));
        ensure("parsed without error", !ec);
        ensure_equals("unauthorized error code", sent["error"]["code"].asInteger(), LLJSONRPCConnection::RPCError::UNAUTHORIZED);

        const LLSD notification = LLJSONRPCConnection::makeEnvelope(LLSD(), "object.list", LLSD::emptyMap(), LLSD(), LLSD());
        conn.processMessage(notification);
        ensure_equals("nothing sent for a notification", conn.mSent.size(), (size_t)1);
    }

    template<> template<>
    void jsonrpcws_object::test<5>()
    {
        set_test_name("malformed JSON is answered with a parse error, and the connection survives");

        CapturingJSONRPCConnection conn;
        conn.setAuthenticated(true);

        conn.onMessage("{not valid json");
        ensure_equals("a parse error is sent", conn.mSent.size(), (size_t)1);

        boost::system::error_code ec;
        const LLSD sent = LlsdFromJson(boost::json::parse(conn.mSent[0], ec));
        ensure("parsed without error", !ec);
        ensure_equals("parse error code", sent["error"]["code"].asInteger(), LLJSONRPCConnection::RPCError::PARSE_ERROR);

        // The connection itself is unaffected: valid follow-up traffic still works.
        int calls = 0;
        conn.registerMethod("object.list",
            [&](const std::string&, const LLSD&, const LLSD&) -> LLSD
            {
                ++calls;
                return LLSD();
            });
        conn.onMessage(boost::json::serialize(LlsdToJson(LLJSONRPCConnection::makeEnvelope(LLSD("req_1"), "object.list", LLSD::emptyMap(), LLSD(), LLSD()))));
        ensure_equals("a later valid request still dispatches", calls, 1);
    }

    template<> template<>
    void jsonrpcws_object::test<6>()
    {
        set_test_name("a batch request is rejected outright; batch support is intentionally disabled");

        CapturingJSONRPCConnection conn;
        conn.setAuthenticated(true);

        const LLSD one = LLJSONRPCConnection::makeEnvelope(LLSD("req_1"), "object.list", LLSD::emptyMap(), LLSD(), LLSD());
        LLSD batch = LLSD::emptyArray();
        batch.append(one);
        batch.append(one);

        conn.onMessage(boost::json::serialize(LlsdToJson(batch)));
        ensure_equals("a single error is sent for the whole batch", conn.mSent.size(), (size_t)1);

        boost::system::error_code ec;
        const LLSD sent = LlsdFromJson(boost::json::parse(conn.mSent[0], ec));
        ensure("parsed without error", !ec);
        ensure_equals("invalid request error code", sent["error"]["code"].asInteger(), LLJSONRPCConnection::RPCError::INVALID_REQUEST);
    }

    template<> template<>
    void jsonrpcws_object::test<7>()
    {
        set_test_name("a missing or wrong jsonrpc version is rejected by validateMessage");

        TestJSONRPCConnection conn;

        LLSD missing_version;
        missing_version["method"] = "object.list";
        ensure("missing version fails validation", !conn.validateMessage(missing_version, true));

        LLSD wrong_version = missing_version;
        wrong_version["jsonrpc"] = "1.0";
        ensure("wrong version fails validation", !conn.validateMessage(wrong_version, true));

        LLSD correct_version = missing_version;
        correct_version["jsonrpc"] = "2.0";
        ensure("correct version passes validation", conn.validateMessage(correct_version, true));
    }

    template<> template<>
    void jsonrpcws_object::test<8>()
    {
        set_test_name("params of the wrong type are rejected; the handler is never invoked");

        TestJSONRPCConnection conn;
        conn.setAuthenticated(true);

        LLSD bad_params;
        bad_params["jsonrpc"] = "2.0";
        bad_params["method"] = "object.list";
        bad_params["params"] = "not-an-array-or-object";
        ensure("string params fail validation", !conn.validateMessage(bad_params, true));

        int calls = 0;
        conn.registerMethod("object.list",
            [&](const std::string&, const LLSD&, const LLSD&) -> LLSD
            {
                ++calls;
                return LLSD();
            });

        conn.processMessage(bad_params);
        ensure_equals("the handler is never invoked", calls, 0);

        LLSD good_params = bad_params;
        good_params["params"] = LLSD::emptyMap();
        conn.processMessage(good_params);
        ensure_equals("valid params do dispatch", calls, 1);
    }

    template<> template<>
    void jsonrpcws_object::test<9>()
    {
        set_test_name("a response for an unknown id is silently ignored");

        TestJSONRPCConnection conn;

        bool callback_called = false;
        conn.testInjectPendingRequest(
            "req_1",
            LLTimer::getTotalSeconds() + 60.0,
            [&](const LLSD&, const LLSD&)
            {
                callback_called = true;
            });

        LLSD result;
        result["value"] = "ok";
        conn.processMessage(LLJSONRPCConnection::makeEnvelope(LLSD("unknown_id"), std::string(), LLSD(), result, LLSD()));

        ensure("no callback fired for an unrelated id", !callback_called);
        ensure_equals("the real pending request is still tracked", conn.testPendingRequestCount(), (size_t)1);

        conn.processMessage(LLJSONRPCConnection::makeEnvelope(LLSD("req_1"), std::string(), LLSD(), result, LLSD()));
        ensure("the callback fires for its own id", callback_called);
        ensure_equals("and it is removed once answered", conn.testPendingRequestCount(), (size_t)0);
    }

    template<> template<>
    void jsonrpcws_object::test<10>()
    {
        set_test_name("a response with both result and error, or with neither, is rejected as invalid");

        TestJSONRPCConnection conn;

        LLSD both;
        both["jsonrpc"] = "2.0";
        both["id"] = "req_1";
        both["result"] = "ok";
        both["error"]["code"] = -1;
        both["error"]["message"] = "bad";
        ensure("result and error together fail validation", !conn.validateMessage(both, false));

        LLSD neither;
        neither["jsonrpc"] = "2.0";
        neither["id"] = "req_1";
        ensure("neither result nor error fails validation", !conn.validateMessage(neither, false));

        LLSD result_only;
        result_only["jsonrpc"] = "2.0";
        result_only["id"] = "req_1";
        result_only["result"] = "ok";
        ensure("result alone passes validation", conn.validateMessage(result_only, false));

        bool callback_called = false;
        conn.testInjectPendingRequest(
            "req_1",
            LLTimer::getTotalSeconds() + 60.0,
            [&](const LLSD&, const LLSD&)
            {
                callback_called = true;
            });

        conn.processMessage(both);
        ensure("an invalid response never reaches the callback", !callback_called);
        ensure_equals("the pending request is still tracked", conn.testPendingRequestCount(), (size_t)1);
    }

    template<> template<>
    void jsonrpcws_object::test<11>()
    {
        set_test_name("an unauthenticated peer is told unauthorized, never that the method doesn't exist");

        CapturingJSONRPCConnection conn;

        const LLSD request = LLJSONRPCConnection::makeEnvelope(LLSD("req_1"), "no.such.method", LLSD::emptyMap(), LLSD(), LLSD());
        conn.processMessage(request);
        ensure_equals("one message sent", conn.mSent.size(), (size_t)1);

        boost::system::error_code ec;
        LLSD sent = LlsdFromJson(boost::json::parse(conn.mSent[0], ec));
        ensure("parsed without error", !ec);
        ensure_equals("unauthorized, not method-not-found", sent["error"]["code"].asInteger(), LLJSONRPCConnection::RPCError::UNAUTHORIZED);

        conn.setAuthenticated(true);
        conn.processMessage(request);
        ensure_equals("a second message sent once authenticated", conn.mSent.size(), (size_t)2);

        sent = LlsdFromJson(boost::json::parse(conn.mSent[1], ec));
        ensure("parsed without error", !ec);
        ensure_equals("now it is method-not-found", sent["error"]["code"].asInteger(), LLJSONRPCConnection::RPCError::METHOD_NOT_FOUND);
    }

    template<> template<>
    void jsonrpcws_object::test<12>()
    {
        set_test_name("unregisterMethod removes both sync and async handlers");

        TestJSONRPCConnection conn;

        conn.registerMethod("sync.method",
            [](const std::string&, const LLSD&, const LLSD&) -> LLSD
            {
                return LLSD();
            });
        conn.registerAsyncMethod("async.method",
            [](const std::string&, const LLSD&, const LLSD&) -> LLSD
            {
                return LLSD();
            });

        ensure_equals("sync method registered", conn.getMethods().count("sync.method"), (size_t)1);
        ensure_equals("async method registered", conn.getMethods().count("async.method"), (size_t)1);

        conn.unregisterMethod("sync.method");
        conn.unregisterMethod("async.method");

        ensure_equals("sync method gone", conn.getMethods().count("sync.method"), (size_t)0);
        ensure_equals("async method gone", conn.getMethods().count("async.method"), (size_t)0);
    }

    template<> template<>
    void jsonrpcws_object::test<13>()
    {
        set_test_name("a handler throwing a plain std::exception is converted to a generic internal error");

        CapturingJSONRPCConnection conn;
        conn.setAuthenticated(true);

        conn.registerMethod("boom",
            [](const std::string&, const LLSD&, const LLSD&) -> LLSD
            {
                throw std::runtime_error("boom detail");
            });

        conn.processMessage(LLJSONRPCConnection::makeEnvelope(LLSD("req_1"), "boom", LLSD::emptyMap(), LLSD(), LLSD()));
        ensure_equals("one message sent", conn.mSent.size(), (size_t)1);

        boost::system::error_code ec;
        const LLSD sent = LlsdFromJson(boost::json::parse(conn.mSent[0], ec));
        ensure("parsed without error", !ec);
        ensure_equals("internal error code", sent["error"]["code"].asInteger(), LLJSONRPCConnection::RPCError::INTERNAL_ERROR);

        // The exception's own message must never reach the client -- it can
        // carry paths, connection strings, or other internal detail.
        ensure("the exception's own message is not forwarded to the client",
               sent["error"]["message"].asString().find("boom detail") == std::string::npos);
    }

    template<> template<>
    void jsonrpcws_object::test<14>()
    {
        set_test_name("call() with no way to send returns undefined and leaves nothing pending");

        TestJSONRPCConnection conn;

        bool callback_called = false;
        const LLSD id = conn.call("object.list", LLSD::emptyMap(),
            [&](const LLSD&, const LLSD&)
            {
                callback_called = true;
            });

        ensure("call() reports it could not send", id.isUndefined());
        ensure_equals("nothing left pending", conn.testPendingRequestCount(), (size_t)0);
        ensure("the callback is never invoked for an unsent call", !callback_called);
    }

    template<> template<>
    void jsonrpcws_object::test<15>()
    {
        set_test_name("the built-in system.* methods are gated by authentication like any other method");

        TestJSONRPCServer server;
        auto conn = std::make_shared<CapturingJSONRPCConnection>();
        server.setupConnectionMethods(conn);

        ensure_equals("system.listMethods is registered", conn->getMethods().count("system.listMethods"), (size_t)1);
        ensure_equals("system.getStats is registered", conn->getMethods().count("system.getStats"), (size_t)1);

        conn->processMessage(LLJSONRPCConnection::makeEnvelope(LLSD("req_1"), "system.listMethods", LLSD::emptyMap(), LLSD(), LLSD()));
        ensure_equals("one message sent", conn->mSent.size(), (size_t)1);

        boost::system::error_code ec;
        const LLSD sent = LlsdFromJson(boost::json::parse(conn->mSent[0], ec));
        ensure("parsed without error", !ec);
        ensure_equals("unauthenticated, even a built-in method is refused", sent["error"]["code"].asInteger(), LLJSONRPCConnection::RPCError::UNAUTHORIZED);
    }

    template<> template<>
    void jsonrpcws_object::test<16>()
    {
        set_test_name("makeEnvelope includes id and method by the documented rules");

        // Notification: id undefined, method non-empty -> id omitted.
        const LLSD notification = LLJSONRPCConnection::makeEnvelope(LLSD(), "object.list", LLSD::emptyMap(), LLSD(), LLSD());
        ensure("notification omits id", !notification.has("id"));
        ensure_equals("notification keeps method", notification["method"].asString(), std::string("object.list"));

        // Request: id defined, method non-empty -> both kept.
        const LLSD request = LLJSONRPCConnection::makeEnvelope(LLSD("req_1"), "object.list", LLSD::emptyMap(), LLSD(), LLSD());
        ensure("request keeps id", request.has("id"));
        ensure_equals("request keeps method", request["method"].asString(), std::string("object.list"));

        // Success response: id defined, method empty -> id kept, method omitted.
        const LLSD response = LLJSONRPCConnection::makeEnvelope(LLSD("req_1"), std::string(), LLSD(), LLSD("ok"), LLSD());
        ensure("response keeps id", response.has("id"));
        ensure("response omits method", !response.has("method"));
        ensure_equals("response keeps result", response["result"].asString(), std::string("ok"));

        // Error response with an undefined id (e.g. a parse error): method
        // is empty, so id is kept even though undefined -- serializes as null.
        LLSD error_obj;
        error_obj["code"] = -32700;
        error_obj["message"] = "Parse error";
        const LLSD error_response = LLJSONRPCConnection::makeEnvelope(LLSD(), std::string(), LLSD(), LLSD(), error_obj);
        ensure("error response keeps id even when undefined", error_response.has("id"));
        ensure("that id is null", error_response["id"].isUndefined());
        ensure("error response omits method", !error_response.has("method"));
    }

    template<> template<>
    void jsonrpcws_object::test<17>()
    {
        set_test_name("onClose fails every pending request with CONNECTION_CLOSED, exactly once");

        TestJSONRPCConnection conn;

        int callback_count_a = 0;
        int callback_count_b = 0;
        conn.testInjectPendingRequest(
            "req_a",
            LLTimer::getTotalSeconds() + 60.0,
            [&](const LLSD& result, const LLSD& error)
            {
                ++callback_count_a;
                ensure("no result on connection close", result.isUndefined());
                ensure_equals("connection closed error code", error["code"].asInteger(), LLJSONRPCConnection::RPCError::CONNECTION_CLOSED);
            });
        conn.testInjectPendingRequest(
            "req_b",
            LLTimer::getTotalSeconds() + 60.0,
            [&](const LLSD&, const LLSD& error)
            {
                ++callback_count_b;
                ensure_equals("connection closed error code", error["code"].asInteger(), LLJSONRPCConnection::RPCError::CONNECTION_CLOSED);
            });
        ensure_equals("two requests pending", conn.testPendingRequestCount(), (size_t)2);

        conn.onClose();

        ensure_equals("first callback fires exactly once", callback_count_a, 1);
        ensure_equals("second callback fires exactly once", callback_count_b, 1);
        ensure_equals("nothing left pending", conn.testPendingRequestCount(), (size_t)0);
    }
}