1 /*
2  * Copyright (c) 2022 Huawei Device Co., Ltd.
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 
16 #include <gtest/gtest.h>
17 #include <hilog/log.h>
18 #include <cmath>
19 #include <thread>
20 
21 #include "local_socketpair.h"
22 
23 using namespace testing::ext;
24 
25 namespace OHOS {
26 
27 static bool g_flag = false;
SendDataThread(LocalSocketPair * socketPair)28 static void SendDataThread(LocalSocketPair *socketPair)
29 {
30     int32_t data = 10;
31     socketPair->SendData((void *)&data, sizeof(int32_t));
32     g_flag = true;
33 }
34 
35 class LocalSocketPairTest : public testing::Test {
36 public:
37     static constexpr HiviewDFX::HiLogLabel LOG_LABEL = {LOG_CORE, 0, "LocalSocketPairTest"};
38 
SetUpTestCase()39     static void SetUpTestCase()
40     {}
41 
TearDownTestCase()42     static void TearDownTestCase()
43     {}
44 };
45 
46 /*
47 * Function: CreateChannel001
48 * Type: Function
49 * Rank: Important(2)
50 * EnvConditions: N/A
51 * CaseDescription: CreateChannel
52 */
53 HWTEST_F(LocalSocketPairTest, CreateChannel001, Function | SmallTest | Level2)
54 {
55     LocalSocketPair socketPair;
56     socketPair.CreateChannel(8, 8);
57     int32_t ret = socketPair.CreateChannel(8, 8);
58     ASSERT_EQ(ret, 0);
59 }
60 /*
61 * Function: ReceiveData001
62 * Type: Function
63 * Rank: Important(2)
64 * EnvConditions: N/A
65 * CaseDescription: ReceiveData
66 */
67 HWTEST_F(LocalSocketPairTest, ReceiveData001, Function | SmallTest | Level2)
68 {
69     LocalSocketPair socketPair;
70     int32_t ret = socketPair.ReceiveData(nullptr, 0);
71     ASSERT_EQ(ret, -1);
72     socketPair.CreateChannel(8, 8);
73     std::thread th(SendDataThread, &socketPair);
74     th.join();
75     while (!g_flag) {}
76     int32_t data = 0;
77     ret = socketPair.ReceiveData(&data, sizeof(int32_t));
78     ASSERT_EQ(data, 10);
79 }
80 
81 /*
82 * Function: SendData
83 * Type: Function
84 * Rank: Important(2)
85 * EnvConditions: N/A
86 * CaseDescription: test SendData without CreateChannel before
87 */
88 HWTEST_F(LocalSocketPairTest, TestSendDataWithoutCreateChannel, Function | SmallTest | Level2)
89 {
90     LocalSocketPair socketPair;
91     int32_t data = 10;
92     int32_t ret = socketPair.SendData((void *)&data, sizeof(int32_t));
93     ASSERT_EQ(ret, -1);
94 }
95 
96 /*
97 * Function: CreateChannel, SendData
98 * Type: Function
99 * Rank: Important(2)
100 * EnvConditions: N/A
101 * CaseDescription: test SendData with nullptr
102 */
103 HWTEST_F(LocalSocketPairTest, TestSendNullptr, Function | SmallTest | Level2)
104 {
105     LocalSocketPair socketPair;
106     int32_t ret = socketPair.CreateChannel(8, 8);
107     ASSERT_EQ(ret, 0);
108     ret = socketPair.SendData(nullptr, sizeof(int32_t));
109     ASSERT_EQ(ret, -1);
110 }
111 
112 /*
113 * Function: CreateChannel
114 * Type: Function
115 * Rank: Important(2)
116 * EnvConditions: N/A
117 * CaseDescription: test CreateChannel after closing 0 fd
118 */
119 HWTEST_F(LocalSocketPairTest, Close0Fd, Function | SmallTest | Level2)
120 {
121     LocalSocketPair socketPair;
122     close(0);
123     int32_t ret = socketPair.CreateChannel(8, 8);
124     ASSERT_EQ(ret, 0);
125 }
126 }