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
21using namespace testing::ext;
22using namespace std;
23
24namespace Syscap {
25void SyscapCodecTest::SetUpTestCase() {}
26void SyscapCodecTest::TearDownTestCase() {}
27void SyscapCodecTest::SetUp() {}
28void SyscapCodecTest::TearDown() {}
29
30/*
31 * @tc.name: EncodeOsSyscap
32 * @tc.desc: Check the OsSyscap Coding.
33 * @tc.type: FUNC
34 */
35HWTEST_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 */
47HWTEST_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 */
63HWTEST_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 */
83HWTEST_F(SyscapCodecTest, DecodePrivateSyscap, TestSize.Level1)
84{
85    char (*priOutput)[SINGLE_SYSCAP_LEN] = nullptr;
86    char priSyscap[] = "Device.syscap1GEDR,Device.syscap2WREGW,Vendor."
87        "syscap3RGD,Vendor.syscap4RWEG,Vendor.syscap5REWGWE,";
88    int decodePriCnt;
89    char expectPriOutput001[] = "SystemCapability.Device.syscap1GEDR";
90    char expectPriOutput002[] = "SystemCapability.Device.syscap2WREGW";
91    char expectPriOutput003[] = "SystemCapability.Vendor.syscap3RGD";
92    char expectPriOutput004[] = "SystemCapability.Vendor.syscap4RWEG";
93    char expectPriOutput005[] = "SystemCapability.Vendor.syscap5REWGWE";
94    EXPECT_TRUE(DecodePrivateSyscap(priSyscap, &priOutput, &decodePriCnt));
95    char (*tmpPtiOutput)[SINGLE_SYSCAP_LEN] = priOutput;
96    EXPECT_STREQ(*tmpPtiOutput++, expectPriOutput001);
97    EXPECT_STREQ(*tmpPtiOutput++, expectPriOutput002);
98    EXPECT_STREQ(*tmpPtiOutput++, expectPriOutput003);
99    EXPECT_STREQ(*tmpPtiOutput++, expectPriOutput004);
100    EXPECT_STREQ(*tmpPtiOutput, expectPriOutput005);
101    EXPECT_EQ(decodePriCnt, 5);
102    free(priOutput);
103}
104
105/*
106 * @tc.name: DecodePrivateSyscap1
107 * @tc.desc: Check the PrivateSyscap Decoding.
108 * @tc.type: FUNC
109 */
110HWTEST_F(SyscapCodecTest, DecodePrivateSyscap1, TestSize.Level1)
111{
112    char *charPriInput = nullptr;
113    char (*priOutput)[SINGLE_SYSCAP_LEN] = nullptr;
114    int priOutLen;
115    int decodePriCnt;
116
117    EXPECT_TRUE(EncodePrivateSyscap(&charPriInput, &priOutLen));
118    if (priOutLen == 0) {
119        EXPECT_TRUE(DecodePrivateSyscap(charPriInput, &priOutput, &decodePriCnt));
120        EXPECT_EQ((void *)priOutput, (void *)nullptr);
121        EXPECT_EQ(decodePriCnt, 0);
122        free(priOutput);
123    }
124    free(charPriInput);
125}
126
127/*
128 * @tc.name: ComparePcidString
129 * @tc.desc: Check the DecodeRpcidToStringFormat Decoding.
130 * @tc.type: FUNC
131 */
132HWTEST_F(SyscapCodecTest, ComparePcidString, TestSize.Level1)
133{
134    CompareError result = {{0}, 0, 0};
135    const char pcidString[] = "263168,0,3473408,0,0,0,1634,0,0,0,0,0,0,0,0,0,0,"\
136                              "0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,SystemCapability.vendor.xxxxx3,"\
137                              "SystemCapability.device.xxxxx4";
138    const char rpcidString[] = "33588992,1766370052,65536,276824064,0,0,0,0,0,0,"\
139                               "0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,"\
140                               "SystemCapability.vendor.xxxxx1,SystemCapability.device.xxxxx2";
141    const char expect[][256] = {"SystemCapability.HiviewDFX.HiLogLite",
142                                "SystemCapability.HiviewDFX.Hiview.FaultLogger",
143                                "SystemCapability.vendor.xxxxx1",
144                                "SystemCapability.device.xxxxx2"};
145    int32_t ret = ComparePcidString(pcidString, rpcidString, &result);
146    EXPECT_EQ(ret, 3);
147    EXPECT_EQ(result.targetApiVersion, 7);
148    EXPECT_EQ(result.missSyscapNum, 4);
149    for (int i = 0; i < result.missSyscapNum; i++) {
150        EXPECT_STREQ(result.syscap[i], expect[i]);
151    }
152    (void)FreeCompareError(&result);
153    EXPECT_EQ(result.targetApiVersion, 0);
154    EXPECT_EQ(result.missSyscapNum, 0);
155    for (int i = 0; i < MAX_MISS_SYSCAP; i++) {
156        EXPECT_EQ((void *)result.syscap[i], (void *)nullptr);
157    }
158}
159} // namespace Syscap
160