• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     };
70 }
71 
72 class StorageManagerStubTest : public testing::Test {
73 public:
SetUpTestCase(void)74     static void SetUpTestCase(void) {};
TearDownTestCase(void)75     static void TearDownTestCase(void) {};
SetUp()76     void SetUp() {};
TearDown()77     void TearDown() {};
78 };
79 
80 /**
81  * @tc.name: Storage_Manager_StorageManagerStubTest_OnRemoteRequest_001
82  * @tc.desc: Verify the OnRemoteRequest function with error descriptor.
83  * @tc.type: FUNC
84  * @tc.require: AR000GK4HB
85  */
86 HWTEST_F(StorageManagerStubTest, Storage_Manager_StorageManagerStubTest_OnRemoteRequest_001, TestSize.Level1)
87 {
88     GTEST_LOG_(INFO) << "Storage_Manager_StorageManagerStubTest_OnRemoteRequest_001 start";
89 
90     std::vector<string> perms;
91     perms.push_back("ohos.permission.STORAGE_MANAGER");
92     perms.push_back("ohos.permission.MOUNT_UNMOUNT_MANAGER");
93     perms.push_back("ohos.permission.MOUNT_FORMAT_MANAGER");
94     uint64_t tokenId = 0;
95     PermissionUtilsTest::SetAccessTokenPermission("StorageManagerPxyTest", perms, tokenId);
96     ASSERT_TRUE(tokenId != 0);
97 
98     StorageManagerStubMock mock;
99 
100     MessageParcel data;
101     MessageParcel reply;
102     MessageOption option(MessageOption::TF_SYNC);
103     bool bRet = data.WriteInterfaceToken(u"error descriptor");
104     EXPECT_TRUE(bRet) << "write token error";
105 
106     int32_t ret = mock.OnRemoteRequest(static_cast<int32_t>(StorageManagerInterfaceCode::PREPARE_ADD_USER), data,
107         reply, option);
108     EXPECT_TRUE(ret == E_PERMISSION_DENIED) << "descriptor error";
109 
110     GTEST_LOG_(INFO) << "Storage_Manager_StorageManagerStubTest_OnRemoteRequest_001 end";
111 }
112 
113 /**
114  * @tc.name: Storage_Manager_StorageManagerStubTest_OnRemoteRequest_002
115  * @tc.desc: Verify the OnRemoteRequest function with error code.
116  * @tc.type: FUNC
117  * @tc.require: AR000GK4HB
118  */
119 HWTEST_F(StorageManagerStubTest, Storage_Manager_StorageManagerStubTest_OnRemoteRequest_002, TestSize.Level1)
120 {
121     GTEST_LOG_(INFO) << "Storage_Manager_StorageManagerStubTest_OnRemoteRequest_002 start";
122 
123     StorageManagerStubMock mock;
124 
125     MessageParcel data;
126     MessageParcel reply;
127     MessageOption option(MessageOption::TF_SYNC);
128 
129     bool bRet = data.WriteInterfaceToken(StorageManagerProxy::GetDescriptor());
130     EXPECT_TRUE(bRet) << "write token error";
131 
132     int32_t ret = mock.OnRemoteRequest(ERROR_CODE, data, reply, option);
133     EXPECT_TRUE(ret != E_OK) << "request code error";
134 
135     GTEST_LOG_(INFO) << "Storage_Manager_StorageManagerStubTest_OnRemoteRequest_002 end";
136 }
137 
138 /**
139  * @tc.name: Storage_Manager_StorageManagerStubTest_OnRemoteRequest_003
140  * @tc.desc: Verify the OnRemoteRequest function.
141  * @tc.type: FUNC
142  * @tc.require: AR000GK4HB
143  */
144 HWTEST_F(StorageManagerStubTest, Storage_Manager_StorageManagerStubTest_OnRemoteRequest_003, TestSize.Level1)
145 {
146     GTEST_LOG_(INFO) << "Storage_Manager_StorageManagerStubTest_OnRemoteRequest_003 start";
147 
148     StorageManagerStubMock mock;
149 
150     EXPECT_CALL(mock, PrepareAddUser(testing::_, testing::_)).WillOnce(testing::Return(E_OK));
151     EXPECT_CALL(mock, RemoveUser(testing::_, testing::_)).WillOnce(testing::Return(E_OK));
152     EXPECT_CALL(mock, PrepareStartUser(testing::_)).WillOnce(testing::Return(E_OK));
153     EXPECT_CALL(mock, StopUser(testing::_)).WillOnce(testing::Return(E_OK));
154     EXPECT_CALL(mock, NotifyVolumeCreated(testing::_)).WillOnce(testing::Return(E_OK));
155     EXPECT_CALL(mock, NotifyVolumeMounted(testing::_, testing::_, testing::_, testing::_, testing::_))
156         .WillOnce(testing::Return(E_OK));
157     EXPECT_CALL(mock, NotifyVolumeStateChanged(testing::_, testing::_)).WillOnce(testing::Return(E_OK));
158     EXPECT_CALL(mock, Mount(testing::_)).WillOnce(testing::Return(E_OK));
159     EXPECT_CALL(mock, Unmount(testing::_)).WillOnce(testing::Return(E_OK));
160     EXPECT_CALL(mock, NotifyDiskCreated(testing::_)).WillOnce(testing::Return(E_OK));
161     EXPECT_CALL(mock, NotifyDiskDestroyed(testing::_)).WillOnce(testing::Return(E_OK));
162     EXPECT_CALL(mock, Partition(testing::_, testing::_)).WillOnce(testing::Return(E_OK));
163     EXPECT_CALL(mock, GetVolumeByUuid(testing::_, testing::_)).WillOnce(testing::Return(E_OK));
164     EXPECT_CALL(mock, GetVolumeById(testing::_, testing::_)).WillOnce(testing::Return(E_OK));
165     EXPECT_CALL(mock, SetVolumeDescription(testing::_, testing::_)).WillOnce(testing::Return(E_OK));
166     EXPECT_CALL(mock, Format(testing::_, testing::_)).WillOnce(testing::Return(E_OK));
167     EXPECT_CALL(mock, GetDiskById(testing::_, testing::_)).WillOnce(testing::Return(E_OK));
168     EXPECT_CALL(mock, GenerateUserKeys(testing::_, testing::_)).WillOnce(testing::Return(E_OK));
169     EXPECT_CALL(mock, DeleteUserKeys(testing::_)).WillOnce(testing::Return(E_OK));
170     EXPECT_CALL(mock, UpdateUserAuth(testing::_, testing::_, testing::_, testing::_, testing::_))
171         .WillOnce(testing::Return(E_OK));
172     EXPECT_CALL(mock, ActiveUserKey(testing::_, testing::_, testing::_)).WillOnce(testing::Return(E_OK));
173     EXPECT_CALL(mock, InactiveUserKey(testing::_)).WillOnce(testing::Return(E_OK));
174     EXPECT_CALL(mock, UpdateKeyContext(testing::_)).WillOnce(testing::Return(E_OK));
175     EXPECT_CALL(mock, GetFreeSizeOfVolume(testing::_, testing::_)).WillOnce(testing::Return(E_OK));
176     EXPECT_CALL(mock, GetTotalSizeOfVolume(testing::_, testing::_)).WillOnce(testing::Return(E_OK));
177     EXPECT_CALL(mock, GetSystemSize(testing::_)).WillOnce(testing::Return(E_OK));
178     EXPECT_CALL(mock, GetTotalSize(testing::_)).WillOnce(testing::Return(E_OK));
179     EXPECT_CALL(mock, GetFreeSize(testing::_)).WillOnce(testing::Return(E_OK));
180     EXPECT_CALL(mock, GetBundleStats(testing::_, testing::_)).WillOnce(testing::Return(E_OK));
181     EXPECT_CALL(mock, GetCurrentBundleStats(testing::_)).WillOnce(testing::Return(E_OK));
182     EXPECT_CALL(mock, GetUserStorageStats(testing::_)).WillOnce(testing::Return(E_OK));
183     EXPECT_CALL(mock, GetUserStorageStats(testing::_, testing::_)).WillOnce(testing::Return(E_OK));
184     EXPECT_CALL(mock, GetAllVolumes(testing::_)).WillOnce(testing::Return(E_OK));
185     EXPECT_CALL(mock, GetAllDisks(testing::_)).WillOnce(testing::Return(E_OK));
186 
187     for (auto c : g_code) {
188         MessageParcel data;
189         MessageParcel reply;
190         MessageOption option(MessageOption::TF_SYNC);
191         bool bRet = data.WriteInterfaceToken(StorageManagerProxy::GetDescriptor());
192         EXPECT_TRUE(bRet) << "write token error";
193         int32_t ret = mock.OnRemoteRequest(c, data, reply, option);
194         EXPECT_TRUE(ret == E_OK);
195         EXPECT_TRUE(reply.ReadInt32() == E_OK);
196     }
197 
198     GTEST_LOG_(INFO) << "Storage_Manager_StorageManagerStubTest_OnRemoteRequest_003 end";
199 }
200 } // STORAGE_MANAGER
201 } // OHOS
202