1#define CTEST_MAIN
2#include "ctest.h"
3
4#include "common.h"
5
6#include "rpc.h"
7
8// RPC Call: session.login
9vrtql_msg* session_login(vrtql_rpc_env* e, vrtql_msg* m)
10{
11 vrtql_msg* reply = vrtql_msg_new();
12 vrtql_msg_set_header(reply, "rc", "0");
13
14 return reply;
15}
16
17// RPC Call: session.logout
18vrtql_msg* session_logout(vrtql_rpc_env* e, vrtql_msg* m)
19{
20 vrtql_msg* reply = vrtql_msg_new();
21 vrtql_msg_set_header(reply, "rc", "0");
22
23 return reply;
24}
25
26// RPC Call: session.info
27vrtql_msg* session_info(vrtql_rpc_env* e, vrtql_msg* m)
28{
29 vrtql_msg* reply = vrtql_msg_new();
30 vrtql_msg_set_header(reply, "rc", "0");
31
32 return reply;
33}
34
35CTEST(test_rpc, server_side_module)
36{
37 // Define module
38 vrtql_rpc_module* module = vrtql_rpc_module_new("session");
39 vrtql_rpc_module_set(module, "login", session_login);
40 vrtql_rpc_module_set(module, "logout", session_logout);
41 vrtql_rpc_module_set(module, "info", session_info);
42
43 // Cleanup
44 vrtql_rpc_module_free(module);
45}
46
47CTEST(test_rpc, server_side_service)
48{
49 // Create RPC system
50 vrtql_rpc_system* system = vrtql_rpc_system_new();
51
52 // Create module
53 vrtql_rpc_module* module = vrtql_rpc_module_new("session");
54 vrtql_rpc_module_set(module, "login", session_login);
55 vrtql_rpc_module_set(module, "logout", session_logout);
56 vrtql_rpc_module_set(module, "info", session_info);
57
58 // Register module in system
59 vrtql_rpc_system_set(system, module);
60
61 // Setup RPC request
62 vrtql_rpc_env env;
63 vrtql_msg* req = vrtql_msg_new();
64 vrtql_msg_set_header(req, "id", "session.login");
65
66 // Invoke RPC
67 vrtql_msg* reply = vrtql_rpc_service(system, &env, req);
68 ASSERT_TRUE(reply != NULL);
69
70 // Verify reply
71 cstr rc = vrtql_msg_get_header(reply, "rc");
72 ASSERT_TRUE(strncmp(rc, "0", 1) == 0);
73
74 // Cleanup
75 vrtql_msg_free(reply);
76 vrtql_rpc_system_free(system);
77}
78
79int main(int argc, const char* argv[])
80{
81 return ctest_main(argc, argv);
82}
83