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/file.h>
25#include <sys/stat.h>
26#include <sys/types.h>
27#include <sys/utsname.h>
28#include <sys/xattr.h>
29#include "securec.h"
30
31using namespace testing::ext;
32using namespace std;
33
34static const char *TEST_HOST_NAME = "TestHostName";
35static const int HOST_NAME_MAX_LEN = 256;
36
37class SetHostNameApiTest : public testing::Test {
38public:
39    static void SetUpTestCase();
40    static void TearDownTestCase();
41    void SetUp();
42    void TearDown();
43private:
44    char hostName[HOST_NAME_MAX_LEN] = { 0 };
45};
46void SetHostNameApiTest::SetUp()
47{
48    (void)gethostname(hostName, sizeof(hostName));
49}
50void SetHostNameApiTest::TearDown()
51{
52    (void)sethostname(hostName, sizeof(hostName));
53}
54void SetHostNameApiTest::SetUpTestCase()
55{
56}
57void SetHostNameApiTest::TearDownTestCase()
58{
59}
60
61/*
62 * @tc.number : SUB_KERNEL_SYSCALL_SETHOSTNAME_0100
63 * @tc.name   : SethostnameChangeHostNameSuccess_0001
64 * @tc.desc   : sethostname change the system hostname success.
65 * @tc.size   : MediumTest
66 * @tc.type   : Function
67 * @tc.level  : Level 1
68 */
69HWTEST_F(SetHostNameApiTest, SethostnameChangeHostNameSuccess_0001, Function | MediumTest | Level1)
70{
71    char buffer[HOST_NAME_MAX_LEN] = { 0 };
72    int ret = sethostname(TEST_HOST_NAME, strlen(TEST_HOST_NAME));
73    EXPECT_EQ(ret, 0);
74    ret = gethostname(buffer, sizeof(buffer));
75    EXPECT_EQ(ret, 0);
76    EXPECT_STREQ(buffer, TEST_HOST_NAME);
77}
78
79/*
80 * @tc.number : SUB_KERNEL_SYSCALL_SETHOSTNAME_0200
81 * @tc.name   : SethostnameUseInvalidLenFailed_0002
82 * @tc.desc   : sethostname use the length larger than 256 failed, errno EINVAL.
83 * @tc.size   : MediumTest
84 * @tc.type   : Function
85 * @tc.level  : Level 2
86 */
87HWTEST_F(SetHostNameApiTest, SethostnameUseInvalidLenFailed_0002, Function | MediumTest | Level2)
88{
89    errno = 0;
90    int ret = sethostname(TEST_HOST_NAME, HOST_NAME_MAX_LEN + 1);
91    EXPECT_EQ(ret, -1);
92    EXPECT_EQ(errno, EINVAL);
93}
94
95/*
96 * @tc.number : SUB_KERNEL_SYSCALL_SETHOSTNAME_0300
97 * @tc.name   : SethostnameUseInvalidNameFailed_0003
98 * @tc.desc   : sethostname use invalid name failed, errno EFAULT.
99 * @tc.size   : MediumTest
100 * @tc.type   : Function
101 * @tc.level  : Level 2
102 */
103HWTEST_F(SetHostNameApiTest, SethostnameUseInvalidNameFailed_0003, Function | MediumTest | Level2)
104{
105    errno = 0;
106    int ret = sethostname(nullptr, strlen(TEST_HOST_NAME));
107    EXPECT_EQ(ret, -1);
108    EXPECT_EQ(errno, EFAULT);
109}
110