1 /* 2 * Copyright (c) 2024-2024 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 <memory> 17 #include <gtest/gtest.h> 18 #include "hap_info_segment.h" 19 20 namespace OHOS { 21 namespace SignatureTools { 22 23 /* 24 * 测试套件,固定写法 25 */ 26 class HapInfoSegmentTest : public testing::Test { 27 public: SetUpTestCase(void)28 static void SetUpTestCase(void) {}; TearDownTestCase()29 static void TearDownTestCase() {}; SetUp()30 void SetUp() {}; TearDown()31 void TearDown() {}; 32 }; 33 34 /** 35 * @tc.name: fromByteArray001 36 * @tc.desc: Go to the first branch, fromByteArray001 inequality. 37 * @tc.size: MEDIUM 38 * @tc.type: FUNC 39 * @tc.level Level 1 40 * @tc.require: SR000H63TL 41 */ 42 HWTEST_F(HapInfoSegmentTest, fromByteArray001, testing::ext::TestSize.Level1) 43 { 44 // 走第一个分支:inMagic 不一样 45 std::shared_ptr<HapInfoSegment> api = std::make_shared<HapInfoSegment>(); 46 47 ByteBuffer byteBuffer(32); 48 byteBuffer.PutInt32(0); // inMagic 49 byteBuffer.Flip(); 50 51 char readComment[32] = { 0 }; 52 byteBuffer.GetData(readComment, 32); 53 std::vector<int8_t> bytes(readComment, readComment + 32); 54 55 api->FromByteArray(bytes); 56 57 EXPECT_NE(api->GetSize(), 0); 58 } 59 60 /** 61 * @tc.name: fromByteArray002 62 * @tc.desc: Go to the third branch, the dataSize is not an integer multiple of 4096. 63 * @tc.size: MEDIUM 64 * @tc.type: FUNC 65 * @tc.level Level 1 66 * @tc.require: SR000H63TL 67 */ 68 HWTEST_F(HapInfoSegmentTest, fromByteArray002, testing::ext::TestSize.Level1) 69 { 70 // 走第三个分支:inHapSignInfo.getDataSize() 71 std::shared_ptr<HapInfoSegment> api = std::make_shared<HapInfoSegment>(); 72 73 ByteBuffer byteBuffer(64); 74 byteBuffer.PutInt32(-1045050266); // inMagic 75 byteBuffer.PutInt32(0); // inSaltSize 76 byteBuffer.PutInt32(0); // inSigSize 77 byteBuffer.PutInt32(1); // inFlags 78 byteBuffer.PutInt64(5); // inDataSize 79 80 std::vector<int8_t> inSalt(32, 0); 81 byteBuffer.PutData((const char*)inSalt.data(), 32); 82 83 byteBuffer.PutInt32(1); // inExtensionNum 84 byteBuffer.PutInt32(4); // inExtensionOffset 85 86 byteBuffer.Flip(); 87 88 char readComment[64] = { 0 }; 89 byteBuffer.GetData(readComment, 64); 90 std::vector<int8_t> bytes(readComment, readComment + 64); 91 92 api->FromByteArray(bytes); 93 94 EXPECT_NE(api->GetSize(), 0); 95 } 96 97 /** 98 * @tc.name: fromByteArray003 99 * @tc.desc: Go to the forth branch, the data has wrong extensionNum in the HapInfoSegment. 100 * @tc.size: MEDIUM 101 * @tc.type: FUNC 102 * @tc.level Level 1 103 * @tc.require: SR000H63TL 104 */ 105 HWTEST_F(HapInfoSegmentTest, fromByteArray003, testing::ext::TestSize.Level1) 106 { 107 // 走第四个分支:inHapSignInfo.getExtensionNum() 108 std::shared_ptr<HapInfoSegment> api = std::make_shared<HapInfoSegment>(); 109 110 ByteBuffer byteBuffer(64); 111 byteBuffer.PutInt32(-1045050266); // inMagic 112 byteBuffer.PutInt32(0); // inSaltSize 113 byteBuffer.PutInt32(0); // inSigSize 114 byteBuffer.PutInt32(1); // inFlags 115 byteBuffer.PutInt64(4096); // inDataSize 116 117 std::vector<int8_t> inSalt(32, 0); 118 byteBuffer.PutData((const char*)inSalt.data(), 32); 119 120 byteBuffer.PutInt32(2); // inExtensionNum 121 byteBuffer.PutInt32(4); // inExtensionOffset 122 123 byteBuffer.Flip(); 124 125 char readComment[64] = { 0 }; 126 byteBuffer.GetData(readComment, 64); 127 std::vector<int8_t> bytes(readComment, readComment + 64); 128 129 api->FromByteArray(bytes); 130 131 EXPECT_NE(api->GetSize(), 0); 132 } 133 134 /** 135 * @tc.name: fromByteArray004 136 * @tc.desc: Test function of SignToolServiceImpl::fromByteArray004() interface for SUCCESS. 137 * @tc.size: MEDIUM 138 * @tc.type: FUNC 139 * @tc.level Level 1 140 * @tc.require: SR000H63TL 141 */ 142 HWTEST_F(HapInfoSegmentTest, fromByteArray004, testing::ext::TestSize.Level1) 143 { 144 // 走完 145 std::shared_ptr<HapInfoSegment> api = std::make_shared<HapInfoSegment>(); 146 147 ByteBuffer byteBuffer(64); 148 byteBuffer.PutInt32(-1045050266); // inMagic 149 byteBuffer.PutInt32(0); // inSaltSize 150 byteBuffer.PutInt32(0); // inSigSize 151 byteBuffer.PutInt32(1); // inFlags 152 byteBuffer.PutInt64(4096); // inDataSize 153 154 std::vector<int8_t> inSalt(32, 0); 155 byteBuffer.PutData((const char*)inSalt.data(), 32); 156 157 byteBuffer.PutInt32(1); // inExtensionNum 158 byteBuffer.PutInt32(4); // inExtensionOffset 159 160 byteBuffer.Flip(); 161 162 char readComment[64] = { 0 }; 163 byteBuffer.GetData(readComment, 64); 164 std::vector<int8_t> bytes(readComment, readComment + 64); 165 166 api->FromByteArray(bytes); 167 168 EXPECT_NE(api->GetSize(), 0); 169 } 170 171 172 /** 173 * @tc.name: getSignInfo 174 * @tc.desc: Test function of SignToolServiceImpl::getSignInfo() interface for SUCCESS. 175 * @tc.size: MEDIUM 176 * @tc.type: FUNC 177 * @tc.level Level 1 178 * @tc.require: SR000H63TL 179 */ 180 HWTEST_F(HapInfoSegmentTest, getSignInfo, testing::ext::TestSize.Level1) 181 { 182 std::shared_ptr<HapInfoSegment> api = std::make_shared<HapInfoSegment>(); 183 184 api->GetSignInfo(); 185 186 EXPECT_NE(api->GetSize(), 0); 187 } 188 189 /** 190 * @tc.name: getSize 191 * @tc.desc: Test function of SignToolServiceImpl::getSize() interface for SUCCESS. 192 * @tc.size: MEDIUM 193 * @tc.type: FUNC 194 * @tc.level Level 1 195 * @tc.require: SR000H63TL 196 */ 197 HWTEST_F(HapInfoSegmentTest, getSize, testing::ext::TestSize.Level1) 198 { 199 std::shared_ptr<HapInfoSegment> api = std::make_shared<HapInfoSegment>(); 200 201 int32_t hapInfoSegmentSize = api->GetSize(); 202 203 EXPECT_EQ(hapInfoSegmentSize, 64); 204 } 205 206 /** 207 * @tc.name: setSignInfo 208 * @tc.desc: Test function of SignToolServiceImpl::setSignInfo() interface for SUCCESS. 209 * @tc.size: MEDIUM 210 * @tc.type: FUNC 211 * @tc.level Level 1 212 * @tc.require: SR000H63TL 213 */ 214 HWTEST_F(HapInfoSegmentTest, setSignInfo, testing::ext::TestSize.Level1) 215 { 216 std::shared_ptr<HapInfoSegment> api = std::make_shared<HapInfoSegment>(); 217 218 int32_t saltSize = 0; 219 int32_t flags = 1; 220 int64_t dataSize = 5390336; 221 std::vector<int8_t> salt; 222 std::vector<int8_t> sig{ 48, -126, 7, -46, 6, 9, 42, -122, 72, -122, -9, 13, 223 1, 7, 2, -96, -126, 7, -61, 48, -126, 7, -65, 2, 1, 1, 49, 13, 48, 11, 6, 224 9, 96, -122, 72, 1, 101, 3, 4, 2, 1, 48, 11, 6, 9, 42, -122, 72, -122, -9, 225 13, 1, 7, 1, -96, -126, 6, 43, 48, -126, 1, -32, 48, -126, 1, -121, -96, 3, 226 2, 1, 2, 2, 4, 85, -67, -54, 116, 48, 10, 6, 8, 42, -122, 72, -50, 61, 4, 3, 227 3, 48, 85, 49, 11, 48, 9, 6, 3, 85, 4, 6, 1 }; 228 SignInfo signInfo(saltSize, flags, dataSize, salt, sig); 229 api->SetSignInfo(signInfo); 230 231 EXPECT_NE(api->GetSize(), 0); 232 } 233 234 /** 235 * @tc.name: toByteArray 236 * @tc.desc: Test function of SignToolServiceImpl::toByteArray() interface for SUCCESS. 237 * @tc.size: MEDIUM 238 * @tc.type: FUNC 239 * @tc.level Level 1 240 * @tc.require: SR000H63TL 241 */ 242 HWTEST_F(HapInfoSegmentTest, toByteArray, testing::ext::TestSize.Level1) 243 { 244 std::shared_ptr<HapInfoSegment> api = std::make_shared<HapInfoSegment>(); 245 246 std::vector<int8_t> byteArray; 247 api->ToByteArray(byteArray); 248 249 EXPECT_EQ(byteArray.size(), 64); 250 } 251 } // namespace SignatureTools 252 } // namespace OHOS