• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #define LOG_TAG "ObjectServiceImplTest"
17 
18 #include "object_service_impl.h"
19 
20 #include <gtest/gtest.h>
21 
22 #include "accesstoken_kit.h"
23 #include "ipc_skeleton.h"
24 #include "token_setproc.h"
25 
26 using namespace testing::ext;
27 using namespace OHOS::DistributedObject;
28 namespace OHOS::Test {
29 class ObjectServiceImplTest : public testing::Test {
30 public:
31     void SetUp();
32     void TearDown();
33 protected:
34     ObjectStore::Asset asset_;
35     std::string uri_;
36     std::string appId_ = "ObjectServiceImplTest_appid_1";
37     std::string sessionId_ = "123";
38     std::vector<uint8_t> data_;
39     std::string deviceId_ = "7001005458323933328a258f413b3900";
40     uint64_t sequenceId_ = 10;
41     uint64_t sequenceId_2 = 20;
42     uint64_t sequenceId_3 = 30;
43     std::string userId_ = "100";
44     std::string bundleName_ = "test_bundleName";
45     ObjectStore::AssetBindInfo assetBindInfo_;
46     pid_t pid_ = 10;
47     uint32_t tokenId_ = 100;
48 };
49 
SetUp()50 void ObjectServiceImplTest::SetUp()
51 {
52     uri_ = "file:://com.examples.hmos.notepad/data/storage/el2/distributedfiles/dir/asset1.jpg";
53     ObjectStore::Asset asset{
54         .id = "test_name",
55         .name = uri_,
56         .uri = uri_,
57         .createTime = "2025.03.05",
58         .modifyTime = "modifyTime",
59         .size = "size",
60         .hash = "modifyTime_size",
61         .path = "/data/storage/el2",
62     };
63     asset_ = asset;
64 
65     data_.push_back(sequenceId_);
66     data_.push_back(sequenceId_2);
67     data_.push_back(sequenceId_3);
68 
69     ObjectStore::AssetBindInfo AssetBindInfo{
70         .storeName = "store_test",
71         .tableName = "table_test",
72         .field = "attachment",
73         .assetName = "asset1.jpg",
74     };
75     assetBindInfo_ = AssetBindInfo;
76 }
77 
TearDown()78 void ObjectServiceImplTest::TearDown() {}
79 
80 /**
81  * @tc.name: OnAssetChanged001
82  * @tc.desc: OnAssetChanged test.
83  * @tc.type: FUNC
84  */
85 HWTEST_F(ObjectServiceImplTest, OnAssetChanged001, TestSize.Level1)
86 {
87     std::string bundleName = "com.examples.hmos.notepad";
88     OHOS::Security::AccessToken::AccessTokenID tokenId =
89         OHOS::Security::AccessToken::AccessTokenKit::GetHapTokenID(100, bundleName, 0);
90         SetSelfTokenID(tokenId);
91 
92     std::shared_ptr<ObjectServiceImpl> objectServiceImpl = std::make_shared<ObjectServiceImpl>();
93 
94     // bundleName not equal tokenId
95     auto ret = objectServiceImpl->OnAssetChanged(bundleName_, sessionId_, deviceId_, asset_);
96     EXPECT_EQ(ret, OBJECT_PERMISSION_DENIED);
97     ret = objectServiceImpl->OnAssetChanged(bundleName, sessionId_, deviceId_, asset_);
98     EXPECT_NE(ret, OBJECT_SUCCESS);
99 }
100 
101 /**
102  * @tc.name: BindAssetStore001
103  * @tc.desc: BindAssetStore test.
104  * @tc.type: FUNC
105  */
106 HWTEST_F(ObjectServiceImplTest, BindAssetStore001, TestSize.Level1)
107 {
108     std::string bundleName = "com.examples.hmos.notepad";
109     OHOS::Security::AccessToken::AccessTokenID tokenId =
110         OHOS::Security::AccessToken::AccessTokenKit::GetHapTokenID(100, bundleName, 0);
111         SetSelfTokenID(tokenId);
112 
113     std::shared_ptr<ObjectServiceImpl> objectServiceImpl = std::make_shared<ObjectServiceImpl>();
114 
115     // bundleName not equal tokenId
116     auto ret = objectServiceImpl->BindAssetStore(bundleName_, sessionId_, asset_, assetBindInfo_);
117     EXPECT_EQ(ret, OBJECT_PERMISSION_DENIED);
118     ret = objectServiceImpl->BindAssetStore(bundleName, sessionId_, asset_, assetBindInfo_);
119     EXPECT_NE(ret, OBJECT_SUCCESS);
120 }
121 
122 /**
123  * @tc.name: DeleteSnapshot001
124  * @tc.desc: DeleteSnapshot test.
125  * @tc.type: FUNC
126  */
127 HWTEST_F(ObjectServiceImplTest, DeleteSnapshot001, TestSize.Level1)
128 {
129     std::string bundleName = "com.examples.hmos.notepad";
130     OHOS::Security::AccessToken::AccessTokenID tokenId =
131         OHOS::Security::AccessToken::AccessTokenKit::GetHapTokenID(100, bundleName, 0);
132         SetSelfTokenID(tokenId);
133 
134     std::shared_ptr<ObjectServiceImpl> objectServiceImpl = std::make_shared<ObjectServiceImpl>();
135 
136     // bundleName not equal tokenId
137     auto ret = objectServiceImpl->DeleteSnapshot(bundleName_, sessionId_);
138     EXPECT_EQ(ret, OBJECT_PERMISSION_DENIED);
139 }
140 }