1 /*
2 * Copyright (C) 2022 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 <gmock/gmock.h>
18
19 #define private public
20 #include "databus_session_callback.h"
21 #include "ipc_thread_skeleton.h"
22 #include "ipc_types.h"
23 #include "iremote_object.h"
24 #include "mock_session_impl.h"
25 #undef private
26
27 using namespace testing::ext;
28 using namespace OHOS;
29
30 namespace {
31 constexpr int UID_TEST = 100;
32 constexpr int PID_TEST = 200;
33 const std::string DEVICE_ID_TEST = "deviceidTest";
34 const std::string SESSION_NAME_TEST = "sessionNameTest";
35 const std::string PEER_SESSION_NAME_TEST = "PeersessionNameTest";
36 }
37
38 class DbSessionCallbackUnitTest : public testing::Test {
39 public:
40 static void SetUpTestCase(void);
41 static void TearDownTestCase(void);
42 void SetUp();
43 void TearDown();
44 };
45
SetUpTestCase()46 void DbSessionCallbackUnitTest::SetUpTestCase()
47 {
48 }
49
TearDownTestCase()50 void DbSessionCallbackUnitTest::TearDownTestCase()
51 {
52 }
53
SetUp()54 void DbSessionCallbackUnitTest::SetUp() {}
55
TearDown()56 void DbSessionCallbackUnitTest::TearDown() {}
57
58 /**
59 * @tc.name: OnSessionOpenedTest001
60 * @tc.desc: Verify the OnSessionOpened function
61 * @tc.type: FUNC
62 */
63 HWTEST_F(DbSessionCallbackUnitTest, OnSessionOpenedTest001, TestSize.Level1)
64 {
65 std::shared_ptr<MockSessionImpl> session = std::make_shared<MockSessionImpl>();
66
67 EXPECT_CALL(*session, GetChannelId())
68 .Times(1)
69 .WillOnce(testing::Return(-1));
70
71 DatabusSessionCallback dbSessionCallback;
72 int ret = dbSessionCallback.OnSessionOpened(session);
73 EXPECT_EQ(ret, SESSION_WRONG_FD_ERR);
74 }
75
76 /**
77 * @tc.name: OnSessionOpenedTest002
78 * @tc.desc: Verify the OnSessionOpened function
79 * @tc.type: FUNC
80 */
81 HWTEST_F(DbSessionCallbackUnitTest, OnSessionOpenedTest002, TestSize.Level1)
82 {
83 std::shared_ptr<MockSessionImpl> session = std::make_shared<MockSessionImpl>();
84
85 EXPECT_CALL(*session, GetChannelId())
86 .Times(1)
87 .WillOnce(testing::Return(1));
88
89 EXPECT_CALL(*session, IsServerSide())
90 .Times(1)
91 .WillOnce(testing::Return(false));
92
93 DatabusSessionCallback dbSessionCallback;
94 int ret = dbSessionCallback.OnSessionOpened(session);
95 EXPECT_EQ(ret, 0);
96 }
97
98 /**
99 * @tc.name: OnSessionOpenedTest003
100 * @tc.desc: Verify the OnSessionOpened function
101 * @tc.type: FUNC
102 */
103 HWTEST_F(DbSessionCallbackUnitTest, OnSessionOpenedTest003, TestSize.Level1)
104 {
105 std::shared_ptr<MockSessionImpl> session = std::make_shared<MockSessionImpl>();
106
107 EXPECT_CALL(*session, GetChannelId())
108 .WillRepeatedly(testing::Return(1));
109
110 EXPECT_CALL(*session, IsServerSide())
111 .WillRepeatedly(testing::Return(true));
112
113 EXPECT_CALL(*session, GetPeerPid())
114 .WillRepeatedly(testing::Return(PID_TEST));
115
116 EXPECT_CALL(*session, GetPeerUid())
117 .WillRepeatedly(testing::Return(UID_TEST));
118
119 EXPECT_CALL(*session, GetPeerDeviceId())
120 .WillRepeatedly(testing::ReturnRef(DEVICE_ID_TEST));
121
122 EXPECT_CALL(*session, GetMySessionName())
123 .WillRepeatedly(testing::ReturnRef(SESSION_NAME_TEST));
124
125 EXPECT_CALL(*session, GetPeerSessionName())
126 .WillRepeatedly(testing::ReturnRef(PEER_SESSION_NAME_TEST));
127
128 DatabusSessionCallback dbSessionCallback;
129 int ret = dbSessionCallback.OnSessionOpened(session);
130
131 char data[] = "testdata";
132 ssize_t len = strlen(data);
133 dbSessionCallback.OnBytesReceived(session, data, len);
134 dbSessionCallback.OnSessionClosed(session);
135
136 EXPECT_EQ(ret, SESSION_UNOPEN_ERR);
137 }
138
139 /**
140 * @tc.name: OnSessionOpenedTest004
141 * @tc.desc: Verify the OnSessionOpened function
142 * @tc.type: FUNC
143 */
144 HWTEST_F(DbSessionCallbackUnitTest, OnSessionOpenedTest004, TestSize.Level1)
145 {
146 std::shared_ptr<MockSessionImpl> session = std::make_shared<MockSessionImpl>();
147
148 EXPECT_CALL(*session, GetChannelId())
149 .WillRepeatedly(testing::Return(1));
150
151 EXPECT_CALL(*session, IsServerSide())
152 .WillRepeatedly(testing::Return(true));
153
154 EXPECT_CALL(*session, GetPeerPid())
155 .WillRepeatedly(testing::Return(PID_TEST));
156
157 EXPECT_CALL(*session, GetPeerUid())
158 .WillRepeatedly(testing::Return(UID_TEST));
159
160 EXPECT_CALL(*session, GetPeerDeviceId())
161 .WillRepeatedly(testing::ReturnRef(DEVICE_ID_TEST));
162
163 EXPECT_CALL(*session, GetMySessionName())
164 .WillRepeatedly(testing::ReturnRef(SESSION_NAME_TEST));
165
166 EXPECT_CALL(*session, GetPeerSessionName())
167 .WillRepeatedly(testing::ReturnRef(PEER_SESSION_NAME_TEST));
168
169 IRemoteInvoker *invoker = nullptr;
170 IPCThreadSkeleton *current = IPCThreadSkeleton::GetCurrent();
171 current->invokers_[IRemoteObject::IF_PROT_DATABUS] = invoker;
172
173 DatabusSessionCallback dbSessionCallback;
174 int ret = dbSessionCallback.OnSessionOpened(session);
175
176 EXPECT_EQ(ret, SESSION_INVOKER_NULL_ERR);
177 char data[] = "testdata";
178 ssize_t len = strlen(data);
179 dbSessionCallback.OnBytesReceived(session, data, len);
180 dbSessionCallback.OnSessionClosed(session);
181 }