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 #include <iostream>
16 #include <gtest/gtest.h>
17 
18 #include "session.h"
19 #include "softbus_errcode.h"
20 #include "stream_adaptor.h"
21 
22 using namespace testing::ext;
23 
24 #define STREAM_ADAPT_DATA_LENGTH 10
25 
26 namespace OHOS {
27 class StreamAdaptorTest : public testing::Test {
28 public:
StreamAdaptorTest()29     StreamAdaptorTest()
30     {}
~StreamAdaptorTest()31     ~StreamAdaptorTest()
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 StreamAdaptorTest::SetUpTestCase(void)
42 {}
43 
TearDownTestCase(void)44 void StreamAdaptorTest::TearDownTestCase(void)
45 {}
46 
SetStatus(int channelId,int status)47 void SetStatus(int channelId, int status)
48 {
49     std::cout << "[server]:channelID:" << channelId << ", status:" << status << std::endl;
50 }
51 
52 static IStreamListener g_callback = {
53     .OnStatusChange = SetStatus,
54 };
55 static char g_pkgName[] = "test";
56 static char g_ip[] = "127.0.0.1";
57 static VtpStreamOpenParam g_param = {
58     g_pkgName,
59     g_ip,
60     NULL,
61     -1,
62     RAW_STREAM,
63     (uint8_t*)"abcdef@ghabcdefghabcdefghfgdabc",
64     SESSION_KEY_LENGTH,
65 };
66 
67 /**
68  * @tc.name: InitAdaptorTest001
69  * @tc.desc: InitAdaptor branch test.
70  * @tc.type: FUNC
71  * @tc.require:
72  */
73 HWTEST_F(StreamAdaptorTest, InitAdaptorTest001, TestSize.Level0)
74 {
75     int32_t channelId = 1;
76     std::shared_ptr<StreamAdaptor> adaptor = std::make_shared<StreamAdaptor>(g_pkgName);
77     adaptor->InitAdaptor(channelId, &g_param, true, &g_callback);
78     EXPECT_EQ(adaptor->GetChannelId(), channelId);
79     adaptor->InitAdaptor(channelId, &g_param, false, &g_callback);
80     EXPECT_EQ(adaptor->GetChannelId(), channelId);
81     adaptor->ReleaseAdaptor();
82 }
83 
84 /**
85  * @tc.name: EncryptTest001
86  * @tc.desc: Encrypt error.
87  * @tc.type: FUNC
88  * @tc.require:
89  */
90 HWTEST_F(StreamAdaptorTest, EncryptTest001, TestSize.Level0)
91 {
92     int32_t channelId = 1;
93     StreamData streamData = {
94         (char *)"",
95         0,
96     };
97     std::shared_ptr<StreamAdaptor> adaptor = std::make_shared<StreamAdaptor>(g_pkgName);
98     adaptor->InitAdaptor(channelId, &g_param, true, &g_callback);
99     ssize_t dataLen = streamData.bufLen + adaptor->GetEncryptOverhead();
100     std::unique_ptr<char[]> data = std::make_unique<char[]>(dataLen);
101     int32_t ret = adaptor->Encrypt(streamData.buf, streamData.bufLen, data.get(), dataLen, adaptor->GetSessionKey());
102     EXPECT_EQ(SOFTBUS_ENCRYPT_ERR, ret);
103     ret = adaptor->Decrypt(data.get(), dataLen, streamData.buf, streamData.bufLen, adaptor->GetSessionKey());
104     EXPECT_EQ(SOFTBUS_DECRYPT_ERR, ret);
105     adaptor->ReleaseAdaptor();
106 }
107 
108 /**
109  * @tc.name: EncryptTest002
110  * @tc.desc: Encrypt success, Decrypt error.
111  * @tc.type: FUNC
112  * @tc.require:
113  */
114 HWTEST_F(StreamAdaptorTest, EncryptTest002, TestSize.Level0)
115 {
116     int32_t channelId = 1;
117     StreamData streamData = {
118         (char *)"balabalab\0",
119         STREAM_ADAPT_DATA_LENGTH,
120     };
121     std::shared_ptr<StreamAdaptor> adaptor = std::make_shared<StreamAdaptor>(g_pkgName);
122     adaptor->InitAdaptor(channelId, &g_param, true, &g_callback);
123     ssize_t dataLen = streamData.bufLen + adaptor->GetEncryptOverhead();
124     std::unique_ptr<char[]> data = std::make_unique<char[]>(dataLen);
125     int32_t ret = adaptor->Encrypt(streamData.buf, streamData.bufLen, data.get(), dataLen, adaptor->GetSessionKey());
126     EXPECT_EQ(dataLen, ret);
127     ret = adaptor->Decrypt(data.get(), dataLen + 1, streamData.buf, streamData.bufLen, adaptor->GetSessionKey());
128     EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
129     adaptor->ReleaseAdaptor();
130 }
131 }