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 }