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 
16 #include <cerrno>
17 #include <gtest/gtest.h>
18 #include <thread>
19 #include "accesstoken_kit.h"
20 #include "account_log_wrapper.h"
21 #define private public
22 #include "account_iam_mgr_stub.h"
23 #include "account_iam_service.h"
24 #undef private
25 #include "token_setproc.h"
26 #include "parcel.h"
27 #include "want.h"
28 using namespace testing;
29 using namespace testing::ext;
30 using namespace OHOS;
31 using namespace OHOS::AccountSA;
32 
33 namespace {
34 DECLARE_INTERFACE_DESCRIPTOR(u"ohos.accountfwk.IAccountIAM");
35 const int32_t LIMIT_CODE = 13;
36 } // namespace
37 
38 class AccountIAMStubModuleTest : public testing::Test {
39 public:
40     static void SetUpTestCase(void);
41     static void TearDownTestCase(void);
42     void SetUp();
43     void TearDown();
44     sptr<AccountIAMService> service_ = nullptr;
45 };
46 
SetUpTestCase(void)47 void AccountIAMStubModuleTest::SetUpTestCase(void)
48 {
49     OHOS::Security::AccessToken::AccessTokenID tokenId =
50         OHOS::Security::AccessToken::AccessTokenKit::GetNativeTokenId("accountmgr");
51     SetSelfTokenID(tokenId);
52 }
53 
TearDownTestCase(void)54 void AccountIAMStubModuleTest::TearDownTestCase(void)
55 {}
56 
SetUp(void)57 void AccountIAMStubModuleTest::SetUp(void) __attribute__((no_sanitize("cfi")))
58 {
59     testing::UnitTest *test = testing::UnitTest::GetInstance();
60     ASSERT_NE(test, nullptr);
61     const testing::TestInfo *testinfo = test->current_test_info();
62     ASSERT_NE(testinfo, nullptr);
63     string testCaseName = string(testinfo->name());
64     ACCOUNT_LOGI("[SetUp] %{public}s start", testCaseName.c_str());
65 
66     service_ = new (std::nothrow) AccountIAMService();
67     ASSERT_NE(service_, nullptr);
68 }
69 
TearDown(void)70 void AccountIAMStubModuleTest::TearDown(void)
71 {}
72 
73 /**
74  * @tc.name: AccountIAMStubModuleTest_OnRemoteRequest_001
75  * @tc.desc: OnRemoteRequest with invalid code.
76  * @tc.type: FUNC
77  * @tc.require:
78  */
79 HWTEST_F(AccountIAMStubModuleTest, AccountIAMStubModuleTest_OnRemoteRequest_001, TestSize.Level0)
80 {
81     MessageParcel data;
82     MessageParcel reply;
83     MessageOption option;
84     data.WriteInterfaceToken(GetDescriptor());
85     EXPECT_NE(service_->OnRemoteRequest(-1, data, reply, option), ERR_NONE);
86 }
87 
88 /**
89  * @tc.name: AccountIAMStubModuleTest_OnRemoteRequest_002
90  * @tc.desc: OnRemoteRequest with not InterfaceToken.
91  * @tc.type: FUNC
92  * @tc.require:
93  */
94 HWTEST_F(AccountIAMStubModuleTest, AccountIAMStubModuleTest_OnRemoteRequest_002, TestSize.Level0)
95 {
96     MessageParcel data;
97     MessageParcel reply;
98     MessageOption option;
99     EXPECT_NE(service_->OnRemoteRequest(-1, data, reply, option), ERR_NONE);
100 }
101 
102 /**
103  * @tc.name: AccountIAMStubModuleTest_OnRemoteRequest_003
104  * @tc.desc: OnRemoteRequest with invalid code.
105  * @tc.type: FUNC
106  * @tc.require:
107  */
108 HWTEST_F(AccountIAMStubModuleTest, AccountIAMStubModuleTest_OnRemoteRequest_003, TestSize.Level0)
109 {
110     for (int code = 0; code <= LIMIT_CODE; code++) {
111         MessageParcel data;
112         MessageParcel reply;
113         MessageOption option;
114         data.WriteInterfaceToken(GetDescriptor());
115         EXPECT_NE(service_->OnRemoteRequest(static_cast<uint32_t>(static_cast<uint32_t>(code)), data, reply, option),
116             ERR_NONE);
117     }
118 }