• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2025 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 <nativetoken_kit.h>
18 #include <token_setproc.h>
19 
20 #include "if_system_ability_manager.h"
21 #include "iservice_registry.h"
22 #include "system_ability_definition.h"
23 
24 #include "dsoftbus_interface.h"
25 #include "ipc_skeleton.h"
26 #include "rpc_test_service_proxy.h"
27 #include "rpc_test_service_stub.h"
28 
29 namespace OHOS {
30 using namespace testing::ext;
31 
InitTokenId(void)32 static void InitTokenId(void)
33 {
34     uint64_t tokenId;
35     const char *perms[] = {
36         OHOS_PERMISSION_DISTRIBUTED_SOFTBUS_CENTER,
37         OHOS_PERMISSION_DISTRIBUTED_DATASYNC,
38     };
39     uint32_t permsSize = sizeof(perms) / sizeof(perms[0]);
40     NativeTokenInfoParams infoInstance = {
41         .dcapsNum = 0,
42         .permsNum = permsSize,
43         .aclsNum = 0,
44         .dcaps = NULL,
45         .perms = perms,
46         .acls = NULL,
47         .processName = "com.rpc.test",
48         .aplStr = "system_core",
49     };
50     tokenId = GetAccessTokenId(&infoInstance);
51     SetSelfTokenID(tokenId);
52 }
53 
54 class RpcServerTest : public testing::Test {
55 public:
56     static void SetUpTestCase(void);
57     static void TearDownTestCase(void);
58 };
59 
SetUpTestCase()60 void RpcServerTest::SetUpTestCase()
61 {
62     InitTokenId();
63 }
64 
TearDownTestCase()65 void RpcServerTest::TearDownTestCase()
66 {
67     IPCSkeleton::JoinWorkThread();
68 }
69 
70 /**
71  * @tc.name: AddTestServices001
72  * @tc.desc: Register distributed services to Samgr
73  * @tc.type: FUNC
74  */
75 HWTEST_F(RpcServerTest, add_test_services_001, TestSize.Level1)
76 {
77     auto saMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
78     ASSERT_TRUE(saMgr != nullptr);
79 
80     sptr<RpcTestServiceStub> stub = new RpcTestServiceStub();
81     ASSERT_TRUE(stub != nullptr);
82 
83     ISystemAbilityManager::SAExtraProp saExtra;
84     saExtra.isDistributed = true;
85     int result = saMgr->AddSystemAbility(RPC_TEST_SERVICE, stub, saExtra);
86     ASSERT_EQ(result, ERR_NONE);
87 }
88 }