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 "idm_common.h" 20 #include "idm_file_manager.h" 21 22 extern "C" { 23 extern ResultCode CapacityExpansion(Buffer *object, uint32_t targetCapacity); 24 extern ResultCode StreamWrite(Buffer *parcel, void *from, uint32_t size); 25 extern ResultCode StreamWriteEnrolledInfo(Buffer *parcel, LinkedList *enrolledList); 26 extern ResultCode StreamWriteCredentialList(Buffer *parcel, LinkedList *credentialList); 27 extern ResultCode StreamWriteUserInfo(Buffer *parcel, UserInfo *userInfo); 28 extern ResultCode StreamRead(Buffer *parcel, uint32_t *index, void *to, uint32_t size); 29 extern ResultCode StreamReadCredentialList(Buffer *parcel, uint32_t *index, LinkedList *credentialList); 30 extern ResultCode StreamReadEnrolledList(Buffer *parcel, uint32_t *index, LinkedList *enrolledList); 31 } 32 33 namespace OHOS { 34 namespace UserIam { 35 namespace UserAuth { 36 using namespace testing; 37 using namespace testing::ext; 38 39 namespace { 40 const uint32_t MAX_BUFFER_LEN = 512000; 41 } // namespace 42 43 class IdmFileMgrTest : public testing::Test { 44 public: SetUpTestCase()45 static void SetUpTestCase() {}; 46 TearDownTestCase()47 static void TearDownTestCase() {}; 48 SetUp()49 void SetUp() {}; 50 TearDown()51 void TearDown() {}; 52 }; 53 54 HWTEST_F(IdmFileMgrTest, TestCapacityExpansion_001, TestSize.Level0) 55 { 56 EXPECT_EQ(CapacityExpansion(nullptr, 0), RESULT_BAD_PARAM); 57 58 uint32_t bufferSize = 10; 59 Buffer *object = CreateBufferBySize(bufferSize); 60 object->maxSize = MAX_BUFFER_LEN; 61 EXPECT_EQ(CapacityExpansion(object, 0), RESULT_BAD_PARAM); 62 } 63 64 HWTEST_F(IdmFileMgrTest, TestCapacityExpansion_002, TestSize.Level0) 65 { 66 uint32_t bufferSize = 10; 67 Buffer *object = CreateBufferBySize(bufferSize); 68 EXPECT_EQ(CapacityExpansion(object, 0), RESULT_SUCCESS); 69 } 70 71 HWTEST_F(IdmFileMgrTest, TestCapacityExpansion_003, TestSize.Level0) 72 { 73 uint32_t bufferSize = 10; 74 Buffer *object = CreateBufferBySize(bufferSize); 75 uint32_t targetCapacity = 5120000; 76 EXPECT_EQ(CapacityExpansion(object, targetCapacity), RESULT_BAD_PARAM); 77 } 78 79 HWTEST_F(IdmFileMgrTest, TestStreamWrite_001, TestSize.Level0) 80 { 81 EXPECT_EQ(StreamWrite(nullptr, nullptr, 0), RESULT_BAD_PARAM); 82 83 uint32_t bufferSize = 10; 84 Buffer *parcel = CreateBufferBySize(bufferSize); 85 uint32_t from = 0; 86 uint32_t objectSize = 5120000; 87 EXPECT_EQ(StreamWrite(parcel, static_cast<void *>(&from), objectSize), RESULT_BAD_PARAM); 88 89 objectSize = 15; 90 uint8_t array[objectSize]; 91 EXPECT_EQ(StreamWrite(parcel, static_cast<void *>(&array), objectSize), RESULT_SUCCESS); 92 } 93 94 HWTEST_F(IdmFileMgrTest, TestStreamWrite_002, TestSize.Level0) 95 { 96 uint32_t bufferSize = 10; 97 Buffer *parcel = CreateBufferBySize(bufferSize); 98 uint32_t objectSize = 5; 99 100 uint8_t array[objectSize]; 101 EXPECT_EQ(StreamWrite(parcel, static_cast<void *>(&array), objectSize), RESULT_SUCCESS); 102 } 103 104 HWTEST_F(IdmFileMgrTest, TestStreamWriteEnrolledInfo, TestSize.Level0) 105 { 106 EXPECT_EQ(StreamWriteEnrolledInfo(nullptr, nullptr), RESULT_BAD_PARAM); 107 108 uint32_t bufferSize = 10; 109 Buffer *parcel = CreateBufferBySize(bufferSize); 110 LinkedList *enrolledList = CreateLinkedList(DestroyEnrolledNode); 111 EXPECT_NE(enrolledList, nullptr); 112 EXPECT_EQ(StreamWriteEnrolledInfo(parcel, enrolledList), RESULT_SUCCESS); 113 114 enrolledList->insert(enrolledList, nullptr); 115 EXPECT_EQ(StreamWriteEnrolledInfo(parcel, enrolledList), RESULT_GENERAL_ERROR); 116 } 117 118 HWTEST_F(IdmFileMgrTest, TestStreamWriteCredentialList, TestSize.Level0) 119 { 120 EXPECT_EQ(StreamWriteCredentialList(nullptr, nullptr), RESULT_BAD_PARAM); 121 122 uint32_t bufferSize = 10; 123 Buffer *parcel = CreateBufferBySize(bufferSize); 124 LinkedList *credentialList = CreateLinkedList(DestroyCredentialNode); 125 EXPECT_NE(credentialList, nullptr); 126 EXPECT_EQ(StreamWriteCredentialList(parcel, credentialList), RESULT_SUCCESS); 127 128 credentialList->insert(credentialList, nullptr); 129 EXPECT_EQ(StreamWriteCredentialList(parcel, credentialList), RESULT_GENERAL_ERROR); 130 } 131 132 HWTEST_F(IdmFileMgrTest, TestStreamWriteUserInfo, TestSize.Level0) 133 { 134 EXPECT_EQ(StreamWriteUserInfo(nullptr, nullptr), RESULT_BAD_PARAM); 135 } 136 137 HWTEST_F(IdmFileMgrTest, TestUpdateFileInfo, TestSize.Level0) 138 { 139 EXPECT_EQ(UpdateFileInfo(nullptr), RESULT_BAD_PARAM); 140 } 141 142 HWTEST_F(IdmFileMgrTest, TestStreamRead, TestSize.Level0) 143 { 144 uint32_t bufferSize = 10; 145 Buffer *parcel = CreateBufferBySize(bufferSize); 146 uint32_t index = 20; 147 uint32_t size = 1000; 148 EXPECT_EQ(StreamRead(parcel, &index, nullptr, size), RESULT_BAD_PARAM); 149 150 parcel->contentSize = 200; 151 EXPECT_EQ(StreamRead(parcel, &index, nullptr, size), RESULT_BAD_PARAM); 152 } 153 154 HWTEST_F(IdmFileMgrTest, TestStreamReadCredentialList, TestSize.Level0) 155 { 156 EXPECT_EQ(StreamReadCredentialList(nullptr, nullptr, nullptr), RESULT_BAD_PARAM); 157 } 158 159 HWTEST_F(IdmFileMgrTest, TestStreamReadEnrolledList, TestSize.Level0) 160 { 161 EXPECT_EQ(StreamReadEnrolledList(nullptr, nullptr, nullptr), RESULT_BAD_PARAM); 162 } 163 } // namespace UserAuth 164 } // namespace UserIam 165 } // namespace OHOS 166