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 <gtest/gtest.h>
17 #define private public
18 #include "account_iam_mgr_proxy.h"
19 #undef private
20 #include "account_log_wrapper.h"
21 #include "iam_common_defines.h"
22 #include "test_common.h"
23 
24 namespace OHOS {
25 namespace AccountTest {
26 
27 using namespace testing;
28 using namespace testing::ext;
29 using namespace OHOS::AccountSA;
30 using namespace OHOS::UserIam::UserAuth;
31 namespace {
32 constexpr int32_t TEST_USER_ID = 200;
33 constexpr uint64_t TEST_CREDENTIAL_ID = 200;
34 } // namespace
35 
36 class AccountIAMMgrProxyTest : public testing::Test {
37 public:
38     static void SetUpTestCase(void);
39     static void TearDownTestCase(void);
40     void SetUp(void) override;
41     void TearDown(void) override;
42 };
43 
SetUpTestCase(void)44 void AccountIAMMgrProxyTest::SetUpTestCase(void)
45 {}
46 
TearDownTestCase(void)47 void AccountIAMMgrProxyTest::TearDownTestCase(void)
48 {}
49 
SetUp(void)50 void AccountIAMMgrProxyTest::SetUp(void) __attribute__((no_sanitize("cfi")))
51 {
52     testing::UnitTest *test = testing::UnitTest::GetInstance();
53     ASSERT_NE(test, nullptr);
54     const testing::TestInfo *testinfo = test->current_test_info();
55     ASSERT_NE(testinfo, nullptr);
56     string testCaseName = string(testinfo->name());
57     ACCOUNT_LOGI("[SetUp] %{public}s start", testCaseName.c_str());
58 }
59 
TearDown(void)60 void AccountIAMMgrProxyTest::TearDown(void)
61 {}
62 
63 /**
64  * @tc.name: OpenSession001
65  * @tc.desc: test OpenSession.
66  * @tc.type: FUNC
67  * @tc.require:
68  */
69 HWTEST_F(AccountIAMMgrProxyTest, OpenSession001, TestSize.Level0)
70 {
71     std::shared_ptr<AccountIAMMgrProxy> accountIAMMgrProxy = std::make_shared<AccountIAMMgrProxy>(nullptr);
72     std::vector<uint8_t> challenge;
73     int32_t ret = accountIAMMgrProxy->OpenSession(TEST_USER_ID, challenge);
74     EXPECT_EQ(ERR_ACCOUNT_COMMON_NULL_PTR_ERROR, ret);
75 }
76 
77 /**
78  * @tc.name: AccountIAMMgrProxy001
79  * @tc.desc: test callback is nullptr.
80  * @tc.type: FUNC
81  * @tc.require:
82  */
83 HWTEST_F(AccountIAMMgrProxyTest, AccountIAMMgrProxy001, TestSize.Level0)
84 {
85     std::shared_ptr<AccountIAMMgrProxy> accountIAMMgrProxy = std::make_shared<AccountIAMMgrProxy>(nullptr);
86     std::vector<uint8_t> challenge;
87     CredentialParameters credInfo;
88     const std::vector<uint8_t> authToken = {0, 0};
89     AccountSA::AuthParam authParam;
90     authParam.userId = TEST_USER_ID;
91     uint64_t contextId;
92     GetPropertyRequest g_request;
93     SetPropertyRequest s_request;
94     accountIAMMgrProxy->AddOrUpdateCredential(TEST_USER_ID, credInfo, nullptr, true);
95     accountIAMMgrProxy->DelCred(TEST_USER_ID, 0, authToken, nullptr);
96     accountIAMMgrProxy->DelUser(TEST_USER_ID, authToken, nullptr);
97     accountIAMMgrProxy->GetProperty(TEST_USER_ID, g_request, nullptr);
98     std::vector<Attributes::AttributeKey> keys { Attributes::AttributeKey::ATTR_PIN_SUB_TYPE };
99     accountIAMMgrProxy->GetPropertyByCredentialId(TEST_CREDENTIAL_ID, keys, nullptr);
100     accountIAMMgrProxy->SetProperty(TEST_USER_ID, s_request, nullptr);
101 
102     std::string cmd = "hilog -x | grep 'AccountIAMFwk'";
103     std::string cmdRes = RunCommand(cmd);
104     ASSERT_TRUE(cmdRes.find("callback is nullptr") != std::string::npos);
105     ASSERT_TRUE(cmdRes.find("get property callback is nullptr") != std::string::npos);
106     ASSERT_TRUE(cmdRes.find("Get property by id callback is nullptr") != std::string::npos);
107     ASSERT_TRUE(cmdRes.find("set property callback is nullptr") != std::string::npos);
108 
109     int32_t ret = accountIAMMgrProxy->GetCredentialInfo(TEST_USER_ID, AuthType::ALL, nullptr);
110     EXPECT_EQ(ERR_ACCOUNT_COMMON_INVALID_PARAMETER, ret);
111     ret = accountIAMMgrProxy->AuthUser(authParam, nullptr, contextId);
112     EXPECT_EQ(ERR_ACCOUNT_COMMON_INVALID_PARAMETER, ret);
113 
114     ret = accountIAMMgrProxy->PrepareRemoteAuth("testString", nullptr);
115     EXPECT_EQ(ERR_ACCOUNT_COMMON_INVALID_PARAMETER, ret);
116 }
117 }  // namespace AccountTest
118 }  // namespace OHOS