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 #include <gtest/gtest.h>
17 #include <system_ability_definition.h>
18
19 #include "accesstoken_kit.h"
20 #include "disk.h"
21 #include "message_parcel.h"
22 #include "mock/uece_activation_callback_mock.h"
23 #include "storage_manager.h"
24 #include "storage_manager_provider.h"
25 #include "storage_service_errno.h"
26 #include "test/common/help_utils.h"
27 #include "user/multi_user_manager_service.h"
28 #include "volume_core.h"
29 #include <array>
30 #include <cstdlib>
31 #include <cstring>
32 #include <gtest/gtest.h>
33 #include <map>
34 #include <memory>
35 #include <mutex>
36 #include <string>
37 #include <thread>
38 #include <tuple>
39 #include <vector>
40 namespace OHOS {
41
42 int g_pStatus = -1;
43
44 namespace Security::AccessToken {
VerifyAccessToken(AccessTokenID tokenID,const std::string & permissionName)45 int AccessTokenKit::VerifyAccessToken(AccessTokenID tokenID, const std::string &permissionName)
46 {
47 return g_pStatus ;
48 }
49 } // namespace Security::AccessToken
50 namespace StorageManager {
51 using namespace testing;
52 using namespace testing::ext;
53 class StorageManagerProviderTest : public testing::Test {
54 public:
SetUpTestCase(void)55 static void SetUpTestCase(void){};
TearDownTestCase(void)56 static void TearDownTestCase(void){};
57 void SetUp();
58 void TearDown();
59
60 StorageManagerProvider *storageManagerProviderTest_;
61 };
62
SetUp(void)63 void StorageManagerProviderTest::SetUp(void)
64 {
65 storageManagerProviderTest_ = new StorageManagerProvider(STORAGE_MANAGER_MANAGER_ID);
66 }
67
TearDown(void)68 void StorageManagerProviderTest::TearDown(void)
69 {
70 if (storageManagerProviderTest_ != nullptr) {
71 delete storageManagerProviderTest_;
72 storageManagerProviderTest_ = nullptr;
73 }
74 }
75
76 /**
77 * @tc.name: StorageManagerProviderTest_MountFileMgrFuse_001
78 * @tc.desc: Verify the MountFileMgrFuse function.
79 * @tc.type: FUNC
80 * @tc.require: AR000H09L6
81 */
82 HWTEST_F(StorageManagerProviderTest, StorageManagerProviderTest_MountFileMgrFuse_001, TestSize.Level1)
83 {
84 GTEST_LOG_(INFO) << "StorageManagerProviderTest_MountFileMgrFuse_001 start";
85 ASSERT_TRUE(storageManagerProviderTest_ != nullptr);
86 int32_t userId = 1001;
87 std::string path = "../mnt/mtp/device/storage/usb";
88 int32_t fuseFd = -1;
89 g_pStatus = Security::AccessToken::PermissionState::PERMISSION_GRANTED;
90 auto ret = storageManagerProviderTest_->MountFileMgrFuse(userId, path, fuseFd);
91 EXPECT_EQ(ret, E_PARAMS_INVALID);
92 EXPECT_EQ(fuseFd, -1);
93 GTEST_LOG_(INFO) << "StorageManagerProviderTest_MountFileMgrFuse_001 end";
94 }
95
96 /**
97 * @tc.name: StorageManagerProviderTest_UMountFileMgrFuse_001
98 * @tc.desc: Verify the UMountFileMgrFuse function.
99 * @tc.type: FUNC
100 * @tc.require: AR000H09L6
101 */
102 HWTEST_F(StorageManagerProviderTest, StorageManagerProviderTest_UMountFileMgrFuse_001, TestSize.Level1)
103 {
104 GTEST_LOG_(INFO) << "StorageManagerProviderTest_UMountFileMgrFuse_001 start";
105 ASSERT_TRUE(storageManagerProviderTest_ != nullptr);
106 int32_t userId = 1001;
107 std::string path = "../mnt/mtp/device/storage/usb";
108 g_pStatus = Security::AccessToken::PermissionState::PERMISSION_GRANTED;
109 auto ret = storageManagerProviderTest_->UMountFileMgrFuse(userId, path);
110 EXPECT_EQ(ret, E_PARAMS_INVALID);
111 GTEST_LOG_(INFO) << "StorageManagerProviderTest_UMountFileMgrFuse_001 end";
112 }
113
114 /**
115 * @tc.name: StorageManagerProviderTest_IsFileOccupied_001
116 * @tc.desc: Verify the IsFileOccupied function.
117 * @tc.type: FUNC
118 * @tc.require: AR000H09L6
119 */
120 HWTEST_F(StorageManagerProviderTest, StorageManagerProviderTest_IsFileOccupied_001, TestSize.Level1)
121 {
122 GTEST_LOG_(INFO) << "StorageManagerProviderTest_IsFileOccupied_001 start";
123 ASSERT_TRUE(storageManagerProviderTest_ != nullptr);
124 std::string path = "../test/file";
125 std::vector<std::string> inputList = {"file1", "file2"};
126 std::vector<std::string> outputList;
127 bool isOccupy = false;
128 g_pStatus = Security::AccessToken::PermissionState::PERMISSION_GRANTED;
129 auto ret = storageManagerProviderTest_->IsFileOccupied(path, inputList, outputList, isOccupy);
130 EXPECT_EQ(ret, E_PARAMS_INVALID);
131 GTEST_LOG_(INFO) << "StorageManagerProviderTest_IsFileOccupied_001 end";
132 }
133
134 /**
135 * @tc.name: StorageManagerProviderTest_QueryUsbIsInUse_001
136 * @tc.desc: Verify the QueryUsbIsInUse function.
137 * @tc.type: FUNC
138 * @tc.require: AR000H09L6
139 */
140 HWTEST_F(StorageManagerProviderTest, StorageManagerProviderTest_QueryUsbIsInUse_001, TestSize.Level1)
141 {
142 GTEST_LOG_(INFO) << "StorageManagerProviderTest_QueryUsbIsInUse_001 start";
143 ASSERT_TRUE(storageManagerProviderTest_ != nullptr);
144 std::string diskPath = "../dev/sda1";
145 bool isInUse = false;
146 g_pStatus = Security::AccessToken::PermissionState::PERMISSION_GRANTED;
147 auto ret = storageManagerProviderTest_->QueryUsbIsInUse(diskPath, isInUse);
148 EXPECT_EQ(ret, E_PARAMS_INVALID);
149 GTEST_LOG_(INFO) << "StorageManagerProviderTest_QueryUsbIsInUse_001 end";
150 }
151
152 /**
153 * @tc.name: StorageManagerProviderTest_IsFilePathInvalid_001
154 * @tc.desc: Verify the IsFilePathInvalid function.
155 * @tc.type: FUNC
156 * @tc.require: AR000H09L6
157 */
158 HWTEST_F(StorageManagerProviderTest, StorageManagerProviderTest_IsFilePathInvalid_001, TestSize.Level1)
159 {
160 GTEST_LOG_(INFO) << "StorageManagerProviderTest_IsFilePathInvalid_001 start";
161 ASSERT_TRUE(storageManagerProviderTest_ != nullptr);
162 const std::array<std::tuple<std::string, bool>, 15> testCases = {
163 std::make_tuple<std::string, bool>("/valid/path/file.txt", true),
164 std::make_tuple<std::string, bool>("../invalid/path", false),
165 std::make_tuple<std::string, bool>("/valid/../path", false),
166 std::make_tuple<std::string, bool>("/path/to/..", false),
167 std::make_tuple<std::string, bool>("", true),
168 std::make_tuple<std::string, bool>("/", true),
169 std::make_tuple<std::string, bool>("../a/../b", false),
170 std::make_tuple<std::string, bool>("a/../b", false),
171 std::make_tuple<std::string, bool>("../a/b", false),
172 std::make_tuple<std::string, bool>("//../a", false),
173 std::make_tuple<std::string, bool>("/valid%path/file.txt", true),
174 std::make_tuple<std::string, bool>("a../b", true),
175 std::make_tuple<std::string, bool>("../", false),
176 std::make_tuple<std::string, bool>("/path/..more", true),
177 std::make_tuple<std::string, bool>("/a/..b", true)};
178 for (auto testCase : testCases) {
179 const std::string &input = std::get<0>(testCase);
180 const bool expected = std::get<1>(testCase);
181 EXPECT_NE(storageManagerProviderTest_->IsFilePathInvalid(input), expected);
182 }
183 GTEST_LOG_(INFO) << "StorageManagerProviderTest_IsFilePathInvalid_001 end";
184 }
185 } // namespace StorageManager
186 } // namespace OHOS
187