• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2020-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 #include "hap_verify_test.h"
16 #include <fstream>
17 #include <gtest/gtest.h>
18 #include <string>
19 #include "app_verify_pub.h"
20 #include "write_file.h"
21 
22 using namespace std;
23 using namespace testing::ext;
24 namespace {
25 const int32_t DELAY_TIME = 4;
26 
27 class HapVerifyTest : public testing::Test {
28 public:
29     static void SetUpTestCase(void);
30 
31     static void TearDownTestCase(void);
32 
33     void SetUp();
34 
35     void TearDown();
36 };
37 
SetUpTestCase(void)38 void HapVerifyTest::SetUpTestCase(void)
39 {
40 }
41 
TearDownTestCase(void)42 void HapVerifyTest::TearDownTestCase(void)
43 {
44 }
45 
SetUp()46 void HapVerifyTest::SetUp()
47 {
48 }
49 
TearDown()50 void HapVerifyTest::TearDown()
51 {
52 }
53 
54 /**
55  * @tc.name: test001
56  * @tc.desc: 测试签名正确的hap包可以安装并验签通过
57  * @tc.function: SUB_SEC_AppSec_AppSign_VerifySignature
58  * @tc.spec: SUB_SEC_AppSec_AppSign_VerifySignature
59  * @tc.feature: SUB_SEC_AppSec_AppSign_VerifySignature
60  * @tc.number: SUB_SEC_AppSec_AppSign_VerifySignature_1500
61  * @tc.size: MEDIUM
62  * @tc.level: level 3
63  * @tc.type: FUNC
64  */
65 HWTEST_F(HapVerifyTest, test001, TestSize.Level3)
66 {
67     const char *signedHap = "./business_packet.hap";
68     DeleteFile(signedHap);
69     CopyFile(GetBusinessHap(), signedHap);
70     sleep(DELAY_TIME);
71     VerifyResult verifyResult = {0};
72     int32_t resultCode = APPVERI_AppVerify(signedHap, &verifyResult);
73     ASSERT_TRUE(resultCode == V_OK);
74     APPVERI_FreeVerifyRst(&verifyResult);
75     DeleteFile(signedHap);
76 }
77 
78 /**
79  * @tc.name: test002
80  * @tc.desc: 安装未签名的应用,校验失败
81  * @tc.function: SUB_SEC_AppSec_AppSign_VerifySignature
82  * @tc.spec: SUB_SEC_AppSec_AppSign_VerifySignature
83  * @tc.feature: SUB_SEC_AppSec_AppSign_VerifySignature
84  * @tc.number: SUB_SEC_AppSec_AppSign_VerifySignature_1500
85  * @tc.size: MEDIUM
86  * @tc.level: level 3
87  * @tc.type: FUNC
88  */
89 HWTEST_F(HapVerifyTest, test002, TestSize.Level3)
90 {
91     const char *unsignedHap = "./unsigned_packet.hap";
92     DeleteFile(unsignedHap);
93     CopyFile(GetUnsignedHap(), unsignedHap);
94     sleep(DELAY_TIME);
95     VerifyResult verifyResult = {0};
96     int32_t resultCode = APPVERI_AppVerify(unsignedHap, &verifyResult);
97     ASSERT_TRUE(resultCode != V_OK);
98     APPVERI_FreeVerifyRst(&verifyResult);
99     DeleteFile(unsignedHap);
100 }
101 
102 /**
103  * @tc.name: test003
104  * @tc.desc: udid号与设备不对应的应用安装验签失败
105  * @tc.function: SUB_SEC_AppSec_AppSign_VerifySignature
106  * @tc.spec: SUB_SEC_AppSec_AppSign_VerifySignature
107  * @tc.feature: SUB_SEC_AppSec_AppSign_VerifySignature
108  * @tc.number: SUB_SEC_AppSec_AppSign_VerifySignature_1500
109  * @tc.size: MEDIUM
110  * @tc.level: level 3
111  * @tc.type: FUNC
112  */
113 HWTEST_F(HapVerifyTest, test003, TestSize.Level3)
114 {
115     APPVERI_SetDebugMode(true);
116     APPVERI_SetActsMode(true);
117     const char *udidUnmatchHap = "./udid_wrong_test.hap";
118     DeleteFile(udidUnmatchHap);
119     CopyFile(GetWrongUdidHap(), udidUnmatchHap);
120     sleep(DELAY_TIME);
121     VerifyResult verifyResult = {0};
122     int32_t resultCode = APPVERI_AppVerify(udidUnmatchHap, &verifyResult);
123     ASSERT_TRUE(resultCode != V_OK);
124     APPVERI_FreeVerifyRst(&verifyResult);
125     APPVERI_SetDebugMode(false);
126     APPVERI_SetActsMode(false);
127     DeleteFile(udidUnmatchHap);
128 }
129 
130 /**
131  * @tc.name: test005
132  * @tc.desc: 被篡改的应用安装验签
133  * @tc.function: SUB_SEC_AppSec_AppSign_VerifySignature
134  * @tc.spec: SUB_SEC_AppSec_AppSign_VerifySignature
135  * @tc.feature: SUB_SEC_AppSec_AppSign_VerifySignature
136  * @tc.number: SUB_SEC_AppSec_AppSign_VerifySignature_1500
137  * @tc.size: MEDIUM
138  * @tc.level: level 3
139  * @tc.type: FUNC
140  */
141 HWTEST_F(HapVerifyTest, test005, TestSize.Level3)
142 {
143     const char *tamperedHap = "./modified_packet.hap";
144     DeleteFile(tamperedHap);
145     CopyFile(GetModifiedHap(), tamperedHap);
146     sleep(DELAY_TIME);
147     VerifyResult verifyResult = {0};
148     int32_t resultCode = APPVERI_AppVerify(tamperedHap, &verifyResult);
149     ASSERT_TRUE(resultCode != V_OK);
150     APPVERI_FreeVerifyRst(&verifyResult);
151     DeleteFile(tamperedHap);
152 }
153 
154 /**
155  * @tc.name: test006
156  * @tc.desc: debug模式打开,安装商用版本hap成功,安装测试版本hap成功
157  * @tc.function: SUB_SEC_AppSec_AppSign_VerifySignature
158  * @tc.spec: SUB_SEC_AppSec_AppSign_VerifySignature
159  * @tc.feature: SUB_SEC_AppSec_AppSign_VerifySignature
160  * @tc.number: SUB_SEC_AppSec_AppSign_VerifySignature_1500
161  * @tc.size: MEDIUM
162  * @tc.level: level 3
163  * @tc.type: FUNC
164  */
165 HWTEST_F(HapVerifyTest, test006, TestSize.Level3)
166 {
167     APPVERI_SetDebugMode(true);
168     APPVERI_SetActsMode(true);
169     const char *testHap = "./success_test.hap";
170     DeleteFile(testHap);
171     CopyFile(GetSuccessHap(), testHap);
172     sleep(DELAY_TIME);
173     VerifyResult verifyResult = {0};
174     int32_t resultCode = APPVERI_AppVerify(testHap, &verifyResult);
175     ASSERT_TRUE(resultCode != V_OK);
176     APPVERI_FreeVerifyRst(&verifyResult);
177     APPVERI_SetActsMode(false);
178     DeleteFile(testHap);
179 
180     const char *businessHap = "./business_packet.hap";
181     DeleteFile(businessHap);
182     CopyFile(GetBusinessHap(), businessHap);
183     sleep(DELAY_TIME);
184     VerifyResult verifyResult1 = {0};
185     resultCode = APPVERI_AppVerify(businessHap, &verifyResult1);
186     APPVERI_FreeVerifyRst(&verifyResult1);
187     ASSERT_TRUE(resultCode == V_OK);
188     APPVERI_SetDebugMode(false);
189     DeleteFile(businessHap);
190 }
191 
192 /**
193  * @tc.name: test007
194  * @tc.desc: debug模式关闭,安装商用版本hap成功,安装测试版本hap失败
195  * @tc.function: SUB_SEC_AppSec_AppSign_VerifySignature
196  * @tc.spec: SUB_SEC_AppSec_AppSign_VerifySignature
197  * @tc.feature: SUB_SEC_AppSec_AppSign_VerifySignature
198  * @tc.number: SUB_SEC_AppSec_AppSign_VerifySignature_1500
199  * @tc.size: MEDIUM
200  * @tc.level: level 3
201  * @tc.type: FUNC
202  */
203 HWTEST_F(HapVerifyTest, test007, TestSize.Level3)
204 {
205     APPVERI_SetDebugMode(false);
206     const char *businessHap = "./business_packet.hap";
207     DeleteFile(businessHap);
208     CopyFile(GetBusinessHap(), businessHap);
209     sleep(DELAY_TIME);
210     VerifyResult verifyResult = {0};
211     int32_t resultCode = APPVERI_AppVerify(businessHap, &verifyResult);
212     ASSERT_TRUE(resultCode == V_OK);
213     APPVERI_FreeVerifyRst(&verifyResult);
214     DeleteFile(businessHap);
215 
216     const char *testHap = "./success_test.hap";
217     DeleteFile(testHap);
218     CopyFile(GetSuccessHap(), testHap);
219     sleep(DELAY_TIME);
220     VerifyResult verifyResult1 = {0};
221     APPVERI_SetActsMode(true);
222     resultCode = APPVERI_AppVerify(testHap, &verifyResult1);
223     APPVERI_FreeVerifyRst(&verifyResult);
224     ASSERT_TRUE(resultCode != V_OK);
225     APPVERI_SetDebugMode(false);
226     APPVERI_SetActsMode(false);
227     DeleteFile(testHap);
228 }
229 
230 /**
231  * @tc.name: test008
232  * @tc.desc: 使用错误证书的应用安全验签失败
233  * @tc.function: SUB_SEC_AppSec_AppSign_VerifySignature
234  * @tc.spec: SUB_SEC_AppSec_AppSign_VerifySignature
235  * @tc.feature: SUB_SEC_AppSec_AppSign_VerifySignature
236  * @tc.number: SUB_SEC_AppSec_AppSign_VerifySignature_1500
237  * @tc.size: MEDIUM
238  * @tc.level: level 3
239  * @tc.type: FUNC
240  */
241 HWTEST_F(HapVerifyTest, test008, TestSize.Level3)
242 {
243     const char *wrongHap = "./wrong_license.hap";
244     DeleteFile(wrongHap);
245     CopyFile(GetWrongLiceseHap(), wrongHap);
246     sleep(DELAY_TIME);
247     VerifyResult verifyResult = {0};
248     int32_t resultCode = APPVERI_AppVerify(wrongHap, &verifyResult);
249     ASSERT_TRUE(resultCode != V_OK);
250     APPVERI_FreeVerifyRst(&verifyResult);
251     DeleteFile(wrongHap);
252 }
253 }
254