1 /* 2 * Copyright (c) 2021 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 "gmock/gmock.h" 18 19 #include "storage_manager_ipc_interface_code.h" 20 #include "storage_manager_proxy.h" 21 #include "ipc/storage_manager_stub.h" 22 #include "storage_manager_stub_mock.h" 23 #include "get_self_permissions.h" 24 25 #include "storage_service_errno.h" 26 #include "storage_service_log.h" 27 28 namespace OHOS { 29 namespace StorageManager { 30 using namespace testing::ext; 31 32 namespace { 33 const int ERROR_CODE = 99999; 34 int32_t g_code[] = { 35 static_cast<int32_t>(StorageManagerInterfaceCode::PREPARE_ADD_USER), 36 static_cast<int32_t>(StorageManagerInterfaceCode::REMOVE_USER), 37 static_cast<int32_t>(StorageManagerInterfaceCode::PREPARE_START_USER), 38 static_cast<int32_t>(StorageManagerInterfaceCode::STOP_USER), 39 static_cast<int32_t>(StorageManagerInterfaceCode::NOTIFY_VOLUME_CREATED), 40 static_cast<int32_t>(StorageManagerInterfaceCode::NOTIFY_VOLUME_MOUNTED), 41 static_cast<int32_t>(StorageManagerInterfaceCode::NOTIFY_VOLUME_STATE_CHANGED), 42 static_cast<int32_t>(StorageManagerInterfaceCode::MOUNT), 43 static_cast<int32_t>(StorageManagerInterfaceCode::UNMOUNT), 44 static_cast<int32_t>(StorageManagerInterfaceCode::NOTIFY_DISK_CREATED), 45 static_cast<int32_t>(StorageManagerInterfaceCode::NOTIFY_DISK_DESTROYED), 46 static_cast<int32_t>(StorageManagerInterfaceCode::PARTITION), 47 static_cast<int32_t>(StorageManagerInterfaceCode::CREATE_USER_KEYS), 48 static_cast<int32_t>(StorageManagerInterfaceCode::DELETE_USER_KEYS), 49 static_cast<int32_t>(StorageManagerInterfaceCode::UPDATE_USER_AUTH), 50 static_cast<int32_t>(StorageManagerInterfaceCode::ACTIVE_USER_KEY), 51 static_cast<int32_t>(StorageManagerInterfaceCode::INACTIVE_USER_KEY), 52 static_cast<int32_t>(StorageManagerInterfaceCode::UPDATE_KEY_CONTEXT), 53 static_cast<int32_t>(StorageManagerInterfaceCode::GET_VOL_BY_UUID), 54 static_cast<int32_t>(StorageManagerInterfaceCode::GET_VOL_BY_ID), 55 static_cast<int32_t>(StorageManagerInterfaceCode::SET_VOL_DESC), 56 static_cast<int32_t>(StorageManagerInterfaceCode::FORMAT), 57 static_cast<int32_t>(StorageManagerInterfaceCode::GET_DISK_BY_ID), 58 static_cast<int32_t>(StorageManagerInterfaceCode::GET_TOTAL), 59 static_cast<int32_t>(StorageManagerInterfaceCode::GET_FREE), 60 static_cast<int32_t>(StorageManagerInterfaceCode::GET_SYSTEM_SIZE), 61 static_cast<int32_t>(StorageManagerInterfaceCode::GET_TOTAL_SIZE), 62 static_cast<int32_t>(StorageManagerInterfaceCode::GET_FREE_SIZE), 63 static_cast<int32_t>(StorageManagerInterfaceCode::GET_BUNDLE_STATUS), 64 static_cast<int32_t>(StorageManagerInterfaceCode::GET_CURR_BUNDLE_STATS), 65 static_cast<int32_t>(StorageManagerInterfaceCode::GET_CURR_USER_STATS), 66 static_cast<int32_t>(StorageManagerInterfaceCode::GET_USER_STATS), 67 static_cast<int32_t>(StorageManagerInterfaceCode::GET_ALL_VOLUMES), 68 static_cast<int32_t>(StorageManagerInterfaceCode::GET_ALL_DISKS), 69 static_cast<int32_t>(StorageManagerInterfaceCode::LOCK_USER_SCREEN), 70 static_cast<int32_t>(StorageManagerInterfaceCode::UNLOCK_USER_SCREEN), 71 static_cast<int32_t>(StorageManagerInterfaceCode::LOCK_SCREEN_STATUS), 72 static_cast<int32_t>(StorageManagerInterfaceCode::SET_BUNDLE_QUOTA), 73 static_cast<int32_t>(StorageManagerInterfaceCode::GENERATE_APP_KEY), 74 static_cast<int32_t>(StorageManagerInterfaceCode::DELETE_APP_KEY), 75 }; 76 } 77 78 class StorageManagerStubTest : public testing::Test { 79 public: SetUpTestCase(void)80 static void SetUpTestCase(void) {}; TearDownTestCase(void)81 static void TearDownTestCase(void) {}; SetUp()82 void SetUp() {}; TearDown()83 void TearDown() {}; 84 }; 85 86 /** 87 * @tc.name: Storage_Manager_StorageManagerStubTest_OnRemoteRequest_001 88 * @tc.desc: Verify the OnRemoteRequest function with error descriptor. 89 * @tc.type: FUNC 90 * @tc.require: AR000GK4HB 91 */ 92 HWTEST_F(StorageManagerStubTest, Storage_Manager_StorageManagerStubTest_OnRemoteRequest_001, TestSize.Level1) 93 { 94 GTEST_LOG_(INFO) << "Storage_Manager_StorageManagerStubTest_OnRemoteRequest_001 start"; 95 96 std::vector<string> perms; 97 perms.push_back("ohos.permission.STORAGE_MANAGER"); 98 perms.push_back("ohos.permission.MOUNT_UNMOUNT_MANAGER"); 99 perms.push_back("ohos.permission.MOUNT_FORMAT_MANAGER"); 100 uint64_t tokenId = 0; 101 PermissionUtilsTest::SetAccessTokenPermission("StorageManagerPxyTest", perms, tokenId); 102 ASSERT_TRUE(tokenId != 0); 103 104 StorageManagerStubMock mock; 105 106 MessageParcel data; 107 MessageParcel reply; 108 MessageOption option(MessageOption::TF_SYNC); 109 bool bRet = data.WriteInterfaceToken(u"error descriptor"); 110 EXPECT_TRUE(bRet) << "write token error"; 111 112 int32_t ret = mock.OnRemoteRequest(static_cast<int32_t>(StorageManagerInterfaceCode::PREPARE_ADD_USER), data, 113 reply, option); 114 EXPECT_TRUE(ret == E_PERMISSION_DENIED) << "descriptor error"; 115 116 GTEST_LOG_(INFO) << "Storage_Manager_StorageManagerStubTest_OnRemoteRequest_001 end"; 117 } 118 119 /** 120 * @tc.name: Storage_Manager_StorageManagerStubTest_OnRemoteRequest_002 121 * @tc.desc: Verify the OnRemoteRequest function with error code. 122 * @tc.type: FUNC 123 * @tc.require: AR000GK4HB 124 */ 125 HWTEST_F(StorageManagerStubTest, Storage_Manager_StorageManagerStubTest_OnRemoteRequest_002, TestSize.Level1) 126 { 127 GTEST_LOG_(INFO) << "Storage_Manager_StorageManagerStubTest_OnRemoteRequest_002 start"; 128 129 StorageManagerStubMock mock; 130 131 MessageParcel data; 132 MessageParcel reply; 133 MessageOption option(MessageOption::TF_SYNC); 134 135 bool bRet = data.WriteInterfaceToken(StorageManagerProxy::GetDescriptor()); 136 EXPECT_TRUE(bRet) << "write token error"; 137 138 int32_t ret = mock.OnRemoteRequest(ERROR_CODE, data, reply, option); 139 EXPECT_TRUE(ret != E_OK) << "request code error"; 140 141 GTEST_LOG_(INFO) << "Storage_Manager_StorageManagerStubTest_OnRemoteRequest_002 end"; 142 } 143 144 /** 145 * @tc.name: Storage_Manager_StorageManagerStubTest_OnRemoteRequest_003 146 * @tc.desc: Verify the OnRemoteRequest function. 147 * @tc.type: FUNC 148 * @tc.require: AR000GK4HB 149 */ 150 HWTEST_F(StorageManagerStubTest, Storage_Manager_StorageManagerStubTest_OnRemoteRequest_003, TestSize.Level1) 151 { 152 GTEST_LOG_(INFO) << "Storage_Manager_StorageManagerStubTest_OnRemoteRequest_003 start"; 153 154 StorageManagerStubMock mock; 155 std::vector<string> perms; 156 perms.push_back("ohos.permission.STORAGE_MANAGER"); 157 perms.push_back("ohos.permission.STORAGE_MANAGER_CRYPT"); 158 perms.push_back("ohos.permission.MOUNT_UNMOUNT_MANAGER"); 159 perms.push_back("ohos.permission.MOUNT_FORMAT_MANAGER"); 160 uint64_t tokenId = 0; 161 PermissionUtilsTest::SetAccessTokenPermission("StorageManagerPxyTest", perms, tokenId); 162 ASSERT_TRUE(tokenId != 0); 163 EXPECT_CALL(mock, PrepareAddUser(testing::_, testing::_)).WillOnce(testing::Return(E_OK)); 164 EXPECT_CALL(mock, RemoveUser(testing::_, testing::_)).WillOnce(testing::Return(E_OK)); 165 EXPECT_CALL(mock, PrepareStartUser(testing::_)).WillOnce(testing::Return(E_OK)); 166 EXPECT_CALL(mock, StopUser(testing::_)).WillOnce(testing::Return(E_OK)); 167 EXPECT_CALL(mock, NotifyVolumeCreated(testing::_)).WillOnce(testing::Return(E_OK)); 168 EXPECT_CALL(mock, NotifyVolumeMounted(testing::_, testing::_, testing::_, testing::_, testing::_)) 169 .WillOnce(testing::Return(E_OK)); 170 EXPECT_CALL(mock, NotifyVolumeStateChanged(testing::_, testing::_)).WillOnce(testing::Return(E_OK)); 171 EXPECT_CALL(mock, Mount(testing::_)).WillOnce(testing::Return(E_OK)); 172 EXPECT_CALL(mock, Unmount(testing::_)).WillOnce(testing::Return(E_OK)); 173 EXPECT_CALL(mock, NotifyDiskCreated(testing::_)).WillOnce(testing::Return(E_OK)); 174 EXPECT_CALL(mock, NotifyDiskDestroyed(testing::_)).WillOnce(testing::Return(E_OK)); 175 EXPECT_CALL(mock, Partition(testing::_, testing::_)).WillOnce(testing::Return(E_OK)); 176 EXPECT_CALL(mock, GetVolumeByUuid(testing::_, testing::_)).WillOnce(testing::Return(E_OK)); 177 EXPECT_CALL(mock, GetVolumeById(testing::_, testing::_)).WillOnce(testing::Return(E_OK)); 178 EXPECT_CALL(mock, SetVolumeDescription(testing::_, testing::_)).WillOnce(testing::Return(E_OK)); 179 EXPECT_CALL(mock, Format(testing::_, testing::_)).WillOnce(testing::Return(E_OK)); 180 EXPECT_CALL(mock, GetDiskById(testing::_, testing::_)).WillOnce(testing::Return(E_OK)); 181 EXPECT_CALL(mock, GenerateUserKeys(testing::_, testing::_)).WillOnce(testing::Return(E_OK)); 182 EXPECT_CALL(mock, DeleteUserKeys(testing::_)).WillOnce(testing::Return(E_OK)); 183 EXPECT_CALL(mock, UpdateUserAuth(testing::_, testing::_, testing::_, testing::_, testing::_)) 184 .WillOnce(testing::Return(E_OK)); 185 EXPECT_CALL(mock, ActiveUserKey(testing::_, testing::_, testing::_)).WillOnce(testing::Return(E_OK)); 186 EXPECT_CALL(mock, InactiveUserKey(testing::_)).WillOnce(testing::Return(E_OK)); 187 EXPECT_CALL(mock, UpdateKeyContext(testing::_)).WillOnce(testing::Return(E_OK)); 188 EXPECT_CALL(mock, GetFreeSizeOfVolume(testing::_, testing::_)).WillOnce(testing::Return(E_OK)); 189 EXPECT_CALL(mock, GetTotalSizeOfVolume(testing::_, testing::_)).WillOnce(testing::Return(E_OK)); 190 EXPECT_CALL(mock, GetSystemSize(testing::_)).WillOnce(testing::Return(E_OK)); 191 EXPECT_CALL(mock, GetTotalSize(testing::_)).WillOnce(testing::Return(E_OK)); 192 EXPECT_CALL(mock, GetFreeSize(testing::_)).WillOnce(testing::Return(E_OK)); 193 EXPECT_CALL(mock, GetBundleStats(testing::_, testing::_, testing::_, testing::_)).WillOnce(testing::Return(E_OK)); 194 EXPECT_CALL(mock, GetCurrentBundleStats(testing::_, testing::_)).WillOnce(testing::Return(E_OK)); 195 EXPECT_CALL(mock, GetUserStorageStats(testing::_)).WillOnce(testing::Return(E_OK)); 196 EXPECT_CALL(mock, GetUserStorageStats(testing::_, testing::_)).WillOnce(testing::Return(E_OK)); 197 EXPECT_CALL(mock, GetAllVolumes(testing::_)).WillOnce(testing::Return(E_OK)); 198 EXPECT_CALL(mock, GetAllDisks(testing::_)).WillOnce(testing::Return(E_OK)); 199 EXPECT_CALL(mock, LockUserScreen(testing::_)).WillOnce(testing::Return(E_OK)); 200 EXPECT_CALL(mock, UnlockUserScreen(testing::_, testing::_, testing::_)).WillOnce(testing::Return(E_OK)); 201 EXPECT_CALL(mock, GetLockScreenStatus(testing::_, testing::_)).WillOnce(testing::Return(E_OK)); 202 EXPECT_CALL(mock, SetBundleQuota(testing::_, testing::_, testing::_, testing::_)).WillOnce(testing::Return(E_OK)); 203 EXPECT_CALL(mock, GenerateAppkey(testing::_, testing::_, testing::_)).WillOnce(testing::Return(E_OK)); 204 EXPECT_CALL(mock, DeleteAppkey(testing::_)).WillOnce(testing::Return(E_OK)); 205 206 for (auto c : g_code) { 207 MessageParcel data; 208 MessageParcel reply; 209 MessageOption option(MessageOption::TF_SYNC); 210 bool bRet = data.WriteInterfaceToken(StorageManagerProxy::GetDescriptor()); 211 EXPECT_TRUE(bRet) << "write token error"; 212 int32_t ret = mock.OnRemoteRequest(c, data, reply, option); 213 EXPECT_TRUE(ret == E_OK); 214 EXPECT_TRUE(reply.ReadInt32() == E_OK); 215 } 216 217 GTEST_LOG_(INFO) << "Storage_Manager_StorageManagerStubTest_OnRemoteRequest_003 end"; 218 } 219 } // STORAGE_MANAGER 220 } // OHOS 221