1 /*
2  * Copyright (c) 2024 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 <gtest/gtest.h>
16 
17 #include "inner_socket.h"
18 #include "nativetoken_kit.h"
19 #include "softbus_error_code.h"
20 #include "token_setproc.h"
21 
22 using namespace testing::ext;
23 namespace OHOS {
24 namespace {
CounterfeitProcess(const char * processName)25 void CounterfeitProcess(const char *processName)
26 {
27     NativeTokenInfoParams infoInstance = {
28         .dcapsNum = 0,
29         .permsNum = 0,
30         .aclsNum = 0,
31         .dcaps = nullptr,
32         .perms = nullptr,
33         .acls = nullptr,
34         .processName = processName,
35         .aplStr = "system_core",
36     };
37     uint64_t tokenId = GetAccessTokenId(&infoInstance);
38     SetSelfTokenID(tokenId);
39 }
40 } // namespace
41 
42 class ClientTransSocketTest : public testing::Test { };
43 
44 /*
45  * @tc.name: DBinderGrantPermissionTest001
46  * @tc.desc: Grant permission to DBinder test.
47  * @tc.type: FUNC
48  * @tc.require:
49  */
50 HWTEST_F(ClientTransSocketTest, DBinderGrantPermissionTest001, TestSize.Level1)
51 {
52     CounterfeitProcess("samgr");
53     int32_t uid = getuid();
54     ASSERT_GE(uid, 0);
55     int32_t pid = getpid();
56     ASSERT_GT(pid, 0);
57     std::string socketName = "DBinder" + std::to_string(uid) + std::string("_") + std::to_string(pid);
58     auto ret = DBinderGrantPermission(uid, pid, socketName.c_str());
59     ASSERT_NE(ret, SOFTBUS_INVALID_PARAM);
60     ret = DBinderRemovePermission(socketName.c_str());
61     ASSERT_NE(ret, SOFTBUS_INVALID_PARAM);
62 }
63 
64 /*
65  * @tc.name: DBinderGrantPermissionTest002
66  * @tc.desc: Other percess call DBinderGrantPermission test.
67  * @tc.type: FUNC
68  * @tc.require:
69  */
70 HWTEST_F(ClientTransSocketTest, DBinderGrantPermissionTest002, TestSize.Level1)
71 {
72     CounterfeitProcess("msdp");
73     int32_t uid = getuid();
74     ASSERT_GE(uid, 0);
75     int32_t pid = getpid();
76     ASSERT_GT(pid, 0);
77     std::string socketName = "DBinder" + std::to_string(uid) + std::string("_") + std::to_string(pid);
78     auto ret = DBinderGrantPermission(uid, pid, socketName.c_str());
79     ASSERT_EQ(ret, SOFTBUS_TRANS_PROXY_SEND_REQUEST_FAILED);
80 }
81 
82 /*
83  * @tc.name: DBinderRemovePermissionTest001
84  * @tc.desc: Other percess call DBinderRemovePermission test.
85  * @tc.type: FUNC
86  * @tc.require:
87  */
88 HWTEST_F(ClientTransSocketTest, DBinderRemovePermissionTest001, TestSize.Level1)
89 {
90     CounterfeitProcess("samgr");
91     int32_t uid = getuid();
92     ASSERT_GE(uid, 0);
93     int32_t pid = getpid();
94     ASSERT_GT(pid, 0);
95     std::string socketName = "DBinder" + std::to_string(uid) + std::string("_") + std::to_string(pid);
96     auto ret = DBinderGrantPermission(uid, pid, socketName.c_str());
97     ASSERT_NE(ret, SOFTBUS_INVALID_PARAM);
98     CounterfeitProcess("msdp");
99     ret = DBinderRemovePermission(socketName.c_str());
100     ASSERT_EQ(ret, SOFTBUS_TRANS_PROXY_SEND_REQUEST_FAILED);
101 }
102 } // namespace OHOS
103