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 
20 #include "cert_utils.h"
21 #include "directory_ex.h"
22 #include "fsverity_utils_helper.h"
23 #include "local_sign_key.h"
24 #include "log.h"
25 #include "pkcs7_generator.h"
26 
27 using namespace OHOS::Security::CodeSign;
28 using namespace testing::ext;
29 using namespace std;
30 
31 namespace OHOS {
32 namespace Security {
33 namespace CodeSign {
34 static const std::string AN_BASE_PATH = "/data/local/ark-cache/tmp/";
35 static const std::string DEMO_AN_PATH2 = AN_BASE_PATH + "demo2.an";
36 static const std::string DEFAULT_HASH_ALGORITHM = "sha256";
37 
38 class LocalCodeSignUtilsTest : public testing::Test {
39 public:
LocalCodeSignUtilsTest()40     LocalCodeSignUtilsTest() {};
~LocalCodeSignUtilsTest()41     virtual ~LocalCodeSignUtilsTest() {};
SetUpTestCase()42     static void SetUpTestCase() {};
TearDownTestCase()43     static void TearDownTestCase() {};
SetUp()44     void SetUp() {};
TearDown()45     void TearDown() {};
46 };
47 
48 /**
49  * @tc.name: LocalCodeSignUtilsTest_0001
50  * @tc.desc: Sign local code successfully, owner ID is empty
51  * @tc.type: Func
52  * @tc.require: issueI8FCGF
53  */
54 HWTEST_F(LocalCodeSignUtilsTest, LocalCodeSignUtilsTest_0001, TestSize.Level0)
55 {
56     ByteBuffer digest;
57     std::string realPath;
58     std::string ownerID = "";
59     bool bRet = OHOS::PathToRealPath(DEMO_AN_PATH2, realPath);
60     EXPECT_EQ(bRet, true);
61     bRet = FsverityUtilsHelper::GetInstance().GenerateFormattedDigest(realPath.c_str(), digest);
62     EXPECT_EQ(bRet, true);
63 
64     ByteBuffer signature;
65     int ret = PKCS7Generator::GenerateSignature(ownerID, LocalSignKey::GetInstance(), DEFAULT_HASH_ALGORITHM.c_str(),
66         digest, signature);
67     EXPECT_EQ(ret, CS_SUCCESS);
68 }
69 
70 /**
71  * @tc.name: LocalCodeSignUtilsTest_0002
72  * @tc.desc: Sign local code with owner ID successfully
73  * @tc.type: Func
74  * @tc.require: issueI88PPA
75  */
76 HWTEST_F(LocalCodeSignUtilsTest, LocalCodeSignUtilsTest_0002, TestSize.Level0)
77 {
78     ByteBuffer digest;
79     std::string realPath;
80     std::string ownerID = "AppName123";
81     bool bRet = OHOS::PathToRealPath(DEMO_AN_PATH2, realPath);
82     EXPECT_EQ(bRet, true);
83     bRet = FsverityUtilsHelper::GetInstance().GenerateFormattedDigest(realPath.c_str(), digest);
84     EXPECT_EQ(bRet, true);
85 
86     ByteBuffer signature;
87     int ret = PKCS7Generator::GenerateSignature(ownerID, LocalSignKey::GetInstance(), DEFAULT_HASH_ALGORITHM.c_str(),
88         digest, signature);
89     EXPECT_EQ(ret, CS_SUCCESS);
90 }
91 
92 /**
93  * @tc.name: LocalCodeSignUtilsTest_0003
94  * @tc.desc: Generate formatted digest failed with wrong path
95  * @tc.type: Func
96  * @tc.require: issueI8FCGF
97  */
98 HWTEST_F(LocalCodeSignUtilsTest, LocalCodeSignUtilsTest_0003, TestSize.Level0)
99 {
100     ByteBuffer digest;
101     std::string realPath = DEMO_AN_PATH2 + "invalid";
102     bool bRet = FsverityUtilsHelper::GetInstance().GenerateFormattedDigest(realPath.c_str(), digest);
103     EXPECT_EQ(bRet, false);
104 }
105 } // namespace CodeSign
106 } // namespace Security
107 } // namespace OHOS
108