1 /*
2  * Copyright (c) 2021 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 
18 #include <iostream>
19 #include "session.h"
20 #include "softbus_errcode.h"
21 
22 using namespace testing::ext;
23 namespace OHOS {
24 static const char *UDP_TEST_PKG_NAME = "com.plrdtest.dsoftbus.server";
25 static const char *UDP_TEST_SESSION_NAME = "com.plrdtest.dsoftbus.JtSendRawStream_0";
26 int32_t g_testWay = 0;
27 class TransQosStatServerTest : public testing::Test {
28 public:
TransQosStatServerTest()29     TransQosStatServerTest()
30     {}
~TransQosStatServerTest()31     ~TransQosStatServerTest()
32     {}
33     static void SetUpTestCase(void);
34     static void TearDownTestCase(void);
SetUp()35     void SetUp() override
36     {}
TearDown()37     void TearDown() override
38     {}
39 };
40 
SetUpTestCase(void)41 void TransQosStatServerTest::SetUpTestCase(void)
42 {
43     printf("********Qos Test Begin*********\r\n");
44     printf("*   0.with onQosEvent  *\r\n");
45     printf("*   1.without onQosEvent   *\r\n");
46     printf("********************************\r\n");
47     printf("input the num:");
48     std::cin >> g_testWay;
49 }
50 
TearDownTestCase(void)51 void TransQosStatServerTest::TearDownTestCase(void)
52 {}
53 
OnSessionOpend(int sessionId,int result)54 static int OnSessionOpend(int sessionId, int result)
55 {
56     printf("on session opened[sessionId = %d, result = %d]\n", sessionId, result);
57     return 0;
58 }
59 
OnSessionClosed(int sessionId)60 static void OnSessionClosed(int sessionId)
61 {
62     printf("on session closed[sessionId = %d]\n", sessionId);
63 }
64 
OnStreamReceived(int sessionId,const StreamData * data,const StreamData * ext,const StreamFrameInfo * param)65 static void OnStreamReceived(int sessionId, const StreamData *data, const StreamData *ext, const StreamFrameInfo *param)
66 {}
67 
OnBytesReceived(int sessionId,const void * data,unsigned int dataLen)68 static void OnBytesReceived(int sessionId, const void *data, unsigned int dataLen)
69 {}
70 
OnMessageReceived(int sessionId,const void * data,unsigned int dataLen)71 static void OnMessageReceived(int sessionId, const void *data, unsigned int dataLen)
72 {}
73 
OnQosEvent(int sessionId,int eventId,int tvCount,const QosTv * tvList)74 static void OnQosEvent(int sessionId, int eventId, int tvCount, const QosTv *tvList)
75 {
76     printf("on QoS metric retrieved [sessionId = %d] [eventId=%d]!!!!!!\n", sessionId, eventId);
77     printf("pktNum:%u\n", tvList->info.appStatistics.pktNum);
78     printf("periodRecvPkts:%u\n", tvList->info.appStatistics.periodRecvPkts);
79     printf("periodRecvPktLoss:%u\n", tvList->info.appStatistics.periodRecvPktLoss);
80     printf("periodRecvRate:%u\n", tvList->info.appStatistics.periodRecvRate);
81     printf("periodRecvRateBps:%" PRIu64 "\n", tvList->info.appStatistics.periodRecvRateBps);
82     printf("periodRtt:%u\n", tvList->info.appStatistics.periodRtt);
83     printf("periodRecvPktLossHighPrecision:%u\n", tvList->info.appStatistics.periodRecvPktLossHighPrecision);
84     printf("periodSendLostPkts:%u\n", tvList->info.appStatistics.periodSendLostPkts);
85     printf("periodSendPkts:%u\n", tvList->info.appStatistics.periodSendPkts);
86     printf("periodSendPktLossHighPrecision:%u\n", tvList->info.appStatistics.periodSendPktLossHighPrecision);
87     printf("periodSendBits:%" PRIu64 "\n", tvList->info.appStatistics.periodSendBits);
88     printf("periodSendRateBps:%" PRIu64 "\n", tvList->info.appStatistics.periodSendRateBps);
89 }
90 
91 static ISessionListener g_hasQosCb = {
92     .OnSessionOpened = OnSessionOpend,
93     .OnSessionClosed = OnSessionClosed,
94     .OnStreamReceived = OnStreamReceived,
95     .OnBytesReceived = OnBytesReceived,
96     .OnMessageReceived = OnMessageReceived,
97     .OnQosEvent = OnQosEvent,
98 };
99 
100 static ISessionListener g_noQosCb = {
101     .OnSessionOpened = OnSessionOpend,
102     .OnSessionClosed = OnSessionClosed,
103     .OnStreamReceived = OnStreamReceived,
104     .OnBytesReceived = OnBytesReceived,
105     .OnMessageReceived = OnMessageReceived,
106     .OnQosEvent = NULL,
107 };
108 
109 /**
110  * @tc.name: TransQosStatServerTest001
111  * @tc.desc: receive with onQosEvent
112  * @tc.type: FUNC
113  * @tc.require:
114  */
115 HWTEST_F(TransQosStatServerTest, QosStatServerTest001, TestSize.Level0)
116 {
117     int32_t ret;
118     if (g_testWay == 0) {
119         ret = CreateSessionServer(UDP_TEST_PKG_NAME, UDP_TEST_SESSION_NAME, &g_hasQosCb);
120     } else {
121         ret = CreateSessionServer(UDP_TEST_PKG_NAME, UDP_TEST_SESSION_NAME, &g_noQosCb);
122     }
123     EXPECT_EQ(ret, SOFTBUS_OK);
124     if (ret == SOFTBUS_OK) {
125         while (1) {
126             sleep(3);
127         }
128     }
129 }
130 } // namespace OHOS