blob: 441391017c791fb2e9e6fdeabb37d3d7c959d8f4 [file] [log] [blame]
Patrick Williams1807fa42016-08-01 22:23:30 -05001#include <iostream>
Patrick Williams1807fa42016-08-01 22:23:30 -05002#include <cassert>
Patrick Williams7802c072016-09-02 15:20:22 -05003#include <sdbusplus/message.hpp>
Patrick Williams5b485792016-08-02 07:35:14 -05004#include <sdbusplus/bus.hpp>
Patrick Williams1807fa42016-08-01 22:23:30 -05005
6// Global to share the dbus type string between client and server.
7static std::string verifyTypeString;
8
Patrick Williams07e1d6f2016-09-08 15:54:09 -05009using verifyCallback_t = void(*)(sd_bus_message*);
10verifyCallback_t verifyCallback = nullptr;
11
Patrick Williams1807fa42016-08-01 22:23:30 -050012static constexpr auto SERVICE = "sdbusplus.test";
13static constexpr auto INTERFACE = SERVICE;
14static constexpr auto TEST_METHOD = "test";
15static constexpr auto QUIT_METHOD = "quit";
16
17// Open up the sdbus and claim SERVICE name.
Patrick Williams5b485792016-08-02 07:35:14 -050018auto serverInit()
Patrick Williams1807fa42016-08-01 22:23:30 -050019{
Patrick Williams5b485792016-08-02 07:35:14 -050020 auto b = sdbusplus::bus::new_default();
21 b.request_name(SERVICE);
22
23 return std::move(b);
Patrick Williams1807fa42016-08-01 22:23:30 -050024}
25
26// Thread to run the dbus server.
27void* server(void* b)
28{
Patrick Williams5b485792016-08-02 07:35:14 -050029 auto bus = sdbusplus::bus::bus(reinterpret_cast<sdbusplus::bus::busp_t>(b));
Patrick Williams1807fa42016-08-01 22:23:30 -050030
31 while(1)
32 {
33 // Wait for messages.
Patrick Williams13b97c52016-09-09 09:36:55 -050034 auto m = bus.process().release();
Patrick Williams1807fa42016-08-01 22:23:30 -050035
Patrick Williams5b485792016-08-02 07:35:14 -050036 if(m == nullptr)
Patrick Williams1807fa42016-08-01 22:23:30 -050037 {
Patrick Williams5b485792016-08-02 07:35:14 -050038 bus.wait();
Patrick Williams1807fa42016-08-01 22:23:30 -050039 continue;
40 }
41
42 if (sd_bus_message_is_method_call(m, INTERFACE, TEST_METHOD))
43 {
44 // Verify the message type matches what the test expects.
Patrick Williams1807fa42016-08-01 22:23:30 -050045 assert(verifyTypeString == sd_bus_message_get_signature(m, true));
Patrick Williams07e1d6f2016-09-08 15:54:09 -050046 if (verifyCallback)
47 {
48 verifyCallback(m);
49 verifyCallback = nullptr;
50 }
51 else
52 {
53 std::cout << "Warning: No verification for "
54 << verifyTypeString << std::endl;
55 }
Patrick Williams1807fa42016-08-01 22:23:30 -050056 // Reply to client.
57 sd_bus_reply_method_return(m, nullptr);
58 }
59 else if (sd_bus_message_is_method_call(m, INTERFACE, QUIT_METHOD))
60 {
61 // Reply and exit.
62 sd_bus_reply_method_return(m, nullptr);
63 break;
64 }
65 }
66}
67
Patrick Williams7802c072016-09-02 15:20:22 -050068auto newMethodCall__test(sdbusplus::bus::bus& b)
Patrick Williams1807fa42016-08-01 22:23:30 -050069{
70 // Allocate a method-call message for INTERFACE,TEST_METHOD.
Patrick Williams7802c072016-09-02 15:20:22 -050071 return b.new_method_call(SERVICE, "/", INTERFACE, TEST_METHOD);
Patrick Williams1807fa42016-08-01 22:23:30 -050072}
73
74void runTests()
75{
76 using namespace std::literals;
77
Patrick Williams5b485792016-08-02 07:35:14 -050078 auto b = sdbusplus::bus::new_default();
Patrick Williams1807fa42016-08-01 22:23:30 -050079
80 // Test r-value int.
81 {
Patrick Williams7802c072016-09-02 15:20:22 -050082 auto m = newMethodCall__test(b);
83 m.append(1);
Patrick Williams1807fa42016-08-01 22:23:30 -050084 verifyTypeString = "i";
Patrick Williams07e1d6f2016-09-08 15:54:09 -050085
86 struct verify
87 {
88 static void op(sd_bus_message* m)
89 {
90 int32_t i = 0;
91 sd_bus_message_read_basic(m, 'i', &i);
92 assert(i == 1);
93 }
94 };
95 verifyCallback = &verify::op;
96
Patrick Williams5b485792016-08-02 07:35:14 -050097 b.call_noreply(m);
Patrick Williams1807fa42016-08-01 22:23:30 -050098 }
Patrick Williams1807fa42016-08-01 22:23:30 -050099 // Test l-value int.
100 {
Patrick Williams7802c072016-09-02 15:20:22 -0500101 auto m = newMethodCall__test(b);
Patrick Williams1807fa42016-08-01 22:23:30 -0500102 int a = 1;
Patrick Williams7802c072016-09-02 15:20:22 -0500103 m.append(a, a);
Patrick Williams1807fa42016-08-01 22:23:30 -0500104 verifyTypeString = "ii";
Patrick Williams07e1d6f2016-09-08 15:54:09 -0500105
106 struct verify
107 {
108 static void op(sd_bus_message* m)
109 {
110 int32_t a = 0, b = 0;
111 sd_bus_message_read(m, "ii", &a, &b);
112 assert(a == 1);
113 assert(b == 1);
114 }
115 };
116 verifyCallback = &verify::op;
117
Patrick Williams5b485792016-08-02 07:35:14 -0500118 b.call_noreply(m);
Patrick Williams1807fa42016-08-01 22:23:30 -0500119 }
120
121 // Test multiple ints.
122 {
Patrick Williams7802c072016-09-02 15:20:22 -0500123 auto m = newMethodCall__test(b);
124 m.append(1, 2, 3, 4, 5);
Patrick Williams1807fa42016-08-01 22:23:30 -0500125 verifyTypeString = "iiiii";
Patrick Williams07e1d6f2016-09-08 15:54:09 -0500126
127 struct verify
128 {
129 static void op(sd_bus_message* m)
130 {
131 int32_t a = 0, b = 0, c = 0, d = 0, e = 0;
132 sd_bus_message_read(m, "iiiii", &a, &b, &c, &d, &e);
133 assert(a == 1);
134 assert(b == 2);
135 assert(c == 3);
136 assert(d == 4);
137 assert(e == 5);
138 }
139 };
140 verifyCallback = &verify::op;
141
Patrick Williams5b485792016-08-02 07:35:14 -0500142 b.call_noreply(m);
Patrick Williams1807fa42016-08-01 22:23:30 -0500143 }
144
145 // Test r-value string.
146 {
Patrick Williams7802c072016-09-02 15:20:22 -0500147 auto m = newMethodCall__test(b);
148 m.append("asdf"s);
Patrick Williams1807fa42016-08-01 22:23:30 -0500149 verifyTypeString = "s";
Patrick Williams07e1d6f2016-09-08 15:54:09 -0500150
151 struct verify
152 {
153 static void op(sd_bus_message* m)
154 {
155 const char* s = nullptr;
156 sd_bus_message_read_basic(m, 's', &s);
157 assert(0 == strcmp("asdf", s));
158 }
159 };
160 verifyCallback = &verify::op;
161
Patrick Williams5b485792016-08-02 07:35:14 -0500162 b.call_noreply(m);
Patrick Williams1807fa42016-08-01 22:23:30 -0500163 }
164
165 // Test multiple strings, various forms.
166 {
Patrick Williams7802c072016-09-02 15:20:22 -0500167 auto m = newMethodCall__test(b);
Patrick Williams1807fa42016-08-01 22:23:30 -0500168 auto str = "jkl;"s;
169 auto str2 = "JKL:"s;
Patrick Williams7802c072016-09-02 15:20:22 -0500170 m.append(1, "asdf", "ASDF"s, str,
171 std::move(str2), 5);
Patrick Williams1807fa42016-08-01 22:23:30 -0500172 verifyTypeString = "issssi";
Patrick Williams07e1d6f2016-09-08 15:54:09 -0500173
174 struct verify
175 {
176 static void op(sd_bus_message* m)
177 {
178 int32_t a = 0, b = 0;
179 const char *s0 = nullptr, *s1 = nullptr, *s2 = nullptr,
180 *s3 = nullptr;
181 sd_bus_message_read(m, "issssi", &a, &s0, &s1, &s2, &s3, &b);
182 assert(a == 1);
183 assert(b == 5);
184 assert(0 == strcmp("asdf", s0));
185 assert(0 == strcmp("ASDF", s1));
186 assert(0 == strcmp("jkl;", s2));
187 assert(0 == strcmp("JKL:", s3));
188 }
189 };
190 verifyCallback = &verify::op;
191
Patrick Williams5b485792016-08-02 07:35:14 -0500192 b.call_noreply(m);
Patrick Williams1807fa42016-08-01 22:23:30 -0500193 }
194
195 // Shutdown server.
Patrick Williams7802c072016-09-02 15:20:22 -0500196 {
197 auto m = b.new_method_call(SERVICE, "/", INTERFACE, QUIT_METHOD);
198 b.call_noreply(m);
199 }
Patrick Williams1807fa42016-08-01 22:23:30 -0500200}
201
202int main()
203{
Patrick Williams07e1d6f2016-09-08 15:54:09 -0500204
Patrick Williams1807fa42016-08-01 22:23:30 -0500205 // Initialize and start server thread.
206 pthread_t t;
207 {
Patrick Williams5b485792016-08-02 07:35:14 -0500208 auto b = serverInit();
209 pthread_create(&t, NULL, server, b.release());
Patrick Williams1807fa42016-08-01 22:23:30 -0500210 }
211
212 runTests();
213
214 // Wait for server thread to exit.
215 pthread_join(t, NULL);
216
217 return 0;
218}