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_DOMAIN_NAME = "domainname.com"; 35static const int DOMAIN_NAME_MAX_LEN = 256; 36static const int MIN_LEN = 1; 37 38class SetDomainNameApiTest : public testing::Test { 39public: 40 static void SetUpTestCase(); 41 static void TearDownTestCase(); 42 void SetUp(); 43 void TearDown(); 44private: 45 char domainName[DOMAIN_NAME_MAX_LEN] = { 0 }; 46}; 47void SetDomainNameApiTest::SetUp() 48{ 49 (void)getdomainname(domainName, sizeof(domainName)); 50} 51void SetDomainNameApiTest::TearDown() 52{ 53 (void)setdomainname(domainName, strlen(domainName)); 54} 55void SetDomainNameApiTest::SetUpTestCase() 56{ 57} 58void SetDomainNameApiTest::TearDownTestCase() 59{ 60} 61 62/* 63 * @tc.number : SUB_KERNEL_SYSCALL_SETDOMAINNAME_0100 64 * @tc.name : SetdomainnameChangeNameSuccess_0001 65 * @tc.desc : setdomainname change the system domainname success. 66 * @tc.size : MediumTest 67 * @tc.type : Function 68 * @tc.level : Level 1 69 */ 70HWTEST_F(SetDomainNameApiTest, SetdomainnameChangeNameSuccess_0001, Function | MediumTest | Level1) 71{ 72 char buffer[DOMAIN_NAME_MAX_LEN] = { 0 }; 73 int ret = setdomainname(TEST_DOMAIN_NAME, strlen(TEST_DOMAIN_NAME)); 74 EXPECT_EQ(ret, 0); 75 ret = getdomainname(buffer, sizeof(buffer)); 76 EXPECT_EQ(ret, 0); 77 EXPECT_STREQ(buffer, TEST_DOMAIN_NAME); 78} 79 80/* 81 * @tc.number : SUB_KERNEL_SYSCALL_SETDOMAINNAME_0200 82 * @tc.name : SetdomainnameUseInvalidLenFailed_0002 83 * @tc.desc : setdomainname use the length larger than 256 failed, errno EINVAL. 84 * @tc.size : MediumTest 85 * @tc.type : Function 86 * @tc.level : Level 2 87 */ 88HWTEST_F(SetDomainNameApiTest, SetdomainnameUseInvalidLenFailed_0002, Function | MediumTest | Level2) 89{ 90 errno = 0; 91 int ret = setdomainname(TEST_DOMAIN_NAME, DOMAIN_NAME_MAX_LEN + 1); 92 EXPECT_EQ(ret, -1); 93 EXPECT_EQ(errno, EINVAL); 94} 95 96/* 97 * @tc.number : SUB_KERNEL_SYSCALL_SETDOMAINNAME_0300 98 * @tc.name : SetdomainnameUseInvalidNameFailed_0003 99 * @tc.desc : setdomainname use invalid name failed. 100 * @tc.size : MediumTest 101 * @tc.type : Function 102 * @tc.level : Level 2 103 */ 104HWTEST_F(SetDomainNameApiTest, SetdomainnameUseInvalidNameFailed_0003, Function | MediumTest | Level2) 105{ 106 errno = 0; 107 int ret = setdomainname(nullptr, strlen(TEST_DOMAIN_NAME)); 108 EXPECT_EQ(ret, -1); 109 EXPECT_EQ(errno, EFAULT); 110} 111 112/* 113 * @tc.number : SUB_KERNEL_SYSCALL_SETDOMAINNAME_0400 114 * @tc.name : GetdomainnameMinLenFailed_0004 115 * @tc.desc : getdomainname use too min len failed return EINVAL. 116 * @tc.size : MediumTest 117 * @tc.type : Function 118 * @tc.level : Level 2 119 */ 120HWTEST_F(SetDomainNameApiTest, GetdomainnameMinLenFailed_0004, Function | MediumTest | Level2) 121{ 122 int ret; 123 char buffer[MIN_LEN] = { 0 }; 124 errno = 0; 125 ret = getdomainname(buffer, sizeof(buffer)); 126 EXPECT_EQ(ret, -1); 127 EXPECT_EQ(errno, EINVAL); 128}