1 /* 2 * Copyright (C) 2025 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 #include <unistd.h> 18 #include <gtest/gtest.h> 19 20 #include "datashare_errno.h" 21 #include "datashare_itypes_utils.h" 22 #include "datashare_log.h" 23 #include "datashare_abs_result_set.h" 24 #include "datashare_result_set.h" 25 #include "ikvstore_data_service.h" 26 #include "ipc_types.h" 27 #include "ishared_result_set_stub.h" 28 #include "itypes_util.h" 29 #include "message_parcel.h" 30 31 namespace OHOS { 32 namespace DataShare { 33 using namespace testing::ext; 34 class IsharedResultSetStubTest : public testing::Test { 35 public: SetUpTestCase(void)36 static void SetUpTestCase(void){}; TearDownTestCase(void)37 static void TearDownTestCase(void){}; SetUp()38 void SetUp(){}; TearDown()39 void TearDown(){}; 40 }; 41 42 class ResultSetBridgeTest : public OHOS::DataShare::ResultSetBridge { 43 public: GetAllColumnNames(std::vector<std::string> & columnNames)44 int GetAllColumnNames(std::vector<std::string> &columnNames) override 45 { 46 return 0; 47 } 48 GetRowCount(int32_t & count)49 int GetRowCount(int32_t &count) override 50 { 51 return 0; 52 } 53 OnGo(int32_t startRowIndex,int32_t targetRowIndex,Writer & writer)54 int OnGo(int32_t startRowIndex, int32_t targetRowIndex, Writer &writer) override 55 { 56 return 0; 57 } 58 }; 59 60 /** 61 * @tc.name: CreateStubTestTest001 62 * @tc.desc: test CreateStub function when resuletset_ = nullptr 63 * @tc.type: FUNC 64 * @tc.require: issueIC7OBM 65 * @tc.precon: None 66 * @tc.step: 67 1.Creat a ISharedResultSetStub object and resuletset_ = nullptr 68 2.call CreateStub function and check the result 69 * @tc.experct: CreateStub failed and reutrn nullptr 70 */ 71 HWTEST_F(IsharedResultSetStubTest, CreateStubTest001, TestSize.Level0) 72 { 73 LOG_INFO("IsharedResultSetStubTest CreateStubTest001::Start"); 74 std::shared_ptr<DataShareResultSet> resultset; 75 MessageParcel parcel; 76 ISharedResultSetStub stub(resultset); 77 auto result = stub.CreateStub(resultset, parcel); 78 EXPECT_EQ(result, nullptr); 79 LOG_INFO("IsharedResultSetStubTest CreateStubTest001::End"); 80 } 81 82 /** 83 * @tc.name: CreateStubTestTest002 84 * @tc.desc: test CreateStub function when resuletset_ is not nullptr 85 * @tc.type: FUNC 86 * @tc.require: issueIC7OBM 87 * @tc.precon: None 88 * @tc.step: 89 1.Creat a ISharedResultSetStub object and resuletset_ is not nullptr 90 2.call CreateStub function and check the result 91 * @tc.experct: CreateStub succees and not reutrn nullptr 92 */ 93 HWTEST_F(IsharedResultSetStubTest, CreateStubTestTest002, TestSize.Level0) 94 { 95 LOG_INFO("IsharedResultSetStubTest CreateStubTestTest002::Start"); 96 std::shared_ptr<DataShareResultSet> resultset = std::make_shared<DataShareResultSet>(); 97 MessageParcel parcel; 98 ISharedResultSetStub stub(resultset); 99 auto result = stub.CreateStub(resultset, parcel); 100 EXPECT_NE(result, nullptr); 101 LOG_INFO("IsharedResultSetStubTest CreateStubTestTest002::End"); 102 } 103 104 /** 105 * @tc.name: OnRemoteRequestTest001 106 * @tc.desc: test OnRemoteRequest function when resuletset_ = nullptr 107 * @tc.type: FUNC 108 * @tc.require: issueIC7OBM 109 * @tc.precon: None 110 * @tc.step: 111 1.Creat a ISharedResultSetStub object and resuletset_ = nullptr 112 2.call OnRemoteRequest function and check the result 113 * @tc.experct: OnRemoteRequest failed and reutrn INVALID_FD 114 */ 115 HWTEST_F(IsharedResultSetStubTest, OnRemoteRequestTest001, TestSize.Level0) 116 { 117 LOG_INFO("IsharedResultSetStubTest OnRemoteRequestTest001::Start"); 118 std::shared_ptr<DataShareResultSet> resultset; 119 ISharedResultSetStub stub(resultset); 120 uint32_t code = 0; 121 MessageParcel data; 122 MessageParcel parcel; 123 MessageOption option; 124 auto result = stub.OnRemoteRequest(code, data, parcel, option); 125 EXPECT_EQ(result, INVALID_FD); 126 LOG_INFO("IsharedResultSetStubTest OnRemoteRequestTest001::End"); 127 } 128 129 /** 130 * @tc.name: HandleGetRowCountRequestTest001 131 * @tc.desc: test HandleGetRowCountRequest function when bridge = nullptr 132 * @tc.type: FUNC 133 * @tc.require: issueIC7OBM 134 * @tc.precon: None 135 * @tc.step: 136 1.Creat a ISharedResultSetStub object and bridge is nullptr 137 2.call HandleGetRowCountRequest function and check the result 138 * @tc.experct: HandleGetRowCountRequest success and write E_ERROR into reply 139 */ 140 HWTEST_F(IsharedResultSetStubTest, HandleGetRowCountRequestTest001, TestSize.Level0) 141 { 142 LOG_INFO("IsharedResultSetStubTest HandleGetRowCountRequestTest001::Start"); 143 std::shared_ptr<DataShareResultSet> resultset = std::make_shared<DataShareResultSet>(); 144 MessageParcel data; 145 MessageParcel reply; 146 ISharedResultSetStub stub(resultset); 147 auto result = stub.HandleGetRowCountRequest(data, reply); 148 EXPECT_EQ(reply.ReadInt32(), E_ERROR); 149 EXPECT_EQ(result, NO_ERROR); 150 LOG_INFO("IsharedResultSetStubTest HandleGetRowCountRequestTest001::End"); 151 } 152 153 /** 154 * @tc.name: HandleGetRowCountRequestTest002 155 * @tc.desc: test HandleGetRowCountRequest function when bridge is not nullptr 156 * @tc.type: FUNC 157 * @tc.require: issueIC7OBM 158 * @tc.precon: None 159 * @tc.step: 160 1.Creat a ISharedResultSetStub object and bridge is not nullptr 161 2.call HandleGetRowCountRequest function and check the result 162 * @tc.experct: HandleGetRowCountRequest success and write count into reply 163 */ 164 HWTEST_F(IsharedResultSetStubTest, HandleGetRowCountRequestTest002, TestSize.Level0) 165 { 166 LOG_INFO("IsharedResultSetStubTest HandleGetRowCountRequestTest002::Start"); 167 std::shared_ptr<DataShareResultSet> resultset = std::make_shared<DataShareResultSet>(); 168 resultset->bridge_ = std::make_shared<ResultSetBridgeTest>(); 169 MessageParcel data; 170 MessageParcel reply; 171 ISharedResultSetStub stub(resultset); 172 auto result = stub.HandleGetRowCountRequest(data, reply); 173 EXPECT_NE(reply.ReadInt32(), E_ERROR); 174 EXPECT_EQ(result, NO_ERROR); 175 LOG_INFO("IsharedResultSetStubTest HandleGetRowCountRequestTest002::End"); 176 } 177 178 /** 179 * @tc.name: HandleGetAllColumnNamesRequestTest001 180 * @tc.desc: test HandleGetAllColumnNamesRequest function when bridge is nullptr 181 * @tc.type: FUNC 182 * @tc.require: issueIC7OBM 183 * @tc.precon: None 184 * @tc.step: 185 1.Creat a ISharedResultSetStub object and bridge is nullptr 186 2.call HandleGetAllColumnNamesRequest function and check the result 187 * @tc.experct: HandleGetAllColumnNamesRequest success and write E_ERROR into reply 188 */ 189 HWTEST_F(IsharedResultSetStubTest, HandleGetAllColumnNamesRequestTest001, TestSize.Level0) 190 { 191 LOG_INFO("IsharedResultSetStubTest HandleGetAllColumnNamesRequestTest001::Start"); 192 std::shared_ptr<DataShareResultSet> resultset = std::make_shared<DataShareResultSet>(); 193 MessageParcel data; 194 MessageParcel reply; 195 ISharedResultSetStub stub(resultset); 196 auto result = stub.HandleGetAllColumnNamesRequest(data, reply); 197 EXPECT_EQ(reply.ReadInt32(), E_ERROR); 198 EXPECT_EQ(result, NO_ERROR); 199 LOG_INFO("IsharedResultSetStubTest HandleGetAllColumnNamesRequestTest001::End"); 200 } 201 202 /** 203 * @tc.name: HandleGetAllColumnNamesRequestTest002 204 * @tc.desc: test HandleGetAllColumnNamesRequest function when bridge is not nullptr 205 * @tc.type: FUNC 206 * @tc.require: issueIC7OBM 207 * @tc.precon: None 208 * @tc.step: 209 1.Creat a ISharedResultSetStub object and bridge is not nullptr 210 2.call HandleGetAllColumnNamesRequest function and check the result 211 * @tc.experct: HandleGetAllColumnNamesRequest success and write count into reply 212 */ 213 HWTEST_F(IsharedResultSetStubTest, HandleGetAllColumnNamesRequestTest002, TestSize.Level0) 214 { 215 LOG_INFO("IsharedResultSetStubTest HandleGetAllColumnNamesRequestTest002::Start"); 216 std::shared_ptr<DataShareResultSet> resultset = std::make_shared<DataShareResultSet>(); 217 resultset->bridge_ = std::make_shared<ResultSetBridgeTest>(); 218 MessageParcel data; 219 MessageParcel reply; 220 ISharedResultSetStub stub(resultset); 221 auto result = stub.HandleGetAllColumnNamesRequest(data, reply); 222 EXPECT_NE(reply.ReadInt32(), E_ERROR); 223 EXPECT_EQ(result, NO_ERROR); 224 LOG_INFO("IsharedResultSetStubTest HandleGetAllColumnNamesRequestTest002::End"); 225 } 226 227 /** 228 * @tc.name: HandleOnGoRequestTest001 229 * @tc.desc: test HandleOnGoRequest function when bridge is nullptr 230 * @tc.type: FUNC 231 * @tc.require: issueIC7OBM 232 * @tc.precon: None 233 * @tc.step: 234 1.Creat a ISharedResultSetStub object and bridge is nullptr 235 2.call HandleOnGoRequest function and check the result 236 * @tc.experct: HandleOnGoRequest success and write -1 into reply 237 */ 238 HWTEST_F(IsharedResultSetStubTest, HandleOnGoRequestTest001, TestSize.Level0) 239 { 240 LOG_INFO("IsharedResultSetStubTest HandleOnGoRequestTest001::Start"); 241 std::shared_ptr<DataShareResultSet> resultset = std::make_shared<DataShareResultSet>(); 242 MessageParcel data; 243 MessageParcel reply; 244 ISharedResultSetStub stub(resultset); 245 auto result = stub.HandleOnGoRequest(data, reply); 246 EXPECT_EQ(reply.ReadInt32(), -1); 247 EXPECT_EQ(result, NO_ERROR); 248 LOG_INFO("IsharedResultSetStubTest HandleOnGoRequestTest001::End"); 249 } 250 251 /** 252 * @tc.name: HandleOnGoRequestTest002 253 * @tc.desc: test HandleOnGoRequest function when bridge is not nullptr 254 * @tc.type: FUNC 255 * @tc.require: issueIC7OBM 256 * @tc.precon: None 257 * @tc.step: 258 1.Creat a ISharedResultSetStub object and bridge is not nullptr 259 2.call HandleOnGoRequest function and check the result 260 * @tc.experct: HandleOnGoRequest success and write count into reply 261 */ 262 HWTEST_F(IsharedResultSetStubTest, HandleOnGoRequestTest002, TestSize.Level0) 263 { 264 LOG_INFO("IsharedResultSetStubTest HandleOnGoRequestTest002::Start"); 265 std::shared_ptr<DataShareResultSet> resultset = std::make_shared<DataShareResultSet>(); 266 resultset->bridge_ = std::make_shared<ResultSetBridgeTest>(); 267 MessageParcel data; 268 MessageParcel reply; 269 ISharedResultSetStub stub(resultset); 270 auto result = stub.HandleOnGoRequest(data, reply); 271 EXPECT_NE(reply.ReadInt32(), E_ERROR); 272 EXPECT_EQ(result, NO_ERROR); 273 LOG_INFO("IsharedResultSetStubTest HandleOnGoRequestTest002::End"); 274 } 275 } 276 }