1fc0b0055Sopenharmony_ci/*
2fc0b0055Sopenharmony_ci * Copyright (c) 2022-2024 Huawei Device Co., Ltd.
3fc0b0055Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4fc0b0055Sopenharmony_ci * you may not use this file except in compliance with the License.
5fc0b0055Sopenharmony_ci * You may obtain a copy of the License at
6fc0b0055Sopenharmony_ci *
7fc0b0055Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8fc0b0055Sopenharmony_ci *
9fc0b0055Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10fc0b0055Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11fc0b0055Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12fc0b0055Sopenharmony_ci * See the License for the specific language governing permissions and
13fc0b0055Sopenharmony_ci * limitations under the License.
14fc0b0055Sopenharmony_ci */
15fc0b0055Sopenharmony_ci
16fc0b0055Sopenharmony_ci#include <gtest/gtest.h>
17fc0b0055Sopenharmony_ci#include <memory>
18fc0b0055Sopenharmony_ci#include <string>
19fc0b0055Sopenharmony_ci#include <fcntl.h>
20fc0b0055Sopenharmony_ci
21fc0b0055Sopenharmony_ci#include "access_token_error.h"
22fc0b0055Sopenharmony_ci#include "constant_common.h"
23fc0b0055Sopenharmony_ci#define private public
24fc0b0055Sopenharmony_ci#include "permission_map.h"
25fc0b0055Sopenharmony_ci#undef private
26fc0b0055Sopenharmony_ci#include "json_parser.h"
27fc0b0055Sopenharmony_ci
28fc0b0055Sopenharmony_ciusing namespace testing::ext;
29fc0b0055Sopenharmony_ci
30fc0b0055Sopenharmony_cinamespace OHOS {
31fc0b0055Sopenharmony_cinamespace Security {
32fc0b0055Sopenharmony_cinamespace AccessToken {
33fc0b0055Sopenharmony_cinamespace {
34fc0b0055Sopenharmony_ciconst static uint32_t MAX_PERM_SIZE = 2048;
35fc0b0055Sopenharmony_ciconst static uint32_t MAX_CONFIG_FILE_SIZE = 5 * 1024;
36fc0b0055Sopenharmony_ciconst static std::string TEST_JSON_PATH = "/data/test.json";
37fc0b0055Sopenharmony_ciconst static std::string TEST_STR =
38fc0b0055Sopenharmony_ci    "iVBORw0KGgoAAAANSUhEUgAAABUAAAAXCAIAAABrvZPKAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAAEXRFWHRTb2Z0d2FyZQBTbmlwYXN0ZV0Xzt0AA"
39fc0b0055Sopenharmony_ci    "iVBORw0KGgoAAAANSUhEUgAAABUAAAAXCAIAAABrvZPKAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAAEXRFWHRTb2Z0d2FyZQBTbmlwYXN0ZV0Xzt0AA"
40fc0b0055Sopenharmony_ci    "iVBORw0KGgoAAAANSUhEUgAAABUAAAAXCAIAAABrvZPKAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAAEXRFWHRTb2Z0d2FyZQBTbmlwYXN0ZV0Xzt0AA"
41fc0b0055Sopenharmony_ci    "iVBORw0KGgoAAAANSUhEUgAAABUAAAAXCAIAAABrvZPKAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAAEXRFWHRTb2Z0d2FyZQBTbmlwYXN0ZV0Xzt0AA"
42fc0b0055Sopenharmony_ci    "iVBORw0KGgoAAAANSUhEUgAAABUAAAAXCAIAAABrvZPKAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAAEXRFWHRTb2Z0d2FyZQBTbmlwYXN0ZV0Xzt0AA"
43fc0b0055Sopenharmony_ci    "iVBORw0KGgoAAAANSUhEUgAAABUAAAAXCAIAAABrvZPKAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAAEXRFWHRTb2Z0d2FyZQBTbmlwYXN0ZV0Xzt0AA"
44fc0b0055Sopenharmony_ci    "iVBORw0KGgoAAAANSUhEUgAAABUAAAAXCAIAAABrvZPKAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAAEXRFWHRTb2Z0d2FyZQBTbmlwYXN0ZV0Xzt0AA"
45fc0b0055Sopenharmony_ci    "iVBORw0KGgoAAAANSUhEUgAAABUAAAAXCAIAAABrvZPKAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAAEXRFWHRTb2Z0d2FyZQBTbmlwYXN0ZV0Xzt0AA"
46fc0b0055Sopenharmony_ci    "iVBORw0KGgoAAAANSUhEUgAAABUAAAAXCAIAAABrvZPKAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAAEXRFWHRTb2Z0d2FyZQBTbmlwYXN0ZV0Xzt0AA"
47fc0b0055Sopenharmony_ci    "iVBORw0KGgoAAAANSUhEUgAAABUAAAAXCAIAAABrvZPKAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAAEXRFWHRTb2Z0d2FyZQBTbmlwYXN0ZV0Xzt0AA";
48fc0b0055Sopenharmony_ci}
49fc0b0055Sopenharmony_ciclass CommonTest : public testing::Test  {
50fc0b0055Sopenharmony_cipublic:
51fc0b0055Sopenharmony_ci    static void SetUpTestCase();
52fc0b0055Sopenharmony_ci    static void TearDownTestCase();
53fc0b0055Sopenharmony_ci
54fc0b0055Sopenharmony_ci    void SetUp();
55fc0b0055Sopenharmony_ci    void TearDown();
56fc0b0055Sopenharmony_ci};
57fc0b0055Sopenharmony_ci
58fc0b0055Sopenharmony_civoid CommonTest::SetUpTestCase() {}
59fc0b0055Sopenharmony_civoid CommonTest::TearDownTestCase() {}
60fc0b0055Sopenharmony_civoid CommonTest::SetUp() {}
61fc0b0055Sopenharmony_civoid CommonTest::TearDown() {}
62fc0b0055Sopenharmony_ci
63fc0b0055Sopenharmony_ci/**
64fc0b0055Sopenharmony_ci * @tc.name: EncryptDevId001
65fc0b0055Sopenharmony_ci * @tc.desc: Test EncryptDevId001 function.
66fc0b0055Sopenharmony_ci * @tc.type: FUNC
67fc0b0055Sopenharmony_ci * @tc.require: issueI5RUCC
68fc0b0055Sopenharmony_ci */
69fc0b0055Sopenharmony_ciHWTEST_F(CommonTest, EncryptDevId001, TestSize.Level1)
70fc0b0055Sopenharmony_ci{
71fc0b0055Sopenharmony_ci    std::string res;
72fc0b0055Sopenharmony_ci    res = ConstantCommon::EncryptDevId("");
73fc0b0055Sopenharmony_ci    EXPECT_EQ(res, "");
74fc0b0055Sopenharmony_ci
75fc0b0055Sopenharmony_ci    res = ConstantCommon::EncryptDevId("12345");
76fc0b0055Sopenharmony_ci    EXPECT_EQ(res, "1*******");
77fc0b0055Sopenharmony_ci
78fc0b0055Sopenharmony_ci    res = ConstantCommon::EncryptDevId("123454321");
79fc0b0055Sopenharmony_ci    EXPECT_EQ(res, "1234****4321");
80fc0b0055Sopenharmony_ci}
81fc0b0055Sopenharmony_ci
82fc0b0055Sopenharmony_ci/*
83fc0b0055Sopenharmony_ci * @tc.name: TransferOpcodeToPermission001
84fc0b0055Sopenharmony_ci * @tc.desc: TransferOpcodeToPermission function test
85fc0b0055Sopenharmony_ci * @tc.type: FUNC
86fc0b0055Sopenharmony_ci * @tc.require: issueI6024A
87fc0b0055Sopenharmony_ci */
88fc0b0055Sopenharmony_ciHWTEST_F(CommonTest, TransferOpcodeToPermission001, TestSize.Level1)
89fc0b0055Sopenharmony_ci{
90fc0b0055Sopenharmony_ci    std::string permissionName;
91fc0b0055Sopenharmony_ci    EXPECT_TRUE(TransferOpcodeToPermission(0, permissionName));
92fc0b0055Sopenharmony_ci    EXPECT_EQ(permissionName, "ohos.permission.ANSWER_CALL");
93fc0b0055Sopenharmony_ci}
94fc0b0055Sopenharmony_ci
95fc0b0055Sopenharmony_ci/*
96fc0b0055Sopenharmony_ci * @tc.name: TransferOpcodeToPermission002
97fc0b0055Sopenharmony_ci * @tc.desc: TransferOpcodeToPermission code oversize
98fc0b0055Sopenharmony_ci * @tc.type: FUNC
99fc0b0055Sopenharmony_ci * @tc.require: issueI6024A
100fc0b0055Sopenharmony_ci */
101fc0b0055Sopenharmony_ciHWTEST_F(CommonTest, TransferOpcodeToPermission002, TestSize.Level1)
102fc0b0055Sopenharmony_ci{
103fc0b0055Sopenharmony_ci    std::string permissionName;
104fc0b0055Sopenharmony_ci    EXPECT_FALSE(TransferOpcodeToPermission(MAX_PERM_SIZE, permissionName));
105fc0b0055Sopenharmony_ci    EXPECT_FALSE(TransferOpcodeToPermission(MAX_PERM_SIZE - 1, permissionName));
106fc0b0055Sopenharmony_ci}
107fc0b0055Sopenharmony_ci
108fc0b0055Sopenharmony_ci/*
109fc0b0055Sopenharmony_ci * @tc.name: GetUnsignedIntFromJson001
110fc0b0055Sopenharmony_ci * @tc.desc: GetUnsignedIntFromJson
111fc0b0055Sopenharmony_ci * @tc.type: FUNC
112fc0b0055Sopenharmony_ci * @tc.require: issueI6024A
113fc0b0055Sopenharmony_ci */
114fc0b0055Sopenharmony_ciHWTEST_F(CommonTest, GetUnsignedIntFromJson001, TestSize.Level1)
115fc0b0055Sopenharmony_ci{
116fc0b0055Sopenharmony_ci    const nlohmann::json json;
117fc0b0055Sopenharmony_ci    u_int32_t out = 0;
118fc0b0055Sopenharmony_ci    EXPECT_FALSE(JsonParser::GetUnsignedIntFromJson(json, "tokenId", out));
119fc0b0055Sopenharmony_ci    EXPECT_EQ(0, out);
120fc0b0055Sopenharmony_ci}
121fc0b0055Sopenharmony_ci
122fc0b0055Sopenharmony_ci/*
123fc0b0055Sopenharmony_ci * @tc.name: ReadCfgFile001
124fc0b0055Sopenharmony_ci * @tc.desc: GetUnsignedIntFromJson json invalid
125fc0b0055Sopenharmony_ci * @tc.type: FUNC
126fc0b0055Sopenharmony_ci * @tc.require: issueI6024A
127fc0b0055Sopenharmony_ci */
128fc0b0055Sopenharmony_ciHWTEST_F(CommonTest, ReadCfgFile001, TestSize.Level1)
129fc0b0055Sopenharmony_ci{
130fc0b0055Sopenharmony_ci    int32_t fd = open(TEST_JSON_PATH.c_str(), O_RDWR | O_CREAT);
131fc0b0055Sopenharmony_ci    EXPECT_NE(-1, fd);
132fc0b0055Sopenharmony_ci    std::string rawData;
133fc0b0055Sopenharmony_ci    EXPECT_EQ(ERR_PARAM_INVALID, JsonParser::ReadCfgFile(TEST_JSON_PATH, rawData));
134fc0b0055Sopenharmony_ci    for (int i = 0; i < MAX_CONFIG_FILE_SIZE; i++) {
135fc0b0055Sopenharmony_ci        size_t strLen = strlen(TEST_STR.c_str());
136fc0b0055Sopenharmony_ci        write(fd, TEST_STR.c_str(), strLen);
137fc0b0055Sopenharmony_ci    }
138fc0b0055Sopenharmony_ci    EXPECT_EQ(ERR_OVERSIZE, JsonParser::ReadCfgFile(TEST_JSON_PATH, rawData));
139fc0b0055Sopenharmony_ci    close(fd);
140fc0b0055Sopenharmony_ci    sleep(5);
141fc0b0055Sopenharmony_ci
142fc0b0055Sopenharmony_ci    remove(TEST_JSON_PATH.c_str());
143fc0b0055Sopenharmony_ci}
144fc0b0055Sopenharmony_ci
145fc0b0055Sopenharmony_ci/*
146fc0b0055Sopenharmony_ci * @tc.name: IsDirExsit001
147fc0b0055Sopenharmony_ci * @tc.desc: IsDirExsit input param error
148fc0b0055Sopenharmony_ci * @tc.type: FUNC
149fc0b0055Sopenharmony_ci * @tc.require: issueI6024A
150fc0b0055Sopenharmony_ci */
151fc0b0055Sopenharmony_ciHWTEST_F(CommonTest, IsDirExsit001, TestSize.Level1)
152fc0b0055Sopenharmony_ci{
153fc0b0055Sopenharmony_ci    EXPECT_FALSE(JsonParser::IsDirExsit(""));
154fc0b0055Sopenharmony_ci    int32_t fd = open(TEST_JSON_PATH.c_str(), O_RDWR | O_CREAT);
155fc0b0055Sopenharmony_ci    EXPECT_NE(-1, fd);
156fc0b0055Sopenharmony_ci
157fc0b0055Sopenharmony_ci    EXPECT_FALSE(JsonParser::IsDirExsit(TEST_JSON_PATH.c_str()));
158fc0b0055Sopenharmony_ci}
159fc0b0055Sopenharmony_ci} // namespace AccessToken
160fc0b0055Sopenharmony_ci} // namespace Security
161fc0b0055Sopenharmony_ci} // namespace OHOS
162