4 * Copyright (c) 2017 Samsung Electronics Co., Ltd.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
22 #include <sensor_internal.h>
24 #include "shared/channel.h"
25 #include "shared/channel_handler.h"
26 #include "shared/ipc_client.h"
27 #include "shared/ipc_server.h"
30 #include "test_bench.h"
34 #define MAX_BUF_SIZE 4096
35 #define TEST_PATH "/run/.sensord_test.socket"
36 #define SLEEP_1S sleep(1)
38 typedef bool (*process_func_t)(const char *msg, int size, int count);
40 static pid_t run_process(process_func_t func, const char *msg, int size, int count)
47 if (!func(msg, size, count))
48 _E("Failed to run process\n");
55 class test_echo_server_handler : public channel_handler
58 void connected(channel *ch) {}
59 void disconnected(channel *ch) {}
60 void read(channel *ch, message &msg)
62 char buf[MAX_BUF_SIZE];
64 message *reply = new(std::nothrow) message();
65 RETM_IF(!reply, "Failed to allocate memory");
68 reply->enclose(buf, MAX_BUF_SIZE);
72 void read_complete(channel *ch) {}
73 void error_caught(channel *ch, int error) {}
77 static bool run_ipc_server_echo(const char *str, int size, int count)
81 ipc_server server(TEST_PATH);
82 test_echo_server_handler handler;
84 server.set_option("max_connection", 10);
85 server.set_option(SO_TYPE, SOCK_STREAM);
86 server.bind(&handler, &eloop);
94 class test_client_handler_30_1M : public channel_handler
97 void connected(channel *ch) {}
98 void disconnected(channel *ch) {}
99 void read(channel *ch, message &msg) {}
100 void read_complete(channel *ch) {}
101 void error_caught(channel *ch, int error) {}
104 /* IPC Client Sleep Test(4096Kb * 1024) */
105 static bool run_ipc_client_sleep_1s(const char *str, int size, int count)
107 ipc_client client(TEST_PATH);
108 test_client_handler_30_1M client_handler;
110 channel *ch = client.connect(&client_handler, NULL);
115 char buf[MAX_BUF_SIZE] = {'1', '1', '1', };
117 msg.enclose(buf, MAX_BUF_SIZE);
125 ch->read_sync(reply);
128 int ret = strncmp(buf, "111", 3);
130 ASSERT_EQ(reply.size(), MAX_BUF_SIZE);
138 /* IPC Client With Small Buffer Test(4096Kb * 1024) */
139 static bool run_ipc_client_small_buffer(const char *str, int size, int count)
141 ipc_client client(TEST_PATH);
142 test_client_handler_30_1M client_handler;
144 channel *ch = client.connect(&client_handler, NULL);
150 char buf[MAX_BUF_SIZE] = {'1', '1', '1', };
152 msg.enclose(buf, MAX_BUF_SIZE);
157 ch->get_option(SO_RCVBUF, buf_size);
158 _I("Before: Buffer size : %d\n", buf_size);
160 ch->set_option(SO_RCVBUF, buf_size/2048);
161 ch->get_option(SO_RCVBUF, buf_size);
162 _I("After: Buffer size : %d\n", buf_size);
164 for (int i = 0; i < 1024; ++i) {
166 ch->read_sync(reply);
171 ret = strncmp(buf, "111", 3);
173 ASSERT_EQ(reply.size(), MAX_BUF_SIZE);
181 /* IPC Client Test(4K * 256) */
182 static bool run_ipc_client_1M(const char *str, int size, int count)
184 ipc_client client(TEST_PATH);
185 test_client_handler_30_1M client_handler;
187 channel *ch = client.connect(&client_handler, NULL);
193 char buf[MAX_BUF_SIZE] = {'1', '1', '1', };
195 msg.enclose(buf, MAX_BUF_SIZE);
199 for (int i = 0; i < 256; ++i) {
201 ch->read_sync(reply);
206 ret = strncmp(buf, "111", 3);
208 ASSERT_EQ(reply.size(), MAX_BUF_SIZE);
216 /* IPC Server Test(Not Echo) */
217 class test_server_handler : public channel_handler
220 void connected(channel *ch) {}
221 void disconnected(channel *ch) {}
222 void read(channel *ch, message &msg)
224 char buf[MAX_BUF_SIZE];
227 message *reply = new(std::nothrow) message();
230 reply->enclose("TEXTTEXTTEXTTEXT", 16);
233 void read_complete(channel *ch) {}
234 void error_caught(channel *ch, int error) {}
237 static bool run_ipc_server(const char *str, int size, int count)
241 ipc_server server(TEST_PATH);
242 test_server_handler handler;
244 server.set_option("max_connection", 10);
245 server.set_option(SO_TYPE, SOCK_STREAM);
246 server.bind(&handler, &eloop);
248 /* run main loop for 5 seconds */
256 /* IPC Client Test(Not Echo) */
257 class test_client_handler : public channel_handler
260 void connected(channel *ch) {}
261 void disconnected(channel *ch) {}
262 void read(channel *ch, message &msg) {}
263 void read_complete(channel *ch) {}
264 void error_caught(channel *ch, int error) {}
267 static bool run_ipc_client_2_channel_message(const char *str, int size, int count)
269 ipc_client client(TEST_PATH);
270 test_client_handler client_handler;
275 char buf[MAX_BUF_SIZE];
277 ch[0] = client.connect(&client_handler, NULL);
280 msg.enclose("TESTTESTTEST", 12);
281 ch[0]->send_sync(&msg);
283 ch[0]->read_sync(reply);
285 ret = strncmp(buf, "TEXTTEXTTEXTTEXT", 16);
288 ch[1] = client.connect(&client_handler, NULL);
291 msg.enclose("TESTTESTTEST", 12);
292 ch[1]->send_sync(&msg);
294 ch[1]->read_sync(reply);
296 ret = strncmp(buf, "TEXTTEXTTEXTTEXT", 16);
307 static bool run_ipc_client_2_channel(const char *str, int size, int count)
309 ipc_client client(TEST_PATH);
310 test_client_handler client_handler;
313 ch[0] = client.connect(&client_handler, NULL);
315 ch[1] = client.connect(&client_handler, NULL);
317 ASSERT_NE(ch[1], ch[0]);
327 static bool run_ipc_client(const char *str, int size, int count)
329 ipc_client client(TEST_PATH);
330 test_client_handler client_handler;
332 channel *ch = client.connect(&client_handler, NULL);
338 char buf[MAX_BUF_SIZE];
340 msg.enclose("TESTTESTTEST", 12);
345 ch->read_sync(reply);
348 ret = strncmp(buf, "TEXTTEXTTEXTTEXT", 16);
358 * @brief Test 3 client + 1 client which sleeps 1 seconds
360 TESTCASE(sensor_ipc, 3_client_with_1s_sleep_client_p)
362 pid_t pid = run_process(run_ipc_server_echo, NULL, 0, 0);
367 for (int i = 0; i < 3; ++i) {
368 pid = run_process(run_ipc_client_1M, NULL, 0, 0);
372 bool ret = run_ipc_client_sleep_1s(NULL, 0, 0);
381 * @brief Test 3 client + 1 client which has small recv buffer(2240)
383 TESTCASE(sensor_ipc, 3_client_with_small_buffer_client_p)
385 pid_t pid = run_process(run_ipc_server_echo, NULL, 0, 0);
390 for (int i = 0; i < 3; ++i) {
391 pid = run_process(run_ipc_client_1M, NULL, 0, 0);
395 bool ret = run_ipc_client_small_buffer(NULL, 0, 0);
404 * @brief Test 30 ipc_client with 1M message
406 TESTCASE(sensor_ipc, 30_client_with_1M_message_p)
408 pid_t pid = run_process(run_ipc_server_echo, NULL, 0, 0);
413 for (int i = 0; i < 30; ++i) {
414 pid = run_process(run_ipc_client_1M, NULL, 0, 0);
418 bool ret = run_ipc_client_1M(NULL, 0, 0);
427 * @brief Test 2 channel of 1 client with message
429 TESTCASE(sensor_ipc, 1_client_with_2_channel_message_p)
431 pid_t pid = run_process(run_ipc_server, NULL, 0, 0);
436 bool ret = run_ipc_client_2_channel_message(NULL, 0, 0);
445 * @brief Test 2 channel of 1 client
447 TESTCASE(sensor_ipc, 1_client_2_channel_simple_p)
449 pid_t pid = run_process(run_ipc_server, NULL, 0, 0);
454 bool ret = run_ipc_client_2_channel(NULL, 0, 0);
463 * @brief Test 100 ipc_client
465 TESTCASE(sensor_ipc, 100_client_p)
467 pid_t pid = run_process(run_ipc_server, NULL, 0, 0);
472 for (int i = 0; i < 99; ++i) {
473 pid = run_process(run_ipc_client, NULL, 0, 0);
477 bool ret = run_ipc_client(NULL, 0, 0);
486 * @brief Test 2 ipc_client
488 TESTCASE(sensor_ipc, 2_client_p)
490 pid_t pid = run_process(run_ipc_server, NULL, 0, 0);
495 pid = run_process(run_ipc_client, NULL, 0, 0);
498 bool ret = run_ipc_client(NULL, 0, 0);
507 * @brief Test ipc_client/ipc_server class
508 * @details 1. connect/disconnect
509 * 2. send "TEST" message from client to server
510 * 3. check that message in server handler
512 TESTCASE(sensor_ipc, server_client_basic_p)
514 pid_t pid = run_process(run_ipc_server, NULL, 0, 0);
519 bool ret = run_ipc_client(NULL, 0, 0);