• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #define LOG_TAG "DistributedTest"
16 
17 #include <gtest/gtest.h>
18 
19 #include "accesstoken_kit.h"
20 #include "directory_ex.h"
21 #include "distributed_kv_data_manager.h"
22 #include "distributed_major.h"
23 #include "log_print.h"
24 #include "nativetoken_kit.h"
25 #include "token_setproc.h"
26 #include "types.h"
27 
28 using namespace testing::ext;
29 using namespace OHOS;
30 using namespace OHOS::DistributedKv;
31 using namespace OHOS::DistributeSystemTest;
32 using namespace OHOS::Security::AccessToken;
33 namespace OHOS::Test {
34 class DistributedTest : public DistributeTest {
35 public:
36     static void SetUpTestCase();
37     static void TearDownTestCase();
38     void SetUp() override;
39     void TearDown() override;
40 
41     Status GetRemote(const Key &key, Value &value);
42 
43     static DistributedKvDataManager manager_;
44     static std::shared_ptr<SingleKvStore> singleKvStore_;
45     static constexpr int FILE_PERMISSION = 0777;
46     static constexpr int WAIT_TIME = 5;
47 };
48 
49 DistributedKvDataManager DistributedTest::manager_;
50 std::shared_ptr<SingleKvStore> DistributedTest::singleKvStore_ = nullptr;
51 
52 class KvStoreSyncCallbackTestImpl : public KvStoreSyncCallback {
53 public:
IsSyncComplete()54     static bool IsSyncComplete()
55     {
56         bool flag = completed_;
57         completed_ = false;
58         return flag;
59     }
60 
SyncCompleted(const std::map<std::string,Status> & results)61     void SyncCompleted(const std::map<std::string, Status> &results) override
62     {
63         ZLOGI("SyncCallback Called!");
64         for (const auto &result : results) {
65             if (result.second == SUCCESS) {
66                 completed_ = true;
67             }
68             ZLOGI("device: %{public}s, status: 0x%{public}x", result.first.c_str(), result.second);
69         }
70     }
71 
72 private:
73     static bool completed_;
74 };
75 
76 bool KvStoreSyncCallbackTestImpl::completed_ = false;
77 
SetUpTestCase()78 void DistributedTest::SetUpTestCase()
79 {
80     const char **perms = new const char *[1];
81     perms[0] = "ohos.permission.DISTRIBUTED_DATASYNC";
82     TokenInfoParams info = {
83         .dcapsNum = 0,
84         .permsNum = 1,
85         .aclsNum = 0,
86         .dcaps = nullptr,
87         .perms = perms,
88         .acls = nullptr,
89         .processName = "distributed_test",
90         .aplStr = "system_basic",
91     };
92     auto tokenId = GetAccessTokenId(&info);
93     SetSelfTokenID(tokenId);
94     AccessTokenKit::ReloadNativeTokenInfo();
95     delete[] perms;
96 
97     Options options = { .createIfMissing = true, .encrypt = false, .autoSync = false,
98                         .kvStoreType = KvStoreType::SINGLE_VERSION };
99     options.area = EL1;
100     options.baseDir = std::string("/data/service/el1/public/database/odmf");
101     AppId appId = { "odmf" };
102     StoreId storeId = { "student" };
103     mkdir(options.baseDir.c_str(), (S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH));
104     auto status = manager_.GetSingleKvStore(options, appId, storeId, singleKvStore_);
105     ASSERT_EQ(status, SUCCESS);
106     ASSERT_NE(singleKvStore_, nullptr);
107     OHOS::ChangeModeDirectory(options.baseDir, FILE_PERMISSION);
108 
109     std::shared_ptr<KvStoreSyncCallback> syncCallback = std::make_shared<KvStoreSyncCallbackTestImpl>();
110     status = singleKvStore_->RegisterSyncCallback(syncCallback);
111     ASSERT_EQ(status, SUCCESS);
112 }
113 
TearDownTestCase()114 void DistributedTest::TearDownTestCase()
115 {
116     (void)manager_.CloseAllKvStore({ "odmf" });
117     (void)manager_.DeleteKvStore({ "odmf" }, { "student" }, "/data/service/el1/public/database/odmf");
118     (void)remove("/data/service/el1/public/database/odmf/key");
119     (void)remove("/data/service/el1/public/database/odmf/kvdb");
120     (void)remove("/data/service/el1/public/database/odmf");
121 }
122 
SetUp()123 void DistributedTest::SetUp()
124 {}
125 
TearDown()126 void DistributedTest::TearDown()
127 {}
128 
GetRemote(const Key & key,Value & value)129 Status DistributedTest::GetRemote(const Key &key, Value &value)
130 {
131     Status status = ERROR;
132     std::string message = "get";
133     message += ",";
134     message += key.ToString();
135     (void)SendMessage(AGENT_NO::ONE, message, message.size(),
136         [&](const std::string &returnBuf, int length) -> bool {
137             auto index = returnBuf.find(",");
138             status = Status(std::stoi(returnBuf.substr(0, index)));
139             if (length > static_cast<int>(index + 1)) {
140                 value = Value(returnBuf.substr(index + 1));
141             }
142             return true;
143         });
144     return status;
145 }
146 
147 /**
148 * @tc.name: SendMessage001
149 * @tc.desc: Verify distributed test framework SendMessage
150 * @tc.type: FUNC
151 */
152 HWTEST_F(DistributedTest, SendMessage001, TestSize.Level1)
153 {
154     ZLOGI("SendMessage001 Start");
155 
156     std::string message = "SendMessage";
157     std::string result = "";
158     auto flag = SendMessage(AGENT_NO::ONE, message, message.size(),
__anon0bd455ec0202(const std::string &returnBuf, int length) 159         [&](const std::string &returnBuf, int length) -> bool {
160             result = returnBuf;
161             return true;
162         });
163     ASSERT_TRUE(flag);
164     ASSERT_EQ(result, "Send Message OK!");
165 
166     ZLOGI("SendMessage001 end");
167 }
168 
169 /**
170 * @tc.name: RunCommand001
171 * @tc.desc: Verify distributed test framework RunCommand
172 * @tc.type: FUNC
173 */
174 HWTEST_F(DistributedTest, RunCommand001, TestSize.Level1)
175 {
176     ZLOGI("RunCommand001 Start");
177 
178     std::string command = "CommandTest";
179     std::string arguments = "";
180     std::string result = "0";
181     auto flag = RunCmdOnAgent(AGENT_NO::ONE, command, arguments, result);
182     ASSERT_TRUE(flag);
183     auto status = GetReturnVal();
184     ASSERT_EQ(status, SUCCESS);
185 
186     ZLOGI("RunCommand001 end");
187 }
188 
189 /**
190 * @tc.name: SyncData001
191 * @tc.desc: Sync data with push mode and get data from other device
192 * @tc.type: FUNC
193 */
194 HWTEST_F(DistributedTest, SyncData001, TestSize.Level1)
195 {
196     ZLOGI("SyncData001 begin");
197 
198     Key key = { "key1" };
199     Value valueLocal = { "value1" };
200     auto status = singleKvStore_->Put(key, valueLocal);
201     ASSERT_EQ(status, SUCCESS);
202 
203     std::vector<std::string> devices;
204     status = singleKvStore_->Sync(devices, SyncMode::PUSH);
205     ASSERT_EQ(status, SUCCESS);
206 
207     sleep(WAIT_TIME);
208 
209     auto flag = KvStoreSyncCallbackTestImpl::IsSyncComplete();
210     ASSERT_TRUE(flag);
211 
212     Value valueRemote;
213     status = GetRemote(key, valueRemote);
214     ASSERT_EQ(status, SUCCESS);
215     ASSERT_EQ(valueLocal.ToString(), valueRemote.ToString());
216 
217     ZLOGI("SyncData001 end");
218 }
219 
220 /**
221 * @tc.name: SyncData002
222 * @tc.desc: Sync data with pull mode by other device and get data from other device
223 * @tc.type: FUNC
224 */
225 HWTEST_F(DistributedTest, SyncData002, TestSize.Level1)
226 {
227     ZLOGI("SyncData002 begin");
228 
229     Key key = { "key2" };
230     Value valueLocal = { "value2" };
231     auto status = singleKvStore_->Put(key, valueLocal);
232     ASSERT_EQ(status, SUCCESS);
233 
234     SyncMode mode = SyncMode::PULL;
235     std::string arguments = std::to_string(mode);
236     auto flag = RunCmdOnAgent(AGENT_NO::ONE, "sync", arguments, "0");
237     ASSERT_TRUE(flag);
238     status = static_cast<Status>(GetReturnVal());
239     ASSERT_EQ(status, SUCCESS);
240 
241     Value valueRemote;
242     status = GetRemote(key, valueRemote);
243     ASSERT_EQ(status, SUCCESS);
244     ASSERT_EQ(valueLocal.ToString(), valueRemote.ToString());
245 
246     ZLOGI("SyncData002 end");
247 }
248 } // namespace OHOS::Test
249 
main(int argc,char * argv[])250 int main(int argc, char *argv[])
251 {
252     g_pDistributetestEnv = new DistributeTestEnvironment("major.desc");
253     testing::AddGlobalTestEnvironment(g_pDistributetestEnv);
254     testing::GTEST_FLAG(output) = "xml:./";
255     testing::InitGoogleTest(&argc, argv);
256     return RUN_ALL_TESTS();
257 }