1 /*
2  * Copyright (c) 2024 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 #include <securec.h>
18 
19 #include "auth_common.h"
20 #include "auth_log.h"
21 #include "auth_normalize_request.h"
22 
23 namespace OHOS {
24 using namespace testing;
25 using namespace testing::ext;
26 
27 class AuthNormalizeRequestTest : public testing::Test {
28 public:
29     static void SetUpTestCase();
30     static void TearDownTestCase();
31     void SetUp();
32     void TearDown();
33 };
34 
SetUpTestCase()35 void AuthNormalizeRequestTest::SetUpTestCase()
36 {
37 }
38 
TearDownTestCase()39 void AuthNormalizeRequestTest::TearDownTestCase() {}
40 
SetUp()41 void AuthNormalizeRequestTest::SetUp()
42 {
43     AUTH_LOGI(AUTH_TEST, "AuthNormalizeRequestTest start");
44 }
45 
TearDown()46 void AuthNormalizeRequestTest::TearDown() {}
47 
48 /*
49  * @tc.name: NOTIFY_NORMALIZE_REQUEST_SUCCESS_TEST_001
50  * @tc.desc: notify normalize request success test
51  * @tc.type: FUNC
52  * @tc.require:
53  */
54 HWTEST_F(AuthNormalizeRequestTest, NOTIFY_NORMALIZE_REQUEST_SUCCESS_TEST_001, TestSize.Level1)
55 {
56     int64_t authSeq = -1;
57     NormalizeRequest request;
58     const char *udidHash = "testudidhash";
59 
60     (void)memset_s(&request, sizeof(NormalizeRequest), 0, sizeof(NormalizeRequest));
61     EXPECT_TRUE(AuthCommonInit() == SOFTBUS_OK);
62     request.authSeq = 1;
63     (void)memcpy_s(request.udidHash, SHA_256_HEX_HASH_LEN, udidHash, SHA_256_HEX_HASH_LEN);
64     uint32_t ret = AddNormalizeRequest(&request);
65     EXPECT_TRUE(ret != 0);
66     request.authSeq = 2;
67     ret = AddNormalizeRequest(&request);
68     EXPECT_TRUE(ret != 0);
69     DelAuthNormalizeRequest(request.authSeq);
70     request.authSeq = 3;
71     ret = AddNormalizeRequest(&request);
72     EXPECT_TRUE(ret != 0);
73     bool result = AuthIsRepeatedAuthRequest(request.authSeq);
74     EXPECT_TRUE(result == true);
75     NotifyNormalizeRequestSuccess(authSeq, false);
76     authSeq = 1;
77     NotifyNormalizeRequestSuccess(authSeq, false);
78     AuthCommonDeinit();
79 }
80 
81 /*
82  * @tc.name: NOTIFY_NORMALIZE_REQUEST_FAIL_TEST_001
83  * @tc.desc: notify normalize request fail test
84  * @tc.type: FUNC
85  * @tc.require:
86  */
87 HWTEST_F(AuthNormalizeRequestTest, NOTIFY_NORMALIZE_REQUEST_FAIL_TEST_001, TestSize.Level1)
88 {
89     int64_t authSeq = -1;
90     NormalizeRequest request;
91     const char *udidHash = "testudidhash1";
92 
93     (void)memset_s(&request, sizeof(NormalizeRequest), 0, sizeof(NormalizeRequest));
94     EXPECT_TRUE(AuthCommonInit() == SOFTBUS_OK);
95     request.authSeq = 1;
96     (void)memcpy_s(request.udidHash, SHA_256_HEX_HASH_LEN, udidHash, SHA_256_HEX_HASH_LEN);
97     uint32_t ret = AddNormalizeRequest(&request);
98     EXPECT_TRUE(ret != 0);
99     request.authSeq = 2;
100     ret = AddNormalizeRequest(&request);
101     EXPECT_TRUE(ret != 0);
102     request.authSeq = 3;
103     ret = AddNormalizeRequest(&request);
104     EXPECT_TRUE(ret != 0);
105     NotifyNormalizeRequestFail(authSeq, -1);
106     authSeq = 1;
107     NotifyNormalizeRequestFail(authSeq, -1);
108     AuthCommonDeinit();
109 }
110 } // namespace OHOS
111