• 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 #define LOG_TAG "UtdConcurrentUpdateTest"
16 #include <gtest/gtest.h>
17 
18 #include <unistd.h>
19 #include <thread>
20 
21 #include "token_setproc.h"
22 #include "accesstoken_kit.h"
23 #include "nativetoken_kit.h"
24 
25 #include "logger.h"
26 #include "utd_client.h"
27 #include "type_descriptor.h"
28 #include "preset_type_descriptors.h"
29 #include "utd_graph.h"
30 #include "custom_utd_store.h"
31 
32 using namespace testing::ext;
33 using namespace OHOS::Security::AccessToken;
34 using namespace OHOS::UDMF;
35 using namespace OHOS;
36 
37 namespace OHOS::Test {
38 constexpr const int32_t USERID = 100;
39 
40 class UtdConcurrentUpdateTest : public testing::Test {
41 public:
42     static void SetUpTestCase();
43     static void TearDownTestCase();
44     void SetUp() override;
45     void TearDown() override;
46     void SetNativeToken(const std::string &processName);
47 };
48 
SetUpTestCase()49 void UtdConcurrentUpdateTest::SetUpTestCase()
50 {
51 }
52 
TearDownTestCase()53 void UtdConcurrentUpdateTest::TearDownTestCase()
54 {
55 }
56 
SetUp()57 void UtdConcurrentUpdateTest::SetUp()
58 {
59     SetNativeToken("foundation");
60 }
61 
TearDown()62 void UtdConcurrentUpdateTest::TearDown()
63 {
64 }
65 
66 static constexpr const char *K_TEST_JSON_STR = R"({
67     "UniformDataTypeDeclarations": [
68         {
69             "TypeId": "com.example.thread.image",
70             "BelongingToTypes": ["general.image"],
71             "FilenameExtensions": [".myImage", ".khImage"],
72             "MIMETypes": ["application/myImage", "application/khImage"],
73             "Description": "My Image.",
74             "ReferenceURL": ""
75         },
76         {
77             "TypeId": "com.example.thread.audio",
78             "BelongingToTypes": ["general.audio"],
79             "FilenameExtensions": [".myAudio", ".khAudio"],
80             "MIMETypes": ["application/myAudio", "application/khAudio"],
81             "Description": "My audio.",
82             "ReferenceURL": ""
83         }
84     ]
85 })";
86 
SetNativeToken(const std::string & processName)87 void UtdConcurrentUpdateTest::SetNativeToken(const std::string &processName)
88 {
89     auto tokenId = AccessTokenKit::GetNativeTokenId(processName);
90     SetSelfTokenID(tokenId);
91 }
92 
ReplaceThread(const std::string & jsonStr,const std::string & newThreadId)93 std::string ReplaceThread(const std::string& jsonStr, const std::string& newThreadId)
94 {
95     std::string result = jsonStr;
96     std::string target = "com.example.thread";
97     size_t pos = 0;
98     while ((pos = result.find(target, pos)) != std::string::npos) {
99         result.replace(pos, target.length(), newThreadId);
100         pos += newThreadId.length();
101     }
102     return result;
103 }
104 
105 /**
106 * @tc.name: MultiThreadUpdate001
107 * @tc.desc: MultiThread Update
108 * @tc.type: FUNC
109 */
110 HWTEST_F(UtdConcurrentUpdateTest, MultiThreadUpdate001, TestSize.Level1)
111 {
112     const int numThreads = 8;
113     for (int i = 1; i <= numThreads; ++i) {
114         UtdClient::GetInstance().UninstallCustomUtds("com.example.thread" + std::to_string(i), USERID);
115     }
116     std::vector<TypeDescriptorCfg> prevCustomTypeCfgs = CustomUtdStore::GetInstance().GetCustomUtd(false, USERID);
117     size_t prevSize = prevCustomTypeCfgs.size();
118 
119     const int trials = 50;
120     for (int i = 0; i < trials; ++i) {
121         bool op = (i % 2 == 0);
__anonf23aa7d20102(int threadId) 122         auto worker = [op](int threadId) {
123             const std::string bundleName = "com.example.thread" + std::to_string(threadId);
124             auto jsonStr = ReplaceThread(K_TEST_JSON_STR, bundleName);
125             if (op) {
126                 UtdClient::GetInstance().InstallCustomUtds(bundleName, jsonStr, USERID);
127                 LOG_INFO(UDMF_CLIENT, "Thread %{public}d Install done", threadId);
128             } else {
129                 UtdClient::GetInstance().UninstallCustomUtds(bundleName, USERID);
130                 LOG_INFO(UDMF_CLIENT, "Thread %{public}d Uninstall done", threadId);
131             }
132         };
133         std::vector<std::thread> threads;
134         for (int j = 1; j <= numThreads; ++j) {
135             threads.emplace_back(worker, j);
136         }
137         for (auto &t : threads) {
138             t.join();
139         }
140 
141         auto customTypeCfgs = CustomUtdStore::GetInstance().GetCustomUtd(false, USERID);
142         if (op) {
143             EXPECT_EQ(customTypeCfgs.size(), numThreads * 2 + prevSize);
144         } else {
145             EXPECT_EQ(customTypeCfgs.size(), prevSize);
146         }
147         for (const auto &cfg : customTypeCfgs) {
148             if (std::find(prevCustomTypeCfgs.begin(), prevCustomTypeCfgs.end(), cfg) == prevCustomTypeCfgs.end()) {
149                 EXPECT_TRUE(cfg.typeId.find("com.example.thread") != std::string::npos);
150                 EXPECT_TRUE(cfg.belongingToTypes.size() > 0);
151                 EXPECT_TRUE(cfg.filenameExtensions.size() > 0);
152                 EXPECT_TRUE(cfg.mimeTypes.size() > 0);
153             }
154         }
155     }
156     for (int i = 1; i <= numThreads; ++i) {
157         UtdClient::GetInstance().UninstallCustomUtds("com.example.thread" + std::to_string(i), USERID);
158     }
159     auto customTypeCfgs = CustomUtdStore::GetInstance().GetCustomUtd(false, USERID);
160     EXPECT_EQ(customTypeCfgs.size(), prevSize);
161 };
162 } // namespace OHOS::Test