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 <gtest/gtest.h>
17 #include <cstdlib>
18 #include "syscap_interface.h"
19 #include "syscap_codec_test.h"
20
21 using namespace testing::ext;
22 using namespace std;
23
24 namespace Syscap {
SetUpTestCase()25 void SyscapCodecTest::SetUpTestCase() {}
TearDownTestCase()26 void SyscapCodecTest::TearDownTestCase() {}
SetUp()27 void SyscapCodecTest::SetUp() {}
TearDown()28 void SyscapCodecTest::TearDown() {}
29
30 /*
31 * @tc.name: EncodeOsSyscap
32 * @tc.desc: Check the OsSyscap Coding.
33 * @tc.type: FUNC
34 */
35 HWTEST_F(SyscapCodecTest, EncodeOsSyscap, TestSize.Level1)
36 {
37 int pcidLen = PCID_MAIN_BYTES;
38 char OsInput[SINGLE_SYSCAP_LEN] = {0};
39 EXPECT_TRUE(EncodeOsSyscap(OsInput, pcidLen));
40 }
41
42 /*
43 * @tc.name: EncodePrivateSyscap
44 * @tc.desc: Check the PrivateSyscap Coding.
45 * @tc.type: FUNC
46 */
47 HWTEST_F(SyscapCodecTest, EncodePrivateSyscap, TestSize.Level1)
48 {
49 char *charPriInput = nullptr;
50 int priOutLen;
51 EXPECT_TRUE(EncodePrivateSyscap(&charPriInput, &priOutLen));
52 // Currently, private syscap is null.
53 EXPECT_EQ(*charPriInput, '\0');
54 EXPECT_EQ(priOutLen, 0);
55 free(charPriInput);
56 }
57
58 /*
59 * @tc.name: DecodeOsSyscap
60 * @tc.desc: Check the OsSyscap Decoding.
61 * @tc.type: FUNC
62 */
63 HWTEST_F(SyscapCodecTest, DecodeOsSyscap, TestSize.Level1)
64 {
65 int osSyscap[32] = {1, 3, 3};
66 char (*osOutput)[SINGLE_SYSCAP_LEN] = nullptr;
67 int decodeOsCnt;
68 char expectOsOutput001[] = "SystemCapability.Account.AppAccount";
69 char expectOsOutput002[] = "SystemCapability.Account.OsAccount";
70 EXPECT_TRUE(DecodeOsSyscap((char *)osSyscap, &osOutput, &decodeOsCnt));
71 char (*tmpOsOutput)[SINGLE_SYSCAP_LEN] = osOutput;
72 EXPECT_STREQ(*tmpOsOutput, expectOsOutput001);
73 EXPECT_STREQ(*(tmpOsOutput + 1), expectOsOutput002);
74 EXPECT_EQ(decodeOsCnt, 2);
75 free(osOutput);
76 }
77
78 /*
79 * @tc.name: DecodePrivateSyscap
80 * @tc.desc: Check the null PrivateSyscap Decoding.
81 * @tc.type: FUNC
82 */
83 HWTEST_F(SyscapCodecTest, DecodePrivateSyscap, TestSize.Level1)
84 {
85 char (*priOutput)[SINGLE_SYSCAP_LEN] = nullptr;
86 char priSyscap[] = "Device.syscap1GEDR,Device.syscap2WREGW,Vendor.syscap3RGD,Vendor.syscap4RWEG,Vendor.syscap5REWGWE,";
87 int decodePriCnt;
88 char expectPriOutput001[] = "SystemCapability.Device.syscap1GEDR";
89 char expectPriOutput002[] = "SystemCapability.Device.syscap2WREGW";
90 char expectPriOutput003[] = "SystemCapability.Vendor.syscap3RGD";
91 char expectPriOutput004[] = "SystemCapability.Vendor.syscap4RWEG";
92 char expectPriOutput005[] = "SystemCapability.Vendor.syscap5REWGWE";
93 EXPECT_TRUE(DecodePrivateSyscap(priSyscap, &priOutput, &decodePriCnt));
94 char (*tmpPtiOutput)[SINGLE_SYSCAP_LEN] = priOutput;
95 EXPECT_STREQ(*tmpPtiOutput++, expectPriOutput001);
96 EXPECT_STREQ(*tmpPtiOutput++, expectPriOutput002);
97 EXPECT_STREQ(*tmpPtiOutput++, expectPriOutput003);
98 EXPECT_STREQ(*tmpPtiOutput++, expectPriOutput004);
99 EXPECT_STREQ(*tmpPtiOutput, expectPriOutput005);
100 EXPECT_EQ(decodePriCnt, 5);
101 free(priOutput);
102 }
103
104 /*
105 * @tc.name: DecodePrivateSyscap1
106 * @tc.desc: Check the PrivateSyscap Decoding.
107 * @tc.type: FUNC
108 */
109 HWTEST_F(SyscapCodecTest, DecodePrivateSyscap1, TestSize.Level1)
110 {
111 char *charPriInput = nullptr;
112 char (*priOutput)[SINGLE_SYSCAP_LEN] = nullptr;
113 int priOutLen;
114 int decodePriCnt;
115
116 EXPECT_TRUE(EncodePrivateSyscap(&charPriInput, &priOutLen));
117 if (priOutLen == 0) {
118 EXPECT_TRUE(DecodePrivateSyscap(charPriInput, &priOutput, &decodePriCnt));
119 EXPECT_EQ((void *)priOutput, (void *)nullptr);
120 EXPECT_EQ(decodePriCnt, 0);
121 free(priOutput);
122 }
123 free(charPriInput);
124 }
125
126 /*
127 * @tc.name: ComparePcidString
128 * @tc.desc: Check the DecodeRpcidToStringFormat Decoding.
129 * @tc.type: FUNC
130 */
131 HWTEST_F(SyscapCodecTest, ComparePcidString, TestSize.Level1)
132 {
133 CompareError result = {{0}, 0, 0};
134 const char pcidString[] = "263168,0,3473408,0,0,0,1634,0,0,0,0,0,0,0,0,0,0,"\
135 "0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,SystemCapability.vendor.xxxxx3,"\
136 "SystemCapability.device.xxxxx4";
137 const char rpcidString[] = "33588992,1766370052,65536,276824064,0,0,0,0,0,0,"\
138 "0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,"\
139 "SystemCapability.vendor.xxxxx1,SystemCapability.device.xxxxx2";
140 const char expect[][256] = {"SystemCapability.HiviewDFX.HiLogLite",
141 "SystemCapability.HiviewDFX.Hiview.FaultLogger",
142 "SystemCapability.vendor.xxxxx1",
143 "SystemCapability.device.xxxxx2"};
144 int32_t ret = ComparePcidString(pcidString, rpcidString, &result);
145 EXPECT_EQ(ret, 3);
146 EXPECT_EQ(result.targetApiVersion, 7);
147 EXPECT_EQ(result.missSyscapNum, 4);
148 for (int i = 0; i < result.missSyscapNum; i++) {
149 EXPECT_STREQ(result.syscap[i], expect[i]);
150 }
151 (void)FreeCompareError(&result);
152 EXPECT_EQ(result.targetApiVersion, 0);
153 EXPECT_EQ(result.missSyscapNum, 0);
154 for (int i = 0; i < MAX_MISS_SYSCAP; i++) {
155 EXPECT_EQ((void *)result.syscap[i], (void *)nullptr);
156 }
157 }
158 } // namespace Syscap
159