diff --git a/test/unittest/app_spawn_socket_test/app_spawn_socket_test.cpp b/test/unittest/app_spawn_socket_test/app_spawn_socket_test.cpp index 8be797c85fdfde724758fb321f4b6b1471cbe843..667728fa39262c9f991041eb2be71fe9529486b4 100644 --- a/test/unittest/app_spawn_socket_test/app_spawn_socket_test.cpp +++ b/test/unittest/app_spawn_socket_test/app_spawn_socket_test.cpp @@ -1,429 +1,429 @@ -/* - * Copyright (c) 2021-2022 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include - -#include -#include - -// redefine private and protected since testcase need to invoke and test private function -#define private public -#define protected public -#include "appspawn_socket.h" -#undef private -#undef protected - -#include "securec.h" - -using namespace testing; -using namespace testing::ext; -using namespace OHOS::AppSpawn; - -class AppSpawnSocketTest : public testing::Test { -public: - static void SetUpTestCase(); - static void TearDownTestCase(); - void SetUp(); - void TearDown(); -}; - -void AppSpawnSocketTest::SetUpTestCase() -{} - -void AppSpawnSocketTest::TearDownTestCase() -{} - -void AppSpawnSocketTest::SetUp() -{} - -void AppSpawnSocketTest::TearDown() -{} - -/* - * Feature: AppSpawn - * Function: AppSpawnSocket - * SubFunction: CreateSocket & CloseSocket - * FunctionPoints: create socket and close socket. - * EnvConditions: mobile that can run ohos test framework - * CaseDescription: Verify if CreateSocket success then can close the socket. - */ -HWTEST(AppSpawnSocketTest, App_Spawn_Socket_001, TestSize.Level0) -{ - GTEST_LOG_(INFO) << "App_Spawn_Socket_001 start"; - - std::unique_ptr appSpawnSocket = std::make_unique("AppSpawnSocketTest"); - EXPECT_TRUE(appSpawnSocket); - - EXPECT_EQ(-1, appSpawnSocket->GetSocketFd()); - auto socketFd = appSpawnSocket->CreateSocket(); - EXPECT_LE(0, socketFd); - appSpawnSocket->CloseSocket(socketFd); - EXPECT_EQ(-1, socketFd); - - GTEST_LOG_(INFO) << "App_Spawn_Socket_001 end"; -} - -/* - * Feature: AppSpawn - * Function: AppSpawnSocket - * SubFunction: CloseSocket - * FunctionPoints: close the invalid socket fd. - * EnvConditions: mobile that can run ohos test framework - * CaseDescription: Verify the function CloseSocket don't close the socket which socket fd is invalid. - */ -HWTEST(AppSpawnSocketTest, App_Spawn_Socket_002, TestSize.Level0) -{ - GTEST_LOG_(INFO) << "App_Spawn_Socket_002 start"; - - std::unique_ptr appSpawnSocket = std::make_unique("AppSpawnSocketTest"); - EXPECT_TRUE(appSpawnSocket); - - int32_t socketFd = -2; - appSpawnSocket->CloseSocket(socketFd); - - EXPECT_EQ(-2, socketFd); - - GTEST_LOG_(INFO) << "App_Spawn_Socket_002 end"; -} - -/* - * Feature: AppSpawn - * Function: AppSpawnSocket - * SubFunction: PackSocketAddr - * FunctionPoints: check the invalid socket name - * EnvConditions: mobile that can run ohos test framework - * CaseDescription: Verify the function PackSocketAddr can check the empty socket name. - */ -HWTEST(AppSpawnSocketTest, App_Spawn_Socket_003, TestSize.Level0) -{ - GTEST_LOG_(INFO) << "App_Spawn_Socket_003 start"; - - std::unique_ptr appSpawnSocket = std::make_unique(""); - EXPECT_TRUE(appSpawnSocket); - - EXPECT_EQ(-EINVAL, appSpawnSocket->PackSocketAddr()); - - GTEST_LOG_(INFO) << "App_Spawn_Socket_003 end"; -} - -/* - * Feature: AppSpawn - * Function: AppSpawnSocket - * SubFunction: PackSocketAddr - * FunctionPoints: check the invalid socket name - * EnvConditions: mobile that can run ohos test framework - * CaseDescription: Verify the function PackSocketAddr can check too long socket name. - */ -HWTEST(AppSpawnSocketTest, App_Spawn_Socket_004, TestSize.Level0) -{ - GTEST_LOG_(INFO) << "App_Spawn_Socket_004 start"; - - std::string invalidSocketName = - "InvalidInvalidInvalidInvalidInvalidInvalidInvalidInvalid" - "InvalidInvalidInvalidInvalidInvalidInvalidInvalidInvalidInvalidInvalidInvalidInvalid"; - std::unique_ptr appSpawnSocket = std::make_unique(invalidSocketName.c_str()); - EXPECT_TRUE(appSpawnSocket); - - EXPECT_EQ(-1, appSpawnSocket->PackSocketAddr()); - - GTEST_LOG_(INFO) << "App_Spawn_Socket_004 end"; -} - -/* - * Feature: AppSpawn - * Function: AppSpawnSocket - * SubFunction: PackSocketAddr - * FunctionPoints: pack socket address - * EnvConditions: mobile that can run ohos test framework - * CaseDescription: Verify the function PackSocketAddr can pack the socket address with valid socket name. - */ -HWTEST(AppSpawnSocketTest, App_Spawn_Socket_005, TestSize.Level0) -{ - GTEST_LOG_(INFO) << "App_Spawn_Socket_005 start"; - - std::unique_ptr appSpawnSocket = std::make_unique("AppSpawnSocketTest"); - EXPECT_TRUE(appSpawnSocket); - - EXPECT_EQ(0, appSpawnSocket->PackSocketAddr()); - - GTEST_LOG_(INFO) << "App_Spawn_Socket_005 end"; -} - -/* - * Feature: AppSpawn - * Function: AppSpawnSocket - * SubFunction: ReadSocketMessage - * FunctionPoints: check params - * EnvConditions: mobile that can run ohos test framework - * CaseDescription: Verify the function ReadSocketMessage can check the invalid socket fd. - */ -HWTEST(AppSpawnSocketTest, App_Spawn_Socket_006, TestSize.Level0) -{ - GTEST_LOG_(INFO) << "App_Spawn_Socket_006 start"; - - std::unique_ptr appSpawnSocket = std::make_unique("AppSpawnSocketTest"); - EXPECT_TRUE(appSpawnSocket); - - int32_t socketFd = -1; - std::unique_ptr buff = std::make_unique(10); - EXPECT_TRUE(buff); - int32_t len = 10; - - EXPECT_EQ(-1, appSpawnSocket->ReadSocketMessage(socketFd, buff.get(), len)); - - GTEST_LOG_(INFO) << "App_Spawn_Socket_006 end"; -} - -/* - * Feature: AppSpawn - * Function: AppSpawnSocket - * SubFunction: ReadSocketMessage - * FunctionPoints: check params - * EnvConditions: mobile that can run ohos test framework - * CaseDescription: Verify the function ReadSocketMessage can check the invalid buffer pointer. - */ -HWTEST(AppSpawnSocketTest, App_Spawn_Socket_007, TestSize.Level0) -{ - GTEST_LOG_(INFO) << "App_Spawn_Socket_007 start"; - - std::unique_ptr appSpawnSocket = std::make_unique("AppSpawnSocketTest"); - EXPECT_TRUE(appSpawnSocket); - auto socketFd = appSpawnSocket->CreateSocket(); - std::unique_ptr buff = nullptr; - int32_t len = 10; - - EXPECT_EQ(-1, appSpawnSocket->ReadSocketMessage(socketFd, buff.get(), len)); - appSpawnSocket->CloseSocket(socketFd); - - GTEST_LOG_(INFO) << "App_Spawn_Socket_007 end"; -} - -/* - * Feature: AppSpawn - * Function: AppSpawnSocket - * SubFunction: ReadSocketMessage - * FunctionPoints: check params - * EnvConditions: mobile that can run ohos test framework - * CaseDescription: Verify the function ReadSocketMessage can check the buffer length is 0. - */ -HWTEST(AppSpawnSocketTest, App_Spawn_Socket_008, TestSize.Level0) -{ - GTEST_LOG_(INFO) << "App_Spawn_Socket_008 start"; - - std::unique_ptr appSpawnSocket = std::make_unique("AppSpawnSocketTest"); - EXPECT_TRUE(appSpawnSocket); - auto socketFd = appSpawnSocket->CreateSocket(); - std::unique_ptr buff = std::make_unique(10); - EXPECT_TRUE(buff); - int32_t len = 0; - - EXPECT_EQ(-1, appSpawnSocket->ReadSocketMessage(socketFd, buff.get(), len)); - appSpawnSocket->CloseSocket(socketFd); - - GTEST_LOG_(INFO) << "App_Spawn_Socket_008 end"; -} - -/* - * Feature: AppSpawn - * Function: AppSpawnSocket - * SubFunction: ReadSocketMessage - * FunctionPoints: check params - * EnvConditions: mobile that can run ohos test framework - * CaseDescription: Verify the function ReadSocketMessage can check the buffer length < 0. - */ -HWTEST(AppSpawnSocketTest, App_Spawn_Socket_009, TestSize.Level0) -{ - GTEST_LOG_(INFO) << "App_Spawn_Socket_009 start"; - - std::unique_ptr appSpawnSocket = std::make_unique("AppSpawnSocketTest"); - EXPECT_TRUE(appSpawnSocket); - auto socketFd = appSpawnSocket->CreateSocket(); - std::unique_ptr buff = std::make_unique(10); - EXPECT_TRUE(buff); - int32_t len = -1; - - EXPECT_EQ(-1, appSpawnSocket->ReadSocketMessage(socketFd, buff.get(), len)); - appSpawnSocket->CloseSocket(socketFd); - - GTEST_LOG_(INFO) << "App_Spawn_Socket_009 end"; -} - -/* - * Feature: AppSpawn - * Function: AppSpawnSocket - * SubFunction: ReadSocketMessage - * FunctionPoints: normal read data - * EnvConditions: mobile that can run ohos test framework - * CaseDescription: Verify the function ReadSocketMessage can read the normal message. - */ -HWTEST(AppSpawnSocketTest, App_Spawn_Socket_010, TestSize.Level0) -{ - GTEST_LOG_(INFO) << "App_Spawn_Socket_010 start"; - - std::unique_ptr appSpawnSocket = std::make_unique("AppSpawnSocketTest"); - EXPECT_TRUE(appSpawnSocket); - std::string content = "hiworld"; - int32_t len = content.length(); - EXPECT_TRUE(len); - std::unique_ptr buff = std::make_unique(len); - EXPECT_TRUE(buff); - int32_t fd[2] = {0, 0}; - - if (pipe(fd) == -1) { - GTEST_LOG_(WARNING) << "create pipe fail"; - return; - } - write(fd[1], content.c_str(), len); - - EXPECT_EQ(len, appSpawnSocket->ReadSocketMessage(fd[0], buff.get(), len)); - EXPECT_EQ(0, strncmp(content.c_str(), (const char *)(buff.get()), len)); - - // close pipe - close(fd[0]); - close(fd[1]); - - GTEST_LOG_(INFO) << "App_Spawn_Socket_010 end"; -} - -/* - * Feature: AppSpawn - * Function: AppSpawnSocket - * SubFunction: WriteSocketMessage - * FunctionPoints: check params - * EnvConditions: mobile that can run ohos test framework - * CaseDescription: Verify the function WriteSocketMessage can check the invalid socket fd. - */ -HWTEST(AppSpawnSocketTest, App_Spawn_Socket_011, TestSize.Level0) -{ - GTEST_LOG_(INFO) << "App_Spawn_Socket_011 start"; - - std::unique_ptr appSpawnSocket = std::make_unique("AppSpawnSocketTest"); - EXPECT_TRUE(appSpawnSocket); - int32_t socketFd = -1; - std::string buff = "hiworld"; - - EXPECT_EQ(-1, appSpawnSocket->WriteSocketMessage(socketFd, buff.c_str(), buff.length())); - - GTEST_LOG_(INFO) << "App_Spawn_Socket_011 end"; -} - -/* - * Feature: AppSpawn - * Function: AppSpawnSocket - * SubFunction: WriteSocketMessage - * FunctionPoints: check params - * EnvConditions: mobile that can run ohos test framework - * CaseDescription: Verify the function ReadSocketMessage can check the invalid buffer pointer. - */ -HWTEST(AppSpawnSocketTest, App_Spawn_Socket_012, TestSize.Level0) -{ - GTEST_LOG_(INFO) << "App_Spawn_Socket_012 start"; - - std::unique_ptr appSpawnSocket = std::make_unique("AppSpawnSocketTest"); - EXPECT_TRUE(appSpawnSocket); - auto socketFd = appSpawnSocket->CreateSocket(); - std::unique_ptr buff = nullptr; - int32_t len = 10; - - EXPECT_EQ(-1, appSpawnSocket->WriteSocketMessage(socketFd, buff.get(), len)); - appSpawnSocket->CloseSocket(socketFd); - - GTEST_LOG_(INFO) << "App_Spawn_Socket_012 end"; -} - -/* - * Feature: AppSpawn - * Function: AppSpawnSocket - * SubFunction: WriteSocketMessage - * FunctionPoints: check params - * EnvConditions: mobile that can run ohos test framework - * CaseDescription: Verify the function ReadSocketMessage can check the buffer length is 0. - */ -HWTEST(AppSpawnSocketTest, App_Spawn_Socket_013, TestSize.Level0) -{ - GTEST_LOG_(INFO) << "App_Spawn_Socket_013 start"; - - std::unique_ptr appSpawnSocket = std::make_unique("AppSpawnSocketTest"); - EXPECT_TRUE(appSpawnSocket); - auto socketFd = appSpawnSocket->CreateSocket(); - std::string buff = "hiworld"; - int32_t len = 0; - - EXPECT_EQ(-1, appSpawnSocket->WriteSocketMessage(socketFd, buff.c_str(), len)); - appSpawnSocket->CloseSocket(socketFd); - - GTEST_LOG_(INFO) << "App_Spawn_Socket_013 end"; -} - -/* - * Feature: AppSpawn - * Function: AppSpawnSocket - * SubFunction: WriteSocketMessage - * FunctionPoints: check params - * EnvConditions: mobile that can run ohos test framework - * CaseDescription: Verify the function ReadSocketMessage can check the buffer length < 0. - */ -HWTEST(AppSpawnSocketTest, App_Spawn_Socket_014, TestSize.Level0) -{ - GTEST_LOG_(INFO) << "App_Spawn_Socket_014 start"; - - std::unique_ptr appSpawnSocket = std::make_unique("AppSpawnSocketTest"); - EXPECT_TRUE(appSpawnSocket); - auto socketFd = appSpawnSocket->CreateSocket(); - std::string buff = "hiworld"; - int32_t len = -1; - - EXPECT_EQ(-1, appSpawnSocket->WriteSocketMessage(socketFd, buff.c_str(), len)); - appSpawnSocket->CloseSocket(socketFd); - - GTEST_LOG_(INFO) << "App_Spawn_Socket_014 end"; -} - -/* - * Feature: AppSpawn - * Function: AppSpawnSocket - * SubFunction: WriteSocketMessage - * FunctionPoints: normal write data - * EnvConditions: mobile that can run ohos test framework - * CaseDescription: Verify the function WriteSocketMessage can write the normal message. - */ -HWTEST(AppSpawnSocketTest, App_Spawn_Socket_015, TestSize.Level0) -{ - GTEST_LOG_(INFO) << "App_Spawn_Socket_015 start"; - - std::unique_ptr appSpawnSocket = std::make_unique("AppSpawnSocketTest"); - EXPECT_TRUE(appSpawnSocket); - std::string content = "hiworld"; - int32_t len = content.length(); - EXPECT_TRUE(len); - std::unique_ptr buff = std::make_unique(len); - EXPECT_TRUE(buff); - int32_t fd[2] = {0, 0}; - - if (pipe(fd) == -1) { - GTEST_LOG_(WARNING) << "create pipe fail"; - return; - } - - EXPECT_EQ(len, appSpawnSocket->WriteSocketMessage(fd[1], content.c_str(), len)); - read(fd[0], buff.get(), len); - EXPECT_EQ(0, strncmp(content.c_str(), (const char *)(buff.get()), len)); - - // close pipe - close(fd[0]); - close(fd[1]); - - GTEST_LOG_(INFO) << "App_Spawn_Socket_015 end"; -} +/* + * Copyright (c) 2021-2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include + +#include +#include + +// redefine private and protected since testcase need to invoke and test private function +#define private public +#define protected public +#include "appspawn_socket.h" +#undef private +#undef protected + +#include "securec.h" + +using namespace testing; +using namespace testing::ext; +using namespace OHOS::AppSpawn; + +class AppSpawnSocketTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp(); + void TearDown(); +}; + +void AppSpawnSocketTest::SetUpTestCase() +{} + +void AppSpawnSocketTest::TearDownTestCase() +{} + +void AppSpawnSocketTest::SetUp() +{} + +void AppSpawnSocketTest::TearDown() +{} + +/* + * Feature: AppSpawn + * Function: AppSpawnSocket + * SubFunction: CreateSocket & CloseSocket + * FunctionPoints: create socket and close socket. + * EnvConditions: mobile that can run ohos test framework + * CaseDescription: Verify if CreateSocket success then can close the socket. + */ +HWTEST(AppSpawnSocketTest, App_Spawn_Socket_001, TestSize.Level0) +{ + GTEST_LOG_(INFO) << "App_Spawn_Socket_001 start"; + + std::unique_ptr appSpawnSocket = std::make_unique("AppSpawnSocketTest"); + EXPECT_TRUE(appSpawnSocket); + + EXPECT_EQ(-1, appSpawnSocket->GetSocketFd()); + auto socketFd = appSpawnSocket->CreateSocket(); + EXPECT_LE(0, socketFd); + appSpawnSocket->CloseSocket(socketFd); + EXPECT_EQ(-1, socketFd); + + GTEST_LOG_(INFO) << "App_Spawn_Socket_001 end"; +} + +/* + * Feature: AppSpawn + * Function: AppSpawnSocket + * SubFunction: CloseSocket + * FunctionPoints: close the invalid socket fd. + * EnvConditions: mobile that can run ohos test framework + * CaseDescription: Verify the function CloseSocket don't close the socket which socket fd is invalid. + */ +HWTEST(AppSpawnSocketTest, App_Spawn_Socket_002, TestSize.Level0) +{ + GTEST_LOG_(INFO) << "App_Spawn_Socket_002 start"; + + std::unique_ptr appSpawnSocket = std::make_unique("AppSpawnSocketTest"); + EXPECT_TRUE(appSpawnSocket); + + int32_t socketFd = -2; + appSpawnSocket->CloseSocket(socketFd); + + EXPECT_EQ(-2, socketFd); + + GTEST_LOG_(INFO) << "App_Spawn_Socket_002 end"; +} + +/* + * Feature: AppSpawn + * Function: AppSpawnSocket + * SubFunction: PackSocketAddr + * FunctionPoints: check the invalid socket name + * EnvConditions: mobile that can run ohos test framework + * CaseDescription: Verify the function PackSocketAddr can check the empty socket name. + */ +HWTEST(AppSpawnSocketTest, App_Spawn_Socket_003, TestSize.Level0) +{ + GTEST_LOG_(INFO) << "App_Spawn_Socket_003 start"; + + std::unique_ptr appSpawnSocket = std::make_unique(""); + EXPECT_TRUE(appSpawnSocket); + + EXPECT_EQ(-EINVAL, appSpawnSocket->PackSocketAddr()); + + GTEST_LOG_(INFO) << "App_Spawn_Socket_003 end"; +} + +/* + * Feature: AppSpawn + * Function: AppSpawnSocket + * SubFunction: PackSocketAddr + * FunctionPoints: check the invalid socket name + * EnvConditions: mobile that can run ohos test framework + * CaseDescription: Verify the function PackSocketAddr can check too long socket name. + */ +HWTEST(AppSpawnSocketTest, App_Spawn_Socket_004, TestSize.Level0) +{ + GTEST_LOG_(INFO) << "App_Spawn_Socket_004 start"; + + std::string invalidSocketName = + "InvalidInvalidInvalidInvalidInvalidInvalidInvalidInvalid" + "InvalidInvalidInvalidInvalidInvalidInvalidInvalidInvalidInvalidInvalidInvalidInvalid"; + std::unique_ptr appSpawnSocket = std::make_unique(invalidSocketName.c_str()); + EXPECT_TRUE(appSpawnSocket); + + EXPECT_EQ(-1, appSpawnSocket->PackSocketAddr()); + + GTEST_LOG_(INFO) << "App_Spawn_Socket_004 end"; +} + +/* + * Feature: AppSpawn + * Function: AppSpawnSocket + * SubFunction: PackSocketAddr + * FunctionPoints: pack socket address + * EnvConditions: mobile that can run ohos test framework + * CaseDescription: Verify the function PackSocketAddr can pack the socket address with valid socket name. + */ +HWTEST(AppSpawnSocketTest, App_Spawn_Socket_005, TestSize.Level0) +{ + GTEST_LOG_(INFO) << "App_Spawn_Socket_005 start"; + + std::unique_ptr appSpawnSocket = std::make_unique("AppSpawnSocketTest"); + EXPECT_TRUE(appSpawnSocket); + + EXPECT_EQ(0, appSpawnSocket->PackSocketAddr()); + + GTEST_LOG_(INFO) << "App_Spawn_Socket_005 end"; +} + +/* + * Feature: AppSpawn + * Function: AppSpawnSocket + * SubFunction: ReadSocketMessage + * FunctionPoints: check params + * EnvConditions: mobile that can run ohos test framework + * CaseDescription: Verify the function ReadSocketMessage can check the invalid socket fd. + */ +HWTEST(AppSpawnSocketTest, App_Spawn_Socket_006, TestSize.Level0) +{ + GTEST_LOG_(INFO) << "App_Spawn_Socket_006 start"; + + std::unique_ptr appSpawnSocket = std::make_unique("AppSpawnSocketTest"); + EXPECT_TRUE(appSpawnSocket); + + int32_t socketFd = -1; + std::unique_ptr buff = std::make_unique(10); + EXPECT_TRUE(buff); + int32_t len = 10; + + EXPECT_EQ(-1, appSpawnSocket->ReadSocketMessage(socketFd, buff.get(), len)); + + GTEST_LOG_(INFO) << "App_Spawn_Socket_006 end"; +} + +/* + * Feature: AppSpawn + * Function: AppSpawnSocket + * SubFunction: ReadSocketMessage + * FunctionPoints: check params + * EnvConditions: mobile that can run ohos test framework + * CaseDescription: Verify the function ReadSocketMessage can check the invalid buffer pointer. + */ +HWTEST(AppSpawnSocketTest, App_Spawn_Socket_007, TestSize.Level0) +{ + GTEST_LOG_(INFO) << "App_Spawn_Socket_007 start"; + + std::unique_ptr appSpawnSocket = std::make_unique("AppSpawnSocketTest"); + EXPECT_TRUE(appSpawnSocket); + auto socketFd = appSpawnSocket->CreateSocket(); + std::unique_ptr buff = nullptr; + int32_t len = 10; + + EXPECT_EQ(-1, appSpawnSocket->ReadSocketMessage(socketFd, buff.get(), len)); + appSpawnSocket->CloseSocket(socketFd); + + GTEST_LOG_(INFO) << "App_Spawn_Socket_007 end"; +} + +/* + * Feature: AppSpawn + * Function: AppSpawnSocket + * SubFunction: ReadSocketMessage + * FunctionPoints: check params + * EnvConditions: mobile that can run ohos test framework + * CaseDescription: Verify the function ReadSocketMessage can check the buffer length is 0. + */ +HWTEST(AppSpawnSocketTest, App_Spawn_Socket_008, TestSize.Level0) +{ + GTEST_LOG_(INFO) << "App_Spawn_Socket_008 start"; + + std::unique_ptr appSpawnSocket = std::make_unique("AppSpawnSocketTest"); + EXPECT_TRUE(appSpawnSocket); + auto socketFd = appSpawnSocket->CreateSocket(); + std::unique_ptr buff = std::make_unique(10); + EXPECT_TRUE(buff); + int32_t len = 0; + + EXPECT_EQ(-1, appSpawnSocket->ReadSocketMessage(socketFd, buff.get(), len)); + appSpawnSocket->CloseSocket(socketFd); + + GTEST_LOG_(INFO) << "App_Spawn_Socket_008 end"; +} + +/* + * Feature: AppSpawn + * Function: AppSpawnSocket + * SubFunction: ReadSocketMessage + * FunctionPoints: check params + * EnvConditions: mobile that can run ohos test framework + * CaseDescription: Verify the function ReadSocketMessage can check the buffer length < 0. + */ +HWTEST(AppSpawnSocketTest, App_Spawn_Socket_009, TestSize.Level0) +{ + GTEST_LOG_(INFO) << "App_Spawn_Socket_009 start"; + + std::unique_ptr appSpawnSocket = std::make_unique("AppSpawnSocketTest"); + EXPECT_TRUE(appSpawnSocket); + auto socketFd = appSpawnSocket->CreateSocket(); + std::unique_ptr buff = std::make_unique(10); + EXPECT_TRUE(buff); + int32_t len = -1; + + EXPECT_EQ(-1, appSpawnSocket->ReadSocketMessage(socketFd, buff.get(), len)); + appSpawnSocket->CloseSocket(socketFd); + + GTEST_LOG_(INFO) << "App_Spawn_Socket_009 end"; +} + +/* + * Feature: AppSpawn + * Function: AppSpawnSocket + * SubFunction: ReadSocketMessage + * FunctionPoints: normal read data + * EnvConditions: mobile that can run ohos test framework + * CaseDescription: Verify the function ReadSocketMessage can read the normal message. + */ +HWTEST(AppSpawnSocketTest, App_Spawn_Socket_010, TestSize.Level0) +{ + GTEST_LOG_(INFO) << "App_Spawn_Socket_010 start"; + + std::unique_ptr appSpawnSocket = std::make_unique("AppSpawnSocketTest"); + EXPECT_TRUE(appSpawnSocket); + std::string content = "hiworld"; + int32_t len = content.length(); + EXPECT_TRUE(len); + std::unique_ptr buff = std::make_unique(len); + EXPECT_TRUE(buff); + int32_t fd[2] = {0, 0}; + + if (pipe(fd) == -1) { + GTEST_LOG_(WARNING) << "create pipe fail"; + return; + } + write(fd[1], content.c_str(), len); + + EXPECT_EQ(len, appSpawnSocket->ReadSocketMessage(fd[0], buff.get(), len)); + EXPECT_EQ(0, strncmp(content.c_str(), (const char *)(buff.get()), len)); + + // close pipe + close(fd[0]); + close(fd[1]); + + GTEST_LOG_(INFO) << "App_Spawn_Socket_010 end"; +} + +/* + * Feature: AppSpawn + * Function: AppSpawnSocket + * SubFunction: WriteSocketMessage + * FunctionPoints: check params + * EnvConditions: mobile that can run ohos test framework + * CaseDescription: Verify the function WriteSocketMessage can check the invalid socket fd. + */ +HWTEST(AppSpawnSocketTest, App_Spawn_Socket_011, TestSize.Level0) +{ + GTEST_LOG_(INFO) << "App_Spawn_Socket_011 start"; + + std::unique_ptr appSpawnSocket = std::make_unique("AppSpawnSocketTest"); + EXPECT_TRUE(appSpawnSocket); + int32_t socketFd = -1; + std::string buff = "hiworld"; + + EXPECT_EQ(-1, appSpawnSocket->WriteSocketMessage(socketFd, buff.c_str(), buff.length())); + + GTEST_LOG_(INFO) << "App_Spawn_Socket_011 end"; +} + +/* + * Feature: AppSpawn + * Function: AppSpawnSocket + * SubFunction: WriteSocketMessage + * FunctionPoints: check params + * EnvConditions: mobile that can run ohos test framework + * CaseDescription: Verify the function ReadSocketMessage can check the invalid buffer pointer. + */ +HWTEST(AppSpawnSocketTest, App_Spawn_Socket_012, TestSize.Level0) +{ + GTEST_LOG_(INFO) << "App_Spawn_Socket_012 start"; + + std::unique_ptr appSpawnSocket = std::make_unique("AppSpawnSocketTest"); + EXPECT_TRUE(appSpawnSocket); + auto socketFd = appSpawnSocket->CreateSocket(); + std::unique_ptr buff = nullptr; + int32_t len = 10; + + EXPECT_EQ(-1, appSpawnSocket->WriteSocketMessage(socketFd, buff.get(), len)); + appSpawnSocket->CloseSocket(socketFd); + + GTEST_LOG_(INFO) << "App_Spawn_Socket_012 end"; +} + +/* + * Feature: AppSpawn + * Function: AppSpawnSocket + * SubFunction: WriteSocketMessage + * FunctionPoints: check params + * EnvConditions: mobile that can run ohos test framework + * CaseDescription: Verify the function ReadSocketMessage can check the buffer length is 0. + */ +HWTEST(AppSpawnSocketTest, App_Spawn_Socket_013, TestSize.Level0) +{ + GTEST_LOG_(INFO) << "App_Spawn_Socket_013 start"; + + std::unique_ptr appSpawnSocket = std::make_unique("AppSpawnSocketTest"); + EXPECT_TRUE(appSpawnSocket); + auto socketFd = appSpawnSocket->CreateSocket(); + std::string buff = "hiworld"; + int32_t len = 0; + + EXPECT_EQ(-1, appSpawnSocket->WriteSocketMessage(socketFd, buff.c_str(), len)); + appSpawnSocket->CloseSocket(socketFd); + + GTEST_LOG_(INFO) << "App_Spawn_Socket_013 end"; +} + +/* + * Feature: AppSpawn + * Function: AppSpawnSocket + * SubFunction: WriteSocketMessage + * FunctionPoints: check params + * EnvConditions: mobile that can run ohos test framework + * CaseDescription: Verify the function ReadSocketMessage can check the buffer length < 0. + */ +HWTEST(AppSpawnSocketTest, App_Spawn_Socket_014, TestSize.Level0) +{ + GTEST_LOG_(INFO) << "App_Spawn_Socket_014 start"; + + std::unique_ptr appSpawnSocket = std::make_unique("AppSpawnSocketTest"); + EXPECT_TRUE(appSpawnSocket); + auto socketFd = appSpawnSocket->CreateSocket(); + std::string buff = "hiworld"; + int32_t len = -1; + + EXPECT_EQ(-1, appSpawnSocket->WriteSocketMessage(socketFd, buff.c_str(), len)); + appSpawnSocket->CloseSocket(socketFd); + + GTEST_LOG_(INFO) << "App_Spawn_Socket_014 end"; +} + +/* + * Feature: AppSpawn + * Function: AppSpawnSocket + * SubFunction: WriteSocketMessage + * FunctionPoints: normal write data + * EnvConditions: mobile that can run ohos test framework + * CaseDescription: Verify the function WriteSocketMessage can write the normal message. + */ +HWTEST(AppSpawnSocketTest, App_Spawn_Socket_015, TestSize.Level0) +{ + GTEST_LOG_(INFO) << "App_Spawn_Socket_015 start"; + + std::unique_ptr appSpawnSocket = std::make_unique("AppSpawnSocketTest"); + EXPECT_TRUE(appSpawnSocket); + std::string content = "hiworld"; + int32_t len = content.length(); + EXPECT_TRUE(len); + std::unique_ptr buff = std::make_unique(len); + EXPECT_TRUE(buff); + int32_t fd[2] = {0, 0}; + + if (pipe(fd) == -1) { + GTEST_LOG_(WARNING) << "create pipe fail"; + return; + } + + EXPECT_EQ(len, appSpawnSocket->WriteSocketMessage(fd[1], content.c_str(), len)); + read(fd[0], buff.get(), len); + EXPECT_EQ(0, strncmp(content.c_str(), (const char *)(buff.get()), len)); + + // close pipe + close(fd[0]); + close(fd[1]); + + GTEST_LOG_(INFO) << "App_Spawn_Socket_015 end"; +} diff --git a/test/unittest/client_socket_test/client_socket_test.cpp b/test/unittest/client_socket_test/client_socket_test.cpp index db9741d159d73160507d2320155208384973e42b..5a10c9e8d4202c62e9fac084a83ab230aeca9ecf 100644 --- a/test/unittest/client_socket_test/client_socket_test.cpp +++ b/test/unittest/client_socket_test/client_socket_test.cpp @@ -1,298 +1,298 @@ -/* - * Copyright (c) 2021-2022 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include - -#include - -// redefine private and protected since testcase need to invoke and test private function -#define private public -#define protected public -#include "client_socket.h" -#undef private -#undef protected - -#include "securec.h" - -using namespace testing; -using namespace testing::ext; -using namespace OHOS::AppSpawn; - -class ClientSocketTest : public testing::Test { -public: - static void SetUpTestCase(); - static void TearDownTestCase(); - void SetUp(); - void TearDown(); -}; - -void ClientSocketTest::SetUpTestCase() -{} - -void ClientSocketTest::TearDownTestCase() -{} - -void ClientSocketTest::SetUp() -{} - -void ClientSocketTest::TearDown() -{} - -/* - * Feature: AppSpawn - * Function: ClientSocket - * SubFunction: CreateClient & ConnectSocket - * FunctionPoints: create client socket - * EnvConditions: mobile that can run ohos test framework - * CaseDescription: Verify although the client socket created success but don't create the server socket, the connect - * socket still fail. - */ -HWTEST(ClientSocketTest, Client_Socket_001, TestSize.Level0) -{ - GTEST_LOG_(INFO) << "Client_Socket_001 start"; - - std::unique_ptr clientSocket = std::make_unique("ClientSocketTest"); - - EXPECT_EQ(-1, clientSocket->GetSocketFd()); - EXPECT_EQ(0, clientSocket->CreateClient()); - int32_t socketFd = clientSocket->GetSocketFd(); - EXPECT_EQ(0, clientSocket->CreateClient()); - EXPECT_EQ(socketFd, clientSocket->GetSocketFd()); - EXPECT_EQ(-1, clientSocket->ConnectSocket()); - - GTEST_LOG_(INFO) << "Client_Socket_001 end"; -} - -/* - * Feature: AppSpawn - * Function: ClientSocket - * SubFunction: ConnectSocket - * FunctionPoints: connect socket - * EnvConditions: mobile that can run ohos test framework - * CaseDescription: Verify connect socket fail when don't create client socket. - */ -HWTEST(ClientSocketTest, Client_Socket_002, TestSize.Level0) -{ - GTEST_LOG_(INFO) << "Client_Socket_002 start"; - - std::unique_ptr clientSocket = std::make_unique("ClientSocketTest"); - - EXPECT_EQ(-1, clientSocket->GetSocketFd()); - EXPECT_EQ(-1, clientSocket->ConnectSocket()); - - GTEST_LOG_(INFO) << "Client_Socket_002 end"; -} - -/* - * Feature: AppSpawn - * Function: ClientSocket - * SubFunction: WriteSocketMessage - * FunctionPoints: write message - * EnvConditions: mobile that can run ohos test framework - * CaseDescription: Verify write message fail when don't create client socket. - */ -HWTEST(ClientSocketTest, Client_Socket_003, TestSize.Level0) -{ - GTEST_LOG_(INFO) << "Client_Socket_003 start"; - - std::unique_ptr clientSocket = std::make_unique("ClientSocketTest"); - std::string buff = "hiworld"; - - EXPECT_EQ(-1, clientSocket->WriteSocketMessage(buff.c_str(), buff.length())); - - GTEST_LOG_(INFO) << "Client_Socket_003 end"; -} - -/* - * Feature: AppSpawn - * Function: ClientSocket - * SubFunction: WriteSocketMessage - * FunctionPoints: check params - * EnvConditions: mobile that can run ohos test framework - * CaseDescription: Verify the function WriteSocketMessage can check the invalid buffer pointer. - */ -HWTEST(ClientSocketTest, Client_Socket_004, TestSize.Level0) -{ - GTEST_LOG_(INFO) << "Client_Socket_004 start"; - - std::unique_ptr clientSocket = std::make_unique("ClientSocketTest"); - std::unique_ptr buff = nullptr; - uint32_t len = 10; - - EXPECT_EQ(0, clientSocket->CreateClient()); - EXPECT_LE(0, clientSocket->GetSocketFd()); - EXPECT_EQ(-1, clientSocket->WriteSocketMessage(buff.get(), len)); - - GTEST_LOG_(INFO) << "Client_Socket_004 end"; -} - -/* - * Feature: AppSpawn - * Function: ClientSocket - * SubFunction: WriteSocketMessage - * FunctionPoints: check params - * EnvConditions: mobile that can run ohos test framework - * CaseDescription: Verify the function WriteSocketMessage can check the buffer length is 0. - */ -HWTEST(ClientSocketTest, Client_Socket_005, TestSize.Level0) -{ - GTEST_LOG_(INFO) << "Client_Socket_005 start"; - - std::unique_ptr clientSocket = std::make_unique("ClientSocketTest"); - std::string buff = "hiworld"; - uint32_t len = 0; - - EXPECT_EQ(0, clientSocket->CreateClient()); - EXPECT_LE(0, clientSocket->GetSocketFd()); - EXPECT_EQ(-1, clientSocket->WriteSocketMessage(buff.c_str(), len)); - - GTEST_LOG_(INFO) << "Client_Socket_005 end"; -} - -/* - * Feature: AppSpawn - * Function: ClientSocket - * SubFunction: WriteSocketMessage - * FunctionPoints: check params - * EnvConditions: mobile that can run ohos test framework - * CaseDescription: Verify the function WriteSocketMessage can check the buffer length < 0. - */ -HWTEST(ClientSocketTest, Client_Socket_006, TestSize.Level0) -{ - GTEST_LOG_(INFO) << "Client_Socket_006 start"; - - std::unique_ptr clientSocket = std::make_unique("ClientSocketTest"); - std::string buff = "hiworld"; - uint32_t len = -1; - - EXPECT_EQ(0, clientSocket->CreateClient()); - EXPECT_LE(0, clientSocket->GetSocketFd()); - EXPECT_EQ(-1, clientSocket->WriteSocketMessage(buff.c_str(), len)); - - GTEST_LOG_(INFO) << "Client_Socket_006 end"; -} - -/* - * Feature: AppSpawn - * Function: ClientSocket - * SubFunction: ReadSocketMessage - * FunctionPoints: read message - * EnvConditions: mobile that can run ohos test framework - * CaseDescription: Verify read message fail when don't create client socket. - */ -HWTEST(ClientSocketTest, Client_Socket_007, TestSize.Level0) -{ - GTEST_LOG_(INFO) << "Client_Socket_007 start"; - - std::unique_ptr clientSocket = std::make_unique("ClientSocketTest"); - int32_t len = 10; - std::unique_ptr buff = std::make_unique(len); - - EXPECT_EQ(-1, clientSocket->ReadSocketMessage(buff.get(), len)); - - GTEST_LOG_(INFO) << "Client_Socket_007 end"; -} - -/* - * Feature: AppSpawn - * Function: ClientSocket - * SubFunction: ReadSocketMessage - * FunctionPoints: check params - * EnvConditions: mobile that can run ohos test framework - * CaseDescription: Verify the function WriteSocketMessage can check the invalid buffer pointer. - */ -HWTEST(ClientSocketTest, Client_Socket_008, TestSize.Level0) -{ - GTEST_LOG_(INFO) << "Client_Socket_008 start"; - - std::unique_ptr clientSocket = std::make_unique("ClientSocketTest"); - int32_t len = 10; - std::unique_ptr buff = nullptr; - - EXPECT_EQ(0, clientSocket->CreateClient()); - EXPECT_LE(0, clientSocket->GetSocketFd()); - EXPECT_EQ(-1, clientSocket->ReadSocketMessage(buff.get(), len)); - - GTEST_LOG_(INFO) << "Client_Socket_008 end"; -} - -/* - * Feature: AppSpawn - * Function: ClientSocket - * SubFunction: ReadSocketMessage - * FunctionPoints: check params - * EnvConditions: mobile that can run ohos test framework - * CaseDescription: Verify the function WriteSocketMessage can check the buffer length is 0. - */ -HWTEST(ClientSocketTest, Client_Socket_009, TestSize.Level0) -{ - GTEST_LOG_(INFO) << "Client_Socket_009 start"; - - std::unique_ptr clientSocket = std::make_unique("ClientSocketTest"); - int32_t len = 0; - std::unique_ptr buff = std::make_unique(10); - - EXPECT_EQ(0, clientSocket->CreateClient()); - EXPECT_LE(0, clientSocket->GetSocketFd()); - EXPECT_EQ(-1, clientSocket->ReadSocketMessage(buff.get(), len)); - - GTEST_LOG_(INFO) << "Client_Socket_009 end"; -} - -/* - * Feature: AppSpawn - * Function: ClientSocket - * SubFunction: ReadSocketMessage - * FunctionPoints: check params - * EnvConditions: mobile that can run ohos test framework - * CaseDescription: Verify the function WriteSocketMessage can check the buffer length < 0. - */ -HWTEST(ClientSocketTest, Client_Socket_010, TestSize.Level0) -{ - GTEST_LOG_(INFO) << "Client_Socket_010 start"; - - std::unique_ptr clientSocket = std::make_unique("ClientSocketTest"); - int32_t len = -1; - std::unique_ptr buff = std::make_unique(10); - - EXPECT_EQ(0, clientSocket->CreateClient()); - EXPECT_LE(0, clientSocket->GetSocketFd()); - EXPECT_EQ(-1, clientSocket->ReadSocketMessage(buff.get(), len)); - - GTEST_LOG_(INFO) << "Client_Socket_010 end"; -} - -/* - * Feature: AppSpawn - * Function: ClientSocket - * SubFunction: GetSocketFd - * FunctionPoints: close the socket - * EnvConditions: mobile that can run ohos test framework - * CaseDescription: Verify the function CloseClient can close the socket which socket fd has created. - */ -HWTEST(ClientSocketTest, Client_Socket_011, TestSize.Level0) -{ - GTEST_LOG_(INFO) << "Client_Socket_011 start"; - - std::unique_ptr clientSocket = std::make_unique("ClientSocketTest"); - - EXPECT_EQ(-1, clientSocket->GetSocketFd()); - EXPECT_EQ(0, clientSocket->CreateClient()); - EXPECT_LE(0, clientSocket->GetSocketFd()); - clientSocket->CloseClient(); - EXPECT_EQ(-1, clientSocket->GetSocketFd()); - - GTEST_LOG_(INFO) << "Client_Socket_011 end"; -} +/* + * Copyright (c) 2021-2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include + +#include + +// redefine private and protected since testcase need to invoke and test private function +#define private public +#define protected public +#include "client_socket.h" +#undef private +#undef protected + +#include "securec.h" + +using namespace testing; +using namespace testing::ext; +using namespace OHOS::AppSpawn; + +class ClientSocketTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp(); + void TearDown(); +}; + +void ClientSocketTest::SetUpTestCase() +{} + +void ClientSocketTest::TearDownTestCase() +{} + +void ClientSocketTest::SetUp() +{} + +void ClientSocketTest::TearDown() +{} + +/* + * Feature: AppSpawn + * Function: ClientSocket + * SubFunction: CreateClient & ConnectSocket + * FunctionPoints: create client socket + * EnvConditions: mobile that can run ohos test framework + * CaseDescription: Verify although the client socket created success but don't create the server socket, the connect + * socket still fail. + */ +HWTEST(ClientSocketTest, Client_Socket_001, TestSize.Level0) +{ + GTEST_LOG_(INFO) << "Client_Socket_001 start"; + + std::unique_ptr clientSocket = std::make_unique("ClientSocketTest"); + + EXPECT_EQ(-1, clientSocket->GetSocketFd()); + EXPECT_EQ(0, clientSocket->CreateClient()); + int32_t socketFd = clientSocket->GetSocketFd(); + EXPECT_EQ(0, clientSocket->CreateClient()); + EXPECT_EQ(socketFd, clientSocket->GetSocketFd()); + EXPECT_EQ(-1, clientSocket->ConnectSocket()); + + GTEST_LOG_(INFO) << "Client_Socket_001 end"; +} + +/* + * Feature: AppSpawn + * Function: ClientSocket + * SubFunction: ConnectSocket + * FunctionPoints: connect socket + * EnvConditions: mobile that can run ohos test framework + * CaseDescription: Verify connect socket fail when don't create client socket. + */ +HWTEST(ClientSocketTest, Client_Socket_002, TestSize.Level0) +{ + GTEST_LOG_(INFO) << "Client_Socket_002 start"; + + std::unique_ptr clientSocket = std::make_unique("ClientSocketTest"); + + EXPECT_EQ(-1, clientSocket->GetSocketFd()); + EXPECT_EQ(-1, clientSocket->ConnectSocket()); + + GTEST_LOG_(INFO) << "Client_Socket_002 end"; +} + +/* + * Feature: AppSpawn + * Function: ClientSocket + * SubFunction: WriteSocketMessage + * FunctionPoints: write message + * EnvConditions: mobile that can run ohos test framework + * CaseDescription: Verify write message fail when don't create client socket. + */ +HWTEST(ClientSocketTest, Client_Socket_003, TestSize.Level0) +{ + GTEST_LOG_(INFO) << "Client_Socket_003 start"; + + std::unique_ptr clientSocket = std::make_unique("ClientSocketTest"); + std::string buff = "hiworld"; + + EXPECT_EQ(-1, clientSocket->WriteSocketMessage(buff.c_str(), buff.length())); + + GTEST_LOG_(INFO) << "Client_Socket_003 end"; +} + +/* + * Feature: AppSpawn + * Function: ClientSocket + * SubFunction: WriteSocketMessage + * FunctionPoints: check params + * EnvConditions: mobile that can run ohos test framework + * CaseDescription: Verify the function WriteSocketMessage can check the invalid buffer pointer. + */ +HWTEST(ClientSocketTest, Client_Socket_004, TestSize.Level0) +{ + GTEST_LOG_(INFO) << "Client_Socket_004 start"; + + std::unique_ptr clientSocket = std::make_unique("ClientSocketTest"); + std::unique_ptr buff = nullptr; + uint32_t len = 10; + + EXPECT_EQ(0, clientSocket->CreateClient()); + EXPECT_LE(0, clientSocket->GetSocketFd()); + EXPECT_EQ(-1, clientSocket->WriteSocketMessage(buff.get(), len)); + + GTEST_LOG_(INFO) << "Client_Socket_004 end"; +} + +/* + * Feature: AppSpawn + * Function: ClientSocket + * SubFunction: WriteSocketMessage + * FunctionPoints: check params + * EnvConditions: mobile that can run ohos test framework + * CaseDescription: Verify the function WriteSocketMessage can check the buffer length is 0. + */ +HWTEST(ClientSocketTest, Client_Socket_005, TestSize.Level0) +{ + GTEST_LOG_(INFO) << "Client_Socket_005 start"; + + std::unique_ptr clientSocket = std::make_unique("ClientSocketTest"); + std::string buff = "hiworld"; + uint32_t len = 0; + + EXPECT_EQ(0, clientSocket->CreateClient()); + EXPECT_LE(0, clientSocket->GetSocketFd()); + EXPECT_EQ(-1, clientSocket->WriteSocketMessage(buff.c_str(), len)); + + GTEST_LOG_(INFO) << "Client_Socket_005 end"; +} + +/* + * Feature: AppSpawn + * Function: ClientSocket + * SubFunction: WriteSocketMessage + * FunctionPoints: check params + * EnvConditions: mobile that can run ohos test framework + * CaseDescription: Verify the function WriteSocketMessage can check the buffer length < 0. + */ +HWTEST(ClientSocketTest, Client_Socket_006, TestSize.Level0) +{ + GTEST_LOG_(INFO) << "Client_Socket_006 start"; + + std::unique_ptr clientSocket = std::make_unique("ClientSocketTest"); + std::string buff = "hiworld"; + uint32_t len = -1; + + EXPECT_EQ(0, clientSocket->CreateClient()); + EXPECT_LE(0, clientSocket->GetSocketFd()); + EXPECT_EQ(-1, clientSocket->WriteSocketMessage(buff.c_str(), len)); + + GTEST_LOG_(INFO) << "Client_Socket_006 end"; +} + +/* + * Feature: AppSpawn + * Function: ClientSocket + * SubFunction: ReadSocketMessage + * FunctionPoints: read message + * EnvConditions: mobile that can run ohos test framework + * CaseDescription: Verify read message fail when don't create client socket. + */ +HWTEST(ClientSocketTest, Client_Socket_007, TestSize.Level0) +{ + GTEST_LOG_(INFO) << "Client_Socket_007 start"; + + std::unique_ptr clientSocket = std::make_unique("ClientSocketTest"); + int32_t len = 10; + std::unique_ptr buff = std::make_unique(len); + + EXPECT_EQ(-1, clientSocket->ReadSocketMessage(buff.get(), len)); + + GTEST_LOG_(INFO) << "Client_Socket_007 end"; +} + +/* + * Feature: AppSpawn + * Function: ClientSocket + * SubFunction: ReadSocketMessage + * FunctionPoints: check params + * EnvConditions: mobile that can run ohos test framework + * CaseDescription: Verify the function WriteSocketMessage can check the invalid buffer pointer. + */ +HWTEST(ClientSocketTest, Client_Socket_008, TestSize.Level0) +{ + GTEST_LOG_(INFO) << "Client_Socket_008 start"; + + std::unique_ptr clientSocket = std::make_unique("ClientSocketTest"); + int32_t len = 10; + std::unique_ptr buff = nullptr; + + EXPECT_EQ(0, clientSocket->CreateClient()); + EXPECT_LE(0, clientSocket->GetSocketFd()); + EXPECT_EQ(-1, clientSocket->ReadSocketMessage(buff.get(), len)); + + GTEST_LOG_(INFO) << "Client_Socket_008 end"; +} + +/* + * Feature: AppSpawn + * Function: ClientSocket + * SubFunction: ReadSocketMessage + * FunctionPoints: check params + * EnvConditions: mobile that can run ohos test framework + * CaseDescription: Verify the function WriteSocketMessage can check the buffer length is 0. + */ +HWTEST(ClientSocketTest, Client_Socket_009, TestSize.Level0) +{ + GTEST_LOG_(INFO) << "Client_Socket_009 start"; + + std::unique_ptr clientSocket = std::make_unique("ClientSocketTest"); + int32_t len = 0; + std::unique_ptr buff = std::make_unique(10); + + EXPECT_EQ(0, clientSocket->CreateClient()); + EXPECT_LE(0, clientSocket->GetSocketFd()); + EXPECT_EQ(-1, clientSocket->ReadSocketMessage(buff.get(), len)); + + GTEST_LOG_(INFO) << "Client_Socket_009 end"; +} + +/* + * Feature: AppSpawn + * Function: ClientSocket + * SubFunction: ReadSocketMessage + * FunctionPoints: check params + * EnvConditions: mobile that can run ohos test framework + * CaseDescription: Verify the function WriteSocketMessage can check the buffer length < 0. + */ +HWTEST(ClientSocketTest, Client_Socket_010, TestSize.Level0) +{ + GTEST_LOG_(INFO) << "Client_Socket_010 start"; + + std::unique_ptr clientSocket = std::make_unique("ClientSocketTest"); + int32_t len = -1; + std::unique_ptr buff = std::make_unique(10); + + EXPECT_EQ(0, clientSocket->CreateClient()); + EXPECT_LE(0, clientSocket->GetSocketFd()); + EXPECT_EQ(-1, clientSocket->ReadSocketMessage(buff.get(), len)); + + GTEST_LOG_(INFO) << "Client_Socket_010 end"; +} + +/* + * Feature: AppSpawn + * Function: ClientSocket + * SubFunction: GetSocketFd + * FunctionPoints: close the socket + * EnvConditions: mobile that can run ohos test framework + * CaseDescription: Verify the function CloseClient can close the socket which socket fd has created. + */ +HWTEST(ClientSocketTest, Client_Socket_011, TestSize.Level0) +{ + GTEST_LOG_(INFO) << "Client_Socket_011 start"; + + std::unique_ptr clientSocket = std::make_unique("ClientSocketTest"); + + EXPECT_EQ(-1, clientSocket->GetSocketFd()); + EXPECT_EQ(0, clientSocket->CreateClient()); + EXPECT_LE(0, clientSocket->GetSocketFd()); + clientSocket->CloseClient(); + EXPECT_EQ(-1, clientSocket->GetSocketFd()); + + GTEST_LOG_(INFO) << "Client_Socket_011 end"; +}