1 /*
2  * Copyright (c) 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 
16 #include "nativetoken_oper_test.h"
17 #include <fcntl.h>
18 #include <poll.h>
19 #include <pthread.h>
20 #include "securec.h"
21 #include "nativetoken.h"
22 #include "nativetoken_json_oper.h"
23 #include "nativetoken_kit.h"
24 
25 using namespace testing::ext;
26 using namespace OHOS::Security;
27 
SetUpTestCase()28 void TokenOperTest::SetUpTestCase()
29 {}
30 
TearDownTestCase()31 void TokenOperTest::TearDownTestCase()
32 {}
33 
SetUp()34 void TokenOperTest::SetUp()
35 {}
36 
TearDown()37 void TokenOperTest::TearDown()
38 {}
39 
40 /**
41  * @tc.name: FreeStrArray001
42  * @tc.desc: FreeStrArray successfully.
43  * @tc.type: FUNC
44  * @tc.require:
45  */
46 HWTEST_F(TokenOperTest, FreeStrArray001, TestSize.Level1)
47 {
48     const int32_t testSize = 2; // 2 means test size
49     char **test = reinterpret_cast<char **>(malloc(sizeof(char *) * testSize));
50     ASSERT_NE(test, nullptr);
51     for (int32_t i = 0; i < testSize; i++) {
52         test[i] = reinterpret_cast<char *>(malloc(sizeof(char)));
53         ASSERT_NE(test[i], nullptr);
54     }
55     FreeStrArray(test, testSize - 1);
56     EXPECT_EQ(test[0], nullptr);
57     FreeStrArray(test, testSize - 1); // arr[i] == nullptr
58     for (int32_t i = 0; i < testSize; i++) {
59         free(test[i]);
60     }
61     free(test);
62 }
63 
64 /**
65  * @tc.name: GetProcessNameFromJson001
66  * @tc.desc: GetProcessNameFromJson successfully.
67  * @tc.type: FUNC
68  * @tc.require:
69  */
70 HWTEST_F(TokenOperTest, GetProcessNameFromJson001, TestSize.Level1)
71 {
72     NativeTokenList tokenNode;
73     std::string stringJson1 = R"()"\
74         R"({"processName":"process5","APL":3,"version":1,"tokenId":678065606,"tokenAttr":0,)"\
75         R"("dcaps":["AT_CAP","ST_CAP"], "permissions":[], "nativeAcls":[]})";
76 
77     cJSON* jsonroot = cJSON_Parse(stringJson1.c_str());
78     EXPECT_EQ(GetProcessNameFromJson(jsonroot, &tokenNode), 0);
79     cJSON_Delete(jsonroot);
80 
81     stringJson1 = R"()"\
82         R"({"processName":2,"APL":3,"version":1,"tokenId":678065606,"tokenAttr":0,)"\
83         R"("dcaps":["AT_CAP","ST_CAP"], "permissions":[], "nativeAcls":[]})";
84     jsonroot = cJSON_Parse(stringJson1.c_str());
85     EXPECT_NE(GetProcessNameFromJson(jsonroot, &tokenNode), 0);
86     cJSON_Delete(jsonroot);
87 
88     stringJson1 = R"()"\
89         R"({"processName":"partitionslot_hostApartitionslot_hostApartitionslot_hostApartitions)"\
90         R"(lot_hostApartitionslot_hostApartitionslot_hostApartitionslot_hostApartitionslot_hostApartitonslt_hosApa)"\
91         R"(lot_hostApartitionslot_hostApartitionslot_hostApartitionslot_hostApartitionslot_hostApartitonslo_hostpa)"\
92         R"(lot_hostApartitionslot_hostApartitionslot_hostApartitionslot_hostApartitionslot_hostApartitonslo_hostpa)"\
93         R"(lot_hostApartitionslot_hostApartitionslot_hostApartitionslot_hostApartitionslot_hostApartitonslt_htApa")"\
94         R"(,"APL":3,"version":1,"tokenId":678065606,"tokenAttr":0,)"\
95         R"("dcaps":["AT_CAP","ST_CAP"], "permissions":[], "nativeAcls":[]})";
96 
97     jsonroot = cJSON_Parse(stringJson1.c_str());
98     EXPECT_NE(GetProcessNameFromJson(jsonroot, &tokenNode), 0);
99     cJSON_Delete(jsonroot);
100 }
101 
102 /**
103  * @tc.name: GetTokenIdFromJson001
104  * @tc.desc: GetTokenIdFromJson successfully.
105  * @tc.type: FUNC
106  * @tc.require:
107  */
108 HWTEST_F(TokenOperTest, GetTokenIdFromJson001, TestSize.Level1)
109 {
110     NativeTokenList tokenNode;
111     const char *stringJson1 = "{\"processName\":\"partitionslot_host\","
112         "\"APL\":2,\"version\":1,\"tokenId\":\"672003577\",\"tokenAttr\":0,\"dcaps\":[],"
113         "\"permissions\":[],\"nativeAcls\":[]}";
114     cJSON* jsonroot = cJSON_Parse(stringJson1);
115     EXPECT_NE(GetTokenIdFromJson(jsonroot, &tokenNode), 0);
116     cJSON_Delete(jsonroot);
117 
118     const char *stringJson2 = "{\"processName\":\"partitionslot_host\","
119         "\"APL\":2,\"version\":1,\"tokenId\":0,\"tokenAttr\":0,\"dcaps\":[],"
120         "\"permissions\":[],\"nativeAcls\":[]}";
121     cJSON* jsonroot2 = cJSON_Parse(stringJson2);
122     EXPECT_NE(GetTokenIdFromJson(jsonroot2, &tokenNode), 0);
123     cJSON_Delete(jsonroot2);
124 
125     const char *stringJson3 = "{\"processName\":\"partitionslot_host\","
126         "\"APL\":2,\"version\":1,\"tokenId\":536962970,\"tokenAttr\":0,\"dcaps\":[],"
127         "\"permissions\":[],\"nativeAcls\":[]}";
128     cJSON* jsonroot3 = cJSON_Parse(stringJson3);
129     EXPECT_NE(GetTokenIdFromJson(jsonroot3, &tokenNode), 0);
130     cJSON_Delete(jsonroot3);
131 }
132 
133 /**
134  * @tc.name: GetAplFromJson001
135  * @tc.desc: GetAplFromJson successfully.
136  * @tc.type: FUNC
137  * @tc.require:
138  */
139 HWTEST_F(TokenOperTest, GetAplFromJson001, TestSize.Level1)
140 {
141     NativeTokenList tokenNode;
142     const char *stringJson1 = "{\"APL\":2}";
143     cJSON* jsonroot = cJSON_Parse(stringJson1);
144     EXPECT_EQ(GetAplFromJson(jsonroot, &tokenNode), 0);
145 
146     const char *stringJson2 = "{\"processName\":\"partitionslot_host\","
147         "\"APL\":\"2\",\"version\":1,\"tokenId\":672003577,\"tokenAttr\":0,\"dcaps\":[],"
148         "\"permissions\":[],\"nativeAcls\":[]}";
149     jsonroot = cJSON_Parse(stringJson2);
150     EXPECT_NE(GetAplFromJson(jsonroot, &tokenNode), 0);
151     cJSON_Delete(jsonroot);
152 
153     const char *stringJson3 = "{\"processName\":\"partitionslot_host\","
154         "\"APL\":-1,\"version\":1,\"tokenId\":672003577,\"tokenAttr\":0,\"dcaps\":[],"
155         "\"permissions\":[],\"nativeAcls\":[]}";
156     jsonroot = cJSON_Parse(stringJson3);
157     EXPECT_NE(GetAplFromJson(jsonroot, &tokenNode), 0);
158     cJSON_Delete(jsonroot);
159 
160     const char *stringJson4 = "{\"processName\":\"partitionslot_host\","
161         "\"APL\":4,\"version\":1,\"tokenId\":672003577,\"tokenAttr\":0,\"dcaps\":[],"
162         "\"permissions\":[],\"nativeAcls\":[]}";
163     jsonroot = cJSON_Parse(stringJson4);
164     EXPECT_NE(GetAplFromJson(jsonroot, &tokenNode), 0);
165     cJSON_Delete(jsonroot);
166 }
167 
168 /**
169  * @tc.name: GetInfoArrFromJson001
170  * @tc.desc: GetInfoArrFromJson successfully.
171  * @tc.type: FUNC
172  * @tc.require:
173  */
174 HWTEST_F(TokenOperTest, GetInfoArrFromJson001, TestSize.Level1)
175 {
176     const int32_t testSize = 2;
177     int32_t resSize;
178     StrArrayAttr attr;
179     attr.strKey = "dcaps";
180     attr.maxStrNum = 1;
181     attr.maxStrLen = 10;
182     char *test[testSize];
183     const char *stringJson1 = "{\"processName\":\"partitionslot_host\","
184         "\"dcaps\":[\"DCAPS_AT\",\"DCAPS_AT\", \"DCAPS_AT\",\"DCAPS_AT\"],"
185         "\"permissions\":[],\"nativeAcls\":[]}";
186     cJSON* jsonroot = cJSON_Parse(stringJson1);
187     EXPECT_NE(GetInfoArrFromJson(jsonroot, test, &resSize, &attr), 0);
188     cJSON_Delete(jsonroot);
189 
190     stringJson1 = "{\"processName\":\"partitionslot_host\","
191         "\"APL\":2,\"version\":1,\"tokenId\":672003577,\"tokenAttr\":0,\"dcaps\":[1],"
192         "\"permissions\":[],\"nativeAcls\":[]}";
193     jsonroot = cJSON_Parse(stringJson1);
194     EXPECT_NE(GetInfoArrFromJson(jsonroot, test, &resSize, &attr), 0);
195     cJSON_Delete(jsonroot);
196 
197     stringJson1 = "{\"processName\":\"partitionslot_host\","
198         "\"APL\":2,\"version\":1,\"tokenId\":672003577,\"tokenAttr\":0,\"dcaps\":[\"DCAPSAAAAAAAA_AT\"],"
199         "\"permissions\":[],\"nativeAcls\":[]}";
200     jsonroot = cJSON_Parse(stringJson1);
201     EXPECT_NE(GetInfoArrFromJson(jsonroot, test, &resSize, &attr), 0);
202     cJSON_Delete(jsonroot);
203 }
204 
205 /**
206  * @tc.name: UpdateGoalItemFromRecord001
207  * @tc.desc: UpdateGoalItemFromRecord abnormal.
208  * @tc.type: FUNC
209  * @tc.require:
210  */
211 HWTEST_F(TokenOperTest, UpdateGoalItemFromRecord001, TestSize.Level1)
212 {
213     NativeTokenList tokenNode;
214 
215     // processName json is null
216     const char *stringJson2 = "[{\"processName_\":\"partitionslot_host\","
217         "\"APL\":2,\"version\":1,\"tokenId\":672003577,\"tokenAttr\":0,\"dcaps\":[],"
218         "\"permissions\":[],\"nativeAcls\":[]}]";
219     cJSON* jsonroot = cJSON_Parse(stringJson2);
220     EXPECT_NE(UpdateGoalItemFromRecord(&tokenNode, jsonroot), 0);
221     cJSON_Delete(jsonroot);
222 
223     std::string stringJson1 = R"()"\
224         R"({"processName":"process5","APL":3,"version":1,"tokenId":678065606,"tokenAttr":0,)"\
225         R"("dcaps":["AT_CAP","ST_CAP"], "permissions":[], "nativeAcls":[]})";
226 
227     cJSON* jsonRoot = cJSON_Parse(stringJson1.c_str());
228     EXPECT_NE(UpdateGoalItemFromRecord(&tokenNode, jsonRoot), 0);
229 
230     // processName json is not string
231     const char *stringJson3 = "[{\"processName\": 1,"
232         "\"APL\":1,\"version\":1,\"tokenId\":672003577,\"tokenAttr\":0,\"dcaps\":[],"
233         "\"permissions\":[],\"nativeAcls\":[]}]";
234     jsonroot = cJSON_Parse(stringJson3);
235     EXPECT_NE(UpdateGoalItemFromRecord(&tokenNode, jsonroot), 0);
236     cJSON_Delete(jsonroot);
237 
238     const char *stringJson4 = "[]";
239     jsonroot = cJSON_Parse(stringJson4);
240     EXPECT_NE(UpdateGoalItemFromRecord(&tokenNode, jsonroot), 0);
241     cJSON_Delete(jsonroot);
242 }
243 
244