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