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 "token_sync_kit_test.h"
17
18 #include "i_token_sync_manager.h"
19 #include "token_sync_manager_client.h"
20
21 using namespace testing::ext;
22
23 namespace OHOS {
24 namespace Security {
25 namespace AccessToken {
SetUpTestCase()26 void TokenSyncKitTest::SetUpTestCase()
27 {}
28
TearDownTestCase()29 void TokenSyncKitTest::TearDownTestCase()
30 {
31 }
32
SetUp()33 void TokenSyncKitTest::SetUp()
34 {
35 }
36
TearDown()37 void TokenSyncKitTest::TearDown()
38 {}
39
40 static const int TIME_500_MS = 1000 * 500; // 0.5 second
41
StartOrStopTokenSyncService(bool start)42 static void StartOrStopTokenSyncService(bool start)
43 {
44 pid_t pid = fork();
45 int ret = 0;
46 if (pid == 0) {
47 if (start) {
48 ret = execlp("service_control", "service_control", "start", "token_sync_service", nullptr);
49 } else {
50 ret = execlp("service_control", "service_control", "stop", "token_sync_service", nullptr);
51 }
52 if (ret == -1) {
53 std::cout << "execlp failed" << std::endl;
54 }
55 exit(0);
56 }
57 usleep(TIME_500_MS);
58 }
59
60 /**
61 * @tc.name: UpdateRemoteHapTokenInfo001
62 * @tc.desc: TokenSyncManagerProxy::UpdateRemoteHapTokenInfo function test
63 * @tc.type: FUNC
64 * @tc.require:
65 */
66 HWTEST_F(TokenSyncKitTest, UpdateRemoteHapTokenInfo001, TestSize.Level1)
67 {
68 HapTokenInfoForSync tokenInfo;
69
70 ASSERT_EQ(TokenSyncError::TOKEN_SYNC_IPC_ERROR,
71 TokenSyncManagerClient::GetInstance().UpdateRemoteHapTokenInfo(tokenInfo));
72
73 StartOrStopTokenSyncService(true);
74 ASSERT_EQ(0, TokenSyncManagerClient::GetInstance().UpdateRemoteHapTokenInfo(tokenInfo));
75 StartOrStopTokenSyncService(false);
76 }
77
78 /**
79 * @tc.name: GetRemoteHapTokenInfo001
80 * @tc.desc: TokenSyncManagerProxy::GetRemoteHapTokenInfo function test
81 * @tc.type: FUNC
82 * @tc.require:
83 */
84 HWTEST_F(TokenSyncKitTest, GetRemoteHapTokenInfo001, TestSize.Level1)
85 {
86 ASSERT_EQ(TokenSyncError::TOKEN_SYNC_IPC_ERROR,
87 TokenSyncManagerClient::GetInstance().GetRemoteHapTokenInfo("", 0));
88
89 StartOrStopTokenSyncService(true);
90 ASSERT_EQ(TokenSyncError::TOKEN_SYNC_PARAMS_INVALID,
91 TokenSyncManagerClient::GetInstance().GetRemoteHapTokenInfo("", 0));
92 StartOrStopTokenSyncService(false);
93 }
94
95 /**
96 * @tc.name: DeleteRemoteHapTokenInfo001
97 * @tc.desc: TokenSyncManagerProxy::DeleteRemoteHapTokenInfo function test
98 * @tc.type: FUNC
99 * @tc.require:
100 */
101 HWTEST_F(TokenSyncKitTest, DeleteRemoteHapTokenInfo001, TestSize.Level1)
102 {
103 ASSERT_EQ(TokenSyncError::TOKEN_SYNC_IPC_ERROR,
104 TokenSyncManagerClient::GetInstance().DeleteRemoteHapTokenInfo(0));
105
106 StartOrStopTokenSyncService(true);
107 ASSERT_EQ(TokenSyncError::TOKEN_SYNC_PARAMS_INVALID,
108 TokenSyncManagerClient::GetInstance().DeleteRemoteHapTokenInfo(0));
109 StartOrStopTokenSyncService(false);
110 }
111 } // namespace AccessToken
112 } // namespace Security
113 } // namespace OHOS
114