1 /*
2  * Copyright (c) 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 #include <regex>
19 #include <string>
20 #include <vector>
21 
22 #include "device_manager.h"
23 #include "device_manager_callback.h"
24 #include "distributed_major.h"
25 #include "dm_device_info.h"
26 #include "hilog/log.h"
27 #include "rdb_errno.h"
28 #include "rdb_helper.h"
29 #include "rdb_open_callback.h"
30 #include "rdb_store_impl.h"
31 #include "rdb_types.h"
32 #include "result_set_proxy.h"
33 
34 using namespace testing::ext;
35 using namespace OHOS::NativeRdb;
36 using namespace OHOS::DistributedRdb;
37 using namespace testing::ext;
38 using namespace OHOS::DistributeSystemTest;
39 using namespace OHOS::DistributedHardware;
40 using namespace OHOS::HiviewDFX;
41 namespace {
42 const int MSG_LENGTH = 100;
43 constexpr HiLogLabel LABEL = { LOG_CORE, 0, "DistributedTest" };
44 static const std::string RDB_TEST_PATH = "/data/test/";
45 constexpr const char *PKG_NAME = "rdb_store_distributed_test";
46 class DistributedTest : public DistributeTest {
47 public:
48     static void SetUpTestCase(void);
49     static void TearDownTestCase(void);
50     void SetUp();
51     void TearDown();
52 
53     static const std::string DATABASE_NAME;
54     static std::shared_ptr<RdbStore> store_;
55     static std::vector<DmDeviceInfo> deviceInfos_;
56     static void InitDevManager();
57 };
58 
59 const std::string DistributedTest::DATABASE_NAME = RDB_TEST_PATH + "distributed_rdb.db";
60 std::shared_ptr<RdbStore> DistributedTest::store_ = nullptr;
61 std::vector<DmDeviceInfo> DistributedTest::deviceInfos_;
62 
63 class DMStateCallback : public DeviceStateCallback {
64 public:
DMStateCallback()65     explicit DMStateCallback()
66     {
67     }
OnDeviceOnline(const DmDeviceInfo & deviceInfo)68     void OnDeviceOnline(const DmDeviceInfo &deviceInfo) override
69     {
70     }
OnDeviceOffline(const DmDeviceInfo & deviceInfo)71     void OnDeviceOffline(const DmDeviceInfo &deviceInfo) override
72     {
73     }
OnDeviceChanged(const DmDeviceInfo & deviceInfo)74     void OnDeviceChanged(const DmDeviceInfo &deviceInfo) override
75     {
76     }
OnDeviceReady(const DmDeviceInfo & deviceInfo)77     void OnDeviceReady(const DmDeviceInfo &deviceInfo) override
78     {
79     }
80 };
81 
82 class DmDeathCallback : public DmInitCallback {
83 public:
DmDeathCallback()84     explicit DmDeathCallback()
85     {
86     }
OnRemoteDied()87     void OnRemoteDied() override
88     {
89     }
90 };
91 
InitDevManager()92 void DistributedTest::InitDevManager()
93 {
94     auto &deviceManager = DeviceManager::GetInstance();
95     auto deviceInitCallback = std::make_shared<DmDeathCallback>();
96     auto deviceCallback = std::make_shared<DMStateCallback>();
97     deviceManager.InitDeviceManager(PKG_NAME, deviceInitCallback);
98     deviceManager.RegisterDevStateCallback(PKG_NAME, "", deviceCallback);
99 }
100 
101 class DistributedTestOpenCallback : public RdbOpenCallback {
102 public:
103     int OnCreate(RdbStore &rdbStore) override;
104     int OnUpgrade(RdbStore &rdbStore, int oldVersion, int newVersion) override;
105     static const std::string CREATE_TABLE_TEST;
106 };
107 
108 const std::string DistributedTestOpenCallback::CREATE_TABLE_TEST =
109     std::string("CREATE TABLE IF NOT EXISTS test ") + std::string("(id INTEGER PRIMARY KEY AUTOINCREMENT, "
110                                                                   "name TEXT NOT NULL, age INTEGER, salary "
111                                                                   "REAL, blobType BLOB)");
112 
OnCreate(RdbStore & store_)113 int DistributedTestOpenCallback::OnCreate(RdbStore &store_)
114 {
115     return store_.ExecuteSql(CREATE_TABLE_TEST);
116 }
117 
OnUpgrade(RdbStore & store_,int oldVersion,int newVersion)118 int DistributedTestOpenCallback::OnUpgrade(RdbStore &store_, int oldVersion, int newVersion)
119 {
120     return E_OK;
121 }
122 
SetUpTestCase(void)123 void DistributedTest::SetUpTestCase(void)
124 {
125     int errCode = E_OK;
126     RdbStoreConfig config(DistributedTest::DATABASE_NAME);
127     config.SetBundleName("com.example.distributed.rdb");
128     config.SetName("distributed_rdb.db");
129     DistributedTestOpenCallback helper;
130     DistributedTest::store_ = RdbHelper::GetRdbStore(config, 1, helper, errCode);
131     EXPECT_NE(DistributedTest::store_, nullptr);
132     InitDevManager();
133     DeviceManager::GetInstance().GetTrustedDeviceList(PKG_NAME, "", deviceInfos_);
134 }
135 
TearDownTestCase(void)136 void DistributedTest::TearDownTestCase(void)
137 {
138     RdbHelper::DeleteRdbStore(DistributedTest::DATABASE_NAME);
139 }
140 
SetUp(void)141 void DistributedTest::SetUp(void)
142 {
143     store_->ExecuteSql("DELETE FROM test");
144 }
145 
TearDown(void)146 void DistributedTest::TearDown(void)
147 {
148 }
149 
150 /**
151  * @tc.name: RemoteQuery001
152  * @tc.desc: normal testcase of DistributedTest
153  * @tc.type: FUNC
154  * @tc.require:issueI5JV75
155  */
156 HWTEST_F(DistributedTest, RemoteQuery001, TestSize.Level1)
157 {
158     std::shared_ptr<RdbStore> &store_ = DistributedTest::store_;
159     int ret;
160     std::string returvalue;
161     std::string msgBuf = "recall function message test.";
__anonf13e8b530202(const std::string &szreturnbuf, int rlen) 162     ret = SendMessage(AGENT_NO::ONE, msgBuf, MSG_LENGTH, [&](const std::string &szreturnbuf, int rlen) -> bool {
163         returvalue = szreturnbuf;
164         return true;
165     });
166     std::vector<std::string> tables = { "test" };
167     DeviceManager::GetInstance().GetTrustedDeviceList(PKG_NAME, "", deviceInfos_);
168     int errCode = E_ERROR;
169     std::string test = store_->ObtainDistributedTableName(deviceInfos_[0].networkId, tables[0], errCode);
170     AbsRdbPredicates predicate(tables[0]);
171     predicate.EqualTo("name", "zhangsan");
172     std::vector<std::string> columns;
173     errCode = E_ERROR;
174     std::shared_ptr<ResultSet> resultSet = store_->RemoteQuery(deviceInfos_[0].networkId, predicate, columns, errCode);
175 
176     EXPECT_TRUE(ret > 0);
177     EXPECT_EQ(returvalue, "zhangsan");
178 }
179 
180 /**
181  * @tc.name: ResultSetProxy001
182  * @tc.desc: Abnormal testcase of distributed ResultSetProxy, if resultSet is Empty
183  * @tc.type: FUNC
184  */
185 HWTEST_F(DistributedTest, ResultSetProxy001, TestSize.Level1)
186 {
187     int errCode = 0;
188     std::shared_ptr<OHOS::NativeRdb::ResultSetProxy> resultSet;
189     ColumnType columnType;
190     errCode = resultSet->GetColumnType(1, columnType);
191     EXPECT_NE(E_OK, errCode);
192 
193     std::string columnName;
194     errCode = resultSet->GetColumnName(1, columnName);
195     EXPECT_NE(E_OK, errCode);
196 
197     std::vector<uint8_t> blob;
198     errCode = resultSet->GetBlob(1, blob);
199     EXPECT_NE(E_OK, errCode);
200 
201     std::string getStringvalue;
202     errCode = resultSet->GetString(1, getStringvalue);
203     EXPECT_NE(E_OK, errCode);
204 
205     int getIntvalue;
206     errCode = resultSet->GetInt(1, getIntvalue);
207     EXPECT_NE(E_OK, errCode);
208 
209     int64_t getLongvalue;
210     errCode = resultSet->GetLong(1, getLongvalue);
211     EXPECT_NE(E_OK, errCode);
212 
213     double getDoublevalue;
214     errCode = resultSet->GetDouble(1, getDoublevalue);
215     EXPECT_NE(E_OK, errCode);
216 
217     bool isNull;
218     errCode = resultSet->IsColumnNull(1, isNull);
219     EXPECT_NE(E_OK, errCode);
220 }
221 } // namespace
222 
main(int argc,char * argv[])223 int main(int argc, char *argv[])
224 {
225     HiLog::Info(LABEL, "begin");
226     g_pDistributetestEnv = new DistributeTestEnvironment("major.desc");
227     testing::AddGlobalTestEnvironment(g_pDistributetestEnv);
228     testing::GTEST_FLAG(output) = "xml:./";
229     testing::InitGoogleTest(&argc, argv);
230     return RUN_ALL_TESTS();
231 }