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 "accesstoken_kit.h" 17 #include "communication_provider.h" 18 #include "gtest/gtest.h" 19 #include "kvstore_meta_manager.h" 20 #include "metadata/meta_data_manager.h" 21 #include "metadata/store_meta_data.h" 22 #include "session_manager/route_head_handler_impl.h" 23 #include "session_manager/upgrade_manager.h" 24 #include "user_delegate.h" 25 26 namespace { 27 using namespace testing::ext; 28 using namespace OHOS::DistributedKv; 29 using namespace OHOS::DistributedData; 30 using namespace OHOS; 31 using namespace OHOS::Security::AccessToken; 32 constexpr const char *PEER_DEVICE_ID = "PEER_DEVICE_ID"; 33 constexpr int PEER_USER_ID = 101; 34 class SessionManagerTest : public testing::Test { 35 public: SetUpTestCase()36 static void SetUpTestCase() 37 { 38 KvStoreMetaManager::GetInstance().InitMetaParameter(); 39 KvStoreMetaManager::GetInstance().InitMetaListener(); 40 41 // init peer device 42 UserMetaData userMetaData; 43 userMetaData.deviceId = PEER_DEVICE_ID; 44 45 UserStatus status; 46 status.isActive = true; 47 status.id = PEER_USER_ID; 48 userMetaData.users = { status }; 49 50 auto peerUserMetaKey = UserMetaRow::GetKeyFor(userMetaData.deviceId); 51 MetaDataManager::GetInstance().SaveMeta({ peerUserMetaKey.begin(), peerUserMetaKey.end() }, userMetaData); 52 53 CapMetaData capMetaData; 54 capMetaData.version = CapMetaData::CURRENT_VERSION; 55 56 auto peerCapMetaKey = CapMetaRow::GetKeyFor(userMetaData.deviceId); 57 MetaDataManager::GetInstance().SaveMeta({ peerCapMetaKey.begin(), peerCapMetaKey.end() }, capMetaData); 58 59 StoreMetaData metaData; 60 metaData.bundleName = "ohos.test.demo"; 61 metaData.storeId = "test_store"; 62 metaData.user = "100"; 63 metaData.deviceId = OHOS::AppDistributedKv::CommunicationProvider::GetInstance().GetLocalDevice().uuid; 64 metaData.tokenId = AccessTokenKit::GetHapTokenID(100, "ohos.test.demo", 0); 65 metaData.uid = 2000000; 66 metaData.storeType = 1; 67 MetaDataManager::GetInstance().SaveMeta(metaData.GetKey(), metaData); 68 } TearDownTestCase()69 static void TearDownTestCase() 70 { 71 auto peerUserMetaKey = UserMetaRow::GetKeyFor(PEER_DEVICE_ID); 72 MetaDataManager::GetInstance().DelMeta(std::string(peerUserMetaKey.begin(), peerUserMetaKey.end())); 73 auto peerCapMetaKey = CapMetaRow::GetKeyFor(PEER_DEVICE_ID); 74 MetaDataManager::GetInstance().DelMeta(std::string(peerCapMetaKey.begin(), peerCapMetaKey.end())); 75 StoreMetaData metaData; 76 metaData.bundleName = "ohos.test.demo"; 77 metaData.storeId = "test_store"; 78 metaData.user = "100"; 79 metaData.deviceId = OHOS::AppDistributedKv::CommunicationProvider::GetInstance().GetLocalDevice().uuid; 80 metaData.tokenId = AccessTokenKit::GetHapTokenID(100, "ohos.test.demo", 0); 81 metaData.uid = 2000000; 82 metaData.storeType = 1; 83 MetaDataManager::GetInstance().DelMeta(metaData.GetKey()); 84 } SetUp()85 void SetUp() 86 { 87 } TearDown()88 void TearDown() 89 { 90 } 91 }; 92 93 /** 94 * @tc.name: PackAndUnPack01 95 * @tc.desc: test get db dir 96 * @tc.type: FUNC 97 * @tc.require: 98 * @tc.author: illybyy 99 */ 100 HWTEST_F(SessionManagerTest, PackAndUnPack01, TestSize.Level2) 101 { 102 const DistributedDB::ExtendInfo info = { 103 .appId = "ohos.test.demo", .storeId = "test_store", .userId = "100", .dstTarget = PEER_DEVICE_ID 104 }; 105 auto sendHandler = RouteHeadHandlerImpl::Create(info); 106 ASSERT_NE(sendHandler, nullptr); 107 uint32_t routeHeadSize = 0; 108 sendHandler->GetHeadDataSize(routeHeadSize); 109 ASSERT_GT(routeHeadSize, 0); 110 std::unique_ptr<uint8_t[]> data = std::make_unique<uint8_t[]>(routeHeadSize); 111 sendHandler->FillHeadData(data.get(), routeHeadSize, routeHeadSize); 112 113 std::vector<std::string> users; 114 auto recvHandler = RouteHeadHandlerImpl::Create({}); 115 ASSERT_NE(recvHandler, nullptr); 116 uint32_t parseSize = 0; 117 recvHandler->ParseHeadData(data.get(), routeHeadSize, parseSize, users); 118 EXPECT_EQ(routeHeadSize, parseSize); 119 ASSERT_EQ(users.size(), 1); 120 EXPECT_EQ(users[0], "101"); 121 } 122 } // namespace