1 /*
2  * Copyright (C) 2024 HiHope Open Source Organization.
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 <cerrno>
17 #include <cstdio>
18 #include <cstdlib>
19 #include <csignal>
20 #include <string>
21 #include <vector>
22 #include <fcntl.h>
23 #include <unistd.h>
24 #include <malloc.h>
25 #include <arpa/inet.h>
26 #include <gtest/gtest.h>
27 #include <netinet/in.h>
28 #include <sys/stat.h>
29 #include <sys/mman.h>
30 #include <sys/socket.h>
31 #include <sys/syscall.h>
32 #include <sys/types.h>
33 #include "securec.h"
34 
35 using namespace testing::ext;
36 
37 class HatsPipe2Test : public testing::Test {
38 public:
39     static void SetUpTestCase();
40     static void TearDownTestCase();
41     void SetUp();
42     void TearDown();
43 private:
44 };
SetUp()45 void HatsPipe2Test::SetUp()
46 {
47 }
48 
TearDown()49 void HatsPipe2Test::TearDown()
50 {
51 }
52 
SetUpTestCase()53 void HatsPipe2Test::SetUpTestCase()
54 {
55 }
56 
TearDownTestCase()57 void HatsPipe2Test::TearDownTestCase()
58 {
59 }
60 
61 /*
62  * @tc.number : SUB_KERNEL_SYSCALL_PIPE2_0100
63  * @tc.name   : Pipe2Success_0001
64  * @tc.desc   : Pipe2 create pipe fds with flag O_CLOEXEC and O_NONBLOCK successfully.
65  * @tc.size   : MediumTest
66  * @tc.type   : Function
67  * @tc.level  : Level 1
68  */
HWTEST_F(HatsPipe2Test, Pipe2Success_0001, Function | MediumTest | Level1)69 HWTEST_F(HatsPipe2Test, Pipe2Success_0001, Function | MediumTest | Level1)
70 {
71     int pipeFds[2];
72 
73     int ret = pipe2(pipeFds, O_CLOEXEC | O_NONBLOCK);
74     EXPECT_EQ(ret, 0);
75     EXPECT_TRUE(pipeFds[0] > 0 && pipeFds[1] > 0);
76 }
77 
78 /*
79  * @tc.number : SUB_KERNEL_SYSCALL_PIPE2_0200
80  * @tc.name   : Pipe2Failed_0002
81  * @tc.desc   : Pipe2 fails for invalid Fd array.
82  * @tc.size   : MediumTest
83  * @tc.type   : Function
84  * @tc.level  : Level 2
85  */
HWTEST_F(HatsPipe2Test, Pipe2Failed_0002, Function | MediumTest | Level2)86 HWTEST_F(HatsPipe2Test, Pipe2Failed_0002, Function | MediumTest | Level2)
87 {
88     int* pipeFds = nullptr;
89 
90     errno = 0;
91     int ret = pipe2(pipeFds, O_CLOEXEC);
92     EXPECT_EQ(ret, -1);
93     EXPECT_EQ(errno, EFAULT);
94 }
95