1 /*
2 * Copyright (c) 2023 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 "hex_string_test.h"
16
17 #include <string>
18 #include "dlp_permission.h"
19 #include "dlp_permission_log.h"
20
21 namespace OHOS {
22 namespace Security {
23 namespace DlpPermission {
24 using namespace testing::ext;
25 using namespace OHOS;
26 using namespace OHOS::Security::DlpPermission;
27
28 namespace {
29 static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {
30 LOG_CORE, SECURITY_DOMAIN_DLP_PERMISSION, "HexStringTest"};
31 }
32
SetUpTestCase()33 void HexStringTest::SetUpTestCase() {}
34
TearDownTestCase()35 void HexStringTest::TearDownTestCase() {}
36
SetUp()37 void HexStringTest::SetUp() {}
38
TearDown()39 void HexStringTest::TearDown() {}
40
41 /**
42 * @tc.name: ByteToHexString001
43 * @tc.desc: ByteToHexString test
44 * @tc.type: FUNC
45 * @tc.require:
46 */
47 HWTEST_F(HexStringTest, ByteToHexString001, TestSize.Level1)
48 {
49 DLP_LOG_INFO(LABEL, "ByteToHexString001");
50 char hexStrBuffer[64] = {0};
51 uint8_t byteBuffer[30] = {0x1, 0x9, 0xf};
52
53 // byte is nullptr
54 EXPECT_EQ(ByteToHexString(nullptr, 0, hexStrBuffer, sizeof(hexStrBuffer)), DLP_SERVICE_ERROR_VALUE_INVALID);
55
56 // hexStr is nullptr
57 EXPECT_EQ(ByteToHexString(byteBuffer, sizeof(byteBuffer), nullptr, 1), DLP_SERVICE_ERROR_VALUE_INVALID);
58
59 // hexStrBuffer len too short
60 EXPECT_EQ(ByteToHexString(byteBuffer, sizeof(byteBuffer), hexStrBuffer, 1), DLP_SERVICE_ERROR_VALUE_INVALID);
61
62 // normal branch
63 EXPECT_EQ(ByteToHexString(byteBuffer, sizeof(byteBuffer), hexStrBuffer, sizeof(hexStrBuffer)), DLP_OK);
64 }
65
66 /**
67 * @tc.name: HexStringToByte001
68 * @tc.desc: HexStringToByte test
69 * @tc.type: FUNC
70 * @tc.require:
71 */
72 HWTEST_F(HexStringTest, HexStringToByte001, TestSize.Level1)
73 {
74 DLP_LOG_INFO(LABEL, "ByteToHexString001");
75 uint8_t byteBuffer[30] = {0};
76
77 // hexStr is nullptr
78 EXPECT_EQ(HexStringToByte(nullptr, 0, byteBuffer, sizeof(byteBuffer)), DLP_SERVICE_ERROR_VALUE_INVALID);
79 std::string test = "1d2c4F";
80 // byte is nullptr
81 EXPECT_EQ(HexStringToByte(test.c_str(), test.length(), nullptr, sizeof(byteBuffer)),
82 DLP_SERVICE_ERROR_VALUE_INVALID);
83 test = "1d2c4F1";
84 // hexStr is not 2 aligned
85 EXPECT_EQ(HexStringToByte(test.c_str(), test.length(), nullptr, sizeof(byteBuffer)),
86 DLP_SERVICE_ERROR_VALUE_INVALID);
87
88 // byte len is short
89 EXPECT_EQ(HexStringToByte(test.c_str(), test.length(), nullptr, 1), DLP_SERVICE_ERROR_VALUE_INVALID);
90 test = "1d2c4Fq";
91 // not hex number
92 EXPECT_EQ(HexStringToByte(test.c_str(), test.length(), nullptr, sizeof(byteBuffer)),
93 DLP_SERVICE_ERROR_VALUE_INVALID);
94 test = "1d2c4F";
95 // normal branch
96 EXPECT_EQ(HexStringToByte(test.c_str(), test.length(), byteBuffer, sizeof(byteBuffer)), DLP_OK);
97 }
98
99 /**
100 * @tc.name: HexStringToByte002
101 * @tc.desc: HexStringToByte test
102 * @tc.type: FUNC
103 * @tc.require:
104 */
105 HWTEST_F(HexStringTest, HexStringToByte002, TestSize.Level1)
106 {
107 DLP_LOG_INFO(LABEL, "ByteToHexString001");
108 uint8_t byteBuffer[30] = {0};
109 // normal branch
110 std::string test = "1d2c4";
111 EXPECT_EQ(HexStringToByte(test.c_str(), test.length(), byteBuffer, sizeof(byteBuffer)),
112 DLP_SERVICE_ERROR_VALUE_INVALID);
113 test = "1d2c4f";
114 EXPECT_EQ(HexStringToByte(test.c_str(), test.length(), byteBuffer, 1), DLP_SERVICE_ERROR_VALUE_INVALID);
115 test = "gd2c4f";
116 EXPECT_EQ(HexStringToByte(test.c_str(), test.length(), byteBuffer, sizeof(byteBuffer)),
117 DLP_SERVICE_ERROR_VALUE_INVALID);
118 test = "1g2c4f";
119 EXPECT_EQ(HexStringToByte(test.c_str(), test.length(), byteBuffer, sizeof(byteBuffer)),
120 DLP_SERVICE_ERROR_VALUE_INVALID);
121 }
122 } // namespace DlpPermission
123 } // namespace Security
124 } // namespace OHOS