1/*
2 * Copyright (c) Huawei Technologies Co., Ltd. 2022. All rights reserved.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15#include <string>
16
17#include <gtest/gtest.h>
18
19#include "ipc_unix_socket.h"
20
21using namespace testing::ext;
22
23namespace OHOS {
24namespace Developtools {
25namespace Hiebpf {
26class IpcUnicSocketTest : public ::testing::Test {
27public:
28    static void SetUpTestCase() {};
29    static void TearDownTestCase() {};
30
31    void SetUp() {}
32    void TearDown() {}
33
34    const std::string TEST_PATH_NAME = "/data/test/unix_socket_hiebpf";
35};
36
37/**
38 * @tc.name: ServerStartStop
39 * @tc.desc: Test framework
40 * @tc.type: FUNC
41 */
42HWTEST_F(IpcUnicSocketTest, ServerStartStop, TestSize.Level1)
43{
44    IpcUnixSocketServer server;
45    ASSERT_TRUE(server.Start(TEST_PATH_NAME));
46    sleep(2);
47    ASSERT_TRUE(server.Stop());
48}
49
50HWTEST_F(IpcUnicSocketTest, ClientConnectFailed, TestSize.Level1)
51{
52    IpcUnixSocketClient client;
53    ASSERT_FALSE(client.Connect(TEST_PATH_NAME));
54}
55
56HWTEST_F(IpcUnicSocketTest, ServerClientCommunicate, TestSize.Level1)
57{
58    IpcUnixSocketServer server;
59    ASSERT_TRUE(server.Start(TEST_PATH_NAME));
60
61    IpcUnixSocketClient client;
62    ASSERT_TRUE(client.Connect(TEST_PATH_NAME));
63    std::string cmd = "test command";
64    ASSERT_TRUE(client.SendMessage(cmd.data(), cmd.size()));
65    const int bufSize = 1024;
66    std::string buf;
67    size_t size = bufSize;
68    buf.resize(size);
69    const uint32_t timeout = 1000;
70    ASSERT_TRUE(client.RecvMessage(buf.data(), size, timeout));
71    ASSERT_EQ(size, 0);
72
73    size = bufSize;
74    server.SetHandleCallback([&](const void *buf, size_t size) {
75        server.SendMessage(buf, size);
76        });
77    ASSERT_TRUE(client.SendMessage(cmd.data(), cmd.size()));
78    ASSERT_TRUE(client.RecvMessage(buf.data(), size, timeout));
79    buf.resize(size);
80    ASSERT_EQ(buf, cmd);
81
82    client.Disconnect();
83    ASSERT_TRUE(server.Stop());
84}
85
86HWTEST_F(IpcUnicSocketTest, ServerClientException, TestSize.Level1)
87{
88    IpcUnixSocketServer server;
89    std::string cmd = "test command";
90    ASSERT_FALSE(server.SendMessage(cmd.data(), cmd.size()));
91
92    ASSERT_TRUE(server.Start(TEST_PATH_NAME));
93    ASSERT_FALSE(server.Start(TEST_PATH_NAME));
94
95    IpcUnixSocketClient client;
96    ASSERT_FALSE(client.SendMessage(cmd.data(), cmd.size()));
97    const uint32_t timeout = 1000;
98    const int bufSize = 1024;
99    size_t size = bufSize;
100    std::string buf;
101    buf.resize(size);
102    ASSERT_FALSE(client.RecvMessage(buf.data(), size, timeout));
103    ASSERT_TRUE(client.Connect(TEST_PATH_NAME));
104    ASSERT_FALSE(client.Connect(TEST_PATH_NAME));
105}
106} // namespace Hiebpf
107} // namespace Developtools
108} // namespace OHOS
109