• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "provision_verify_test.h"
16 
17 #include <gtest/gtest.h>
18 #include "profile_verify.h"
19 
20 using namespace testing::ext;
21 namespace OHOS {
22 namespace SignatureTools {
23 const std::string INVALID_PROVISION_1 = R"(
24 {
25     "version-name": "1.0.0",
26     "issuer": "App Gallery"
27 })";
28 const std::string INVALID_PROVISION_2 = R"(
29 {
30     "version-code": 2,
31     "version-name": "1.1.0",
32     "uuid": "uuid_1_1",
33     "type": "release",
34     "app-distribution-type": "os_integration",
35     "validity": {
36         "not-before": 1262275201,
37         "not-after": 2524579301
38     },
39     "bundle-info": {
40         "developer-id": "developer_id_1_1",
41         "development-certificate": "ABCD",
42         "distribution-certificate": "1234",
43         "bundle-name": "com.test1_1",
44         "apl": "normal",
45         "app-feature": "hos_normal_app"
46     }
47 }})";   // brackets mismatching
48 const std::string INVALID_PROVISION_3 = R"(
49 {
50     "version-code": 2,
51     "version-name": "2.0.0",
52     "uuid": "uuid_2",
53     "type": "release",
54     "app-distribution-type": "os_integration",
55     "validity": {
56         "not-before": 1262275200,
57         "not-after": 2524579300
58     },
59     "bundle-info": {
60         "developer-id": "developer_id_2",
61         "development-certificate": "ABCDEFG",
62         "distribution-certificate": "123456789",
63         "bundle-name": "com.test2",
64         "app-feature": "hos_normal_app"
65     }
66 })";  // apl not exist
67 /* validity: 2010 - 2030 */
68 const std::string VALID_PROVISION = R"(
69 {
70     "version-code": 2,
71     "version-name": "2.0.0",
72     "uuid": "uuid_2",
73     "type": "release",
74     "app-distribution-type": "os_integration",
75     "validity": {
76         "not-before": 1262275200,
77         "not-after": 2524579300
78     },
79     "bundle-info": {
80         "developer-id": "developer_id_2",
81         "development-certificate": "ABCDEFG",
82         "distribution-certificate": "123456789",
83         "bundle-name": "com.test2",
84         "apl": "normal",
85         "app-feature": "hos_normal_app"
86     }
87 })";
88 const std::string VALID_PROVISION_PERMISSION = R"(
89 {
90     "version-code": 3,
91     "version-name": "3.0.0",
92     "uuid": "uuid_3",
93     "type": "release",
94     "app-distribution-type": "app_gallery",
95     "validity": {
96         "not-before": 1262275300,
97         "not-after": 2524579300
98     },
99     "bundle-info": {
100         "developer-id": "developer_id_3",
101         "development-certificate": "ABCDEFGH",
102         "distribution-certificate": "1234567890",
103         "bundle-name": "com.test3",
104         "apl": "normal",
105         "app-feature": "hos_normal_app"
106     },
107     "permissions" : {
108         "restricted-permissions" : ["ohos.permission.GET_PHONE_STATE",
109             "ohos.permission.GET_APP_ACCOUNTS"],
110         "restricted-capabilities" : ["ohos.permission.test"]
111     },
112     "issuer": "App Gallery"
113 })";
114 const std::string UNAUTHORIZED_DEVICE_EMPTY_DEVICE_LIST_PROVISION = R"(
115 {
116     "version-code": 6,
117     "version-name": "6.0.0",
118     "uuid": "uuid_6",
119     "type": "debug",
120     "validity": {
121         "not-before": 1262275600,
122         "not-after": 2524579300
123     },
124     "bundle-info": {
125         "developer-id": "developer_id_6",
126         "development-certificate": "ABCDEFGHIJ",
127         "distribution-certificate": "1234567890123",
128         "bundle-name": "com.test6",
129         "apl": "normal",
130         "app-feature": "hos_normal_app"
131     },
132     "debug-info": {
133         "device-id-type": "udid"
134     },
135     "issuer": "App Gallery"
136 })";
137 const std::string UNSUPPORTED_DEVICE_TYPE_PROVISION = R"(
138 {
139     "version-code": 7,
140     "version-name": "7.0.0",
141     "uuid": "uuid_7",
142     "type": "debug",
143     "validity": {
144         "not-before": 1262275600,
145         "not-after": 2524579300
146     },
147     "bundle-info": {
148         "developer-id": "developer_id_7",
149         "development-certificate": "ABCDEFGHIJM",
150         "distribution-certificate": "12345678901231",
151         "bundle-name": "com.test7",
152         "apl": "normal",
153         "app-feature": "hos_normal_app"
154     },
155     "debug-info": {
156         "device-id-type": "unknownType",
157         "device-ids" : ["1234ABCD"]
158     },
159     "issuer": "App Gallery"
160 })";
161 const std::string DEVICE_UNAUTH_PROVISION = R"(
162 {
163     "version-code": 8,
164     "version-name": "8.0.0",
165     "uuid": "uuid_8",
166     "type": "debug",
167     "validity": {
168         "not-before": 1262275700,
169         "not-after": 2524579300
170     },
171     "bundle-info": {
172         "developer-id": "developer_id_8",
173         "development-certificate": "ABCDEFGHIJK",
174         "distribution-certificate": "12345678901234",
175         "bundle-name": "com.test8",
176         "apl": "normal",
177         "app-feature": "hos_normal_app"
178     },
179     "debug-info": {
180         "device-id-type": "udid",
181         "device-ids" : ["1234ABCD"]
182     },
183     "issuer": "App Gallery"
184 })";
185 
186 class ProvisionVerifyTest : public testing::Test {
187 public:
188     static void SetUpTestCase(void);
189 
190     static void TearDownTestCase(void);
191 
192     void SetUp();
193 
194     void TearDown();
195 };
196 
SetUpTestCase(void)197 void ProvisionVerifyTest::SetUpTestCase(void)
198 {
199 }
200 
TearDownTestCase(void)201 void ProvisionVerifyTest::TearDownTestCase(void)
202 {
203 }
204 
SetUp()205 void ProvisionVerifyTest::SetUp()
206 {
207 }
208 
TearDown()209 void ProvisionVerifyTest::TearDown()
210 {
211 }
212 
213 /**
214  * @tc.name: Test HarmonyAppProvision ProvisionVerify001 function
215  * @tc.desc: The static function will return verify result;
216  * @tc.type: FUNC
217  */
218 HWTEST_F(ProvisionVerifyTest, ProvisionVerify001, TestSize.Level1)
219 {
220     /*
221         * @tc.steps: step1. input a invalid HarmonyAppProvision
222         *     and run ParseAndVerify function.
223         * @tc.expected: step1. return code will be PROVISION_INVALID.
224         */
225     ProfileInfo info;
226     int32_t ret = ParseAndVerify(INVALID_PROVISION_1, info);
227     ASSERT_NE(ret, AppProvisionVerifyResult::PROVISION_OK);
228     ret = ParseAndVerify(INVALID_PROVISION_2, info);
229     ASSERT_NE(ret, AppProvisionVerifyResult::PROVISION_OK);
230     ret = ParseAndVerify(INVALID_PROVISION_3, info);
231     ASSERT_EQ(ret, AppProvisionVerifyResult::PROVISION_OK);
232 }
233 
234 /**
235  * @tc.name: Test HarmonyAppProvision ProvisionVerify002 function
236  * @tc.desc: The static function will return verify result;
237  * @tc.type: FUNC
238  */
239 HWTEST_F(ProvisionVerifyTest, ProvisionVerify002, TestSize.Level1)
240 {
241     /*
242         * @tc.steps: step2. input a valid HarmonyAppProvision (without permission info)
243         *     and run ParseAndVerify function.
244         * @tc.expected: step2. return code will be PROVISION_OK.
245         */
246     ProfileInfo info;
247     int32_t ret = ParseAndVerify(VALID_PROVISION, info);
248     ASSERT_EQ(ret, AppProvisionVerifyResult::PROVISION_OK);
249     ASSERT_EQ(info.versionCode, 2); // version code is 2
250     ASSERT_EQ(info.versionName, "2.0.0");
251     ASSERT_EQ(info.uuid, "uuid_2");
252     ASSERT_EQ(info.type, ProvisionType::RELEASE);
253     ASSERT_TRUE(info.permissions.restrictedPermissions.empty());
254     ASSERT_TRUE(info.permissions.restrictedCapabilities.empty());
255 }
256 
257 /**
258  * @tc.name: Test HarmonyAppProvision ProvisionVerify003 function
259  * @tc.desc: The static function will return verify result;
260  * @tc.type: FUNC
261  */
262 HWTEST_F(ProvisionVerifyTest, ProvisionVerify003, TestSize.Level1)
263 {
264     /*
265         * @tc.steps: step3. input a valid HarmonyAppProvision (with permission info)
266         *     and run ParseAndVerify function.
267         * @tc.expected: step3. return code will be PROVISION_OK.
268         */
269     ProfileInfo info;
270     int32_t ret = ParseAndVerify(VALID_PROVISION_PERMISSION, info);
271     ASSERT_EQ(ret, AppProvisionVerifyResult::PROVISION_OK);
272     ASSERT_EQ(info.versionCode, 3); // version code is 3
273     ASSERT_EQ(info.versionName, "3.0.0");
274     ASSERT_EQ(info.uuid, "uuid_3");
275     ASSERT_EQ(info.type, ProvisionType::RELEASE);
276     ASSERT_EQ(info.distributionType, AppDistType::APP_GALLERY);
277     ASSERT_TRUE(info.permissions.restrictedPermissions.size() == 2);    // 2 restricted permissions
278     ASSERT_EQ(info.permissions.restrictedPermissions[0], "ohos.permission.GET_PHONE_STATE");
279     ASSERT_TRUE(info.permissions.restrictedCapabilities.size() == 1);   // 1 restricted capabilities
280     ASSERT_EQ(info.permissions.restrictedCapabilities[0], "ohos.permission.test");
281 }
282 } // namespace SignatureTools
283 } // namespace OHOS
284