1 /* 2 * Copyright (c) 2022 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 18 #include "buffer.h" 19 #include "global_config_file_manager.h" 20 #include "idm_common.h" 21 #include "idm_file_manager.h" 22 23 extern "C" { 24 extern ResultCode CapacityExpansion(Buffer *object, uint32_t targetCapacity); 25 extern ResultCode StreamWrite(Buffer *parcel, void *from, uint32_t size); 26 extern ResultCode StreamWriteEnrolledInfo(Buffer *parcel, LinkedList *enrolledList); 27 extern ResultCode StreamWriteCredentialList(Buffer *parcel, LinkedList *credentialList); 28 extern ResultCode StreamWriteUserInfo(Buffer *parcel, UserInfo *userInfo); 29 extern ResultCode StreamRead(Buffer *parcel, uint32_t *index, void *to, uint32_t size); 30 extern ResultCode StreamReadCredentialList(Buffer *parcel, uint32_t *index, LinkedList *credentialList); 31 extern ResultCode StreamReadEnrolledList(Buffer *parcel, uint32_t *index, LinkedList *enrolledList); 32 } 33 34 namespace OHOS { 35 namespace UserIam { 36 namespace UserAuth { 37 using namespace testing; 38 using namespace testing::ext; 39 40 namespace { 41 constexpr uint32_t MAX_BUFFER_LEN = 512000; 42 } // namespace 43 44 class IdmFileMgrTest : public testing::Test { 45 public: SetUpTestCase()46 static void SetUpTestCase() {}; 47 TearDownTestCase()48 static void TearDownTestCase() {}; 49 SetUp()50 void SetUp() {}; 51 TearDown()52 void TearDown() {}; 53 }; 54 55 HWTEST_F(IdmFileMgrTest, TestCapacityExpansion_001, TestSize.Level0) 56 { 57 EXPECT_EQ(CapacityExpansion(nullptr, 0), RESULT_BAD_PARAM); 58 59 constexpr uint32_t bufferSize = 10; 60 Buffer *object = CreateBufferBySize(bufferSize); 61 object->maxSize = MAX_BUFFER_LEN; 62 EXPECT_EQ(CapacityExpansion(object, 0), RESULT_BAD_PARAM); 63 } 64 65 HWTEST_F(IdmFileMgrTest, TestCapacityExpansion_002, TestSize.Level0) 66 { 67 constexpr uint32_t bufferSize = 10; 68 Buffer *object = CreateBufferBySize(bufferSize); 69 EXPECT_EQ(CapacityExpansion(object, 0), RESULT_SUCCESS); 70 } 71 72 HWTEST_F(IdmFileMgrTest, TestCapacityExpansion_003, TestSize.Level0) 73 { 74 constexpr uint32_t bufferSize = 10; 75 Buffer *object = CreateBufferBySize(bufferSize); 76 constexpr uint32_t targerCapacity = 5120000; 77 EXPECT_EQ(CapacityExpansion(object, targerCapacity), RESULT_BAD_PARAM); 78 } 79 80 HWTEST_F(IdmFileMgrTest, TestStreamWrite_001, TestSize.Level0) 81 { 82 EXPECT_EQ(StreamWrite(nullptr, nullptr, 0), RESULT_BAD_PARAM); 83 84 constexpr uint32_t bufferSize = 10; 85 Buffer *parcel = CreateBufferBySize(bufferSize); 86 uint32_t from = 0; 87 constexpr uint32_t objectSize1 = 5120000; 88 EXPECT_EQ(StreamWrite(parcel, static_cast<void *>(&from), objectSize1), RESULT_BAD_PARAM); 89 90 constexpr uint32_t objectSize2 = 15; 91 uint8_t array[objectSize2]; 92 EXPECT_EQ(StreamWrite(parcel, static_cast<void *>(&array), objectSize2), RESULT_SUCCESS); 93 } 94 95 HWTEST_F(IdmFileMgrTest, TestStreamWrite_002, TestSize.Level0) 96 { 97 constexpr uint32_t bufferSize = 10; 98 Buffer *parcel = CreateBufferBySize(bufferSize); 99 constexpr uint32_t objectSize = 5; 100 101 uint8_t array[objectSize]; 102 EXPECT_EQ(StreamWrite(parcel, static_cast<void *>(&array), objectSize), RESULT_SUCCESS); 103 } 104 105 HWTEST_F(IdmFileMgrTest, TestStreamWriteEnrolledInfo, TestSize.Level0) 106 { 107 EXPECT_EQ(StreamWriteEnrolledInfo(nullptr, nullptr), RESULT_BAD_PARAM); 108 109 constexpr uint32_t bufferSize = 10; 110 Buffer *parcel = CreateBufferBySize(bufferSize); 111 LinkedList *enrolledList = CreateLinkedList(DestroyEnrolledNode); 112 EXPECT_NE(enrolledList, nullptr); 113 EXPECT_EQ(StreamWriteEnrolledInfo(parcel, enrolledList), RESULT_SUCCESS); 114 115 enrolledList->insert(enrolledList, nullptr); 116 EXPECT_EQ(StreamWriteEnrolledInfo(parcel, enrolledList), RESULT_GENERAL_ERROR); 117 } 118 119 HWTEST_F(IdmFileMgrTest, TestStreamWriteCredentialList, TestSize.Level0) 120 { 121 EXPECT_EQ(StreamWriteCredentialList(nullptr, nullptr), RESULT_BAD_PARAM); 122 123 constexpr uint32_t bufferSize = 10; 124 Buffer *parcel = CreateBufferBySize(bufferSize); 125 LinkedList *credentialList = CreateLinkedList(DestroyCredentialNode); 126 EXPECT_NE(credentialList, nullptr); 127 EXPECT_EQ(StreamWriteCredentialList(parcel, credentialList), RESULT_SUCCESS); 128 129 credentialList->insert(credentialList, nullptr); 130 EXPECT_EQ(StreamWriteCredentialList(parcel, credentialList), RESULT_GENERAL_ERROR); 131 } 132 133 HWTEST_F(IdmFileMgrTest, TestStreamWriteUserInfo, TestSize.Level0) 134 { 135 EXPECT_EQ(StreamWriteUserInfo(nullptr, nullptr), RESULT_BAD_PARAM); 136 } 137 138 HWTEST_F(IdmFileMgrTest, TestUpdateFileInfo, TestSize.Level0) 139 { 140 EXPECT_EQ(UpdateFileInfo(nullptr), RESULT_BAD_PARAM); 141 } 142 143 HWTEST_F(IdmFileMgrTest, TestStreamRead, TestSize.Level0) 144 { 145 constexpr uint32_t bufferSize = 10; 146 Buffer *parcel = CreateBufferBySize(bufferSize); 147 uint32_t index = 20; 148 constexpr uint32_t SIZE = 1000; 149 EXPECT_EQ(StreamRead(parcel, &index, nullptr, SIZE), RESULT_BAD_PARAM); 150 constexpr uint32_t contextSize = 200; 151 parcel->contentSize = contextSize; 152 EXPECT_EQ(StreamRead(parcel, &index, nullptr, SIZE), RESULT_BAD_PARAM); 153 } 154 155 HWTEST_F(IdmFileMgrTest, TestStreamReadCredentialList, TestSize.Level0) 156 { 157 EXPECT_EQ(StreamReadCredentialList(nullptr, nullptr, nullptr), RESULT_BAD_PARAM); 158 } 159 160 HWTEST_F(IdmFileMgrTest, TestStreamReadEnrolledList, TestSize.Level0) 161 { 162 EXPECT_EQ(StreamReadEnrolledList(nullptr, nullptr, nullptr), RESULT_BAD_PARAM); 163 } 164 165 HWTEST_F(IdmFileMgrTest, TestUpdateGlobalConfigFile, TestSize.Level0) 166 { 167 uint32_t configInfoNum = 1; 168 EXPECT_EQ(UpdateGlobalConfigFile(nullptr, configInfoNum), RESULT_BAD_PARAM); 169 170 GlobalConfigInfo globalConfigInfo = {}; 171 EXPECT_EQ(UpdateGlobalConfigFile(&globalConfigInfo, configInfoNum), RESULT_SUCCESS); 172 globalConfigInfo.type = ENABLE_STATUS; 173 globalConfigInfo.value.enableStatus = true; 174 globalConfigInfo.userIds[0] = 1; 175 globalConfigInfo.userIdNum = 1; 176 globalConfigInfo.authType = 1; 177 EXPECT_EQ(UpdateGlobalConfigFile(&globalConfigInfo, configInfoNum), RESULT_SUCCESS); 178 globalConfigInfo.value.enableStatus = false; 179 EXPECT_EQ(UpdateGlobalConfigFile(&globalConfigInfo, configInfoNum), RESULT_SUCCESS); 180 181 GlobalConfigInfo globalConfigInfo1 = {}; 182 globalConfigInfo1.type = PIN_EXPIRED_PERIOD; 183 globalConfigInfo1.value.pinExpiredPeriod = 1; 184 EXPECT_EQ(UpdateGlobalConfigFile(&globalConfigInfo, configInfoNum), RESULT_SUCCESS); 185 } 186 187 HWTEST_F(IdmFileMgrTest, TestLoadGlobalConfigInfo, TestSize.Level0) 188 { 189 uint32_t len = 1; 190 EXPECT_EQ(LoadGlobalConfigInfo(nullptr, len, nullptr), RESULT_BAD_PARAM); 191 192 uint32_t configInfoNum = 1; 193 GlobalConfigInfo *param = {}; 194 EXPECT_EQ(LoadGlobalConfigInfo(param, len, &configInfoNum), RESULT_BAD_PARAM); 195 } 196 } // namespace UserAuth 197 } // namespace UserIam 198 } // namespace OHOS 199