• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     adaptor->InitAdaptor(channelId, &g_param, false, &g_callback);
79     adaptor->ReleaseAdaptor();
80 }
81 
82 /**
83  * @tc.name: EncryptTest001
84  * @tc.desc: Encrypt error.
85  * @tc.type: FUNC
86  * @tc.require:
87  */
88 HWTEST_F(StreamAdaptorTest, EncryptTest001, TestSize.Level0)
89 {
90     int32_t channelId = 1;
91     StreamData streamData = {
92         (char *)"",
93         0,
94     };
95     std::shared_ptr<StreamAdaptor> adaptor = std::make_shared<StreamAdaptor>(g_pkgName);
96     adaptor->InitAdaptor(channelId, &g_param, true, &g_callback);
97     ssize_t dataLen = streamData.bufLen + adaptor->GetEncryptOverhead();
98     std::unique_ptr<char[]> data = std::make_unique<char[]>(dataLen);
99     int32_t ret = adaptor->Encrypt(streamData.buf, streamData.bufLen, data.get(), dataLen, adaptor->GetSessionKey());
100     EXPECT_EQ(SOFTBUS_ENCRYPT_ERR, ret);
101     ret = adaptor->Decrypt(data.get(), dataLen, streamData.buf, streamData.bufLen, adaptor->GetSessionKey());
102     EXPECT_EQ(SOFTBUS_DECRYPT_ERR, ret);
103     adaptor->ReleaseAdaptor();
104 }
105 
106 /**
107  * @tc.name: EncryptTest002
108  * @tc.desc: Encrypt success, Decrypt error.
109  * @tc.type: FUNC
110  * @tc.require:
111  */
112 HWTEST_F(StreamAdaptorTest, EncryptTest002, TestSize.Level0)
113 {
114     int32_t channelId = 1;
115     StreamData streamData = {
116         (char *)"balabalab\0",
117         STREAM_ADAPT_DATA_LENGTH,
118     };
119     std::shared_ptr<StreamAdaptor> adaptor = std::make_shared<StreamAdaptor>(g_pkgName);
120     adaptor->InitAdaptor(channelId, &g_param, true, &g_callback);
121     ssize_t dataLen = streamData.bufLen + adaptor->GetEncryptOverhead();
122     std::unique_ptr<char[]> data = std::make_unique<char[]>(dataLen);
123     int32_t ret = adaptor->Encrypt(streamData.buf, streamData.bufLen, data.get(), dataLen, adaptor->GetSessionKey());
124     EXPECT_EQ(dataLen, ret);
125     ret = adaptor->Decrypt(data.get(), dataLen + 1, streamData.buf, streamData.bufLen, adaptor->GetSessionKey());
126     EXPECT_EQ(SOFTBUS_ERR, ret);
127     adaptor->ReleaseAdaptor();
128 }
129 }