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