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 } 35 36 class StorageManagerStubTest : public testing::Test { 37 public: SetUpTestCase(void)38 static void SetUpTestCase(void) {}; TearDownTestCase(void)39 static void TearDownTestCase(void) {}; SetUp()40 void SetUp() {}; TearDown()41 void TearDown() {}; 42 }; 43 44 /** 45 * @tc.name: Storage_Manager_StorageManagerStubTest_OnRemoteRequest_001 46 * @tc.desc: Verify the OnRemoteRequest function with error descriptor. 47 * @tc.type: FUNC 48 * @tc.require: AR000GK4HB 49 */ 50 HWTEST_F(StorageManagerStubTest, Storage_Manager_StorageManagerStubTest_OnRemoteRequest_001, TestSize.Level1) 51 { 52 GTEST_LOG_(INFO) << "Storage_Manager_StorageManagerStubTest_OnRemoteRequest_001 start"; 53 54 std::vector<string> perms; 55 perms.push_back("ohos.permission.STORAGE_MANAGER"); 56 perms.push_back("ohos.permission.MOUNT_UNMOUNT_MANAGER"); 57 perms.push_back("ohos.permission.MOUNT_FORMAT_MANAGER"); 58 uint64_t tokenId = 0; 59 PermissionUtilsTest::SetAccessTokenPermission("StorageManagerPxyTest", perms, tokenId); 60 ASSERT_TRUE(tokenId != 0); 61 62 StorageManagerStubMock mock; 63 64 MessageParcel data; 65 MessageParcel reply; 66 MessageOption option(MessageOption::TF_SYNC); 67 bool bRet = data.WriteInterfaceToken(u"error descriptor"); 68 EXPECT_TRUE(bRet) << "write token error"; 69 70 int32_t ret = mock.OnRemoteRequest(static_cast<int32_t>(StorageManagerInterfaceCode::PREPARE_ADD_USER), data, 71 reply, option); 72 EXPECT_TRUE(ret == E_PERMISSION_DENIED) << "descriptor error"; 73 74 GTEST_LOG_(INFO) << "Storage_Manager_StorageManagerStubTest_OnRemoteRequest_001 end"; 75 } 76 77 /** 78 * @tc.name: Storage_Manager_StorageManagerStubTest_OnRemoteRequest_002 79 * @tc.desc: Verify the OnRemoteRequest function with error code. 80 * @tc.type: FUNC 81 * @tc.require: AR000GK4HB 82 */ 83 HWTEST_F(StorageManagerStubTest, Storage_Manager_StorageManagerStubTest_OnRemoteRequest_002, TestSize.Level1) 84 { 85 GTEST_LOG_(INFO) << "Storage_Manager_StorageManagerStubTest_OnRemoteRequest_002 start"; 86 87 StorageManagerStubMock mock; 88 89 MessageParcel data; 90 MessageParcel reply; 91 MessageOption option(MessageOption::TF_SYNC); 92 93 bool bRet = data.WriteInterfaceToken(StorageManagerProxy::GetDescriptor()); 94 EXPECT_TRUE(bRet) << "write token error"; 95 96 int32_t ret = mock.OnRemoteRequest(ERROR_CODE, data, reply, option); 97 EXPECT_TRUE(ret != E_OK) << "request code error"; 98 99 GTEST_LOG_(INFO) << "Storage_Manager_StorageManagerStubTest_OnRemoteRequest_002 end"; 100 } 101 } // STORAGE_MANAGER 102 } // OHOS 103