1 /*
2 * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
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 <gtest/gtest.h>
17
18 #define private public
19 #include "app_spawn_socket.h"
20 #undef private
21
22 using namespace testing;
23 using namespace testing::ext;
24 using namespace OHOS::AppSpawn;
25
26 namespace OHOS {
27 namespace AppExecFwk {
28 class AppSpawnSocketTest : public testing::Test {
29 public:
30 static void SetUpTestCase();
31 static void TearDownTestCase();
32 void SetUp();
33 void TearDown();
34 };
35
SetUpTestCase()36 void AppSpawnSocketTest::SetUpTestCase()
37 {}
38
TearDownTestCase()39 void AppSpawnSocketTest::TearDownTestCase()
40 {}
41
SetUp()42 void AppSpawnSocketTest::SetUp()
43 {}
44
TearDown()45 void AppSpawnSocketTest::TearDown()
46 {}
47
48 /*
49 * Feature: AppSpawnSocket
50 * Function: OpenAppSpawnConnection
51 * SubFunction: NA
52 * FunctionPoints: AppSpawnSocket OpenAppSpawnConnection
53 * EnvConditions: NA
54 * CaseDescription: Verify OpenAppSpawnConnection
55 */
56 HWTEST_F(AppSpawnSocketTest, OpenAppSpawnConnection_001, TestSize.Level0)
57 {
58 auto appSpawnSocket = std::make_shared<AppSpawnSocket>(true);
59 ErrCode res1 = appSpawnSocket->OpenAppSpawnConnection();
60 EXPECT_EQ(res1, ERR_OK);
61 appSpawnSocket->clientSocket_ = nullptr;
62 ErrCode res2 = appSpawnSocket->OpenAppSpawnConnection();
63 EXPECT_EQ(res2, ERR_APPEXECFWK_BAD_APPSPAWN_SOCKET);
64
65 appSpawnSocket->CloseAppSpawnConnection();
66 appSpawnSocket->clientSocket_ = nullptr;
67 appSpawnSocket->CloseAppSpawnConnection();
68 }
69
70 /*
71 * Feature: AppSpawnSocket
72 * Function: WriteMessage
73 * SubFunction: NA
74 * FunctionPoints: AppSpawnSocket WriteMessage
75 * EnvConditions: NA
76 * CaseDescription: Verify WriteMessage
77 */
78 HWTEST_F(AppSpawnSocketTest, WriteMessage_001, TestSize.Level0)
79 {
80 auto appSpawnSocket = std::make_shared<AppSpawnSocket>(true);
81 const void *buf = nullptr;
82 int32_t len = 0;
83 ErrCode res = appSpawnSocket->WriteMessage(buf, len);
84 EXPECT_EQ(res, ERR_INVALID_VALUE);
85 }
86
87 /*
88 * Feature: AppSpawnSocket
89 * Function: WriteMessage
90 * SubFunction: NA
91 * FunctionPoints: AppSpawnSocket WriteMessage
92 * EnvConditions: NA
93 * CaseDescription: Verify WriteMessage
94 */
95 HWTEST_F(AppSpawnSocketTest, WriteMessage_002, TestSize.Level0)
96 {
97 auto appSpawnSocket = std::make_shared<AppSpawnSocket>(true);
98 const void *buf = nullptr;
99 int32_t len = 1;
100 ErrCode res = appSpawnSocket->WriteMessage(buf, len);
101 EXPECT_EQ(res, ERR_INVALID_VALUE);
102 }
103
104 /*
105 * Feature: AppSpawnSocket
106 * Function: WriteMessage
107 * SubFunction: NA
108 * FunctionPoints: AppSpawnSocket WriteMessage
109 * EnvConditions: NA
110 * CaseDescription: Verify WriteMessage
111 */
112 HWTEST_F(AppSpawnSocketTest, WriteMessage_003, TestSize.Level0)
113 {
114 auto appSpawnSocket = std::make_shared<AppSpawnSocket>(true);
115 char data = 'a';
116 const void *buf = &data;
117 int32_t len = 1;
118 ErrCode res1 = appSpawnSocket->WriteMessage(buf, len);
119 EXPECT_EQ(res1, ERR_APPEXECFWK_SOCKET_WRITE_FAILED);
120 appSpawnSocket->clientSocket_ = nullptr;
121 ErrCode res2 = appSpawnSocket->WriteMessage(buf, len);
122 EXPECT_EQ(res2, ERR_APPEXECFWK_BAD_APPSPAWN_SOCKET);
123 }
124
125 /*
126 * Feature: AppSpawnSocket
127 * Function: ReadMessage
128 * SubFunction: NA
129 * FunctionPoints: AppSpawnSocket ReadMessage
130 * EnvConditions: NA
131 * CaseDescription: Verify ReadMessage
132 */
133 HWTEST_F(AppSpawnSocketTest, ReadMessage_001, TestSize.Level0)
134 {
135 auto appSpawnSocket = std::make_shared<AppSpawnSocket>(true);
136 void *buf = nullptr;
137 int32_t len = 0;
138 ErrCode res = appSpawnSocket->ReadMessage(buf, len);
139 EXPECT_EQ(res, ERR_INVALID_VALUE);
140 }
141
142 /*
143 * Feature: AppSpawnSocket
144 * Function: ReadMessage
145 * SubFunction: NA
146 * FunctionPoints: AppSpawnSocket ReadMessage
147 * EnvConditions: NA
148 * CaseDescription: Verify ReadMessage
149 */
150 HWTEST_F(AppSpawnSocketTest, ReadMessage_002, TestSize.Level0)
151 {
152 auto appSpawnSocket = std::make_shared<AppSpawnSocket>(true);
153 void *buf = nullptr;
154 int32_t len = 1;
155 ErrCode res = appSpawnSocket->ReadMessage(buf, len);
156 EXPECT_EQ(res, ERR_INVALID_VALUE);
157 }
158
159 /*
160 * Feature: AppSpawnSocket
161 * Function: ReadMessage
162 * SubFunction: NA
163 * FunctionPoints: AppSpawnSocket ReadMessage
164 * EnvConditions: NA
165 * CaseDescription: Verify ReadMessage
166 */
167 HWTEST_F(AppSpawnSocketTest, ReadMessage_003, TestSize.Level0)
168 {
169 auto appSpawnSocket = std::make_shared<AppSpawnSocket>(true);
170 char data = 'a';
171 void *buf = &data;
172 int32_t len = 1;
173 ErrCode res1 = appSpawnSocket->ReadMessage(buf, len);
174 EXPECT_EQ(res1, ERR_APPEXECFWK_SOCKET_READ_FAILED);
175 appSpawnSocket->clientSocket_ = nullptr;
176 ErrCode res2 = appSpawnSocket->ReadMessage(buf, len);
177 EXPECT_EQ(res2, ERR_APPEXECFWK_BAD_APPSPAWN_CLIENT);
178 }
179 } // namespace AppExecFwk
180 } // namespace OHOS
181