1 /* 2 * Copyright (c) 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 <cstdlib> 17 #include <gtest/gtest.h> 18 #include <string> 19 #include <parameters.h> 20 21 #include "cert_utils.h" 22 #include "directory_ex.h" 23 #include "fsverity_utils_helper.h" 24 #include "local_sign_key.h" 25 #include "log.h" 26 #include "pkcs7_generator.h" 27 #include "hks_api.h" 28 #include "byte_buffer.h" 29 #include "cert_path.h" 30 31 32 using namespace OHOS::Security::CodeSign; 33 using namespace testing::ext; 34 using namespace std; 35 36 namespace OHOS { 37 namespace Security { 38 namespace CodeSign { 39 static const std::string AN_BASE_PATH = "/data/local/ark-cache/tmp/"; 40 static const std::string DEMO_AN_PATH2 = AN_BASE_PATH + "demo2.an"; 41 static const std::string DEFAULT_HASH_ALGORITHM = "sha256"; 42 43 class LocalCodeSignUtilsMockTest : public testing::Test { 44 public: LocalCodeSignUtilsMockTest()45 LocalCodeSignUtilsMockTest() {}; ~LocalCodeSignUtilsMockTest()46 virtual ~LocalCodeSignUtilsMockTest() {}; SetUpTestCase()47 static void SetUpTestCase() {}; TearDownTestCase()48 static void TearDownTestCase() {}; SetUp()49 void SetUp() {}; TearDown()50 void TearDown() {}; 51 }; 52 53 /** 54 * @tc.name: LocalCodeSignUtilsMockTest_0001 55 * @tc.desc: Sign local code successfully, owner ID is empty, and set g_count. 56 * @tc.type: Func 57 * @tc.require: issueI8FCGF 58 */ 59 HWTEST_F(LocalCodeSignUtilsMockTest, LocalCodeSignUtilsMockTest_0001, TestSize.Level0) 60 { 61 ByteBuffer digest; 62 std::string realPath; 63 std::string ownerID = ""; 64 bool bRet = OHOS::PathToRealPath(DEMO_AN_PATH2, realPath); 65 EXPECT_EQ(bRet, true); 66 bRet = FsverityUtilsHelper::GetInstance().GenerateFormattedDigest(realPath.c_str(), digest); 67 EXPECT_EQ(bRet, true); 68 69 ByteBuffer signature; 70 g_count = ATTESTKEY; 71 int ret = PKCS7Generator::GenerateSignature(ownerID, LocalSignKey::GetInstance(), DEFAULT_HASH_ALGORITHM.c_str(), 72 digest, signature); 73 EXPECT_EQ(ret, CS_ERR_HUKS_OBTAIN_CERT); 74 75 g_count = INIT; 76 ret = PKCS7Generator::GenerateSignature(ownerID, LocalSignKey::GetInstance(), DEFAULT_HASH_ALGORITHM.c_str(), 77 digest, signature); 78 EXPECT_EQ(ret, CS_ERR_HUKS_SIGN); 79 80 g_count = UPDATE; 81 ret = PKCS7Generator::GenerateSignature(ownerID, LocalSignKey::GetInstance(), DEFAULT_HASH_ALGORITHM.c_str(), 82 digest, signature); 83 EXPECT_EQ(ret, CS_ERR_HUKS_SIGN); 84 85 g_count = FINISH; 86 ret = PKCS7Generator::GenerateSignature(ownerID, LocalSignKey::GetInstance(), DEFAULT_HASH_ALGORITHM.c_str(), 87 digest, signature); 88 EXPECT_EQ(ret, CS_ERR_HUKS_SIGN); 89 } 90 91 /** 92 * @tc.name: LocalCodeSignUtilsMockTest_0002 93 * @tc.desc: Generate formatted digest failed with wrong path 94 * @tc.type: Func 95 * @tc.require: issueI8FCGF 96 */ 97 HWTEST_F(LocalCodeSignUtilsMockTest, LocalCodeSignUtilsMockTest_0002, TestSize.Level0) 98 { 99 std::unique_ptr<ByteBuffer> challenge = GetRandomChallenge(); 100 LocalSignKey &key = LocalSignKey::GetInstance(); 101 key.SetChallenge(*challenge); 102 bool bRet = key.InitKey(); 103 EXPECT_EQ(bRet, true); 104 105 g_count = ERROR; 106 bRet = key.InitKey(); 107 EXPECT_EQ(bRet, false); 108 109 g_count = KEYEXIST; 110 bRet = key.InitKey(); 111 EXPECT_EQ(bRet, false); 112 113 int32_t iRet = key.GetFormattedCertChain(*challenge); 114 EXPECT_EQ(iRet, 0); 115 } 116 117 /** 118 * @tc.name: LocalCodeSignUtilsMockTest_0003 119 * @tc.desc: LocalSignKey GetSignCert test 120 * @tc.type: Func 121 * @tc.require: issueI8FCGF 122 */ 123 HWTEST_F(LocalCodeSignUtilsMockTest, LocalCodeSignUtilsMockTest_0003, TestSize.Level0) 124 { 125 LocalSignKey &key = LocalSignKey::GetInstance(); 126 EXPECT_NE(key.GetSignCert(), nullptr); 127 } 128 129 /** 130 * @tc.name: LocalCodeSignUtilsMockTest_0004 131 * @tc.desc: cert_utils FreeCertChain certChain is nullptr or certChain->certs is nullptr 132 * @tc.type: Func 133 * @tc.require: issueI8FCGF 134 */ 135 HWTEST_F(LocalCodeSignUtilsMockTest, LocalCodeSignUtilsMockTest_0004, TestSize.Level0) 136 { 137 struct HksCertChain *certChain = nullptr; 138 uint32_t pos = 0; 139 (void)OHOS::Security::CodeSign::FreeCertChain(&certChain, pos); 140 141 certChain = static_cast<struct HksCertChain *>(malloc(sizeof(struct HksCertChain))); 142 EXPECT_NE(certChain, nullptr); 143 certChain->certs = nullptr; 144 (void)OHOS::Security::CodeSign::FreeCertChain(&certChain, pos); 145 } 146 147 /** 148 * @tc.name: LocalCodeSignUtilsMockTest_0005 149 * @tc.desc: cert_utils CheckChallengeSize func test 150 * @tc.type: Func 151 * @tc.require: issueI8FCGF 152 */ 153 HWTEST_F(LocalCodeSignUtilsMockTest, LocalCodeSignUtilsMockTest_0005, TestSize.Level0) 154 { 155 uint32_t size = 0; 156 bool bRet = OHOS::Security::CodeSign::CheckChallengeSize(size); 157 EXPECT_EQ(bRet, true); 158 159 size = 33; 160 bRet = OHOS::Security::CodeSign::CheckChallengeSize(size); 161 EXPECT_EQ(bRet, false); 162 } 163 164 /** 165 * @tc.name: LocalCodeSignUtilsMockTest_0006 166 * @tc.desc: cert_utils FormattedCertChain func test 167 * @tc.type: Func 168 * @tc.require: issueI8FCGF 169 */ 170 HWTEST_F(LocalCodeSignUtilsMockTest, LocalCodeSignUtilsMockTest_0006, TestSize.Level0) 171 { 172 const HksCertChain *certChain = LocalSignKey::GetInstance().GetCertChain(); 173 std::unique_ptr<ByteBuffer> buffer = GetRandomChallenge(); 174 bool bRet = OHOS::Security::CodeSign::FormattedCertChain(certChain, *buffer); 175 EXPECT_EQ(bRet, true); 176 } 177 178 /** 179 * @tc.name: LocalCodeSignUtilsMockTest_0007 180 * @tc.desc: cert_utils GetCertChainFormBuffer func test 181 * @tc.type: Func 182 * @tc.require: issueI8FCGF 183 */ 184 HWTEST_F(LocalCodeSignUtilsMockTest, LocalCodeSignUtilsMockTest_0007, TestSize.Level0) 185 { 186 ByteBuffer certChainBuffer; 187 ByteBuffer signCert; 188 ByteBuffer issuer; 189 std::vector<ByteBuffer> chain; 190 bool bRet = OHOS::Security::CodeSign::GetCertChainFormBuffer(certChainBuffer, signCert, issuer, chain); 191 EXPECT_EQ(bRet, false); 192 } 193 194 /** 195 * @tc.name: LocalCodeSignUtilsMockTest_0009 196 * @tc.desc: cert_path IsDeveloperModeOn and GetCertChainFormBuffer func test 197 * @tc.type: Func 198 * @tc.require: issueI8FCGF 199 */ 200 HWTEST_F(LocalCodeSignUtilsMockTest, LocalCodeSignUtilsMockTest_0009, TestSize.Level0) 201 { 202 (void)FsverityUtilsHelper::GetInstance().ErrorMsgLogCallback(nullptr); 203 204 if (OHOS::system::GetBoolParameter("const.security.developermode.state", false)) { 205 EXPECT_EQ(IsDeveloperModeOn(), true); 206 } else { 207 EXPECT_EQ(IsDeveloperModeOn(), false); 208 } 209 } 210 211 /** 212 * @tc.name: LocalCodeSignUtilsMockTest_0010 213 * @tc.desc: cert_path CodeSignGetUdid func test 214 * @tc.type: Func 215 * @tc.require: issueI8FCGF 216 */ 217 HWTEST_F(LocalCodeSignUtilsMockTest, LocalCodeSignUtilsMockTest_0010, TestSize.Level0) 218 { 219 EXPECT_EQ(CodeSignGetUdid(nullptr), -1); 220 } 221 } // namespace CodeSign 222 } // namespace Security 223 } // namespace OHOS 224