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 <string>
20#include <vector>
21#include <fcntl.h>
22#include <unistd.h>
23#include <gtest/gtest.h>
24#include <sys/ipc.h>
25#include <sys/stat.h>
26#include <sys/sem.h>
27#include <sys/types.h>
28#include "securec.h"
29
30using namespace testing::ext;
31using namespace std;
32
33class HatsSemopTest : public testing::Test {
34public:
35    static void SetUpTestCase();
36    static void TearDownTestCase();
37    void SetUp();
38    void TearDown();
39private:
40};
41void HatsSemopTest::SetUp()
42{
43}
44void HatsSemopTest::TearDown()
45{
46}
47void HatsSemopTest::SetUpTestCase()
48{
49}
50void HatsSemopTest::TearDownTestCase()
51{
52}
53
54static const int SEMPAHORE_NUM = 1;
55
56/*
57 * @tc.number : SUB_KERNEL_SYSCALL_SEMOP_0100
58 * @tc.name   : SemopAddSemvalSuccess_0001
59 * @tc.desc   : semop add semaphore value success.
60 * @tc.size   : MediumTest
61 * @tc.type   : Function
62 * @tc.level  : Level 1
63 */
64HWTEST_F(HatsSemopTest, SemopAddSemvalSuccess_0001, Function | MediumTest | Level1)
65{
66    int ret;
67    int semid;
68    struct sembuf sop;
69    sop.sem_op = 1;
70
71    semid = semget(IPC_PRIVATE, SEMPAHORE_NUM, IPC_CREAT | 0666);
72    EXPECT_TRUE(semid >= 0);
73
74    ret = semop(semid, &sop, 1);
75    EXPECT_TRUE(ret == 0);
76}
77
78/*
79 * @tc.number : SUB_KERNEL_SYSCALL_SEMOP_0200
80 * @tc.name   : SemopInvalidSemidFail_0002
81 * @tc.desc   : semop add semaphore value of a invalid sephamore id fail.
82 * @tc.size   : MediumTest
83 * @tc.type   : Function
84 * @tc.level  : Level 2
85 */
86HWTEST_F(HatsSemopTest, SemopInvalidSemidFail_0002, Function | MediumTest | Level2)
87{
88    int ret;
89    int semid;
90    struct sembuf sop;
91    sop.sem_op = 1;
92    semid = -1;
93
94    errno = 0;
95    ret = semop(semid, &sop, 1);
96    EXPECT_TRUE(ret == -1);
97    EXPECT_EQ(errno, EINVAL);
98}
99