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 "cms_utils.h"
19 #include "verify_code_signature.h"
20
21 namespace OHOS {
22 namespace SignatureTools {
23
24 class CmsUtilsTest : public testing::Test {
25 public:
26 static void SetUpTestCase(void);
27 static void TearDownTestCase();
28 void SetUp();
29 void TearDown();
30 };
31
SetUpTestCase(void)32 void CmsUtilsTest::SetUpTestCase(void)
33 {
34 (void)rename("./codeSigning/entry-default-unsigned-so.txt", "./codeSigning/entry-default-unsigned-so.hap");
35 }
36
TearDownTestCase(void)37 void CmsUtilsTest::TearDownTestCase(void)
38 {
39 }
40
SetUp()41 void CmsUtilsTest::SetUp()
42 {
43 }
44
TearDown()45 void CmsUtilsTest::TearDown()
46 {
47 }
48
49 /**
50 * @tc.name: CheckOwnerID001
51 * @tc.desc: check owner id with p7 is null
52 * @tc.size: MEDIUM
53 * @tc.type: FUNC
54 * @tc.level Level 1
55 * @tc.require: SR000H63TL
56 */
57 HWTEST_F(CmsUtilsTest, CheckOwnerID001, testing::ext::TestSize.Level1)
58 {
59 // 走进分支 if (p7 == nullptr)
60 const std::string signature;
61 const std::string profileOwnerID;
62 const std::string profileType = "debug";
63 bool flag = CmsUtils::CheckOwnerID(signature, profileOwnerID, profileType);
64
65 EXPECT_EQ(flag, false);
66 }
67
68 /**
69 * @tc.name: CheckOwnerID002
70 * @tc.desc: check owner id with profile type is debug
71 * @tc.size: MEDIUM
72 * @tc.type: FUNC
73 * @tc.level Level 1
74 * @tc.require: SR000H63TL
75 */
76 HWTEST_F(CmsUtilsTest, CheckOwnerID002, testing::ext::TestSize.Level1)
77 {
78 // 走进 for 下的 if ("debug" == profileType) 分支
79 std::string signature;
80 FileUtils::ReadFile("./codeSigning/signed-profile.p7b", signature);
81
82 const std::string profileOwnerID = "xxx";
83 const std::string profileType = "debug";
84 bool flag = CmsUtils::CheckOwnerID(signature, profileOwnerID, profileType);
85
86 EXPECT_EQ(flag, true);
87 }
88
89 /**
90 * @tc.name: CheckOwnerID003
91 * @tc.desc: check owner id with owner id is empty
92 * @tc.size: MEDIUM
93 * @tc.type: FUNC
94 * @tc.level Level 1
95 * @tc.require: SR000H63TL
96 */
97 HWTEST_F(CmsUtilsTest, CheckOwnerID003, testing::ext::TestSize.Level1)
98 {
99 // 走进 for 下的 if (ownerID.empty()) 分支
100 std::string signature;
101 FileUtils::ReadFile("./codeSigning/signed-profile.p7b", signature);
102
103 const std::string profileOwnerID;
104 const std::string profileType = "release";
105 bool flag = CmsUtils::CheckOwnerID(signature, profileOwnerID, profileType);
106
107 EXPECT_EQ(flag, true);
108 }
109
110 /**
111 * @tc.name: CheckOwnerID004
112 * @tc.desc: check owner id with profile type is release
113 * @tc.size: MEDIUM
114 * @tc.type: FUNC
115 * @tc.level Level 1
116 * @tc.require: SR000H63TL
117 */
118 HWTEST_F(CmsUtilsTest, CheckOwnerID004, testing::ext::TestSize.Level1)
119 {
120 // 走进 for 下的 else 分支
121 std::string signature;
122 FileUtils::ReadFile("./codeSigning/signed-profile.p7b", signature);
123
124 const std::string profileOwnerID = "007";
125 const std::string profileType = "release";
126 bool flag = CmsUtils::CheckOwnerID(signature, profileOwnerID, profileType);
127
128 EXPECT_EQ(flag, false);
129 }
130
131 /**
132 * @tc.name: VerifyHap
133 * @tc.desc: verify hap without profile content
134 * @tc.size: MEDIUM
135 * @tc.type: FUNC
136 * @tc.level Level 1
137 * @tc.require: SR000H63TL
138 */
139 HWTEST_F(CmsUtilsTest, VerifyHap, testing::ext::TestSize.Level1)
140 {
141 std::string file = "./codeSigning/entry-default-signed-so.hap";
142 int64_t offset = 1397151;
143 int64_t length = 23221;
144 std::string fileFormat = "hap";
145 std::string profileContent = "";
146 bool flag = VerifyCodeSignature::VerifyHap(file, offset, length, fileFormat, profileContent);
147
148 EXPECT_EQ(flag, false);
149 }
150
151 /**
152 * @tc.name: CreateNIDFromOID
153 * @tc.desc: Test function of CmsUtilsTest::CreateNIDFromOID() interface for SUCCESS.
154 * @tc.size: MEDIUM
155 * @tc.type: FUNC
156 * @tc.level Level 1
157 * @tc.require: SR000H63TL
158 */
159 HWTEST_F(CmsUtilsTest, CreateNIDFromOID, testing::ext::TestSize.Level1)
160 {
161 std::string oid;
162 std::string shortName;
163 std::string longName;
164 int nID = CmsUtils::CreateNIDFromOID(oid, shortName, longName);
165
166 EXPECT_EQ(nID, 0);
167 }
168
169 /**
170 * @tc.name: VerifySignDataWithUnsignedDataDigest
171 * @tc.desc: Test function of CmsUtilsTest::VerifySignDataWithUnsignedDataDigest() interface for FAILED.
172 * @tc.size: MEDIUM
173 * @tc.type: FUNC
174 * @tc.level Level 1
175 * @tc.require: SR000H63TL
176 */
177 HWTEST_F(CmsUtilsTest, VerifySignDataWithUnsignedDataDigest, testing::ext::TestSize.Level1)
178 {
179 std::vector<int8_t> unsignedDataDigest;
180 std::vector<int8_t> signedData;
181 bool flag = CmsUtils::VerifySignDataWithUnsignedDataDigest(unsignedDataDigest, signedData);
182
183 EXPECT_EQ(flag, false);
184 }
185 } // namespace SignatureTools
186 } // namespace OHOS